RSI helpers  0.1
RSI helpers
Query.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Object;
4 
5 /**
6  * Simple query builder.
7  */
8 class Query extends \Rsi\Thing{
9 
10  protected $_select = [];
11  protected $_tables = [];
12  protected $_where = [];
13  protected $_group = [];
14  protected $_having = [];
15  protected $_order = [];
16  protected $_limit = null;
17  protected $_offset = null;
18  protected $_args = [];
19 
20  public function __construct($table){
21  $this->_tables[] = $table;
22  }
23 
24  public function select($column){
25  $this->_select[] = $column;
26  return $this;
27  }
28 
29  public function join($join,$outer = false){
30  $this->_tables[] = ($outer ? 'left ' : '') . 'join ' . $join;
31  return $this;
32  }
33 
34  public function where($condition){
35  $this->_where[] = is_array($condition) ? '(' . implode(' or ',$condition) . ')' : $condition;
36  return $this;
37  }
38 
39  public function group($group){
40  $this->_group[] = $group;
41  return $this;
42  }
43 
44  public function having($condition){
45  $this->_having[] = $condition;
46  return $this;
47  }
48 
49  public function order($order){
50  $this->_order[] = $order;
51  return $this;
52  }
53 
54  public function limit($limit,$offset = 0){
55  $this->_limit = $limit;
56  $this->_offset = $offset;
57  return $this;
58  }
59 
60  public function arg($key,$value){
61  $this->_args[$key] = $value;
62  return $this;
63  }
64 
65  public function args($values = null){
66  $this->_args = array_merge($this->_args,$values);
67  return $this;
68  }
69 
70  protected function getArgs(){
71  return $this->_args;
72  }
73 
74  protected function setArgs($value){
75  $this->args($value);
76  }
77 
78  protected function getSql(){
79  return
80  'select ' . ($this->_select ? implode(',',$this->_select) : '*') .
81  "\nfrom " . implode("\n ",$this->_tables) .
82  ($this->_where ? "\nwhere " . implode("\n and ",$this->_where) : '') .
83  ($this->_group ? "\ngroup by " . implode(',',$this->_group) : '') .
84  ($this->_having ? "\nhaving " . implode(' and ',$this->_having) : '') .
85  ($this->_order ? "\norder by " . implode(',',$this->_order) : '') .
86  ($this->_limit ? "\nlimit {$this->_offset},{$this->_limit}" : '');
87  }
88 
89  protected function _set($key,$value){
90  $this->arg($key,$value);
91  }
92 
93  protected function _get($key){
94  return \Rsi\Record::get($this->_args,$key,false);
95  }
96 
97  public function __toString(){
98  return $this->getSql();
99  }
100 
101 }
_get($key)
Definition: Query.php:93
arg($key, $value)
Definition: Query.php:60
_set($key, $value)
Definition: Query.php:89
having($condition)
Definition: Query.php:44
order($order)
Definition: Query.php:49
__construct($table)
Definition: Query.php:20
Simple query builder.
Definition: Query.php:8
group($group)
Definition: Query.php:39
limit($limit, $offset=0)
Definition: Query.php:54
args($values=null)
Definition: Query.php:65
join($join, $outer=false)
Definition: Query.php:29
where($condition)
Definition: Query.php:34
select($column)
Definition: Query.php:24
setArgs($value)
Definition: Query.php:74