Database_Statement Class Reference

Inherited by DB_Mysql_Statement.

List of all members.

Public Member Functions

  • __construct ($resConnection, $blnDisableAutocommit=false)
    Validate the connection resource and store the query.
  • __get ($strKey)
    Return a parameter.
  • prepare ($strQuery)
    Prepare a statement.
  • db_input ($arrParams)
    Retocompatibilté by oscss.
  • set ($arrParams)
    Take an associative array and autogenerate the SET/VALUES subpart of a query.
  • limit ($intRows, $intOffset=0)
    Limit the current result to a certain number of rows and take an offset value as second argument.
  • execute ()
    Escape parameters and execute the current statement.
  • executeUncached ()
    Execute the current statement but do not cache the result.
  • query ($strQuery='')
    Execute a query and return the result object.
  • explain ()
    Explain the current query.

Protected Member Functions

Protected Attributes

Static Protected Attributes


Detailed Description

Definition at line 272 of file Database.php.


Constructor & Destructor Documentation

Database_Statement::__construct ( resConnection,
blnDisableAutocommit = false 
)

Validate the connection resource and store the query.

Parameters:
resource 
boolean 
Exceptions:
Exception 

Definition at line 312 of file Database.php.

00313   {
00314           if (!is_resource($resConnection) && !is_object($resConnection))
00315           {
00316                   throw new Exception('Invalid connection resource');
00317           }
00318 
00319           $this->resConnection = $resConnection;
00320           $this->blnDisableAutocommit = $blnDisableAutocommit;
00321   }


Member Function Documentation

Database_Statement::__get ( strKey  ) 

Return a parameter.

Supported parameters:

  • query: current query string
  • error: last error message
  • affectedRows: number of affected rows
  • insertId: last insert ID

Throw an exception on requests for protected properties.

Parameters:
string 
Returns:
mixed

Definition at line 337 of file Database.php.

00338   {
00339           switch ($strKey)
00340           {
00341                   case 'query':
00342                           return $this->strQuery;
00343                           break;
00344 
00345                   case 'error':
00346                           return $this->get_error();
00347                           break;
00348 
00349                   case 'affectedRows':
00350                           return $this->affected_rows();
00351                           break;
00352 
00353                   case 'insertId':
00354                           return $this->insert_id();
00355                           break;
00356 
00357                   default:
00358                           return null;
00359                           break;
00360           }
00361   }

Database_Statement::affected_rows (  )  [abstract, protected]

Reimplemented in DB_Mysql_Statement.

Referenced by __get().

Database_Statement::db_input ( arrParams  ) 

Retocompatibilté by oscss.

Definition at line 406 of file Database.php.

00406                                        {
00407     return $this->escapeParams($arrParams);
00408   }

Database_Statement::debugQuery ( objResult = false  )  [protected]

Debug a query.

Parameters:
object 

Definition at line 624 of file Database.php.

Referenced by query().

00625   {
00626           if (!_cst_bool('OSCSS_DEBUG'))
00627           {
00628                   return;
00629           }
00630 
00631           $arrData[] = $this->strQuery;
00632 
00633           if (!$objResult || strncmp(strtoupper($this->strQuery), 'SELECT', 6) !== 0)
00634           {
00635                   $arrData[] = sprintf('%d rows affected', $this->affectedRows);
00636                   $GLOBALS['TL_DEBUG'][] = $arrData;
00637 
00638                   return;
00639           }
00640 
00641           $arrData[] = sprintf('%s rows returned', $objResult->numRows);
00642 
00643           if (($arrExplain = $this->explain()) != false)
00644           {
00645                   $arrData[] = $arrExplain;
00646           }
00647 
00648           $GLOBALS['TL_DEBUG'][] = $arrData;
00649   }

Database_Statement::escapeParams ( arrParams  )  [protected]

Escape parameters and serialize objects and arrays.

Parameters:
array 
Returns:
array

Definition at line 589 of file Database.php.

Referenced by db_input(), replaceWildcards(), and set().

00590   {
00591           foreach ($arrParams as $k=>$v)
00592           {
00593                   switch (gettype($v))
00594                   {
00595                           case 'string':
00596                                   $arrParams[$k] = $this->string_escape($v);
00597                                   break;
00598 
00599                           case 'boolean':
00600                                   $arrParams[$k] = ($v === true) ? 1 : 0;
00601                                   break;
00602 
00603                           case 'object':
00604                                   $arrParams[$k] = $this->string_escape(serialize($v));
00605                                   break;
00606 
00607                           case 'array':
00608                                   $arrParams[$k] = $this->string_escape(serialize($v));
00609                                   break;
00610 
00611                           default:
00612                                   $arrParams[$k] = ($v === NULL) ? 'NULL' : $v;
00613                                   break;
00614                   }
00615           }
00616 
00617           return $arrParams;
00618   }

Database_Statement::execute (  ) 

Escape parameters and execute the current statement.

Returns:
object
Exceptions:
Exception 

Definition at line 471 of file Database.php.

00472   {
00473           $arrParams = func_get_args();
00474 
00475           if (is_array($arrParams[0]))
00476           {
00477                   $arrParams = array_values($arrParams[0]);
00478           }
00479 
00480           $this->replaceWildcards($arrParams);
00481           $strKey = md5($this->strQuery);
00482 
00483           // Try to load result from cache
00484           if (isset(self::$arrCache[$strKey]) && !self::$arrCache[$strKey]->isModified)
00485           {
00486                   return self::$arrCache[$strKey]->reset();
00487           }
00488 
00489           $objResult = $this->query();
00490 
00491           // Cache result objects
00492           if ($objResult instanceof Database_Result)
00493           {
00494                   self::$arrCache[$strKey] = $objResult;
00495           }
00496 
00497           return $objResult;
00498   }

Database_Statement::execute_query (  )  [abstract, protected]

Reimplemented in DB_Mysql_Statement.

Referenced by query().

Database_Statement::executeUncached (  ) 

Execute the current statement but do not cache the result.

Returns:
object
Exceptions:
Exception 

Definition at line 506 of file Database.php.

00507   {
00508           $arrParams = func_get_args();
00509 
00510           if (is_array($arrParams[0]))
00511           {
00512                   $arrParams = array_values($arrParams[0]);
00513           }
00514 
00515           $this->replaceWildcards($arrParams);
00516           return $this->query();
00517   }

Database_Statement::explain (  ) 

Explain the current query.

Returns:
int

Definition at line 656 of file Database.php.

Referenced by debugQuery().

00656                             {
00657           return $this->explain_query();
00658   }

Database_Statement::explain_query (  )  [abstract, protected]

Reimplemented in DB_Mysql_Statement.

Referenced by explain().

Database_Statement::get_error (  )  [abstract, protected]

Reimplemented in DB_Mysql_Statement.

Referenced by __get().

Database_Statement::insert_id (  )  [abstract, protected]

Reimplemented in DB_Mysql_Statement.

Referenced by __get().

Database_Statement::limit ( intRows,
intOffset = 0 
)

Limit the current result to a certain number of rows and take an offset value as second argument.

Parameters:
int 
int 
Returns:
object

Definition at line 459 of file Database.php.

00460   {
00461           $this->limit_query($intRows, (($intOffset >= 0) ? $intOffset : 0));
00462           return $this;
00463   }

Database_Statement::limit_query ( intOffset,
intRows 
) [abstract, protected]

Reimplemented in DB_Mysql_Statement.

Referenced by limit().

Database_Statement::prepare ( strQuery  ) 

Prepare a statement.

Parameters:
string 
Returns:
object
Exceptions:
Exception 

Definition at line 370 of file Database.php.

00371   {
00372           if (!strlen($strQuery))
00373           {
00374                   throw new Exception('Empty query string');
00375           }
00376 
00377           $this->resResult = NULL;
00378           $this->strQuery = $this->prepare_query($strQuery);
00379 
00380           // Autogenerate SET/VALUES subpart
00381           if (in_array(substr(strtoupper($this->strQuery), 0, 6), array('INSERT', 'UPDATE')))
00382           {
00383                   $this->strQuery = str_replace('%s', '%p', $this->strQuery);
00384           }
00385 
00386           // Replace wildcards
00387           $arrChunks = preg_split("/('[^']*')/", $this->strQuery, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
00388 
00389           foreach ($arrChunks as $k=>$v)
00390           {
00391                   if (substr($v, 0, 1) == "'")
00392                   {
00393                           continue;
00394                   }
00395 
00396                   $arrChunks[$k] = str_replace('?', '%s', $v);
00397           }
00398 
00399           $this->strQuery = implode('', $arrChunks);
00400           return $this;
00401   }

Database_Statement::prepare_query ( strQuery  )  [abstract, protected]

Abstract database driver methods.

Reimplemented in DB_Mysql_Statement.

Referenced by prepare().

Database_Statement::query ( strQuery = ''  ) 

Execute a query and return the result object.

Parameters:
string 
Returns:
object
Exceptions:
Exception 

Definition at line 526 of file Database.php.

Referenced by execute(), and executeUncached().

00527   {
00528           if (!empty($strQuery))
00529           {
00530                   $this->strQuery = $strQuery;
00531           }
00532 
00533           // Make sure there is a query string
00534           if ($this->strQuery == '')
00535           {
00536                   throw new Exception('Empty query string');
00537           }
00538 
00539           // Execute the query
00540           if (($this->resResult = $this->execute_query()) == false)
00541           {
00542                   throw new Exception(sprintf('Query error: %s (%s)', $this->error, $this->strQuery));
00543           }
00544 
00545           // No result set available
00546           if (!is_resource($this->resResult) && !is_object($this->resResult))
00547           {
00548 
00549                   $this->debugQuery();
00550                   return ($this->resResult===false)? false : $this;
00551 //                return $this;
00552           }
00553 
00554           // Instantiate a result object
00555           $strClass = DB_DRIVER . '_Result';
00556           $objResult = new $strClass($this->resResult, $this->strQuery);
00557           $this->debugQuery($objResult);
00558 
00559           return $objResult;
00560   }

Database_Statement::replaceWildcards ( arrParams  )  [protected]

Build the query string.

Parameters:
array 
Exceptions:
Exception 

Definition at line 568 of file Database.php.

Referenced by execute(), and executeUncached().

00569   {
00570           $arrParams = $this->escapeParams($arrParams);
00571 
00572           // Clean wildcards
00573           $this->strQuery = preg_replace('/%([^bcdufosxX])/', '%%$1', $this->strQuery);
00574           $this->strQuery = preg_replace('/%%+/', '%%', $this->strQuery);
00575 
00576           // Replace wildcards
00577           if (($this->strQuery = @vsprintf($this->strQuery, $arrParams)) == false)
00578           {
00579                   throw new Exception('Too few arguments to build the query string');
00580           }
00581   }

Database_Statement::set ( arrParams  ) 

Take an associative array and autogenerate the SET/VALUES subpart of a query.

Usage example: $objStatement->prepare("UPDATE table %s")->set(array('id'=>'my_id')); will be transformed into "UPDATE table SET id='my_id'".

Parameters:
array 
Returns:
object

Definition at line 419 of file Database.php.

00420   {
00421           $arrParams = $this->escapeParams($arrParams);
00422 
00423           if (strpos($this->strQuery, '%s') < 0)
00424           {
00425                   return $this;
00426           }
00427 
00428           $strType = strtoupper(preg_replace('/\s+.*$/i', '', trim($this->strQuery)));
00429 
00430           switch ($strType)
00431           {
00432                   case 'INSERT':
00433                           $strQuery = sprintf('(%s) VALUES (%s)',
00434                                                                   implode(', ', array_keys($arrParams)),
00435                                                                   str_replace('%', '%%', implode(', ', array_values($arrParams))));
00436                           break;
00437 
00438                   case 'UPDATE':
00439                           $arrSet = array();
00440                           foreach ($arrParams as $k=>$v)
00441                           {
00442                                   $arrSet[] = $k . '=' . $v;
00443                           }
00444                           $strQuery = 'SET ' . str_replace('%', '%%', implode(', ', $arrSet));
00445                           break;
00446           }
00447 
00448           $this->strQuery = str_replace('%p', $strQuery, $this->strQuery);
00449           return $this;
00450   }

Database_Statement::string_escape ( strString  )  [abstract, protected]

Reimplemented in DB_Mysql_Statement.

Referenced by escapeParams().


Member Data Documentation

Database_Statement::$arrCache = array() [static, protected]

Definition at line 303 of file Database.php.

Database_Statement::$blnDisableAutocommit = false [protected]

Definition at line 297 of file Database.php.

Referenced by __construct().

Database_Statement::$resConnection [protected]

Definition at line 279 of file Database.php.

Referenced by __construct().

Database_Statement::$resResult [protected]

Definition at line 285 of file Database.php.

Database_Statement::$strQuery [protected]

Definition at line 291 of file Database.php.

Referenced by prepare(), DB_Mysql_Statement::prepare_query(), query(), and set().


The documentation for this class was generated from the following file: