[ Index ] |
|
Code source de CakePHP 1.1.13.4450 |
1 <?php 2 /* SVN FILE: $Id: datasource.php 4409 2007-02-02 13:20:59Z phpnut $ */ 3 /** 4 * DataSource base class 5 * 6 * Long description for file 7 * 8 * PHP versions 4 and 5 9 * 10 * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/> 11 * Copyright 2005-2007, Cake Software Foundation, Inc. 12 * 1785 E. Sahara Avenue, Suite 490-204 13 * Las Vegas, Nevada 89104 14 * 15 * Licensed under The MIT License 16 * Redistributions of files must retain the above copyright notice. 17 * 18 * @filesource 19 * @copyright Copyright 2005-2007, Cake Software Foundation, Inc. 20 * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project 21 * @package cake 22 * @subpackage cake.cake.libs.model.datasources 23 * @since CakePHP(tm) v 0.10.5.1790 24 * @version $Revision: 4409 $ 25 * @modifiedby $LastChangedBy: phpnut $ 26 * @lastmodified $Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $ 27 * @license http://www.opensource.org/licenses/mit-license.php The MIT License 28 */ 29 /** 30 * DataSource base class 31 * 32 * Long description for file 33 * 34 * @package cake 35 * @subpackage cake.cake.libs.model.datasources 36 */ 37 class DataSource extends Object { 38 /** 39 * Are we connected to the DataSource? 40 * 41 * @var boolean 42 * @access public 43 */ 44 var $connected = false; 45 /** 46 * Print debug info? 47 * 48 * @var boolean 49 * @access public 50 */ 51 var $debug = false; 52 /** 53 * Print full query debug info? 54 * 55 * @var boolean 56 * @access public 57 */ 58 var $fullDebug = false; 59 /** 60 * Error description of last query 61 * 62 * @var unknown_type 63 * @access public 64 */ 65 var $error = null; 66 /** 67 * String to hold how many rows were affected by the last SQL operation. 68 * 69 * @var string 70 * @access public 71 */ 72 var $affected = null; 73 /** 74 * Number of rows in current resultset 75 * 76 * @var int 77 * @access public 78 */ 79 var $numRows = null; 80 /** 81 * Time the last query took 82 * 83 * @var int 84 * @access public 85 */ 86 var $took = null; 87 /** 88 * Enter description here... 89 * 90 * @var array 91 * @access private 92 */ 93 var $_result = null; 94 /** 95 * Queries count. 96 * 97 * @var int 98 * @access private 99 */ 100 var $_queriesCnt = 0; 101 /** 102 * Total duration of all queries. 103 * 104 * @var unknown_type 105 * @access private 106 */ 107 var $_queriesTime = null; 108 /** 109 * Log of queries executed by this DataSource 110 * 111 * @var unknown_type 112 * @access private 113 */ 114 var $_queriesLog = array(); 115 /** 116 * Maximum number of items in query log, to prevent query log taking over 117 * too much memory on large amounts of queries -- I we've had problems at 118 * >6000 queries on one system. 119 * 120 * @var int Maximum number of queries in the queries log. 121 * @access private 122 */ 123 var $_queriesLogMax = 200; 124 /** 125 * Caches serialzed results of executed queries 126 * 127 * @var array Maximum number of queries in the queries log. 128 * @access private 129 */ 130 var $_queryCache = array(); 131 /** 132 * The default configuration of a specific DataSource 133 * 134 * @var array 135 * @access public 136 */ 137 var $_baseConfig = array(); 138 /** 139 * Holds references to descriptions loaded by the DataSource 140 * 141 * @var array 142 * @access private 143 */ 144 var $__descriptions = array(); 145 /** 146 * Holds a list of sources (tables) contained in the DataSource 147 * 148 * @var array 149 * @access private 150 */ 151 var $__sources = null; 152 /** 153 * A reference to the physical connection of this DataSource 154 * 155 * @var array 156 * @access public 157 */ 158 var $connection = null; 159 /** 160 * The DataSource configuration 161 * 162 * @var array 163 * @access public 164 */ 165 var $config = array(); 166 /** 167 * The DataSource configuration key name 168 * 169 * @var string 170 * @access public 171 */ 172 var $configKeyName = null; 173 /** 174 * Whether or not this DataSource is in the middle of a transaction 175 * 176 * @var boolean 177 * @access public 178 */ 179 var $__transactionStarted = false; 180 /** 181 * Constructor. 182 */ 183 function __construct() { 184 parent::__construct(); 185 if (func_num_args() > 0) { 186 $this->setConfig(func_get_arg(0)); 187 } 188 } 189 /** 190 * Returns true if the DataSource supports the given interface (method) 191 * 192 * @param string $interface The name of the interface (method) 193 * @return boolean True on success 194 */ 195 function isInterfaceSupported($interface) { 196 $methods = get_class_methods(get_class($this)); 197 $methods = strtolower(implode('|', $methods)); 198 $methods = explode('|', $methods); 199 $return = in_array(strtolower($interface), $methods); 200 return $return; 201 } 202 /** 203 * Sets the configuration for the DataSource 204 * 205 * @param array $config The configuration array 206 * @return void 207 */ 208 function setConfig($config) { 209 if (is_array($this->_baseConfig)) { 210 $this->config = $this->_baseConfig; 211 foreach($config as $key => $val) { 212 $this->config[$key] = $val; 213 } 214 } 215 } 216 /** 217 * Cache the DataSource description 218 * 219 * @param string $object The name of the object (model) to cache 220 * @param mixed $data The description of the model, usually a string or array 221 * @return void 222 */ 223 function __cacheDescription($object, $data = null) { 224 if (Configure::read() > 0) { 225 $expires = "+15 seconds"; 226 } else { 227 $expires = "+999 days"; 228 } 229 230 if ($data !== null) { 231 $this->__descriptions[$object] =& $data; 232 $cache = serialize($data); 233 } else { 234 $cache = null; 235 } 236 $new = cache('models' . DS . ConnectionManager::getSourceName($this) . '_' . $object, $cache, $expires); 237 238 if ($new != null) { 239 $new = unserialize($new); 240 } 241 return $new; 242 } 243 /** 244 * To-be-overridden in subclasses. 245 * 246 * @return string 247 */ 248 function conditions($conditions) { 249 return $conditions; 250 } 251 /** 252 * To-be-overridden in subclasses. 253 * 254 * @param unknown_type $name 255 * @return unknown 256 */ 257 function name($name) { 258 return $name; 259 } 260 /** 261 * To-be-overridden in subclasses. 262 * 263 * @param unknown_type $value 264 * @return unknown 265 */ 266 function value($value) { 267 return $value; 268 } 269 /** 270 * Returns a Model description (metadata) or null if none found. 271 * 272 * @param Model $model 273 * @return mixed 274 */ 275 function describe($model) { 276 if (isset($this->__descriptions[$model->table])) { 277 return $this->__descriptions[$model->table]; 278 } 279 $cache = $this->__cacheDescription($model->tablePrefix.$model->table); 280 281 if ($cache !== null) { 282 $this->__descriptions[$model->table] =& $cache; 283 return $cache; 284 } 285 return null; 286 } 287 /** 288 * To-be-overridden in subclasses. 289 * 290 * @param unknown_type $model 291 * @param unknown_type $fields 292 * @param unknown_type $values 293 * @return unknown 294 */ 295 function create(&$model, $fields = null, $values = null) { 296 return false; 297 } 298 /** 299 * To-be-overridden in subclasses. 300 * 301 * @param unknown_type $model 302 * @param unknown_type $queryData 303 * @return unknown 304 */ 305 function read(&$model, $queryData = array()) { 306 return false; 307 } 308 /** 309 * To-be-overridden in subclasses. 310 * 311 * @param unknown_type $model 312 * @param unknown_type $fields 313 * @param unknown_type $values 314 * @return unknown 315 */ 316 function update(&$model, $fields = null, $values = null) { 317 return false; 318 } 319 /** 320 * To-be-overridden in subclasses. 321 * 322 * @param unknown_type $model 323 * @param unknown_type $id 324 */ 325 function delete(&$model, $id = null) { 326 if ($id == null) { 327 $id = $model->id; 328 } 329 } 330 /** 331 * To-be-overridden in subclasses. 332 * 333 * @param mixed $fields 334 * @return mixed 335 */ 336 function fields($fields) { 337 return $fields; 338 } 339 /** 340 * To-be-overridden in subclasses. 341 * 342 * @param Model $model 343 * @param unknown_type $fields 344 * @return unknown 345 */ 346 function getColumnType(&$model, $fields) { 347 return false; 348 } 349 /** 350 * Enter description here... 351 * 352 * @param unknown_type $query 353 * @param unknown_type $data 354 * @param unknown_type $association 355 * @param unknown_type $assocData 356 * @param Model $model 357 * @param Model $linkModel 358 * @param array $stack 359 * @return unknown 360 */ 361 function insertQueryData($query, $data, $association, $assocData, &$model, &$linkModel, $stack) { 362 $keys = array('{$__cakeID__$}', '{$__cakeForeignKey__$}'); 363 364 foreach($keys as $key) { 365 $val = null; 366 367 if (strpos($query, $key) !== false) { 368 switch($key) { 369 case '{$__cakeID__$}': 370 if (isset($data[$model->name]) || isset($data[$association])) { 371 if (isset($data[$model->name][$model->primaryKey])) { 372 $val = $data[$model->name][$model->primaryKey]; 373 } elseif (isset($data[$association][$model->primaryKey])) { 374 $val = $data[$association][$model->primaryKey]; 375 } 376 } else { 377 $found = false; 378 foreach (array_reverse($stack) as $assoc) { 379 if (isset($data[$assoc]) && isset($data[$assoc][$model->primaryKey])) { 380 $val = $data[$assoc][$model->primaryKey]; 381 $found = true; 382 break; 383 } 384 } 385 if (!$found) { 386 $val = ''; 387 } 388 } 389 break; 390 case '{$__cakeForeignKey__$}': 391 foreach($model->__associations as $id => $name) { 392 foreach($model->$name as $assocName => $assoc) { 393 if ($assocName === $association) { 394 if (isset($assoc['foreignKey'])) { 395 $foreignKey = $assoc['foreignKey']; 396 397 if (isset($data[$model->name][$foreignKey])) { 398 $val = $data[$model->name][$foreignKey]; 399 } elseif (isset($data[$association][$foreignKey])) { 400 $val = $data[$association][$foreignKey]; 401 } else { 402 $found = false; 403 foreach (array_reverse($stack) as $assoc) { 404 if (isset($data[$assoc]) && isset($data[$assoc][$foreignKey])) { 405 $val = $data[$assoc][$foreignKey]; 406 $found = true; 407 break; 408 } 409 } 410 if (!$found) { 411 $val = ''; 412 } 413 } 414 } 415 break 3; 416 } 417 } 418 } 419 break; 420 } 421 if(empty($val) && $val !== 0) { 422 return false; 423 } 424 $query = r($key, $this->value($val, $model->getColumnType($model->primaryKey)), $query); 425 } 426 } 427 return $query; 428 } 429 /** 430 * To-be-overridden in subclasses. 431 * 432 * @param unknown_type $model 433 * @param unknown_type $key 434 * @return unknown 435 */ 436 function resolveKey($model, $key) { 437 return $model->name . $key; 438 } 439 /** 440 * Gets a value from an array or object. 441 * The special {n}, as seen in the Model::generateList method, is taken care of here. 442 * 443 * @param array $data 444 * @param mixed $path As an array, or as a dot-separated string. 445 * @return array 446 */ 447 function getFieldValue($data, $path) { 448 if (!is_array($path)) { 449 $path = explode('.', $path); 450 } 451 $tmp = array(); 452 453 foreach($path as $i => $key) { 454 if (intval($key) > 0 || $key == '0') { 455 if (isset($data[intval($key)])) { 456 $data = $data[intval($key)]; 457 } else { 458 return null; 459 } 460 } elseif ($key == '{n}') { 461 foreach($data as $j => $val) { 462 $tmp[] = DataSource::getFieldValue($val, array_slice($path, $i + 1)); 463 } 464 return $tmp; 465 } else { 466 if (isset($data[$key])) { 467 $data = $data[$key]; 468 } else { 469 return null; 470 } 471 } 472 } 473 return $data; 474 } 475 /** 476 * Closes the current datasource. 477 * 478 */ 479 function __destruct() { 480 if ($this->connected) { 481 $this->close(); 482 } 483 } 484 } 485 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 19:27:47 2007 | par Balluche grâce à PHPXref 0.7 |