Documentation is available at Delete.php
1 <?php
2 /**
3 * $Id: Delete.php,v 1.1.1.1 2004/02/14 01:43:23 luckec Exp $
4 *
5 * Subclass of tgcSqlBuilder that helps to create DELETE sql-statements.
6 *
7 * @package tgcSqlBuilder
8 * @author Carsten Lucke <luckec@tool-garage.de>
9 * @copyright Carsten Lucke <http://www.tool-garage.de>
10 */
11
12
13
14 /**
15 * Subclass of tgcSqlBuilder that helps to create DELETE sql-statements.
16 *
17 * @package tgcSqlBuilder
18 * @access public
19 * @version 1.0.0
20 * @author Carsten Lucke <luckec@tool-garage.de>
21 */
22 class tgcSqlBuilder_Delete extends tgcSqlBuilder
23 {
24
25 /**
26 * Keeps the ORDER BY information.
27 *
28 * The array's structure looks like that:
29 * <pre>
30 * array(
31 * array(
32 * 'table' => $tableName,
33 * 'column' => $columnName,
34 * 'direction' => $direction
35 * ),
36 * array( ... ),
37 * ...
38 * )
39 * </pre>
40 *
41 * @access private
42 * @var array ORDER BY information
43 *
44 *
45 */
46 var $_orderBy = array();
47
48 /**
49 * Keeps the WHERE information.
50 *
51 * Structure:
52 * <pre>
53 * array(
54 * 'OR' => array(
55 * array(
56 * 'table' => $tableName,
57 * 'column' => $columnName,
58 * 'value' => $value,
59 * 'compOp' => $comparisonOperator,
60 * },
61 * ...
62 * ),
63 * 'AND' => array(
64 * array(
65 * 'table' => $tableName,
66 * 'column' => $columnName,
67 * 'value' => $value,
68 * 'compOp' => $comparisonOperator,
69 * },
70 * ...
71 * )
72 * </pre>
73 *
74 * @access private
75 * @var array WHERE information
76 */
77 var $_where = array(
78 SQLBUILDER_LOGICAL_AND => array(),
79 SQLBUILDER_LOGICAL_OR => array()
80 );
81
82 /**
83 * Keeps the raw WHERE information.
84 *
85 * Structure:
86 * <pre>
87 * array(
88 * 'OR' => array(
89 * $statement1,
90 * $statement2,
91 * ...
92 * ),
93 * 'AND' => array(
94 * $statement1,
95 * $statement2,
96 * ...
97 * )
98 * </pre>
99 *
100 * @access private
101 * @var array WHERE information
102 */
103 var $_rawWhere = array(
104 SQLBUILDER_LOGICAL_AND => array(),
105 SQLBUILDER_LOGICAL_OR => array()
106 );
107
108 /**
109 * Keeps the LIMIT information.
110 *
111 * @access private
112 * @var int limit information
113 */
114 var $_limit;
115
116 /**
117 * Tablename on which a sql-statement concerns.
118 *
119 * It's a numeric array, that contains the tablenames, that shall be used in an sql-statement.
120 *
121 * @access private
122 * @var array tablename
123 */
124 var $_tables;
125
126
127
128 /**
129 * Constructor
130 *
131 * @access public
132 * @param object $dbc PEAR::DB connection object
133 */
134 function tgcSqlBuilder_Delete(&$dbc)
135 {
136 parent::tgcSqlBuilder($dbc);
137 }
138
139 /**
140 * Generate the sql-statement.
141 *
142 * This method generates a query based on the object-information and returns it as a string.
143 *
144 * <code>
145 * $sql = new tgcSqlBuilder_Delete($dbc);
146 * $query = $sql->generateQuery();
147 * </code>
148 *
149 * @access public
150 * @return string sql-statement
151 */
152 function generateQuery()
153 {
154 // check if a table has been specified
155 if (empty($this->_tables))
156 {
157 return PEAR::raiseError (
158 'You have to specifiy a tablename first',
159 SQLBUILDER_ERROR_NO_TABLE_FOUND
160 );
161 }
162
163 $query = 'DELETE FROM ' . $this->_tables;
164
165 if (count($this->_where[SQLBUILDER_LOGICAL_AND]) || count($this->_where[SQLBUILDER_LOGICAL_OR])) {
166 $query .= ' WHERE ' . $this->_generateWhereInformation($this->_where, $this->_rawWhere);
167 }
168
169 if (count($this->_orderBy)) {
170 $query .= ' ORDER BY ' . $this->_generateOrderByInformation($this->_orderBy);
171 }
172
173 if (! is_null($this->_limit)) {
174 $query .= ' LIMIT ' . $this->_limit;
175 }
176
177 $this->reset();
178 return $query;
179 }
180
181 /**
182 * Add the statements table.
183 *
184 * If you call this method twice, the tablename that was set in first call will be overwritten.
185 *
186 * <code>
187 * $sql = new tgcSqlBuilder_Delete($dbc);
188 * $sql->addTable('users');
189 * // now the generated sql-statement would look like: DELETE FROM users ...
190 * </code>
191 *
192 * @access public
193 * @param string $tableName tablename
194 */
195 function addTable($tableName)
196 {
197 $this->_tables = $tableName;
198 }
199
200 /**
201 * Remove the tablename.
202 *
203 * @access public
204 */
205 function removeTable()
206 {
207 $this->_tables = null;
208 }
209
210 /**
211 * Add an ORDER BY setting.
212 *
213 * The parameter $direction can be either SQLBUILDER_ORDER_ASC or SQLBUILDER_ORDER_DESC.
214 * If none is specified, then SQLBUILDER_ORDER_ASC will be used.
215 *
216 * You can also leave $column null, if you want to order by an alias.
217 * <code>
218 * $sql = new tgcSqlBuilder_Delete($dbc);
219 *
220 * // ... ORDER BY alias1 ASC ...
221 * $sql->addOrderBy('alias1');
222 *
223 * // ... ORDER BY alias2 DESC ...
224 * $sql->addOrderBy('alias2', null, SQLBUILDER_ORDER_DESC);
225 * </code>
226 *
227 * If a setting for this table/column exists, it will be overwritten.
228 *
229 * @access public
230 * @param string $table tablename
231 * @param string $column columnname
232 * @param string $direction oder direction
233 */
234 function addOrderBy($table, $column = null, $direction = null)
235 {
236 $direction = is_null($direction) ? SQLBUILDER_ORDER_ASC : $direction;
237
238 if (! is_null($column)) {
239 $orderBy = array(
240 'table' => $table,
241 'column' => $column,
242 'direction' => $direction
243 );
244 } else {
245 $orderBy = array(
246 'table' => $table,
247 'column' => null,
248 'direction' => $direction
249 );
250 }
251
252 $exists = false;
253 foreach ($this->_orderBy as $index => $orderByData) {
254 if ($orderByData['table'] == $table && $orderByData['column'] == $column) {
255 $exists = true;
256 break;
257 }
258 }
259 if ($exists) {
260 $this->_orderBy[$index] = $orderBy;
261 return;
262 }
263
264 array_push($this->_orderBy, $orderBy);
265 }
266
267 /**
268 * Remove an ORDER BY setting.
269 *
270 * If you don't specify any parameter, then all ORDER BY information will be removed.
271 * If you specify a tablename and a columnname, then this specific ORDER BY setting will be removed.
272 *
273 * @access public
274 * @param string $table tablename
275 * @param string $column columnname
276 * @return mixed true on success or PEAR_Error (possible error(s): SQLBUILDER_ERROR_INVALID_PARAM_COMBO)
277 */
278 function removeOrderBy($table = null, $column = null)
279 {
280 // delete all
281 if (is_null($table) && is_null($column)) {
282 $this->_orderBy = array();
283 return true;
284 }
285
286 // delete table.column
287 if (! is_null($table) && ! is_null($column)) {
288 $newOrderBy = array();
289 foreach ($this->_orderBy as $orderByData) {
290 if (! ($orderByData['table'] == $table && $orderByData['column'] == $column)) {
291 array_push($newOrderBy, $orderByData);
292 }
293 }
294 $this->_orderBy = $newOrderBy;
295 return true;
296 }
297
298 // delete alias
299 if (! is_null($table) && is_null($column)) {
300 $newOrderBy = array();
301 foreach ($this->_orderBy as $orderByData) {
302 if (! ($orderByData['table'] == $table && $orderByData['column'] == null)) {
303 array_push($newOrderBy, $orderByData);
304 }
305 }
306 $this->_orderBy = $newOrderBy;
307 return true;
308 }
309
310 return PEAR::raiseError (
311 'The combination of parameters is invalid',
312 SQLBUILDER_ERROR_INVALID_PARAM_COMBO,
313 null,
314 null,
315 'You have to call the method with none or two parameters'
316 );
317 }
318
319 /**
320 * Add a WHERE statement.
321 *
322 * Possible comparison operators are:
323 * SQLBUILDER_COMP_EQUAL, SQLBUILDER_COMP_NOT_EQUAL, SQLBUILDER_COMP_LESSER_THAN, SQLBUILDER_COMP_LESSER_EQUAL,
324 * SQLBUILDER_COMP_GREATER_EQUAL, SQLBUILDER_COMP_GREATER_THAN, SQLBUILDER_COMP_STARTSWITH, SQLBUILDER_COMP_CONTAINS,
325 * SQLBUILDER_COMP_ENDSWITH, SQLBUILDER_COMP_BETWEEN
326 *
327 * If none is specified then SQLBUILDER_COMP_EQUAL will be used.
328 *
329 * Possible logical expressions are:
330 * SQLBUILDER_LOGICAL_AND, SQLBUILDER_LOGICAL_OR
331 *
332 * If none is specified, then SQLBUILDER_LOGICAL_AND will be used.
333 *
334 * When you are using SQLBUILDER_COMP_BETWEEN, then specify $values as a numerical array with two values
335 * in correct order.
336 *
337 * @access public
338 * @param string $table tablename
339 * @param string $column columnname
340 * @param mixed $value value(s)
341 * @param string $compOp comparison operator
342 * @param string $logic logical linkup
343 */
344 function addWhere($table, $column, $value, $compOp = null, $logic = null)
345 {
346 $this->_addWhereHaving($this->_where, $table, $column, $value, $compOp, $logic);
347 }
348
349 /**
350 * Remove a WHERE statement.
351 *
352 * If you don't specify any parameter, then all WHERE information will be removed.
353 * If you specify a tablename and a columnname, then this specific ORDER BY setting will be removed.
354 *
355 * @access public
356 * @param string $table tablename
357 * @param string $column columnname
358 * @param string $logic logical operation (possible values: SQLBUILDER_LOGICAL_AND, SQLBUILDER_LOGICAL_OR)
359 * @return mixed true on success or PEAR_Error (possible error(s): SQLBUILDER_ERROR_INVALID_PARAM_COMBO)
360 */
361 function removeWhere($table = null, $column = null, $logic = null)
362 {
363 return $this->_removeWhereHaving($this->_where, $table, $column, $logic);
364 }
365
366 /**
367 * Set the LIMIT for the sql-statement.
368 *
369 * @access public
370 * @param int $offset offset
371 * @param int $rows rows
372 */
373 function setLimit($rows)
374 {
375 $this->_limit = $rows;
376 }
377
378 /**
379 * Remove the LIMIT for the sql-statement.
380 *
381 * @access public
382 */
383 function unsetLimit()
384 {
385 $this->_limit = null;
386 }
387
388 /**
389 * Reset the object's whole information.
390 *
391 * @access public
392 */
393 function reset()
394 {
395 $this->removeTable();
396 $this->removeWhere();
397 $this->removeRawWhere();
398 $this->removeOrderBy();
399 $this->unsetLimit();
400 }
401
402 /**
403 * Add a raw WHERE statement.
404 *
405 * You can add a raw WHERE statement and define a logical operator. As default this is the logical AND.
406 *
407 * @access public
408 * @param string $statement raw WHERE statement
409 * @param string $logic SQLBUILDER_LOGICAL_AND or SQLBUILDER_LOGICAL_OR
410 */
411 function addRawWhere($statement, $logic = SQLBUILDER_LOGICAL_AND)
412 {
413 $this->_addRawWhereHaving($this->_rawWhere, $statement, $logic);
414 }
415
416 /**
417 * Remove the raw WHERE statements, that have been stored so far.
418 *
419 * @access public
420 * @param string $logic SQLBUILDER_LOGICAL_AND or SQLBUILDER_LOGICAL_OR
421 */
422 function removeRawWhere($logic = null)
423 {
424 return $this->_removeRawWhereHaving($this->_rawWhere, $logic);
425 }
426
427 }
428 ?>
Documentation generated on Fri, 19 Nov 2004 23:54:02 +0100 by phpDocumentor 1.2.3