Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Db / Adapter / Adapter.php
blob8a948b67af69bc320a8b84b3877cb865a01d7c9d
1 <?php
2 /**
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
8 */
10 namespace Zend\Db\Adapter;
12 use Zend\Db\ResultSet;
14 /**
15 * @property Driver\DriverInterface $driver
16 * @property Platform\PlatformInterface $platform
18 class Adapter implements AdapterInterface, Profiler\ProfilerAwareInterface
20 /**
21 * Query Mode Constants
23 const QUERY_MODE_EXECUTE = 'execute';
24 const QUERY_MODE_PREPARE = 'prepare';
26 /**
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';
38 /**
39 * @var Driver\DriverInterface
41 protected $driver = null;
43 /**
44 * @var Platform\PlatformInterface
46 protected $platform = null;
48 /**
49 * @var Profiler\ProfilerInterface
51 protected $profiler = null;
53 /**
54 * @var ResultSet\ResultSetInterface
56 protected $queryResultSetPrototype = null;
58 /**
59 * @var Driver\StatementInterface
61 protected $lastPreparedStatement = null;
63 /**
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();
97 if ($profiler) {
98 $this->setProfiler($profiler);
103 * @param Profiler\ProfilerInterface $profiler
104 * @return Adapter
106 public function setProfiler(Profiler\ProfilerInterface $profiler)
108 $this->profiler = $profiler;
109 if ($this->driver instanceof Profiler\ProfilerAwareInterface) {
110 $this->driver->setProfiler($profiler);
112 return $this;
116 * @return null|Profiler\ProfilerInterface
118 public function getProfiler()
120 return $this->profiler;
124 * getDriver()
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
161 * @param string $sql
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;
170 $parameters = null;
171 } elseif (is_array($parametersOrQueryMode) || $parametersOrQueryMode instanceof ParameterContainer) {
172 $mode = self::QUERY_MODE_PREPARE;
173 $parameters = $parametersOrQueryMode;
174 } else {
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();
185 } else {
186 return $this->lastPreparedStatement;
188 } else {
189 $result = $this->driver->getConnection()->execute($sql);
192 if ($result instanceof Driver\ResultInterface && $result->isQueryResult()) {
193 $resultSet = clone $this->queryResultSetPrototype;
194 $resultSet->initialize($result);
195 return $resultSet;
198 return $result;
202 * Create statement
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);
215 return $statement;
218 public function getHelpers(/* $functions */)
220 $functions = array();
221 $platform = $this->platform;
222 foreach (func_get_args() as $arg) {
223 switch ($arg) {
224 case self::FUNCTION_QUOTE_IDENTIFIER:
225 $functions[] = function ($value) use ($platform) { return $platform->quoteIdentifier($value); };
226 break;
227 case self::FUNCTION_QUOTE_VALUE:
228 $functions[] = function ($value) use ($platform) { return $platform->quoteValue($value); };
229 break;
236 * @param $name
237 * @throws Exception\InvalidArgumentException
238 * @return Driver\DriverInterface|Platform\PlatformInterface
240 public function __get($name)
242 switch (strtolower($name)) {
243 case 'driver':
244 return $this->driver;
245 case 'platform':
246 return $this->platform;
247 default:
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');
273 $options = array();
274 if (isset($parameters['options'])) {
275 $options = (array) $parameters['options'];
276 unset($parameters['options']);
279 $driverName = strtolower($parameters['driver']);
280 switch ($driverName) {
281 case 'mysqli':
282 $driver = new Driver\Mysqli\Mysqli($parameters, null, null, $options);
283 break;
284 case 'sqlsrv':
285 $driver = new Driver\Sqlsrv\Sqlsrv($parameters);
286 break;
287 case 'oci8':
288 $driver = new Driver\Oci8\Oci8($parameters);
289 break;
290 case 'pgsql':
291 $driver = new Driver\Pgsql\Pgsql($parameters);
292 break;
293 case 'ibmdb2':
294 $driver = new Driver\IbmDb2\IbmDb2($parameters);
295 break;
296 case 'pdo':
297 default:
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);
307 return $driver;
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);
320 } else {
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) {
328 case 'Mysql':
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);
332 case 'SqlServer':
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);
335 case 'Oracle':
336 // oracle does not accept a driver as an option, no driver specific quoting available
337 return new Platform\Oracle($options);
338 case 'Sqlite':
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);
341 case 'Postgresql':
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);
345 case 'IbmDb2':
346 // ibm_db2 driver escaping does not need an action connection
347 return new Platform\IbmDb2($options);
348 default:
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;
359 } else {
360 throw new Exception\InvalidArgumentException(
361 '"profiler" parameter must be an instance of ProfilerInterface or a boolean'
364 return $profiler;
368 * @param array $parameters
369 * @return Driver\DriverInterface
370 * @throws \InvalidArgumentException
371 * @throws Exception\InvalidArgumentException
372 * @deprecated
374 protected function createDriverFromParameters(array $parameters)
376 return $this->createDriver($parameters);
380 * @param Driver\DriverInterface $driver
381 * @return Platform\PlatformInterface
382 * @deprecated
384 protected function createPlatformFromDriver(Driver\DriverInterface $driver)
386 return $this->createPlatform($driver);