Class it_dbi:
/**
* Select a set of records from table and fetch the first one
* @param $query Vararg. Arrays of (field => value) pairs or plain string query parts. Default: Select all
* 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()"
* @param $query['SELECT'] expression to be returned, e.g. 'SELECT' => 'DISTINCT foo'. defaults to '*'
* @param $query['FROM'] table names to use for joins, e.g. 'FROM' => 'tableA LEFT JOIN tableB ON a=b'
* @param $query['JOIN'] like 'FROM'
* @param $query['CALC_FOUND_ROWS'] if true, if true member var _found_rows contains number of matching rows before LIMIT
* @param $query['LIMIT'] max number of rows to return; false for no limit
* @param $query['NOFETCH'] if true the first row is not prefetched (e.g. when directly using _result)
* @return Number of matching rows, use iterate() to fetch the next record
* @see iterate()
* @see _where()
*/
function select(...$args)
{
$query = array();
foreach ($args as $arg)
$query = array_merge($query, (array)$arg);
$this->_connect();
$result = 0;
$calc_found_rows = false;
$what = '*';
if (isset($query['SELECT']))
{
$what = $query['SELECT'];
unset($query['SELECT']);
}
unset($this->_found_rows);
if (isset($query['CALC_FOUND_ROWS']) && $query['CALC_FOUND_ROWS'])
{
$calc_found_rows = true;
$what = 'SQL_CALC_FOUND_ROWS '.$what;
}
unset($query['CALC_FOUND_ROWS']); # Always unset, so CALC_FOUND_ROWS => false doesn't generate bogus query
if (EDC('nocache') && static::$_global_key == 'it_dbi')
$what = 'SQL_NO_CACHE ' . $what;
$nofetch = $this->_nofetch = $query['NOFETCH'];
unset($query['NOFETCH']);
$this->clear();
if ($this->_result = $this->query($sql = "SELECT $what " . $this->_from($query) . " " . $this->_where($query)))
{
$result = $this->_num_rows($this->_result);
if ($calc_found_rows)
{
$count = $this->_fetch_assoc($this->query('SELECT FOUND_ROWS() AS count'))['count'];
$this->_found_rows = intval($count);
}
if (!$this->iterate() && ($this->_p['safety'] >= 2))
$this->_fatal("select(): query produced no results", $sql);
}
$this->_nofetch = !$nofetch;
return $result;
}