<div dir="ltr">Hi Stephan,<br><br>On Thu, 25 Sep 2014, Stephan Eggermont wrote:<br>&gt; What is the actual status of the PostgresV3 driver? In the last mailing list<br>&gt; discussions about it there was a reference to newer features not yet<br>&gt; on squeaksource?<br><br>We use it daily in our projects, and I would say it’s pretty stable.<br><br>We&#39;ve uploaded the latest version to the squeaksource repository in two forms: as a single package, and as multiple separate packages. The former version will not be updated anymore. The latter consists of the following:<br><br>PostgresV3-Core:<br>This is basically the implementation of the protocol. It enables one to create connections and execute text queries.<br><br>PostgresV3-Pool (depends on Core):<br>This is an implementation of a connection pool. We always use it when we&#39;re connecting to a database. It also gives support for easy transaction handling.<br><br>PostgresV3-CodeMirror (depends on Core and Pool):<br>This is a set of high level tools which we use most of the time. It allows one to map Postgresql functions to Smalltalk methods. One can load, edit and save selected functions of a schema from a class browser. Invoking the smalltalk methods will result in calling the Postgresql functions. This tool has some limitations: only plpgsql functions are supported, some flags can&#39;t be specified (e.g. security definer), no support for inout/out parameters, the return type has to be refcursor, or a basic type.<br><br>PostgresV3-Objects:<br>This is the old way to map rows to objects and cache them. We don&#39;t use it anymore in new projects. The Core has support for custom rowClass per query (you can define what class should be instantiated for the rows returned by the query), which provides better row-object mapping.<br><br>&gt; What kind of advantages should I expect when compared to the dbx stuff? Is there a Glorp driver for it?<br><br>We are not familiar with the Glorp and DBXTalk project, so can’t comment on that.<br><br>As mentioned by Levente in a previous mail we have a rather specific usage pattern:<br>We use stored procedures (written in plpgsql) to communicate with the database.<br><br>There is a class PG3SchemaMirror to aid you with that. You subclass it, and this subclass corresponds to a schema in your database. Its methods correspond to the database functions you want to call from Squeak.<br><br>For example you may have a “users” schema in your database, and a UsersSchemaMirror class in your image, with a method like this:<br><br>authenticate: username password: password<br><br>    &lt;pg3Function: ‘authenticate’<br>        arguments: #(‘_username’ text ‘_password’ text)<br>        returns: #boolean<br>        volatility: #volatile&gt;<br><br>begin<br>    return exists (<br>        select 1<br>        from users.user<br>        where<br>           username = _username and<br>           password_hash = crypt(_password, password_hash));<br>end;<br><br>and then from your code you can call it like:<br><br>(UsersSchemaMirror default authenticate: username password: password)<br>    ifTrue: [ “ authenticated successfully “ ]<br>    ifFalse: [ “authentication failed ” ]<br><br><br>Here is a step-by-step guide:<br><br>First create a subclass of PG3ConnectionPool and implement its defaultConnectionArgument class side method:<br><br>YourConnectionPool class &gt;&gt; defaultConnectionArguments<br><br>   ^PG3ConnectionArguments new<br>      hostname: &#39;127.0.0.1&#39;;<br>      port: 5432;<br>      username: ‘username’;<br>      password: ‘password’;<br>      databaseName: ‘dbname’;<br>      yourself<br><br>now you can execute queries like this:<br><br>(YourConnectionPool default executeQuery: ‘select 3 + 4’)<br>    first “ a query may returns multiple result sets, now we select the first “<br>    rows “ we request the rows of the result set “<br>    first “ then the first row “<br>    first “ and the first column, this returns the number 7 ”<br><br>To create a schema mirror, subclass PG3SchemaMirror and implement its pool and schema class side methods.<br><br>YourSchemaMirror class &gt;&gt; pool<br><br>    ^YourConnectionPool default<br><br><br>YourSchemaMirror class &gt;&gt; schema<br><br>    ^’schema_name’<br><br><br>Schema mirrors mirror the functions in your database and provide an interface to call them. There is a little tool to inspect the differences between the methods of the schema mirror and the functions in the database, which can be invoked by:<br><br>YourSchemaMirror browseChanges<br><br>You can up and download functions via this interface. In earlier times we would write the database functions in pgAdmin3 and download them into the image. Nowadays we use the smalltalk browser to write the plpgsql code. You can set the autocommit behavior with:<br><br>YourSchemaMirror commitOnAccept: true<br><br><br>The method mirroring a database function has three parts:<br> - the method signature,<br> - the pragma describing the database function’s signature,<br> - and the body of the database function.<br><br>There are two types of these functions, one that return simple types (text, numbers, booleans, arrays, …) and the other that return a collection of rows. Let’s take a look at some examples.<br><br>Suppose that you have a table in your schema that lists your plonks, and you want a method that count all the plonks of a specific color, then you may write something like this:<br><br>YourSchemaMirror &gt;&gt; numberOfPlonksColored: color<br><br>    &lt;pg3Function ‘number_of_plonks_colored’<br>        arguments: #(‘_color’ text)<br>        returns: #integer<br>        volatility: #volatile&gt;<br><br>begin<br>    return (<br>        select count(1)<br>        from your.plonk<br>        where color = _color);<br>end;<br><br>The method signature and the arguments specified in the pragma must correspond to each other. The arguments are described with a literal array of even elements. The odd ones are the parameter names, the even ones are the argument types.<br><br>Now you may want to list all your plonks of a color:<br><br>YourSchemaMirror &gt;&gt; plonksColored: color<br><br>    &lt;pg3Function: ‘plonks_colored’<br>        arguments: #(‘ref’ refcursor ‘_color’ text)<br>        returns: #refcursor<br>        volatility: #volatile<br>        rowClass: #Plonk&gt;<br><br>begin<br>    open ref for<br>        select<br>           <a href="http://p.id">p.id</a>,<br>           (select count(1)<br>            from your.griffle g<br>            where g.plonk_id = <a href="http://p.id">p.id</a>) as “griffleCount”<br>        from your.plonk p<br>        where color = _color<br>        order by age desc;<br>    return ref;<br>end;<br><br>Methods returning rows must have the return type refcursor, and also their corresponding database function’s first argument is a refcursor (which you omit from the method signature).<br><br>You may optionally specify a row class name (in our case Plonk) which must be a subclass of PG3Row. The returned objects understand the column names. So you can say:<br><br>(YourSchemaMirror default plonksColored: ‘red’) select: [ :each | each griffleCount &gt; 3 ]<br><br>Using row classes has the benefit that you can implement some behaviour on the returned objects. Eg. you may create a method in Plonk to return all the griffles of a plonk:<br><br><br>Plonk &gt;&gt; griffles<br><br>    ^YourSchemaMirror default grifflesOfPlonk: self id<br><br>Usually we don’t keep these objects around and don’t share them. Most of the time we try to encapsulate database changes into single functions, so consistency is not an issue. Besides plpgsql is a really good language to manipulate a relational database. But using database transactions are straightforward too, just call the pool’s executeTransaction: method with a block. The block may have an argument where it receives the connection, which can be used to rollback the transaction. Eg.:<br><br><br>    YourConnectionPool default executeTransaction: [ :connection |<br>        | redPlonks |<br>        redPlonks := YourSchemaMirror default plonksColored: ‘red’.<br>        redPlonks size &gt; 1 ifTrue: [<br>            | griffle |<br>            griffle := redPlonks first removeGriffle.<br>            redPlonks last addGriffle: griffle.<br>            (redPlonks last griffles count: [ :each | each smell = ‘stinky’ ]) &gt; 2 ifTrue: [<br>               “ the last red plonk has too many stinky griffles. rollback! ”<br>               connection rollback ] ] ]<br><br>That’s it for now. If you have any questions, we are here to help.<br><br>Cheers, Balázs</div>