/
datasource.php
518 lines (506 loc) · 11.8 KB
/
datasource.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
<?php
/* SVN FILE: $Id$ */
/**
* DataSource base class
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.libs.model.datasources
* @since CakePHP(tm) v 0.10.5.1790
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/**
* DataSource base class
*
* Long description for file
*
* @package cake
* @subpackage cake.cake.libs.model.datasources
*/
class DataSource extends Object {
/**
* Are we connected to the DataSource?
*
* @var boolean
* @access public
*/
var $connected = false;
/**
* Print full query debug info?
*
* @var boolean
* @access public
*/
var $fullDebug = false;
/**
* Error description of last query
*
* @var unknown_type
* @access public
*/
var $error = null;
/**
* String to hold how many rows were affected by the last SQL operation.
*
* @var string
* @access public
*/
var $affected = null;
/**
* Number of rows in current resultset
*
* @var int
* @access public
*/
var $numRows = null;
/**
* Time the last query took
*
* @var int
* @access public
*/
var $took = null;
/**
* The starting character that this DataSource uses for quoted identifiers.
*
* @var string
*/
var $startQuote = null;
/**
* The ending character that this DataSource uses for quoted identifiers.
*
* @var string
*/
var $endQuote = null;
/**
* Enter description here...
*
* @var array
* @access protected
*/
var $_result = null;
/**
* Queries count.
*
* @var int
* @access protected
*/
var $_queriesCnt = 0;
/**
* Total duration of all queries.
*
* @var unknown_type
* @access protected
*/
var $_queriesTime = null;
/**
* Log of queries executed by this DataSource
*
* @var unknown_type
* @access protected
*/
var $_queriesLog = array();
/**
* Maximum number of items in query log, to prevent query log taking over
* too much memory on large amounts of queries -- I we've had problems at
* >6000 queries on one system.
*
* @var int Maximum number of queries in the queries log.
* @access protected
*/
var $_queriesLogMax = 200;
/**
* Caches serialzed results of executed queries
*
* @var array Maximum number of queries in the queries log.
* @access protected
*/
var $_queryCache = array();
/**
* The default configuration of a specific DataSource
*
* @var array
* @access protected
*/
var $_baseConfig = array();
/**
* Holds references to descriptions loaded by the DataSource
*
* @var array
* @access private
*/
var $__descriptions = array();
/**
* Holds a list of sources (tables) contained in the DataSource
*
* @var array
* @access protected
*/
var $_sources = null;
/**
* A reference to the physical connection of this DataSource
*
* @var array
* @access public
*/
var $connection = null;
/**
* The DataSource configuration
*
* @var array
* @access public
*/
var $config = array();
/**
* The DataSource configuration key name
*
* @var string
* @access public
*/
var $configKeyName = null;
/**
* Whether or not this DataSource is in the middle of a transaction
*
* @var boolean
* @access protected
*/
var $_transactionStarted = false;
/**
* Whether or not source data like available tables and schema descriptions
* should be cached
*
* @var boolean
*/
var $cacheSources = true;
/**
* Constructor.
*/
function __construct($config = array()) {
parent::__construct();
$this->setConfig($config);
}
/**
* Caches/returns cached results for child instances
*
* @return array
*/
function listSources($data = null) {
if ($this->cacheSources === false) {
return null;
}
if ($this->_sources !== null) {
return $this->_sources;
}
$key = ConnectionManager::getSourceName($this) . '_' . $this->config['database'] . '_list';
$key = preg_replace('/[^A-Za-z0-9_\-.+]/', '_', $key);
$sources = Cache::read($key, '_cake_model_');
if (empty($sources)) {
$sources = $data;
Cache::write($key, $data, '_cake_model_');
}
$this->_sources = $sources;
return $sources;
}
/**
* Convenience method for DboSource::listSources(). Returns source names in lowercase.
*
* @return array
*/
function sources($reset = false) {
if ($reset === true) {
$this->_sources = null;
}
return array_map('strtolower', $this->listSources());
}
/**
* Returns a Model description (metadata) or null if none found.
*
* @param Model $model
* @return mixed
*/
function describe($model) {
if ($this->cacheSources === false) {
return null;
}
$table = $this->fullTableName($model, false);
if (isset($this->__descriptions[$table])) {
return $this->__descriptions[$table];
}
$cache = $this->__cacheDescription($table);
if ($cache !== null) {
$this->__descriptions[$table] =& $cache;
return $cache;
}
return null;
}
/**
* Begin a transaction
*
* @return boolean Returns true if a transaction is not in progress
*/
function begin(&$model) {
return !$this->_transactionStarted;
}
/**
* Commit a transaction
*
* @return boolean Returns true if a transaction is in progress
*/
function commit(&$model) {
return $this->_transactionStarted;
}
/**
* Rollback a transaction
*
* @return boolean Returns true if a transaction is in progress
*/
function rollback(&$model) {
return $this->_transactionStarted;
}
/**
* Converts column types to basic types
*
* @param string $real Real column type (i.e. "varchar(255)")
* @return string Abstract column type (i.e. "string")
*/
function column($real) {
return false;
}
/**
* Used to create new records. The "C" CRUD.
*
* To-be-overridden in subclasses.
*
* @param Model $model The Model to be created.
* @param array $fields An Array of fields to be saved.
* @param array $values An Array of values to save.
* @return boolean success
*/
function create(&$model, $fields = null, $values = null) {
return false;
}
/**
* Used to read records from the Datasource. The "R" in CRUD
*
* To-be-overridden in subclasses.
*
* @param Model $model The model being read.
* @param array $queryData An array of query data used to find the data you want
* @return mixed
*/
function read(&$model, $queryData = array()) {
return false;
}
/**
* Update a record(s) in the datasource.
*
* To-be-overridden in subclasses.
*
* @param Model $model Instance of the model class being updated
* @param array $fields Array of fields to be updated
* @param array $values Array of values to be update $fields to.
* @return boolean Success
*/
function update(&$model, $fields = null, $values = null) {
return false;
}
/**
* Delete a record(s) in the datasource.
*
* To-be-overridden in subclasses.
*
* @param Model $model The model class having record(s) deleted
* @param mixed $id Primary key of the model
*/
function delete(&$model, $id = null) {
if ($id == null) {
$id = $model->id;
}
}
/**
* Returns the ID generated from the previous INSERT operation.
*
* @param unknown_type $source
* @return in
*/
function lastInsertId($source = null) {
return false;
}
/**
* Returns the ID generated from the previous INSERT operation.
*
* @param unknown_type $source
* @return in
*/
function lastNumRows($source = null) {
return false;
}
/**
* Returns the ID generated from the previous INSERT operation.
*
* @param unknown_type $source
* @return in
*/
function lastAffected($source = null) {
return false;
}
/**
* Returns true if the DataSource supports the given interface (method)
*
* @param string $interface The name of the interface (method)
* @return boolean True on success
*/
function isInterfaceSupported($interface) {
$methods = get_class_methods(get_class($this));
$methods = strtolower(implode('|', $methods));
$methods = explode('|', $methods);
$return = in_array(strtolower($interface), $methods);
return $return;
}
/**
* Sets the configuration for the DataSource
*
* @param array $config The configuration array
* @return void
*/
function setConfig($config = array()) {
$this->config = array_merge($this->_baseConfig, $this->config, $config);
}
/**
* Cache the DataSource description
*
* @param string $object The name of the object (model) to cache
* @param mixed $data The description of the model, usually a string or array
*/
function __cacheDescription($object, $data = null) {
if ($this->cacheSources === false) {
return null;
}
if ($data !== null) {
$this->__descriptions[$object] =& $data;
}
$key = ConnectionManager::getSourceName($this) . '_' . $object;
$cache = Cache::read($key, '_cake_model_');
if (empty($cache)) {
$cache = $data;
Cache::write($key, $cache, '_cake_model_');
}
return $cache;
}
/**
* Enter description here...
*
* @param unknown_type $query
* @param unknown_type $data
* @param unknown_type $association
* @param unknown_type $assocData
* @param Model $model
* @param Model $linkModel
* @param array $stack
* @return unknown
*/
function insertQueryData($query, $data, $association, $assocData, &$model, &$linkModel, $stack) {
$keys = array('{$__cakeID__$}', '{$__cakeForeignKey__$}');
foreach ($keys as $key) {
$val = null;
if (strpos($query, $key) !== false) {
switch ($key) {
case '{$__cakeID__$}':
if (isset($data[$model->alias]) || isset($data[$association])) {
if (isset($data[$model->alias][$model->primaryKey])) {
$val = $data[$model->alias][$model->primaryKey];
} elseif (isset($data[$association][$model->primaryKey])) {
$val = $data[$association][$model->primaryKey];
}
} else {
$found = false;
foreach (array_reverse($stack) as $assoc) {
if (isset($data[$assoc]) && isset($data[$assoc][$model->primaryKey])) {
$val = $data[$assoc][$model->primaryKey];
$found = true;
break;
}
}
if (!$found) {
$val = '';
}
}
break;
case '{$__cakeForeignKey__$}':
foreach ($model->__associations as $id => $name) {
foreach ($model->$name as $assocName => $assoc) {
if ($assocName === $association) {
if (isset($assoc['foreignKey'])) {
$foreignKey = $assoc['foreignKey'];
if (isset($data[$model->alias][$foreignKey])) {
$val = $data[$model->alias][$foreignKey];
} elseif (isset($data[$association][$foreignKey])) {
$val = $data[$association][$foreignKey];
} else {
$found = false;
foreach (array_reverse($stack) as $assoc) {
if (isset($data[$assoc]) && isset($data[$assoc][$foreignKey])) {
$val = $data[$assoc][$foreignKey];
$found = true;
break;
}
}
if (!$found) {
$val = '';
}
}
}
break 3;
}
}
}
break;
}
if (empty($val) && $val !== '0') {
return false;
}
$query = str_replace($key, $this->value($val, $model->getColumnType($model->primaryKey)), $query);
}
}
return $query;
}
/**
* To-be-overridden in subclasses.
*
* @param unknown_type $model
* @param unknown_type $key
* @return unknown
*/
function resolveKey($model, $key) {
return $model->alias . $key;
}
/**
* Closes the current datasource.
*
*/
function __destruct() {
if ($this->_transactionStarted) {
$null = null;
$this->rollback($null);
}
if ($this->connected) {
$this->close();
}
}
}
?>