Source for file base_db.class.inc.php
Documentation is available at base_db.class.inc.php
* This file is part of Group-Office. You should have received a copy of the
* Group-Office license along with Group-Office. See the file /LICENSE.TXT
* If you have questions write an e-mail to info@intermesh.nl
* @copyright Copyright Intermesh
* @version $Id: about.php 1088 2008-10-07 13:02:06Z mschering $
* @author Merijn Schering <mschering@intermesh.nl>
* Class that connects to MySQL using the MySQLi extension
* @version $Id: imap.class.inc 1201 2008-10-22 18:23:34Z mschering $
* @author Merijn Schering <mschering@intermesh.nl>
* Set to true for debugging messages.
* "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore errror, but spit a warning)
* The sequence table to use for autoincrementing numbers.
* The current record from a select query
* The current row index when waling through a result set
* Type of database connector
* The database connection link identifier
* The result object from a query
* True when a table is locked
* Constructor a config object with db_host, db_pass, db_user and db_name
* may be passed so it can connect to a different database then the default.
* @param unknown_type $config
* Set's the connection parameters. A config object with db_host, db_pass, db_user and db_name
* may be passed so it can connect to a different database then the default.
if(!isset ($config) && isset ($GO_CONFIG))
if (isset ($config->db_host)) {
$this->host = $config->db_host;
if (isset ($config->db_name)) {
if (isset ($config->db_user)) {
$this->user = $config->db_user;
if (isset ($config->db_pass)) {
* Set the connection parameters manually
* @param string $database
* Connnects to the database
* @return resource The connection link identifier
* Frees the memory associated with a result
* @param string $types The types of the parameters. possible values: i, d, s, b for integet, double, string and blob
* @param mixed $params If a single or an array of parameters are given in the statement will be prepared
* @return object The result object
public function query($sql, $types= '', $params= array())
* Returns the number of rows found when you have used
* SELECT SQL_CALC_FOUND_ROWS
* Walk the reseult set from a select query
* @param int $result_type DB_ASSOC, DB_BOTH or DB_NUM
* @param string $mode Modes are: "read", "read local", "write", "low priority write"
public function lock($table, $mode = "write") {
while(list ($key,$value) = each($table)) {
if(is_int($key)) $key = $mode;
$query .= str_replace(",", " $key, ", $value) . " $key, ";
$query .= "$value $key, ";
$query = substr($query, 0, - 2);
} elseif(strpos($table, ",")) {
$query .= str_replace(",", " $mode, ", $table) . " $mode";
$query .= "$table $mode";
if(!$this->query($query)) {
$this->halt("lock() failed.");
* @return bool True on success
// set before unlock to avoid potential loop
if(!$this->query("unlock tables")) {
$this->halt("unlock() failed.");
* Fetch a single field from a result record
* @param string $name Field name or index
* @return mixed the field value
public function f($name) {
if (isset ($this->record[$name])) {
* Print a single field from a result record.
* @param string $name Field name or index
* @return mixed the field value
public function p($name) {
if (isset ($this->record[$name])) {
* Get a next unique ID for a table. Used instead of auto increment
* so that we have better support for different database backends.
* @param string $seq_name
* @return int the next unique ID
public function nextid($seq_name) {
/* if no current lock, lock sequence table */
$this->halt("cannot lock ". $this->seq_table. " - has it been created?");
/* get sequence number and increment */
$q = sprintf("select nextid from %s where seq_name = '%s'",
$this->halt('query failed in nextid: '. $q);
/* No current value, make one */
$q = sprintf("insert into %s values('%s', %s)",
$this->halt('query failed in nextid: '. $q);
$currentid = $this->f("nextid");
$nextid = $currentid + 1;
$q = sprintf("update %s set nextid = '%s' where seq_name = '%s'",
$this->halt('query failed in nextid: '. $q);
/* if nextid() locked the sequence table, unlock it */
* Return the number of rows found in the last select statement
* @return int Number of rows
* Gets the number of affected rows in a previous MySQL operatio
* Get the number of fields in a result
* Updates a row from a table
* @param string $table The table name
* @param string $index The field name to select on ( WHERE '$index'=1). This field must be present in the $fields parameter
* @param array $fields An associative array with fieldname=>value
* @param string $types The types of the parameters. possible values: i, d, s, b for integet, double, string and blob
* @param bool $trim Trim values in the array
public function update_row($table, $index, $fields, $types= '', $trim= true)
$this->halt('Invalid update row called');
foreach($fields as $key => $value)
$field_values[] = $value;
$field_types.= $types[$count];
$index_types.= $types[$count];
$index_values[] = $value;
$sql = "UPDATE `$table` SET ". implode(',',$updates). " WHERE ". implode(' AND ', $indexes);
foreach($index_values as $index_value)
$field_values[]= $index_value;
$field_types .= $index_types;
if(!$this->query($sql, $field_types, $field_values))
* Inserts a row in a table
* @param string $table The table name
* @param array $fields An associative array with fieldname=>value
* @param string $types The types of the parameters. possible values: i, d, s, b for integet, double, string and blob
* @param bool $trim Trim values in the array
public function insert_row($table, $fields, $types= '', $trim= true, $replace= false)
$this->halt('Invalid insert row called');
foreach($fields as $key => $value)
$field_values[] = $value;
$sql = $replace ? 'REPLACE' : 'INSERT';
$sql .= " INTO `$table` (`". implode('`,`', $field_names). "`) VALUES ".
if(!$this->query($sql, $types, $field_values))
* Replaces a row in a table
* @param string $table The table name
* @param string $index The field name to select on ( WHERE '$index'=1). This field must be present in the $fields parameter
* @param array $fields An associative array with fieldname=>value
* @param string $types The types of the parameters. possible values: i, d, s, b for integet, double, string and blob
* @param bool $trim Trim values in the array
public function replace_row($table, $fields, $types, $trim= true)
return $this->insert_row($table, $fields, $types,$trim, true);
* Escapes a value to make it safe to send to MySQL
* @param bool $trim Trim the value
* @return mixed the escaped value.
public function escape($value, $trim= true)
* Returns the auto generated id used in the last query
* Sets the error and errno property
* Halts when an error occurs
* @param unknown_type $msg
protected function halt($msg) {
go_log(LOG_DEBUG, sprintf("<b>Database error:</b> %s<br>\n<b>MySQL Error</b>: %s (%s)<br>\n",
throw new Exception(sprintf("<b>Database error:</b> %s<br>\n<b>MySQL Error</b>: %s (%s)<br>\n",
echo sprintf("<b>Database error:</b> %s<br>\n<b>MySQL Error</b>: %s (%s)<br>\n",
|