Re: [PHP-DEV] [PATCH] New PDO methods for PostgreSQL driver

[email protected] (Denis Gasparin) Tue, 25 May 2010 00:04:19 +0200 (CEST)
Newsgroups php.pdo
Message-ID <26184626.34392.1274738658986.JavaMail.root@mailserver.edistar.com>
> Some test or doc would help educate us on the new functionality. 

I'll provide the tests as soon as possible.

> Also I haven't seen anything to explain your earlier comment to Lester that
> "In particular you'll need two database connections (one with the
> original driver and one with PDO) in order to use the native copy*
> functions.".
> 

The old pgsql driver makes a db connection using pg_connect(). After a call to this function a resource handle to the database connection is returned

Passing this handle to pg_copy_from() and pg_copy_to() functions, it is possible to issue the copy from/to commands to PostgreSQL.

Now, if I would like to use pg_copy_from() and pg_copy_to() in a script that is already using a PDO connection i need two database connections:
- one made with PDO ($c = new PDO("...")) 
- one made with pg_connect (to use pg_copy_from() and pg_copy_to()).


> Since PDO development skills tend to be siloed, it really helps to
> explain DB-specific functionality clearly. Where does this 2nd
> connection get used and what's the dependency of pdo_pgsql on the
> non-PDO postgresql driver?
> 


The copy to/from sql statements are used to bulk download and upload data to a PostgreSQL.
They are sensibly faster than a sequence of selects/inserts (also of prepared selects/inserts).

They accept both as main parameter a filename or stdout/stdin respectively.

The filename represents a file in the database filesystem (apache/php and postgresql must reside on the same machine or share the file via shared filesystem). I quote from the postgresql manual:

<<<
COPY with a file name instructs the PostgreSQL server to directly read from or write to a file. The file must be accessible to the server and the name must be specified from the viewpoint of the (database) server.
>>>

Because of this "limitation", if you'd like to load a csv file into pgsql via copy command, you must before copy into a shared folder and the issue the copy from filename command.
Using the methods added to PDO, you can do it directly from the webserver.

About copying to/from stdin and stdout, PostgreSQL enters a particular status after a copy stdin/stdout command has been issued via sql.
These status codes are PGRES_COPY_OUT and PGRES_COPY_IN. When in this status, the only way to interact with the database server is via PQgetCopyData and PQputCopyData.

There are not PDO methods or functions that implements these functions and there is no way to share the connection between PDO driver and old pgsql drivers.

These are the relevant PostgreSQL man pages:

http://www.postgresql.org/docs/8.2/interactive/libpq-copy.html
http://www.postgresql.org/docs/8.2/interactive/sql-copy.html

Denis

> Chris
> 
> 
> Denis Gasparin wrote:
> > I attached the patch as a single file.
> >
> > There are currently no tests written for the new methods.
> >
> > I'll provide them if needed to integrate the patch in the pdo_pgsql
> > driver.
> >
> > Denis
> >
> >
> > ----- Messaggio originale -----
> >> Da: "Denis Gasparin" <[email protected]>
> >> A: "Ilia Alshanetsky" <[email protected]>
> >> Cc: [email protected]
> >> Inviato: Lunedì, 24 maggio 2010 21:45:30
> >> Oggetto: Re: [PHP-DEV] [PATCH] New PDO methods for PostgreSQL
> >> driver
> >
> >> I'll provide the patches in a single file as soon as possible.
> >>
> >> Actually all the methods are wrappers against the native PostgreSQL
> >> commands (connection status, copy to/from).
> >>
> >> I needed to develop them as methods because it is not possible to
> >> get the same results with a sql statement (in particular for
> >> connection status).
> >>
> >> It was not possible also for COPY TO/FROM because of the way
> >> PostgreSQL handle them ( special functions were developed also for
> >> the old pgsql driver).
> >>
> >> We are currently using them in production and we needed them in
> >> order to avoid additional connections to database (one with the old
> >> driver and one with PDO).
> >>
> >> Denis
> >>
> >>
> >> ----- Messaggio originale -----
> >>> Da: "Ilia Alshanetsky" <[email protected]>
> >>> A: "Denis Gasparin" <[email protected]>
> >>> Cc: [email protected]
> >>> Inviato: Lunedì, 24 maggio 2010 19:54:46
> >>> Oggetto: Re: [PHP-DEV] [PATCH] New PDO methods for PostgreSQL
> >>> driver Denis,
> >>>
> >>>
> >>> Could you merge the patches into a single for easier code review.
> >>> Also, the copy to/from file seems like it would just be a wrapper
> >>> against the native COPY PostgreSQL command, is there really a need
> >>> to provide a method for it?
> >>>
> >>>
> >>> On Mon, May 24, 2010 at 4:57 AM, Denis Gasparin <
> >>> [email protected] > wrote:
> >>>
> >>>
> >>> Hi.
> >>>
> >>> I developed some patches for PDO/Postgresql driver in order to add
> >>> some useful methods that were available in the original pgsql
> >>> driver.
> >>>
> >>> The attached patches apply on 5.3.2 and svn 5.3.x.
> >>> If needed, i have patches also for 5.2.x.
> >>>
> >>> Please comment and tell me improvements or tips.
> >>>
> >>> Thank you in advance,
> >>>
> >>> Denis Gasparin
> >>>
> >>> Documentation of the added methods follows:
> >>>
> >>> pgsqlIsInTransaction()
> >>>
> >>> It uses the native Postgresql functions to check transaction
> >>> status..
> >>> It returns one of the following status codes:
> >>> * PDO::PGSQL_TRANSACTION_IDLE: connection in idle status
> >>> * PDO::PGSQL_TRANSACTION_ACTIVE: connection is executing a command
> >>> * PDO::PGSQL_TRANSACTION_INTRANS: connection is idle in a valid
> >>> transaction block
> >>> * PDO::PGSQL_TRANSACTION_INERROR: connection is idle, in a failed
> >>> transaction block
> >>> * PDO::PGSQL_TRANSACTION_UNKNOWN: connection is in a bad status
> >>>
> >>>
> >>>
> >>> pgsqlCopyFromArray($table,Array $data,$delimiter,$null, Array
> >>> $fields)
> >>>
> >>> It uses the native Postgresql copy construct to append $data to
> >>> $table. It returns boolean.
> >>> Parameters: * (mandatory) $table: table to append data to
> >>> * (mandatory) $data: Array of rows with data in table field order
> >>> (or as specified in the $fields array). Fields must be separated
> >>> by $delimiter or by
> >>> postgresql standard \t)
> >>> * $delimiter: alternative delimiter to use in place of the
> >>> standard postgres delimiter ("\t")
> >>> * $null: alternative string to use as null value. Default is "\N"
> >>> * $fields: array with table fields that are specified in $data
> >>> parameter
> >>>
> >>>
> >>>
> >>> pgsqlCopyFromFile($table,$filename,$delimiter,$null,$fields)
> >>>
> >>> It uses the native Postgresql copy construct to append $filename
> >>> contents to $table.
> >>> It returns boolean.
> >>> Parameters: * (mandatory) $table: table to append data to.
> >>> * (mandatory) $filename: file with contents to append to $table.
> >>> See Postgresql documentation for the format.
> >>> * $delimiter: alternative delimiter to use in place of the
> >>> standard postgres delimiter ("\t")
> >>> * $null: alternative string to use as null value. Default is "\N"
> >>> * $fields: array with table fields that are specified in $filename
> >>> file
> >>>
> >>> pgsqlCopyToArray($table,$delimiter,$null,$fields)
> >>>
> >>> It uses the native Postgresql copy construct to retrieve $table
> >>> contents and store them to an array.
> >>> It returns an array of rows or false in case of problems.
> >>> The format of the rows into the array is indicated in the
> >>> $delimiter, $null and $fields parameters.
> >>> Parameters: * (mandatory) $table: table to retrieve data from.
> >>> * $delimiter: alternative delimiter to use in place of the
> >>> standard postgres delimiter ("\t")
> >>> * $null: alternative string to use as null value. Default is "\N"
> >>> * $fields: array with table fields to include in the row of the
> >>> array.
> >>>
> >>>
> >>> pgsqlCopyToFile($table,$filename,$delimiter,$null,$fields)
> >>>
> >>>
> >>> It uses the native Postgresql copy construct to retrieve $table
> >>> contents and store them into a file.
> >>> It returns boolean.
> >>> The format of the rows stored into the file is indicated in the
> >>> $delimiter, $null and $fields parameters.
> >>> Parameters: * (mandatory) $table: table to retrieve data from.
> >>> * (mandatory) $filename: file where to store the contents of the
> >>> table * $delimiter: alternative delimiter to use in place of the
> >>> standard postgres delimiter ("\t")
> >>> * $null: alternative string to use as null value. Default is "\N"
> >>> * $fields: array with table fields to include in the row of the
> >>> array.
> >>>
> >>> -- PHP Internals - PHP Runtime Development Mailing List
> >>> To unsubscribe, visit: http://www.php.net/unsub.php
> >> -- PHP Internals - PHP Runtime Development Mailing List
> >> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> --
> Email: [email protected]
> Tel: +1 650 506 8630
> Blog: http://blogs.oracle.com/opal/
> Free PHP Book: http://tinyurl.com/ugpomhome