From c890d082aac9be5ce5adeb9d8f633b03bac0630a Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Tue, 17 Sep 2013 09:51:20 +0400 Subject: [PATCH] Add SqlCriteria class. Add SqlModel.find() with use SqlCriteria. --- model/SqlCriteria.php | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++ model/SqlModel.php | 34 ++++++++++++++++++-- 2 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 model/SqlCriteria.php diff --git a/model/SqlCriteria.php b/model/SqlCriteria.php new file mode 100644 index 0000000..0f86ed3 --- /dev/null +++ b/model/SqlCriteria.php @@ -0,0 +1,86 @@ + $value, 'field in ? => new DbExpr('(1,2,3)')) + * @return SqlCriteria + */ + public function where($conditions) + { + $this->where = $conditions; + return $this; + } + + /** + * @param $fields array() + * @param array $fields_to_desc + * @return SqlCriteria + */ + public function order($fields, $fields_to_desc = array()) + { + if (!is_array($fields)) { + $fields = array($fields); + } + foreach ($fields as $term) { + $term = $this->db->quoteIdentifier($term) . (in_array($term, $fields_to_desc) ? ' DESC' : ''); + } + $this->order = implode(',', $fields); + return $this; + } + + /** + * @param $limit int + * @param $offset int + * @return SqlCriteria + */ + public function limit($limit, $offset = 0) + { + if (empty($offset)) { + $this->limit = (int) $limit; + } + $this->limit = (int)$limit . ',' . (int) $offset; + return $this; + } + + /** + * @param mixed $select array @ex array('field1', 'field2', 'field3' => 'field') + * @return string + */ + public function select($select) + { + if (!is_array($select)) { + $select = array($select); + } + $this->select = implode(',', $select); + return $this; + } + + /** + * @param $field string + * @return SqlCriteria + */ + public function distinct($field) + { + $this->distinct = $field; + return $this; + } +} \ No newline at end of file diff --git a/model/SqlModel.php b/model/SqlModel.php index 7eb98ae..2d12afe 100644 --- a/model/SqlModel.php +++ b/model/SqlModel.php @@ -15,7 +15,6 @@ */ abstract class SqlModel extends Model { - /** * @param string $ident * @return string Quoted identifier. @@ -177,5 +176,36 @@ abstract class SqlModel extends Model } return $result; } - + + /** + * @param $criteria SqlCriteria + */ + public function find($criteria) + { + foreach ($criteria->select as $field => &$term) { + if (is_int($field)) { + $term = $this->db->quoteIdentifier($term); + } else { + $term = $this->db->quoteIdentifier($field) . ' as ' . $this->db->quoteIdentifier($term); + } + } + foreach ($criteria->where as $cond => &$term) { + $term = $this->db->quoteInto($cond, $term); + } + foreach ($criteria->order as $field => $term) { + if (!is_int($field) && $term == 'desc') { + $term = $term = $this->db->quoteIdentifier($field) . ' DESC'; + } else { + $term = $this->db->quoteIdentifier($term); + } + } + if ($criteria->distinct != '') { + $criteria->distinct = 'DISTINCT ' . $this->db->quoteIdentifier($criteria->distinct); + } + $sql = 'SELECT ' . (($criteria->distinct) ? $criteria->distinct : implode(',', $criteria->select)) + . ' FROM ' . $this->db->quoteIdentifier($this->table) + . (($criteria->where) ? (' WHERE ' . implode(' AND ', $criteria->where)) : '') + . (($criteria->order) ? (' ORDER BY ' . implode(',', $criteria->order)) : '') + . (($criteria->limit) ? (' LIMIT ' . implode(',',array_map(function($item){return intval($item);},explode(',',$criteria->limit)))) : ''); + } } \ No newline at end of file