PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/monica/monica/vendor/zendframework/zendframework/library/Zend/Db/TableGateway/AbstractTableGateway.php

https://bitbucket.org/alexandretaz/maniac_divers
PHP | 488 lines | 246 code | 66 blank | 176 comment | 34 complexity | 6d5fd29afc7f50fc738ab9e7e063ca3b MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Db\TableGateway;
  10. use Zend\Db\Adapter\AdapterInterface;
  11. use Zend\Db\ResultSet\ResultSet;
  12. use Zend\Db\ResultSet\ResultSetInterface;
  13. use Zend\Db\Sql\Delete;
  14. use Zend\Db\Sql\Insert;
  15. use Zend\Db\Sql\Select;
  16. use Zend\Db\Sql\Sql;
  17. use Zend\Db\Sql\TableIdentifier;
  18. use Zend\Db\Sql\Update;
  19. use Zend\Db\Sql\Where;
  20. /**
  21. *
  22. * @property AdapterInterface $adapter
  23. * @property int $lastInsertValue
  24. * @property string $table
  25. */
  26. abstract class AbstractTableGateway implements TableGatewayInterface
  27. {
  28. /**
  29. * @var bool
  30. */
  31. protected $isInitialized = false;
  32. /**
  33. * @var AdapterInterface
  34. */
  35. protected $adapter = null;
  36. /**
  37. * @var string
  38. */
  39. protected $table = null;
  40. /**
  41. * @var array
  42. */
  43. protected $columns = array();
  44. /**
  45. * @var Feature\FeatureSet
  46. */
  47. protected $featureSet = null;
  48. /**
  49. * @var ResultSetInterface
  50. */
  51. protected $resultSetPrototype = null;
  52. /**
  53. * @var Sql
  54. */
  55. protected $sql = null;
  56. /**
  57. *
  58. * @var integer
  59. */
  60. protected $lastInsertValue = null;
  61. /**
  62. * @return bool
  63. */
  64. public function isInitialized()
  65. {
  66. return $this->isInitialized;
  67. }
  68. /**
  69. * Initialize
  70. *
  71. * @throws Exception\RuntimeException
  72. * @return null
  73. */
  74. public function initialize()
  75. {
  76. if ($this->isInitialized) {
  77. return;
  78. }
  79. if (!$this->featureSet instanceof Feature\FeatureSet) {
  80. $this->featureSet = new Feature\FeatureSet;
  81. }
  82. $this->featureSet->setTableGateway($this);
  83. $this->featureSet->apply('preInitialize', array());
  84. if (!$this->adapter instanceof AdapterInterface) {
  85. throw new Exception\RuntimeException('This table does not have an Adapter setup');
  86. }
  87. if (!is_string($this->table) && !$this->table instanceof TableIdentifier) {
  88. throw new Exception\RuntimeException('This table object does not have a valid table set.');
  89. }
  90. if (!$this->resultSetPrototype instanceof ResultSetInterface) {
  91. $this->resultSetPrototype = new ResultSet;
  92. }
  93. if (!$this->sql instanceof Sql) {
  94. $this->sql = new Sql($this->adapter, $this->table);
  95. }
  96. $this->featureSet->apply('postInitialize', array());
  97. $this->isInitialized = true;
  98. }
  99. /**
  100. * Get table name
  101. *
  102. * @return string
  103. */
  104. public function getTable()
  105. {
  106. return $this->table;
  107. }
  108. /**
  109. * Get adapter
  110. *
  111. * @return AdapterInterface
  112. */
  113. public function getAdapter()
  114. {
  115. return $this->adapter;
  116. }
  117. /**
  118. * @return array
  119. */
  120. public function getColumns()
  121. {
  122. return $this->columns;
  123. }
  124. /**
  125. * @return Feature\FeatureSet
  126. */
  127. public function getFeatureSet()
  128. {
  129. return $this->featureSet;
  130. }
  131. /**
  132. * Get select result prototype
  133. *
  134. * @return ResultSet
  135. */
  136. public function getResultSetPrototype()
  137. {
  138. return $this->resultSetPrototype;
  139. }
  140. /**
  141. * @return Sql
  142. */
  143. public function getSql()
  144. {
  145. return $this->sql;
  146. }
  147. /**
  148. * Select
  149. *
  150. * @param Where|\Closure|string|array $where
  151. * @return ResultSet
  152. */
  153. public function select($where = null)
  154. {
  155. if (!$this->isInitialized) {
  156. $this->initialize();
  157. }
  158. $select = $this->sql->select();
  159. if ($where instanceof \Closure) {
  160. $where($select);
  161. } elseif ($where !== null) {
  162. $select->where($where);
  163. }
  164. return $this->selectWith($select);
  165. }
  166. /**
  167. * @param Select $select
  168. * @return null|ResultSetInterface
  169. * @throws \RuntimeException
  170. */
  171. public function selectWith(Select $select)
  172. {
  173. if (!$this->isInitialized) {
  174. $this->initialize();
  175. }
  176. return $this->executeSelect($select);
  177. }
  178. /**
  179. * @param Select $select
  180. * @return ResultSet
  181. * @throws \RuntimeException
  182. */
  183. protected function executeSelect(Select $select)
  184. {
  185. $selectState = $select->getRawState();
  186. if ($selectState['table'] != $this->table) {
  187. throw new \RuntimeException('The table name of the provided select object must match that of the table');
  188. }
  189. if ($selectState['columns'] == array(Select::SQL_STAR)
  190. && $this->columns !== array()) {
  191. $select->columns($this->columns);
  192. }
  193. // apply preSelect features
  194. $this->featureSet->apply('preSelect', array($select));
  195. // prepare and execute
  196. $statement = $this->sql->prepareStatementForSqlObject($select);
  197. $result = $statement->execute();
  198. // build result set
  199. $resultSet = clone $this->resultSetPrototype;
  200. $resultSet->initialize($result);
  201. // apply postSelect features
  202. $this->featureSet->apply('postSelect', array($statement, $result, $resultSet));
  203. return $resultSet;
  204. }
  205. /**
  206. * Insert
  207. *
  208. * @param array $set
  209. * @return int
  210. */
  211. public function insert($set)
  212. {
  213. if (!$this->isInitialized) {
  214. $this->initialize();
  215. }
  216. $insert = $this->sql->insert();
  217. $insert->values($set);
  218. return $this->executeInsert($insert);
  219. }
  220. /**
  221. * @param Insert $insert
  222. * @return mixed
  223. */
  224. public function insertWith(Insert $insert)
  225. {
  226. if (!$this->isInitialized) {
  227. $this->initialize();
  228. }
  229. return $this->executeInsert($insert);
  230. }
  231. /**
  232. * @todo add $columns support
  233. *
  234. * @param Insert $insert
  235. * @return mixed
  236. * @throws Exception\RuntimeException
  237. */
  238. protected function executeInsert(Insert $insert)
  239. {
  240. $insertState = $insert->getRawState();
  241. if ($insertState['table'] != $this->table) {
  242. throw new Exception\RuntimeException('The table name of the provided Insert object must match that of the table');
  243. }
  244. // apply preInsert features
  245. $this->featureSet->apply('preInsert', array($insert));
  246. $statement = $this->sql->prepareStatementForSqlObject($insert);
  247. $result = $statement->execute();
  248. $this->lastInsertValue = $this->adapter->getDriver()->getConnection()->getLastGeneratedValue();
  249. // apply postInsert features
  250. $this->featureSet->apply('postInsert', array($statement, $result));
  251. return $result->getAffectedRows();
  252. }
  253. /**
  254. * Update
  255. *
  256. * @param array $set
  257. * @param string|array|closure $where
  258. * @return int
  259. */
  260. public function update($set, $where = null)
  261. {
  262. if (!$this->isInitialized) {
  263. $this->initialize();
  264. }
  265. $sql = $this->sql;
  266. $update = $sql->update();
  267. $update->set($set);
  268. if ($where !== null) {
  269. $update->where($where);
  270. }
  271. return $this->executeUpdate($update);
  272. }
  273. /**
  274. * @param \Zend\Db\Sql\Update $update
  275. * @return mixed
  276. */
  277. public function updateWith(Update $update)
  278. {
  279. if (!$this->isInitialized) {
  280. $this->initialize();
  281. }
  282. return $this->executeUpdate($update);
  283. }
  284. /**
  285. * @todo add $columns support
  286. *
  287. * @param Update $update
  288. * @return mixed
  289. * @throws Exception\RuntimeException
  290. */
  291. protected function executeUpdate(Update $update)
  292. {
  293. $updateState = $update->getRawState();
  294. if ($updateState['table'] != $this->table) {
  295. throw new Exception\RuntimeException('The table name of the provided Update object must match that of the table');
  296. }
  297. // apply preUpdate features
  298. $this->featureSet->apply('preUpdate', array($update));
  299. $statement = $this->sql->prepareStatementForSqlObject($update);
  300. $result = $statement->execute();
  301. // apply postUpdate features
  302. $this->featureSet->apply('postUpdate', array($statement, $result));
  303. return $result->getAffectedRows();
  304. }
  305. /**
  306. * Delete
  307. *
  308. * @param Where|\Closure|string|array $where
  309. * @return int
  310. */
  311. public function delete($where)
  312. {
  313. if (!$this->isInitialized) {
  314. $this->initialize();
  315. }
  316. $delete = $this->sql->delete();
  317. if ($where instanceof \Closure) {
  318. $where($delete);
  319. } else {
  320. $delete->where($where);
  321. }
  322. return $this->executeDelete($delete);
  323. }
  324. /**
  325. * @param Delete $delete
  326. * @return mixed
  327. */
  328. public function deleteWith(Delete $delete)
  329. {
  330. $this->initialize();
  331. return $this->executeDelete($delete);
  332. }
  333. /**
  334. * @todo add $columns support
  335. *
  336. * @param Delete $delete
  337. * @return mixed
  338. * @throws Exception\RuntimeException
  339. */
  340. protected function executeDelete(Delete $delete)
  341. {
  342. $deleteState = $delete->getRawState();
  343. if ($deleteState['table'] != $this->table) {
  344. throw new Exception\RuntimeException('The table name of the provided Update object must match that of the table');
  345. }
  346. // pre delete update
  347. $this->featureSet->apply('preDelete', array($delete));
  348. $statement = $this->sql->prepareStatementForSqlObject($delete);
  349. $result = $statement->execute();
  350. // apply postDelete features
  351. $this->featureSet->apply('postDelete', array($statement, $result));
  352. return $result->getAffectedRows();
  353. }
  354. /**
  355. * Get last insert value
  356. *
  357. * @return integer
  358. */
  359. public function getLastInsertValue()
  360. {
  361. return $this->lastInsertValue;
  362. }
  363. /**
  364. * __get
  365. *
  366. * @param string $property
  367. * @throws Exception\InvalidArgumentException
  368. * @return mixed
  369. */
  370. public function __get($property)
  371. {
  372. switch (strtolower($property)) {
  373. case 'lastinsertvalue':
  374. return $this->lastInsertValue;
  375. case 'adapter':
  376. return $this->adapter;
  377. case 'table':
  378. return $this->table;
  379. }
  380. if ($this->featureSet->canCallMagicGet($property)) {
  381. return $this->featureSet->callMagicGet($property);
  382. }
  383. throw new Exception\InvalidArgumentException('Invalid magic property access in ' . __CLASS__ . '::__get()');
  384. }
  385. /**
  386. * @param string $property
  387. * @param mixed $value
  388. * @return mixed
  389. * @throws Exception\InvalidArgumentException
  390. */
  391. public function __set($property, $value)
  392. {
  393. if ($this->featureSet->canCallMagicSet($property)) {
  394. return $this->featureSet->callMagicSet($property, $value);
  395. }
  396. throw new Exception\InvalidArgumentException('Invalid magic property access in ' . __CLASS__ . '::__set()');
  397. }
  398. /**
  399. * @param $method
  400. * @param $arguments
  401. * @return mixed
  402. * @throws Exception\InvalidArgumentException
  403. */
  404. public function __call($method, $arguments)
  405. {
  406. if ($this->featureSet->canCallMagicCall($method)) {
  407. return $this->featureSet->callMagicCall($method, $arguments);
  408. }
  409. throw new Exception\InvalidArgumentException('Invalid method (' . $method . ') called, caught by ' . __CLASS__ . '::__call()');
  410. }
  411. /**
  412. * __clone
  413. */
  414. public function __clone()
  415. {
  416. $this->resultSetPrototype = (isset($this->resultSetPrototype)) ? clone $this->resultSetPrototype : null;
  417. $this->sql = clone $this->sql;
  418. if (is_object($this->table)) {
  419. $this->table = clone $this->table;
  420. }
  421. }
  422. }