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 | |
parent | c226e4acff8b0f06520ee61803bc9b8972a15cbd (diff) | |
download | itools-479528efc0dafcf0dd57a48dc8d9dd1a270827b8.tar.gz itools-479528efc0dafcf0dd57a48dc8d9dd1a270827b8.tar.bz2 itools-479528efc0dafcf0dd57a48dc8d9dd1a270827b8.zip |
Allow multiple arguments as query to select() and constructor
-rw-r--r-- | it_dbi.class | 39 | ||||
-rwxr-xr-x | tests/it_dbi.t | 27 |
2 files changed, 50 insertions, 16 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) { diff --git a/tests/it_dbi.t b/tests/it_dbi.t index 82826f1..afffbae 100755 --- a/tests/it_dbi.t +++ b/tests/it_dbi.t @@ -6,7 +6,7 @@ require 'searchlib/search_test.class'; # Initialize DB -$db = array('db' => "lib_search_ch"); +$db = array('db' => "lib_search_ch", 'safety' => 0); $dbi = new it_dbi($db); $dbi->query('create temporary table it_dbi_test ( ID int not null auto_increment, @@ -34,6 +34,31 @@ is( "read" ); +is( + $record->select(), + 3, + "select without parameters select all", +); + +is( + $record->select(array('foo <>' => ""), "LIMIT 1"), + 1, + "select with multiple parameters (LIMIT part)" +); +is( + $record->foo, + "bar", + "select with multiple parameters (foo part)" +); + +$record = new it_dbi($db + array('table' => "it_dbi_test"), array('x >' => 0), "ORDER BY x DESC"); +is( + $record->x, + 64738, + "constructor with multiple select parameters" +); + + $record->select(array('x' => 64738)); is( array($record->_key, $record->x, $record->foo), |