added possibility to use a sql query-string to so_sql::search

This commit is contained in:
Ralf Becker 2003-04-16 14:32:15 +00:00
parent 5da28cbdb2
commit 28362cbdaa

View File

@ -380,27 +380,35 @@ class so_sql
@function search @function search
@abstract searches db for rows matching searchcriteria @abstract searches db for rows matching searchcriteria
@discussion '*' and '?' are replaced with sql-wildcards '%' and '_' @discussion '*' and '?' are replaced with sql-wildcards '%' and '_'
@param $criteria array of key and data cols @param $criteria array of key and data cols, OR a SQL query (content for WHERE), fully quoted (!)
@param $only_keys True returns only keys, False returns all cols @param $only_keys True returns only keys, False returns all cols
@param $order_by fieldnames + {ASC|DESC} separated by colons ',' @param $order_by fieldnames + {ASC|DESC} separated by colons ','
@param $extra_cols string to be added to the SELECT, eg. (count(*) as num)
@param $wildcard string appended befor and after each criteria @param $wildcard string appended befor and after each criteria
@param $empty False=empty criteria are ignored in query, True=empty have to be empty in row @param $empty False=empty criteria are ignored in query, True=empty have to be empty in row
@param $op defaults to 'AND', can be set to 'OR' too, then criteria's are OR'ed together
@result array of matching rows (the row is an array of the cols) or False @result array of matching rows (the row is an array of the cols) or False
*/ */
function search($criteria,$only_keys=True,$order_by='',$extra_cols='',$wildcard='',$empty=False) function search($criteria,$only_keys=True,$order_by='',$extra_cols='',$wildcard='',$empty=False,$op='AND')
{
if (!is_array($criteria))
{
$query = ' WHERE '.$criteria;
}
else
{ {
$criteria = $this->data2db($criteria); $criteria = $this->data2db($criteria);
foreach($this->db_cols as $db_col => $col) foreach($this->db_cols as $db_col => $col)
{ //echo "testing col='$col', criteria[$col]='".$criteria[$col]."'<br>"; { //echo "testing col='$col', criteria[$col]='".$criteria[$col]."'<br>";
if (isset($criteria[$col]) && ($empty || $criteria[$col] != '')) if (isset($criteria[$col]) && ($empty || $criteria[$col] != ''))
{ {
$query .= ($query ? ' AND ' : ' WHERE ') . $db_col . $query .= ($query ? " $op " : ' WHERE ') . $db_col .
($wildcard || strstr($criteria[$col],'*') || strstr($criteria[$col],'?') ? ($wildcard || strstr($criteria[$col],'*') || strstr($criteria[$col],'?') ?
" LIKE '$wildcard".strtr(str_replace('_','\\_',addslashes($criteria[$col])),'*?','%_')."$wildcard'" : " LIKE '$wildcard".strtr(str_replace('_','\\_',addslashes($criteria[$col])),'*?','%_')."$wildcard'" :
"='".addslashes($criteria[$col])."'"); "='".addslashes($criteria[$col])."'");
} }
} }
}
$this->db->query($sql = 'SELECT '.($only_keys ? implode(',',$this->db_key_cols) : '*'). $this->db->query($sql = 'SELECT '.($only_keys ? implode(',',$this->db_key_cols) : '*').
($extra_cols != '' ? ",$extra_cols" : '')." FROM $this->table_name $query" . ($extra_cols != '' ? ",$extra_cols" : '')." FROM $this->table_name $query" .
($order_by != '' ? " ORDER BY $order_by" : ''),__LINE__,__FILE__); ($order_by != '' ? " ORDER BY $order_by" : ''),__LINE__,__FILE__);