diff --git a/phpgwapi/inc/class.egw_db.inc.php b/phpgwapi/inc/class.egw_db.inc.php
index 83d3ec5c5c..585b5cf7b7 100644
--- a/phpgwapi/inc/class.egw_db.inc.php
+++ b/phpgwapi/inc/class.egw_db.inc.php
@@ -1,1491 +1,1581 @@
+ * @copyright (c) 2003-8 by Ralf Becker new ADOdb connection to $this->Type://$this->Host/$this->Database: Link_ID".($this->Link_ID === $GLOBALS['egw']->ADOdb ? '===' : '!==')."\$GLOBALS[egw]->ADOdb ".print_r($this->Link_ID->ServerInfo(),true)." ".print_r($this->Link_ID->ServerInfo(),true)." new ADOdb connection to $this->Type://$this->Host/$this->Database: Link_ID".($this->Link_ID === $GLOBALS['egw']->ADOdb ? '===' : '!==')."\$GLOBALS[egw]->ADOdb ".print_r($this->Link_ID->ServerInfo(),true)." ".print_r($this->Link_ID->ServerInfo(),true)." db::get_last_insert_id(table='$table',field='$field') not yet implemented for db-type '$this->Type' OR no insert operation before
Parameters: '".implode("','",$inputarr)."'":''),
- $line, $file);
+ fwrite($f,"*** Error $this->Errno: $this->Error\n".function_backtrace()."\n");
}
- return $this->Query_ID;
+ fclose($f);
}
-
- /**
- * Execute a query with limited result set
- *
- * @param string $Query_String the query to be executed
- * @param int $offset row to start from, default 0
- * @param int $line the line method was called from - use __LINE__
- * @param string $file the file method was called from - use __FILE__
- * @param int $num_rows number of rows to return (optional), default -1 = all, 0 will use $GLOBALS['egw_info']['user']['preferences']['common']['maxmatchs']
- * @param array/boolean $inputarr array for binding variables to parameters or false (default)
- * @return ADORecordSet or false, if the query fails
- */
- function limit_query($Query_String, $offset, $line = '', $file = '', $num_rows = '',$inputarr=false)
+ if (!$this->Query_ID)
{
- return $this->query($Query_String,$line,$file,$offset,$num_rows,$inputarr);
+ $this->halt("Invalid SQL: ".(is_array($Query_String)?$Query_String[0]:$Query_String).
+ ($inputarr ? "
Parameters: '".implode("','",$inputarr)."'":''),
+ $line, $file);
}
+ return $this->Query_ID;
+ }
- /**
- * Move to the next row in the results set
- *
- * Specifying a fetch_mode only works for newly fetched rows, the first row always gets fetched by query!!!
- *
- * @param int $fetch_mode ADODB_FETCH_BOTH = numerical+assoc keys (eGW default), ADODB_FETCH_ASSOC or ADODB_FETCH_NUM
- * @return bool was another row found?
- */
- function next_record($fetch_mode=ADODB_FETCH_BOTH)
+ /**
+ * Execute a query with limited result set
+ *
+ * @param string $Query_String the query to be executed
+ * @param int $offset row to start from, default 0
+ * @param int $line the line method was called from - use __LINE__
+ * @param string $file the file method was called from - use __FILE__
+ * @param int $num_rows number of rows to return (optional), default -1 = all, 0 will use $GLOBALS['egw_info']['user']['preferences']['common']['maxmatchs']
+ * @param array/boolean $inputarr array for binding variables to parameters or false (default)
+ * @return ADORecordSet or false, if the query fails
+ */
+ function limit_query($Query_String, $offset, $line = '', $file = '', $num_rows = '',$inputarr=false)
+ {
+ return $this->query($Query_String,$line,$file,$offset,$num_rows,$inputarr);
+ }
+
+ /**
+ * Move to the next row in the results set
+ *
+ * Specifying a fetch_mode only works for newly fetched rows, the first row always gets fetched by query!!!
+ *
+ * @param int $fetch_mode ADODB_FETCH_BOTH = numerical+assoc keys (eGW default), ADODB_FETCH_ASSOC or ADODB_FETCH_NUM
+ * @return bool was another row found?
+ */
+ function next_record($fetch_mode=ADODB_FETCH_BOTH)
+ {
+ if (!$this->Query_ID)
{
- if (!$this->Query_ID)
- {
- $this->halt('next_record called with no query pending.');
- return 0;
- }
- if ($this->Link_ID->fetchMode != $fetch_mode)
- {
- $this->Link_ID->SetFetchMode($fetch_mode);
- }
- if ($this->Row) // first row is already fetched
- {
- $this->Query_ID->MoveNext();
- }
- ++$this->Row;
-
- $this->Record = $this->Query_ID->fields;
-
- if ($this->Query_ID->EOF || !$this->Query_ID->RecordCount() || !is_array($this->Record))
- {
- return False;
- }
- if ($this->capabilities['name_case'] == 'upper') // maxdb, oracle, ...
- {
- switch($fetch_mode)
- {
- case ADODB_FETCH_ASSOC:
- $this->Record = array_change_key_case($this->Record);
- break;
- case ADODB_FETCH_NUM:
- $this->Record = array_values($this->Record);
- break;
- default:
- $this->Record = array_change_key_case($this->Record);
- if (!isset($this->Record[0]))
- {
- $this->Record += array_values($this->Record);
- }
- break;
- }
- }
- return True;
+ $this->halt('next_record called with no query pending.');
+ return 0;
}
-
- /**
- * Move to position in result set
- *
- * @param int $pos required row (optional), default first row
- * @return boolean true if sucessful or false if not found
- */
- function seek($pos = 0)
+ if ($this->Link_ID->fetchMode != $fetch_mode)
{
- if (!$this->Query_ID || !$this->Query_ID->Move($this->Row = $pos))
- {
- $this->halt("seek($pos) failed: resultset has " . $this->num_rows() . " rows");
- $this->Query_ID->Move( $this->num_rows() );
- $this->Row = $this->num_rows();
- return False;
- }
- return True;
+ $this->Link_ID->SetFetchMode($fetch_mode);
}
-
- /**
- * Begin Transaction
- *
- * @return int/boolean current transaction-id, of false if no connection
- */
- function transaction_begin()
+ if ($this->Row) // first row is already fetched
{
- if (!$this->Link_ID && !$this->connect())
- {
- return False;
- }
- //return $this->Link_ID->BeginTrans();
- return $this->Link_ID->StartTrans();
+ $this->Query_ID->MoveNext();
}
+ ++$this->Row;
- /**
- * Complete the transaction
- *
- * @return bool True if sucessful, False if fails
- */
- function transaction_commit()
+ $this->Record = $this->Query_ID->fields;
+
+ if ($this->Query_ID->EOF || !$this->Query_ID->RecordCount() || !is_array($this->Record))
{
- if (!$this->Link_ID && !$this->connect())
- {
- return False;
- }
- //return $this->Link_ID->CommitTrans();
- return $this->Link_ID->CompleteTrans();
+ return False;
}
-
- /**
- * Rollback the current transaction
- *
- * @return bool True if sucessful, False if fails
- */
- function transaction_abort()
+ if ($this->capabilities['name_case'] == 'upper') // maxdb, oracle, ...
{
- if (!$this->Link_ID && !$this->connect())
+ switch($fetch_mode)
{
- return False;
- }
- //return $this->Link_ID->RollbackTrans();
- return $this->Link_ID->FailTrans();
- }
-
- /**
- * Find the primary key of the last insertion on the current db connection
- *
- * @param string $table name of table the insert was performed on
- * @param string $field the autoincrement primary key of the table
- * @return int the id, -1 if fails
- */
- function get_last_insert_id($table, $field)
- {
- if (!$this->Link_ID && !$this->connect())
- {
- return False;
- }
- $id = $this->Link_ID->PO_Insert_ID($table,$field); // simulates Insert_ID with "SELECT MAX($field) FROM $table" if not native availible
-
- if ($id === False) // function not supported
- {
- echo "
File: %s",$file);
- }
- if ($line)
- {
- printf("
Line: %s",$line);
- }
- printf("
Function: %s
Session halted.
"; - if (is_object($GLOBALS['egw']->common)) - { - $GLOBALS['egw']->common->egw_exit(True); - } - else // happens eg. in setup - { - exit(); - } - } - } - - function haltmsg($msg) - { - printf("Database error: %s
\n", $msg);
- if (($this->Errno || $this->Error) && $this->Error != "()")
- {
- printf("$this->Type Error: %s (%s)
\n",$this->Errno,$this->Error);
- }
- }
-
- /**
- * Get description of a table
- *
- * Beside the column-name all other data depends on the db-type !!!
- *
- * @param string $table name of table to describe
- * @param bool $full optional, default False summary information, True full information
- * @return array table meta data
- */
- function metadata($table='',$full=false)
- {
- if (!$this->Link_ID && !$this->connect())
- {
- return False;
- }
- $columns = $this->Link_ID->MetaColumns($table);
- //$columns = $this->Link_ID->MetaColumnsSQL($table);
- //echo "metadata('$table')=
\n".print_r($columns,True)."\n"; - - $metadata = array(); - $i = 0; - foreach($columns as $column) - { - // for backwards compatibilty (depreciated) - unset($flags); - if($column->auto_increment) $flags .= "auto_increment "; - if($column->primary_key) $flags .= "primary_key "; - if($column->binary) $flags .= "binary "; - -// _debug_array($column); - $metadata[$i] = array( - 'table' => $table, - 'name' => $column->name, - 'type' => $column->type, - 'len' => $column->max_length, - 'flags' => $flags, // for backwards compatibilty (depreciated) used by JiNN atm - 'not_null' => $column->not_null, - 'auto_increment' => $column->auto_increment, - 'primary_key' => $column->primary_key, - 'binary' => $column->binary, - 'has_default' => $column->has_default, - 'default' => $column->default_value, - ); - $metadata[$i]['table'] = $table; - if ($full) - { - $metadata['meta'][$column->name] = $i; - } - ++$i; - } - if ($full) - { - $metadata['num_fields'] = $i; - } - return $metadata; - } - - /** - * Get a list of table names in the current database - * - * @return array list of the tables - */ - function table_names() - { - if (!$this->Link_ID) $this->connect(); - if (!$this->Link_ID) - { - return False; - } - $result = array(); - $tables = $this->Link_ID->MetaTables('TABLES'); - if (is_array($tables)) - { - foreach($tables as $table) - { - if ($this->capabilities['name_case'] == 'upper') - { - $table = strtolower($table); - } - $result[] = array( - 'table_name' => $table, - 'tablespace_name' => $this->Database, - 'database' => $this->Database - ); - } - } - return $result; - } - - /** - * Return a list of indexes in current database - * - * @return array list of indexes - */ - function index_names() - { - $indices = array(); - if ($this->Type != 'pgsql') - { - echo "
db::index_names() not yet implemented for db-type '$this->Type'
\n"; - return $indices; - } - $this->query("SELECT relname FROM pg_class WHERE NOT relname ~ 'pg_.*' AND relkind ='i' ORDER BY relname"); - while ($this->next_record()) - { - $indices[] = array( - 'index_name' => $this->f(0), - 'tablespace_name' => $this->Database, - 'database' => $this->Database, - ); - } - return $indices; - } - - /** - * Returns an array containing column names that are the primary keys of $tablename. - * - * @return array of columns - */ - function pkey_columns($tablename) - { - if (!$this->Link_ID && !$this->connect()) - { - return False; - } - return $this->Link_ID->MetaPrimaryKeys($tablename); - } - - /** - * Create a new database - * - * @param string $adminname name of database administrator user (optional) - * @param string $adminpasswd password for the database administrator user (optional) - * @param string $charset default charset for the database - */ - function create_database($adminname = '', $adminpasswd = '', $charset='') - { - $currentUser = $this->User; - $currentPassword = $this->Password; - $currentDatabase = $this->Database; - - $sqls = array(); - $set_charset = ''; - switch ($this->Type) - { - case 'pgsql': - $meta_db = 'template1'; - $sqls[] = "CREATE DATABASE $currentDatabase"; + case ADODB_FETCH_ASSOC: + $this->Record = array_change_key_case($this->Record); break; - case 'mysql': - $create = "CREATE DATABASE `$currentDatabase`"; - if ($charset && isset($this->Link_ID->charset2mysql[$charset]) && (float) $this->ServerInfo['version'] >= 4.1) - { - $create .= ' DEFAULT CHARACTER SET '.$this->Link_ID->charset2mysql[$charset].';'; - } - $sqls[] = $create; - $sqls[] = "GRANT ALL ON `$currentDatabase`.* TO $currentUser@localhost IDENTIFIED BY ".$this->quote($currentPassword); - $meta_db = 'mysql'; + case ADODB_FETCH_NUM: + $this->Record = array_values($this->Record); break; default: - echo "db::create_database(user='$adminname',\$pw) not yet implemented for DB-type '$this->Type'
\n"; + $this->Record = array_change_key_case($this->Record); + if (!isset($this->Record[0])) + { + $this->Record += array_values($this->Record); + } break; } - if ($adminname != '') - { - $this->User = $adminname; - $this->Password = $adminpasswd; - $this->Database = $meta_db; - } - $this->disconnect(); - foreach($sqls as $sql) - { - $this->query($sql,__LINE__,__FILE__); - } - $this->disconnect(); - - $this->User = $currentUser; - $this->Password = $currentPassword; - $this->Database = $currentDatabase; - $this->connect(); } + return True; + } - /** - * concat a variable number of strings together, to be used in a query - * - * Example: $db->concat($db->quote('Hallo '),'username') would return - * for mysql "concat('Hallo ',username)" or "'Hallo ' || username" for postgres - * @param string $str1 already quoted stringliteral or column-name, variable number of arguments - * @return string to be used in a query - */ - function concat($str1) + /** + * Move to position in result set + * + * @param int $pos required row (optional), default first row + * @return boolean true if sucessful or false if not found + */ + function seek($pos = 0) + { + if (!$this->Query_ID || !$this->Query_ID->Move($this->Row = $pos)) { - $args = func_get_args(); - - if (!$this->Link_ID && !$this->connect()) - { - return False; - } - return call_user_func_array(array(&$this->Link_ID,'concat'),$args); + $this->halt("seek($pos) failed: resultset has " . $this->num_rows() . " rows"); + $this->Query_ID->Move( $this->num_rows() ); + $this->Row = $this->num_rows(); + return False; } - - /** - * Convert a unix timestamp stored as integer in the db into a db timestamp, like MySQL: FROM_UNIXTIME(ts) - * - * @param string $expr name of an integer column or integer expression - * @return string SQL expression of type timestamp - */ - function from_unixtime($expr) + return True; + } + + /** + * Begin Transaction + * + * @return int/boolean current transaction-id, of false if no connection + */ + function transaction_begin() + { + if (!$this->Link_ID && !$this->connect()) { - switch($this->Type) - { - case 'mysql': - return "FROM_UNIXTIME($expr)"; - - case 'pgsql': // we use date(,0) as we store server-time - return "(timestamp '".date('Y-m-d H:i:s',0)."' + ($expr) * interval '1 sec')"; - - case 'mssql': // we use date(,0) as we store server-time - return "DATEADD(second,($expr),'".date('Y-m-d H:i:s',0)."')"; - } - return false; + return False; } + //return $this->Link_ID->BeginTrans(); + return $this->Link_ID->StartTrans(); + } - /** - * format a timestamp as string, like MySQL: DATE_FORMAT(ts) - * - * Please note: only a subset of the MySQL formats are implemented - * - * @param string $expr name of a timestamp column or timestamp expression - * @param string $format format specifier like '%Y-%m-%d %H:%i:%s' or '%V%X' ('%v%x') weeknumber & year with Sunday (Monday) as first day - * @return string SQL expression of type timestamp - */ - function date_format($expr,$format) + /** + * Complete the transaction + * + * @return bool True if sucessful, False if fails + */ + function transaction_commit() + { + if (!$this->Link_ID && !$this->connect()) { - switch($this->Type) - { - case 'mysql': - return "DATE_FORMAT($expr,'$format')"; - - case 'pgsql': - $format = str_replace( - array('%Y', '%y','%m','%d','%H', '%h','%i','%s','%V','%v','%X', '%x'), - array('YYYY','YY','MM','DD','HH24','HH','MI','SS','IW','IW','YYYY','YYYY'), - $format); - return "TO_CHAR($expr,'$format')"; - - case 'mssql': - $from = $to = array(); - foreach(array('%Y'=>'yyyy','%y'=>'yy','%m'=>'mm','%d'=>'dd','%H'=>'hh','%i'=>'mi','%s'=>'ss','%V'=>'wk','%v'=>'wk','%X'=>'yyyy','%x'=>'yyyy') as $f => $t) - { - $from[] = $f; - $to[] = "'+DATEPART($t,($expr))+'"; - } - $from[] = "''+"; $to[] = ''; - $from[] = "+''"; $to[] = ''; - return str_replace($from,$to,$format); - } - return false; + return False; } + //return $this->Link_ID->CommitTrans(); + return $this->Link_ID->CompleteTrans(); + } - /** - * Correctly Quote Identifiers like table- or colmnnames for use in SQL-statements - * - * This is mostly copy & paste from adodb's datadict class - * @param $name string - * @return string quoted string - */ - function name_quote($name = NULL) + /** + * Rollback the current transaction + * + * @return bool True if sucessful, False if fails + */ + function transaction_abort() + { + if (!$this->Link_ID && !$this->connect()) { - if (!is_string($name)) { - return FALSE; - } - - $name = trim($name); - - if (!$this->Link_ID && !$this->connect()) - { - return False; - } - - $quote = $this->Link_ID->nameQuote; - - // if name is of the form `name`, quote it - if ( preg_match('/^`(.+)`$/', $name, $matches) ) { - return $quote . $matches[1] . $quote; - } - - // if name contains special characters, quote it - if ( preg_match('/\W/', $name) ) { - return $quote . $name . $quote; - } - - return $name; + return False; } + //return $this->Link_ID->RollbackTrans(); + return $this->Link_ID->FailTrans(); + } - /** - * Escape values before sending them to the database - prevents SQL injunction and SQL errors ;-) - * - * Please note that the quote function already returns necessary quotes: quote('Hello') === "'Hello'". - * Int and Auto types are casted to int: quote('1','int') === 1, quote('','int') === 0, quote('Hello','int') === 0 - * - * @param mixed $value the value to be escaped - * @param string/boolean $type=false string the type of the db-column, default False === varchar - * @param boolean $not_null=true is column NOT NULL, default true, else php null values are written as SQL NULL - * @param int $length=null length of the varchar column, to truncate it if the database requires it (eg. Postgres) - * @return string escaped sting - */ - function quote($value,$type=False,$not_null=true,$length=null) + /** + * Find the primary key of the last insertion on the current db connection + * + * @param string $table name of table the insert was performed on + * @param string $field the autoincrement primary key of the table + * @return int the id, -1 if fails + */ + function get_last_insert_id($table, $field) + { + if (!$this->Link_ID && !$this->connect()) { - if ($this->Debug) echo "db::quote(".(is_null($value)?'NULL':"'$value'").",'$type','$not_null')
\n"; - - if (!$not_null && is_null($value)) // writing unset php-variables and those set to NULL now as SQL NULL - { - return 'NULL'; - } - switch($type) - { - case 'int': - case 'auto': - // atm. (php5.2) php has only 32bit integers, it converts everything else to float. - // Casting it to int gives a negative number instead of the big 64bit integer! - // There for we have to keep it as float by using round instead the int cast. - return is_float($value) ? round($value) : (int) $value; - case 'bool': - if ($this->Type == 'mysql') // maybe it's not longer necessary with mysql5 - { - return $value ? 1 : 0; - } - return $value ? 'true' : 'false'; - case 'float': - case 'decimal': - return (double) $value; - } - if (!$this->Link_ID && !$this->connect()) - { - return False; - } - switch($type) - { - case 'blob': - switch ($this->Link_ID->blobEncodeType) - { - case 'C': // eg. postgres - return "'" . $this->Link_ID->BlobEncode($value) . "'"; - case 'I': - return $this->Link_ID->BlobEncode($value); - } - break; // handled like strings - case 'date': - return $this->Link_ID->DBDate($value); - case 'timestamp': - return $this->Link_ID->DBTimeStamp($value); - } - if (!is_null($length) && strlen($value) > $length) - { - $value = substr($value,0,$length); - } - return $this->Link_ID->qstr($value); + return False; } + $id = $this->Link_ID->PO_Insert_ID($table,$field); // simulates Insert_ID with "SELECT MAX($field) FROM $table" if not native availible - /** - * Implodes an array of column-value pairs for the use in sql-querys. - * All data is run through quote (does either addslashes() or (int)) - prevents SQL injunction and SQL errors ;-). - * - * @author RalfBeckerdb::column_data_implode('$glue',".print_r($array,True).",'$use_key',".print_r($only,True).",
".print_r($column_definitions,True)."\n"; + echo "
db::get_last_insert_id(table='$table',field='$field') not yet implemented for db-type '$this->Type' OR no insert operation before
\n"; + function_backtrace(); + return -1; + } + return $id; + } - // do we need to truncate varchars to their max length (INSERT and UPDATE on Postgres) - $truncate_varchar = $glue == ',' && $this->capabilities['require_truncate_varchar']; + /** + * Lock a table + * + * @deprecated not used anymore as it costs to much performance, use transactions if needed + * @param string $table name of table to lock + * @param string $mode type of lock required (optional), default write + * @return bool True if sucessful, False if fails + */ + function lock($table, $mode='write') + {} - $keys = $values = array(); - foreach($array as $key => $data) + /** + * Unlock a table + * + * @deprecated not used anymore as it costs to much performance, use transactions if needed + * @return bool True if sucessful, False if fails + */ + function unlock() + {} + + /** + * Get the number of rows affected by last update or delete + * + * @return int number of rows + */ + function affected_rows() + { + if (!$this->Link_ID && !$this->connect()) + { + return False; + } + return $this->Link_ID->Affected_Rows(); + } + + /** + * Number of rows in current result set + * + * @return int number of rows + */ + function num_rows() + { + return $this->Query_ID ? $this->Query_ID->RecordCount() : False; + } + + /** + * Number of fields in current row + * + * @return int number of fields + */ + function num_fields() + { + return $this->Query_ID ? $this->Query_ID->FieldCount() : False; + } + + /** + * @deprecated use num_rows() + */ + function nf() + { + return $this->num_rows(); + } + + /** + * @deprecated use print num_rows() + */ + function np() + { + print $this->num_rows(); + } + + /** + * Return the value of a column + * + * @param string/integer $Name name of field or positional index starting from 0 + * @param bool $strip_slashes string escape chars from field(optional), default false + * depricated param, as correctly quoted values dont need any stripslashes! + * @return string the field value + */ + function f($Name, $strip_slashes = False) + { + if ($strip_slashes) + { + return stripslashes($this->Record[$Name]); + } + return $this->Record[$Name]; + } + + /** + * Print the value of a field + * + * @param string $Name name of field to print + * @param bool $strip_slashes string escape chars from field(optional), default false + * depricated param, as correctly quoted values dont need any stripslashes! + */ + function p($Name, $strip_slashes = True) + { + print $this->f($Name, $strip_slashes); + } + + /** + * Returns a query-result-row as an associative array (no numerical keys !!!) + * + * @param bool $do_next_record should next_record() be called or not (default not) + * @param string $strip='' string to strip of the column-name, default '' + * @return array/bool the associative array or False if no (more) result-row is availible + */ + function row($do_next_record=False,$strip='') + { + if ($do_next_record && !$this->next_record(ADODB_FETCH_ASSOC) || !is_array($this->Record)) + { + return False; + } + $result = array(); + foreach($this->Record as $column => $value) + { + if (!is_numeric($column)) { - if (is_int($key) || !$only || $only === True && isset($column_definitions[$key]) || - is_array($only) && in_array($key,$only)) + if ($strip) $column = str_replace($strip,'',$column); + + $result[$column] = $value; + } + } + return $result; + } + + /** + * Error handler + * + * @param string $msg error message + * @param int $line line of calling method/function (optional) + * @param string $file file of calling method/function (optional) + */ + function halt($msg, $line = '', $file = '') + { + if ($this->Link_ID) // only if we have a link, else infinite loop + { + $this->Error = $this->Link_ID->ErrorMsg(); // need to be BEFORE unlock, + $this->Errno = $this->Link_ID->ErrorNo(); // else we get its error or none + + $this->unlock(); /* Just in case there is a table currently locked */ + } + if ($this->Halt_On_Error == "no") + { + return; + } + if ($this->Halt_On_Error == 'yes') + { + throw new egw_exception_db($msg.($this->Error?":\n".$this->Error:''),$this->Errno); + } + $this->haltmsg($msg); + + if ($file) + { + printf("Session halted.
"; + if (is_object($GLOBALS['egw']->common)) + { + $GLOBALS['egw']->common->egw_exit(True); + } + else // happens eg. in setup + { + exit(); + } + } + } + + function haltmsg($msg) + { + printf("Database error: %s
\n", $msg);
+ if (($this->Errno || $this->Error) && $this->Error != "()")
+ {
+ printf("$this->Type Error: %s (%s)
\n",$this->Errno,$this->Error);
+ }
+ }
+
+ /**
+ * Get description of a table
+ *
+ * Beside the column-name all other data depends on the db-type !!!
+ *
+ * @param string $table name of table to describe
+ * @param bool $full optional, default False summary information, True full information
+ * @return array table meta data
+ */
+ function metadata($table='',$full=false)
+ {
+ if (!$this->Link_ID && !$this->connect())
+ {
+ return False;
+ }
+ $columns = $this->Link_ID->MetaColumns($table);
+ //$columns = $this->Link_ID->MetaColumnsSQL($table);
+ //echo "metadata('$table')=
\n".print_r($columns,True)."\n"; + + $metadata = array(); + $i = 0; + foreach($columns as $column) + { + // for backwards compatibilty (depreciated) + unset($flags); + if($column->auto_increment) $flags .= "auto_increment "; + if($column->primary_key) $flags .= "primary_key "; + if($column->binary) $flags .= "binary "; + +// _debug_array($column); + $metadata[$i] = array( + 'table' => $table, + 'name' => $column->name, + 'type' => $column->type, + 'len' => $column->max_length, + 'flags' => $flags, // for backwards compatibilty (depreciated) used by JiNN atm + 'not_null' => $column->not_null, + 'auto_increment' => $column->auto_increment, + 'primary_key' => $column->primary_key, + 'binary' => $column->binary, + 'has_default' => $column->has_default, + 'default' => $column->default_value, + ); + $metadata[$i]['table'] = $table; + if ($full) + { + $metadata['meta'][$column->name] = $i; + } + ++$i; + } + if ($full) + { + $metadata['num_fields'] = $i; + } + return $metadata; + } + + /** + * Get a list of table names in the current database + * + * @return array list of the tables + */ + function table_names() + { + if (!$this->Link_ID) $this->connect(); + if (!$this->Link_ID) + { + return False; + } + $result = array(); + $tables = $this->Link_ID->MetaTables('TABLES'); + if (is_array($tables)) + { + foreach($tables as $table) + { + if ($this->capabilities['name_case'] == 'upper') { - $keys[] = $this->name_quote($key); + $table = strtolower($table); + } + $result[] = array( + 'table_name' => $table, + 'tablespace_name' => $this->Database, + 'database' => $this->Database + ); + } + } + return $result; + } - if (!is_int($key) && is_array($column_definitions) && !isset($column_definitions[$key])) + /** + * Return a list of indexes in current database + * + * @return array list of indexes + */ + function index_names() + { + $indices = array(); + if ($this->Type != 'pgsql') + { + echo "
db::index_names() not yet implemented for db-type '$this->Type'
\n"; + return $indices; + } + $this->query("SELECT relname FROM pg_class WHERE NOT relname ~ 'pg_.*' AND relkind ='i' ORDER BY relname"); + while ($this->next_record()) + { + $indices[] = array( + 'index_name' => $this->f(0), + 'tablespace_name' => $this->Database, + 'database' => $this->Database, + ); + } + return $indices; + } + + /** + * Returns an array containing column names that are the primary keys of $tablename. + * + * @return array of columns + */ + function pkey_columns($tablename) + { + if (!$this->Link_ID && !$this->connect()) + { + return False; + } + return $this->Link_ID->MetaPrimaryKeys($tablename); + } + + /** + * Create a new database + * + * @param string $adminname name of database administrator user (optional) + * @param string $adminpasswd password for the database administrator user (optional) + * @param string $charset default charset for the database + */ + function create_database($adminname = '', $adminpasswd = '', $charset='') + { + $currentUser = $this->User; + $currentPassword = $this->Password; + $currentDatabase = $this->Database; + + $sqls = array(); + $set_charset = ''; + switch ($this->Type) + { + case 'pgsql': + $meta_db = 'template1'; + $sqls[] = "CREATE DATABASE $currentDatabase"; + break; + case 'mysql': + $create = "CREATE DATABASE `$currentDatabase`"; + if ($charset && isset($this->Link_ID->charset2mysql[$charset]) && (float) $this->ServerInfo['version'] >= 4.1) + { + $create .= ' DEFAULT CHARACTER SET '.$this->Link_ID->charset2mysql[$charset].';'; + } + $sqls[] = $create; + $sqls[] = "GRANT ALL ON `$currentDatabase`.* TO $currentUser@localhost IDENTIFIED BY ".$this->quote($currentPassword); + $meta_db = 'mysql'; + break; + default: + echo "db::create_database(user='$adminname',\$pw) not yet implemented for DB-type '$this->Type'
\n"; + break; + } + if ($adminname != '') + { + $this->User = $adminname; + $this->Password = $adminpasswd; + $this->Database = $meta_db; + } + $this->disconnect(); + foreach($sqls as $sql) + { + $this->query($sql,__LINE__,__FILE__); + } + $this->disconnect(); + + $this->User = $currentUser; + $this->Password = $currentPassword; + $this->Database = $currentDatabase; + $this->connect(); + } + + /** + * concat a variable number of strings together, to be used in a query + * + * Example: $db->concat($db->quote('Hallo '),'username') would return + * for mysql "concat('Hallo ',username)" or "'Hallo ' || username" for postgres + * @param string $str1 already quoted stringliteral or column-name, variable number of arguments + * @return string to be used in a query + */ + function concat($str1) + { + $args = func_get_args(); + + if (!$this->Link_ID && !$this->connect()) + { + return False; + } + return call_user_func_array(array(&$this->Link_ID,'concat'),$args); + } + + /** + * Convert a unix timestamp stored as integer in the db into a db timestamp, like MySQL: FROM_UNIXTIME(ts) + * + * @param string $expr name of an integer column or integer expression + * @return string SQL expression of type timestamp + */ + function from_unixtime($expr) + { + switch($this->Type) + { + case 'mysql': + return "FROM_UNIXTIME($expr)"; + + case 'pgsql': // we use date(,0) as we store server-time + return "(timestamp '".date('Y-m-d H:i:s',0)."' + ($expr) * interval '1 sec')"; + + case 'mssql': // we use date(,0) as we store server-time + return "DATEADD(second,($expr),'".date('Y-m-d H:i:s',0)."')"; + } + return false; + } + + /** + * format a timestamp as string, like MySQL: DATE_FORMAT(ts) + * + * Please note: only a subset of the MySQL formats are implemented + * + * @param string $expr name of a timestamp column or timestamp expression + * @param string $format format specifier like '%Y-%m-%d %H:%i:%s' or '%V%X' ('%v%x') weeknumber & year with Sunday (Monday) as first day + * @return string SQL expression of type timestamp + */ + function date_format($expr,$format) + { + switch($this->Type) + { + case 'mysql': + return "DATE_FORMAT($expr,'$format')"; + + case 'pgsql': + $format = str_replace( + array('%Y', '%y','%m','%d','%H', '%h','%i','%s','%V','%v','%X', '%x'), + array('YYYY','YY','MM','DD','HH24','HH','MI','SS','IW','IW','YYYY','YYYY'), + $format); + return "TO_CHAR($expr,'$format')"; + + case 'mssql': + $from = $to = array(); + foreach(array('%Y'=>'yyyy','%y'=>'yy','%m'=>'mm','%d'=>'dd','%H'=>'hh','%i'=>'mi','%s'=>'ss','%V'=>'wk','%v'=>'wk','%X'=>'yyyy','%x'=>'yyyy') as $f => $t) + { + $from[] = $f; + $to[] = "'+DATEPART($t,($expr))+'"; + } + $from[] = "''+"; $to[] = ''; + $from[] = "+''"; $to[] = ''; + return str_replace($from,$to,$format); + } + return false; + } + + /** + * Correctly Quote Identifiers like table- or colmnnames for use in SQL-statements + * + * This is mostly copy & paste from adodb's datadict class + * @param $name string + * @return string quoted string + */ + function name_quote($name = NULL) + { + if (!is_string($name)) { + return FALSE; + } + + $name = trim($name); + + if (!$this->Link_ID && !$this->connect()) + { + return False; + } + + $quote = $this->Link_ID->nameQuote; + + // if name is of the form `name`, quote it + if ( preg_match('/^`(.+)`$/', $name, $matches) ) { + return $quote . $matches[1] . $quote; + } + + // if name contains special characters, quote it + if ( preg_match('/\W/', $name) ) { + return $quote . $name . $quote; + } + + return $name; + } + + /** + * Escape values before sending them to the database - prevents SQL injection and SQL errors ;-) + * + * Please note that the quote function already returns necessary quotes: quote('Hello') === "'Hello'". + * Int and Auto types are casted to int: quote('1','int') === 1, quote('','int') === 0, quote('Hello','int') === 0 + * Arrays of id's stored in strings: quote(array(1,2,3),'string') === "'1,2,3'" + * + * @param mixed $value the value to be escaped + * @param string/boolean $type=false string the type of the db-column, default False === varchar + * @param boolean $not_null=true is column NOT NULL, default true, else php null values are written as SQL NULL + * @param int $length=null length of the varchar column, to truncate it if the database requires it (eg. Postgres) + * @param string $glue=',' used to glue array values together for the string type + * @return string escaped sting + */ + function quote($value,$type=False,$not_null=true,$length=null,$glue=',') + { + if ($this->Debug) echo "db::quote(".(is_null($value)?'NULL':"'$value'").",'$type','$not_null')
\n"; + + if (!$not_null && is_null($value)) // writing unset php-variables and those set to NULL now as SQL NULL + { + return 'NULL'; + } + switch($type) + { + case 'int': + case 'auto': + // atm. (php5.2) php has only 32bit integers, it converts everything else to float. + // Casting it to int gives a negative number instead of the big 64bit integer! + // There for we have to keep it as float by using round instead the int cast. + return is_float($value) ? round($value) : (int) $value; + case 'bool': + if ($this->Type == 'mysql') // maybe it's not longer necessary with mysql5 + { + return $value ? 1 : 0; + } + return $value ? 'true' : 'false'; + case 'float': + case 'decimal': + return (double) $value; + } + if (!$this->Link_ID && !$this->connect()) + { + return False; + } + switch($type) + { + case 'blob': + switch ($this->Link_ID->blobEncodeType) + { + case 'C': // eg. postgres + return "'" . $this->Link_ID->BlobEncode($value) . "'"; + case 'I': + return $this->Link_ID->BlobEncode($value); + } + break; // handled like strings + case 'date': + return $this->Link_ID->DBDate($value); + case 'timestamp': + return $this->Link_ID->DBTimeStamp($value); + } + if (is_array($value)) + { + $value = implode($glue,$value); + } + if (!is_null($length) && strlen($value) > $length) + { + $value = substr($value,0,$length); + } + return $this->Link_ID->qstr($value); + } + + /** + * Implodes an array of column-value pairs for the use in sql-querys. + * All data is run through quote (does either addslashes() or (int)) - prevents SQL injunction and SQL errors ;-). + * + * @author RalfBeckerdb::column_data_implode('$glue',".print_r($array,True).",'$use_key',".print_r($only,True).",
".print_r($column_definitions,True)."\n"; + + // do we need to truncate varchars to their max length (INSERT and UPDATE on Postgres) + $truncate_varchar = $glue == ',' && $this->capabilities['require_truncate_varchar']; + + $keys = $values = array(); + foreach($array as $key => $data) + { + if (is_int($key) || !$only || $only === True && isset($column_definitions[$key]) || + is_array($only) && in_array($key,$only)) + { + $keys[] = $this->name_quote($key); + + if (!is_int($key) && is_array($column_definitions) && !isset($column_definitions[$key])) + { + // give a warning that we have no column-type + $this->halt("db::column_data_implode('$glue',".print_r($array,True).",'$use_key',".print_r($only,True).",
".print_r($column_definitions,True)."nothing known about column '$key'!"); + } + $column_type = is_array($column_definitions) ? @$column_definitions[$key]['type'] : False; + $not_null = is_array($column_definitions) && isset($column_definitions[$key]['nullable']) ? !$column_definitions[$key]['nullable'] : false; + + if ($truncate_varchar) + { + $maxlength = $column_definitions[$key]['type'] == 'varchar' ? $column_definitions[$key]['precision'] : null; + } + if (is_array($data)) + { + $or_null = ''; + foreach($data as $k => $v) { - // give a warning that we have no column-type - $this->halt("db::column_data_implode('$glue',".print_r($array,True).",'$use_key',".print_r($only,True).",
".print_r($column_definitions,True)."nothing known about column '$key'!"); - } - $column_type = is_array($column_definitions) ? @$column_definitions[$key]['type'] : False; - $not_null = is_array($column_definitions) && isset($column_definitions[$key]['nullable']) ? !$column_definitions[$key]['nullable'] : false; - - if ($truncate_varchar) - { - $maxlength = $column_definitions[$key]['type'] == 'varchar' ? $column_definitions[$key]['precision'] : null; - } - if (is_array($data)) - { - $or_null = ''; - foreach($data as $k => $v) + if (!$not_null && $use_key===True && is_null($v)) { - if (!$not_null && $use_key===True && is_null($v)) - { - $or_null = $this->name_quote($key).' IS NULL)'; - unset($data[$k]); - continue; - } - $data[$k] = $this->quote($v,$column_type,$not_null,$maxlength); + $or_null = $this->name_quote($key).' IS NULL)'; + unset($data[$k]); + continue; } - $values[] = ($or_null?'(':'').(!count($data) ? '' : - ($use_key===True ? $this->name_quote($key).' IN ' : '') . - '('.implode(',',$data).')'.($or_null ? ' OR ' : '')).$or_null; - } - elseif (is_int($key) && $use_key===True) - { - $values[] = $data; - } - elseif ($glue != ',' && $use_key === True && !$not_null && is_null($data)) - { - $values[] = $this->name_quote($key) .' IS NULL'; - } - else - { - $values[] = ($use_key===True ? $this->name_quote($key) . '=' : '') . $this->quote($data,$column_type,$not_null,$maxlength); + $data[$k] = $this->quote($v,$column_type,$not_null,$maxlength); } + $values[] = ($or_null?'(':'').(!count($data) ? '' : + ($use_key===True ? $this->name_quote($key).' IN ' : '') . + '('.implode(',',$data).')'.($or_null ? ' OR ' : '')).$or_null; + } + elseif (is_int($key) && $use_key===True) + { + $values[] = $data; + } + elseif ($glue != ',' && $use_key === True && !$not_null && is_null($data)) + { + $values[] = $this->name_quote($key) .' IS NULL'; + } + else + { + $values[] = ($use_key===True ? $this->name_quote($key) . '=' : '') . $this->quote($data,$column_type,$not_null,$maxlength); } } - return ($use_key==='VALUES' ? '('.implode(',',$keys).') VALUES (' : ''). - implode($glue,$values) . ($use_key==='VALUES' ? ')' : ''); } + return ($use_key==='VALUES' ? '('.implode(',',$keys).') VALUES (' : ''). + implode($glue,$values) . ($use_key==='VALUES' ? ')' : ''); + } - /** - * Sets the default column-definitions for use with column_data_implode() - * - * @author RalfBecker
db::insert('$table',".print_r($data,True).",".print_r($where,True).",$line,$file,'$app')
\n"; + + if (!$table_def) $table_def = $this->get_table_definitions($app,$table); + + $sql_append = ''; + $cmd = 'INSERT'; + if (is_array($where) && count($where)) + { + switch($this->Type) + { + case 'sapdb': case 'maxdb': + $sql_append = ' UPDATE DUPLICATES'; + break; + case 'mysql': + // use replace if primary keys are included + if (count(array_intersect(array_keys($where),(array)$table_def['pk'])) == count($table_def['pk'])) { - return $data['table_defs'][$table]; - } - } - $app = false; - } - if (!$app) - { - $app = $this->app ? $this->app : $GLOBALS['egw_info']['flags']['currentapp']; - } - if (isset($GLOBALS['egw_info']['apps'])) // dont set it, if it does not exist!!! - { - $this->app_data = &$GLOBALS['egw_info']['apps'][$app]; - } - // this happens during the eGW startup or in setup - else - { - $this->app_data =& $this->all_app_data[$app]; - } - if (!isset($this->app_data['table_defs'])) - { - $tables_current = EGW_INCLUDE_ROOT . "/$app/setup/tables_current.inc.php"; - if (!@file_exists($tables_current)) - { - return $this->app_data['table_defs'] = False; - } - include($tables_current); - $this->app_data['table_defs'] =& $phpgw_baseline; - unset($phpgw_baseline); - } - if ($table && (!$this->app_data['table_defs'] || !isset($this->app_data['table_defs'][$table]))) - { - return False; - } - return $table ? $this->app_data['table_defs'][$table] : $this->app_data['table_defs']; - } - - /** - * Insert a row of data into a table or updates it if $where is given, all data is quoted according to it's type - * - * @author RalfBeckerdb::insert('$table',".print_r($data,True).",".print_r($where,True).",$line,$file,'$app')
\n"; - - if (!$table_def) $table_def = $this->get_table_definitions($app,$table); - - $sql_append = ''; - $cmd = 'INSERT'; - if (is_array($where) && count($where)) - { - switch($this->Type) - { - case 'sapdb': case 'maxdb': - $sql_append = ' UPDATE DUPLICATES'; + $cmd = 'REPLACE'; break; - case 'mysql': - // use replace if primary keys are included - if (count(array_intersect(array_keys($where),(array)$table_def['pk'])) == count($table_def['pk'])) - { - $cmd = 'REPLACE'; + } + // fall through !!! + default: + $this->select($table,'count(*)',$where,$line,$file); + if ($this->next_record() && $this->f(0)) + { + return !!$this->update($table,$data,$where,$line,$file,$app); + } + break; + } + // the checked values need to be inserted too, value in data has precedence, also cant insert sql strings (numerical id) + foreach($where as $column => $value) + { + if (!is_numeric($column) && !isset($data[$column])) + { + $data[$column] = $value; + } + } + } + $inputarr = false; + if ($use_prepared_statement && $this->Link_ID->_bindInputArray) // eg. MaxDB + { + $this->Link_ID->Param(false); // reset param-counter + $cols = array_keys($data); + foreach($cols as $k => $col) + { + if (!isset($table_def['fd'][$col])) // ignore columns not in this table + { + unset($cols[$k]); + continue; + } + $params[] = $this->Link_ID->Param($col); + } + $sql = "$cmd INTO $table (".implode(',',$cols).') VALUES ('.implode(',',$params).')'.$sql_append; + // check if we already prepared that statement + if (!isset($this->prepared_sql[$sql])) + { + $this->prepared_sql[$sql] = $this->Link_ID->Prepare($sql); + } + $sql = $this->prepared_sql[$sql]; + $inputarr = &$data; + } + else + { + $sql = "$cmd INTO $table ".$this->column_data_implode(',',$data,'VALUES',true,$table_def['fd']).$sql_append; + } + if ($this->Debug) echo "db::insert('$table',".print_r($data,True).",".print_r($where,True).",$line,$file,'$app') sql='$sql'
\n"; + return $this->query($sql,$line,$file,0,-1,$inputarr); + } + + /** + * Updates the data of one or more rows in a table, all data is quoted according to it's type + * + * @author RalfBeckerdb::update('$table',".print_r($data,true).','.print_r($where,true).",$line,$file,'$app')
\n"; + if (!$table_def) $table_def = $this->get_table_definitions($app,$table); + + $blobs2update = array(); + // SapDB/MaxDB cant update LONG columns / blob's: if a blob-column is included in the update we remember it in $blobs2update + // and remove it from $data + switch ($this->Type) + { + case 'sapdb': + case 'maxdb': + if ($use_prepared_statement) break; + // check if data contains any LONG columns + foreach($data as $col => $val) + { + switch ($table_def['fd'][$col]['type']) + { + case 'text': + case 'longtext': + case 'blob': + $blobs2update[$col] = &$data[$col]; + unset($data[$col]); break; - } - // fall through !!! - default: - $this->select($table,'count(*)',$where,$line,$file); - if ($this->next_record() && $this->f(0)) - { - return !!$this->update($table,$data,$where,$line,$file,$app); - } - break; - } - // the checked values need to be inserted too, value in data has precedence, also cant insert sql strings (numerical id) - foreach($where as $column => $value) - { - if (!is_numeric($column) && !isset($data[$column])) - { - $data[$column] = $value; } } - } + break; + } + $where = $this->column_data_implode(' AND ',$where,True,true,$table_def['fd']); + + if (count($data)) + { $inputarr = false; if ($use_prepared_statement && $this->Link_ID->_bindInputArray) // eg. MaxDB { $this->Link_ID->Param(false); // reset param-counter - $cols = array_keys($data); - foreach($cols as $k => $col) + foreach($data as $col => $val) { - if (!isset($table_def['fd'][$col])) // ignore columns not in this table - { - unset($cols[$k]); - continue; - } - $params[] = $this->Link_ID->Param($col); + if (!isset($table_def['fd'][$col])) continue; // ignore columns not in this table + $params[] = $this->name_quote($col).'='.$this->Link_ID->Param($col); } - $sql = "$cmd INTO $table (".implode(',',$cols).') VALUES ('.implode(',',$params).')'.$sql_append; + $sql = "UPDATE $table SET ".implode(',',$params).' WHERE '.$where; // check if we already prepared that statement if (!isset($this->prepared_sql[$sql])) { @@ -1496,260 +1586,187 @@ } else { - $sql = "$cmd INTO $table ".$this->column_data_implode(',',$data,'VALUES',true,$table_def['fd']).$sql_append; + $sql = "UPDATE $table SET ". + $this->column_data_implode(',',$data,True,true,$table_def['fd']).' WHERE '.$where; } - if ($this->Debug) echo "db::insert('$table',".print_r($data,True).",".print_r($where,True).",$line,$file,'$app') sql='$sql'
\n"; - return $this->query($sql,$line,$file,0,-1,$inputarr); + $ret = $this->query($sql,$line,$file,0,-1,$inputarr); + if ($this->Debug) echo "db::query('$sql',$line,$file) = '$ret'
\n"; } - - /** - * Updates the data of one or more rows in a table, all data is quoted according to it's type - * - * @author RalfBeckerdb::update('$table',".print_r($data,true).','.print_r($where,true).",$line,$file,'$app')
\n"; - if (!$table_def) $table_def = $this->get_table_definitions($app,$table); - - $blobs2update = array(); - // SapDB/MaxDB cant update LONG columns / blob's: if a blob-column is included in the update we remember it in $blobs2update - // and remove it from $data - switch ($this->Type) + foreach($blobs2update as $col => $val) { - case 'sapdb': - case 'maxdb': - if ($use_prepared_statement) break; - // check if data contains any LONG columns - foreach($data as $col => $val) - { - switch ($table_def['fd'][$col]['type']) - { - case 'text': - case 'longtext': - case 'blob': - $blobs2update[$col] = &$data[$col]; - unset($data[$col]); - break; - } - } + $ret = $this->Link_ID->UpdateBlob($table,$col,$val,$where,$table_def['fd'][$col]['type'] == 'blob' ? 'BLOB' : 'CLOB'); + if ($this->Debug) echo "adodb::UpdateBlob('$table','$col','$val','$where') = '$ret'
\n"; + if (!$ret) $this->halt("Error in UpdateBlob($table,$col,\$val,$where)",$line,$file); + } + } + return $ret; + } + + /** + * Deletes one or more rows in table, all data is quoted according to it's type + * + * @author RalfBeckerdb::query('$sql',$line,$file) = '$ret'
\n"; - } - // if we have any blobs to update, we do so now - if (($ret || !count($data)) && count($blobs2update)) - { - foreach($blobs2update as $col => $val) - { - $ret = $this->Link_ID->UpdateBlob($table,$col,$val,$where,$table_def['fd'][$col]['type'] == 'blob' ? 'BLOB' : 'CLOB'); - if ($this->Debug) echo "adodb::UpdateBlob('$table','$col','$val','$where') = '$ret'
\n"; - if (!$ret) $this->halt("Error in UpdateBlob($table,$col,\$val,$where)",$line,$file); - } - } - return $ret; } + if ($this->Debug) echo "db::expression($table,
".print_r(func_get_args(),True).") ='$sql'\n"; + return $sql; + } - /** - * Deletes one or more rows in table, all data is quoted according to it's type - * - * @author RalfBecker
db::select('$table',".print_r($cols,True).",".print_r($where,True).",$line,$file,$offset,'$app',$num_rows,'$join')
\n"; + + if (!$table_def) $table_def = $this->get_table_definitions($app,$table); + if (is_array($cols)) { - if (!$table_def) $table_def = $this->get_table_definitions($app,$table); - $sql = "DELETE FROM $table WHERE ". - $this->column_data_implode(' AND ',$where,True,False,$table_def['fd']); - - return $this->query($sql,$line,$file); + $cols = implode(',',$cols); } - - /** - * Formats and quotes a sql expression to be used eg. as where-clause - * - * The function has a variable number of arguments, from which the expession gets constructed - * eg. db::expression('my_table','(',array('name'=>"test'ed",'lang'=>'en'),') OR ',array('owner'=>array('',4,10))) - * gives "(name='test\'ed' AND lang='en') OR 'owner' IN (0,4,5,6,10)" if name,lang are strings and owner is an integer - * - * @param string/array $table_def table-name or definition array - * @param mixed $args variable number of arguments of the following types: - * string: get's as is into the result - * array: column-name / value pairs: the value gets quoted according to the type of the column and prefixed - * with column-name=, multiple pairs are AND'ed together, see db::column_data_implode - * bool: If False or is_null($arg): the next 2 (!) arguments gets ignored - * - * Please note: As the function has a variable number of arguments, you CAN NOT add further parameters !!! - * - * @return string the expression generated from the arguments - */ - function expression($table_def,$args) + if (is_array($where)) { - if (!is_array($table_def)) $table_def = $this->get_table_definitions('',$table_def); - $sql = ''; - $ignore_next = 0; - foreach(func_get_args() as $n => $arg) - { - if ($n < 1) continue; // table-name + $where = $this->column_data_implode(' AND ',$where,True,False,$table_def['fd']); + } + $sql = "SELECT $cols FROM $table $join"; - if ($ignore_next) - { - --$ignore_next; - continue; - } - if (is_null($arg)) $arg = False; + // if we have a where clause, we need to add it together with the WHERE statement, if thats not in the join + if ($where) $sql .= (strpos($join,"WHERE")!==false) ? ' AND ('.$where.')' : ' WHERE '.$where; - switch(gettype($arg)) - { - case 'string': - $sql .= $arg; - break; - case 'boolean': - $ignore_next += !$arg ? 2 : 0; - break; - case 'array': - $sql .= $this->column_data_implode(' AND ',$arg,True,False,$table_def['fd']); - break; - } - } - if ($this->Debug) echo "db::expression($table,
".print_r(func_get_args(),True).") ='$sql'\n"; + if ($append) $sql .= ' '.$append; + + if ($this->Debug) echo "
sql='$sql'
"; + + if ($line === false && $file === false) // call by union, to return the sql rather then run the query + { return $sql; } - - /** - * Selects one or more rows in table depending on where, all data is quoted according to it's type - * - * @author RalfBeckerdb::select('$table',".print_r($cols,True).",".print_r($where,True).",$line,$file,$offset,'$app',$num_rows,'$join')
\n"; - - if (!$table_def) $table_def = $this->get_table_definitions($app,$table); - if (is_array($cols)) - { - $cols = implode(',',$cols); - } - if (is_array($where)) - { - $where = $this->column_data_implode(' AND ',$where,True,False,$table_def['fd']); - } - $sql = "SELECT $cols FROM $table $join"; - - // if we have a where clause, we need to add it together with the WHERE statement, if thats not in the join - if ($where) $sql .= (strpos($join,"WHERE")!==false) ? ' AND ('.$where.')' : ' WHERE '.$where; - - if ($append) $sql .= ' '.$append; - - if ($this->Debug) echo "sql='$sql'
"; - - if ($line === false && $file === false) // call by union, to return the sql rather then run the query - { - return $sql; - } - return $this->query($sql,$line,$file,$offset,$offset===False ? -1 : (int)$num_rows); - } - - /** - * Does a union over multiple selects - * - * @author RalfBeckerdb::union(".print_r($selects,True).",$line,$file,$order_by,$offset,$num_rows)
\n"; - - $sql = array(); - foreach($selects as $select) - { - $sql[] = call_user_func_array(array($this,'select'),array( - $select['table'], - $select['cols'], - $select['where'], - false, // line - false, // file - false, // offset - $select['append'], - $select['app'], - 0, // num_rows, - $select['join'], - $select['table_def'], - )); - } - $sql = count($sql) > 1 ? '(' . implode(")\nUNION\n(",$sql).')' : $sql[0]; - - if ($order_by) $sql .= (!stristr($order_by,'ORDER BY') ? "\nORDER BY " : '').$order_by; - - if ($this->Debug) echo "sql='$sql'
"; - - return $this->query($sql,$line,$file,$offset,$offset===False ? -1 : (int)$num_rows); - } + return $this->query($sql,$line,$file,$offset,$offset===False ? -1 : (int)$num_rows,false,ADODB_FETCH_ASSOC); } + + /** + * Does a union over multiple selects + * + * @author RalfBeckerdb::union(".print_r($selects,True).",$line,$file,$order_by,$offset,$num_rows)
\n"; + + $sql = array(); + foreach($selects as $select) + { + $sql[] = call_user_func_array(array($this,'select'),array( + $select['table'], + $select['cols'], + $select['where'], + false, // line + false, // file + false, // offset + $select['append'], + $select['app'], + 0, // num_rows, + $select['join'], + $select['table_def'], + )); + } + $sql = count($sql) > 1 ? '(' . implode(")\nUNION\n(",$sql).')' : $sql[0]; + + if ($order_by) $sql .= (!stristr($order_by,'ORDER BY') ? "\nORDER BY " : '').$order_by; + + if ($this->Debug) echo "sql='$sql'
"; + + return $this->query($sql,$line,$file,$offset,$offset===False ? -1 : (int)$num_rows,false,ADODB_FETCH_ASSOC); + } +}