2013-09-17 09:51:20 +04:00
< ? php
class SqlCriteria
{
2013-09-19 10:05:00 +04:00
private $select = array ();
2013-09-17 09:51:20 +04:00
2013-09-19 10:05:00 +04:00
private $distinct = '' ;
2013-09-17 09:51:20 +04:00
2013-09-19 10:05:00 +04:00
private $where = array ();
2013-09-17 09:51:20 +04:00
2013-10-29 16:42:33 +04:00
private $group_by = array ();
2013-09-19 10:05:00 +04:00
private $order = array ( 'sort' => array (), 'order' => array ());
2013-09-17 09:51:20 +04:00
2013-09-19 10:05:00 +04:00
private $limit = '' ;
2013-09-17 09:51:20 +04:00
/**
2013-09-19 10:05:00 +04:00
* @ var SqlModel
2013-09-17 09:51:20 +04:00
*/
2013-09-19 10:05:00 +04:00
private $model ;
2013-09-20 22:11:54 +04:00
private $sql_expression ;
private $sql_expression_params = array ();
2013-09-19 12:54:10 +04:00
/**
* @ param $model SqlModel
2013-09-20 22:11:54 +04:00
* @ 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
2013-09-19 12:54:10 +04:00
*/
2013-09-20 22:11:54 +04:00
public function __construct ( $model , $sql_expression = null , $sql_expression_params = array ())
2013-09-17 09:51:20 +04:00
{
2013-09-19 12:54:10 +04:00
$this -> model = $model ;
2013-09-20 22:11:54 +04:00
$this -> sql_expression = $sql_expression ;
$this -> sql_expression_params = $sql_expression_params ;
2013-09-19 12:54:10 +04:00
}
2013-09-19 10:05:00 +04:00
2013-09-19 12:54:10 +04:00
/**
* @ return SqlResultProvider
*/
public function find ()
{
2013-10-29 16:42:33 +04:00
return $this -> model -> find ( $this -> select , $this -> distinct , $this -> where , $this -> order , $this -> limit , null , $this -> group_by , $this -> sql_expression , $this -> sql_expression_params );
2013-09-20 22:11:54 +04:00
}
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' );
2013-09-17 09:51:20 +04:00
}
/**
2013-09-20 22:11:54 +04:00
* @ 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
2013-09-17 09:51:20 +04:00
* @ return SqlCriteria
2013-09-19 10:05:00 +04:00
* @ desc Allow multiple calls
2013-09-17 09:51:20 +04:00
*/
2013-09-20 22:11:54 +04:00
public function where ( $cond , $value = null )
2013-09-17 09:51:20 +04:00
{
2013-09-20 22:11:54 +04:00
if ( is_null ( $value )) {
if ( is_array ( $cond )) {
$this -> where = $this -> where + $cond ;
} else {
$this -> where [] = $cond ;
}
} else {
$this -> where [ $cond ] = $value ;
}
2013-09-17 09:51:20 +04:00
return $this ;
}
/**
2013-09-20 22:11:54 +04:00
* @ param $field string
* @ param $value array
* @ return SqlCriteria
*/
public function whereIn ( $field , $value )
{
return $this -> where ( $field . ' in ?' , $value );
}
/**
2013-10-17 21:23:32 +04:00
* @ param $field string
* @ param $value array
* @ return SqlCriteria
*/
public function whereNotIn ( $field , $value )
{
return $this -> where ( $field . ' not in ?' , $value );
}
/**
2013-10-20 13:37:38 +04:00
* @ param $field string
* @ param $value array
* @ return SqlCriteria
*/
public function whereNot ( $field , $value )
{
return $this -> whereNotIn ( $field , $value );
}
2013-10-29 16:42:33 +04:00
public function groupBy ( $fields )
{
if ( is_array ( $fields )) {
$this -> group_by = $this -> group_by + $fields ;
} else {
$this -> group_by [] = $fields ;
}
return $this ;
}
2013-10-20 13:37:38 +04:00
/**
2013-09-19 10:05:00 +04:00
* @ param $field string Field @ ex 'field'
* @ param $order_desc bool Descendant sort direction
2013-09-17 09:51:20 +04:00
* @ return SqlCriteria
2013-09-19 10:05:00 +04:00
* @ desc Allow multiple calls
2013-09-17 09:51:20 +04:00
*/
2013-09-19 10:05:00 +04:00
public function order ( $field , $order_desc = false )
2013-09-17 09:51:20 +04:00
{
2013-09-19 10:05:00 +04:00
$this -> order [ 'sort' ][] = $field ;
if ( $order_desc ) {
$this -> order [ 'order' ][ $field ] = 'desc' ;
2013-09-17 09:51:20 +04:00
}
return $this ;
}
/**
* @ param $limit int
* @ param $offset int
* @ return SqlCriteria
*/
public function limit ( $limit , $offset = 0 )
{
2013-09-19 10:05:00 +04:00
if ( $offset ) {
$this -> limit = ( int ) $offset . ',' ;
2013-09-17 09:51:20 +04:00
}
2013-09-19 10:05:00 +04:00
$this -> limit .= ( int ) $limit ;
2013-09-17 09:51:20 +04:00
return $this ;
}
/**
2013-10-09 07:07:34 +04:00
* @ param string | array $fields
* @ ex SqlCriteria :: select ( 'field' )
* @ ex SqlCriteria -> select ( array ( 'field1' , 'field2' ))
* @ ex SqlCriteria -> select ( 'field1,field2' )
2013-09-19 10:05:00 +04:00
* @ return SqlCriteria
2013-09-17 09:51:20 +04:00
*/
2013-10-09 07:07:34 +04:00
public function select ( $fields )
2013-09-17 09:51:20 +04:00
{
2013-10-09 07:07:34 +04:00
$fields = explode ( ',' , $fields );
2013-10-10 15:28:41 +04:00
$this -> select = array_merge ( $this -> select , $fields );
2013-09-17 09:51:20 +04:00
return $this ;
}
/**
2013-09-20 22:11:54 +04:00
* @ param $field string | bool If true then distinct by *
2013-09-17 09:51:20 +04:00
* @ return SqlCriteria
*/
public function distinct ( $field )
{
$this -> distinct = $field ;
return $this ;
}
}