3 * Zend Framework (http://framework.zend.com/)
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
10 namespace Zend\Db\Adapter
;
12 use Zend\Db\ResultSet
;
15 * @property Driver\DriverInterface $driver
16 * @property Platform\PlatformInterface $platform
18 class Adapter
implements AdapterInterface
, Profiler\ProfilerAwareInterface
21 * Query Mode Constants
23 const QUERY_MODE_EXECUTE
= 'execute';
24 const QUERY_MODE_PREPARE
= 'prepare';
27 * Prepare Type Constants
29 const PREPARE_TYPE_POSITIONAL
= 'positional';
30 const PREPARE_TYPE_NAMED
= 'named';
32 const FUNCTION_FORMAT_PARAMETER_NAME
= 'formatParameterName';
33 const FUNCTION_QUOTE_IDENTIFIER
= 'quoteIdentifier';
34 const FUNCTION_QUOTE_VALUE
= 'quoteValue';
36 const VALUE_QUOTE_SEPARATOR
= 'quoteSeparator';
39 * @var Driver\DriverInterface
41 protected $driver = null;
44 * @var Platform\PlatformInterface
46 protected $platform = null;
49 * @var Profiler\ProfilerInterface
51 protected $profiler = null;
54 * @var ResultSet\ResultSetInterface
56 protected $queryResultSetPrototype = null;
59 * @var Driver\StatementInterface
61 protected $lastPreparedStatement = null;
64 * @param Driver\DriverInterface|array $driver
65 * @param Platform\PlatformInterface $platform
66 * @param ResultSet\ResultSetInterface $queryResultPrototype
67 * @param Profiler\ProfilerInterface $profiler
68 * @throws Exception\InvalidArgumentException
70 public function __construct($driver, Platform\PlatformInterface
$platform = null, ResultSet\ResultSetInterface
$queryResultPrototype = null, Profiler\ProfilerInterface
$profiler = null)
72 // first argument can be an array of parameters
73 $parameters = array();
75 if (is_array($driver)) {
76 $parameters = $driver;
77 if ($profiler === null && isset($parameters['profiler'])) {
78 $profiler = $this->createProfiler($parameters);
80 $driver = $this->createDriver($parameters);
81 } elseif (!$driver instanceof Driver\DriverInterface
) {
82 throw new Exception\
InvalidArgumentException(
83 'The supplied or instantiated driver object does not implement Zend\Db\Adapter\Driver\DriverInterface'
87 $driver->checkEnvironment();
88 $this->driver
= $driver;
90 if ($platform == null) {
91 $platform = $this->createPlatform($parameters);
94 $this->platform
= $platform;
95 $this->queryResultSetPrototype
= ($queryResultPrototype) ?
: new ResultSet\
ResultSet();
98 $this->setProfiler($profiler);
103 * @param Profiler\ProfilerInterface $profiler
106 public function setProfiler(Profiler\ProfilerInterface
$profiler)
108 $this->profiler
= $profiler;
109 if ($this->driver
instanceof Profiler\ProfilerAwareInterface
) {
110 $this->driver
->setProfiler($profiler);
116 * @return null|Profiler\ProfilerInterface
118 public function getProfiler()
120 return $this->profiler
;
126 * @throws Exception\RuntimeException
127 * @return Driver\DriverInterface
129 public function getDriver()
131 if ($this->driver
== null) {
132 throw new Exception\
RuntimeException('Driver has not been set or configured for this adapter.');
134 return $this->driver
;
138 * @return Platform\PlatformInterface
140 public function getPlatform()
142 return $this->platform
;
146 * @return ResultSet\ResultSetInterface
148 public function getQueryResultSetPrototype()
150 return $this->queryResultSetPrototype
;
153 public function getCurrentSchema()
155 return $this->driver
->getConnection()->getCurrentSchema();
159 * query() is a convenience function
162 * @param string|array|ParameterContainer $parametersOrQueryMode
163 * @throws Exception\InvalidArgumentException
164 * @return Driver\StatementInterface|ResultSet\ResultSet
166 public function query($sql, $parametersOrQueryMode = self
::QUERY_MODE_PREPARE
)
168 if (is_string($parametersOrQueryMode) && in_array($parametersOrQueryMode, array(self
::QUERY_MODE_PREPARE
, self
::QUERY_MODE_EXECUTE
))) {
169 $mode = $parametersOrQueryMode;
171 } elseif (is_array($parametersOrQueryMode) ||
$parametersOrQueryMode instanceof ParameterContainer
) {
172 $mode = self
::QUERY_MODE_PREPARE
;
173 $parameters = $parametersOrQueryMode;
175 throw new Exception\
InvalidArgumentException('Parameter 2 to this method must be a flag, an array, or ParameterContainer');
178 if ($mode == self
::QUERY_MODE_PREPARE
) {
179 $this->lastPreparedStatement
= null;
180 $this->lastPreparedStatement
= $this->driver
->createStatement($sql);
181 $this->lastPreparedStatement
->prepare();
182 if (is_array($parameters) ||
$parameters instanceof ParameterContainer
) {
183 $this->lastPreparedStatement
->setParameterContainer((is_array($parameters)) ?
new ParameterContainer($parameters) : $parameters);
184 $result = $this->lastPreparedStatement
->execute();
186 return $this->lastPreparedStatement
;
189 $result = $this->driver
->getConnection()->execute($sql);
192 if ($result instanceof Driver\ResultInterface
&& $result->isQueryResult()) {
193 $resultSet = clone $this->queryResultSetPrototype
;
194 $resultSet->initialize($result);
204 * @param string $initialSql
205 * @param ParameterContainer $initialParameters
206 * @return Driver\StatementInterface
208 public function createStatement($initialSql = null, $initialParameters = null)
210 $statement = $this->driver
->createStatement($initialSql);
211 if ($initialParameters == null ||
!$initialParameters instanceof ParameterContainer
&& is_array($initialParameters)) {
212 $initialParameters = new ParameterContainer((is_array($initialParameters) ?
$initialParameters : array()));
214 $statement->setParameterContainer($initialParameters);
218 public function getHelpers(/* $functions */)
220 $functions = array();
221 $platform = $this->platform
;
222 foreach (func_get_args() as $arg) {
224 case self
::FUNCTION_QUOTE_IDENTIFIER
:
225 $functions[] = function ($value) use ($platform) { return $platform->quoteIdentifier($value); };
227 case self
::FUNCTION_QUOTE_VALUE
:
228 $functions[] = function ($value) use ($platform) { return $platform->quoteValue($value); };
237 * @throws Exception\InvalidArgumentException
238 * @return Driver\DriverInterface|Platform\PlatformInterface
240 public function __get($name)
242 switch (strtolower($name)) {
244 return $this->driver
;
246 return $this->platform
;
248 throw new Exception\
InvalidArgumentException('Invalid magic property on adapter');
254 * @param array $parameters
255 * @return Driver\DriverInterface
256 * @throws \InvalidArgumentException
257 * @throws Exception\InvalidArgumentException
259 protected function createDriver($parameters)
261 if (!isset($parameters['driver'])) {
262 throw new Exception\
InvalidArgumentException(__FUNCTION__
. ' expects a "driver" key to be present inside the parameters');
265 if ($parameters['driver'] instanceof Driver\DriverInterface
) {
266 return $parameters['driver'];
269 if (!is_string($parameters['driver'])) {
270 throw new Exception\
InvalidArgumentException(__FUNCTION__
. ' expects a "driver" to be a string or instance of DriverInterface');
274 if (isset($parameters['options'])) {
275 $options = (array) $parameters['options'];
276 unset($parameters['options']);
279 $driverName = strtolower($parameters['driver']);
280 switch ($driverName) {
282 $driver = new Driver\Mysqli\
Mysqli($parameters, null, null, $options);
285 $driver = new Driver\Sqlsrv\
Sqlsrv($parameters);
288 $driver = new Driver\Oci8\
Oci8($parameters);
291 $driver = new Driver\Pgsql\
Pgsql($parameters);
294 $driver = new Driver\IbmDb2\
IbmDb2($parameters);
298 if ($driverName == 'pdo' ||
strpos($driverName, 'pdo') === 0) {
299 $driver = new Driver\Pdo\
Pdo($parameters);
303 if (!isset($driver) ||
!$driver instanceof Driver\DriverInterface
) {
304 throw new Exception\
InvalidArgumentException('DriverInterface expected', null, null);
311 * @param Driver\DriverInterface $driver
312 * @return Platform\PlatformInterface
314 protected function createPlatform($parameters)
316 if (isset($parameters['platform'])) {
317 $platformName = $parameters['platform'];
318 } elseif ($this->driver
instanceof Driver\DriverInterface
) {
319 $platformName = $this->driver
->getDatabasePlatformName(Driver\DriverInterface
::NAME_FORMAT_CAMELCASE
);
321 throw new Exception\
InvalidArgumentException('A platform could not be determined from the provided configuration');
324 // currently only supported by the IbmDb2 & Oracle concrete implementations
325 $options = (isset($parameters['platform_options'])) ?
$parameters['platform_options'] : array();
327 switch ($platformName) {
329 // mysqli or pdo_mysql driver
330 $driver = ($this->driver
instanceof Driver\Mysqli\Mysqli ||
$this->driver
instanceof Driver\Pdo\Pdo
) ?
$this->driver
: null;
331 return new Platform\
Mysql($driver);
333 // PDO is only supported driver for quoting values in this platform
334 return new Platform\
SqlServer(($this->driver
instanceof Driver\Pdo\Pdo
) ?
$this->driver
: null);
336 // oracle does not accept a driver as an option, no driver specific quoting available
337 return new Platform\
Oracle($options);
339 // PDO is only supported driver for quoting values in this platform
340 return new Platform\
Sqlite(($this->driver
instanceof Driver\Pdo\Pdo
) ?
$this->driver
: null);
342 // pgsql or pdo postgres driver
343 $driver = ($this->driver
instanceof Driver\Pgsql\Pgsql ||
$this->driver
instanceof Driver\Pdo\Pdo
) ?
$this->driver
: null;
344 return new Platform\
Postgresql($driver);
346 // ibm_db2 driver escaping does not need an action connection
347 return new Platform\
IbmDb2($options);
349 return new Platform\
Sql92();
353 protected function createProfiler($parameters)
355 if ($parameters['profiler'] instanceof Profiler\ProfilerInterface
) {
356 $profiler = $parameters['profiler'];
357 } elseif (is_bool($parameters['profiler'])) {
358 $profiler = ($parameters['profiler'] == true) ?
new Profiler\Profiler
: null;
360 throw new Exception\
InvalidArgumentException(
361 '"profiler" parameter must be an instance of ProfilerInterface or a boolean'
368 * @param array $parameters
369 * @return Driver\DriverInterface
370 * @throws \InvalidArgumentException
371 * @throws Exception\InvalidArgumentException
374 protected function createDriverFromParameters(array $parameters)
376 return $this->createDriver($parameters);
380 * @param Driver\DriverInterface $driver
381 * @return Platform\PlatformInterface
384 protected function createPlatformFromDriver(Driver\DriverInterface
$driver)
386 return $this->createPlatform($driver);