Source for file dbapi.abstract.class.inc.php
Documentation is available at dbapi.abstract.class.inc.php
* The constructor. Can optionally have the database details passed to it, or alternatively it can use the globals (using the globals is deprecated functionality).
* <<<< TODO throw E_USER_DEPRECATED after ensuring that Install and DocumentParser do not use the globals.
* @param $parent Parent object e.g. $modx or $install
* @param $host db hostname
* @param $dbase db schema name
* @param $uid db username
* @param $pwd db password
* @param $pre table prefix
* @param $charset client character set
* @param $connection SQL to set connection character set
final public function __construct($parent, $host =
'', $dbase =
'', $uid =
'',$pwd =
'', $pre =
null, $charset =
'', $connection_method =
'SET CHARACTER SET') {
$this->config['host'] =
$host ?
$host :
$GLOBALS['database_server'];
$this->config['dbase'] =
$dbase ?
$dbase :
$GLOBALS['dbase'];
$this->config['user'] =
$uid ?
$uid :
$GLOBALS['database_user'];
$this->config['pass'] =
$pwd ?
$pwd :
$GLOBALS['database_password'];
$this->config['charset'] =
$charset ?
$charset :
$GLOBALS['database_connection_charset'];
$this->config['connection_method'] =
$this->_dbconnectionmethod =
(isset
($GLOBALS['database_connection_method']) ?
$GLOBALS['database_connection_method'] :
$connection_method);
$this->config['table_prefix'] =
($pre !==
NULL) ?
$pre :
$GLOBALS['table_prefix']; // Not currently used by the DBAPI. Used just to store the value.
* Called in the constructor to set up arrays containing the types
* of database fields that can be used with specific PHP types.
* Connect to the database.
* Can optionally have the database details passed to it but this is deprecated functionality. Pass the details in the constructor instead.
* @param $host db hostname
* @param $dbase db schema name
* @param $uid db username
* @param $pwd db password
* @param $persist If true, make a persistent connection.
final public function connect($host =
'', $dbase =
'', $uid =
'', $pwd =
'', $persist =
false) {
$uid =
$uid ?
$uid :
$this->config['user'];
$pwd =
$pwd ?
$pwd :
$this->config['pass'];
$host =
$host ?
$host :
$this->config['host'];
$dbase =
str_replace('`', '', $dbase ?
$dbase :
$this->config['dbase']);
$charset =
$this->config['charset'];
$connection_method =
$this->config['connection_method'];
$tstart =
$this->parent->getMicroTime();
$this->parent->messageQuit('Failed to create the database connection!');
$this->parent->messageQuit("Failed to select the database '$dbase'!");
@$this->query("{
$connection_method} {
$charset}"); // We should be able to remove this and it's associated functionality
$tend =
$this->parent->getMicroTime();
$totaltime =
$tend -
$tstart;
if ($this->parent->dumpSQL) {
$this->parent->queryCode .=
"<fieldset style='text-align:left'><legend>Database connection</legend>" .
sprintf("Database connection was created in %2.4f s", $totaltime) .
"</fieldset><br />";
$this->isConnected =
true;
$this->parent->queryTime +=
$totaltime;
* Test database connection or selection
* Intended for installer use only.
* Does not set character set of connection.
* Will return false on failure and will not log errors or display any errors via DocumentParser::MessageQuit().
* @param $host db hostname
* @param $dbase Optional db schema name
* @param $uid db username
* @param $pwd db password
* @param $query Optional query to run
function test_connect($host =
'', $dbase =
'', $uid =
'', $pwd =
'', $query =
'') {
$uid =
$uid ?
$uid :
$this->config['user'];
$pwd =
$pwd ?
$pwd :
$this->config['pass'];
$host =
$host ?
$host :
$this->config['host'];
if ($this->conn &&
!empty($dbase)) {
$dbase =
str_replace('`', '', $dbase ?
$dbase :
$this->config['dbase']);
if ($this->conn &&
!empty($query)) {
$output =
$this->query($query, true);
* Make a persistent connection to the database.
$this->connect('', '', '', '', true);
if (empty ($this->conn)) {
* Connect to the RDBMS persistently.
* Set connection character set
abstract protected function select_db($dbname);
final public function escape($s) {
* Developers should use select, update, insert (etc), delete where possible
* @param bool $suppress_errors If true, return false on error, otherwise quit via MessageQuit().
final public function query($sql, $suppress_errors =
false) {
$tstart =
$this->parent->getMicroTime();
if (!$result =
@$this->_query($sql, $this->conn)) {
$this->parent->messageQuit("Execution of a query to the database failed - " .
$this->getLastError(), $sql);
$tend =
$this->parent->getMicroTime();
$totaltime =
$tend -
$tstart;
$this->parent->queryTime =
$this->parent->queryTime +
$totaltime;
if ($this->parent->dumpSQL) {
$this->parent->queryCode .=
"<fieldset style='text-align:left'><legend>Query " .
($this->executedQueries +
1) .
" - " .
sprintf("%2.4f s", $totaltime) .
"</legend>" .
$sql .
"</fieldset><br />";
$this->parent->executedQueries =
$this->parent->executedQueries +
1;
* @param string|array$fields
final public function delete($from, $where =
'', $fields =
'') {
$where =
($where !=
'') ?
"WHERE $where" :
'';
return $this->query("DELETE $fields FROM $from $where");
* @param string|array$fields
final public function select($fields =
"*", $from =
'', $where =
'', $orderby =
'', $limit =
'') {
$where =
($where !=
'') ?
"WHERE $where" :
'';
$orderby =
($orderby !=
'') ?
"ORDER BY $orderby " :
'';
$limit =
($limit !=
'') ?
"LIMIT $limit" :
'';
return $this->query("SELECT $fields FROM $from $where $orderby $limit");
* @param string|array$fields
final public function update($fields, $table, $where =
'') {
foreach ($fields as $key =>
$value) {
if (!empty ($flds)) $flds .=
',';
$flds .=
"$key = '$value'";
$where =
($where !=
'') ?
"WHERE $where" :
'';
return $this->query("UPDATE $table SET $flds $where");
* @param string|array$fields
* @param string $intotable
* @param string|array$fromfields
* @param string $fromtable
* @return mixed Either last id inserted (if supported) or the result from the query
final public function insert($fields, $intotable, $fromfields =
"*", $fromtable =
'', $where =
'', $limit =
'') {
return $this->__insert('INSERT', $fields, $intotable, $fromfields, $fromtable, $where, $limit);
* @param string|array$fields
* @param string $intotable
* @param string|array$fromfields
* @param string $fromtable
* @return mixed Either last id inserted (if supported) or the result from the query
public function insert_ignore($fields, $intotable, $fromfields =
"*", $fromtable =
'', $where =
'', $limit =
'') {
return $this->__insert('INSERT IGNORE', $fields, $intotable, $fromfields, $fromtable, $where, $limit);
* @param string|array$fields
* @param string $intotable
* @param string|array$fromfields
* @param string $fromtable
* @return mixed Either last id inserted (if supported) or the result from the query
public function replace($fields, $intotable, $fromfields =
"*", $fromtable =
'', $where =
'', $limit =
'') {
return $this->__insert('REPLACE', $fields, $intotable, $fromfields, $fromtable, $where, $limit);
* Internal private insert function for use by the above.
private function __insert($insert_method, $fields, $intotable, $fromfields =
"*", $fromtable =
'', $where =
'', $limit =
'') {
$flds =
'('.
implode(',', $keys).
') '.
(!$fromtable &&
$values ?
'VALUES(\''.
implode('\',\'', $values).
'\')' :
'');
$where =
($where !=
'') ?
"WHERE $where" :
'';
$limit =
($limit !=
'') ?
"LIMIT $limit" :
'';
$sql =
"SELECT $fromfields FROM $fromtable $where $limit";
$rt =
$this->query("$insert_method $intotable $flds $sql");
return $lid ?
$lid :
$rt;
* Get the number of affected rows.
abstract public function getLastError($return_number =
false);
* Get the number of rows in a resultset. Return 0 if resultset invalid.
* @param resource $rs Resultset
* Return an array of column values
* @param resource $rs Resultset
* @param string $mode 'assoc', 'num' or 'both'.
final public function getRow($rs, $mode =
'assoc') {
} elseif ($mode ==
'num') {
} elseif ($mode ==
'both') {
$this->parent->messageQuit("Unknown get type ($mode) specified for getRow - must be empty, 'assoc', 'num' or 'both'.");
* Returns an array of the values found on column $name
* @param string $name Column name
* @param mixed $rsq Resultset or query string
final public function getColumn($name, $rsq) {
$rsq =
$this->query($rsq);
while ($row =
$this->getRow($rsq)) {
* Returns an array containing the column names in a resultset.
* @param mixed $rsq Resultset or query string
$rsq =
$this->query($rsq);
* Returns the value from the first column in the set.
* @param mixed $rsq Resultset or query string
$rsq =
$this->query($rsq);
$r =
$this->getRow($rsq, 'num');
* Returns an XML representation of the dataset $rsq
* @param mixed Resultset or query string
final public function getXML($rsq) {
$rsq =
$this->query($rsq);
$xmldata =
"<xml>\r\n<recordset>\r\n";
while ($row =
$this->getRow($rsq, 'assoc')) {
$xmldata .=
"<item>\r\n";
for ($j =
0; $line =
each($row); $j++
) {
$xmldata .=
"<{$line['key']}>{$line['value']}</{$line['key']}>\r\n";
$xmldata .=
"</item>\r\n";
$xmldata .=
"</recordset>\r\n</xml>";
* Returns an array of structure detail for each column of a
* @param string $table The full name of the database table
* Returns a string containing the database server version
* Free memory associated with a resultset
* Prepares a date in the proper format for specific database types given a UNIX timestamp
* @param int $timestamp: a UNIX timestamp
* @param string $fieldType: the type of field to format the date for
* (in MySQL, you have DATE, TIME, YEAR, and DATETIME)
public function prepareDate($timestamp, $fieldType =
'DATETIME') {
if (!$timestamp ===
false &&
$timestamp >
0) {
$date =
date('Y-m-d', $timestamp);
$date =
date('H:i:s', $timestamp);
$date =
date('Y', $timestamp);
$date =
date('Y-m-d H:i:s', $timestamp);
* @param string|resource$rsq Resultset or SQL query
* @param array $params Data grid parameters
$rsq =
$this->query($rsq);
require_once(dirname(__FILE__
).
'../includes/controls/datagrid.class.php');
$grd =
new DataGrid('', $rsq);
$grd->noRecordMsg =
$params['noRecordMsg'];
$grd->columnHeaderClass =
$params['columnHeaderClass'];
$grd->cssClass =
$params['cssClass'];
$grd->itemClass =
$params['itemClass'];
$grd->altItemClass =
$params['altItemClass'];
$grd->columnHeaderStyle =
$params['columnHeaderStyle'];
$grd->cssStyle =
$params['cssStyle'];
$grd->itemStyle =
$params['itemStyle'];
$grd->altItemStyle =
$params['altItemStyle'];
$grd->columns =
$params['columns'];
$grd->fields =
$params['fields'];
$grd->colWidths =
$params['colWidths'];
$grd->colAligns =
$params['colAligns'];
$grd->colColors =
$params['colColors'];
$grd->colTypes =
$params['colTypes'];
$grd->colWraps =
$params['colWraps'];
$grd->cellPadding =
$params['cellPadding'];
$grd->cellSpacing =
$params['cellSpacing'];
$grd->header =
$params['header'];
$grd->footer =
$params['footer'];
$grd->pageSize =
$params['pageSize'];
$grd->pagerLocation =
$params['pagerLocation'];
$grd->pagerClass =
$params['pagerClass'];
$grd->pagerStyle =
$params['pagerStyle'];
* Turns a recordset into a multidimensional array
* @param resource $rs Resultset
* @return mixed An array of row arrays from recordset, or empty array if
* the recordset was empty, returns false if no recordset
while ($row =
$this->getRow($rs)) {
* Is a variable a resultset handle?
* Test for presence of Clipper db tables
// -------------------------------------------
// LOW LEVEL RBDMS-SPECIFIC INTERNAL FUNCTIONS
// -------------------------------------------
abstract protected function _escape($s);
abstract protected function _query($sql);
* Get the last insert ID.
* Get the number of records in the resultset.
* Get the column names in a resultset
* @param mixed $rs resultset
* Get a row into an associative array.
* Get a row into a numeric array.
* Get a row into both an associative and numeric array.
Documentation generated on Fri, 21 Jun 2013 12:37:00 +0100 by phpDocumentor 1.4.4