You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
1.6 KiB

  1. <?php
  2. class SqlCriteria
  3. {
  4. public $select = '*';
  5. public $distinct = '';
  6. public $where = array();
  7. public $order = array();
  8. public $limit = '';
  9. /**
  10. * @return SqlCriteria
  11. */
  12. public static function getInstance()
  13. {
  14. return self;
  15. }
  16. /**
  17. * @param $conditions array @ex array('field=?' => $value, 'field in ? => new DbExpr('(1,2,3)'))
  18. * @return SqlCriteria
  19. */
  20. public function where($conditions)
  21. {
  22. $this->where = $conditions;
  23. return $this;
  24. }
  25. /**
  26. * @param $fields array @ex array('field1', 'field2' => SqlModel::ORDER_DESC)
  27. * @return SqlCriteria
  28. */
  29. public function order($fields)
  30. {
  31. if (!is_array($fields)) {
  32. $fields = array($fields);
  33. }
  34. $this->order = $fields;
  35. return $this;
  36. }
  37. /**
  38. * @param $limit int
  39. * @param $offset int
  40. * @return SqlCriteria
  41. */
  42. public function limit($limit, $offset = 0)
  43. {
  44. if (empty($offset)) {
  45. $this->limit = (int) $limit;
  46. }
  47. $this->limit = (int)$limit . ',' . (int) $offset;
  48. return $this;
  49. }
  50. /**
  51. * @param mixed $select array @ex array('field1', 'field2', 'field3' => 'field')
  52. * @return string
  53. */
  54. public function select($select)
  55. {
  56. if (!is_array($select)) {
  57. $select = array($select);
  58. }
  59. $this->select = $select;
  60. return $this;
  61. }
  62. /**
  63. * @param $field string
  64. * @return SqlCriteria
  65. */
  66. public function distinct($field)
  67. {
  68. $this->distinct = $field;
  69. return $this;
  70. }
  71. }