Source for file Query.class.php
Documentation is available at Query.class.php
* Gumbo Library Framework
* This library is being released under the terms of the New BSD License. A
* copy of the license is packaged with the software (LICENSE.txt). If no
* copy is found, a copy of the license template can be found at:
* http://www.opensource.org/licenses/bsd-license.php
* @copyright Copyright (c) 2007, iBayou, Michael Luster
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @author Michael Luster <mluster79@yahoo.com>
* @link http://sourceforge.net/projects/phpgumbo
* This class was designed to provide a central location for executing common
* queries (select,insert,update,delete) on a database. The current implementation
* works with a PDO object. Once the PDO object is created, it is assigned to
* "Is this abstracting the abstraction layer?"
* Probably. This is meant to wrap specific methods inside various DB classes
* that abstract database functionality (PDO, ADODB, PEAR_DB, Zend_DB, etc.). The
* problem was switching between them. Each perform the same tasks, except the
* interfaces are different. Therefore, having to switch between ADODB and PDO could
* become a large chore. Imagine searching through thousands of lines of code to
* find the ADODB calls and switching to PDO. (I've had to do it twice on the same
* project, it sucks). By using this class, I was able to make changes to the DB
* system in one place, while the application code remained untouched.
* This class is not going to be further extended. There will not be a Gumbo_Query_PDO,
* Gumbo_Query_ADODB, etc. Since a large majority of applications will use one
* database connection, I feel it's better just to have one implementation. In order
* to use other DB systems, simply change the implementation for your
* specific needs (place inside the 'Custom' directory). The system will load the
* 'custom' class first, ignoring this one. Since there is only one, any changes
* would be made to the implementation, not the application.
* (This topic is certainly up for debate, so feel free to post your opinion on the
* SourceForge Project Forum)
* @copyright Copyright (c) 2007, iBayou, Michael Luster
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @author Michael Luster <mluster79@yahoo.com>
* @link http://sourceforge.net/projects/phpgumbo
gumbo_load ("Interface_Singleton");
class Gumbo_Query implements Gumbo_Interface_Singleton {
/** @var Gumbo_Interface_Query $_private */
private static $_instance =
null;
/** @var mixed $_db Database Abstraction Object (default implementation for PDO) */
/** @var string $_token replacement token character for the prepare method */
if (self::$_instance ==
null) {
self::$_instance =
new Gumbo_Query ();
* Returns an array of Items with the results of a "SELECT ... " query
* @param string $sql query to execute
* @param array $params token parameter values
* @throws Gumbo_Exception
public static function results ($sql, $params=
null) {
// execute query, return results as associative array
$e->setFunction (__METHOD__
);
* Executes the given query and returns the results
* This is a Bridge between the DB interface and the Item class. It
* will execute a sinle query and return the results. If a SELECT
* is performed, then a single associative array representing a
* record is returned. If an INSERT is performed, then the new insert
* ID is returned. UPDATE and DELETE queries will not return anything.
* @param array $params list of parameters for the prepare method
* @param string $name sequence/column name for returning lastInsertId()
* @throws Gumbo_Exception
* @todo implementation for DB classes
public static function execute ($sql, $params=
null, $name=
null) {
if (is_null ($params) ||
($params ==
false)) { $params =
array (); }
// check for a SELECT query
if ($sql_type ==
"select") {
if (!$sql) { return null; }
// check for an INSERT query
if ($sql_type ==
"insert") {
// @todo execute query, return new ID
// check for an UPDATE or DELETE query
if ($sql_type ==
"update" ||
$sql_type ==
"delete") {
// @todo execute query, return affected rows
// @todo execute query, return results
$e->setFunction (__METHOD__
);
* Prepares a query by substituting a token character with parameter values
* @param array $params list of parameters
* @param string $token replacement token character
* @throws Gumbo_Exception
public static function prepare ($sql, $params=
null, $token=
null) {
if (is_null ($params) ||
($params ==
false)) { $params =
array (); }
if (count ($params) >
0) {
throw
new Gumbo_Exception ("Invalid Token Countfor: {$sql} for " .
count ($params) .
" Parameters");
foreach ($tokens as $key=>
$val) {
if (isset
($params [$key])) {
if (is_numeric ($params [$key])) { $sql .=
$params [$key]; }
$e->setFunction (__METHOD__
);
* Sets the DB Object Reference
public function setDb ($db) {
* Sets the token character
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
* Returns the DB Resource or Object
public static function getDb () {
* Returns the token character