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