diff options
author | Christian Schneider | 2008-04-01 15:22:15 +0000 |
---|---|---|
committer | Christian Schneider | 2008-04-01 15:22:15 +0000 |
commit | 479528efc0dafcf0dd57a48dc8d9dd1a270827b8 (patch) | |
tree | 5b6e2fec9d6313366abb91b0bada3f4e06918a46 /it_dbi.class | |
parent | c226e4acff8b0f06520ee61803bc9b8972a15cbd (diff) | |
download | itools-479528efc0dafcf0dd57a48dc8d9dd1a270827b8.tar.gz itools-479528efc0dafcf0dd57a48dc8d9dd1a270827b8.tar.bz2 itools-479528efc0dafcf0dd57a48dc8d9dd1a270827b8.zip |
Allow multiple arguments as query to select() and constructor
Diffstat (limited to 'it_dbi.class')
-rw-r--r-- | it_dbi.class | 39 |
1 files changed, 24 insertions, 15 deletions
diff --git a/it_dbi.class b/it_dbi.class index e755732..e59bdc2 100644 --- a/it_dbi.class +++ b/it_dbi.class @@ -78,8 +78,8 @@ function it_dbi($p = array(), $query = null) if ($p['getfieldinfo']) $this->_p += $this->_get_field_info(); # Get $this->_fields and p[keyfield, autoincrement, randomid] - if (is_array($query)) - $this->select($query); + if (is_array($query)) # Call with all arguments except first one + call_user_func_array(array($this, "select"), array(0 => null) + func_get_args()); elseif (isset($query)) $this->read($query); } @@ -230,16 +230,14 @@ function _where($params = "", $link = null, $omit_where = false) if (is_array($params) && (count($params) > 0)) { $query = ''; + $stringquery = ''; $sep = ''; foreach($params as $field => $value) { if (is_int($field)) /* no key specified; just append */ { - if ($field === $value) /* ignore array(1 => 1) et al */ - continue; - - $query .= " $value"; + $stringquery .= " $value"; } else { @@ -307,6 +305,8 @@ function _where($params = "", $link = null, $omit_where = false) } } + $query .= $stringquery; + if ($needs_where && !$omit_where) $query = "WHERE $query"; } @@ -419,7 +419,7 @@ function read($id=null) /** * Select a set of records from table and fetch the first one - * @param $query Optional array of (field => value) or (int => sqlstring) pairs. Defaults to null (select all records) + * @param $query One or more optional arrays of (field => value) or sqlstring pairs. Defaults to null (select all records) * Fields will be joined by AND * Fields can contain a compare operator: 'name LIKE' => "j%" or 'amount >' => 100 * Fields can start with - to prevent quoting of right side: '-modified' => "CURDATE()" @@ -431,8 +431,12 @@ function read($id=null) * @see iterate() * @see _where() */ -function select($query = null) +function select(/* $query = array|string, ... */) { + $query = array(); + foreach (func_get_args() as $arg) + $query = array_merge($query, (array)$arg); + $this->_connect(); $result = 0; @@ -465,16 +469,18 @@ function select($query = null) $this->clear(false); if ($this->_result = $this->query($sql = "SELECT $what FROM $join " . $this->_where($query, $this->_link))) + { $result = mysql_num_rows($this->_result); - if ($calc_found_rows) - { - list($count) = mysql_fetch_row($this->query('SELECT FOUND_ROWS()')); - $this->_found_rows = intval($count); - } + if ($calc_found_rows) + { + list($count) = mysql_fetch_row($this->query('SELECT FOUND_ROWS()')); + $this->_found_rows = intval($count); + } - if (!$this->iterate() && ($this->_p['safety'] >= 2)) - $this->_fatal("select(): query produced no results: \"$sql\""); + if (!$this->iterate() && ($this->_p['safety'] >= 2)) + $this->_fatal("select(): query produced no results: \"$sql\""); + } $this->_nofetch = !$nofetch; @@ -628,7 +634,10 @@ function _get_field_info() } $GLOBALS['it_dbi']->_state[$this->_p['dbid']]['fields'][$this->_p['table']] = $this->_fields; + $GLOBALS['it_dbi']->_state[$this->_p['dbid']]['isint'][$this->_p['table']] = $this->_isint; } + else # Existing _fields, copy other info too + $this->_isint = $GLOBALS['it_dbi']->_state[$this->_p['dbid']]['isint'][$this->_p['table']]; foreach($this->_fields as $field) { |