[eluser]Unknown[/eluser]
propose to make a few useful methods. this will give the opportunity to make requests of the form:
Code:
SELECT * FROM (`fr_users`) WHERE ( `id1` = 1 AND `id3` = 1 ) OR ( `id2` = 1 AND `id4` = 1 )
Code:
var $ar_where_exp = array();
var $ar_where_exp_type = array();
Code:
public function where_exp($key, $value = NULL, $escape = TRUE, $exp = '', $exp_type = 'AND ')
{
return $this->_where_exp($key, $value, 'AND ', $escape, $exp, $exp_type);
}
Code:
public function or_where_exp($key, $value = NULL, $escape = TRUE, $exp = '', $exp_type = 'AND ')
{
return $this->_where_exp($key, $value, 'OR ', $escape, $exp, $exp_type);
}
Code:
protected function _where_exp($key, $value = NULL, $type = 'AND ', $escape = NULL, $exp, $exp_type = 'AND ')
{
if ( ! is_array($key))
{
$key = array($key => $value);
}
// If the escape value was not set will will base it on the global setting
if ( ! is_bool($escape))
{
$escape = $this->_protect_identifiers;
}
foreach ($key as $k => $v)
{
$prefix = (!isset($this->ar_where_exp[$exp])) ? '' : $type;
if (!isset($this->ar_where_exp_type[$exp])) $this->ar_where_exp_type[$exp] = $exp_type;
if (is_null($v) && ! $this->_has_operator($k))
{
// value appears not to have been set, assign the test to IS NULL
$k .= ' IS NULL';
}
if ( ! is_null($v))
{
if ($escape === TRUE)
{
$k = $this->_protect_identifiers($k, FALSE, $escape);
$v = ' '.$this->escape($v);
}
if ( ! $this->_has_operator($k))
{
$k .= ' = ';
}
}
else
{
$k = $this->_protect_identifiers($k, FALSE, $escape);
}
$this->ar_where_exp[$exp][] = $prefix.$k.$v;
}
return $this;
}
in function _compile_select
Code:
// Write the "WHERE EXP" portion of the query
if (count($this->ar_where_exp) > 0 and count($this->ar_where) == 0)
{
$sql .= "\nWHERE ";
}
$t = false;
foreach ($this->ar_where_exp as $k => $v) {
if ($t or count($this->ar_where) > 0 OR count($this->ar_like) > 0)
{
$sql .= $this->ar_where_exp_type[$k];
} else {
$t = true;
}
$sql .= '( '.implode("\n", $this->ar_where_exp[$k]).' ) ';
}