|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* This file is part of Aura for PHP. |
|
5
|
|
|
* |
|
6
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace Aura\SqlQuery; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* |
|
13
|
|
|
* Creates query statement objects. |
|
14
|
|
|
* |
|
15
|
|
|
* @package Aura.SqlQuery |
|
16
|
|
|
* |
|
17
|
|
|
*/ |
|
18
|
|
|
class QueryFactory |
|
19
|
|
|
{ |
|
20
|
|
|
const COMMON = 'common'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* |
|
24
|
|
|
* What database are we building for? |
|
25
|
|
|
* |
|
26
|
|
|
* @param string |
|
27
|
|
|
* |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $db; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* |
|
33
|
|
|
* Build "common" query objects regardless of database type? |
|
34
|
|
|
* |
|
35
|
|
|
* @param bool |
|
36
|
|
|
* |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $common = false; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* |
|
42
|
|
|
* A map of `table.col` names to last-insert-id names. |
|
43
|
|
|
* |
|
44
|
|
|
* @var array |
|
45
|
|
|
* |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $last_insert_id_names = array(); |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* |
|
51
|
|
|
* A Quoter for identifiers. |
|
52
|
|
|
* |
|
53
|
|
|
* @param QuoterInterface |
|
54
|
|
|
* |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $quoter; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* |
|
60
|
|
|
* A count of Query instances, used for determining $seq_bind_prefix. |
|
61
|
|
|
* |
|
62
|
|
|
* @var int |
|
63
|
|
|
* |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $instance_count = 0; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* |
|
69
|
|
|
* Constructor. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $db The database type. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $common Pass the constant self::COMMON to force common |
|
74
|
|
|
* query objects instead of db-specific ones. |
|
75
|
|
|
* |
|
76
|
|
|
*/ |
|
77
|
408 |
|
public function __construct($db, $common = null) |
|
78
|
|
|
{ |
|
79
|
408 |
|
$this->db = ucfirst(strtolower($db)); |
|
80
|
408 |
|
$this->common = ($common === self::COMMON); |
|
81
|
408 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* |
|
85
|
|
|
* Sets the last-insert-id names to be used for Insert queries.. |
|
86
|
|
|
* |
|
87
|
|
|
* @param array $last_insert_id_names A map of `table.col` names to |
|
88
|
|
|
* last-insert-id names. |
|
89
|
|
|
* |
|
90
|
|
|
* @return null |
|
91
|
|
|
* |
|
92
|
|
|
*/ |
|
93
|
67 |
|
public function setLastInsertIdNames(array $last_insert_id_names) |
|
94
|
|
|
{ |
|
95
|
67 |
|
$this->last_insert_id_names = $last_insert_id_names; |
|
96
|
67 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* |
|
100
|
|
|
* Returns a new SELECT object. |
|
101
|
|
|
* |
|
102
|
|
|
* @return Common\SelectInterface |
|
103
|
|
|
* |
|
104
|
|
|
*/ |
|
105
|
265 |
|
public function newSelect() |
|
106
|
|
|
{ |
|
107
|
265 |
|
return $this->newInstance('Select'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* |
|
112
|
|
|
* Returns a new INSERT object. |
|
113
|
|
|
* |
|
114
|
|
|
* @return Common\InsertInterface |
|
115
|
|
|
* |
|
116
|
|
|
*/ |
|
117
|
77 |
|
public function newInsert() |
|
118
|
|
|
{ |
|
119
|
77 |
|
$insert = $this->newInstance('Insert'); |
|
120
|
77 |
|
$insert->setLastInsertIdNames($this->last_insert_id_names); |
|
121
|
77 |
|
return $insert; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* |
|
126
|
|
|
* Returns a new UPDATE object. |
|
127
|
|
|
* |
|
128
|
|
|
* @return Common\UpdateInterface |
|
129
|
|
|
* |
|
130
|
|
|
*/ |
|
131
|
38 |
|
public function newUpdate() |
|
132
|
|
|
{ |
|
133
|
38 |
|
return $this->newInstance('Update'); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* |
|
138
|
|
|
* Returns a new DELETE object. |
|
139
|
|
|
* |
|
140
|
|
|
* @return Common\DeleteInterface |
|
141
|
|
|
* |
|
142
|
|
|
*/ |
|
143
|
28 |
|
public function newDelete() |
|
144
|
|
|
{ |
|
145
|
28 |
|
return $this->newInstance('Delete'); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* |
|
150
|
|
|
* Returns a new query object. |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $query The query object type. |
|
153
|
|
|
* |
|
154
|
|
|
* @return AbstractQuery |
|
155
|
|
|
* |
|
156
|
|
|
*/ |
|
157
|
408 |
|
protected function newInstance($query) |
|
158
|
|
|
{ |
|
159
|
408 |
|
$queryClass = "Aura\SqlQuery\\{$this->db}\\{$query}"; |
|
160
|
408 |
|
if ($this->common) { |
|
161
|
20 |
|
$queryClass = "Aura\SqlQuery\Common\\{$query}"; |
|
162
|
20 |
|
} |
|
163
|
|
|
|
|
164
|
408 |
|
$builderClass = "Aura\SqlQuery\\{$this->db}\\{$query}Builder"; |
|
165
|
408 |
|
if ($this->common || ! class_exists($builderClass)) { |
|
166
|
249 |
|
$builderClass = "Aura\SqlQuery\Common\\{$query}Builder"; |
|
|
|
|
|
|
167
|
249 |
|
} |
|
168
|
|
|
|
|
169
|
408 |
|
return new $queryClass( |
|
170
|
408 |
|
$this->getQuoter(), |
|
171
|
408 |
|
$this->newBuilder($query), |
|
172
|
408 |
|
$this->newSeqBindPrefix() |
|
173
|
408 |
|
); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
408 |
|
protected function newBuilder($query) |
|
177
|
|
|
{ |
|
178
|
408 |
|
$builderClass = "Aura\SqlQuery\\{$this->db}\\{$query}Builder"; |
|
179
|
408 |
|
if ($this->common || ! class_exists($builderClass)) { |
|
180
|
249 |
|
$builderClass = "Aura\SqlQuery\Common\\{$query}Builder"; |
|
181
|
249 |
|
} |
|
182
|
408 |
|
return new $builderClass(); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* |
|
187
|
|
|
* Returns the Quoter object for queries; creates one if needed. |
|
188
|
|
|
* |
|
189
|
|
|
* @return Quoter |
|
190
|
|
|
* |
|
191
|
|
|
*/ |
|
192
|
408 |
|
protected function getQuoter() |
|
193
|
|
|
{ |
|
194
|
408 |
|
if (! $this->quoter) { |
|
195
|
408 |
|
$this->quoter = $this->newQuoter(); |
|
196
|
408 |
|
} |
|
197
|
408 |
|
return $this->quoter; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
408 |
|
protected function newQuoter() |
|
201
|
|
|
{ |
|
202
|
408 |
|
$quoterClass = "Aura\SqlQuery\\{$this->db}\Quoter"; |
|
203
|
408 |
|
if ($this->common || ! class_exists($quoterClass)) { |
|
204
|
177 |
|
$quoterClass = "Aura\SqlQuery\Common\Quoter"; |
|
205
|
177 |
|
} |
|
206
|
408 |
|
return new $quoterClass(); |
|
207
|
|
|
} |
|
208
|
|
|
/** |
|
209
|
|
|
* |
|
210
|
|
|
* Returns a new sequential-placeholder prefix for a query object. |
|
211
|
|
|
* |
|
212
|
|
|
* We need these to deconflict between bound values in subselect queries. |
|
213
|
|
|
* |
|
214
|
|
|
* @return string |
|
215
|
|
|
* |
|
216
|
|
|
*/ |
|
217
|
408 |
|
protected function newSeqBindPrefix() |
|
218
|
|
|
{ |
|
219
|
408 |
|
$seq_bind_prefix = ''; |
|
220
|
408 |
|
if ($this->instance_count) { |
|
221
|
21 |
|
$seq_bind_prefix = '_' . $this->instance_count; |
|
222
|
21 |
|
} |
|
223
|
|
|
|
|
224
|
408 |
|
$this->instance_count ++; |
|
225
|
408 |
|
return $seq_bind_prefix; |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.