Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 0.2.0

Bug #13175 Wrong limit query syntax with some PDO DBMS drivers
Submitted: 2008-02-22 04:36 UTC
From: jfalvarez Assigned: wiesemann
Status: Closed Package: Structures_DataGrid_DataSource_PDO
PHP Version: 5.2.3 OS: Linux
Roadmaps: (Not assigned)    
Subscription  


Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know! Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem : 24 + 46 = ?

 
 [2008-02-22 04:36 UTC] jfalvarez (Juan Felipe Alvarez Saldarriaga)
Description: ------------ The problem is that the _getRecords method from the class Structures_DataGrid_DataSource_PDO have burned the LIMIT/OFFSET ( $query .= ' LIMIT ' . $offset . ', ' . $limit; ) string to be append to the current SQL QUERY, this LIMIT/OFFSET cluase just work for MySQL but not for Postgres (this is what I got when I run the current query "ERROR: LIMIT #,# syntax is not supported HINT: Use separate LIMIT and OFFSET clauses."), I really don't know if PDO set the right LIMIT/OFFSET clause for the current driver, but in my case, doesn't work I always got this error: Warning: array_merge() [function.array-merge]: Argument #2 is not an array in /usr/share/php/Structures/DataGrid/Renderer.php on line 870 This error is un the build method, $chunk variable needs to be an array, an array with all the rows found, and because the LIMIT/OFFSET clause is wrong, then $chunk is equal to NULL. So, this is my patch: --- PDO.old.php 2008-02-21 23:32:17.000000000 -0500 +++ PDO.php 2008-02-21 23:33:55.000000000 -0500 @@ -159,11 +159,34 @@ */ function _getRecords($query, $limit, $offset) { - if (!is_null($limit)) { - $query .= ' LIMIT ' . $offset . ', ' . $limit; - } elseif ($offset > 0) { - $query .= ' LIMIT ' . $offset . ', ' . PHP_INT_MAX; - } + // Set proper LIMIT/OFFSET clause for each PDO driver. + switch ( strtolower( $this->_handle->getAttribute( PDO::ATTR_DRIVER_NAME ) ) ) + { + case "pgsql": + if ( false === is_null( $limit ) ) + { + $query .= " LIMIT {$limit} OFFSET {$offset}"; + } + else if ( $offset > 0 ) + { + $query .= " LIMIT " . PHP_INT_MAX . " OFFSET {$offset}"; + } + + break; + + default: + if ( !is_null( $limit ) ) + { + $query .= ' LIMIT ' . $offset . ', ' . $limit; + } + else if ( $offset > 0 ) + { + $query .= ' LIMIT ' . $offset . ', ' . PHP_INT_MAX; + } + + break; + } + if (($result = $this->_handle->query($query)) !== false) { return $result->fetchAll(PDO::FETCH_ASSOC); } else { Test script: --------------- $objDataGrid =& new Structures_DataGrid( 10 ); $mixReturn = $objDataGrid->bind( "SELECT users.* FROM users", array( "dsn" => "pgsql:host=localhost;port=5432;dbname=my_database;user=my_user;password=my_password" ), "PDO" ); $objDataGrid->addColumn( new Structures_DataGrid_Column( "Nombre", "full_name", null, array( "align" => "center" ) ) ); $objDataGrid->addColumn( new Structures_DataGrid_Column( "Acciones", null, null, array( "align" => "center" ), null, array( "Clients_AdminController", "printActions()" ) ) ); $objDataGrid->render( "Pager" ); $objDataGrid->render(); Expected result: ---------------- Warning: array_merge() [function.array-merge]: Argument #2 is not an array in /usr/share/php/Structures/DataGrid/Renderer.php on line 870 There's not rows on the recordSet.

Comments

 [2008-03-25 03:10 UTC] jfalvarez (Juan Felipe Alvarez Saldarriaga)
Nobody will take this ticket ? Thanks.
 [2008-03-25 08:31 UTC] wiesemann (Mark Wiesemann)
Somebody will take of your report, sure, but this might take some more days. Your patch has two problems: - it doesn't comply to the PEAR Coding Standards - it fixes the problem only for Postgres, while it also needs to be fixed for the other supported DBMS
 [2008-03-25 20:41 UTC] jfalvarez (Juan Felipe Alvarez Saldarriaga)
Great! :) Sorry for the patch, is just an example :). Thanks.
 [2008-03-26 17:12 UTC] wiesemann (Mark Wiesemann)
This bug has been fixed in CVS. If this was a documentation problem, the fix will appear on pear.php.net by the end of next Sunday (CET). If this was a problem with the pear.php.net website, the change should be live shortly. Otherwise, the fix will appear in the package's next release. Thank you for the report and for helping us make PEAR better. --- Test and comments are welcome ...
 [2008-03-26 17:12 UTC] wiesemann (Mark Wiesemann)
(package selection corrected)