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

Request #4379 New DataSource driver (DBQuery)
Submitted: 2005-05-18 12:31 UTC
From: post at mark-wiesemann dot de Assigned: asnagy
Status: Closed Package: Structures_DataGrid
PHP Version: Irrelevant OS: Irrelevant
Roadmaps: (Not assigned)    
Subscription  


 [2005-05-18 12:31 UTC] post at mark-wiesemann dot de
Description: ------------ http://www.markwiesemann.de/php/DBQuery.phps is a new DataSource driver which expects a PEAR::DB object and a query string. Usage example: $dsn = 'mysql://user:pass@localhost/database': $db = DB::connect($dsn); $dg =& new Structures_DataGrid(); $query = 'SELECT * FROM table'; $result = $dg->bind($db, array('query' => $query), 'DBQuery'); $dg->render(); Code for DataSource.php, function _detectSourceType(): // DB query case (strtolower(get_parent_class($source)) == 'db_common'): return 'DBQuery'; // to be replaced by a new constant break; Attention: This is a very first implementation that may not work for every query string. If somebody has a query that fails: please send the query to me.

Comments

 [2005-05-18 18:23 UTC] dufuz
There's no need for a while new container for this, you just make people either pass dsn or a object to the DB on and than check if it's a connection witht he isConnecion function, you can look at how LiveUser does it.
 [2005-05-18 18:24 UTC] dufuz
LiveUser_Admin I mean, in the storage containers.
 [2005-05-18 19:02 UTC] post at mark-wiesemann dot de
Don't know if I got your point right!? You want to add the possibility to pass either a PEAR::DB object or a dsn string as you do it in the init functions of the storage containers in LU_Admin? (Okay, good suggestion.) Or do you call the whole container class in question? (Then more details please.)
 [2005-05-19 17:32 UTC] post at mark-wiesemann dot de
I have implemented Helgi's suggestion and some other small optimizations: http://www.markwiesemann.de/php/DBQuery-new.phps New ways to call the bind function (first param is now the query string): $dg->bind($query, array('connection' => $db)); $dg->bind($query, array('dsn' => $dsn)); New code for DataSource.php: case (is_string($source) && preg_match('#SELECT\s.*\sFROM#is', $source) === 1): return 'DBQuery'; // to be replaced by a new constant break;
 [2005-05-21 06:07 UTC] dufuz
Very good, but you know you do not have to name the option differently, unless you really want to I guess You can do if (isset($dbc)) { if (DB::isConnection($dbc)) { } elseif (is_string($dbc) || is_array($dbc)) { } return false; } return false; Tho just a idea so people do not have to warp this in a array ;) Unless you really want to as said before, maybe you plan on more options, dunno, just letting you know of the possibility.
 [2005-05-21 10:20 UTC] post at mark-wiesemann dot de
You're right, there is no need to use an array. I thought an array would be used in the bind function prototype in DataSource.php but that's not the case. I can't think of more options for this container so there is really no need of using an array. Thanks for the hint, I will change the code (also for the DB_Table container).
 [2005-05-22 14:57 UTC] post at mark-wiesemann dot de
http://www.markwiesemann.de/php/DBQuery-new2.phps contains a bugfix for empty database tables (line 174). I did not remove the need to pass an array to the bind function because in Core.php there is the following line: function bind($rs, $options = array(), $type = null) As this bind function passes $options to the bind functions in the DataSource container classes it makes more sense to not change it there.
 [2005-05-24 09:33 UTC] post at mark-wiesemann dot de
Sorry, but the last version contained a bug: When no row limit is specified, the DB query fetched zero rows. The fixed version can be found here: http://www.markwiesemann.de/php/DBQuery-new3.phps The if statement and the call to $result = $this->_dbc->query() in line 158ff. is the fix for this problem.
 [2005-05-27 15:03 UTC] asnagy
I added this to CVS, please test it.
 [2005-05-29 15:34 UTC] post at mark-wiesemann dot de
The class itself works as expected. But #1: Please don't forget to add the code in _detectSourceType() funtion for the two new containers. But #2: If you use it like this: $dg->bind('xSELECT * FROM test', array('dsn' => $dsn), 'DBQuery'); // note the "x"! => query will fail In this case fetch function from DBQuery will return the result object of DB class. In HTMLTable renderer, getTable calls fetchDataSource() and gets the PEAR::Error object. This object is returned to toHTML function. A solution for this would be to change line 246 (return $table->toHTML();) to: if (PEAR::isError($table)) { return $table; } else { return $table->toHTML(); } The same applies for the other renderers. But if one calls directly $dg->render() (contains echo $this->toHTML();) it would now echo "Object". Maybe render() function should also get a check like the one above, e.g: $html = $this->toHTML(); if (PEAR::isError($html)) { return $html; } else { echo $html; return true; } This way one can check the returned value from $dg->render() and can display an error message. If you agree on these two changes I can provide a patch for all renderers.
 [2005-10-31 19:38 UTC] olivierg at php dot net
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. Excellent Job ! You just forgot to call DataSource::setOptions() from DataSource_DBQuery::bind(). That caused general datasource options (such as 'generate_columns', 'fields', etc...) to be ignored. I fixed this little bug (and improved the Driver Howto, mentionning this point). The DataSource core now includes the detection routine you provided, and the DATAGRID_SOURCE_DBQUERY constant has been added. Since, the fetchDataSource() calls have been transformed into a single call performed by the Core, there's no need to tweak the Renderers anymore. If the query fails the bind() method will return a PEAR_Error. I have done basic testing on the new CVS code and it seems to work fine. Thanks !
 [2005-11-21 18:26 UTC] post at mark-wiesemann dot de
Reopening, as the file DataSource/DBQuery.php is missing in package.xml.
 [2005-12-13 12:18 UTC] 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.