PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zend-servicemanager/Zend/ServiceManager/library/Zend/Db/Adapter/Adapter.php

https://bitbucket.org/zbahij/eprojets_app
PHP | 388 lines | 235 code | 45 blank | 108 comment | 45 complexity | bfc85aaa45c757b90f54b0bf6d16cff1 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;
  10. use Zend\Db\ResultSet;
  11. /**
  12. * @property Driver\DriverInterface $driver
  13. * @property Platform\PlatformInterface $platform
  14. */
  15. class Adapter implements AdapterInterface, Profiler\ProfilerAwareInterface
  16. {
  17. /**
  18. * Query Mode Constants
  19. */
  20. const QUERY_MODE_EXECUTE = 'execute';
  21. const QUERY_MODE_PREPARE = 'prepare';
  22. /**
  23. * Prepare Type Constants
  24. */
  25. const PREPARE_TYPE_POSITIONAL = 'positional';
  26. const PREPARE_TYPE_NAMED = 'named';
  27. const FUNCTION_FORMAT_PARAMETER_NAME = 'formatParameterName';
  28. const FUNCTION_QUOTE_IDENTIFIER = 'quoteIdentifier';
  29. const FUNCTION_QUOTE_VALUE = 'quoteValue';
  30. const VALUE_QUOTE_SEPARATOR = 'quoteSeparator';
  31. /**
  32. * @var Driver\DriverInterface
  33. */
  34. protected $driver = null;
  35. /**
  36. * @var Platform\PlatformInterface
  37. */
  38. protected $platform = null;
  39. /**
  40. * @var Profiler\ProfilerInterface
  41. */
  42. protected $profiler = null;
  43. /**
  44. * @var ResultSet\ResultSetInterface
  45. */
  46. protected $queryResultSetPrototype = null;
  47. /**
  48. * @var Driver\StatementInterface
  49. */
  50. protected $lastPreparedStatement = null;
  51. /**
  52. * @param Driver\DriverInterface|array $driver
  53. * @param Platform\PlatformInterface $platform
  54. * @param ResultSet\ResultSetInterface $queryResultPrototype
  55. * @param Profiler\ProfilerInterface $profiler
  56. * @throws Exception\InvalidArgumentException
  57. */
  58. public function __construct($driver, Platform\PlatformInterface $platform = null, ResultSet\ResultSetInterface $queryResultPrototype = null, Profiler\ProfilerInterface $profiler = null)
  59. {
  60. // first argument can be an array of parameters
  61. $parameters = array();
  62. if (is_array($driver)) {
  63. $parameters = $driver;
  64. if ($profiler === null && isset($parameters['profiler'])) {
  65. $profiler = $this->createProfiler($parameters);
  66. }
  67. $driver = $this->createDriver($parameters);
  68. } elseif (!$driver instanceof Driver\DriverInterface) {
  69. throw new Exception\InvalidArgumentException(
  70. 'The supplied or instantiated driver object does not implement Zend\Db\Adapter\Driver\DriverInterface'
  71. );
  72. }
  73. $driver->checkEnvironment();
  74. $this->driver = $driver;
  75. if ($platform == null) {
  76. $platform = $this->createPlatform($parameters);
  77. }
  78. $this->platform = $platform;
  79. $this->queryResultSetPrototype = ($queryResultPrototype) ?: new ResultSet\ResultSet();
  80. if ($profiler) {
  81. $this->setProfiler($profiler);
  82. }
  83. }
  84. /**
  85. * @param Profiler\ProfilerInterface $profiler
  86. * @return Adapter
  87. */
  88. public function setProfiler(Profiler\ProfilerInterface $profiler)
  89. {
  90. $this->profiler = $profiler;
  91. if ($this->driver instanceof Profiler\ProfilerAwareInterface) {
  92. $this->driver->setProfiler($profiler);
  93. }
  94. return $this;
  95. }
  96. /**
  97. * @return null|Profiler\ProfilerInterface
  98. */
  99. public function getProfiler()
  100. {
  101. return $this->profiler;
  102. }
  103. /**
  104. * getDriver()
  105. *
  106. * @throws Exception\RuntimeException
  107. * @return Driver\DriverInterface
  108. */
  109. public function getDriver()
  110. {
  111. if ($this->driver == null) {
  112. throw new Exception\RuntimeException('Driver has not been set or configured for this adapter.');
  113. }
  114. return $this->driver;
  115. }
  116. /**
  117. * @return Platform\PlatformInterface
  118. */
  119. public function getPlatform()
  120. {
  121. return $this->platform;
  122. }
  123. /**
  124. * @return ResultSet\ResultSetInterface
  125. */
  126. public function getQueryResultSetPrototype()
  127. {
  128. return $this->queryResultSetPrototype;
  129. }
  130. public function getCurrentSchema()
  131. {
  132. return $this->driver->getConnection()->getCurrentSchema();
  133. }
  134. /**
  135. * query() is a convenience function
  136. *
  137. * @param string $sql
  138. * @param string|array|ParameterContainer $parametersOrQueryMode
  139. * @throws Exception\InvalidArgumentException
  140. * @return Driver\StatementInterface|ResultSet\ResultSet
  141. */
  142. public function query($sql, $parametersOrQueryMode = self::QUERY_MODE_PREPARE)
  143. {
  144. if (is_string($parametersOrQueryMode) && in_array($parametersOrQueryMode, array(self::QUERY_MODE_PREPARE, self::QUERY_MODE_EXECUTE))) {
  145. $mode = $parametersOrQueryMode;
  146. $parameters = null;
  147. } elseif (is_array($parametersOrQueryMode) || $parametersOrQueryMode instanceof ParameterContainer) {
  148. $mode = self::QUERY_MODE_PREPARE;
  149. $parameters = $parametersOrQueryMode;
  150. } else {
  151. throw new Exception\InvalidArgumentException('Parameter 2 to this method must be a flag, an array, or ParameterContainer');
  152. }
  153. if ($mode == self::QUERY_MODE_PREPARE) {
  154. $this->lastPreparedStatement = null;
  155. $this->lastPreparedStatement = $this->driver->createStatement($sql);
  156. $this->lastPreparedStatement->prepare();
  157. if (is_array($parameters) || $parameters instanceof ParameterContainer) {
  158. $this->lastPreparedStatement->setParameterContainer((is_array($parameters)) ? new ParameterContainer($parameters) : $parameters);
  159. $result = $this->lastPreparedStatement->execute();
  160. } else {
  161. return $this->lastPreparedStatement;
  162. }
  163. } else {
  164. $result = $this->driver->getConnection()->execute($sql);
  165. }
  166. if ($result instanceof Driver\ResultInterface && $result->isQueryResult()) {
  167. $resultSet = clone $this->queryResultSetPrototype;
  168. $resultSet->initialize($result);
  169. return $resultSet;
  170. }
  171. return $result;
  172. }
  173. /**
  174. * Create statement
  175. *
  176. * @param string $initialSql
  177. * @param ParameterContainer $initialParameters
  178. * @return Driver\StatementInterface
  179. */
  180. public function createStatement($initialSql = null, $initialParameters = null)
  181. {
  182. $statement = $this->driver->createStatement($initialSql);
  183. if ($initialParameters == null || !$initialParameters instanceof ParameterContainer && is_array($initialParameters)) {
  184. $initialParameters = new ParameterContainer((is_array($initialParameters) ? $initialParameters : array()));
  185. }
  186. $statement->setParameterContainer($initialParameters);
  187. return $statement;
  188. }
  189. public function getHelpers(/* $functions */)
  190. {
  191. $functions = array();
  192. $platform = $this->platform;
  193. foreach (func_get_args() as $arg) {
  194. switch ($arg) {
  195. case self::FUNCTION_QUOTE_IDENTIFIER:
  196. $functions[] = function ($value) use ($platform) { return $platform->quoteIdentifier($value); };
  197. break;
  198. case self::FUNCTION_QUOTE_VALUE:
  199. $functions[] = function ($value) use ($platform) { return $platform->quoteValue($value); };
  200. break;
  201. }
  202. }
  203. }
  204. /**
  205. * @param $name
  206. * @throws Exception\InvalidArgumentException
  207. * @return Driver\DriverInterface|Platform\PlatformInterface
  208. */
  209. public function __get($name)
  210. {
  211. switch (strtolower($name)) {
  212. case 'driver':
  213. return $this->driver;
  214. case 'platform':
  215. return $this->platform;
  216. default:
  217. throw new Exception\InvalidArgumentException('Invalid magic property on adapter');
  218. }
  219. }
  220. /**
  221. * @param array $parameters
  222. * @return Driver\DriverInterface
  223. * @throws \InvalidArgumentException
  224. * @throws Exception\InvalidArgumentException
  225. */
  226. protected function createDriver($parameters)
  227. {
  228. if (!isset($parameters['driver'])) {
  229. throw new Exception\InvalidArgumentException(__FUNCTION__ . ' expects a "driver" key to be present inside the parameters');
  230. }
  231. if ($parameters['driver'] instanceof Driver\DriverInterface) {
  232. return $parameters['driver'];
  233. }
  234. if (!is_string($parameters['driver'])) {
  235. throw new Exception\InvalidArgumentException(__FUNCTION__ . ' expects a "driver" to be a string or instance of DriverInterface');
  236. }
  237. $options = array();
  238. if (isset($parameters['options'])) {
  239. $options = (array) $parameters['options'];
  240. unset($parameters['options']);
  241. }
  242. $driverName = strtolower($parameters['driver']);
  243. switch ($driverName) {
  244. case 'mysqli':
  245. $driver = new Driver\Mysqli\Mysqli($parameters, null, null, $options);
  246. break;
  247. case 'sqlsrv':
  248. $driver = new Driver\Sqlsrv\Sqlsrv($parameters);
  249. break;
  250. case 'oci8':
  251. $driver = new Driver\Oci8\Oci8($parameters);
  252. break;
  253. case 'pgsql':
  254. $driver = new Driver\Pgsql\Pgsql($parameters);
  255. break;
  256. case 'ibmdb2':
  257. $driver = new Driver\IbmDb2\IbmDb2($parameters);
  258. break;
  259. case 'pdo':
  260. default:
  261. if ($driverName == 'pdo' || strpos($driverName, 'pdo') === 0) {
  262. $driver = new Driver\Pdo\Pdo($parameters);
  263. }
  264. }
  265. if (!isset($driver) || !$driver instanceof Driver\DriverInterface) {
  266. throw new Exception\InvalidArgumentException('DriverInterface expected', null, null);
  267. }
  268. return $driver;
  269. }
  270. /**
  271. * @param Driver\DriverInterface $driver
  272. * @return Platform\PlatformInterface
  273. */
  274. protected function createPlatform($parameters)
  275. {
  276. if (isset($parameters['platform'])) {
  277. $platformName = $parameters['platform'];
  278. } elseif ($this->driver instanceof Driver\DriverInterface) {
  279. $platformName = $this->driver->getDatabasePlatformName(Driver\DriverInterface::NAME_FORMAT_CAMELCASE);
  280. } else {
  281. throw new Exception\InvalidArgumentException('A platform could not be determined from the provided configuration');
  282. }
  283. // currently only supported by the IbmDb2 & Oracle concrete implementations
  284. $options = (isset($parameters['platform_options'])) ? $parameters['platform_options'] : array();
  285. switch ($platformName) {
  286. case 'Mysql':
  287. // mysqli or pdo_mysql driver
  288. $driver = ($this->driver instanceof Driver\Mysqli\Mysqli || $this->driver instanceof Driver\Pdo\Pdo) ? $this->driver : null;
  289. return new Platform\Mysql($driver);
  290. case 'SqlServer':
  291. // PDO is only supported driver for quoting values in this platform
  292. return new Platform\SqlServer(($this->driver instanceof Driver\Pdo\Pdo) ? $this->driver : null);
  293. case 'Oracle':
  294. // oracle does not accept a driver as an option, no driver specific quoting available
  295. return new Platform\Oracle($options);
  296. case 'Sqlite':
  297. // PDO is only supported driver for quoting values in this platform
  298. return new Platform\Sqlite(($this->driver instanceof Driver\Pdo\Pdo) ? $this->driver : null);
  299. case 'Postgresql':
  300. // pgsql or pdo postgres driver
  301. $driver = ($this->driver instanceof Driver\Pgsql\Pgsql || $this->driver instanceof Driver\Pdo\Pdo) ? $this->driver : null;
  302. return new Platform\Postgresql($driver);
  303. case 'IbmDb2':
  304. // ibm_db2 driver escaping does not need an action connection
  305. return new Platform\IbmDb2($options);
  306. default:
  307. return new Platform\Sql92();
  308. }
  309. }
  310. protected function createProfiler($parameters)
  311. {
  312. if ($parameters['profiler'] instanceof Profiler\ProfilerInterface) {
  313. $profiler = $parameters['profiler'];
  314. } elseif (is_bool($parameters['profiler'])) {
  315. $profiler = ($parameters['profiler'] == true) ? new Profiler\Profiler : null;
  316. } else {
  317. throw new Exception\InvalidArgumentException(
  318. '"profiler" parameter must be an instance of ProfilerInterface or a boolean'
  319. );
  320. }
  321. return $profiler;
  322. }
  323. /**
  324. * @param array $parameters
  325. * @return Driver\DriverInterface
  326. * @throws \InvalidArgumentException
  327. * @throws Exception\InvalidArgumentException
  328. * @deprecated
  329. */
  330. protected function createDriverFromParameters(array $parameters)
  331. {
  332. return $this->createDriver($parameters);
  333. }
  334. /**
  335. * @param Driver\DriverInterface $driver
  336. * @return Platform\PlatformInterface
  337. * @deprecated
  338. */
  339. protected function createPlatformFromDriver(Driver\DriverInterface $driver)
  340. {
  341. return $this->createPlatform($driver);
  342. }
  343. }