* @copyright 2002-16 by RalfBecker@outdoor-training.de
* @version $Id$
*/
namespace EGroupware\Api\Storage;
use EGroupware\Api;
/**
* generalized SQL Storage Object
*
* the class can be used in following ways:
* 1) by calling the constructor with an app and table-name or
* 2) by setting the following documented class-vars in a class derived from this one
* Of cause you can derive from the class and call the constructor with params.
*
* @todo modify search() to return an interator instead of an array
*/
class Base
{
/**
* need to be set in the derived class to the db-table-name
*
* @var string
*/
var $table_name;
/**
* db-col-name of autoincrement id or ''
*
* @var string
*/
var $autoinc_id = '';
/**
* all cols in data which are not (direct)in the db, for data_merge
*
* @var array
*/
var $non_db_cols = array();
/**
* 4 turns on the so_sql debug-messages, default 0
*
* @var int
*/
var $debug = 0;
/**
* string to be written to db if a col-value is '', eg. "''" or 'NULL' (default)
*
* @var string
*/
var $empty_on_write = 'NULL';
/**
* total number of entries of last search with start != false
*
* @var int|boolean
*/
var $total = false;
/**
* protected instance or reference (depeding on $no_clone param of constructor) of the db-object
*
* @var Api\Db
*/
protected $db;
/**
* unique keys/index, set by derived class or via so_sql($app,$table)
*
* @var array
*/
var $db_uni_cols = array();
/**
* db-col-name / internal-name pairs, set by derived calls or via so_sql($app,$table)
*
* @var array
*/
var $db_key_cols = array();
/**
* db-col-name / internal-name pairs, set by derived calls or via so_sql($app,$table)
*
* @var array
*/
var $db_data_cols = array();
/**
* @var array $db_cols all columns = $db_key_cols + $db_data_cols, set in the constructor
*/
var $db_cols = array();
/**
* eGW table definition
*
* @var array
*/
var $table_def = array();
/**
* Appname to use in all queries, set via constructor
*
* @var string
*/
var $app;
/**
* holds the content of all columns
*
* @var array
*/
var $data = array();
/**
* Timestaps that need to be adjusted to user-time on reading or saving
*
* @var array
*/
var $timestamps = array();
/**
* Type of timestamps returned by this class (read and search methods), default null means leave them unchanged
*
* Possible values:
* - 'ts'|'integer' convert every timestamp to an integer unix timestamp
* - 'string' convert every timestamp to a 'Y-m-d H:i:s' string
* - 'object' convert every timestamp to a Api\DateTime object
*
* @var string
*/
public $timestamp_type;
/**
* Offset in secconds between user and server-time, it need to be add to a server-time to get the user-time
* or substracted from a user-time to get the server-time
*
* @var int
* @deprecated use Api\DateTime methods instead, as the offset between user and server time is only valid for current time
*/
var $tz_offset_s;
/**
* Current time in user timezone
*
* @var int|string|\DateTime format depends on $this->timestamp_type
*/
var $now;
/**
* Which columns should be searched, if a non-empty string is passed to criteria parameter of search()
*
* If not set (by extending class), all data columns will be searched.
*
* @var array
*/
var $columns_to_search;
/**
* Table has boolean fields, which need automatic conversation, got set automatic by call to setup_table
*
* Set it to false, if you dont want automatic conversation
*
* @var boolean
*/
protected $has_bools = false;
/**
* Should search return an iterator (true) or an array (false = default)
*
* @var boolean
*/
public $search_return_iterator = false;
/**
* constructor of the class
*
* NEED to be called from the constructor of the derived class !!!
*
* @param string $app should be set if table-defs to be read from ".__METHOD__."('$app','$table') so_sql::data_merge(".print_r($new,true).")
".__METHOD__."() User change TZ since read! tz-read=".$this->data[self::USER_TIMEZONE_READ].' != current-tz='.Api\DateTime::$user_timezone->getName()." --> fixing
\n"; error_log(__METHOD__."() User changed TZ since read! tz-read=".$this->data[self::USER_TIMEZONE_READ].' != current-tz='.Api\DateTime::$user_timezone->getName()." --> fixing"); $GLOBALS['egw_info']['user']['preferences']['common']['tz'] = $this->data[self::USER_TIMEZONE_READ]; Api\DateTime::setUserPrefs($this->data[self::USER_TIMEZONE_READ]); $this->set_times(); } $this->data2db(); if ((int) $this->debug >= 4) { echo "so_sql::save(".print_r($keys,true).") autoinc_id='$this->autoinc_id', data="; _debug_array($this->data); } if ($this->autoinc_id && !$this->data[$this->db_key_cols[$this->autoinc_id]]) // insert with auto id { foreach($this->db_cols as $db_col => $col) { if (!$this->autoinc_id || $db_col != $this->autoinc_id) // not write auto-inc-id { if (!array_key_exists($col,$this->data) && // handling of unset columns in $this->data (isset($this->table_def['fd'][$db_col]['default']) || // we have a default value !isset($this->table_def['fd'][$db_col]['nullable']) || $this->table_def['fd'][$db_col]['nullable'])) // column is nullable { continue; // no need to write that (unset) column } if ($this->table_def['fd'][$db_col]['type'] == 'varchar' && strlen($this->data[$col]) > $this->table_def['fd'][$db_col]['precision']) { // truncate the field to mamimum length, if upper layers didn't care $data[$db_col] = substr($this->data[$col],0,$this->table_def['fd'][$db_col]['precision']); } else { $data[$db_col] = !is_a($this->data[$col], 'DateTime') && (string)$this->data[$col] === '' && $this->empty_on_write == 'NULL' ? null : $this->data[$col]; } } } $this->db->insert($this->table_name,$data,false,__LINE__,__FILE__,$this->app); if ($this->autoinc_id) { $this->data[$this->db_key_cols[$this->autoinc_id]] = $this->db->get_last_insert_id($this->table_name,$this->autoinc_id); } } else // insert in table without auto id or update of existing row, dont write colums unset in $this->data { foreach($this->db_data_cols as $db_col => $col) { // we need to update columns set to null: after a $this->data[$col]=null: // - array_key_exits($col,$this->data) === true // - isset($this->data[$col]) === false if (!array_key_exists($col,$this->data) && // handling of unset columns in $this->data ($this->autoinc_id || // update of table with auto id or isset($this->table_def['fd'][$db_col]['default']) || // we have a default value or !isset($this->table_def['fd'][$db_col]['nullable']) || $this->table_def['fd'][$db_col]['nullable'])) // column is nullable { continue; // no need to write that (unset) column } $data[$db_col] = !is_object($this->data[$col]) && (string) $this->data[$col] === '' && $this->empty_on_write == 'NULL' ? null : $this->data[$col]; } // allow to add direct sql updates, eg. "etag=etag+1" with int keys if (is_array($keys) && isset($keys[0])) { for($n=0; isset($keys[$n]); ++$n) { $data[] = $keys[$n]; } } $keys = $extra_where; foreach($this->db_key_cols as $db_col => $col) { $keys[$db_col] = $this->data[$col]; } if (!$data && !$this->autoinc_id) // happens if all columns are in the primary key { $data = $keys; $keys = False; } if ($this->autoinc_id) { $this->db->update($this->table_name,$data,$keys,__LINE__,__FILE__,$this->app); if (($nothing_affected = !$this->db->Errno && !$this->db->affected_rows()) && $extra_where) { return true; // extra_where not met, eg. etag wrong } } // always try an insert if we have no autoinc_id, as we dont know if the data exists if (!$this->autoinc_id || $nothing_affected) { $this->db->insert($this->table_name,$data,$keys,__LINE__,__FILE__,$this->app); } } $this->db2data(); return $this->db->Errno; } /** * Update only the given fields, if the primary key is not given, it will be taken from $this->data * * @param array $_fields * @param boolean $merge =true if true $fields will be merged with $this->data (after update!), otherwise $this->data will be just $fields * @return int|boolean 0 on success, or errno != 0 on error, or true if $extra_where is given and no rows affected */ function update($_fields,$merge=true) { if ($merge) $this->data_merge($_fields); $fields = $this->data2db($_fields); // extract the keys from $fields or - if not set there - from $this->data $keys = array(); foreach($this->db_key_cols as $col => $name) { $keys[$col] = isset($fields[$name]) ? $fields[$name] : $this->data[$name]; unset($fields[$name]); } // extract the data from $fields $data = array(); foreach($this->db_data_cols as $col => $name) { if (array_key_exists($name,$fields)) { $data[$col] = $fields[$name]; unset($fields[$name]); } } // add direct sql like 'etag=etag+1' (it has integer keys) foreach($fields as $key => $value) { if (is_int($key)) { $data[] = $value; } } if (!$data) { return 0; // nothing to update } if (!$this->db->update($this->table_name,$data,$keys,__LINE__,__FILE__,$this->app)) { return $this->db->Errno; } return 0; } /** * deletes row representing keys in internal data or the supplied $keys if != null * * @param array|int $keys =null if given array with col => value pairs to characterise the rows to delete, or integer autoinc id * @param boolean $only_return_query =false return $query of delete call to db object, but not run it (used by so_sql_cf!) * @return int|array affected rows, should be 1 if ok, 0 if an error or array with id's if $only_return_ids */ function delete($keys=null,$only_return_query=false) { if ($this->autoinc_id && $keys && !is_array($keys)) { $keys = array($this->autoinc_id => $keys); } if (!is_array($keys) || !count($keys)) // use internal data { $data = $this->data; $keys = $this->db_key_cols; } else // data and keys are supplied in $keys { $data = $keys; $keys = array(); foreach($this->db_cols as $db_col => $col) { if (isset($data[$col])) { $keys[$db_col] = $col; } } // keep sql fragments (with integer key) foreach($data as $key => $val) { if (is_int($key)) { $keys[] = $val; } } } $data = $this->data2db($data); foreach($keys as $db_col => $col) { if (is_int($db_col)) { $query[] = $col; } else { $query[$db_col] = $data[$col]; } } if ($only_return_query) return $query; $this->db->delete($this->table_name,$query,__LINE__,__FILE__,$this->app); return $this->db->affected_rows(); } /** * searches db for rows matching searchcriteria * * '*' and '?' are replaced with sql-wildcards '%' and '_' * * For a union-query you call search for each query with $start=='UNION' and one more with only $order_by and $start set to run the union-query. * * @param array|string $criteria array of key and data cols, OR string with search pattern (incl. * or ? as wildcards) * @param boolean|string|array $only_keys =true True returns only keys, False returns all cols. or * comma seperated list or array of columns to return * @param string $order_by ='' fieldnames + {ASC|DESC} separated by colons ',', can also contain a GROUP BY (if it contains ORDER BY) * @param string|array $extra_cols ='' string or array of strings to be added to the SELECT, eg. "count(*) as num" * @param string $wildcard ='' appended befor and after each criteria * @param boolean $empty =false False=empty criteria are ignored in query, True=empty have to be empty in row * @param string $op ='AND' defaults to 'AND', can be set to 'OR' too, then criteria's are OR'ed together * @param mixed $start =false if != false, return only maxmatch rows begining with start, or array($start,$num), or 'UNION' for a part of a union query * @param array $filter =null if set (!=null) col-data pairs, to be and-ed (!) into the query without wildcards * @param string $join ='' sql to do a join, added as is after the table-name, eg. "JOIN table2 ON x=y" or * "LEFT JOIN table2 ON (x=y AND z=o)", Note: there's no quoting done on $join, you are responsible for it!!! * @param boolean $need_full_no_count =false If true an unlimited query is run to determine the total number of rows, default false * @todo return an interator instead of an array * @return array|NULL|true array of matching rows (the row is an array of the cols), NULL (nothing matched) or true (multiple union queries) */ function &search($criteria,$only_keys=True,$order_by='',$extra_cols='',$wildcard='',$empty=False,$op='AND',$start=false,$filter=null,$join='',$need_full_no_count=false) { //error_log(__METHOD__.'('.array2string(array_combine(array_slice(array('criteria','only_keys','order_by','extra_cols','wildcard','empty','op','start','filter','join','need_full_no_count'), 0, count(func_get_args())), func_get_args())).')'); if ((int) $this->debug >= 4) echo "so_sql::search(".print_r($criteria,true).",'$only_keys','$order_by',".print_r($extra_cols,true).",'$wildcard','$empty','$op','$start',".print_r($filter,true).",'$join')
\n"; // if extending class or instanciator set columns to search, convert string criteria to array if ($criteria && !is_array($criteria)) { $search = $this->search2criteria($criteria,$wildcard,$op); $criteria = array($search); } if (!is_array($criteria)) { $query = $criteria; } else { $query = $this->parse_search($criteria, $wildcard, $empty, $op); } if (is_array($filter)) { $db_filter = array(); $data2db_filter = $this->data2db($filter); if (!is_array($data2db_filter)) { echo function_backtrace()."so_sql::search(,only_keys=$only_keys,order_by='$order_by',wildcard='$wildcard',empty=$empty,$op,start='$start',".print_r($filter,true).") query=".print_r($query,true).", total='$this->total'
\n"; echo "sql='{$this->db->Query_ID->sql}'
\n"; if ($mysql_calc_rows) { $this->total = $this->db->query('SELECT FOUND_ROWS()')->fetchColumn(); } // ToDo: Implement that as an iterator, as $rs is also an interator and we could return one instead of an array if ($this->search_return_iterator) { return new Db2DataIterator($this,$rs); } $arr = array(); $n = 0; if ($rs) foreach($rs as $row) { $data = array(); foreach($cols as $db_col => $col) { $data[$col] = (isset($row[$db_col]) ? $row[$db_col] : $row[$col]); } $arr[] = $this->db2data($data); $n++; } if (!$n) $arr = null; return $arr; } /** * Parse an array of search criteria into something that can be passed on * to the DB * * @param array $_criteria * @param string $wildcard ='' appended befor and after each criteria * @param boolean $empty =false False=empty criteria are ignored in query, True=empty have to be empty in row * @param string $op ='AND' defaults to 'AND', can be set to 'OR' too, then criteria's are OR'ed together * @return Array * @throws Api\Db\Exception */ protected function parse_search(Array $_criteria, $wildcard, $empty, $op) { $criteria = $this->data2db($_criteria); foreach($criteria as $col => $val) { if (is_int($col)) { $query[] = $val; } elseif ($empty || $val != '') { if (!($db_col = array_search($col,$this->db_cols))) { $db_col = $col; } if ($val === '') { if (isset($this->table_def['fd'][$db_col]) && $this->table_def['fd'][$db_col]['type'] == 'varchar' && $this->table_def['fd'][$db_col]['nullable'] !== false) { unset($criteria[$col]); $query[] = '(' . $db_col . ' IS NULL OR ' . $db_col . " = '')"; } else { $query[$db_col] = ''; } } elseif ($wildcard || $criteria[$col][0] == '!' || is_string($criteria[$col]) && (strpos($criteria[$col],'*')!==false || strpos($criteria[$col],'?')!==false)) { // if search pattern alread contains a wildcard, do NOT add further ones automatic if (is_string($criteria[$col]) && (strpos($criteria[$col],'*')!==false || strpos($criteria[$col],'?')!==false)) { $wildcard = ''; } $cmp_op = ' '.$this->db->capabilities['case_insensitive_like'].' '; $negate = false; if ($criteria[$col][0] == '!') { $cmp_op = ' NOT'.$cmp_op; $criteria[$col] = substr($criteria[$col],1); $negate = true; } foreach(explode(' ',$criteria[$col]) as $crit) { $query[] = ($negate ? ' ('.$db_col.' IS NULL OR ' : '').$db_col.$cmp_op. $this->db->quote($wildcard.str_replace(array('%','_','*','?'),array('\\%','\\_','%','_'),$crit).$wildcard). ($negate ? ') ' : ''); } } elseif (strpos($db_col,'.') !== false) // we have a table-name specified { list($table,$only_col) = explode('.',$db_col); $type = $this->db->get_column_attribute($only_col, $table, true, 'type'); if (empty($type)) { throw new Api\Db\Exception("Can not determine type of column '$only_col' in table '$table'!"); } if (is_array($val) && count($val) > 1) { foreach($val as &$v) { $v = $this->db->quote($v, $type); } $query[] = $sql = $db_col.' IN (' .implode(',',$val).')'; } else { $query[] = $db_col.'='.$this->db->quote(is_array($val)?array_shift($val):$val,$type); } } else { $query[$db_col] = $criteria[$col]; } } } if (is_array($query) && $op != 'AND') $query = $this->db->column_data_implode(' '.$op.' ',$query); return $query; } /** * Fix GROUP BY clause to contain all non-aggregate selected columns * * No need to call for MySQL because MySQL does NOT give an error in above case. * (Of cause developer has to make sure to group by enough columns, eg. a unique key, for selected columns to be defined.) * * MySQL also does not allow to use [tablename.]* in GROUP BY, which PostgreSQL allows! * (To use this for MySQL too, we would have to replace * with all columns of a table.) * * @param string $group_by [GROUP BY ...[HAVING ...]][ORDER BY ...] * @param string|array $columns better provide an array as exploding by comma can lead to error with functions containing one * @param string $table_name table-name * @param string $autoinc_id id-column * @return string */ public static function fix_group_by_columns($group_by, &$columns, $table_name, $autoinc_id) { $matches = null; if (substr($GLOBALS['egw']->db->Type, 0, 5) == 'mysql' || !preg_match('/(GROUP BY .*)(HAVING.*|ORDER BY.*)?$/iU', $group_by, $matches)) { return $group_by; // nothing to do } $changes = 0; $group_by_cols = preg_split('/, */', trim(substr($matches[1], 9))); if (!is_array($columns)) { $columns = preg_split('/, */', $columns); // fix columns containing commas as part of function calls for($n = 0; $n < count($columns); ++$n) { $col =& $columns[$n]; while (substr_count($col, '(') > substr_count($col, ')') && ++$n < count($columns)) { $col .= ','.$columns[$n]; unset($columns[$n]); } } unset($col); } foreach($columns as $n => $col) { if ($col == '*') { // MySQL does NOT allow to GROUP BY table.* $col = $columns[$n] = $table_name.'.'.($GLOBALS['egw']->db->Type == 'mysql' ? $autoinc_id : '*'); ++$changes; } // only check columns and non-aggregate functions if (strpos($col, '(') === false || !preg_match('/(COUNT|MIN|MAX|AVG|SUM|BIT_[A-Z]+|STD[A-Z_]*|VAR[A-Z_]*|ARRAY_AGG)\(/i', $col)) { if (($pos = stripos($col, 'DISTINCT ')) !== false) { $col = substr($col, $pos+9); } $alias = $col; if (stripos($col, ' AS ')) list($col, $alias) = preg_split('/ +AS +/i', $col); // do NOT group by constant expressions if (preg_match('/^ *(-?[0-9]+|".*"|\'.*\'|NULL) *$/i', $col)) continue; if (!in_array($col, $group_by_cols) && !in_array($alias, $group_by_cols)) { // instead of aliased primary key, we have to use original column incl. table-name as alias is ambigues $group_by_cols[] = $col == $table_name.'.'.$autoinc_id ? $col : $alias; //error_log(__METHOD__."() col=$col, alias=$alias --> group_by_cols=".array2string($group_by_cols)); ++$changes; } } } $ret = $group_by; if ($changes) { $ret = str_replace($matches[1], 'GROUP BY '.implode(',', $group_by_cols).' ', $group_by); //error_log(__METHOD__."('$group_by', ".array2string($columns).") group_by_cols=".array2string($group_by_cols)." changed to $ret"); } return $ret; } /** * Return criteria array for a given search pattern * * We handle quoted text, wildcards and boolean operators (+/-, AND/OR). If * the pattern is '#' followed by an integer, the search is limited to just * the primary key. * * @param string $_pattern search pattern incl. * or ? as wildcard, if no wildcards used we append and prepend one! * @param string &$wildcard ='' on return wildcard char to use, if pattern does not already contain wildcards! * @param string &$op ='AND' on return boolean operation to use, if pattern does not start with ! we use OR else AND * @param string $extra_col =null extra column to search * @param array $search_cols =[] List of columns to search. If not provided, all columns in $this->db_cols will be considered * @param ?bool $search_cfs =null null: do it only for Api\Storage, false: never do it * @return array or column => value pairs */ public function search2criteria($_pattern,&$wildcard='',&$op='AND',$extra_col=null, $search_cols=[],$search_cfs=null) { $pattern = trim($_pattern); // This function can get called multiple times. Make sure it doesn't re-process. if (empty($pattern) || is_array($pattern)) return $pattern; if(strpos($pattern, 'CAST(COALESCE(') !== false) { return $pattern; } $criteria = array(); $filter = array(); $columns = array(); /* * Special handling for numeric columns. They are only considered if the pattern is numeric. * If the pattern is numeric, an equality search is used instead. */ $numeric_types = array('auto', 'int', 'float', 'double', 'decimal'); $numeric_columns = array(); // Special handling for an ID search, #so_sql::get_rows(".print_r($query,true).",,)
\n"; } $criteria = array(); $op = 'AND'; if ($query['search']) { $criteria = $query['search']; } $rows = $this->search($criteria,$only_keys,$query['order']?$query['order'].' '.$query['sort']:'',$extra_cols, '',false,$op,$query['num_rows']?array((int)$query['start'],$query['num_rows']):(int)$query['start'], $query['col_filter'],$join,$need_full_no_count); if (!$rows) $rows = array(); // otherwise false returned from search would be returned as array(false) return $this->total; } /** * Check if values for unique keys and the primary keys are unique are unique * * @param array $data =null data-set to check, defaults to $this->data * @return int 0: all keys are unique, 1: first key not unique, 2: ... */ function not_unique($data=null) { if (!is_array($data)) { $data = $this->data; } $n = 1; $uni_keys = $this->db_uni_cols; // add the primary key, only if it's NOT an auto id if (!$this->autoinc_id) { $uni_keys[] = $this->db_key_cols; } foreach($uni_keys as $db_col => $col) { if (is_array($col)) { $query = array(); foreach($col as $db_c => $c) { $query[$db_c] = $data[$c]; } } else { $query = array($db_col => $data[$col]); } foreach($this->db->select($this->table_name,$this->db_key_cols,$query,__LINE__,__FILE__,false,'',$this->app) as $other) { foreach($this->db_key_cols as $key_col) { if ($data[$key_col] != $other[$key_col]) { if ((int) $this->debug >= 4) { echo "not_unique in ".array2string($col)." as for '$key_col': '${data[$key_col]}' != '${other[$key_col]}'
\n"; } return $n; // different entry => $n not unique } } } ++$n; } return 0; } /** * Query DB for a list / array with one colum as key and an other one(s) as value, eg. id => title pairs * * We do some caching as these kind of function is usualy called multiple times, eg. for option-lists. * * @param string $value_col array of column-names for the values of the array, can also be an expression aliased with AS, * if more then one column given, an array with keys identical to the given ones is returned and not just the value of the column * @param string $key_col ='' column-name for the keys, default '' = same as (first) $value_col: returns a distinct list * @param array $filter =array() to filter the entries * @param string $order ='' order, default '' = same as (first) $value_col * @return array with key_col => value_col pairs or array if more then one value_col given (keys as in value_col) */ function query_list($value_col,$key_col='',$filter=array(),$order='') { static $cache = array(); $cache_key = serialize($value_col).'-'.$key_col.'-'.serialize($filter).'-'.$order; if (isset($cache[$cache_key])) { return $cache[$cache_key]; } if (!is_array($value_col)) $value_col = array($value_col); $cols = $ret = array(); foreach($value_col as $key => $col) { $matches = null; $cols[$key] = preg_match('/AS ([a-z_0-9]+)$/i',$col,$matches) ? $matches[1] : $col; } if (!$order) $order = current($cols); if (($search =& $this->search(array(),($key_col ? $key_col.',' : 'DISTINCT ').implode(',',$value_col),$order,'','',false,'AND',false,$filter))) { if (preg_match('/AS ([a-z_0-9]+)$/i',$key_col,$matches)) { $key_col = $matches[1]; } elseif (!$key_col) { $key_col = current($cols); } foreach($search as $row) { if (count($cols) > 1) { $data = array(); foreach($cols as $key => $col) { $data[$key] = $row[$col]; } } else { $data = $row[current($cols)]; } if ($data) $ret[$row[$key_col]] = $data; } } return $cache[$cache_key] =& $ret; } /** * Get comments for all columns or a specific one * * @param string $column =null name of column or null for all (default) * @return array|string array with internal-name => comment pairs, or string with comment, if $column given */ public function get_comments($column=null) { static $comments=null; if (is_null($comments)) { foreach($this->db_cols as $db_col => $col) { $comments[$col] = $this->table_def['fd'][$db_col]['comment']; } } return is_null($column) ? $comments : $comments[$column]; } }