PluginProbe ʕ •ᴥ•ʔ
MailPoet – Newsletters, Email Marketing, and Automation / 3.19.3
MailPoet – Newsletters, Email Marketing, and Automation v3.19.3
5.30.0 5.29.0 5.28.1 5.28.0 5.27.0 5.26.0 5.26.1 5.25.0 5.24.0 4.43.0 4.43.1 4.44.0 4.44.1 4.45.0 4.46.0 4.47.0 4.48.0 4.48.1 4.48.2 4.49.0 4.49.1 4.5.0 4.5.1 4.5.2 4.50.0 4.50.1 4.51.0 4.51.1 4.51.2 4.52.0 4.53.0 4.54.0 4.55.0 4.56.0 4.57.0 4.58.0 4.58.1 4.58.2 4.6.0 4.6.1 4.6.2 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 5.0.0 5.0.1 5.0.2 5.1.0 5.1.1 5.10.0 5.10.1 5.11.0 5.12.0 5.12.1 5.12.10 5.12.11 5.12.12 5.12.13 5.12.2 5.12.3 5.12.4 5.12.5 5.12.6 5.12.7 5.12.8 5.12.9 5.13.0 5.13.1 5.13.2 5.14.0 5.14.1 5.14.2 5.14.3 5.15.0 5.15.1 5.16.0 5.16.1 5.16.2 5.16.3 5.16.4 5.17.0 5.17.1 5.17.2 5.17.3 5.17.4 5.17.5 5.17.6 5.18.0 5.19.0 5.2.0 5.2.1 5.2.2 5.2.3 5.20.0 5.21.0 5.21.1 5.21.2 5.21.3 5.22.0 5.22.1 5.22.2 5.22.3 5.22.4 5.23.0 5.23.1 5.23.2 5.3.0 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.3.6 5.3.7 5.4.0 5.4.1 5.4.2 5.5.0 5.5.1 5.5.2 5.6.0 5.6.1 5.6.2 5.6.3 5.6.4 5.7.0 5.7.1 5.8.0 5.8.1 5.9.0 3.0.0-beta.15 3.7.1 3.0.0-beta.16 3.7.2 3.0.0-beta.17 3.7.3 3.0.0-beta.18 3.7.4 3.0.0-beta.19 3.7.5 3.0.0-beta.2 3.7.6 3.0.0-beta.20 3.7.8 3.0.0-beta.21 3.70.0 3.0.0-beta.22 3.71.0 3.0.0-beta.23 3.71.1 3.0.0-beta.23.1 3.71.2 3.0.0-beta.23.2 3.71.3 3.0.0-beta.24 3.72.0 3.0.0-beta.25 3.73.0 3.0.0-beta.26 3.73.1 3.0.0-beta.27 3.73.2 3.0.0-beta.28 3.74.0 3.0.0-beta.29 3.74.1 3.0.0-beta.3 3.74.2 3.0.0-beta.30 3.74.3 3.0.0-beta.31 3.75.0 3.0.0-beta.32 3.75.1 3.0.0-beta.33 3.76.0 3.0.0-beta.33.1 3.77.0 3.0.0-beta.34.0.0 3.77.1 3.0.0-beta.36.0.0 3.78.0 3.0.0-beta.36.0.1 3.79.0 3.0.0-beta.36.2.0 3.8 3.0.0-beta.36.3.0 3.8.1 3.0.0-beta.36.3.1 3.8.2 3.0.0-beta.37.0.0 3.8.3 3.0.0-beta.4 3.8.4 3.0.0-beta.5 3.8.5 3.0.0-beta.6 3.8.6 3.0.0-beta.7 3.80.0 3.0.0-beta.7.1 3.81.0 3.0.0-beta.8 3.82.0 3.0.0-beta.9 3.83.0 3.0.0-rc.1.0.0 3.84.0 3.0.0-rc.1.0.1 3.84.1 3.0.0-rc.1.0.2 3.85.0 3.0.0-rc.1.0.3 3.85.1 3.0.0-rc.1.0.4 3.86.0 3.0.0-rc.2.0.0 3.87.0 3.0.0-rc.2.0.1 3.87.1 3.0.0-rc.2.0.2 3.87.2 3.0.0-rc.2.0.3 3.88.0 3.0.1 3.88.1 3.0.2 3.88.2 3.0.3 3.89.0 3.0.4 3.89.1 3.0.5 3.89.2 3.0.6 3.89.3 3.0.7 3.89.4 3.0.8 3.9.0 3.0.9 3.9.1 3.1.0 3.90.0 3.10 3.90.1 3.10.1 3.90.2 3.100.0 3.91.0 3.100.1 3.91.1 3.100.2 3.92.0 3.101.0 3.92.1 3.101.1 3.93.0 3.102.0 3.93.1 3.102.1 3.94.0 3.103.0 3.95.0 3.103.1 3.95.1 3.11.0 3.96.0 3.11.1 3.96.1 3.11.2 3.97.0 3.11.3 3.98.0 3.11.4 3.98.1 3.11.5 3.99.0 3.12.0 3.99.1 3.12.1 4.0.0 3.13.0 4.0.1 3.14.0 4.1.0 3.14.1 4.1.1 3.15.0 4.10.0 3.16.0 4.11.0 3.16.1 4.11.1 3.16.2 4.12.0 3.16.3 4.12.1 3.17.0 4.12.2 3.17.1 4.13.0 3.17.2 4.14.0 3.18.0 4.15.0 3.18.1 4.16.0 3.18.2 4.17.0 3.19.0 4.17.1 3.19.1 4.18.0 3.19.2 4.18.1 3.19.3 4.19.0 3.2.0 4.2.0 3.2.1 4.20.0 3.2.2 4.20.1 3.2.3 4.20.2 3.2.4 4.21.0 3.2.5 4.22.0 3.20.0 4.22.1 3.21.0 4.22.2 3.21.1 4.23.0 3.22.0 4.24.0 3.23.0 4.25.0 3.23.1 4.26.0 3.23.2 4.26.1 3.24.0 4.27.0 3.25.0 4.28.0 3.25.1 4.29.0 3.26.0 4.3.0 3.26.1 4.3.1 3.27.0 4.30.0 3.28.0 4.31.0 3.29.0 4.31.1 3.3.0 4.32.0 3.3.1 4.33.0 3.3.2 4.34.0 3.3.3 4.35.0 3.3.4 4.35.1 3.3.5 4.36.0 3.3.6 4.37.0 3.30.0 4.38.0 3.31.0 4.39.0 3.31.1 4.4.0 3.32.0 4.40.0 3.32.1 4.41.0 3.32.2 4.41.1 3.33.0 4.41.2 3.34.0 4.41.3 3.34.1 4.42.0 3.34.2 4.42.1 3.34.3 3.34.4 3.35.0 3.35.1 3.35.3 3.35.4 3.36.0 3.37.0 3.37.1 3.37.2 3.37.3 3.38.0 3.38.1 3.39.0 3.39.1 3.39.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.40.0 3.40.1 3.41.0 3.41.1 3.41.2 3.42.0 3.42.1 3.42.2 3.42.3 3.43.0 3.43.1 3.44.0 3.45.0 3.45.1 3.46.0 3.46.1 3.46.10 3.46.11 3.46.12 3.46.13 3.46.14 3.46.2 3.46.3 3.46.4 3.46.5 3.46.6 3.46.7 3.46.8 3.46.9 3.47.0 3.47.1 3.47.10 3.47.11 3.47.2 3.47.3 3.47.5 3.47.6 3.47.7 3.47.9 3.48.0 3.48.1 3.49.0 3.49.1 3.5.0 3.5.1 3.50.0 3.51.0 3.51.1 3.51.2 3.52.0 3.53.0 3.54.0 3.54.1 3.54.2 3.54.3 3.55.0 3.55.1 3.56.0 3.56.1 3.56.2 3.57.0 3.57.1 3.58.0 3.59.0 3.59.1 3.59.2 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.60.0 3.60.1 3.60.10 3.60.11 3.60.12 3.60.2 3.60.3 3.60.4 3.60.6 3.60.7 3.60.8 3.60.9 3.61.0 3.62.0 3.62.1 3.63.0 3.64.0 3.64.1 3.64.2 3.64.3 3.65.0 trunk 3.65.1 3.0.0 3.66.0 3.0.0-beta.1 3.67.0 3.0.0-beta.10 3.67.1 3.0.0-beta.11 3.68.0 3.0.0-beta.12 3.69.0 3.0.0-beta.13 3.69.1 3.0.0-beta.14 3.7.0
mailpoet / vendor / j4mie / idiorm / idiorm.php
mailpoet / vendor / j4mie / idiorm Last commit date
idiorm.php 8 years ago index.php 9 years ago
idiorm.php
2540 lines
1 <?php
2
3 /**
4 *
5 * Idiorm
6 *
7 * http://github.com/j4mie/idiorm/
8 *
9 * A single-class super-simple database abstraction layer for PHP.
10 * Provides (nearly) zero-configuration object-relational mapping
11 * and a fluent interface for building basic, commonly-used queries.
12 *
13 * BSD Licensed.
14 *
15 * Copyright (c) 2010, Jamie Matthews
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are met:
20 *
21 * * Redistributions of source code must retain the above copyright notice, this
22 * list of conditions and the following disclaimer.
23 *
24 * * Redistributions in binary form must reproduce the above copyright notice,
25 * this list of conditions and the following disclaimer in the documentation
26 * and/or other materials provided with the distribution.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *
39 *
40 * The methods documented below are magic methods that conform to PSR-1.
41 * This documentation exposes these methods to doc generators and IDEs.
42 * @see http://www.php-fig.org/psr/psr-1/
43 *
44 * @method static array|string getConfig($key = null, $connection_name = self::DEFAULT_CONNECTION)
45 * @method static null resetConfig()
46 * @method static \ORM forTable($table_name, $connection_name = self::DEFAULT_CONNECTION)
47 * @method static null setDb($db, $connection_name = self::DEFAULT_CONNECTION)
48 * @method static null resetDb()
49 * @method static null setupLimitClauseStyle($connection_name)
50 * @method static \PDO getDb($connection_name = self::DEFAULT_CONNECTION)
51 * @method static bool rawExecute($query, $parameters = array())
52 * @method static \PDOStatement getLastStatement()
53 * @method static string getLastQuery($connection_name = null)
54 * @method static array getQueryLog($connection_name = self::DEFAULT_CONNECTION)
55 * @method array getConnectionNames()
56 * @method $this useIdColumn($id_column)
57 * @method \ORM|bool findOne($id=null)
58 * @method array|\IdiormResultSet findMany()
59 * @method \IdiormResultSet findResultSet()
60 * @method array findArray()
61 * @method $this forceAllDirty()
62 * @method $this rawQuery($query, $parameters = array())
63 * @method $this tableAlias($alias)
64 * @method int countNullIdColumns()
65 * @method $this selectExpr($expr, $alias=null)
66 * @method \ORM selectMany($values)
67 * @method \ORM selectManyExpr($values)
68 * @method $this rawJoin($table, $constraint, $table_alias, $parameters = array())
69 * @method $this innerJoin($table, $constraint, $table_alias=null)
70 * @method $this leftOuterJoin($table, $constraint, $table_alias=null)
71 * @method $this rightOuterJoin($table, $constraint, $table_alias=null)
72 * @method $this fullOuterJoin($table, $constraint, $table_alias=null)
73 * @method $this whereEqual($column_name, $value=null)
74 * @method $this whereNotEqual($column_name, $value=null)
75 * @method $this whereIdIs($id)
76 * @method $this whereAnyIs($values, $operator='=')
77 * @method array|string whereIdIn($ids)
78 * @method $this whereLike($column_name, $value=null)
79 * @method $this whereNotLike($column_name, $value=null)
80 * @method $this whereGt($column_name, $value=null)
81 * @method $this whereLt($column_name, $value=null)
82 * @method $this whereGte($column_name, $value=null)
83 * @method $this whereLte($column_name, $value=null)
84 * @method $this whereIn($column_name, $values)
85 * @method $this whereNotIn($column_name, $values)
86 * @method $this whereNull($column_name)
87 * @method $this whereNotNull($column_name)
88 * @method $this whereRaw($clause, $parameters=array())
89 * @method $this orderByDesc($column_name)
90 * @method $this orderByAsc($column_name)
91 * @method $this orderByExpr($clause)
92 * @method $this groupBy($column_name)
93 * @method $this groupByExpr($expr)
94 * @method $this havingEqual($column_name, $value=null)
95 * @method $this havingNotEqual($column_name, $value=null)
96 * @method $this havingIdIs($id)
97 * @method $this havingLike($column_name, $value=null)
98 * @method $this havingNotLike($column_name, $value=null)
99 * @method $this havingGt($column_name, $value=null)
100 * @method $this havingLt($column_name, $value=null)
101 * @method $this havingGte($column_name, $value=null)
102 * @method $this havingLte($column_name, $value=null)
103 * @method $this havingIn($column_name, $values=null)
104 * @method $this havingNotIn($column_name, $values=null)
105 * @method $this havingNull($column_name)
106 * @method $this havingNotNull($column_name)
107 * @method $this havingRaw($clause, $parameters=array())
108 * @method static this clearCache($table_name = null, $connection_name = self::DEFAULT_CONNECTION)
109 * @method array asArray()
110 * @method bool setExpr($key, $value = null)
111 * @method bool isDirty($key)
112 * @method bool isNew()
113 */
114
115 class ORM implements ArrayAccess {
116
117 // ----------------------- //
118 // --- CLASS CONSTANTS --- //
119 // ----------------------- //
120
121 // WHERE and HAVING condition array keys
122 const CONDITION_FRAGMENT = 0;
123 const CONDITION_VALUES = 1;
124
125 const DEFAULT_CONNECTION = 'default';
126
127 // Limit clause style
128 const LIMIT_STYLE_TOP_N = "top";
129 const LIMIT_STYLE_LIMIT = "limit";
130
131 // ------------------------ //
132 // --- CLASS PROPERTIES --- //
133 // ------------------------ //
134
135 // Class configuration
136 protected static $_default_config = array(
137 'connection_string' => 'sqlite::memory:',
138 'id_column' => 'id',
139 'id_column_overrides' => array(),
140 'error_mode' => PDO::ERRMODE_EXCEPTION,
141 'username' => null,
142 'password' => null,
143 'driver_options' => null,
144 'identifier_quote_character' => null, // if this is null, will be autodetected
145 'limit_clause_style' => null, // if this is null, will be autodetected
146 'logging' => false,
147 'logger' => null,
148 'caching' => false,
149 'caching_auto_clear' => false,
150 'return_result_sets' => false,
151 );
152
153 // Map of configuration settings
154 protected static $_config = array();
155
156 // Map of database connections, instances of the PDO class
157 protected static $_db = array();
158
159 // Last query run, only populated if logging is enabled
160 protected static $_last_query;
161
162 // Log of all queries run, mapped by connection key, only populated if logging is enabled
163 protected static $_query_log = array();
164
165 // Query cache, only used if query caching is enabled
166 protected static $_query_cache = array();
167
168 // Reference to previously used PDOStatement object to enable low-level access, if needed
169 protected static $_last_statement = null;
170
171 // --------------------------- //
172 // --- INSTANCE PROPERTIES --- //
173 // --------------------------- //
174
175 // Key name of the connections in self::$_db used by this instance
176 protected $_connection_name;
177
178 // The name of the table the current ORM instance is associated with
179 protected $_table_name;
180
181 // Alias for the table to be used in SELECT queries
182 protected $_table_alias = null;
183
184 // Values to be bound to the query
185 protected $_values = array();
186
187 // Columns to select in the result
188 protected $_result_columns = array('*');
189
190 // Are we using the default result column or have these been manually changed?
191 protected $_using_default_result_columns = true;
192
193 // Join sources
194 protected $_join_sources = array();
195
196 // Should the query include a DISTINCT keyword?
197 protected $_distinct = false;
198
199 // Is this a raw query?
200 protected $_is_raw_query = false;
201
202 // The raw query
203 protected $_raw_query = '';
204
205 // The raw query parameters
206 protected $_raw_parameters = array();
207
208 // Array of WHERE clauses
209 protected $_where_conditions = array();
210
211 // LIMIT
212 protected $_limit = null;
213
214 // OFFSET
215 protected $_offset = null;
216
217 // ORDER BY
218 protected $_order_by = array();
219
220 // GROUP BY
221 protected $_group_by = array();
222
223 // HAVING
224 protected $_having_conditions = array();
225
226 // The data for a hydrated instance of the class
227 protected $_data = array();
228
229 // Fields that have been modified during the
230 // lifetime of the object
231 protected $_dirty_fields = array();
232
233 // Fields that are to be inserted in the DB raw
234 protected $_expr_fields = array();
235
236 // Is this a new object (has create() been called)?
237 protected $_is_new = false;
238
239 // Name of the column to use as the primary key for
240 // this instance only. Overrides the config settings.
241 protected $_instance_id_column = null;
242
243 // ---------------------- //
244 // --- STATIC METHODS --- //
245 // ---------------------- //
246
247 /**
248 * Pass configuration settings to the class in the form of
249 * key/value pairs. As a shortcut, if the second argument
250 * is omitted and the key is a string, the setting is
251 * assumed to be the DSN string used by PDO to connect
252 * to the database (often, this will be the only configuration
253 * required to use Idiorm). If you have more than one setting
254 * you wish to configure, another shortcut is to pass an array
255 * of settings (and omit the second argument).
256 * @param string|array $key
257 * @param mixed $value
258 * @param string $connection_name Which connection to use
259 */
260 public static function configure($key, $value = null, $connection_name = self::DEFAULT_CONNECTION) {
261 self::_setup_db_config($connection_name); //ensures at least default config is set
262
263 if (is_array($key)) {
264 // Shortcut: If only one array argument is passed,
265 // assume it's an array of configuration settings
266 foreach ($key as $conf_key => $conf_value) {
267 self::configure($conf_key, $conf_value, $connection_name);
268 }
269 } else {
270 if (is_null($value)) {
271 // Shortcut: If only one string argument is passed,
272 // assume it's a connection string
273 $value = $key;
274 $key = 'connection_string';
275 }
276 self::$_config[$connection_name][$key] = $value;
277 }
278 }
279
280 /**
281 * Retrieve configuration options by key, or as whole array.
282 * @param string $key
283 * @param string $connection_name Which connection to use
284 */
285 public static function get_config($key = null, $connection_name = self::DEFAULT_CONNECTION) {
286 if ($key) {
287 return self::$_config[$connection_name][$key];
288 } else {
289 return self::$_config[$connection_name];
290 }
291 }
292
293 /**
294 * Delete all configs in _config array.
295 */
296 public static function reset_config() {
297 self::$_config = array();
298 }
299
300 /**
301 * Despite its slightly odd name, this is actually the factory
302 * method used to acquire instances of the class. It is named
303 * this way for the sake of a readable interface, ie
304 * ORM::for_table('table_name')->find_one()-> etc. As such,
305 * this will normally be the first method called in a chain.
306 * @param string $table_name
307 * @param string $connection_name Which connection to use
308 * @return ORM
309 */
310 public static function for_table($table_name, $connection_name = self::DEFAULT_CONNECTION) {
311 self::_setup_db($connection_name);
312 return new self($table_name, array(), $connection_name);
313 }
314
315 /**
316 * Set up the database connection used by the class
317 * @param string $connection_name Which connection to use
318 */
319 protected static function _setup_db($connection_name = self::DEFAULT_CONNECTION) {
320 if (!array_key_exists($connection_name, self::$_db) ||
321 !is_object(self::$_db[$connection_name])) {
322 self::_setup_db_config($connection_name);
323
324 $db = new PDO(
325 self::$_config[$connection_name]['connection_string'],
326 self::$_config[$connection_name]['username'],
327 self::$_config[$connection_name]['password'],
328 self::$_config[$connection_name]['driver_options']
329 );
330
331 $db->setAttribute(PDO::ATTR_ERRMODE, self::$_config[$connection_name]['error_mode']);
332 self::set_db($db, $connection_name);
333 }
334 }
335
336 /**
337 * Ensures configuration (multiple connections) is at least set to default.
338 * @param string $connection_name Which connection to use
339 */
340 protected static function _setup_db_config($connection_name) {
341 if (!array_key_exists($connection_name, self::$_config)) {
342 self::$_config[$connection_name] = self::$_default_config;
343 }
344 }
345
346 /**
347 * Set the PDO object used by Idiorm to communicate with the database.
348 * This is public in case the ORM should use a ready-instantiated
349 * PDO object as its database connection. Accepts an optional string key
350 * to identify the connection if multiple connections are used.
351 * @param PDO $db
352 * @param string $connection_name Which connection to use
353 */
354 public static function set_db($db, $connection_name = self::DEFAULT_CONNECTION) {
355 self::_setup_db_config($connection_name);
356 self::$_db[$connection_name] = $db;
357 if(!is_null(self::$_db[$connection_name])) {
358 self::_setup_identifier_quote_character($connection_name);
359 self::_setup_limit_clause_style($connection_name);
360 }
361 }
362
363 /**
364 * Close and delete all registered PDO objects in _db array.
365 */
366 public static function reset_db() {
367 self::$_db = null;
368
369 self::$_db = array();
370 }
371
372 /**
373 * Detect and initialise the character used to quote identifiers
374 * (table names, column names etc). If this has been specified
375 * manually using ORM::configure('identifier_quote_character', 'some-char'),
376 * this will do nothing.
377 * @param string $connection_name Which connection to use
378 */
379 protected static function _setup_identifier_quote_character($connection_name) {
380 if (is_null(self::$_config[$connection_name]['identifier_quote_character'])) {
381 self::$_config[$connection_name]['identifier_quote_character'] =
382 self::_detect_identifier_quote_character($connection_name);
383 }
384 }
385
386 /**
387 * Detect and initialise the limit clause style ("SELECT TOP 5" /
388 * "... LIMIT 5"). If this has been specified manually using
389 * ORM::configure('limit_clause_style', 'top'), this will do nothing.
390 * @param string $connection_name Which connection to use
391 */
392 public static function _setup_limit_clause_style($connection_name) {
393 if (is_null(self::$_config[$connection_name]['limit_clause_style'])) {
394 self::$_config[$connection_name]['limit_clause_style'] =
395 self::_detect_limit_clause_style($connection_name);
396 }
397 }
398
399 /**
400 * Return the correct character used to quote identifiers (table
401 * names, column names etc) by looking at the driver being used by PDO.
402 * @param string $connection_name Which connection to use
403 * @return string
404 */
405 protected static function _detect_identifier_quote_character($connection_name) {
406 switch(self::get_db($connection_name)->getAttribute(PDO::ATTR_DRIVER_NAME)) {
407 case 'pgsql':
408 case 'sqlsrv':
409 case 'dblib':
410 case 'mssql':
411 case 'sybase':
412 case 'firebird':
413 return '"';
414 case 'mysql':
415 case 'sqlite':
416 case 'sqlite2':
417 default:
418 return '`';
419 }
420 }
421
422 /**
423 * Returns a constant after determining the appropriate limit clause
424 * style
425 * @param string $connection_name Which connection to use
426 * @return string Limit clause style keyword/constant
427 */
428 protected static function _detect_limit_clause_style($connection_name) {
429 switch(self::get_db($connection_name)->getAttribute(PDO::ATTR_DRIVER_NAME)) {
430 case 'sqlsrv':
431 case 'dblib':
432 case 'mssql':
433 return ORM::LIMIT_STYLE_TOP_N;
434 default:
435 return ORM::LIMIT_STYLE_LIMIT;
436 }
437 }
438
439 /**
440 * Returns the PDO instance used by the the ORM to communicate with
441 * the database. This can be called if any low-level DB access is
442 * required outside the class. If multiple connections are used,
443 * accepts an optional key name for the connection.
444 * @param string $connection_name Which connection to use
445 * @return PDO
446 */
447 public static function get_db($connection_name = self::DEFAULT_CONNECTION) {
448 self::_setup_db($connection_name); // required in case this is called before Idiorm is instantiated
449 return self::$_db[$connection_name];
450 }
451
452 /**
453 * Executes a raw query as a wrapper for PDOStatement::execute.
454 * Useful for queries that can't be accomplished through Idiorm,
455 * particularly those using engine-specific features.
456 * @example raw_execute('SELECT `name`, AVG(`order`) FROM `customer` GROUP BY `name` HAVING AVG(`order`) > 10')
457 * @example raw_execute('INSERT OR REPLACE INTO `widget` (`id`, `name`) SELECT `id`, `name` FROM `other_table`')
458 * @param string $query The raw SQL query
459 * @param array $parameters Optional bound parameters
460 * @param string $connection_name Which connection to use
461 * @return bool Success
462 */
463 public static function raw_execute($query, $parameters = array(), $connection_name = self::DEFAULT_CONNECTION) {
464 self::_setup_db($connection_name);
465 return self::_execute($query, $parameters, $connection_name);
466 }
467
468 /**
469 * Returns the PDOStatement instance last used by any connection wrapped by the ORM.
470 * Useful for access to PDOStatement::rowCount() or error information
471 * @return PDOStatement
472 */
473 public static function get_last_statement() {
474 return self::$_last_statement;
475 }
476
477 /**
478 * Internal helper method for executing statments. Logs queries, and
479 * stores statement object in ::_last_statment, accessible publicly
480 * through ::get_last_statement()
481 * @param string $query
482 * @param array $parameters An array of parameters to be bound in to the query
483 * @param string $connection_name Which connection to use
484 * @return bool Response of PDOStatement::execute()
485 */
486 protected static function _execute($query, $parameters = array(), $connection_name = self::DEFAULT_CONNECTION) {
487 $statement = self::get_db($connection_name)->prepare($query);
488 self::$_last_statement = $statement;
489 $time = microtime(true);
490
491 foreach ($parameters as $key => &$param) {
492 if (is_null($param)) {
493 $type = PDO::PARAM_NULL;
494 } else if (is_bool($param)) {
495 $type = PDO::PARAM_BOOL;
496 } else if (is_int($param)) {
497 $type = PDO::PARAM_INT;
498 } else {
499 $type = PDO::PARAM_STR;
500 }
501
502 $statement->bindParam(is_int($key) ? ++$key : $key, $param, $type);
503 }
504
505 $q = $statement->execute();
506 self::_log_query($query, $parameters, $connection_name, (microtime(true)-$time));
507
508 return $q;
509 }
510
511 /**
512 * Add a query to the internal query log. Only works if the
513 * 'logging' config option is set to true.
514 *
515 * This works by manually binding the parameters to the query - the
516 * query isn't executed like this (PDO normally passes the query and
517 * parameters to the database which takes care of the binding) but
518 * doing it this way makes the logged queries more readable.
519 * @param string $query
520 * @param array $parameters An array of parameters to be bound in to the query
521 * @param string $connection_name Which connection to use
522 * @param float $query_time Query time
523 * @return bool
524 */
525 protected static function _log_query($query, $parameters, $connection_name, $query_time) {
526 // If logging is not enabled, do nothing
527 if (!self::$_config[$connection_name]['logging']) {
528 return false;
529 }
530
531 if (!isset(self::$_query_log[$connection_name])) {
532 self::$_query_log[$connection_name] = array();
533 }
534
535 if (empty($parameters)) {
536 $bound_query = $query;
537 } else {
538 // Escape the parameters
539 $parameters = array_map(array(self::get_db($connection_name), 'quote'), $parameters);
540
541 if (array_values($parameters) === $parameters) {
542 // ? placeholders
543 // Avoid %format collision for vsprintf
544 $query = str_replace("%", "%%", $query);
545
546 // Replace placeholders in the query for vsprintf
547 if(false !== strpos($query, "'") || false !== strpos($query, '"')) {
548 $query = IdiormString::str_replace_outside_quotes("?", "%s", $query);
549 } else {
550 $query = str_replace("?", "%s", $query);
551 }
552
553 // Replace the question marks in the query with the parameters
554 $bound_query = vsprintf($query, $parameters);
555 } else {
556 // named placeholders
557 foreach ($parameters as $key => $val) {
558 $query = str_replace($key, $val, $query);
559 }
560 $bound_query = $query;
561 }
562 }
563
564 self::$_last_query = $bound_query;
565 self::$_query_log[$connection_name][] = $bound_query;
566
567
568 if(is_callable(self::$_config[$connection_name]['logger'])){
569 $logger = self::$_config[$connection_name]['logger'];
570 $logger($bound_query, $query_time);
571 }
572
573 return true;
574 }
575
576 /**
577 * Get the last query executed. Only works if the
578 * 'logging' config option is set to true. Otherwise
579 * this will return null. Returns last query from all connections if
580 * no connection_name is specified
581 * @param null|string $connection_name Which connection to use
582 * @return string
583 */
584 public static function get_last_query($connection_name = null) {
585 if ($connection_name === null) {
586 return self::$_last_query;
587 }
588 if (!isset(self::$_query_log[$connection_name])) {
589 return '';
590 }
591
592 return end(self::$_query_log[$connection_name]);
593 }
594
595 /**
596 * Get an array containing all the queries run on a
597 * specified connection up to now.
598 * Only works if the 'logging' config option is
599 * set to true. Otherwise, returned array will be empty.
600 * @param string $connection_name Which connection to use
601 */
602 public static function get_query_log($connection_name = self::DEFAULT_CONNECTION) {
603 if (isset(self::$_query_log[$connection_name])) {
604 return self::$_query_log[$connection_name];
605 }
606 return array();
607 }
608
609 /**
610 * Get a list of the available connection names
611 * @return array
612 */
613 public static function get_connection_names() {
614 return array_keys(self::$_db);
615 }
616
617 // ------------------------ //
618 // --- INSTANCE METHODS --- //
619 // ------------------------ //
620
621 /**
622 * "Private" constructor; shouldn't be called directly.
623 * Use the ORM::for_table factory method instead.
624 */
625 protected function __construct($table_name, $data = array(), $connection_name = self::DEFAULT_CONNECTION) {
626 $this->_table_name = $table_name;
627 $this->_data = $data;
628
629 $this->_connection_name = $connection_name;
630 self::_setup_db_config($connection_name);
631 }
632
633 /**
634 * Create a new, empty instance of the class. Used
635 * to add a new row to your database. May optionally
636 * be passed an associative array of data to populate
637 * the instance. If so, all fields will be flagged as
638 * dirty so all will be saved to the database when
639 * save() is called.
640 */
641 public function create($data=null) {
642 $this->_is_new = true;
643 if (!is_null($data)) {
644 return $this->hydrate($data)->force_all_dirty();
645 }
646 return $this;
647 }
648
649 /**
650 * Specify the ID column to use for this instance or array of instances only.
651 * This overrides the id_column and id_column_overrides settings.
652 *
653 * This is mostly useful for libraries built on top of Idiorm, and will
654 * not normally be used in manually built queries. If you don't know why
655 * you would want to use this, you should probably just ignore it.
656 */
657 public function use_id_column($id_column) {
658 $this->_instance_id_column = $id_column;
659 return $this;
660 }
661
662 /**
663 * Create an ORM instance from the given row (an associative
664 * array of data fetched from the database)
665 */
666 protected function _create_instance_from_row($row) {
667 $instance = self::for_table($this->_table_name, $this->_connection_name);
668 $instance->use_id_column($this->_instance_id_column);
669 $instance->hydrate($row);
670 return $instance;
671 }
672
673 /**
674 * Tell the ORM that you are expecting a single result
675 * back from your query, and execute it. Will return
676 * a single instance of the ORM class, or false if no
677 * rows were returned.
678 * As a shortcut, you may supply an ID as a parameter
679 * to this method. This will perform a primary key
680 * lookup on the table.
681 */
682 public function find_one($id=null) {
683 if (!is_null($id)) {
684 $this->where_id_is($id);
685 }
686 $this->limit(1);
687 $rows = $this->_run();
688
689 if (empty($rows)) {
690 return false;
691 }
692
693 return $this->_create_instance_from_row($rows[0]);
694 }
695
696 /**
697 * Tell the ORM that you are expecting multiple results
698 * from your query, and execute it. Will return an array
699 * of instances of the ORM class, or an empty array if
700 * no rows were returned.
701 * @return array|\IdiormResultSet
702 */
703 public function find_many() {
704 if(self::$_config[$this->_connection_name]['return_result_sets']) {
705 return $this->find_result_set();
706 }
707 return $this->_find_many();
708 }
709
710 /**
711 * Tell the ORM that you are expecting multiple results
712 * from your query, and execute it. Will return an array
713 * of instances of the ORM class, or an empty array if
714 * no rows were returned.
715 * @return array
716 */
717 protected function _find_many() {
718 $rows = $this->_run();
719 return array_map(array($this, '_create_instance_from_row'), $rows);
720 }
721
722 /**
723 * Tell the ORM that you are expecting multiple results
724 * from your query, and execute it. Will return a result set object
725 * containing instances of the ORM class.
726 * @return \IdiormResultSet
727 */
728 public function find_result_set() {
729 return new IdiormResultSet($this->_find_many());
730 }
731
732 /**
733 * Tell the ORM that you are expecting multiple results
734 * from your query, and execute it. Will return an array,
735 * or an empty array if no rows were returned.
736 * @return array
737 */
738 public function find_array() {
739 return $this->_run();
740 }
741
742 /**
743 * Tell the ORM that you wish to execute a COUNT query.
744 * Will return an integer representing the number of
745 * rows returned.
746 */
747 public function count($column = '*') {
748 return $this->_call_aggregate_db_function(__FUNCTION__, $column);
749 }
750
751 /**
752 * Tell the ORM that you wish to execute a MAX query.
753 * Will return the max value of the choosen column.
754 */
755 public function max($column) {
756 return $this->_call_aggregate_db_function(__FUNCTION__, $column);
757 }
758
759 /**
760 * Tell the ORM that you wish to execute a MIN query.
761 * Will return the min value of the choosen column.
762 */
763 public function min($column) {
764 return $this->_call_aggregate_db_function(__FUNCTION__, $column);
765 }
766
767 /**
768 * Tell the ORM that you wish to execute a AVG query.
769 * Will return the average value of the choosen column.
770 */
771 public function avg($column) {
772 return $this->_call_aggregate_db_function(__FUNCTION__, $column);
773 }
774
775 /**
776 * Tell the ORM that you wish to execute a SUM query.
777 * Will return the sum of the choosen column.
778 */
779 public function sum($column) {
780 return $this->_call_aggregate_db_function(__FUNCTION__, $column);
781 }
782
783 /**
784 * Execute an aggregate query on the current connection.
785 * @param string $sql_function The aggregate function to call eg. MIN, COUNT, etc
786 * @param string $column The column to execute the aggregate query against
787 * @return int
788 */
789 protected function _call_aggregate_db_function($sql_function, $column) {
790 $alias = strtolower($sql_function);
791 $sql_function = strtoupper($sql_function);
792 if('*' != $column) {
793 $column = $this->_quote_identifier($column);
794 }
795 $result_columns = $this->_result_columns;
796 $this->_result_columns = array();
797 $this->select_expr("$sql_function($column)", $alias);
798 $result = $this->find_one();
799 $this->_result_columns = $result_columns;
800
801 $return_value = 0;
802 if($result !== false && isset($result->$alias)) {
803 if (!is_numeric($result->$alias)) {
804 $return_value = $result->$alias;
805 }
806 elseif((int) $result->$alias == (float) $result->$alias) {
807 $return_value = (int) $result->$alias;
808 } else {
809 $return_value = (float) $result->$alias;
810 }
811 }
812 return $return_value;
813 }
814
815 /**
816 * This method can be called to hydrate (populate) this
817 * instance of the class from an associative array of data.
818 * This will usually be called only from inside the class,
819 * but it's public in case you need to call it directly.
820 */
821 public function hydrate($data=array()) {
822 $this->_data = $data;
823 return $this;
824 }
825
826 /**
827 * Force the ORM to flag all the fields in the $data array
828 * as "dirty" and therefore update them when save() is called.
829 */
830 public function force_all_dirty() {
831 $this->_dirty_fields = $this->_data;
832 return $this;
833 }
834
835 /**
836 * Perform a raw query. The query can contain placeholders in
837 * either named or question mark style. If placeholders are
838 * used, the parameters should be an array of values which will
839 * be bound to the placeholders in the query. If this method
840 * is called, all other query building methods will be ignored.
841 */
842 public function raw_query($query, $parameters = array()) {
843 $this->_is_raw_query = true;
844 $this->_raw_query = $query;
845 $this->_raw_parameters = $parameters;
846 return $this;
847 }
848
849 /**
850 * Add an alias for the main table to be used in SELECT queries
851 */
852 public function table_alias($alias) {
853 $this->_table_alias = $alias;
854 return $this;
855 }
856
857 /**
858 * Internal method to add an unquoted expression to the set
859 * of columns returned by the SELECT query. The second optional
860 * argument is the alias to return the expression as.
861 */
862 protected function _add_result_column($expr, $alias=null) {
863 if (!is_null($alias)) {
864 $expr .= " AS " . $this->_quote_identifier($alias);
865 }
866
867 if ($this->_using_default_result_columns) {
868 $this->_result_columns = array($expr);
869 $this->_using_default_result_columns = false;
870 } else {
871 $this->_result_columns[] = $expr;
872 }
873 return $this;
874 }
875
876 /**
877 * Counts the number of columns that belong to the primary
878 * key and their value is null.
879 */
880 public function count_null_id_columns() {
881 if (is_array($this->_get_id_column_name())) {
882 return count(array_filter($this->id(), 'is_null'));
883 } else {
884 return is_null($this->id()) ? 1 : 0;
885 }
886 }
887
888 /**
889 * Add a column to the list of columns returned by the SELECT
890 * query. This defaults to '*'. The second optional argument is
891 * the alias to return the column as.
892 */
893 public function select($column, $alias=null) {
894 $column = $this->_quote_identifier($column);
895 return $this->_add_result_column($column, $alias);
896 }
897
898 /**
899 * Add an unquoted expression to the list of columns returned
900 * by the SELECT query. The second optional argument is
901 * the alias to return the column as.
902 */
903 public function select_expr($expr, $alias=null) {
904 return $this->_add_result_column($expr, $alias);
905 }
906
907 /**
908 * Add columns to the list of columns returned by the SELECT
909 * query. This defaults to '*'. Many columns can be supplied
910 * as either an array or as a list of parameters to the method.
911 *
912 * Note that the alias must not be numeric - if you want a
913 * numeric alias then prepend it with some alpha chars. eg. a1
914 *
915 * @example select_many(array('alias' => 'column', 'column2', 'alias2' => 'column3'), 'column4', 'column5');
916 * @example select_many('column', 'column2', 'column3');
917 * @example select_many(array('column', 'column2', 'column3'), 'column4', 'column5');
918 *
919 * @return \ORM
920 */
921 public function select_many() {
922 $columns = func_get_args();
923 if(!empty($columns)) {
924 $columns = $this->_normalise_select_many_columns($columns);
925 foreach($columns as $alias => $column) {
926 if(is_numeric($alias)) {
927 $alias = null;
928 }
929 $this->select($column, $alias);
930 }
931 }
932 return $this;
933 }
934
935 /**
936 * Add an unquoted expression to the list of columns returned
937 * by the SELECT query. Many columns can be supplied as either
938 * an array or as a list of parameters to the method.
939 *
940 * Note that the alias must not be numeric - if you want a
941 * numeric alias then prepend it with some alpha chars. eg. a1
942 *
943 * @example select_many_expr(array('alias' => 'column', 'column2', 'alias2' => 'column3'), 'column4', 'column5')
944 * @example select_many_expr('column', 'column2', 'column3')
945 * @example select_many_expr(array('column', 'column2', 'column3'), 'column4', 'column5')
946 *
947 * @return \ORM
948 */
949 public function select_many_expr() {
950 $columns = func_get_args();
951 if(!empty($columns)) {
952 $columns = $this->_normalise_select_many_columns($columns);
953 foreach($columns as $alias => $column) {
954 if(is_numeric($alias)) {
955 $alias = null;
956 }
957 $this->select_expr($column, $alias);
958 }
959 }
960 return $this;
961 }
962
963 /**
964 * Take a column specification for the select many methods and convert it
965 * into a normalised array of columns and aliases.
966 *
967 * It is designed to turn the following styles into a normalised array:
968 *
969 * array(array('alias' => 'column', 'column2', 'alias2' => 'column3'), 'column4', 'column5'))
970 *
971 * @param array $columns
972 * @return array
973 */
974 protected function _normalise_select_many_columns($columns) {
975 $return = array();
976 foreach($columns as $column) {
977 if(is_array($column)) {
978 foreach($column as $key => $value) {
979 if(!is_numeric($key)) {
980 $return[$key] = $value;
981 } else {
982 $return[] = $value;
983 }
984 }
985 } else {
986 $return[] = $column;
987 }
988 }
989 return $return;
990 }
991
992 /**
993 * Add a DISTINCT keyword before the list of columns in the SELECT query
994 */
995 public function distinct() {
996 $this->_distinct = true;
997 return $this;
998 }
999
1000 /**
1001 * Internal method to add a JOIN source to the query.
1002 *
1003 * The join_operator should be one of INNER, LEFT OUTER, CROSS etc - this
1004 * will be prepended to JOIN.
1005 *
1006 * The table should be the name of the table to join to.
1007 *
1008 * The constraint may be either a string or an array with three elements. If it
1009 * is a string, it will be compiled into the query as-is, with no escaping. The
1010 * recommended way to supply the constraint is as an array with three elements:
1011 *
1012 * first_column, operator, second_column
1013 *
1014 * Example: array('user.id', '=', 'profile.user_id')
1015 *
1016 * will compile to
1017 *
1018 * ON `user`.`id` = `profile`.`user_id`
1019 *
1020 * The final (optional) argument specifies an alias for the joined table.
1021 */
1022 protected function _add_join_source($join_operator, $table, $constraint, $table_alias=null) {
1023
1024 $join_operator = trim("{$join_operator} JOIN");
1025
1026 $table = $this->_quote_identifier($table);
1027
1028 // Add table alias if present
1029 if (!is_null($table_alias)) {
1030 $table_alias = $this->_quote_identifier($table_alias);
1031 $table .= " {$table_alias}";
1032 }
1033
1034 // Build the constraint
1035 if (is_array($constraint)) {
1036 list($first_column, $operator, $second_column) = $constraint;
1037 $first_column = $this->_quote_identifier($first_column);
1038 $second_column = $this->_quote_identifier($second_column);
1039 $constraint = "{$first_column} {$operator} {$second_column}";
1040 }
1041
1042 $this->_join_sources[] = "{$join_operator} {$table} ON {$constraint}";
1043 return $this;
1044 }
1045
1046 /**
1047 * Add a RAW JOIN source to the query
1048 */
1049 public function raw_join($table, $constraint, $table_alias, $parameters = array()) {
1050 // Add table alias if present
1051 if (!is_null($table_alias)) {
1052 $table_alias = $this->_quote_identifier($table_alias);
1053 $table .= " {$table_alias}";
1054 }
1055
1056 $this->_values = array_merge($this->_values, $parameters);
1057
1058 // Build the constraint
1059 if (is_array($constraint)) {
1060 list($first_column, $operator, $second_column) = $constraint;
1061 $first_column = $this->_quote_identifier($first_column);
1062 $second_column = $this->_quote_identifier($second_column);
1063 $constraint = "{$first_column} {$operator} {$second_column}";
1064 }
1065
1066 $this->_join_sources[] = "{$table} ON {$constraint}";
1067 return $this;
1068 }
1069
1070 /**
1071 * Add a simple JOIN source to the query
1072 */
1073 public function join($table, $constraint, $table_alias=null) {
1074 return $this->_add_join_source("", $table, $constraint, $table_alias);
1075 }
1076
1077 /**
1078 * Add an INNER JOIN souce to the query
1079 */
1080 public function inner_join($table, $constraint, $table_alias=null) {
1081 return $this->_add_join_source("INNER", $table, $constraint, $table_alias);
1082 }
1083
1084 /**
1085 * Add a LEFT OUTER JOIN souce to the query
1086 */
1087 public function left_outer_join($table, $constraint, $table_alias=null) {
1088 return $this->_add_join_source("LEFT OUTER", $table, $constraint, $table_alias);
1089 }
1090
1091 /**
1092 * Add an RIGHT OUTER JOIN souce to the query
1093 */
1094 public function right_outer_join($table, $constraint, $table_alias=null) {
1095 return $this->_add_join_source("RIGHT OUTER", $table, $constraint, $table_alias);
1096 }
1097
1098 /**
1099 * Add an FULL OUTER JOIN souce to the query
1100 */
1101 public function full_outer_join($table, $constraint, $table_alias=null) {
1102 return $this->_add_join_source("FULL OUTER", $table, $constraint, $table_alias);
1103 }
1104
1105 /**
1106 * Internal method to add a HAVING condition to the query
1107 */
1108 protected function _add_having($fragment, $values=array()) {
1109 return $this->_add_condition('having', $fragment, $values);
1110 }
1111
1112 /**
1113 * Internal method to add a HAVING condition to the query
1114 */
1115 protected function _add_simple_having($column_name, $separator, $value) {
1116 return $this->_add_simple_condition('having', $column_name, $separator, $value);
1117 }
1118
1119 /**
1120 * Internal method to add a HAVING clause with multiple values (like IN and NOT IN)
1121 */
1122 public function _add_having_placeholder($column_name, $separator, $values) {
1123 if (!is_array($column_name)) {
1124 $data = array($column_name => $values);
1125 } else {
1126 $data = $column_name;
1127 }
1128 $result = $this;
1129 foreach ($data as $key => $val) {
1130 $column = $result->_quote_identifier($key);
1131 $placeholders = $result->_create_placeholders($val);
1132 $result = $result->_add_having("{$column} {$separator} ({$placeholders})", $val);
1133 }
1134 return $result;
1135 }
1136
1137 /**
1138 * Internal method to add a HAVING clause with no parameters(like IS NULL and IS NOT NULL)
1139 */
1140 public function _add_having_no_value($column_name, $operator) {
1141 $conditions = (is_array($column_name)) ? $column_name : array($column_name);
1142 $result = $this;
1143 foreach($conditions as $column) {
1144 $column = $this->_quote_identifier($column);
1145 $result = $result->_add_having("{$column} {$operator}");
1146 }
1147 return $result;
1148 }
1149
1150 /**
1151 * Internal method to add a WHERE condition to the query
1152 */
1153 protected function _add_where($fragment, $values=array()) {
1154 return $this->_add_condition('where', $fragment, $values);
1155 }
1156
1157 /**
1158 * Internal method to add a WHERE condition to the query
1159 */
1160 protected function _add_simple_where($column_name, $separator, $value) {
1161 return $this->_add_simple_condition('where', $column_name, $separator, $value);
1162 }
1163
1164 /**
1165 * Add a WHERE clause with multiple values (like IN and NOT IN)
1166 */
1167 public function _add_where_placeholder($column_name, $separator, $values) {
1168 if (!is_array($column_name)) {
1169 $data = array($column_name => $values);
1170 } else {
1171 $data = $column_name;
1172 }
1173 $result = $this;
1174 foreach ($data as $key => $val) {
1175 $column = $result->_quote_identifier($key);
1176 $placeholders = $result->_create_placeholders($val);
1177 $result = $result->_add_where("{$column} {$separator} ({$placeholders})", $val);
1178 }
1179 return $result;
1180 }
1181
1182 /**
1183 * Add a WHERE clause with no parameters(like IS NULL and IS NOT NULL)
1184 */
1185 public function _add_where_no_value($column_name, $operator) {
1186 $conditions = (is_array($column_name)) ? $column_name : array($column_name);
1187 $result = $this;
1188 foreach($conditions as $column) {
1189 $column = $this->_quote_identifier($column);
1190 $result = $result->_add_where("{$column} {$operator}");
1191 }
1192 return $result;
1193 }
1194
1195 /**
1196 * Internal method to add a HAVING or WHERE condition to the query
1197 */
1198 protected function _add_condition($type, $fragment, $values=array()) {
1199 $conditions_class_property_name = "_{$type}_conditions";
1200 if (!is_array($values)) {
1201 $values = array($values);
1202 }
1203 array_push($this->$conditions_class_property_name, array(
1204 self::CONDITION_FRAGMENT => $fragment,
1205 self::CONDITION_VALUES => $values,
1206 ));
1207 return $this;
1208 }
1209
1210 /**
1211 * Helper method to compile a simple COLUMN SEPARATOR VALUE
1212 * style HAVING or WHERE condition into a string and value ready to
1213 * be passed to the _add_condition method. Avoids duplication
1214 * of the call to _quote_identifier
1215 *
1216 * If column_name is an associative array, it will add a condition for each column
1217 */
1218 protected function _add_simple_condition($type, $column_name, $separator, $value) {
1219 $multiple = is_array($column_name) ? $column_name : array($column_name => $value);
1220 $result = $this;
1221
1222 foreach($multiple as $key => $val) {
1223 // Add the table name in case of ambiguous columns
1224 if (count($result->_join_sources) > 0 && strpos($key, '.') === false) {
1225 $table = $result->_table_name;
1226 if (!is_null($result->_table_alias)) {
1227 $table = $result->_table_alias;
1228 }
1229
1230 $key = "{$table}.{$key}";
1231 }
1232 $key = $result->_quote_identifier($key);
1233 $result = $result->_add_condition($type, "{$key} {$separator} ?", $val);
1234 }
1235 return $result;
1236 }
1237
1238 /**
1239 * Return a string containing the given number of question marks,
1240 * separated by commas. Eg "?, ?, ?"
1241 */
1242 protected function _create_placeholders($fields) {
1243 if(!empty($fields)) {
1244 $db_fields = array();
1245 foreach($fields as $key => $value) {
1246 // Process expression fields directly into the query
1247 if(array_key_exists($key, $this->_expr_fields)) {
1248 $db_fields[] = $value;
1249 } else {
1250 $db_fields[] = '?';
1251 }
1252 }
1253 return implode(', ', $db_fields);
1254 }
1255 }
1256
1257 /**
1258 * Helper method that filters a column/value array returning only those
1259 * columns that belong to a compound primary key.
1260 *
1261 * If the key contains a column that does not exist in the given array,
1262 * a null value will be returned for it.
1263 */
1264 protected function _get_compound_id_column_values($value) {
1265 $filtered = array();
1266 foreach($this->_get_id_column_name() as $key) {
1267 $filtered[$key] = isset($value[$key]) ? $value[$key] : null;
1268 }
1269 return $filtered;
1270 }
1271
1272 /**
1273 * Helper method that filters an array containing compound column/value
1274 * arrays.
1275 */
1276 protected function _get_compound_id_column_values_array($values) {
1277 $filtered = array();
1278 foreach($values as $value) {
1279 $filtered[] = $this->_get_compound_id_column_values($value);
1280 }
1281 return $filtered;
1282 }
1283
1284 /**
1285 * Add a WHERE column = value clause to your query. Each time
1286 * this is called in the chain, an additional WHERE will be
1287 * added, and these will be ANDed together when the final query
1288 * is built.
1289 *
1290 * If you use an array in $column_name, a new clause will be
1291 * added for each element. In this case, $value is ignored.
1292 */
1293 public function where($column_name, $value=null) {
1294 return $this->where_equal($column_name, $value);
1295 }
1296
1297 /**
1298 * More explicitly named version of for the where() method.
1299 * Can be used if preferred.
1300 */
1301 public function where_equal($column_name, $value=null) {
1302 return $this->_add_simple_where($column_name, '=', $value);
1303 }
1304
1305 /**
1306 * Add a WHERE column != value clause to your query.
1307 */
1308 public function where_not_equal($column_name, $value=null) {
1309 return $this->_add_simple_where($column_name, '!=', $value);
1310 }
1311
1312 /**
1313 * Special method to query the table by its primary key
1314 *
1315 * If primary key is compound, only the columns that
1316 * belong to they key will be used for the query
1317 */
1318 public function where_id_is($id) {
1319 return (is_array($this->_get_id_column_name())) ?
1320 $this->where($this->_get_compound_id_column_values($id), null) :
1321 $this->where($this->_get_id_column_name(), $id);
1322 }
1323
1324 /**
1325 * Allows adding a WHERE clause that matches any of the conditions
1326 * specified in the array. Each element in the associative array will
1327 * be a different condition, where the key will be the column name.
1328 *
1329 * By default, an equal operator will be used against all columns, but
1330 * it can be overriden for any or every column using the second parameter.
1331 *
1332 * Each condition will be ORed together when added to the final query.
1333 */
1334 public function where_any_is($values, $operator='=') {
1335 $data = array();
1336 $query = array("((");
1337 $first = true;
1338 foreach ($values as $value) {
1339 if ($first) {
1340 $first = false;
1341 } else {
1342 $query[] = ") OR (";
1343 }
1344 $firstsub = true;
1345 foreach($value as $key => $item) {
1346 $op = is_string($operator) ? $operator : (isset($operator[$key]) ? $operator[$key] : '=');
1347 if ($firstsub) {
1348 $firstsub = false;
1349 } else {
1350 $query[] = "AND";
1351 }
1352 $query[] = $this->_quote_identifier($key);
1353 $data[] = $item;
1354 $query[] = $op . " ?";
1355 }
1356 }
1357 $query[] = "))";
1358 return $this->where_raw(join($query, ' '), $data);
1359 }
1360
1361 /**
1362 * Similar to where_id_is() but allowing multiple primary keys.
1363 *
1364 * If primary key is compound, only the columns that
1365 * belong to they key will be used for the query
1366 */
1367 public function where_id_in($ids) {
1368 return (is_array($this->_get_id_column_name())) ?
1369 $this->where_any_is($this->_get_compound_id_column_values_array($ids)) :
1370 $this->where_in($this->_get_id_column_name(), $ids);
1371 }
1372
1373 /**
1374 * Add a WHERE ... LIKE clause to your query.
1375 */
1376 public function where_like($column_name, $value=null) {
1377 return $this->_add_simple_where($column_name, 'LIKE', $value);
1378 }
1379
1380 /**
1381 * Add where WHERE ... NOT LIKE clause to your query.
1382 */
1383 public function where_not_like($column_name, $value=null) {
1384 return $this->_add_simple_where($column_name, 'NOT LIKE', $value);
1385 }
1386
1387 /**
1388 * Add a WHERE ... > clause to your query
1389 */
1390 public function where_gt($column_name, $value=null) {
1391 return $this->_add_simple_where($column_name, '>', $value);
1392 }
1393
1394 /**
1395 * Add a WHERE ... < clause to your query
1396 */
1397 public function where_lt($column_name, $value=null) {
1398 return $this->_add_simple_where($column_name, '<', $value);
1399 }
1400
1401 /**
1402 * Add a WHERE ... >= clause to your query
1403 */
1404 public function where_gte($column_name, $value=null) {
1405 return $this->_add_simple_where($column_name, '>=', $value);
1406 }
1407
1408 /**
1409 * Add a WHERE ... <= clause to your query
1410 */
1411 public function where_lte($column_name, $value=null) {
1412 return $this->_add_simple_where($column_name, '<=', $value);
1413 }
1414
1415 /**
1416 * Add a WHERE ... IN clause to your query
1417 */
1418 public function where_in($column_name, $values) {
1419 return $this->_add_where_placeholder($column_name, 'IN', $values);
1420 }
1421
1422 /**
1423 * Add a WHERE ... NOT IN clause to your query
1424 */
1425 public function where_not_in($column_name, $values) {
1426 return $this->_add_where_placeholder($column_name, 'NOT IN', $values);
1427 }
1428
1429 /**
1430 * Add a WHERE column IS NULL clause to your query
1431 */
1432 public function where_null($column_name) {
1433 return $this->_add_where_no_value($column_name, "IS NULL");
1434 }
1435
1436 /**
1437 * Add a WHERE column IS NOT NULL clause to your query
1438 */
1439 public function where_not_null($column_name) {
1440 return $this->_add_where_no_value($column_name, "IS NOT NULL");
1441 }
1442
1443 /**
1444 * Add a raw WHERE clause to the query. The clause should
1445 * contain question mark placeholders, which will be bound
1446 * to the parameters supplied in the second argument.
1447 */
1448 public function where_raw($clause, $parameters=array()) {
1449 return $this->_add_where($clause, $parameters);
1450 }
1451
1452 /**
1453 * Add a LIMIT to the query
1454 */
1455 public function limit($limit) {
1456 $this->_limit = $limit;
1457 return $this;
1458 }
1459
1460 /**
1461 * Add an OFFSET to the query
1462 */
1463 public function offset($offset) {
1464 $this->_offset = $offset;
1465 return $this;
1466 }
1467
1468 /**
1469 * Add an ORDER BY clause to the query
1470 */
1471 protected function _add_order_by($column_name, $ordering) {
1472 $column_name = $this->_quote_identifier($column_name);
1473 $this->_order_by[] = "{$column_name} {$ordering}";
1474 return $this;
1475 }
1476
1477 /**
1478 * Add an ORDER BY column DESC clause
1479 */
1480 public function order_by_desc($column_name) {
1481 return $this->_add_order_by($column_name, 'DESC');
1482 }
1483
1484 /**
1485 * Add an ORDER BY column ASC clause
1486 */
1487 public function order_by_asc($column_name) {
1488 return $this->_add_order_by($column_name, 'ASC');
1489 }
1490
1491 /**
1492 * Add an unquoted expression as an ORDER BY clause
1493 */
1494 public function order_by_expr($clause) {
1495 $this->_order_by[] = $clause;
1496 return $this;
1497 }
1498
1499 /**
1500 * Add a column to the list of columns to GROUP BY
1501 */
1502 public function group_by($column_name) {
1503 $column_name = $this->_quote_identifier($column_name);
1504 $this->_group_by[] = $column_name;
1505 return $this;
1506 }
1507
1508 /**
1509 * Add an unquoted expression to the list of columns to GROUP BY
1510 */
1511 public function group_by_expr($expr) {
1512 $this->_group_by[] = $expr;
1513 return $this;
1514 }
1515
1516 /**
1517 * Add a HAVING column = value clause to your query. Each time
1518 * this is called in the chain, an additional HAVING will be
1519 * added, and these will be ANDed together when the final query
1520 * is built.
1521 *
1522 * If you use an array in $column_name, a new clause will be
1523 * added for each element. In this case, $value is ignored.
1524 */
1525 public function having($column_name, $value=null) {
1526 return $this->having_equal($column_name, $value);
1527 }
1528
1529 /**
1530 * More explicitly named version of for the having() method.
1531 * Can be used if preferred.
1532 */
1533 public function having_equal($column_name, $value=null) {
1534 return $this->_add_simple_having($column_name, '=', $value);
1535 }
1536
1537 /**
1538 * Add a HAVING column != value clause to your query.
1539 */
1540 public function having_not_equal($column_name, $value=null) {
1541 return $this->_add_simple_having($column_name, '!=', $value);
1542 }
1543
1544 /**
1545 * Special method to query the table by its primary key.
1546 *
1547 * If primary key is compound, only the columns that
1548 * belong to they key will be used for the query
1549 */
1550 public function having_id_is($id) {
1551 return (is_array($this->_get_id_column_name())) ?
1552 $this->having($this->_get_compound_id_column_values($id), null) :
1553 $this->having($this->_get_id_column_name(), $id);
1554 }
1555
1556 /**
1557 * Add a HAVING ... LIKE clause to your query.
1558 */
1559 public function having_like($column_name, $value=null) {
1560 return $this->_add_simple_having($column_name, 'LIKE', $value);
1561 }
1562
1563 /**
1564 * Add where HAVING ... NOT LIKE clause to your query.
1565 */
1566 public function having_not_like($column_name, $value=null) {
1567 return $this->_add_simple_having($column_name, 'NOT LIKE', $value);
1568 }
1569
1570 /**
1571 * Add a HAVING ... > clause to your query
1572 */
1573 public function having_gt($column_name, $value=null) {
1574 return $this->_add_simple_having($column_name, '>', $value);
1575 }
1576
1577 /**
1578 * Add a HAVING ... < clause to your query
1579 */
1580 public function having_lt($column_name, $value=null) {
1581 return $this->_add_simple_having($column_name, '<', $value);
1582 }
1583
1584 /**
1585 * Add a HAVING ... >= clause to your query
1586 */
1587 public function having_gte($column_name, $value=null) {
1588 return $this->_add_simple_having($column_name, '>=', $value);
1589 }
1590
1591 /**
1592 * Add a HAVING ... <= clause to your query
1593 */
1594 public function having_lte($column_name, $value=null) {
1595 return $this->_add_simple_having($column_name, '<=', $value);
1596 }
1597
1598 /**
1599 * Add a HAVING ... IN clause to your query
1600 */
1601 public function having_in($column_name, $values=null) {
1602 return $this->_add_having_placeholder($column_name, 'IN', $values);
1603 }
1604
1605 /**
1606 * Add a HAVING ... NOT IN clause to your query
1607 */
1608 public function having_not_in($column_name, $values=null) {
1609 return $this->_add_having_placeholder($column_name, 'NOT IN', $values);
1610 }
1611
1612 /**
1613 * Add a HAVING column IS NULL clause to your query
1614 */
1615 public function having_null($column_name) {
1616 return $this->_add_having_no_value($column_name, 'IS NULL');
1617 }
1618
1619 /**
1620 * Add a HAVING column IS NOT NULL clause to your query
1621 */
1622 public function having_not_null($column_name) {
1623 return $this->_add_having_no_value($column_name, 'IS NOT NULL');
1624 }
1625
1626 /**
1627 * Add a raw HAVING clause to the query. The clause should
1628 * contain question mark placeholders, which will be bound
1629 * to the parameters supplied in the second argument.
1630 */
1631 public function having_raw($clause, $parameters=array()) {
1632 return $this->_add_having($clause, $parameters);
1633 }
1634
1635 /**
1636 * Build a SELECT statement based on the clauses that have
1637 * been passed to this instance by chaining method calls.
1638 */
1639 protected function _build_select() {
1640 // If the query is raw, just set the $this->_values to be
1641 // the raw query parameters and return the raw query
1642 if ($this->_is_raw_query) {
1643 $this->_values = $this->_raw_parameters;
1644 return $this->_raw_query;
1645 }
1646
1647 // Build and return the full SELECT statement by concatenating
1648 // the results of calling each separate builder method.
1649 return $this->_join_if_not_empty(" ", array(
1650 $this->_build_select_start(),
1651 $this->_build_join(),
1652 $this->_build_where(),
1653 $this->_build_group_by(),
1654 $this->_build_having(),
1655 $this->_build_order_by(),
1656 $this->_build_limit(),
1657 $this->_build_offset(),
1658 ));
1659 }
1660
1661 /**
1662 * Build the start of the SELECT statement
1663 */
1664 protected function _build_select_start() {
1665 $fragment = 'SELECT ';
1666 $result_columns = join(', ', $this->_result_columns);
1667
1668 if (!is_null($this->_limit) &&
1669 self::$_config[$this->_connection_name]['limit_clause_style'] === ORM::LIMIT_STYLE_TOP_N) {
1670 $fragment .= "TOP {$this->_limit} ";
1671 }
1672
1673 if ($this->_distinct) {
1674 $result_columns = 'DISTINCT ' . $result_columns;
1675 }
1676
1677 $fragment .= "{$result_columns} FROM " . $this->_quote_identifier($this->_table_name);
1678
1679 if (!is_null($this->_table_alias)) {
1680 $fragment .= " " . $this->_quote_identifier($this->_table_alias);
1681 }
1682 return $fragment;
1683 }
1684
1685 /**
1686 * Build the JOIN sources
1687 */
1688 protected function _build_join() {
1689 if (count($this->_join_sources) === 0) {
1690 return '';
1691 }
1692
1693 return join(" ", $this->_join_sources);
1694 }
1695
1696 /**
1697 * Build the WHERE clause(s)
1698 */
1699 protected function _build_where() {
1700 return $this->_build_conditions('where');
1701 }
1702
1703 /**
1704 * Build the HAVING clause(s)
1705 */
1706 protected function _build_having() {
1707 return $this->_build_conditions('having');
1708 }
1709
1710 /**
1711 * Build GROUP BY
1712 */
1713 protected function _build_group_by() {
1714 if (count($this->_group_by) === 0) {
1715 return '';
1716 }
1717 return "GROUP BY " . join(", ", $this->_group_by);
1718 }
1719
1720 /**
1721 * Build a WHERE or HAVING clause
1722 * @param string $type
1723 * @return string
1724 */
1725 protected function _build_conditions($type) {
1726 $conditions_class_property_name = "_{$type}_conditions";
1727 // If there are no clauses, return empty string
1728 if (count($this->$conditions_class_property_name) === 0) {
1729 return '';
1730 }
1731
1732 $conditions = array();
1733 foreach ($this->$conditions_class_property_name as $condition) {
1734 $conditions[] = $condition[self::CONDITION_FRAGMENT];
1735 $this->_values = array_merge($this->_values, $condition[self::CONDITION_VALUES]);
1736 }
1737
1738 return strtoupper($type) . " " . join(" AND ", $conditions);
1739 }
1740
1741 /**
1742 * Build ORDER BY
1743 */
1744 protected function _build_order_by() {
1745 if (count($this->_order_by) === 0) {
1746 return '';
1747 }
1748 return "ORDER BY " . join(", ", $this->_order_by);
1749 }
1750
1751 /**
1752 * Build LIMIT
1753 */
1754 protected function _build_limit() {
1755 $fragment = '';
1756 if (!is_null($this->_limit) &&
1757 self::$_config[$this->_connection_name]['limit_clause_style'] == ORM::LIMIT_STYLE_LIMIT) {
1758 if (self::get_db($this->_connection_name)->getAttribute(PDO::ATTR_DRIVER_NAME) == 'firebird') {
1759 $fragment = 'ROWS';
1760 } else {
1761 $fragment = 'LIMIT';
1762 }
1763 $fragment .= " {$this->_limit}";
1764 }
1765 return $fragment;
1766 }
1767
1768 /**
1769 * Build OFFSET
1770 */
1771 protected function _build_offset() {
1772 if (!is_null($this->_offset)) {
1773 $clause = 'OFFSET';
1774 if (self::get_db($this->_connection_name)->getAttribute(PDO::ATTR_DRIVER_NAME) == 'firebird') {
1775 $clause = 'TO';
1776 }
1777 return "$clause " . $this->_offset;
1778 }
1779 return '';
1780 }
1781
1782 /**
1783 * Wrapper around PHP's join function which
1784 * only adds the pieces if they are not empty.
1785 */
1786 protected function _join_if_not_empty($glue, $pieces) {
1787 $filtered_pieces = array();
1788 foreach ($pieces as $piece) {
1789 if (is_string($piece)) {
1790 $piece = trim($piece);
1791 }
1792 if (!empty($piece)) {
1793 $filtered_pieces[] = $piece;
1794 }
1795 }
1796 return join($glue, $filtered_pieces);
1797 }
1798
1799 /**
1800 * Quote a string that is used as an identifier
1801 * (table names, column names etc). This method can
1802 * also deal with dot-separated identifiers eg table.column
1803 */
1804 protected function _quote_one_identifier($identifier) {
1805 $parts = explode('.', $identifier);
1806 $parts = array_map(array($this, '_quote_identifier_part'), $parts);
1807 return join('.', $parts);
1808 }
1809
1810 /**
1811 * Quote a string that is used as an identifier
1812 * (table names, column names etc) or an array containing
1813 * multiple identifiers. This method can also deal with
1814 * dot-separated identifiers eg table.column
1815 */
1816 protected function _quote_identifier($identifier) {
1817 if (is_array($identifier)) {
1818 $result = array_map(array($this, '_quote_one_identifier'), $identifier);
1819 return join(', ', $result);
1820 } else {
1821 return $this->_quote_one_identifier($identifier);
1822 }
1823 }
1824
1825 /**
1826 * This method performs the actual quoting of a single
1827 * part of an identifier, using the identifier quote
1828 * character specified in the config (or autodetected).
1829 */
1830 protected function _quote_identifier_part($part) {
1831 if ($part === '*') {
1832 return $part;
1833 }
1834
1835 $quote_character = self::$_config[$this->_connection_name]['identifier_quote_character'];
1836 // double up any identifier quotes to escape them
1837 return $quote_character .
1838 str_replace($quote_character,
1839 $quote_character . $quote_character,
1840 $part
1841 ) . $quote_character;
1842 }
1843
1844 /**
1845 * Create a cache key for the given query and parameters.
1846 */
1847 protected static function _create_cache_key($query, $parameters, $table_name = null, $connection_name = self::DEFAULT_CONNECTION) {
1848 if(isset(self::$_config[$connection_name]['create_cache_key']) and is_callable(self::$_config[$connection_name]['create_cache_key'])){
1849 return call_user_func_array(self::$_config[$connection_name]['create_cache_key'], array($query, $parameters, $table_name, $connection_name));
1850 }
1851 $parameter_string = join(',', $parameters);
1852 $key = $query . ':' . $parameter_string;
1853 return sha1($key);
1854 }
1855
1856 /**
1857 * Check the query cache for the given cache key. If a value
1858 * is cached for the key, return the value. Otherwise, return false.
1859 */
1860 protected static function _check_query_cache($cache_key, $table_name = null, $connection_name = self::DEFAULT_CONNECTION) {
1861 if(isset(self::$_config[$connection_name]['check_query_cache']) and is_callable(self::$_config[$connection_name]['check_query_cache'])){
1862 return call_user_func_array(self::$_config[$connection_name]['check_query_cache'], array($cache_key, $table_name, $connection_name));
1863 } elseif (isset(self::$_query_cache[$connection_name][$cache_key])) {
1864 return self::$_query_cache[$connection_name][$cache_key];
1865 }
1866 return false;
1867 }
1868
1869 /**
1870 * Clear the query cache
1871 */
1872 public static function clear_cache($table_name = null, $connection_name = self::DEFAULT_CONNECTION) {
1873 self::$_query_cache = array();
1874 if(isset(self::$_config[$connection_name]['clear_cache']) and is_callable(self::$_config[$connection_name]['clear_cache'])){
1875 return call_user_func_array(self::$_config[$connection_name]['clear_cache'], array($table_name, $connection_name));
1876 }
1877 }
1878
1879 /**
1880 * Add the given value to the query cache.
1881 */
1882 protected static function _cache_query_result($cache_key, $value, $table_name = null, $connection_name = self::DEFAULT_CONNECTION) {
1883 if(isset(self::$_config[$connection_name]['cache_query_result']) and is_callable(self::$_config[$connection_name]['cache_query_result'])){
1884 return call_user_func_array(self::$_config[$connection_name]['cache_query_result'], array($cache_key, $value, $table_name, $connection_name));
1885 } elseif (!isset(self::$_query_cache[$connection_name])) {
1886 self::$_query_cache[$connection_name] = array();
1887 }
1888 self::$_query_cache[$connection_name][$cache_key] = $value;
1889 }
1890
1891 /**
1892 * Execute the SELECT query that has been built up by chaining methods
1893 * on this class. Return an array of rows as associative arrays.
1894 */
1895 protected function _run() {
1896 $query = $this->_build_select();
1897 $caching_enabled = self::$_config[$this->_connection_name]['caching'];
1898
1899 if ($caching_enabled) {
1900 $cache_key = self::_create_cache_key($query, $this->_values, $this->_table_name, $this->_connection_name);
1901 $cached_result = self::_check_query_cache($cache_key, $this->_table_name, $this->_connection_name);
1902
1903 if ($cached_result !== false) {
1904 $this->_reset_idiorm_state();
1905 return $cached_result;
1906 }
1907 }
1908
1909 self::_execute($query, $this->_values, $this->_connection_name);
1910 $statement = self::get_last_statement();
1911
1912 $rows = array();
1913 while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
1914 $rows[] = $row;
1915 }
1916
1917 if ($caching_enabled) {
1918 self::_cache_query_result($cache_key, $rows, $this->_table_name, $this->_connection_name);
1919 }
1920
1921 $this->_reset_idiorm_state();
1922 return $rows;
1923 }
1924
1925 /**
1926 * Reset the Idiorm instance state
1927 */
1928 private function _reset_idiorm_state() {
1929 $this->_values = array();
1930 $this->_result_columns = array('*');
1931 $this->_using_default_result_columns = true;
1932 }
1933
1934 /**
1935 * Return the raw data wrapped by this ORM
1936 * instance as an associative array. Column
1937 * names may optionally be supplied as arguments,
1938 * if so, only those keys will be returned.
1939 */
1940 public function as_array() {
1941 if (func_num_args() === 0) {
1942 return $this->_data;
1943 }
1944 $args = func_get_args();
1945 return array_intersect_key($this->_data, array_flip($args));
1946 }
1947
1948 /**
1949 * Return the value of a property of this object (database row)
1950 * or null if not present.
1951 *
1952 * If a column-names array is passed, it will return a associative array
1953 * with the value of each column or null if it is not present.
1954 */
1955 public function get($key) {
1956 if (is_array($key)) {
1957 $result = array();
1958 foreach($key as $column) {
1959 $result[$column] = isset($this->_data[$column]) ? $this->_data[$column] : null;
1960 }
1961 return $result;
1962 } else {
1963 return isset($this->_data[$key]) ? $this->_data[$key] : null;
1964 }
1965 }
1966
1967 /**
1968 * Return the name of the column in the database table which contains
1969 * the primary key ID of the row.
1970 */
1971 protected function _get_id_column_name() {
1972 if (!is_null($this->_instance_id_column)) {
1973 return $this->_instance_id_column;
1974 }
1975 if (isset(self::$_config[$this->_connection_name]['id_column_overrides'][$this->_table_name])) {
1976 return self::$_config[$this->_connection_name]['id_column_overrides'][$this->_table_name];
1977 }
1978 return self::$_config[$this->_connection_name]['id_column'];
1979 }
1980
1981 /**
1982 * Get the primary key ID of this object.
1983 */
1984 public function id($disallow_null = false) {
1985 $id = $this->get($this->_get_id_column_name());
1986
1987 if ($disallow_null) {
1988 if (is_array($id)) {
1989 foreach ($id as $id_part) {
1990 if ($id_part === null) {
1991 throw new Exception('Primary key ID contains null value(s)');
1992 }
1993 }
1994 } else if ($id === null) {
1995 throw new Exception('Primary key ID missing from row or is null');
1996 }
1997 }
1998
1999 return $id;
2000 }
2001
2002 /**
2003 * Set a property to a particular value on this object.
2004 * To set multiple properties at once, pass an associative array
2005 * as the first parameter and leave out the second parameter.
2006 * Flags the properties as 'dirty' so they will be saved to the
2007 * database when save() is called.
2008 */
2009 public function set($key, $value = null) {
2010 return $this->_set_orm_property($key, $value);
2011 }
2012
2013 /**
2014 * Set a property to a particular value on this object.
2015 * To set multiple properties at once, pass an associative array
2016 * as the first parameter and leave out the second parameter.
2017 * Flags the properties as 'dirty' so they will be saved to the
2018 * database when save() is called.
2019 * @param string|array $key
2020 * @param string|null $value
2021 */
2022 public function set_expr($key, $value = null) {
2023 return $this->_set_orm_property($key, $value, true);
2024 }
2025
2026 /**
2027 * Set a property on the ORM object.
2028 * @param string|array $key
2029 * @param string|null $value
2030 * @param bool $raw Whether this value should be treated as raw or not
2031 */
2032 protected function _set_orm_property($key, $value = null, $expr = false) {
2033 if (!is_array($key)) {
2034 $key = array($key => $value);
2035 }
2036 foreach ($key as $field => $value) {
2037 $this->_data[$field] = $value;
2038 $this->_dirty_fields[$field] = $value;
2039 if (false === $expr and isset($this->_expr_fields[$field])) {
2040 unset($this->_expr_fields[$field]);
2041 } else if (true === $expr) {
2042 $this->_expr_fields[$field] = true;
2043 }
2044 }
2045 return $this;
2046 }
2047
2048 /**
2049 * Check whether the given field has been changed since this
2050 * object was saved.
2051 */
2052 public function is_dirty($key) {
2053 return array_key_exists($key, $this->_dirty_fields);
2054 }
2055
2056 /**
2057 * Check whether the model was the result of a call to create() or not
2058 * @return bool
2059 */
2060 public function is_new() {
2061 return $this->_is_new;
2062 }
2063
2064 /**
2065 * Save any fields which have been modified on this object
2066 * to the database.
2067 */
2068 public function save() {
2069 $query = array();
2070
2071 // remove any expression fields as they are already baked into the query
2072 $values = array_values(array_diff_key($this->_dirty_fields, $this->_expr_fields));
2073
2074 if (!$this->_is_new) { // UPDATE
2075 // If there are no dirty values, do nothing
2076 if (empty($values) && empty($this->_expr_fields)) {
2077 return true;
2078 }
2079 $query = $this->_build_update();
2080 $id = $this->id(true);
2081 if (is_array($id)) {
2082 $values = array_merge($values, array_values($id));
2083 } else {
2084 $values[] = $id;
2085 }
2086 } else { // INSERT
2087 $query = $this->_build_insert();
2088 }
2089
2090 $success = self::_execute($query, $values, $this->_connection_name);
2091 $caching_auto_clear_enabled = self::$_config[$this->_connection_name]['caching_auto_clear'];
2092 if($caching_auto_clear_enabled){
2093 self::clear_cache($this->_table_name, $this->_connection_name);
2094 }
2095 // If we've just inserted a new record, set the ID of this object
2096 if ($this->_is_new) {
2097 $this->_is_new = false;
2098 if ($this->count_null_id_columns() != 0) {
2099 $db = self::get_db($this->_connection_name);
2100 if($db->getAttribute(PDO::ATTR_DRIVER_NAME) == 'pgsql') {
2101 // it may return several columns if a compound primary
2102 // key is used
2103 $row = self::get_last_statement()->fetch(PDO::FETCH_ASSOC);
2104 foreach($row as $key => $value) {
2105 $this->_data[$key] = $value;
2106 }
2107 } else {
2108 $column = $this->_get_id_column_name();
2109 // if the primary key is compound, assign the last inserted id
2110 // to the first column
2111 if (is_array($column)) {
2112 $column = reset($column);
2113 }
2114 $this->_data[$column] = $db->lastInsertId();
2115 }
2116 }
2117 }
2118
2119 $this->_dirty_fields = $this->_expr_fields = array();
2120 return $success;
2121 }
2122
2123 /**
2124 * Add a WHERE clause for every column that belongs to the primary key
2125 */
2126 public function _add_id_column_conditions(&$query) {
2127 $query[] = "WHERE";
2128 $keys = is_array($this->_get_id_column_name()) ? $this->_get_id_column_name() : array( $this->_get_id_column_name() );
2129 $first = true;
2130 foreach($keys as $key) {
2131 if ($first) {
2132 $first = false;
2133 }
2134 else {
2135 $query[] = "AND";
2136 }
2137 $query[] = $this->_quote_identifier($key);
2138 $query[] = "= ?";
2139 }
2140 }
2141
2142 /**
2143 * Build an UPDATE query
2144 */
2145 protected function _build_update() {
2146 $query = array();
2147 $query[] = "UPDATE {$this->_quote_identifier($this->_table_name)} SET";
2148
2149 $field_list = array();
2150 foreach ($this->_dirty_fields as $key => $value) {
2151 if(!array_key_exists($key, $this->_expr_fields)) {
2152 $value = '?';
2153 }
2154 $field_list[] = "{$this->_quote_identifier($key)} = $value";
2155 }
2156 $query[] = join(", ", $field_list);
2157 $this->_add_id_column_conditions($query);
2158 return join(" ", $query);
2159 }
2160
2161 /**
2162 * Build an INSERT query
2163 */
2164 protected function _build_insert() {
2165 $query[] = "INSERT INTO";
2166 $query[] = $this->_quote_identifier($this->_table_name);
2167 $field_list = array_map(array($this, '_quote_identifier'), array_keys($this->_dirty_fields));
2168 $query[] = "(" . join(", ", $field_list) . ")";
2169 $query[] = "VALUES";
2170
2171 $placeholders = $this->_create_placeholders($this->_dirty_fields);
2172 $query[] = "({$placeholders})";
2173
2174 if (self::get_db($this->_connection_name)->getAttribute(PDO::ATTR_DRIVER_NAME) == 'pgsql') {
2175 $query[] = 'RETURNING ' . $this->_quote_identifier($this->_get_id_column_name());
2176 }
2177
2178 return join(" ", $query);
2179 }
2180
2181 /**
2182 * Delete this record from the database
2183 */
2184 public function delete() {
2185 $query = array(
2186 "DELETE FROM",
2187 $this->_quote_identifier($this->_table_name)
2188 );
2189 $this->_add_id_column_conditions($query);
2190 return self::_execute(join(" ", $query), is_array($this->id(true)) ? array_values($this->id(true)) : array($this->id(true)), $this->_connection_name);
2191 }
2192
2193 /**
2194 * Delete many records from the database
2195 */
2196 public function delete_many() {
2197 // Build and return the full DELETE statement by concatenating
2198 // the results of calling each separate builder method.
2199 $query = $this->_join_if_not_empty(" ", array(
2200 "DELETE FROM",
2201 $this->_quote_identifier($this->_table_name),
2202 $this->_build_where(),
2203 ));
2204
2205 return self::_execute($query, $this->_values, $this->_connection_name);
2206 }
2207
2208 // --------------------- //
2209 // --- ArrayAccess --- //
2210 // --------------------- //
2211
2212 public function offsetExists($key) {
2213 return array_key_exists($key, $this->_data);
2214 }
2215
2216 public function offsetGet($key) {
2217 return $this->get($key);
2218 }
2219
2220 public function offsetSet($key, $value) {
2221 if(is_null($key)) {
2222 throw new InvalidArgumentException('You must specify a key/array index.');
2223 }
2224 $this->set($key, $value);
2225 }
2226
2227 public function offsetUnset($key) {
2228 unset($this->_data[$key]);
2229 unset($this->_dirty_fields[$key]);
2230 }
2231
2232 // --------------------- //
2233 // --- MAGIC METHODS --- //
2234 // --------------------- //
2235 public function __get($key) {
2236 return $this->offsetGet($key);
2237 }
2238
2239 public function __set($key, $value) {
2240 $this->offsetSet($key, $value);
2241 }
2242
2243 public function __unset($key) {
2244 $this->offsetUnset($key);
2245 }
2246
2247
2248 public function __isset($key) {
2249 return $this->offsetExists($key);
2250 }
2251
2252 /**
2253 * Magic method to capture calls to undefined class methods.
2254 * In this case we are attempting to convert camel case formatted
2255 * methods into underscore formatted methods.
2256 *
2257 * This allows us to call ORM methods using camel case and remain
2258 * backwards compatible.
2259 *
2260 * @param string $name
2261 * @param array $arguments
2262 * @return ORM
2263 */
2264 public function __call($name, $arguments)
2265 {
2266 $method = strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $name));
2267
2268 if (method_exists($this, $method)) {
2269 return call_user_func_array(array($this, $method), $arguments);
2270 } else {
2271 throw new IdiormMethodMissingException("Method $name() does not exist in class " . get_class($this));
2272 }
2273 }
2274
2275 /**
2276 * Magic method to capture calls to undefined static class methods.
2277 * In this case we are attempting to convert camel case formatted
2278 * methods into underscore formatted methods.
2279 *
2280 * This allows us to call ORM methods using camel case and remain
2281 * backwards compatible.
2282 *
2283 * @param string $name
2284 * @param array $arguments
2285 * @return ORM
2286 */
2287 public static function __callStatic($name, $arguments)
2288 {
2289 $method = strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $name));
2290
2291 return call_user_func_array(array('ORM', $method), $arguments);
2292 }
2293 }
2294
2295 /**
2296 * A class to handle str_replace operations that involve quoted strings
2297 * @example IdiormString::str_replace_outside_quotes('?', '%s', 'columnA = "Hello?" AND columnB = ?');
2298 * @example IdiormString::value('columnA = "Hello?" AND columnB = ?')->replace_outside_quotes('?', '%s');
2299 * @author Jeff Roberson <ridgerunner@fluxbb.org>
2300 * @author Simon Holywell <treffynnon@php.net>
2301 * @link http://stackoverflow.com/a/13370709/461813 StackOverflow answer
2302 */
2303 class IdiormString {
2304 protected $subject;
2305 protected $search;
2306 protected $replace;
2307
2308 /**
2309 * Get an easy to use instance of the class
2310 * @param string $subject
2311 * @return \self
2312 */
2313 public static function value($subject) {
2314 return new self($subject);
2315 }
2316
2317 /**
2318 * Shortcut method: Replace all occurrences of the search string with the replacement
2319 * string where they appear outside quotes.
2320 * @param string $search
2321 * @param string $replace
2322 * @param string $subject
2323 * @return string
2324 */
2325 public static function str_replace_outside_quotes($search, $replace, $subject) {
2326 return self::value($subject)->replace_outside_quotes($search, $replace);
2327 }
2328
2329 /**
2330 * Set the base string object
2331 * @param string $subject
2332 */
2333 public function __construct($subject) {
2334 $this->subject = (string) $subject;
2335 }
2336
2337 /**
2338 * Replace all occurrences of the search string with the replacement
2339 * string where they appear outside quotes
2340 * @param string $search
2341 * @param string $replace
2342 * @return string
2343 */
2344 public function replace_outside_quotes($search, $replace) {
2345 $this->search = $search;
2346 $this->replace = $replace;
2347 return $this->_str_replace_outside_quotes();
2348 }
2349
2350 /**
2351 * Validate an input string and perform a replace on all ocurrences
2352 * of $this->search with $this->replace
2353 * @author Jeff Roberson <ridgerunner@fluxbb.org>
2354 * @link http://stackoverflow.com/a/13370709/461813 StackOverflow answer
2355 * @return string
2356 */
2357 protected function _str_replace_outside_quotes(){
2358 $re_valid = '/
2359 # Validate string having embedded quoted substrings.
2360 ^ # Anchor to start of string.
2361 (?: # Zero or more string chunks.
2362 "[^"\\\\]*(?:\\\\.[^"\\\\]*)*" # Either a double quoted chunk,
2363 | \'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\' # or a single quoted chunk,
2364 | [^\'"\\\\]+ # or an unquoted chunk (no escapes).
2365 )* # Zero or more string chunks.
2366 \z # Anchor to end of string.
2367 /sx';
2368 if (!preg_match($re_valid, $this->subject)) {
2369 throw new IdiormStringException("Subject string is not valid in the replace_outside_quotes context.");
2370 }
2371 $re_parse = '/
2372 # Match one chunk of a valid string having embedded quoted substrings.
2373 ( # Either $1: Quoted chunk.
2374 "[^"\\\\]*(?:\\\\.[^"\\\\]*)*" # Either a double quoted chunk,
2375 | \'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\' # or a single quoted chunk.
2376 ) # End $1: Quoted chunk.
2377 | ([^\'"\\\\]+) # or $2: an unquoted chunk (no escapes).
2378 /sx';
2379 return preg_replace_callback($re_parse, array($this, '_str_replace_outside_quotes_cb'), $this->subject);
2380 }
2381
2382 /**
2383 * Process each matching chunk from preg_replace_callback replacing
2384 * each occurrence of $this->search with $this->replace
2385 * @author Jeff Roberson <ridgerunner@fluxbb.org>
2386 * @link http://stackoverflow.com/a/13370709/461813 StackOverflow answer
2387 * @param array $matches
2388 * @return string
2389 */
2390 protected function _str_replace_outside_quotes_cb($matches) {
2391 // Return quoted string chunks (in group $1) unaltered.
2392 if ($matches[1]) return $matches[1];
2393 // Process only unquoted chunks (in group $2).
2394 return preg_replace('/'. preg_quote($this->search, '/') .'/',
2395 $this->replace, $matches[2]);
2396 }
2397 }
2398
2399 /**
2400 * A result set class for working with collections of model instances
2401 * @author Simon Holywell <treffynnon@php.net>
2402 * @method null setResults(array $results)
2403 * @method array getResults()
2404 */
2405 class IdiormResultSet implements Countable, IteratorAggregate, ArrayAccess, Serializable {
2406 /**
2407 * The current result set as an array
2408 * @var array
2409 */
2410 protected $_results = array();
2411
2412 /**
2413 * Optionally set the contents of the result set by passing in array
2414 * @param array $results
2415 */
2416 public function __construct(array $results = array()) {
2417 $this->set_results($results);
2418 }
2419
2420 /**
2421 * Set the contents of the result set by passing in array
2422 * @param array $results
2423 */
2424 public function set_results(array $results) {
2425 $this->_results = $results;
2426 }
2427
2428 /**
2429 * Get the current result set as an array
2430 * @return array
2431 */
2432 public function get_results() {
2433 return $this->_results;
2434 }
2435
2436 /**
2437 * Get the current result set as an array
2438 * @return array
2439 */
2440 public function as_array() {
2441 return $this->get_results();
2442 }
2443
2444 /**
2445 * Get the number of records in the result set
2446 * @return int
2447 */
2448 public function count() {
2449 return count($this->_results);
2450 }
2451
2452 /**
2453 * Get an iterator for this object. In this case it supports foreaching
2454 * over the result set.
2455 * @return \ArrayIterator
2456 */
2457 public function getIterator() {
2458 return new ArrayIterator($this->_results);
2459 }
2460
2461 /**
2462 * ArrayAccess
2463 * @param int|string $offset
2464 * @return bool
2465 */
2466 public function offsetExists($offset) {
2467 return isset($this->_results[$offset]);
2468 }
2469
2470 /**
2471 * ArrayAccess
2472 * @param int|string $offset
2473 * @return mixed
2474 */
2475 public function offsetGet($offset) {
2476 return $this->_results[$offset];
2477 }
2478
2479 /**
2480 * ArrayAccess
2481 * @param int|string $offset
2482 * @param mixed $value
2483 */
2484 public function offsetSet($offset, $value) {
2485 $this->_results[$offset] = $value;
2486 }
2487
2488 /**
2489 * ArrayAccess
2490 * @param int|string $offset
2491 */
2492 public function offsetUnset($offset) {
2493 unset($this->_results[$offset]);
2494 }
2495
2496 /**
2497 * Serializable
2498 * @return string
2499 */
2500 public function serialize() {
2501 return serialize($this->_results);
2502 }
2503
2504 /**
2505 * Serializable
2506 * @param string $serialized
2507 * @return array
2508 */
2509 public function unserialize($serialized) {
2510 return unserialize($serialized);
2511 }
2512
2513 /**
2514 * Call a method on all models in a result set. This allows for method
2515 * chaining such as setting a property on all models in a result set or
2516 * any other batch operation across models.
2517 * @example ORM::for_table('Widget')->find_many()->set('field', 'value')->save();
2518 * @param string $method
2519 * @param array $params
2520 * @return \IdiormResultSet
2521 */
2522 public function __call($method, $params = array()) {
2523 foreach($this->_results as $model) {
2524 if (method_exists($model, $method)) {
2525 call_user_func_array(array($model, $method), $params);
2526 } else {
2527 throw new IdiormMethodMissingException("Method $method() does not exist in class " . get_class($this));
2528 }
2529 }
2530 return $this;
2531 }
2532 }
2533
2534 /**
2535 * A placeholder for exceptions eminating from the IdiormString class
2536 */
2537 class IdiormStringException extends Exception {}
2538
2539 class IdiormMethodMissingException extends Exception {}
2540