datasource.php
Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 class DataSource extends Object {
00038
00039
00040
00041
00042
00043
00044 var $connected = false;
00045
00046
00047
00048
00049
00050
00051 var $debug = false;
00052
00053
00054
00055
00056
00057
00058 var $fullDebug = false;
00059
00060
00061
00062
00063
00064
00065 var $error = null;
00066
00067
00068
00069
00070
00071
00072 var $affected = null;
00073
00074
00075
00076
00077
00078
00079 var $numRows = null;
00080
00081
00082
00083
00084
00085
00086 var $took = null;
00087
00088
00089
00090
00091
00092
00093 var $_result = null;
00094
00095
00096
00097
00098
00099
00100 var $_queriesCnt = 0;
00101
00102
00103
00104
00105
00106
00107 var $_queriesTime = null;
00108
00109
00110
00111
00112
00113
00114 var $_queriesLog = array();
00115
00116
00117
00118
00119
00120
00121
00122
00123 var $_queriesLogMax = 200;
00124
00125
00126
00127
00128
00129
00130 var $_queryCache = array();
00131
00132
00133
00134
00135
00136
00137 var $_baseConfig = array();
00138
00139
00140
00141
00142
00143
00144 var $__descriptions = array();
00145
00146
00147
00148
00149
00150
00151 var $_sources = null;
00152
00153
00154
00155
00156
00157
00158 var $connection = null;
00159
00160
00161
00162
00163
00164
00165 var $config = array();
00166
00167
00168
00169
00170
00171
00172 var $configKeyName = null;
00173
00174
00175
00176
00177
00178
00179 var $_transactionStarted = false;
00180
00181
00182
00183
00184
00185 var $cacheSources = true;
00186
00187
00188
00189 function __construct() {
00190 parent::__construct();
00191 if (func_num_args() > 0) {
00192 $this->setConfig(func_get_arg(0));
00193 }
00194 }
00195
00196
00197
00198
00199
00200 function listSources($data = null) {
00201 if ($this->cacheSources === false) {
00202 return null;
00203 }
00204 if ($this->_sources != null) {
00205 return $this->_sources;
00206 }
00207
00208 if (Configure::read() > 0) {
00209 $expires = "+30 seconds";
00210 } else {
00211 $expires = "+999 days";
00212 }
00213
00214 if ($data != null) {
00215 $data = serialize($data);
00216 }
00217 $filename = ConnectionManager::getSourceName($this) . '_' . preg_replace("/[^A-Za-z0-9_-]/", "_", $this->config['database']) . '_list';
00218 $new = cache('models' . DS . $filename, $data, $expires);
00219
00220 if ($new != null) {
00221 $new = unserialize($new);
00222 $this->_sources = $new;
00223 }
00224 return $new;
00225 }
00226
00227
00228
00229
00230
00231 function sources() {
00232 $return = array_map('strtolower', $this->listSources());
00233 return $return;
00234 }
00235
00236
00237
00238
00239
00240
00241 function describe($model) {
00242 if ($this->cacheSources === false) {
00243 return null;
00244 }
00245 if (isset($this->__descriptions[$model->tablePrefix . $model->table])) {
00246 return $this->__descriptions[$model->tablePrefix . $model->table];
00247 }
00248 $cache = $this->__cacheDescription($model->tablePrefix . $model->table);
00249
00250 if ($cache !== null) {
00251 $this->__descriptions[$model->tablePrefix . $model->table] =& $cache;
00252 return $cache;
00253 }
00254 return null;
00255 }
00256
00257
00258
00259
00260
00261
00262 function begin(&$model) {
00263 return true;
00264 }
00265
00266
00267
00268
00269
00270
00271 function commit(&$model) {
00272 return true;
00273 }
00274
00275
00276
00277
00278
00279
00280 function rollback(&$model) {
00281 return true;
00282 }
00283
00284
00285
00286
00287
00288
00289 function column($real) {
00290 return false;
00291 }
00292
00293
00294
00295
00296
00297
00298
00299
00300 function create(&$model, $fields = null, $values = null) {
00301 return false;
00302 }
00303
00304
00305
00306
00307
00308
00309
00310 function read(&$model, $queryData = array()) {
00311 return false;
00312 }
00313
00314
00315
00316
00317
00318
00319
00320
00321 function update(&$model, $fields = null, $values = null) {
00322 return false;
00323 }
00324
00325
00326
00327
00328
00329
00330 function delete(&$model, $id = null) {
00331 if ($id == null) {
00332 $id = $model->id;
00333 }
00334 }
00335
00336
00337
00338
00339
00340
00341 function lastInsertId($source = null) {
00342 return false;
00343 }
00344
00345
00346
00347
00348
00349
00350 function lastNumRows($source = null) {
00351 return false;
00352 }
00353
00354
00355
00356
00357
00358
00359 function lastAffected($source = null) {
00360 return false;
00361 }
00362
00363
00364
00365
00366
00367
00368 function isInterfaceSupported($interface) {
00369 $methods = get_class_methods(get_class($this));
00370 $methods = strtolower(implode('|', $methods));
00371 $methods = explode('|', $methods);
00372 $return = in_array(strtolower($interface), $methods);
00373 return $return;
00374 }
00375
00376
00377
00378
00379
00380
00381 function setConfig($config) {
00382 if (is_array($this->_baseConfig)) {
00383 $this->config = $this->_baseConfig;
00384 foreach ($config as $key => $val) {
00385 $this->config[$key] = $val;
00386 }
00387 }
00388 }
00389
00390
00391
00392
00393
00394
00395
00396 function __cacheDescription($object, $data = null) {
00397 if ($this->cacheSources === false) {
00398 return null;
00399 }
00400 if (Configure::read() > 0) {
00401 $expires = "+15 seconds";
00402 } else {
00403 $expires = "+999 days";
00404 }
00405
00406 if ($data !== null) {
00407 $this->__descriptions[$object] =& $data;
00408 $cache = serialize($data);
00409 } else {
00410 $cache = null;
00411 }
00412 $new = cache('models' . DS . ConnectionManager::getSourceName($this) . '_' . $object, $cache, $expires);
00413
00414 if ($new != null) {
00415 $new = unserialize($new);
00416 }
00417 return $new;
00418 }
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431 function insertQueryData($query, $data, $association, $assocData, &$model, &$linkModel, $stack) {
00432 $keys = array('{$__cakeID__$}', '{$__cakeForeignKey__$}');
00433
00434 foreach ($keys as $key) {
00435 $val = null;
00436
00437 if (strpos($query, $key) !== false) {
00438 switch($key) {
00439 case '{$__cakeID__$}':
00440 if (isset($data[$model->name]) || isset($data[$association])) {
00441 if (isset($data[$model->name][$model->primaryKey])) {
00442 $val = $data[$model->name][$model->primaryKey];
00443 } elseif (isset($data[$association][$model->primaryKey])) {
00444 $val = $data[$association][$model->primaryKey];
00445 }
00446 } else {
00447 $found = false;
00448 foreach (array_reverse($stack) as $assoc) {
00449 if (isset($data[$assoc]) && isset($data[$assoc][$model->primaryKey])) {
00450 $val = $data[$assoc][$model->primaryKey];
00451 $found = true;
00452 break;
00453 }
00454 }
00455 if (!$found) {
00456 $val = '';
00457 }
00458 }
00459 break;
00460 case '{$__cakeForeignKey__$}':
00461 foreach ($model->__associations as $id => $name) {
00462 foreach ($model->$name as $assocName => $assoc) {
00463 if ($assocName === $association) {
00464 if (isset($assoc['foreignKey'])) {
00465 $foreignKey = $assoc['foreignKey'];
00466
00467 if (isset($data[$model->name][$foreignKey])) {
00468 $val = $data[$model->name][$foreignKey];
00469 } elseif (isset($data[$association][$foreignKey])) {
00470 $val = $data[$association][$foreignKey];
00471 } else {
00472 $found = false;
00473 foreach (array_reverse($stack) as $assoc) {
00474 if (isset($data[$assoc]) && isset($data[$assoc][$foreignKey])) {
00475 $val = $data[$assoc][$foreignKey];
00476 $found = true;
00477 break;
00478 }
00479 }
00480 if (!$found) {
00481 $val = '';
00482 }
00483 }
00484 }
00485 break 3;
00486 }
00487 }
00488 }
00489 break;
00490 }
00491 if (empty($val) && $val !== '0') {
00492 return false;
00493 }
00494 $query = r($key, $this->value($val, $model->getColumnType($model->primaryKey)), $query);
00495 }
00496 }
00497 return $query;
00498 }
00499
00500
00501
00502
00503
00504
00505
00506 function resolveKey($model, $key) {
00507 return $model->name . $key;
00508 }
00509
00510
00511
00512
00513 function __destruct() {
00514 if ($this->connected) {
00515 $this->close();
00516 }
00517 }
00518 }
00519 ?>