1 | <?php |
||
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 | * The quote prefix/suffix to use for each type. |
||
43 | * |
||
44 | * @param array |
||
45 | * |
||
46 | */ |
||
47 | protected $quotes = array( |
||
48 | 'Common' => array('"', '"'), |
||
49 | 'Mysql' => array('`', '`'), |
||
50 | 'Pgsql' => array('"', '"'), |
||
51 | 'Sqlite' => array('"', '"'), |
||
52 | 'Sqlsrv' => array('[', ']'), |
||
53 | ); |
||
54 | |||
55 | /** |
||
56 | * |
||
57 | * The quote name prefix extracted from `$quotes`. |
||
58 | * |
||
59 | * @var string |
||
60 | * |
||
61 | */ |
||
62 | protected $quote_name_prefix; |
||
63 | |||
64 | /** |
||
65 | * |
||
66 | * The quote name suffix extracted from `$quotes`. |
||
67 | * |
||
68 | * @var string |
||
69 | * |
||
70 | */ |
||
71 | protected $quote_name_suffix; |
||
72 | |||
73 | /** |
||
74 | * |
||
75 | * A map of `table.col` names to last-insert-id names. |
||
76 | * |
||
77 | * @var array |
||
78 | * |
||
79 | */ |
||
80 | protected $last_insert_id_names = array(); |
||
81 | |||
82 | /** |
||
83 | * |
||
84 | * A Quoter for identifiers. |
||
85 | * |
||
86 | * @param Quoter |
||
87 | * |
||
88 | */ |
||
89 | protected $quoter; |
||
90 | |||
91 | /** |
||
92 | * |
||
93 | * A count of Query instances, used for determining $seq_bind_prefix. |
||
94 | * |
||
95 | * @var int |
||
96 | * |
||
97 | */ |
||
98 | protected $instance_count = 0; |
||
99 | |||
100 | /** |
||
101 | * |
||
102 | * Constructor. |
||
103 | * |
||
104 | * @param string $db The database type. |
||
105 | * |
||
106 | * @param string $common Pass the constant self::COMMON to force common |
||
107 | * query objects instead of db-specific ones. |
||
108 | * |
||
109 | */ |
||
110 | 397 | public function __construct( |
|
111 | $db, |
||
112 | 357 | $common = null |
|
113 | ) { |
||
114 | $this->db = ucfirst(strtolower($db)); |
||
115 | 397 | $this->common = ($common === self::COMMON); |
|
116 | 397 | $this->quote_name_prefix = $this->quotes[$this->db][0]; |
|
117 | 397 | $this->quote_name_suffix = $this->quotes[$this->db][1]; |
|
118 | 40 | } |
|
119 | |||
120 | /** |
||
121 | * |
||
122 | * Sets the last-insert-id names to be used for Insert queries.. |
||
123 | * |
||
124 | * @param array $last_insert_id_names A map of `table.col` names to |
||
125 | * last-insert-id names. |
||
126 | * |
||
127 | * @return null |
||
128 | * |
||
129 | */ |
||
130 | 66 | public function setLastInsertIdNames(array $last_insert_id_names) |
|
131 | { |
||
132 | 66 | $this->last_insert_id_names = $last_insert_id_names; |
|
133 | } |
||
134 | |||
135 | /** |
||
136 | * |
||
137 | * Returns a new SELECT object. |
||
138 | * |
||
139 | * @return Common\SelectInterface |
||
140 | * |
||
141 | */ |
||
142 | public function newSelect() |
||
143 | { |
||
144 | return $this->newInstance('Select'); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * |
||
149 | * Returns a new INSERT object. |
||
150 | * |
||
151 | * @return Common\InsertInterface |
||
152 | * |
||
153 | */ |
||
154 | 76 | public function newInsert() |
|
155 | { |
||
156 | $insert = $this->newInstance('Insert'); |
||
157 | $insert->setLastInsertIdNames($this->last_insert_id_names); |
||
158 | 76 | return $insert; |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * |
||
163 | * Returns a new UPDATE object. |
||
164 | * |
||
165 | * @return Common\UpdateInterface |
||
166 | * |
||
167 | */ |
||
168 | public function newUpdate() |
||
169 | { |
||
170 | return $this->newInstance('Update'); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * |
||
175 | * Returns a new DELETE object. |
||
176 | * |
||
177 | * @return Common\DeleteInterface |
||
178 | * |
||
179 | */ |
||
180 | public function newDelete() |
||
181 | { |
||
182 | return $this->newInstance('Delete'); |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * |
||
187 | * Returns a new query object. |
||
188 | * |
||
189 | * @param string $query The query object type. |
||
190 | * |
||
191 | * @return AbstractQuery |
||
192 | * |
||
193 | */ |
||
194 | 376 | protected function newInstance($query) |
|
195 | { |
||
196 | 376 | if ($this->common) { |
|
197 | 20 | $class = "Aura\SqlQuery\Common"; |
|
198 | } else { |
||
199 | 356 | $class = "Aura\SqlQuery\\{$this->db}"; |
|
200 | 20 | } |
|
201 | |||
202 | 376 | $class .= "\\{$query}"; |
|
203 | |||
204 | return new $class( |
||
205 | 376 | $this->getQuoter(), |
|
206 | 376 | $this->newSeqBindPrefix() |
|
207 | ); |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * |
||
212 | * Returns the Quoter object for queries; creates one if needed. |
||
213 | * |
||
214 | * @return Quoter |
||
215 | * |
||
216 | */ |
||
217 | 397 | protected function getQuoter() |
|
218 | { |
||
219 | 376 | if (! $this->quoter) { |
|
220 | 397 | $this->quoter = new Quoter( |
|
221 | 397 | $this->quote_name_prefix, |
|
222 | 397 | $this->quote_name_suffix |
|
223 | ); |
||
224 | } |
||
225 | |||
226 | 376 | return $this->quoter; |
|
227 | } |
||
228 | |||
229 | /** |
||
230 | * |
||
231 | * Returns a new sequential-placeholder prefix for a query object. |
||
232 | * |
||
233 | * We need these to deconflict between bound values in subselect queries. |
||
234 | * |
||
235 | * @return string |
||
236 | * |
||
237 | */ |
||
238 | 376 | protected function newSeqBindPrefix() |
|
248 | } |
||
249 |