PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/Cake/Model/Datasource/DataSource.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 421 lines | 183 code | 41 blank | 197 comment | 31 complexity | 023204fe4261907d0394276ca73e9048 MD5 | raw file
  1. <?php
  2. /**
  3. * DataSource base class
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.Model.Datasource
  16. * @since CakePHP(tm) v 0.10.5.1790
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. /**
  20. * DataSource base class
  21. *
  22. * @package Cake.Model.Datasource
  23. */
  24. class DataSource extends Object {
  25. /**
  26. * Are we connected to the DataSource?
  27. *
  28. * @var boolean
  29. */
  30. public $connected = false;
  31. /**
  32. * The default configuration of a specific DataSource
  33. *
  34. * @var array
  35. */
  36. protected $_baseConfig = array();
  37. /**
  38. * Holds references to descriptions loaded by the DataSource
  39. *
  40. * @var array
  41. */
  42. protected $_descriptions = array();
  43. /**
  44. * Holds a list of sources (tables) contained in the DataSource
  45. *
  46. * @var array
  47. */
  48. protected $_sources = null;
  49. /**
  50. * The DataSource configuration
  51. *
  52. * @var array
  53. */
  54. public $config = array();
  55. /**
  56. * Whether or not this DataSource is in the middle of a transaction
  57. *
  58. * @var boolean
  59. */
  60. protected $_transactionStarted = false;
  61. /**
  62. * Whether or not source data like available tables and schema descriptions
  63. * should be cached
  64. *
  65. * @var boolean
  66. */
  67. public $cacheSources = true;
  68. /**
  69. * Constructor.
  70. *
  71. * @param array $config Array of configuration information for the datasource.
  72. */
  73. public function __construct($config = array()) {
  74. parent::__construct();
  75. $this->setConfig($config);
  76. }
  77. /**
  78. * Caches/returns cached results for child instances
  79. *
  80. * @param mixed $data
  81. * @return array Array of sources available in this datasource.
  82. */
  83. public function listSources($data = null) {
  84. if ($this->cacheSources === false) {
  85. return null;
  86. }
  87. if ($this->_sources !== null) {
  88. return $this->_sources;
  89. }
  90. $key = ConnectionManager::getSourceName($this) . '_' . $this->config['database'] . '_list';
  91. $key = preg_replace('/[^A-Za-z0-9_\-.+]/', '_', $key);
  92. $sources = Cache::read($key, '_cake_model_');
  93. if (empty($sources)) {
  94. $sources = $data;
  95. Cache::write($key, $data, '_cake_model_');
  96. }
  97. return $this->_sources = $sources;
  98. }
  99. /**
  100. * Returns a Model description (metadata) or null if none found.
  101. *
  102. * @param Model|string $model
  103. * @return array Array of Metadata for the $model
  104. */
  105. public function describe($model) {
  106. if ($this->cacheSources === false) {
  107. return null;
  108. }
  109. if (is_string($model)) {
  110. $table = $model;
  111. } else {
  112. $table = $model->tablePrefix . $model->table;
  113. }
  114. if (isset($this->_descriptions[$table])) {
  115. return $this->_descriptions[$table];
  116. }
  117. $cache = $this->_cacheDescription($table);
  118. if ($cache !== null) {
  119. $this->_descriptions[$table] =& $cache;
  120. return $cache;
  121. }
  122. return null;
  123. }
  124. /**
  125. * Begin a transaction
  126. *
  127. * @return boolean Returns true if a transaction is not in progress
  128. */
  129. public function begin() {
  130. return !$this->_transactionStarted;
  131. }
  132. /**
  133. * Commit a transaction
  134. *
  135. * @return boolean Returns true if a transaction is in progress
  136. */
  137. public function commit() {
  138. return $this->_transactionStarted;
  139. }
  140. /**
  141. * Rollback a transaction
  142. *
  143. * @return boolean Returns true if a transaction is in progress
  144. */
  145. public function rollback() {
  146. return $this->_transactionStarted;
  147. }
  148. /**
  149. * Converts column types to basic types
  150. *
  151. * @param string $real Real column type (i.e. "varchar(255)")
  152. * @return string Abstract column type (i.e. "string")
  153. */
  154. public function column($real) {
  155. return false;
  156. }
  157. /**
  158. * Used to create new records. The "C" CRUD.
  159. *
  160. * To-be-overridden in subclasses.
  161. *
  162. * @param Model $model The Model to be created.
  163. * @param array $fields An Array of fields to be saved.
  164. * @param array $values An Array of values to save.
  165. * @return boolean success
  166. */
  167. public function create(Model $model, $fields = null, $values = null) {
  168. return false;
  169. }
  170. /**
  171. * Used to read records from the Datasource. The "R" in CRUD
  172. *
  173. * To-be-overridden in subclasses.
  174. *
  175. * @param Model $model The model being read.
  176. * @param array $queryData An array of query data used to find the data you want
  177. * @return mixed
  178. */
  179. public function read(Model $model, $queryData = array()) {
  180. return false;
  181. }
  182. /**
  183. * Update a record(s) in the datasource.
  184. *
  185. * To-be-overridden in subclasses.
  186. *
  187. * @param Model $model Instance of the model class being updated
  188. * @param array $fields Array of fields to be updated
  189. * @param array $values Array of values to be update $fields to.
  190. * @return boolean Success
  191. */
  192. public function update(Model $model, $fields = null, $values = null) {
  193. return false;
  194. }
  195. /**
  196. * Delete a record(s) in the datasource.
  197. *
  198. * To-be-overridden in subclasses.
  199. *
  200. * @param Model $model The model class having record(s) deleted
  201. * @param mixed $conditions The conditions to use for deleting.
  202. * @return void
  203. */
  204. public function delete(Model $model, $id = null) {
  205. return false;
  206. }
  207. /**
  208. * Returns the ID generated from the previous INSERT operation.
  209. *
  210. * @param mixed $source
  211. * @return mixed Last ID key generated in previous INSERT
  212. */
  213. public function lastInsertId($source = null) {
  214. return false;
  215. }
  216. /**
  217. * Returns the number of rows returned by last operation.
  218. *
  219. * @param mixed $source
  220. * @return integer Number of rows returned by last operation
  221. */
  222. public function lastNumRows($source = null) {
  223. return false;
  224. }
  225. /**
  226. * Returns the number of rows affected by last query.
  227. *
  228. * @param mixed $source
  229. * @return integer Number of rows affected by last query.
  230. */
  231. public function lastAffected($source = null) {
  232. return false;
  233. }
  234. /**
  235. * Check whether the conditions for the Datasource being available
  236. * are satisfied. Often used from connect() to check for support
  237. * before establishing a connection.
  238. *
  239. * @return boolean Whether or not the Datasources conditions for use are met.
  240. */
  241. public function enabled() {
  242. return true;
  243. }
  244. /**
  245. * Sets the configuration for the DataSource.
  246. * Merges the $config information with the _baseConfig and the existing $config property.
  247. *
  248. * @param array $config The configuration array
  249. * @return void
  250. */
  251. public function setConfig($config = array()) {
  252. $this->config = array_merge($this->_baseConfig, $this->config, $config);
  253. }
  254. /**
  255. * Cache the DataSource description
  256. *
  257. * @param string $object The name of the object (model) to cache
  258. * @param mixed $data The description of the model, usually a string or array
  259. * @return mixed
  260. */
  261. protected function _cacheDescription($object, $data = null) {
  262. if ($this->cacheSources === false) {
  263. return null;
  264. }
  265. if ($data !== null) {
  266. $this->_descriptions[$object] =& $data;
  267. }
  268. $key = ConnectionManager::getSourceName($this) . '_' . $object;
  269. $cache = Cache::read($key, '_cake_model_');
  270. if (empty($cache)) {
  271. $cache = $data;
  272. Cache::write($key, $cache, '_cake_model_');
  273. }
  274. return $cache;
  275. }
  276. /**
  277. * Replaces `{$__cakeID__$}` and `{$__cakeForeignKey__$}` placeholders in query data.
  278. *
  279. * @param string $query Query string needing replacements done.
  280. * @param array $data Array of data with values that will be inserted in placeholders.
  281. * @param string $association Name of association model being replaced
  282. * @param array $assocData
  283. * @param Model $model Instance of the model to replace $__cakeID__$
  284. * @param Model $linkModel Instance of model to replace $__cakeForeignKey__$
  285. * @param array $stack
  286. * @return string String of query data with placeholders replaced.
  287. * @todo Remove and refactor $assocData, ensure uses of the method have the param removed too.
  288. */
  289. public function insertQueryData($query, $data, $association, $assocData, Model $model, Model $linkModel, $stack) {
  290. $keys = array('{$__cakeID__$}', '{$__cakeForeignKey__$}');
  291. foreach ($keys as $key) {
  292. $val = null;
  293. $type = null;
  294. if (strpos($query, $key) !== false) {
  295. switch ($key) {
  296. case '{$__cakeID__$}':
  297. if (isset($data[$model->alias]) || isset($data[$association])) {
  298. if (isset($data[$model->alias][$model->primaryKey])) {
  299. $val = $data[$model->alias][$model->primaryKey];
  300. } elseif (isset($data[$association][$model->primaryKey])) {
  301. $val = $data[$association][$model->primaryKey];
  302. }
  303. } else {
  304. $found = false;
  305. foreach (array_reverse($stack) as $assoc) {
  306. if (isset($data[$assoc]) && isset($data[$assoc][$model->primaryKey])) {
  307. $val = $data[$assoc][$model->primaryKey];
  308. $found = true;
  309. break;
  310. }
  311. }
  312. if (!$found) {
  313. $val = '';
  314. }
  315. }
  316. $type = $model->getColumnType($model->primaryKey);
  317. break;
  318. case '{$__cakeForeignKey__$}':
  319. foreach ($model->associations() as $id => $name) {
  320. foreach ($model->$name as $assocName => $assoc) {
  321. if ($assocName === $association) {
  322. if (isset($assoc['foreignKey'])) {
  323. $foreignKey = $assoc['foreignKey'];
  324. $assocModel = $model->$assocName;
  325. $type = $assocModel->getColumnType($assocModel->primaryKey);
  326. if (isset($data[$model->alias][$foreignKey])) {
  327. $val = $data[$model->alias][$foreignKey];
  328. } elseif (isset($data[$association][$foreignKey])) {
  329. $val = $data[$association][$foreignKey];
  330. } else {
  331. $found = false;
  332. foreach (array_reverse($stack) as $assoc) {
  333. if (isset($data[$assoc]) && isset($data[$assoc][$foreignKey])) {
  334. $val = $data[$assoc][$foreignKey];
  335. $found = true;
  336. break;
  337. }
  338. }
  339. if (!$found) {
  340. $val = '';
  341. }
  342. }
  343. }
  344. break 3;
  345. }
  346. }
  347. }
  348. break;
  349. }
  350. if (empty($val) && $val !== '0') {
  351. return false;
  352. }
  353. $query = str_replace($key, $this->value($val, $type), $query);
  354. }
  355. }
  356. return $query;
  357. }
  358. /**
  359. * To-be-overridden in subclasses.
  360. *
  361. * @param Model $model Model instance
  362. * @param string $key Key name to make
  363. * @return string Key name for model.
  364. */
  365. public function resolveKey(Model $model, $key) {
  366. return $model->alias . $key;
  367. }
  368. /**
  369. * Closes the current datasource.
  370. *
  371. */
  372. public function __destruct() {
  373. if ($this->_transactionStarted) {
  374. $this->rollback();
  375. }
  376. if ($this->connected) {
  377. $this->close();
  378. }
  379. }
  380. }