Refactoring SqlCriteria, SqlModel. Add SqlResultProvider.fetchField().

This commit is contained in:
2013-09-20 22:11:54 +04:00
parent 72c38e641d
commit dff9c0b2af
4 changed files with 125 additions and 49 deletions

View File

@ -17,12 +17,20 @@ class SqlCriteria
*/
private $model;
private $sql_expression;
private $sql_expression_params = array();
/**
* @param $model SqlModel
* @param $sql_expression string|null Sql expression with SELECT and FROM operators. If fetched, then SqlCriteria::select(), SqlCriteria::distinct() disabled for use.
* @param $sql_expression_params array additional params to be replaced in sql expression
*/
public function __construct($model)
public function __construct($model, $sql_expression = null, $sql_expression_params = array())
{
$this->model = $model;
$this->sql_expression = $sql_expression;
$this->sql_expression_params = $sql_expression_params;
}
/**
@ -30,22 +38,45 @@ class SqlCriteria
*/
public function find()
{
return $this->model->find($this->select, $this->distinct, $this->where, $this->order, $this->limit);
return $this->model->find($this->select, $this->distinct, $this->where, $this->order, $this->limit, null, null, $this->sql_expression, $this->sql_expression_params);
}
public function count()
{
return $this->model->find(new DbExpr('COUNT(*) as count'), '', $this->where, null, null, null, null, $this->sql_expression, $this->sql_expression_params)->fetchField('count');
}
/**
* @param $cond string Condition with "?" placeholder @ex 'field=?'
* @param $value string|array|DbExpr Value. Array transformed to DbExpr(implode(',' Array)) All elements in the array mast be integer
* @param $cond string|array Condition with "?" placeholder @ex 'field=?' or 'field=1' or array('field=?' => 1', 'field=1')
* @param $value string|array|DbExpr|null Value. Array transformed to DbExpr(implode(',' Array)) All elements in the array mast be integer
* @return SqlCriteria
* @desc Allow multiple calls
*/
public function where($cond, $value)
public function where($cond, $value = null)
{
$this->where[$cond] = $value;
if (is_null($value)) {
if (is_array($cond)) {
$this->where = $this->where + $cond;
} else {
$this->where[] = $cond;
}
} else {
$this->where[$cond] = $value;
}
return $this;
}
/**
* @param $field string
* @param $value array
* @return SqlCriteria
*/
public function whereIn($field, $value)
{
return $this->where($field . ' in ?', $value);
}
/**
* @param $field string Field @ex 'field'
* @param $order_desc bool Descendant sort direction
* @return SqlCriteria
@ -85,7 +116,7 @@ class SqlCriteria
}
/**
* @param $field string
* @param $field string|bool If true then distinct by *
* @return SqlCriteria
*/
public function distinct($field)