Documentation is available at Insert.php
1 <?php
2 /**
3 * $Id: Insert.php,v 1.1.1.1 2004/02/14 01:43:22 luckec Exp $
4 *
5 * Subclass of tgcSqlBuilder that helps to create INSERT 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 * No insert values specified for a query
16 *
17 * @access public
18 */
19 define('SQLBUILDER_ERROR_NO_INSERT_VALUES', 201);
20
21
22
23 /**
24 * Subclass of tgcSqlBuilder that helps to create INSERT sql-statements.
25 *
26 * @package tgcSqlBuilder
27 * @access public
28 * @version 1.0.0
29 * @author Carsten Lucke <luckec@tool-garage.de>
30 */
31 class tgcSqlBuilder_Insert extends tgcSqlBuilder
32 {
33
34 /**
35 * Tablename on which a sql-statement concerns.
36 *
37 * It's a numeric array, that contains the tablenames, that shall be used in an sql-statement.
38 *
39 * @access private
40 * @var array tablename
41 */
42 var $_tables;
43
44 /**
45 * Insert data.
46 *
47 * Array structure:
48 * <pre>
49 * array (
50 * $col1Name => $col1Value,
51 * $col2Name => $col2Value,
52 * ...
53 * )
54 * </pre>
55 *
56 * @access private
57 * @var array insert data
58 */
59 var $_insert = array();
60
61 /**
62 * Raw Insert data.
63 *
64 * Array structure:
65 * <pre>
66 * array (
67 * statement1,
68 * statement2,
69 * ...
70 * )
71 * </pre>
72 *
73 * @access private
74 * @var array insert data
75 */
76 var $_rawInsert = array();
77
78
79
80 /**
81 * Constructor
82 *
83 * @access public
84 * @param object $dbc PEAR::DB connection object
85 */
86 function tgcSqlBuilder_Insert(&$dbc)
87 {
88 parent::tgcSqlBuilder($dbc);
89 }
90
91 /**
92 * Generate the sql-statement.
93 *
94 * This method generates a query based on the object-information and returns it as a string.
95 *
96 * <code>
97 * $sql = new tgcSqlBuilder_Insert($dbc);
98 * $query = $sql->generateQuery();
99 * </code>
100 *
101 * @access public
102 * @return mixed sql-statement or PEAR_Error (possible error(s): SQLBUILDER_ERROR_NO_INSERT_VALUES)
103 */
104 function generateQuery()
105 {
106 // check if a table has been specified
107 if (empty($this->_tables))
108 {
109 return PEAR::raiseError (
110 'You have to specifiy a tablename first',
111 SQLBUILDER_ERROR_NO_TABLE_FOUND
112 );
113 }
114
115 $query = 'INSERT INTO ' . $this->_tables;
116
117 $insertInformation = $this->_generateInsertInformation($this->_insert, $this->_rawInsert);
118 if ($insertInformation != '') {
119 $query .= ' ' . $insertInformation;
120 $this->reset();
121
122 return $query;
123 }
124
125 return PEAR::raiseError (
126 'No insert values found.',
127 SQLBUILDER_ERROR_NO_INSERT_VALUES,
128 null,
129 null,
130 'To generate a valid query you have to specify at least one insert value'
131 );
132 }
133
134 /**
135 * Generates the INSERT information.
136 *
137 * @access private
138 * @param array $insert INSERT information
139 * @param array $rawInsert raw INSERT information
140 * @return string statement for the query on success, else empty string
141 */
142 function _generateInsertInformation($insert, $rawInsert)
143 {
144 $information = '';
145
146 if (count($insert) || count($rawInsert)) {
147 $colNames = array_keys($insert);
148 $values = array_map(array($this, 'escape'), array_values($insert));
149
150 foreach ($rawInsert as $name => $value) {
151 array_push($colNames, $name);
152 array_push($values, $value);
153 }
154
155 $information = sprintf (" (%s) VALUES (%s)", implode(', ', $colNames), implode(', ', $values));
156 $foundValues = true;
157 return $information;
158 }
159
160 return '';
161 }
162
163 /**
164 * Add the statements table.
165 *
166 * If you call this method twice, the tablename that was set in first call will be overwritten.
167 *
168 * <code>
169 * $sql = new tgcSqlBuilder_Select($dbc);
170 * $sql->addTable('users');
171 * // now the generated sql-statement would look like: DELETE FROM users ...
172 * </code>
173 *
174 * @access public
175 * @param string $tableName tablename
176 */
177 function addTable($tableName)
178 {
179 $this->_tables = $tableName;
180 }
181
182 /**
183 * Remove the tablename.
184 *
185 * @access public
186 */
187 function removeTable()
188 {
189 $this->_tables = null;
190 }
191
192 /**
193 * Add a value to insert.
194 *
195 * There are two ways of calling this method:
196 *
197 * <code>
198 * // simple method-call
199 * $sql = new tgcSqlBuilder_Insert($dbc);
200 * $sql->addInsert('col_Username', 'superman');
201 *
202 * // complex method-call, you can add more than one insert with only one method-call
203 * $insert = array (
204 * 'col_Username' => 'superman',
205 * 'col_Email' => 'superman@superheroes.com'
206 * );
207 * $sql->addInsert($insert);
208 * </code>
209 *
210 * @access public
211 * @param mixed $colName columnname or associative array containing pairs of $colName => $value
212 * @param mixed $value value to insert
213 * @return mixed true on success, else PEAR_Error (possible error(s): SQLBUILDER_ERROR_INVALID_PARAM_COMBO)
214 */
215 function addInsert($colName, $value = null)
216 {
217 if (is_array($colName) && is_null($value)) {
218 foreach ($colName as $key => $value) {
219 $this->_insert[$key] = $value;
220 }
221 return true;
222 }
223
224 if (! is_null($colName) && ! is_null($value)) {
225 $this->_insert[$colName] = $value;
226 return true;
227 }
228
229 return PEAR::raiseError( 'Invalid parameter combination',
230 SQLBUILDER_ERROR_INVALID_PARAM_COMBO,
231 null,
232 null,
233 'You called this method with an invalid parameter combination.'
234 );
235 }
236
237 /**
238 * Remove one or all insert columns.
239 *
240 * If you specify a columnname, then just this column's insert will be removed, else all inserts will be removed.
241 *
242 * @access public
243 * @param string $colName columnname
244 * @return mixed true on success, else PEAR_Error (possible error(s): SQLBUILDER_ERROR_COLUMN_DOES_NOT_EXIST)
245 */
246 function removeInsert($colName = null)
247 {
248 if(! is_null($colName)) {
249 if (isset($this->_insert[$colName])) {
250 unset($this->_insert[$colName]);
251 return true;
252 }
253
254 return PEAR::raiseError (
255 'Column doesn\'t exist',
256 SQLBUILDER_ERROR_COLUMN_DOES_NOT_EXIST,
257 null,
258 null,
259 'The column you tried to remove does not exist.'
260 );
261 }
262
263 $this->_insert = array();
264 return true;
265 }
266
267 /**
268 * Add a raw INSERT statement
269 *
270 * There are two ways of calling this method:
271 *
272 * <code>
273 * // simple method-call
274 * $sql = new tgcSqlBuilder_Insert($dbc);
275 * $sql->addInsert('creationTime', 'NOW()');
276 *
277 * // complex method-call, you can add more than one insert with only one method-call
278 * $insert = array (
279 * 'created' => 'NOW()',
280 * 'changed' => 'NOW()'
281 * );
282 * $sql->addInsert($insert);
283 * </code>
284 *
285 * @access public
286 * @param mixed $colName columnname or associative array containing pairs of $colName => $value
287 * @param mixed $value value to insert
288 * @return mixed true on success, else PEAR_Error (possible error(s): SQLBUILDER_ERROR_INVALID_PARAM_COMBO)
289 */
290 function addRawInsert($colName, $value = null)
291 {
292 if (is_array($colName) && is_null($value)) {
293 foreach ($colName as $key => $value) {
294 $this->_rawInsert[$key] = $value;
295 }
296 return true;
297 }
298
299 if (! is_null($colName) && ! is_null($value)) {
300 $this->_rawInsert[$colName] = $value;
301 return true;
302 }
303
304 return PEAR::raiseError( 'Invalid parameter combination',
305 SQLBUILDER_ERROR_INVALID_PARAM_COMBO,
306 null,
307 null,
308 'You called this method with an invalid parameter combination.'
309 );
310 }
311
312 /**
313 * Remove one or all insert columns.
314 *
315 * If you specify a columnname, then just this column's insert will be removed, else all inserts will be removed.
316 *
317 * @access public
318 * @param string $colName columnname
319 * @return mixed true on success, else PEAR_Error (possible error(s): SQLBUILDER_ERROR_COLUMN_DOES_NOT_EXIST)
320 */
321 function removeRawInsert($colName = null)
322 {
323 if(! is_null($colName)) {
324 if (isset($this->_rawInsert[$colName])) {
325 unset($this->_rawInsert[$colName]);
326 return true;
327 }
328
329 return PEAR::raiseError (
330 'Column doesn\'t exist',
331 SQLBUILDER_ERROR_COLUMN_DOES_NOT_EXIST,
332 null,
333 null,
334 'The column you tried to remove does not exist.'
335 );
336 }
337
338 $this->_rawInsert = array();
339 return true;
340 }
341
342 /**
343 * Reset the object's whole information.
344 *
345 * @access public
346 */
347 function reset()
348 {
349 $this->removeTable();
350 $this->removeInsert();
351 $this->removeRawInsert();
352 }
353
354 }
355 ?>
Documentation generated on Fri, 19 Nov 2004 23:54:03 +0100 by phpDocumentor 1.2.3