/
select.class.php
192 lines (152 loc) · 5.76 KB
/
select.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
namespace PluSQL;
use Exception,Bind;
class Select
{
private $tables;
private $target;
private $initial_table;
private $connection;
private $query_props;
private $last_added;
public function __construct(Connection $connection)
{
$this->tables = array();
$this->target = NULL;
$this->initial_table = NULL;
$this->connection = $connection;
$this->query_props = array();
$this->last_added = NULL;
}
public function __call($name,$args)
{
if(!count($args))
throw new InvalidReturnSelectorException('You have used a method call in your from clause without passing in the name of a table to return');
return $this->fromClause($name,$args[0]);
}
private function fromClause($name,$return = NULL)
{
$previous = NULL;
if($this->target !== NULL)
$previous = $this->target;
if(!isset($this->tables[$name]))
{
$this->tables[$name] = new Table($name);
if($previous)
$previous->joinTable($this->tables[$name]);
}
if(!$return)
$this->target = $this->tables[$name];
else
{
if(!isset($this->tables[$return]))
throw new InvalidReturnSelectorException('You can\'t ask for '.$return.' in your from clause, because you haven\'t already accessed it');
$this->target = $this->tables[$return];
}
if($this->initial_table === NULL)
$this->initial_table = $this->target;
$this->last_added = $name;
return $this;
}
public function joinType($type)
{
if(isset($this->tables[$this->last_added]))
$this->tables[$this->last_added]->setJoinType($type);
return $this;
}
public function __get($name)
{
$ret = $this->fromClause($name);
return $ret;
}
public function buildFromClause()
{
$from_clause = '';
$stack = array($this->initial_table);
while($t = array_pop($stack))
{
if(!$from_clause)
$from_clause = 'FROM '.$t->name();
foreach($t->joinTo() as $t2)
{
try
{
$on = new OnClause($this->connection->link(),$t->name(),$t2->name(),$t2->joinType());
$onstring = $on->toString();
$from_clause .= ' '.$t2->joinType().' '.$t2->name().' ON '.$onstring;
}
catch(ManyToManyJoinException $exc)
{
$left_on = new OnClause($this->connection->link(),$t->name(),$exc->joiningTable()->name(),$t2->joinType());
$right_on = new OnClause($this->connection->link(),$exc->joiningTable()->name(),$t2->name(),$t2->joinType());
$from_clause .= ' '.$t2->joinType().' '.$exc->joiningTable()->name().' ON '.$left_on->toString();
$from_clause .= ' '.$t2->joinType().' '.$t2->name().' ON '.$right_on->toString();
}
$stack[] = $t2;
}
}
return $from_clause;
}
public function run()
{
return $this->connection->query((string)$this);
}
public function __toString()
{
$query = 'SELECT '.$this->select().' '.$this->buildFromClause();
if($where = $this->where())
$query .= ' WHERE '.$where;
if($group_by = $this->groupBy())
$query .= ' GROUP BY '.$group_by;
if($having = $this->having())
$query .= ' HAVING '.$having;
if($order_by = $this->orderBy())
$query .= ' ORDER BY '.$order_by;
if($limit = $this->limit())
$query .= ' LIMIT '.$limit;
return $query;
}
private function queryProperty($name,$value = NULL)
{
if($value !== NULL)
{
$this->query_props[$name] = $value;
$ret = $this;
}
else
{
if(isset($this->query_props[$name]))
$ret = $this->query_props[$name];
else
$ret = FALSE;
}
return $ret;
}
public function select($select = NULL)
{
return $this->queryProperty('select',$select);
}
public function where($where = NULL)
{
if($where instanceof Bind)
$where->setLink($this->connection->link());
return $this->queryProperty('where',$where);
}
public function groupBy($group_by = NULL)
{
return $this->queryProperty('group_by',$group_by);
}
public function having($having = NULL)
{
return $this->queryProperty('having',$having);
}
public function orderBy($order_by = NULL)
{
return $this->queryProperty('order_by',$order_by);
}
public function limit($limit = NULL)
{
return $this->queryProperty('limit',$limit);
}
}
class InvalidReturnSelectorException extends Exception{}