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 / Driver / Mysqli / Connection.php
blob190000dea4663ba1ad9fc6a76172dd06249fde63
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\Driver\Mysqli;
12 use Zend\Db\Adapter\Driver\ConnectionInterface;
13 use Zend\Db\Adapter\Exception;
14 use Zend\Db\Adapter\Profiler;
16 class Connection implements ConnectionInterface, Profiler\ProfilerAwareInterface
19 /**
20 * @var Mysqli
22 protected $driver = null;
24 /**
25 * @var Profiler\ProfilerInterface
27 protected $profiler = null;
29 /**
30 * Connection parameters
32 * @var array
34 protected $connectionParameters = array();
36 /**
37 * @var \mysqli
39 protected $resource = null;
41 /**
42 * In transaction
44 * @var bool
46 protected $inTransaction = false;
48 /**
49 * Constructor
51 * @param array|mysqli|null $connectionInfo
52 * @throws \Zend\Db\Adapter\Exception\InvalidArgumentException
54 public function __construct($connectionInfo = null)
56 if (is_array($connectionInfo)) {
57 $this->setConnectionParameters($connectionInfo);
58 } elseif ($connectionInfo instanceof \mysqli) {
59 $this->setResource($connectionInfo);
60 } elseif (null !== $connectionInfo) {
61 throw new Exception\InvalidArgumentException('$connection must be an array of parameters, a mysqli object or null');
65 /**
66 * @param Mysqli $driver
67 * @return Connection
69 public function setDriver(Mysqli $driver)
71 $this->driver = $driver;
72 return $this;
75 /**
76 * @param Profiler\ProfilerInterface $profiler
77 * @return Connection
79 public function setProfiler(Profiler\ProfilerInterface $profiler)
81 $this->profiler = $profiler;
82 return $this;
85 /**
86 * @return null|Profiler\ProfilerInterface
88 public function getProfiler()
90 return $this->profiler;
93 /**
94 * Set connection parameters
96 * @param array $connectionParameters
97 * @return Connection
99 public function setConnectionParameters(array $connectionParameters)
101 $this->connectionParameters = $connectionParameters;
102 return $this;
106 * Get connection parameters
108 * @return array
110 public function getConnectionParameters()
112 return $this->connectionParameters;
116 * Get current schema
118 * @return string
120 public function getCurrentSchema()
122 if (!$this->isConnected()) {
123 $this->connect();
126 /** @var $result \mysqli_result */
127 $result = $this->resource->query('SELECT DATABASE()');
128 $r = $result->fetch_row();
129 return $r[0];
133 * Set resource
135 * @param \mysqli $resource
136 * @return Connection
138 public function setResource(\mysqli $resource)
140 $this->resource = $resource;
141 return $this;
145 * Get resource
147 * @return \mysqli
149 public function getResource()
151 $this->connect();
152 return $this->resource;
156 * Connect
158 * @throws Exception\RuntimeException
159 * @return Connection
161 public function connect()
163 if ($this->resource instanceof \mysqli) {
164 return $this;
167 // localize
168 $p = $this->connectionParameters;
170 // given a list of key names, test for existence in $p
171 $findParameterValue = function (array $names) use ($p) {
172 foreach ($names as $name) {
173 if (isset($p[$name])) {
174 return $p[$name];
177 return;
180 $hostname = $findParameterValue(array('hostname', 'host'));
181 $username = $findParameterValue(array('username', 'user'));
182 $password = $findParameterValue(array('password', 'passwd', 'pw'));
183 $database = $findParameterValue(array('database', 'dbname', 'db', 'schema'));
184 $port = (isset($p['port'])) ? (int) $p['port'] : null;
185 $socket = (isset($p['socket'])) ? $p['socket'] : null;
187 $this->resource = new \mysqli();
188 $this->resource->init();
190 if (!empty($p['driver_options'])) {
191 foreach ($p['driver_options'] as $option => $value) {
192 if (is_string($option)) {
193 $option = strtoupper($option);
194 if (!defined($option)) {
195 continue;
197 $option = constant($option);
199 $this->resource->options($option, $value);
203 $this->resource->real_connect($hostname, $username, $password, $database, $port, $socket);
205 if ($this->resource->connect_error) {
206 throw new Exception\RuntimeException(
207 'Connection error',
208 null,
209 new Exception\ErrorException($this->resource->connect_error, $this->resource->connect_errno)
213 if (!empty($p['charset'])) {
214 $this->resource->set_charset($p['charset']);
217 return $this;
221 * Is connected
223 * @return bool
225 public function isConnected()
227 return ($this->resource instanceof \mysqli);
231 * Disconnect
233 * @return void
235 public function disconnect()
237 if ($this->resource instanceof \mysqli) {
238 $this->resource->close();
240 unset($this->resource);
244 * Begin transaction
246 * @return void
248 public function beginTransaction()
250 if (!$this->isConnected()) {
251 $this->connect();
254 $this->resource->autocommit(false);
255 $this->inTransaction = true;
259 * Commit
261 * @return void
263 public function commit()
265 if (!$this->resource) {
266 $this->connect();
269 $this->resource->commit();
270 $this->inTransaction = false;
271 $this->resource->autocommit(true);
275 * Rollback
277 * @throws Exception\RuntimeException
278 * @return Connection
280 public function rollback()
282 if (!$this->resource) {
283 throw new Exception\RuntimeException('Must be connected before you can rollback.');
286 if (!$this->inTransaction) {
287 throw new Exception\RuntimeException('Must call commit() before you can rollback.');
290 $this->resource->rollback();
291 $this->resource->autocommit(true);
292 return $this;
296 * Execute
298 * @param string $sql
299 * @throws Exception\InvalidQueryException
300 * @return Result
302 public function execute($sql)
304 if (!$this->isConnected()) {
305 $this->connect();
308 if ($this->profiler) {
309 $this->profiler->profilerStart($sql);
312 $resultResource = $this->resource->query($sql);
314 if ($this->profiler) {
315 $this->profiler->profilerFinish($sql);
318 // if the returnValue is something other than a mysqli_result, bypass wrapping it
319 if ($resultResource === false) {
320 throw new Exception\InvalidQueryException($this->resource->error);
323 $resultPrototype = $this->driver->createResult(($resultResource === true) ? $this->resource : $resultResource);
324 return $resultPrototype;
328 * Get last generated id
330 * @param null $name Ignored
331 * @return int
333 public function getLastGeneratedValue($name = null)
335 return $this->resource->insert_id;