Rev 47 | Rev 51 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 47 | Rev 48 | ||
---|---|---|---|
1 | <?php
|
1 | <?php
|
2 | 2 | ||
3 | require_once __DIR__ . '/../global.inc'; |
3 | require_once __DIR__ . '/../global.inc'; |
4 | require_once __DIR__ . '/../AbstractModel.php'; |
4 | require_once __DIR__ . '/../AbstractModel.php'; |
5 | 5 | ||
6 | /**
|
6 | /**
|
7 | * Generic database model class using PDO (PHP Data Objects)
|
7 | * Generic database model class using PDO (PHP Data Objects)
|
8 | *
|
8 | *
|
9 | * @property-read PDO $connection
|
9 | * @property-read PDO $connection
|
10 | * Database connection. Established on read access to this
|
10 | * Database connection. Established on read access to this
|
11 | * property if not yet established.
|
11 | * property if not yet established.
|
12 | * @property-read array $lastError
|
12 | * @property-read array $lastError
|
13 | * Last error information of the database operation.
|
13 | * Last error information of the database operation.
|
14 | * See {@link PDOStatement::errorInfo()}.
|
14 | * See {@link PDOStatement::errorInfo()}.
|
15 | * @property-read string $lastInsertId
|
15 | * @property-read string $lastInsertId
|
16 | * ID of the last inserted row, or the last value from a sequence object,
|
16 | * ID of the last inserted row, or the last value from a sequence object,
|
17 | * depending on the underlying driver. May not be supported by all databases.
|
17 | * depending on the underlying driver. May not be supported by all databases.
|
18 | * @property-read array $lastResult
|
18 | * @property-read array $lastResult
|
19 | * Last result of the database operation
|
19 | * Last result of the database operation
|
20 | * @property-read boolean $lastSuccess
|
20 | * @property-read boolean $lastSuccess
|
21 | * Last success value of the database operation
|
21 | * Last success value of the database operation
|
22 | * @author Thomas Lahn
|
22 | * @author Thomas Lahn
|
23 | */
|
23 | */
|
24 | class Database extends AbstractModel |
24 | class Database extends AbstractModel |
25 | {
|
25 | {
|
26 | /* Access properties */
|
26 | /* Access properties */
|
27 | 27 | ||
28 | /**
|
28 | /**
|
29 | * DSN of the database
|
29 | * DSN of the database
|
30 | * @var string
|
30 | * @var string
|
31 | */
|
31 | */
|
32 | protected $_dsn = ''; |
32 | protected $_dsn = ''; |
33 | 33 | ||
34 | /**
|
34 | /**
|
35 | * Username to access the database
|
35 | * Username to access the database
|
36 | * @var string
|
36 | * @var string
|
37 | */
|
37 | */
|
38 | protected $_username; |
38 | protected $_username; |
39 | 39 | ||
40 | /**
|
40 | /**
|
41 | * Password to access the database
|
41 | * Password to access the database
|
42 | * @var string
|
42 | * @var string
|
43 | */
|
43 | */
|
44 | protected $_password; |
44 | protected $_password; |
45 | 45 | ||
46 | /**
|
46 | /**
|
47 | * PDO driver-specific options
|
47 | * PDO driver-specific options
|
48 | * @var array
|
48 | * @var array
|
49 | */
|
49 | */
|
50 | protected $_options = array(); |
50 | protected $_options = array(); |
51 | 51 | ||
52 | /**
|
52 | /**
|
53 | * Database-specific string to use for quoting a name or value
|
53 | * Database-specific string to use for quoting a name or value
|
54 | * left-hand side (for security reasons and to prevent a name
|
54 | * left-hand side (for security reasons and to prevent a name
|
55 | * from being parsed as a keyword).
|
55 | * from being parsed as a keyword).
|
56 | * @var string
|
56 | * @var string
|
57 | */
|
57 | */
|
58 | protected $_leftQuote = ''; |
58 | protected $_leftQuote = ''; |
59 | 59 | ||
60 | /**
|
60 | /**
|
61 | * Database-specific string to use for quoting a name or value
|
61 | * Database-specific string to use for quoting a name or value
|
62 | * left-hand side (for security reasons and to prevent a name
|
62 | * left-hand side (for security reasons and to prevent a name
|
63 | * from being parsed as a keyword).
|
63 | * from being parsed as a keyword).
|
64 | * @var string
|
64 | * @var string
|
65 | */
|
65 | */
|
66 | protected $_rightQuote = ''; |
66 | protected $_rightQuote = ''; |
67 | 67 | ||
68 | /* Status properties */
|
68 | /* Status properties */
|
69 | 69 | ||
70 | /**
|
70 | /**
|
71 | * Database connection
|
71 | * Database connection
|
72 | * @var PDO
|
72 | * @var PDO
|
73 | */
|
73 | */
|
74 | protected $_connection; |
74 | protected $_connection; |
75 | 75 | ||
76 | /**
|
76 | /**
|
77 | * Last success value of the database operation
|
77 | * Last success value of the database operation
|
78 | * @var boolean
|
78 | * @var boolean
|
79 | */
|
79 | */
|
80 | protected $_lastSuccess; |
80 | protected $_lastSuccess; |
81 | 81 | ||
82 | /**
|
82 | /**
|
83 | * Last error information of the database operation
|
83 | * Last error information of the database operation
|
84 | * @var array
|
84 | * @var array
|
85 | */
|
85 | */
|
86 | protected $_lastError; |
86 | protected $_lastError; |
87 | 87 | ||
88 | /**
|
88 | /**
|
89 | * Last result of the database operation
|
89 | * Last result of the database operation
|
90 | * @var array
|
90 | * @var array
|
91 | */
|
91 | */
|
92 | protected $_lastResult; |
92 | protected $_lastResult; |
93 | 93 | ||
94 | /**
|
94 | /**
|
95 | * ID of the last inserted row, or the last value from a sequence object,
|
95 | * ID of the last inserted row, or the last value from a sequence object,
|
96 | * depending on the underlying driver. May not be supported by all databases.
|
96 | * depending on the underlying driver. May not be supported by all databases.
|
97 | * @var string
|
97 | * @var string
|
98 | */
|
98 | */
|
99 | protected $_lastInsertId = ''; |
99 | protected $_lastInsertId = ''; |
100 | 100 | ||
101 | /**
|
101 | /**
|
102 | * Creates a new <code>Database</code> instance.
|
102 | * Creates a new <code>Database</code> instance.
|
103 | *
|
103 | *
|
104 | * Each of the parameters is optional and can also be given
|
104 | * Each of the parameters is optional and can also be given
|
105 | * by a protected property where the parameter name is preceded
|
105 | * by a protected property where the parameter name is preceded
|
106 | * by <code>_</code>. Parameter values overwrite the default
|
106 | * by <code>_</code>. Parameter values overwrite the default
|
107 | * property values. It is recommended to use default property
|
107 | * property values. It is recommended to use default property
|
108 | * values of inheriting classes except for small applications
|
108 | * values of inheriting classes except for small applications
|
109 | * and testing purposes.
|
109 | * and testing purposes.
|
110 | *
|
110 | *
|
111 | * @param string $dsn
|
111 | * @param string $dsn
|
112 | * @param string $username
|
112 | * @param string $username
|
113 | * @param string $password
|
113 | * @param string $password
|
114 | * @param array $options
|
114 | * @param array $options
|
115 | * @see PDO::__construct()
|
115 | * @see PDO::__construct()
|
116 | */
|
116 | */
|
117 | public function __construct ($dsn = '', $username = null, |
117 | public function __construct ($dsn = '', $username = null, |
118 | $password = null, array $options = array()) |
118 | $password = null, array $options = array()) |
119 | {
|
119 | {
|
120 | if ($dsn !== '') |
120 | if ($dsn !== '') |
121 | {
|
121 | {
|
122 | $this->_dsn = $dsn; |
122 | $this->_dsn = $dsn; |
123 | }
|
123 | }
|
124 | 124 | ||
125 | if ($username !== null) |
125 | if ($username !== null) |
126 | {
|
126 | {
|
127 | $this->_username = $username; |
127 | $this->_username = $username; |
128 | }
|
128 | }
|
129 | 129 | ||
130 | if ($password !== null) |
130 | if ($password !== null) |
131 | {
|
131 | {
|
132 | $this->_password = $password; |
132 | $this->_password = $password; |
133 | }
|
133 | }
|
134 | 134 | ||
135 | if ($options) |
135 | if ($options) |
136 | {
|
136 | {
|
137 | $this->_options = $options; |
137 | $this->_options = $options; |
138 | }
|
138 | }
|
139 | }
|
139 | }
|
140 | 140 | ||
141 | /**
|
141 | /**
|
142 | * Reads the connection configuration for this database
|
142 | * Reads the connection configuration for this database
|
143 | * from the configuration file, application/.config
|
143 | * from the configuration file, application/.config
|
144 | *
|
144 | *
|
145 | * There must be an INI section named "database:" followed
|
145 | * There must be an INI section named "database:" followed
|
146 | * by the value of the <code>$_dbname</code> property
|
146 | * by the value of the <code>$_dbname</code> property
|
147 | * containing keys and values for the properties of the
|
147 | * containing keys and values for the properties of the
|
148 | * <code>Database</code> instance. Except for the key
|
148 | * <code>Database</code> instance. Except for the key
|
149 | * <code>dbname</code>, which allows for aliases, all
|
149 | * <code>dbname</code>, which allows for aliases, all
|
150 | * keys are ignored if the corresponding properties
|
150 | * keys are ignored if the corresponding properties
|
151 | * were set. That is, definitions in the class file
|
151 | * were set. That is, definitions in the class file
|
152 | * override those in the configuration file.
|
152 | * override those in the configuration file.
|
153 | *
|
153 | *
|
154 | * @return boolean|array
|
154 | * @return boolean|array
|
155 | * <code>true</code> if the configuration
|
155 | * <code>true</code> if the configuration
|
156 | * file could be read, the configuration array otherwise.
|
156 | * file could be read, the configuration array otherwise.
|
157 | */
|
157 | */
|
158 | public function readConfig () |
158 | public function readConfig () |
159 | {
|
159 | {
|
160 | $config = parse_ini_file('application/.config', true); |
160 | $config = parse_ini_file('application/.config', true); |
161 | if ($config !== false) |
161 | if ($config !== false) |
162 | {
|
162 | {
|
163 | $section = 'database:' . $this->_dbname; |
163 | $section = 'database:' . $this->_dbname; |
164 | if (isset($config[$section])) |
164 | if (isset($config[$section])) |
165 | {
|
165 | {
|
166 | $dbconfig = $config[$section]; |
166 | $dbconfig = $config[$section]; |
167 | foreach (array('host', 'dbname', 'username', 'password', 'charset') as $key) |
167 | foreach (array('host', 'dbname', 'username', 'password', 'charset') as $key) |
168 | {
|
168 | {
|
169 | $property = "_$key"; |
169 | $property = "_$key"; |
170 | if (isset($dbconfig[$key]) |
170 | if (isset($dbconfig[$key]) |
171 | && $key == 'dbname' |
171 | && $key == 'dbname' |
172 | || (property_exists($this, $property) |
172 | || (property_exists($this, $property) |
173 | && $this->$property === null)) |
173 | && $this->$property === null)) |
174 | {
|
174 | {
|
175 | $this->$property = $dbconfig[$key]; |
175 | $this->$property = $dbconfig[$key]; |
176 | }
|
176 | }
|
177 | }
|
177 | }
|
178 | }
|
178 | }
|
179 | }
|
179 | }
|
180 | 180 | ||
181 | return $config; |
181 | return $config; |
182 | }
|
182 | }
|
183 | 183 | ||
184 | /**
|
184 | /**
|
185 | * @return PDO
|
185 | * @return PDO
|
186 | */
|
186 | */
|
187 | public function getConnection () |
187 | public function getConnection () |
188 | {
|
188 | {
|
189 | if ($this->_connection === null) |
189 | if ($this->_connection === null) |
190 | {
|
190 | {
|
191 | $this->_connection = |
191 | $this->_connection = |
192 | new PDO($this->_dsn, $this->_username, $this->_password, $this->_options); |
192 | new PDO($this->_dsn, $this->_username, $this->_password, $this->_options); |
193 | }
|
193 | }
|
194 | 194 | ||
195 | return $this->_connection; |
195 | return $this->_connection; |
196 | }
|
196 | }
|
197 | 197 | ||
198 | /**
|
198 | /**
|
199 | * Creates a database according to the specified parameters
|
199 | * Creates a database according to the specified parameters
|
200 | *
|
200 | *
|
201 | * Should be overwritten and called by inheriting classes.
|
201 | * Should be overwritten and called by inheriting classes.
|
202 | *
|
202 | *
|
203 | * @param string $dsn
|
203 | * @param string $dsn
|
204 | * Connection DSN (required; must not include the database
|
204 | * Connection DSN (required; must not include the database
|
205 | * name).
|
205 | * name).
|
206 | * @param string $username = null
|
206 | * @param string $username = null
|
207 | * Connection username. The default is specified by the
|
207 | * Connection username. The default is specified by the
|
208 | * <code>$_username</code> property. Note that creating
|
208 | * <code>$_username</code> property. Note that creating
|
209 | * the database usually requires a user with more privileges
|
209 | * the database usually requires a user with more privileges
|
210 | * than the one accessing the database or its tables.
|
210 | * than the one accessing the database or its tables.
|
211 | * @param string $password = null
|
211 | * @param string $password = null
|
212 | * Connection password. The default is specified by the
|
212 | * Connection password. The default is specified by the
|
213 | * <code>$_password</code> property.
|
213 | * <code>$_password</code> property.
|
214 | * @param array? $options = null
|
214 | * @param array? $options = null
|
215 | * Connection options. The default is specified by the
|
215 | * Connection options. The default is specified by the
|
216 | * <code>$_options</code> property.
|
216 | * <code>$_options</code> property.
|
217 | * @param string $spec = null
|
217 | * @param string $spec = null
|
218 | * Additional database specifications, like character encoding
|
218 | * Additional database specifications, like character encoding
|
219 | * and collation.
|
219 | * and collation.
|
220 | * @param boolean $force = false
|
220 | * @param boolean $force = false
|
221 | * If a true-value, the database will be attempted to be
|
221 | * If a true-value, the database will be attempted to be
|
222 | * created even if there is a database of the name specified
|
222 | * created even if there is a database of the name specified
|
223 | * by the <code>$_dbname</code> property.
|
223 | * by the <code>$_dbname</code> property.
|
224 | * @return int
|
224 | * @return int
|
225 | * The number of rows affected by the CREATE DATABASE statement.
|
225 | * The number of rows affected by the CREATE DATABASE statement.
|
226 | * @see PDO::__construct()
|
226 | * @see PDO::__construct()
|
227 | * @see PDO::exec()
|
227 | * @see PDO::exec()
|
228 | */
|
228 | */
|
229 | public function create ($dsn, $username = null, $password = null, |
229 | public function create ($dsn, $username = null, $password = null, |
230 | array $options = null, $dbspec = null, $force = false) |
230 | array $options = null, $dbspec = null, $force = false) |
231 | {
|
231 | {
|
232 | $connection = new PDO($dsn, |
232 | $connection = new PDO($dsn, |
233 | $username !== null ? $username : $this->_username, |
233 | $username !== null ? $username : $this->_username, |
234 | $password !== null ? $password : $this->_password, |
234 | $password !== null ? $password : $this->_password, |
235 | $options !== null ? $options : $this->_options); |
235 | $options !== null ? $options : $this->_options); |
236 | 236 | ||
237 | $query = 'CREATE DATABASE' |
237 | $query = 'CREATE DATABASE' |
238 | . (!$force ? ' IF NOT EXISTS' : '') |
238 | . (!$force ? ' IF NOT EXISTS' : '') |
239 | . ' ' . $this->escapeName($this->_dbname) |
239 | . ' ' . $this->escapeName($this->_dbname) |
240 | . ($dbspec ? ' ' . $dbspec : ''); |
240 | . ($dbspec ? ' ' . $dbspec : ''); |
241 | 241 | ||
242 | return $connection->exec($query); |
242 | return $connection->exec($query); |
243 | }
|
243 | }
|
244 | 244 | ||
245 | /**
|
245 | /**
|
246 | * Initiates a transaction
|
246 | * Initiates a transaction
|
247 | *
|
247 | *
|
248 | * @return bool
|
248 | * @return bool
|
249 | * @see PDO::beginTransaction()
|
249 | * @see PDO::beginTransaction()
|
250 | */
|
250 | */
|
251 | public function beginTransaction() |
251 | public function beginTransaction() |
252 | {
|
252 | {
|
253 | return $this->connection->beginTransaction(); |
253 | return $this->connection->beginTransaction(); |
254 | }
|
254 | }
|
255 | 255 | ||
256 | /**
|
256 | /**
|
257 | * Rolls back a transaction
|
257 | * Rolls back a transaction
|
258 | *
|
258 | *
|
259 | * @return bool
|
259 | * @return bool
|
260 | * @see PDO::rollBack()
|
260 | * @see PDO::rollBack()
|
261 | */
|
261 | */
|
262 | public function rollBack() |
262 | public function rollBack() |
263 | {
|
263 | {
|
264 | return $this->connection->rollBack(); |
264 | return $this->connection->rollBack(); |
265 | }
|
265 | }
|
266 | 266 | ||
267 | /**
|
267 | /**
|
268 | * Commits a transaction
|
268 | * Commits a transaction
|
269 | *
|
269 | *
|
270 | * @return bool
|
270 | * @return bool
|
271 | * @see PDO::commit()
|
271 | * @see PDO::commit()
|
272 | */
|
272 | */
|
273 | public function commit() |
273 | public function commit() |
274 | {
|
274 | {
|
275 | return $this->connection->commit(); |
275 | return $this->connection->commit(); |
276 | }
|
276 | }
|
277 | 277 | ||
278 | /**
|
278 | /**
|
279 | * Prepares a statement for execution with the database
|
279 | * Prepares a statement for execution with the database
|
280 | * @param string $query
|
280 | * @param string $query
|
281 | */
|
281 | */
|
282 | public function prepare($query, array $driver_options = array()) |
282 | public function prepare($query, array $driver_options = array()) |
283 | {
|
283 | {
|
284 | return $this->connection->prepare($query, $driver_options); |
284 | return $this->connection->prepare($query, $driver_options); |
285 | }
|
285 | }
|
286 | 286 | ||
287 | /**
|
287 | /**
|
288 | * Returns the ID of the last inserted row, or the last value from
|
288 | * Returns the ID of the last inserted row, or the last value from
|
289 | * a sequence object, depending on the underlying driver.
|
289 | * a sequence object, depending on the underlying driver.
|
290 | *
|
290 | *
|
291 | * @return int
|
291 | * @return int
|
292 | */
|
292 | */
|
293 | public function getLastInsertId() |
293 | public function getLastInsertId() |
294 | {
|
294 | {
|
295 | return $this->_lastInsertId; |
295 | return $this->_lastInsertId; |
296 | }
|
296 | }
|
297 | 297 | ||
298 | /**
|
298 | /**
|
299 | * Escapes a database name so that it can be used in a query.
|
299 | * Escapes a database name so that it can be used in a query.
|
300 | *
|
300 | *
|
301 | * @param string $name
|
301 | * @param string $name
|
302 | * The name to be escaped
|
302 | * The name to be escaped
|
303 | * @return string
|
303 | * @return string
|
304 | * The escaped name
|
304 | * The escaped name
|
305 | */
|
305 | */
|
306 | public function escapeName($name) |
306 | public function escapeName($name) |
307 | {
|
307 | {
|
308 | return $this->_leftQuote . $name . $this->_rightQuote; |
308 | return $this->_leftQuote . $name . $this->_rightQuote; |
309 | }
|
309 | }
|
310 | 310 | ||
311 | /**
|
311 | /**
|
312 | * Determines if an array is associative (has not all integer keys).
|
312 | * Determines if an array is associative (has not all integer keys).
|
313 | *
|
313 | *
|
314 | * @author
|
314 | * @author
|
315 | * Algorithm courtesy of squirrel, <http://stackoverflow.com/a/5969617/855543>.
|
315 | * Algorithm courtesy of squirrel, <http://stackoverflow.com/a/5969617/855543>.
|
316 | * @param array $a
|
316 | * @param array $a
|
317 | * @return boolean
|
317 | * @return boolean
|
318 | * <code>true</code> if <var>$a</var> is associative,
|
318 | * <code>true</code> if <var>$a</var> is associative,
|
319 | * <code>false</code> otherwise
|
319 | * <code>false</code> otherwise
|
320 | */
|
320 | */
|
321 | protected function _isAssociativeArray(array $a) |
321 | protected function _isAssociativeArray(array $a) |
322 | {
|
322 | {
|
323 | for (reset($a); is_int(key($a)); next($a)); |
323 | for (reset($a); is_int(key($a)); next($a)); |
324 | return !is_null(key($a)); |
324 | return !is_null(key($a)); |
325 | }
|
325 | }
|
326 | 326 | ||
327 | /**
|
327 | /**
|
328 | * Escapes an associative array so that its string representation can be used
|
328 | * Escapes an associative array so that its string representation can be used
|
329 | * as list with table or column aliases in a query.
|
329 | * as list with table or column aliases in a query.
|
330 | *
|
330 | *
|
331 | * This method does not actually escape anything; it only inserts the
|
331 | * This method does not actually escape anything; it only inserts the
|
332 | * 'AS' keyword. It should be overridden by inheriting methods.
|
332 | * 'AS' keyword. It should be overridden by inheriting methods.
|
333 | *
|
333 | *
|
334 | * NOTE: This method intentionally does not check whether the array actually
|
334 | * NOTE: This method intentionally does not check whether the array actually
|
335 | * is associative.
|
335 | * is associative.
|
336 | *
|
336 | *
|
337 | * @param array &$array
|
337 | * @param array &$array
|
338 | * The array to be escaped
|
338 | * The array to be escaped
|
339 | * @return array
|
339 | * @return array
|
340 | * The escaped array
|
340 | * The escaped array
|
341 | */
|
341 | */
|
342 | protected function _escapeAliasArray(array &$array) |
342 | protected function _escapeAliasArray(array &$array) |
343 | {
|
343 | {
|
344 | foreach ($array as $column => &$value) |
344 | foreach ($array as $column => &$value) |
345 | {
|
345 | {
|
346 | $quotedColumn = $column; |
346 | $quotedColumn = $column; |
347 | if (strpos($column, $this->_leftQuote) === false |
347 | if (strpos($column, $this->_leftQuote) === false |
348 | && strpos($column, $this->_rightQuote) === false) |
348 | && strpos($column, $this->_rightQuote) === false) |
349 | {
|
349 | {
|
350 | $quotedColumn = $this->_leftQuote . $column . $this->_rightQuote; |
350 | $quotedColumn = $this->_leftQuote . $column . $this->_rightQuote; |
351 | }
|
351 | }
|
352 | 352 | ||
353 | $value = $value . ' AS ' . $quotedColumn; |
353 | $value = $value . ' AS ' . $quotedColumn; |
354 | }
|
354 | }
|
355 | 355 | ||
356 | return $array; |
356 | return $array; |
357 | }
|
357 | }
|
358 | 358 | ||
359 | /**
|
359 | /**
|
360 | * @param array $a
|
360 | * @param array $a
|
361 | * @param string $prefix
|
361 | * @param string $prefix
|
362 | */
|
362 | */
|
363 | private static function _expand(array $a, $prefix) |
363 | private static function _expand(array $a, $prefix) |
364 | {
|
364 | {
|
365 | $a2 = array(); |
365 | $a2 = array(); |
366 | 366 | ||
367 | foreach ($a as $key => $value) |
367 | foreach ($a as $key => $value) |
368 | {
|
368 | {
|
369 | $a2[] = ':' . $prefix . ($key + 1); |
369 | $a2[] = ':' . $prefix . ($key + 1); |
370 | }
|
370 | }
|
371 | 371 | ||
372 | return $a2; |
372 | return $a2; |
373 | }
|
373 | }
|
374 | 374 | ||
375 | /**
|
375 | /**
|
376 | * Escapes an associative array so that its string representation can be used
|
376 | * Escapes an associative array so that its string representation can be used
|
377 | * as value list in a query.
|
377 | * as value list in a query.
|
378 | *
|
378 | *
|
379 | * This method should be overridden by inheriting classes to escape
|
379 | * This method should be overridden by inheriting classes to escape
|
380 | * column names as fitting for the database schema they support. It is
|
380 | * column names as fitting for the database schema they support. It is
|
381 | * strongly recommended that the overriding methods call this method with
|
381 | * strongly recommended that the overriding methods call this method with
|
382 | * an appropriate <var>$escape</var> parameter, pass all other parameters
|
382 | * an appropriate <var>$escape</var> parameter, pass all other parameters
|
383 | * on unchanged, and return its return value.
|
383 | * on unchanged, and return its return value.
|
384 | *
|
384 | *
|
385 | * NOTE: Intentionally does not check whether the array actually is associative!
|
385 | * NOTE: Intentionally does not check whether the array actually is associative!
|
386 | *
|
386 | *
|
387 | * @param array &$array
|
387 | * @param array &$array
|
388 | * The array to be escaped
|
388 | * The array to be escaped
|
389 | * @param string $suffix
|
389 | * @param string $suffix
|
390 | * The string to be appended to the column name for the value placeholder.
|
390 | * The string to be appended to the column name for the value placeholder.
|
391 | * The default is the empty string.
|
391 | * The default is the empty string.
|
392 | * @param array $escape
|
392 | * @param array $escape
|
393 | * The strings to use left-hand side (index 0) and right-hand side (index 1)
|
393 | * The strings to use left-hand side (index 0) and right-hand side (index 1)
|
394 | * of the column name. The default is the empty string, respectively.
|
394 | * of the column name. The default is the empty string, respectively.
|
395 | * @return array
|
395 | * @return array
|
396 | * The escaped array
|
396 | * The escaped array
|
397 | */
|
397 | */
|
398 | protected function _escapeValueArray(array &$array, $suffix = '') |
398 | protected function _escapeValueArray(array &$array, $suffix = '') |
399 | {
|
399 | {
|
400 | $result = array(); |
400 | $result = array(); |
401 | 401 | ||
402 | foreach ($array as $column => $value) |
402 | foreach ($array as $column => $value) |
403 | {
|
403 | {
|
404 | $op = '='; |
404 | $op = '='; |
405 | $placeholder = ":{$column}"; |
405 | $placeholder = ":{$column}"; |
406 | 406 | ||
407 | if (is_array($value) && $this->_isAssociativeArray($value)) |
407 | if (is_array($value) && $this->_isAssociativeArray($value)) |
408 | {
|
408 | {
|
409 | reset($value); |
409 | reset($value); |
410 | $op = ' ' . key($value) . ' '; |
410 | $op = ' ' . key($value) . ' '; |
411 | 411 | ||
412 | $value = $value[key($value)]; |
412 | $value = $value[key($value)]; |
413 | }
|
413 | }
|
414 | 414 | ||
415 | if (is_array($value)) |
415 | if (is_array($value)) |
416 | {
|
416 | {
|
417 | $placeholder = '(' . implode(', ', self::_expand($value, $column)) . ')'; |
417 | $placeholder = '(' . implode(', ', self::_expand($value, $column)) . ')'; |
418 | }
|
418 | }
|
419 | 419 | ||
420 | $result[] = $this->_leftQuote . $column . $this->_rightQuote . "{$op}{$placeholder}{$suffix}"; |
420 | $result[] = $this->_leftQuote . $column . $this->_rightQuote . "{$op}{$placeholder}{$suffix}"; |
421 | }
|
421 | }
|
422 | 422 | ||
423 | return $result; |
423 | return $result; |
424 | }
|
424 | }
|
425 | 425 | ||
426 | /**
|
426 | /**
|
427 | * Constructs the WHERE part of a query
|
427 | * Constructs the WHERE part of a query
|
428 | *
|
428 | *
|
429 | * @param string|array $where
|
429 | * @param string|array $where
|
430 | * Condition
|
430 | * Condition
|
431 | * @param string $suffix
|
431 | * @param string $suffix
|
432 | * The string to be appended to the column name for the value placeholder,
|
432 | * The string to be appended to the column name for the value placeholder,
|
433 | * passed on to {@link Database::_escapeValueArray()}. The default is
|
433 | * passed on to {@link Database::_escapeValueArray()}. The default is
|
434 | * the empty string.
|
434 | * the empty string.
|
435 | * @return string
|
435 | * @return string
|
436 | * @see Database::_escapeValueArray()
|
436 | * @see Database::_escapeValueArray()
|
437 | */
|
437 | */
|
438 | protected function _where($where, $suffix = '') |
438 | protected function _where($where, $suffix = '') |
439 | {
|
439 | {
|
440 | if (!is_null($where)) |
440 | if (!is_null($where)) |
441 | {
|
441 | {
|
442 | if (is_array($where)) |
442 | if (is_array($where)) |
443 | {
|
443 | {
|
444 | if (count($where) < 1) |
444 | if (count($where) < 1) |
445 | {
|
445 | {
|
446 | return ''; |
446 | return ''; |
447 | }
|
447 | }
|
448 | 448 | ||
449 | if ($this->_isAssociativeArray($where)) |
449 | if ($this->_isAssociativeArray($where)) |
450 | {
|
450 | {
|
451 | $where = $this->_escapeValueArray($where, $suffix); |
451 | $where = $this->_escapeValueArray($where, $suffix); |
452 | }
|
452 | }
|
453 | 453 | ||
454 | $where = '(' . implode(') AND (', $where) . ')'; |
454 | $where = '(' . implode(') AND (', $where) . ')'; |
455 | }
|
455 | }
|
456 | 456 | ||
457 | return ' WHERE ' . $where; |
457 | return ' WHERE ' . $where; |
458 | }
|
458 | }
|
459 | 459 | ||
460 | return ''; |
460 | return ''; |
461 | }
|
461 | }
|
462 | 462 | ||
463 | /**
|
463 | /**
|
464 | * Selects data from one or more tables; the resulting records are stored
|
464 | * Selects data from one or more tables; the resulting records are stored
|
465 | * in the <code>result</code> property and returned as an associative array,
|
465 | * in the <code>result</code> property and returned as an associative array,
|
466 | * where the keys are the column (alias) names.
|
466 | * where the keys are the column (alias) names.
|
467 | *
|
467 | *
|
468 | * @param string|array[string] $tables Table(s) to select from
|
468 | * @param string|array[string] $tables Table(s) to select from
|
469 | * @param string|array[string] $columns Column(s) to select from (optional)
|
469 | * @param string|array[string] $columns Column(s) to select from (optional)
|
470 | * @param string|array $where Condition (optional)
|
470 | * @param string|array $where Condition (optional)
|
471 | * @param string $order Sort order (optional)
|
471 | * @param string $order Sort order (optional)
|
472 | * If provided, MUST start with ORDER BY or GROUP BY
|
472 | * If provided, MUST start with ORDER BY or GROUP BY
|
473 | * @param string $limit Limit (optional)
|
473 | * @param string $limit Limit (optional)
|
474 | * @param int $fetch_style
|
474 | * @param int $fetch_style
|
475 | * The mode that should be used for {@link PDOStatement::fetchAll()}.
|
475 | * The mode that should be used for {@link PDOStatement::fetchAll()}.
|
476 | * The default is {@link PDO::FETCH_ASSOC}.
|
476 | * The default is {@link PDO::FETCH_ASSOC}.
|
477 | * @return array
|
477 | * @return array
|
478 | * @see Database::prepare()
|
478 | * @see Database::prepare()
|
479 | * @see PDOStatement::fetchAll()
|
479 | * @see PDOStatement::fetchAll()
|
480 | */
|
480 | */
|
481 | public function select($tables, $columns = null, $where = null, |
481 | public function select($tables, $columns = null, $where = null, |
482 | $order = null, $limit = null, $fetch_style = PDO::FETCH_ASSOC) |
482 | $order = null, $limit = null, $fetch_style = PDO::FETCH_ASSOC) |
483 | {
|
483 | {
|
484 | if (is_null($columns)) |
484 | if (is_null($columns)) |
485 | {
|
485 | {
|
486 | $columns = array('*'); |
486 | $columns = array('*'); |
487 | }
|
487 | }
|
488 | 488 | ||
489 | if (is_array($columns)) |
489 | if (is_array($columns)) |
490 | {
|
490 | {
|
491 | if ($this->_isAssociativeArray($columns)) |
491 | if ($this->_isAssociativeArray($columns)) |
492 | {
|
492 | {
|
493 | $columns = $this->_escapeAliasArray($columns); |
493 | $columns = $this->_escapeAliasArray($columns); |
494 | }
|
494 | }
|
495 | 495 | ||
496 | $columns = implode(', ', $columns); |
496 | $columns = implode(', ', $columns); |
497 | }
|
497 | }
|
498 | 498 | ||
499 | if (is_array($tables)) |
499 | if (is_array($tables)) |
500 | {
|
500 | {
|
501 | if ($this->_isAssociativeArray($columns)) |
501 | if ($this->_isAssociativeArray($columns)) |
502 | {
|
502 | {
|
503 | $columns = $this->_escapeAliasArray($columns); |
503 | $columns = $this->_escapeAliasArray($columns); |
504 | }
|
504 | }
|
505 | 505 | ||
506 | $tables = implode(', ', $tables); |
506 | $tables = implode(', ', $tables); |
507 | }
|
507 | }
|
508 | 508 | ||
509 | $query = "SELECT {$columns} FROM {$tables}" . $this->_where($where); |
509 | $query = "SELECT {$columns} FROM {$tables}" . $this->_where($where); |
510 | 510 | ||
511 | if (!is_null($order)) |
511 | if (!is_null($order)) |
512 | {
|
512 | {
|
513 | if (is_array($order)) |
513 | if (is_array($order)) |
514 | {
|
514 | {
|
515 | $order = 'ORDER BY ' . implode(', ', $order); |
515 | $order = 'ORDER BY ' . implode(', ', $order); |
516 | }
|
516 | }
|
517 | 517 | ||
518 | $query .= " $order"; |
518 | $query .= " $order"; |
519 | }
|
519 | }
|
520 | 520 | ||
521 | if (!is_null($limit)) |
521 | if (!is_null($limit)) |
522 | {
|
522 | {
|
523 | $query .= " LIMIT $limit"; |
523 | $query .= " LIMIT $limit"; |
524 | }
|
524 | }
|
525 | 525 | ||
526 | $stmt = $this->prepare($query); |
526 | $stmt = $this->prepare($query); |
527 | 527 | ||
528 | $params = array(); |
528 | $params = array(); |
529 | 529 | ||
530 | if (is_array($where) && $this->_isAssociativeArray($where)) |
530 | if (is_array($where) && $this->_isAssociativeArray($where)) |
531 | {
|
531 | {
|
532 | /* FIXME: Export and reuse this */
|
532 | /* FIXME: Export and reuse this */
|
533 | foreach ($where as $column => $condition) |
533 | foreach ($where as $column => $condition) |
534 | {
|
534 | {
|
535 | /* TODO: Also handle function calls as keys */
|
535 | /* TODO: Also handle function calls as keys */
|
536 | if (is_array($condition) && $this->_isAssociativeArray($condition)) |
536 | if (is_array($condition) && $this->_isAssociativeArray($condition)) |
537 | {
|
537 | {
|
538 | reset($condition); |
538 | reset($condition); |
539 | $condition = $condition[key($condition)]; |
539 | $condition = $condition[key($condition)]; |
540 | 540 | ||
541 | if (is_array($condition)) |
541 | if (is_array($condition)) |
542 | {
|
542 | {
|
543 | foreach (self::_expand($condition, $column) as $param_index => $param_name) |
543 | foreach (self::_expand($condition, $column) as $param_index => $param_name) |
544 | {
|
544 | {
|
545 | $params[$param_name] = $condition[$param_index]; |
545 | $params[$param_name] = $condition[$param_index]; |
546 | }
|
546 | }
|
547 | }
|
547 | }
|
548 | }
|
548 | }
|
549 | else
|
549 | else
|
550 | {
|
550 | {
|
551 | $params[":{$column}"] = $condition; |
551 | $params[":{$column}"] = $condition; |
552 | }
|
552 | }
|
553 | }
|
553 | }
|
554 | }
|
554 | }
|
555 | 555 | ||
556 | /* DEBUG */
|
556 | /* DEBUG */
|
557 | if (defined('DEBUG') && DEBUG > 1) |
557 | if (defined('DEBUG') && DEBUG > 1) |
558 | {
|
558 | {
|
559 | debug(array( |
559 | debug(array( |
560 | 'query' => $query, |
560 | 'query' => $query, |
561 | 'params' => $params |
561 | 'params' => $params |
562 | )); |
562 | )); |
563 | }
|
563 | }
|
564 | 564 | ||
565 | $success =& $this->_lastSuccess; |
565 | $success =& $this->_lastSuccess; |
566 | $success = $stmt->execute($params); |
566 | $success = $stmt->execute($params); |
567 | 567 | ||
568 | $errorInfo =& $this->_lastError; |
568 | $errorInfo =& $this->_lastError; |
569 | $errorInfo = $stmt->errorInfo(); |
569 | $errorInfo = $stmt->errorInfo(); |
570 | 570 | ||
571 | $result =& $this->_lastResult; |
571 | $result =& $this->_lastResult; |
572 | $result = $stmt->fetchAll($fetch_style); |
572 | $result = $stmt->fetchAll($fetch_style); |
573 | 573 | ||
574 | if (defined('DEBUG') && DEBUG > 1) |
574 | if (defined('DEBUG') && DEBUG > 1) |
575 | {
|
575 | {
|
576 | debug(array( |
576 | debug(array( |
577 | '_lastSuccess' => $success, |
577 | '_lastSuccess' => $success, |
578 | '_lastError' => $errorInfo, |
578 | '_lastError' => $errorInfo, |
579 | '_lastResult' => $result |
579 | '_lastResult' => $result |
580 | )); |
580 | )); |
581 | }
|
581 | }
|
582 | 582 | ||
583 | return $result; |
583 | return $result; |
584 | }
|
584 | }
|
585 | 585 | ||
586 | /**
|
586 | /**
|
587 | * Sets and returns the ID of the last inserted row, or the last value from
|
587 | * Sets and returns the ID of the last inserted row, or the last value from
|
588 | * a sequence object, depending on the underlying driver.
|
588 | * a sequence object, depending on the underlying driver.
|
589 | *
|
589 | *
|
590 | * @param string $name
|
590 | * @param string $name
|
591 | * Name of the sequence object from which the ID should be returned.
|
591 | * Name of the sequence object from which the ID should be returned.
|
592 | * @return string
|
592 | * @return string
|
593 | */
|
593 | */
|
594 | protected function _setLastInsertId($name = null) |
594 | protected function _setLastInsertId($name = null) |
595 | {
|
595 | {
|
596 | return ($this->_lastInsertId = $this->connection->lastInsertId($name)); |
596 | return ($this->_lastInsertId = $this->connection->lastInsertId($name)); |
597 | }
|
597 | }
|
598 | 598 | ||
599 | /**
|
599 | /**
|
600 | * Resets the the ID of the last inserted row, or the last value from
|
600 | * Resets the the ID of the last inserted row, or the last value from
|
601 | * a sequence object, depending on the underlying driver.
|
601 | * a sequence object, depending on the underlying driver.
|
602 | *
|
602 | *
|
603 | * @return string
|
603 | * @return string
|
604 | * The default value
|
604 | * The default value
|
605 | */
|
605 | */
|
606 | protected function _resetLastInsertId() |
606 | protected function _resetLastInsertId() |
607 | {
|
607 | {
|
608 | return ($this->_lastInsertId = ''); |
608 | return ($this->_lastInsertId = ''); |
609 | }
|
609 | }
|
610 | 610 | ||
611 | /**
|
611 | /**
|
612 | * Updates one or more records
|
612 | * Updates one or more records
|
613 | *
|
613 | *
|
614 | * @param string|array $tables
|
614 | * @param string|array $tables
|
615 | * Table name
|
615 | * Table name
|
616 | * @param array $values
|
616 | * @param array $updates
|
617 | * Associative array of column-value pairs
|
617 | * Associative array of column-value pairs
|
618 | * @param array|string $where
|
618 | * @param array|string $where
|
619 | * Only the records matching this condition are updated
|
619 | * Only the records matching this condition are updated
|
620 | * @return bool
|
620 | * @return bool
|
621 | */
|
621 | */
|
622 | public function update($tables, $updates, $where = null) |
622 | public function update($tables, array $updates, $where = null) |
623 | {
|
623 | {
|
624 | if (!$tables) |
624 | if (!$tables) |
625 | {
|
625 | {
|
626 | throw new InvalidArgumentException('No table specified'); |
626 | throw new InvalidArgumentException('No table specified'); |
627 | }
|
627 | }
|
628 | 628 | ||
629 | if (is_array($tables)) |
629 | if (is_array($tables)) |
630 | {
|
630 | {
|
631 | $tables = implode(', ', $tables); |
631 | $tables = implode(', ', $tables); |
632 | }
|
632 | }
|
633 | 633 | ||
634 | if (!$updates) |
634 | if (!$updates) |
635 | {
|
635 | {
|
636 | throw new InvalidArgumentException('No values specified'); |
636 | throw new InvalidArgumentException('No values specified'); |
637 | }
|
637 | }
|
638 | 638 | ||
639 | $params = array(); |
639 | $params = array(); |
640 | 640 | ||
641 | if ($this->_isAssociativeArray($updates)) |
641 | if ($this->_isAssociativeArray($updates)) |
642 | {
|
642 | {
|
643 | foreach ($updates as $key => $condition) |
643 | foreach ($updates as $key => $condition) |
644 | {
|
644 | {
|
645 | $params[":{$key}"] = $condition; |
645 | $params[":{$key}"] = $condition; |
646 | }
|
646 | }
|
647 | }
|
647 | }
|
648 | 648 | ||
649 | $updates = implode(', ', $this->_escapeValueArray($updates)); |
649 | $updates = implode(', ', $this->_escapeValueArray($updates)); |
650 | 650 | ||
651 | /* TODO: Should escape table names with escapeName(), but what about aliases? */
|
651 | /* TODO: Should escape table names with escapeName(), but what about aliases? */
|
652 | $query = "UPDATE {$tables} SET {$updates}" . $this->_where($where, '2'); |
652 | $query = "UPDATE {$tables} SET {$updates}" . $this->_where($where, '2'); |
653 | 653 | ||
654 | $stmt = $this->prepare($query); |
654 | $stmt = $this->prepare($query); |
655 | 655 | ||
656 | if (is_array($where) && $this->_isAssociativeArray($where)) |
656 | if (is_array($where) && $this->_isAssociativeArray($where)) |
657 | {
|
657 | {
|
658 | foreach ($where as $column => $condition) |
658 | foreach ($where as $column => $condition) |
659 | {
|
659 | {
|
660 | if (is_array($condition) && $this->_isAssociativeArray($condition)) |
660 | if (is_array($condition) && $this->_isAssociativeArray($condition)) |
661 | {
|
661 | {
|
662 | reset($condition); |
662 | reset($condition); |
663 | $condition = $condition[key($condition)]; |
663 | $condition = $condition[key($condition)]; |
664 | 664 | ||
665 | if (is_array($condition)) |
665 | if (is_array($condition)) |
666 | {
|
666 | {
|
667 | foreach (self::_expand($condition, $column) as $param_index => $param_name) |
667 | foreach (self::_expand($condition, $column) as $param_index => $param_name) |
668 | {
|
668 | {
|
669 | $params[$param_name] = $condition[$param_index]; |
669 | $params[$param_name] = $condition[$param_index]; |
670 | }
|
670 | }
|
671 | }
|
671 | }
|
672 | }
|
672 | }
|
673 | else
|
673 | else
|
674 | {
|
674 | {
|
675 | $params[":{$column}2"] = $condition; |
675 | $params[":{$column}2"] = $condition; |
676 | }
|
676 | }
|
677 | }
|
677 | }
|
678 | }
|
678 | }
|
679 | 679 | ||
680 | /* DEBUG */
|
680 | /* DEBUG */
|
681 | if (defined('DEBUG') && DEBUG > 1) |
681 | if (defined('DEBUG') && DEBUG > 1) |
682 | {
|
682 | {
|
683 | debug(array( |
683 | debug(array( |
684 | 'query' => $query, |
684 | 'query' => $query, |
685 | 'params' => $params |
685 | 'params' => $params |
686 | )); |
686 | )); |
687 | }
|
687 | }
|
688 | 688 | ||
689 | $success =& $this->_lastSuccess; |
689 | $success =& $this->_lastSuccess; |
690 | $success = $stmt->execute($params); |
690 | $success = $stmt->execute($params); |
691 | 691 | ||
692 | $errorInfo =& $this->_lastError; |
692 | $errorInfo =& $this->_lastError; |
693 | $errorInfo = $stmt->errorInfo(); |
693 | $errorInfo = $stmt->errorInfo(); |
694 | 694 | ||
695 | $this->_resetLastInsertId(); |
695 | $this->_resetLastInsertId(); |
696 | 696 | ||
697 | $result =& $this->_lastResult; |
697 | $result =& $this->_lastResult; |
698 | $result = $stmt->fetchAll(); |
698 | $result = $stmt->fetchAll(); |
699 | 699 | ||
700 | if (defined('DEBUG') && DEBUG > 1) |
700 | if (defined('DEBUG') && DEBUG > 1) |
701 | {
|
701 | {
|
702 | debug(array( |
702 | debug(array( |
703 | '_lastSuccess' => $success, |
703 | '_lastSuccess' => $success, |
704 | '_lastError' => $errorInfo, |
704 | '_lastError' => $errorInfo, |
705 | '_lastResult' => $result |
705 | '_lastResult' => $result |
706 | )); |
706 | )); |
707 | }
|
707 | }
|
708 | 708 | ||
709 | return $success; |
709 | return $success; |
710 | }
|
710 | }
|
711 | 711 | ||
712 | /**
|
712 | /**
|
713 | * Inserts a record into a table.<p>The AUTO_INCREMENT value of the inserted
|
713 | * Inserts a record into a table.<p>The AUTO_INCREMENT value of the inserted
|
714 | * row, if any (> 0), is stored in the {@link $lastInsertId} property of
|
714 | * row, if any (> 0), is stored in the {@link $lastInsertId} property of
|
715 | * the <code>Database</code> instance.</p>
|
715 | * the <code>Database</code> instance.</p>
|
716 | *
|
716 | *
|
717 | * @param string $table
|
717 | * @param string $table
|
718 | * Table name
|
718 | * Table name
|
719 | * @param array|string $values
|
719 | * @param array|string $values
|
720 | * Associative array of column-value pairs, indexed array,
|
720 | * Associative array of column-value pairs, indexed array,
|
721 | * or comma-separated list of values. If <var>$values</var> is not
|
721 | * or comma-separated list of values. If <var>$values</var> is not
|
722 | * an associative array, <var>$cols</var> must be passed if the
|
722 | * an associative array, <var>$cols</var> must be passed if the
|
723 | * values are not in column order (see below).
|
723 | * values are not in column order (see below).
|
724 | * @param array|string $cols
|
724 | * @param array|string $cols
|
725 | * Indexed array, or comma-separated list of column names.
|
725 | * Indexed array, or comma-separated list of column names.
|
726 | * Needs only be passed if <var>$values</var> is not an associative array
|
726 | * Needs only be passed if <var>$values</var> is not an associative array
|
727 | * and the values are not in column order (default: <code>null</code>);
|
727 | * and the values are not in column order (default: <code>null</code>);
|
728 | * is ignored otherwise. <strong>You SHOULD NOT rely on column order.</strong>
|
728 | * is ignored otherwise. <strong>You SHOULD NOT rely on column order.</strong>
|
729 | * @return bool
|
729 | * @return bool
|
730 | * <code>true</code> if successful, <code>false</code> otherwise
|
730 | * <code>true</code> if successful, <code>false</code> otherwise
|
731 | * @see PDOStatement::execute()
|
731 | * @see PDOStatement::execute()
|
732 | */
|
732 | */
|
733 | public function insert($table, $values, $cols = null) |
733 | public function insert($table, $values, $cols = null) |
734 | {
|
734 | {
|
735 | if ($cols != null) |
735 | if ($cols != null) |
736 | {
|
736 | {
|
737 | $cols = ' (' |
737 | $cols = ' (' |
738 | . (is_array($cols) |
738 | . (is_array($cols) |
739 | ? implode(', ', array_map(array($this, 'escapeName'), $cols)) |
739 | ? implode(', ', array_map(array($this, 'escapeName'), $cols)) |
740 | : $cols) . ')'; |
740 | : $cols) . ')'; |
741 | }
|
741 | }
|
742 | else
|
742 | else
|
743 | {
|
743 | {
|
744 | $cols = ''; |
744 | $cols = ''; |
745 | }
|
745 | }
|
746 | 746 | ||
747 | /* DEBUG */
|
747 | /* DEBUG */
|
748 | if (defined('DEBUG') && DEBUG > 2) |
748 | if (defined('DEBUG') && DEBUG > 2) |
749 | {
|
749 | {
|
750 | debug(array('values' => $values)); |
750 | debug(array('values' => $values)); |
751 | }
|
751 | }
|
752 | 752 | ||
753 | $params = array(); |
753 | $params = array(); |
754 | 754 | ||
755 | if (is_array($values)) |
755 | if (is_array($values)) |
756 | {
|
756 | {
|
757 | if ($this->_isAssociativeArray($values)) |
757 | if ($this->_isAssociativeArray($values)) |
758 | {
|
758 | {
|
759 | foreach ($values as $key => $condition) |
759 | foreach ($values as $key => $condition) |
760 | {
|
760 | {
|
761 | $params[":{$key}"] = $condition; |
761 | $params[":{$key}"] = $condition; |
762 | }
|
762 | }
|
763 | 763 | ||
764 | $values = $this->_escapeValueArray($values); |
764 | $values = $this->_escapeValueArray($values); |
765 | 765 | ||
766 | $cols = ''; |
766 | $cols = ''; |
767 | $values = 'SET ' . implode(', ', $values); |
767 | $values = 'SET ' . implode(', ', $values); |
768 | }
|
768 | }
|
769 | else
|
769 | else
|
770 | {
|
770 | {
|
771 | foreach ($values as &$value) |
771 | foreach ($values as &$value) |
772 | {
|
772 | {
|
773 | if (is_string($value)) |
773 | if (is_string($value)) |
774 | {
|
774 | {
|
775 | $value = "'" . $value . "'"; |
775 | $value = "'" . $value . "'"; |
776 | }
|
776 | }
|
777 | }
|
777 | }
|
778 | 778 | ||
779 | $values = ' VALUES (' . implode(', ', $values) . ')'; |
779 | $values = ' VALUES (' . implode(', ', $values) . ')'; |
780 | }
|
780 | }
|
781 | }
|
781 | }
|
782 | 782 | ||
783 | /* TODO: Should escape table names with escapeName(), but what about aliases? */
|
783 | /* TODO: Should escape table names with escapeName(), but what about aliases? */
|
784 | $query = "INSERT INTO {$table} {$cols} {$values}"; |
784 | $query = "INSERT INTO {$table} {$cols} {$values}"; |
785 | 785 | ||
786 | $stmt = $this->prepare($query); |
786 | $stmt = $this->prepare($query); |
787 | 787 | ||
788 | /* DEBUG */
|
788 | /* DEBUG */
|
789 | if (defined('DEBUG') && DEBUG > 1) |
789 | if (defined('DEBUG') && DEBUG > 1) |
790 | {
|
790 | {
|
791 | debug(array( |
791 | debug(array( |
792 | 'query' => $query, |
792 | 'query' => $query, |
793 | 'params' => $params |
793 | 'params' => $params |
794 | )); |
794 | )); |
795 | }
|
795 | }
|
796 | 796 | ||
797 | $success =& $this->_lastSuccess; |
797 | $success =& $this->_lastSuccess; |
798 | $success = $stmt->execute($params); |
798 | $success = $stmt->execute($params); |
799 | 799 | ||
800 | $errorInfo =& $this->_lastError; |
800 | $errorInfo =& $this->_lastError; |
801 | $errorInfo = $stmt->errorInfo(); |
801 | $errorInfo = $stmt->errorInfo(); |
802 | 802 | ||
803 | $this->_setLastInsertId(); |
803 | $this->_setLastInsertId(); |
804 | 804 | ||
805 | $result =& $this->_lastResult; |
805 | $result =& $this->_lastResult; |
806 | $result = $stmt->fetchAll(); |
806 | $result = $stmt->fetchAll(); |
807 | 807 | ||
808 | if (defined('DEBUG') && DEBUG > 1) |
808 | if (defined('DEBUG') && DEBUG > 1) |
809 | {
|
809 | {
|
810 | debug(array( |
810 | debug(array( |
811 | '_lastSuccess' => $success, |
811 | '_lastSuccess' => $success, |
812 | '_lastError' => $errorInfo, |
812 | '_lastError' => $errorInfo, |
813 | '_lastInsertId' => $this->_lastInsertId, |
813 | '_lastInsertId' => $this->_lastInsertId, |
814 | '_lastResult' => $result |
814 | '_lastResult' => $result |
815 | )); |
815 | )); |
816 | }
|
816 | }
|
817 | 817 | ||
818 | return $success; |
818 | return $success; |
819 | }
|
819 | }
|
820 | 820 | ||
821 | /**
|
821 | /**
|
822 | * Retrieves all rows from a table
|
822 | * Retrieves all rows from a table
|
823 | *
|
823 | *
|
824 | * @param int[optional] $fetch_style
|
824 | * @param int[optional] $fetch_style
|
825 | * @param int[optional] $column_index
|
825 | * @param int[optional] $column_index
|
826 | * @param array[optional] $ctor_args
|
826 | * @param array[optional] $ctor_args
|
827 | * @return array
|
827 | * @return array
|
828 | * @see PDOStatement::fetchAll()
|
828 | * @see PDOStatement::fetchAll()
|
829 | */
|
829 | */
|
830 | public function fetchAll($table, $fetch_style = null, $column_index = null, array $ctor_args = null) |
830 | public function fetchAll($table, $fetch_style = null, $column_index = null, array $ctor_args = null) |
831 | {
|
831 | {
|
832 | /* NOTE: Cannot use table name as statement parameter */
|
832 | /* NOTE: Cannot use table name as statement parameter */
|
833 | $stmt = $this->prepare("SELECT * FROM $table"); |
833 | $stmt = $this->prepare("SELECT * FROM $table"); |
834 | $this->_lastSuccess = $stmt->execute(); |
834 | $this->_lastSuccess = $stmt->execute(); |
835 | 835 | ||
836 | $this->_lastError = $stmt->errorInfo(); |
836 | $this->_lastError = $stmt->errorInfo(); |
837 | 837 | ||
838 | $result =& $this->_lastResult; |
838 | $result =& $this->_lastResult; |
839 | 839 | ||
840 | if (is_null($fetch_style)) |
840 | if (is_null($fetch_style)) |
841 | {
|
841 | {
|
842 | $fetch_style = PDO::FETCH_ASSOC; |
842 | $fetch_style = PDO::FETCH_ASSOC; |
843 | }
|
843 | }
|
844 | 844 | ||
845 | if (!is_null($ctor_args)) |
845 | if (!is_null($ctor_args)) |
846 | {
|
846 | {
|
847 | $result = $stmt->fetchAll($fetch_style, $column_index, $ctor_args); |
847 | $result = $stmt->fetchAll($fetch_style, $column_index, $ctor_args); |
848 | }
|
848 | }
|
849 | else if (!is_null($column_index)) |
849 | else if (!is_null($column_index)) |
850 | {
|
850 | {
|
851 | $result = $stmt->fetchAll($fetch_style, $column_index); |
851 | $result = $stmt->fetchAll($fetch_style, $column_index); |
852 | }
|
852 | }
|
853 | else if (!is_null($fetch_style)) |
853 | else if (!is_null($fetch_style)) |
854 | {
|
854 | {
|
855 | $result = $stmt->fetchAll($fetch_style); |
855 | $result = $stmt->fetchAll($fetch_style); |
856 | }
|
856 | }
|
857 | else
|
857 | else
|
858 | {
|
858 | {
|
859 | $result = $stmt->fetchAll(); |
859 | $result = $stmt->fetchAll(); |
860 | }
|
860 | }
|
861 | 861 | ||
862 | return $result; |
862 | return $result; |
863 | }
|
863 | }
|
864 | 864 | ||
865 | /**
|
865 | /**
|
866 | * Deletes one or more records
|
866 | * Deletes one or more records
|
867 | *
|
867 | *
|
868 | * @param string|array $tables
|
868 | * @param string|array $tables
|
869 | * Table name(s)
|
869 | * Table name(s)
|
870 | * @param array|string $where
|
870 | * @param array|string $where
|
871 | * Only the records matching this condition are deleted
|
871 | * Only the records matching this condition are deleted
|
872 | * @return bool
|
872 | * @return bool
|
873 | * @see PDOStatement::execute()
|
873 | * @see PDOStatement::execute()
|
874 | */
|
874 | */
|
875 | public function delete($tables, $where = null) |
875 | public function delete($tables, $where = null) |
876 | {
|
876 | {
|
877 | if (!$tables) |
877 | if (!$tables) |
878 | {
|
878 | {
|
879 | throw new InvalidArgumentException('No table specified'); |
879 | throw new InvalidArgumentException('No table specified'); |
880 | }
|
880 | }
|
881 | 881 | ||
882 | if (is_array($tables)) |
882 | if (is_array($tables)) |
883 | {
|
883 | {
|
884 | $tables = implode(', ', $tables); |
884 | $tables = implode(', ', $tables); |
885 | }
|
885 | }
|
886 | 886 | ||
887 | $params = array(); |
887 | $params = array(); |
888 | 888 | ||
889 | $query = "DELETE FROM {$tables}" . $this->_where($where); |
889 | $query = "DELETE FROM {$tables}" . $this->_where($where); |
890 | 890 | ||
891 | $stmt = $this->prepare($query); |
891 | $stmt = $this->prepare($query); |
892 | 892 | ||
893 | if ($this->_isAssociativeArray($where)) |
893 | if ($this->_isAssociativeArray($where)) |
894 | {
|
894 | {
|
895 | foreach ($where as $column => $condition) |
895 | foreach ($where as $column => $condition) |
896 | {
|
896 | {
|
897 | if (is_array($condition) && $this->_isAssociativeArray($condition)) |
897 | if (is_array($condition) && $this->_isAssociativeArray($condition)) |
898 | {
|
898 | {
|
899 | reset($condition); |
899 | reset($condition); |
900 | $condition = $condition[key($condition)]; |
900 | $condition = $condition[key($condition)]; |
901 | 901 | ||
902 | if (is_array($condition)) |
902 | if (is_array($condition)) |
903 | {
|
903 | {
|
904 | foreach (self::_expand($condition, $column) as $param_index => $param_name) |
904 | foreach (self::_expand($condition, $column) as $param_index => $param_name) |
905 | {
|
905 | {
|
906 | $params[$param_name] = $condition[$param_index]; |
906 | $params[$param_name] = $condition[$param_index]; |
907 | }
|
907 | }
|
908 | }
|
908 | }
|
909 | }
|
909 | }
|
910 | else
|
910 | else
|
911 | {
|
911 | {
|
912 | $params[":{$column}"] = $condition; |
912 | $params[":{$column}"] = $condition; |
913 | }
|
913 | }
|
914 | }
|
914 | }
|
915 | }
|
915 | }
|
916 | 916 | ||
917 | /* DEBUG */
|
917 | /* DEBUG */
|
918 | if (defined('DEBUG') && DEBUG > 1) |
918 | if (defined('DEBUG') && DEBUG > 1) |
919 | {
|
919 | {
|
920 | debug(array( |
920 | debug(array( |
921 | 'query' => $query, |
921 | 'query' => $query, |
922 | 'params' => $params |
922 | 'params' => $params |
923 | )); |
923 | )); |
924 | }
|
924 | }
|
925 | 925 | ||
926 | $success =& $this->_lastSuccess; |
926 | $success =& $this->_lastSuccess; |
927 | $success = $stmt->execute($params); |
927 | $success = $stmt->execute($params); |
928 | 928 | ||
929 | $result =& $this->_lastResult; |
929 | $result =& $this->_lastResult; |
930 | $result = $stmt->fetchAll(); |
930 | $result = $stmt->fetchAll(); |
931 | 931 | ||
932 | $errorInfo =& $this->_lastError; |
932 | $errorInfo =& $this->_lastError; |
933 | $errorInfo = $stmt->errorInfo(); |
933 | $errorInfo = $stmt->errorInfo(); |
934 | 934 | ||
935 | if (defined('DEBUG') && DEBUG > 1) |
935 | if (defined('DEBUG') && DEBUG > 1) |
936 | {
|
936 | {
|
937 | debug(array( |
937 | debug(array( |
938 | '_lastSuccess' => $success, |
938 | '_lastSuccess' => $success, |
939 | '_lastError' => $errorInfo, |
939 | '_lastError' => $errorInfo, |
940 | '_lastResult' => $result |
940 | '_lastResult' => $result |
941 | )); |
941 | )); |
942 | }
|
942 | }
|
943 | 943 | ||
944 | return $success; |
944 | return $success; |
945 | }
|
945 | }
|
946 | }
|
946 | }
|