PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zendframework/library/Zend/Db/Adapter/Driver/Pdo/Pdo.php

https://bitbucket.org/zbahij/eprojets_app
PHP | 309 lines | 175 code | 31 blank | 103 comment | 32 complexity | bc826092d55e834cf4c79edd1f341e32 MD5 | raw file
  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\Adapter\Driver\Pdo;
  10. use PDOStatement;
  11. use Zend\Db\Adapter\Driver\DriverInterface;
  12. use Zend\Db\Adapter\Driver\Feature\AbstractFeature;
  13. use Zend\Db\Adapter\Driver\Feature\DriverFeatureInterface;
  14. use Zend\Db\Adapter\Exception;
  15. use Zend\Db\Adapter\Profiler;
  16. class Pdo implements DriverInterface, DriverFeatureInterface, Profiler\ProfilerAwareInterface
  17. {
  18. /**
  19. * @const
  20. */
  21. const FEATURES_DEFAULT = 'default';
  22. /**
  23. * @var Connection
  24. */
  25. protected $connection = null;
  26. /**
  27. * @var Statement
  28. */
  29. protected $statementPrototype = null;
  30. /**
  31. * @var Result
  32. */
  33. protected $resultPrototype = null;
  34. /**
  35. * @var array
  36. */
  37. protected $features = array();
  38. /**
  39. * @param array|Connection|\PDO $connection
  40. * @param null|Statement $statementPrototype
  41. * @param null|Result $resultPrototype
  42. * @param string $features
  43. */
  44. public function __construct($connection, Statement $statementPrototype = null, Result $resultPrototype = null, $features = self::FEATURES_DEFAULT)
  45. {
  46. if (!$connection instanceof Connection) {
  47. $connection = new Connection($connection);
  48. }
  49. $this->registerConnection($connection);
  50. $this->registerStatementPrototype(($statementPrototype) ?: new Statement());
  51. $this->registerResultPrototype(($resultPrototype) ?: new Result());
  52. if (is_array($features)) {
  53. foreach ($features as $name => $feature) {
  54. $this->addFeature($name, $feature);
  55. }
  56. } elseif ($features instanceof AbstractFeature) {
  57. $this->addFeature($features->getName(), $features);
  58. } elseif ($features === self::FEATURES_DEFAULT) {
  59. $this->setupDefaultFeatures();
  60. }
  61. }
  62. /**
  63. * @param Profiler\ProfilerInterface $profiler
  64. * @return Pdo
  65. */
  66. public function setProfiler(Profiler\ProfilerInterface $profiler)
  67. {
  68. $this->profiler = $profiler;
  69. if ($this->connection instanceof Profiler\ProfilerAwareInterface) {
  70. $this->connection->setProfiler($profiler);
  71. }
  72. if ($this->statementPrototype instanceof Profiler\ProfilerAwareInterface) {
  73. $this->statementPrototype->setProfiler($profiler);
  74. }
  75. return $this;
  76. }
  77. /**
  78. * @return null|Profiler\ProfilerInterface
  79. */
  80. public function getProfiler()
  81. {
  82. return $this->profiler;
  83. }
  84. /**
  85. * Register connection
  86. *
  87. * @param Connection $connection
  88. * @return Pdo
  89. */
  90. public function registerConnection(Connection $connection)
  91. {
  92. $this->connection = $connection;
  93. $this->connection->setDriver($this);
  94. return $this;
  95. }
  96. /**
  97. * Register statement prototype
  98. *
  99. * @param Statement $statementPrototype
  100. */
  101. public function registerStatementPrototype(Statement $statementPrototype)
  102. {
  103. $this->statementPrototype = $statementPrototype;
  104. $this->statementPrototype->setDriver($this);
  105. }
  106. /**
  107. * Register result prototype
  108. *
  109. * @param Result $resultPrototype
  110. */
  111. public function registerResultPrototype(Result $resultPrototype)
  112. {
  113. $this->resultPrototype = $resultPrototype;
  114. }
  115. /**
  116. * Add feature
  117. *
  118. * @param string $name
  119. * @param AbstractFeature $feature
  120. * @return Pdo
  121. */
  122. public function addFeature($name, $feature)
  123. {
  124. if ($feature instanceof AbstractFeature) {
  125. $name = $feature->getName(); // overwrite the name, just in case
  126. $feature->setDriver($this);
  127. }
  128. $this->features[$name] = $feature;
  129. return $this;
  130. }
  131. /**
  132. * Setup the default features for Pdo
  133. *
  134. * @return Pdo
  135. */
  136. public function setupDefaultFeatures()
  137. {
  138. $driverName = $this->connection->getDriverName();
  139. if ($driverName == 'sqlite') {
  140. $this->addFeature(null, new Feature\SqliteRowCounter);
  141. } elseif ($driverName == 'oci') {
  142. $this->addFeature(null, new Feature\OracleRowCounter);
  143. }
  144. return $this;
  145. }
  146. /**
  147. * Get feature
  148. *
  149. * @param $name
  150. * @return AbstractFeature|false
  151. */
  152. public function getFeature($name)
  153. {
  154. if (isset($this->features[$name])) {
  155. return $this->features[$name];
  156. }
  157. return false;
  158. }
  159. /**
  160. * Get database platform name
  161. *
  162. * @param string $nameFormat
  163. * @return string
  164. */
  165. public function getDatabasePlatformName($nameFormat = self::NAME_FORMAT_CAMELCASE)
  166. {
  167. $name = $this->getConnection()->getDriverName();
  168. if ($nameFormat == self::NAME_FORMAT_CAMELCASE) {
  169. switch ($name) {
  170. case 'pgsql':
  171. return 'Postgresql';
  172. case 'oci':
  173. return 'Oracle';
  174. default:
  175. return ucfirst($name);
  176. }
  177. } else {
  178. switch ($name) {
  179. case 'sqlite':
  180. return 'SQLite';
  181. case 'mysql':
  182. return 'MySQL';
  183. case 'pgsql':
  184. return 'PostgreSQL';
  185. case 'oci':
  186. return 'Oracle';
  187. default:
  188. return ucfirst($name);
  189. }
  190. }
  191. }
  192. /**
  193. * Check environment
  194. */
  195. public function checkEnvironment()
  196. {
  197. if (!extension_loaded('PDO')) {
  198. throw new Exception\RuntimeException('The PDO extension is required for this adapter but the extension is not loaded');
  199. }
  200. }
  201. /**
  202. * @return Connection
  203. */
  204. public function getConnection()
  205. {
  206. return $this->connection;
  207. }
  208. /**
  209. * @param string|PDOStatement $sqlOrResource
  210. * @return Statement
  211. */
  212. public function createStatement($sqlOrResource = null)
  213. {
  214. $statement = clone $this->statementPrototype;
  215. if ($sqlOrResource instanceof PDOStatement) {
  216. $statement->setResource($sqlOrResource);
  217. } else {
  218. if (is_string($sqlOrResource)) {
  219. $statement->setSql($sqlOrResource);
  220. }
  221. if (!$this->connection->isConnected()) {
  222. $this->connection->connect();
  223. }
  224. $statement->initialize($this->connection->getResource());
  225. }
  226. return $statement;
  227. }
  228. /**
  229. * @param resource $resource
  230. * @param mixed $context
  231. * @return Result
  232. */
  233. public function createResult($resource, $context = null)
  234. {
  235. $result = clone $this->resultPrototype;
  236. $rowCount = null;
  237. // special feature, sqlite PDO counter
  238. if ($this->connection->getDriverName() == 'sqlite'
  239. && ($sqliteRowCounter = $this->getFeature('SqliteRowCounter'))
  240. && $resource->columnCount() > 0) {
  241. $rowCount = $sqliteRowCounter->getRowCountClosure($context);
  242. }
  243. // special feature, oracle PDO counter
  244. if ($this->connection->getDriverName() == 'oci'
  245. && ($oracleRowCounter = $this->getFeature('OracleRowCounter'))
  246. && $resource->columnCount() > 0) {
  247. $rowCount = $oracleRowCounter->getRowCountClosure($context);
  248. }
  249. $result->initialize($resource, $this->connection->getLastGeneratedValue(), $rowCount);
  250. return $result;
  251. }
  252. /**
  253. * @return array
  254. */
  255. public function getPrepareType()
  256. {
  257. return self::PARAMETERIZATION_NAMED;
  258. }
  259. /**
  260. * @param string $name
  261. * @param string|null $type
  262. * @return string
  263. */
  264. public function formatParameterName($name, $type = null)
  265. {
  266. if ($type == null && !is_numeric($name) || $type == self::PARAMETERIZATION_NAMED) {
  267. return ':' . $name;
  268. }
  269. return '?';
  270. }
  271. /**
  272. * @return mixed
  273. */
  274. public function getLastGeneratedValue($name = null)
  275. {
  276. return $this->connection->getLastGeneratedValue($name);
  277. }
  278. }