/vendor/zendframework/zend-db/src/Adapter/Driver/Mysqli/Connection.php

https://github.com/tmccormi/openemr · PHP · 285 lines · 176 code · 47 blank · 62 comment · 22 complexity · e84298d3b7fefadc87d76d68e1b48dc0 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-2016 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\Driver\Mysqli;
  10. use Exception as GenericException;
  11. use Zend\Db\Adapter\Driver\AbstractConnection;
  12. use Zend\Db\Adapter\Exception;
  13. class Connection extends AbstractConnection
  14. {
  15. /**
  16. * @var Mysqli
  17. */
  18. protected $driver = null;
  19. /**
  20. * @var \mysqli
  21. */
  22. protected $resource = null;
  23. /**
  24. * Constructor
  25. *
  26. * @param array|mysqli|null $connectionInfo
  27. * @throws \Zend\Db\Adapter\Exception\InvalidArgumentException
  28. */
  29. public function __construct($connectionInfo = null)
  30. {
  31. if (is_array($connectionInfo)) {
  32. $this->setConnectionParameters($connectionInfo);
  33. } elseif ($connectionInfo instanceof \mysqli) {
  34. $this->setResource($connectionInfo);
  35. } elseif (null !== $connectionInfo) {
  36. throw new Exception\InvalidArgumentException(
  37. '$connection must be an array of parameters, a mysqli object or null'
  38. );
  39. }
  40. }
  41. /**
  42. * @param Mysqli $driver
  43. * @return self Provides a fluent interface
  44. */
  45. public function setDriver(Mysqli $driver)
  46. {
  47. $this->driver = $driver;
  48. return $this;
  49. }
  50. /**
  51. * {@inheritDoc}
  52. */
  53. public function getCurrentSchema()
  54. {
  55. if (! $this->isConnected()) {
  56. $this->connect();
  57. }
  58. $result = $this->resource->query('SELECT DATABASE()');
  59. $r = $result->fetch_row();
  60. return $r[0];
  61. }
  62. /**
  63. * Set resource
  64. *
  65. * @param \mysqli $resource
  66. * @return self Provides a fluent interface
  67. */
  68. public function setResource(\mysqli $resource)
  69. {
  70. $this->resource = $resource;
  71. return $this;
  72. }
  73. /**
  74. * {@inheritDoc}
  75. */
  76. public function connect()
  77. {
  78. if ($this->resource instanceof \mysqli) {
  79. return $this;
  80. }
  81. // localize
  82. $p = $this->connectionParameters;
  83. // given a list of key names, test for existence in $p
  84. $findParameterValue = function (array $names) use ($p) {
  85. foreach ($names as $name) {
  86. if (isset($p[$name])) {
  87. return $p[$name];
  88. }
  89. }
  90. return;
  91. };
  92. $hostname = $findParameterValue(['hostname', 'host']);
  93. $username = $findParameterValue(['username', 'user']);
  94. $password = $findParameterValue(['password', 'passwd', 'pw']);
  95. $database = $findParameterValue(['database', 'dbname', 'db', 'schema']);
  96. $port = (isset($p['port'])) ? (int) $p['port'] : null;
  97. $socket = (isset($p['socket'])) ? $p['socket'] : null;
  98. $useSSL = (isset($p['use_ssl'])) ? $p['use_ssl'] : 0;
  99. $clientKey = (isset($p['client_key'])) ? $p['client_key'] : null;
  100. $clientCert = (isset($p['client_cert'])) ? $p['client_cert'] : null;
  101. $caCert = (isset($p['ca_cert'])) ? $p['ca_cert'] : null;
  102. $caPath = (isset($p['ca_path'])) ? $p['ca_path'] : null;
  103. $cipher = (isset($p['cipher'])) ? $p['cipher'] : null;
  104. $this->resource = new \mysqli();
  105. $this->resource->init();
  106. if (! empty($p['driver_options'])) {
  107. foreach ($p['driver_options'] as $option => $value) {
  108. if (is_string($option)) {
  109. $option = strtoupper($option);
  110. if (! defined($option)) {
  111. continue;
  112. }
  113. $option = constant($option);
  114. }
  115. $this->resource->options($option, $value);
  116. }
  117. }
  118. $flags = null;
  119. if ($useSSL && ! $socket) {
  120. $this->resource->ssl_set($clientKey, $clientCert, $caCert, $caPath, $cipher);
  121. //MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT is not valid option, needs to be set as flag
  122. if (isset($p['driver_options'])
  123. && isset($p['driver_options'][MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT])
  124. ) {
  125. $flags = MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT;
  126. }
  127. }
  128. try {
  129. $this->resource->real_connect($hostname, $username, $password, $database, $port, $socket, $flags);
  130. } catch (GenericException $e) {
  131. throw new Exception\RuntimeException(
  132. 'Connection error',
  133. null,
  134. new Exception\ErrorException($this->resource->connect_error, $this->resource->connect_errno)
  135. );
  136. }
  137. if ($this->resource->connect_error) {
  138. throw new Exception\RuntimeException(
  139. 'Connection error',
  140. null,
  141. new Exception\ErrorException($this->resource->connect_error, $this->resource->connect_errno)
  142. );
  143. }
  144. if (! empty($p['charset'])) {
  145. $this->resource->set_charset($p['charset']);
  146. }
  147. return $this;
  148. }
  149. /**
  150. * {@inheritDoc}
  151. */
  152. public function isConnected()
  153. {
  154. return ($this->resource instanceof \mysqli);
  155. }
  156. /**
  157. * {@inheritDoc}
  158. */
  159. public function disconnect()
  160. {
  161. if ($this->resource instanceof \mysqli) {
  162. $this->resource->close();
  163. }
  164. $this->resource = null;
  165. }
  166. /**
  167. * {@inheritDoc}
  168. */
  169. public function beginTransaction()
  170. {
  171. if (! $this->isConnected()) {
  172. $this->connect();
  173. }
  174. $this->resource->autocommit(false);
  175. $this->inTransaction = true;
  176. return $this;
  177. }
  178. /**
  179. * {@inheritDoc}
  180. */
  181. public function commit()
  182. {
  183. if (! $this->isConnected()) {
  184. $this->connect();
  185. }
  186. $this->resource->commit();
  187. $this->inTransaction = false;
  188. $this->resource->autocommit(true);
  189. return $this;
  190. }
  191. /**
  192. * {@inheritDoc}
  193. */
  194. public function rollback()
  195. {
  196. if (! $this->isConnected()) {
  197. throw new Exception\RuntimeException('Must be connected before you can rollback.');
  198. }
  199. if (! $this->inTransaction) {
  200. throw new Exception\RuntimeException('Must call beginTransaction() before you can rollback.');
  201. }
  202. $this->resource->rollback();
  203. $this->resource->autocommit(true);
  204. $this->inTransaction = false;
  205. return $this;
  206. }
  207. /**
  208. * {@inheritDoc}
  209. *
  210. * @throws Exception\InvalidQueryException
  211. */
  212. public function execute($sql)
  213. {
  214. if (! $this->isConnected()) {
  215. $this->connect();
  216. }
  217. if ($this->profiler) {
  218. $this->profiler->profilerStart($sql);
  219. }
  220. $resultResource = $this->resource->query($sql);
  221. if ($this->profiler) {
  222. $this->profiler->profilerFinish($sql);
  223. }
  224. // if the returnValue is something other than a mysqli_result, bypass wrapping it
  225. if ($resultResource === false) {
  226. throw new Exception\InvalidQueryException($this->resource->error);
  227. }
  228. $resultPrototype = $this->driver->createResult(($resultResource === true) ? $this->resource : $resultResource);
  229. return $resultPrototype;
  230. }
  231. /**
  232. * {@inheritDoc}
  233. */
  234. public function getLastGeneratedValue($name = null)
  235. {
  236. return $this->resource->insert_id;
  237. }
  238. }