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\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
22 protected $driver = null;
25 * @var Profiler\ProfilerInterface
27 protected $profiler = null;
30 * Connection parameters
34 protected $connectionParameters = array();
39 protected $resource = null;
46 protected $inTransaction = false;
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');
66 * @param Mysqli $driver
69 public function setDriver(Mysqli
$driver)
71 $this->driver
= $driver;
76 * @param Profiler\ProfilerInterface $profiler
79 public function setProfiler(Profiler\ProfilerInterface
$profiler)
81 $this->profiler
= $profiler;
86 * @return null|Profiler\ProfilerInterface
88 public function getProfiler()
90 return $this->profiler
;
94 * Set connection parameters
96 * @param array $connectionParameters
99 public function setConnectionParameters(array $connectionParameters)
101 $this->connectionParameters
= $connectionParameters;
106 * Get connection parameters
110 public function getConnectionParameters()
112 return $this->connectionParameters
;
120 public function getCurrentSchema()
122 if (!$this->isConnected()) {
126 /** @var $result \mysqli_result */
127 $result = $this->resource->query('SELECT DATABASE()');
128 $r = $result->fetch_row();
135 * @param \mysqli $resource
138 public function setResource(\mysqli
$resource)
140 $this->resource = $resource;
149 public function getResource()
152 return $this->resource;
158 * @throws Exception\RuntimeException
161 public function connect()
163 if ($this->resource instanceof \mysqli
) {
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])) {
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)) {
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(
209 new Exception\
ErrorException($this->resource->connect_error
, $this->resource->connect_errno
)
213 if (!empty($p['charset'])) {
214 $this->resource->set_charset($p['charset']);
225 public function isConnected()
227 return ($this->resource instanceof \mysqli
);
235 public function disconnect()
237 if ($this->resource instanceof \mysqli
) {
238 $this->resource->close();
240 unset($this->resource);
248 public function beginTransaction()
250 if (!$this->isConnected()) {
254 $this->resource->autocommit(false);
255 $this->inTransaction
= true;
263 public function commit()
265 if (!$this->resource) {
269 $this->resource->commit();
270 $this->inTransaction
= false;
271 $this->resource->autocommit(true);
277 * @throws Exception\RuntimeException
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);
299 * @throws Exception\InvalidQueryException
302 public function execute($sql)
304 if (!$this->isConnected()) {
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
333 public function getLastGeneratedValue($name = null)
335 return $this->resource->insert_id
;