PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/ARC2/Store/Adapter/mysqliAdapter.php

http://github.com/semsol/arc2
PHP | 263 lines | 174 code | 40 blank | 49 comment | 29 complexity | 03576f0d08ac51311e17769f31c6fb52 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Adapter to enable usage of mysqli_* functions.
  4. *
  5. * @author Benjamin Nowack <bnowack@semsol.com>
  6. * @author Konrad Abicht <konrad.abicht@pier-and-peer.com>
  7. * @license W3C Software License and GPL
  8. * @homepage <https://github.com/semsol/arc2>
  9. */
  10. namespace ARC2\Store\Adapter;
  11. /**
  12. * mysqli Adapter - Handles database operations using mysqli.
  13. */
  14. class mysqliAdapter extends AbstractAdapter
  15. {
  16. protected $last_result;
  17. public function checkRequirements()
  18. {
  19. if (false == \extension_loaded('mysqli') || false == \function_exists('mysqli_connect')) {
  20. throw new \Exception('Extension mysqli is not loaded or function mysqli_connect is not available.');
  21. }
  22. }
  23. public function getAdapterName()
  24. {
  25. return 'mysqli';
  26. }
  27. /**
  28. * Connect to server or storing a given connection.
  29. *
  30. * @return string|MysqliDbExtended string if an error occoured, instance of MysqliDbExtended otherwise
  31. */
  32. public function connect($existingConnection = null)
  33. {
  34. // reuse a given existing connection.
  35. // it assumes that $existingConnection is a mysqli connection object
  36. if (null !== $existingConnection) {
  37. $this->db = new MysqliDbExtended($existingConnection);
  38. // create your own connection
  39. } elseif (null == $this->db) {
  40. // connect
  41. try {
  42. $this->db = new MysqliDbExtended(
  43. $this->configuration['db_host'],
  44. $this->configuration['db_user'],
  45. $this->configuration['db_pwd'],
  46. null,
  47. $this->configuration['db_port'] ?? 3306
  48. );
  49. } catch (\Exception $e) {
  50. return $e->getMessage();
  51. }
  52. }
  53. if (isset($this->configuration['db_name'])
  54. && true !== $this->db->simpleQuery('USE `'.$this->configuration['db_name'].'`')) {
  55. $fixed = 0;
  56. /* try to create it */
  57. if ($this->configuration['db_name']) {
  58. $this->db->simpleQuery('
  59. CREATE DATABASE IF NOT EXISTS `'.$this->configuration['db_name'].'`
  60. DEFAULT CHARACTER SET utf8
  61. DEFAULT COLLATE utf8_general_ci
  62. '
  63. );
  64. if ($this->db->simpleQuery('USE `'.$this->configuration['db_name'].'`')) {
  65. $this->db->simpleQuery("SET NAMES 'utf8'");
  66. $fixed = 1;
  67. }
  68. }
  69. if (!$fixed) {
  70. return $this->addError($this->db->getErrorMessage());
  71. } else {
  72. if (preg_match('/^utf8/', $this->getCollation())) {
  73. $this->db->simpleQuery("SET NAMES 'utf8'");
  74. }
  75. // This is RDF, we may need many JOINs...
  76. $this->db->simpleQuery('SET SESSION SQL_BIG_SELECTS=1');
  77. }
  78. }
  79. return $this->db;
  80. }
  81. public function disconnect()
  82. {
  83. return $this->db->disconnect();
  84. }
  85. public function escape($value)
  86. {
  87. return $this->db->escape($value);
  88. }
  89. public function fetchList($sql)
  90. {
  91. return $this->db->rawQuery($sql);
  92. }
  93. public function fetchRow($sql)
  94. {
  95. $row = $this->db->rawQueryOne($sql);
  96. return null != $row ? $row : false;
  97. }
  98. public function getCollation()
  99. {
  100. $row = $this->fetchRow('SHOW TABLE STATUS LIKE "'.$this->getTablePrefix().'setting"');
  101. if (isset($row['Collation'])) {
  102. return $row['Collation'];
  103. } else {
  104. return '';
  105. }
  106. }
  107. public function getConnectionId()
  108. {
  109. if (null != $this->db) {
  110. return $this->db->mysqli()->thread_id;
  111. }
  112. }
  113. /**
  114. * For backward compatibility reasons. Get mysqli connection object.
  115. *
  116. * @return mysqli
  117. */
  118. public function getConnection()
  119. {
  120. return $this->db->mysqli();
  121. }
  122. public function getDBSName()
  123. {
  124. if (null == $this->db) {
  125. return null;
  126. }
  127. return false !== strpos($this->getServerInfo(), 'MariaDB')
  128. ? 'mariadb'
  129. : 'mysql';
  130. }
  131. public function getLastInsertId()
  132. {
  133. if (null != $this->db) {
  134. return $this->db->getLastInsertId();
  135. }
  136. return 'No database connection (mysqliAdapter).';
  137. }
  138. public function getServerInfo()
  139. {
  140. $this->connect();
  141. return $this->db->mysqli()->server_info;
  142. }
  143. /**
  144. * Returns the version of the database server like 05-00-12.
  145. */
  146. public function getServerVersion()
  147. {
  148. $res = preg_match(
  149. "/([0-9]+)\.([0-9]+)\.([0-9]+)/",
  150. $this->getServerInfo(),
  151. $matches
  152. );
  153. return 1 == $res
  154. ? sprintf('%02d-%02d-%02d', $matches[1], $matches[2], $matches[3])
  155. : '00-00-00';
  156. }
  157. public function getErrorMessage()
  158. {
  159. return $this->db->getErrorMessage();
  160. }
  161. public function getErrorCode()
  162. {
  163. return $this->db->getErrorCode();
  164. }
  165. public function getNumberOfRows($sql)
  166. {
  167. return $this->db->getNumberOfRows($sql);
  168. }
  169. public function getStoreName()
  170. {
  171. if (isset($this->configuration['store_name'])) {
  172. return $this->configuration['store_name'];
  173. }
  174. return 'arc';
  175. }
  176. public function getTablePrefix()
  177. {
  178. $prefix = '';
  179. if (isset($this->configuration['db_table_prefix'])) {
  180. $prefix = $this->configuration['db_table_prefix'].'_';
  181. }
  182. $prefix .= $this->getStoreName().'_';
  183. return $prefix;
  184. }
  185. /**
  186. * For compatibility reasons. Executes a query using mysqli and returns the result. Dont use
  187. * this function directly. It is only used once to make sure, ARC2 keeps its backward compatibility
  188. * while in the 2.x branch.
  189. *
  190. * @param string $sql query to execute
  191. *
  192. * @return mysqli result|false
  193. */
  194. public function mysqliQuery($sql)
  195. {
  196. return $this->db->mysqliQuery($sql);
  197. }
  198. /**
  199. * @param string $sql Query
  200. *
  201. * @return bool true if query ran fine, false otherwise
  202. */
  203. public function simpleQuery($sql)
  204. {
  205. if (null == $this->db) {
  206. $this->connect();
  207. }
  208. return $this->db->simpleQuery($sql);
  209. }
  210. /**
  211. * @param string $sql Query with return of affected rows
  212. *
  213. * @return int number of affected rows
  214. */
  215. public function exec($sql)
  216. {
  217. if (null == $this->db) {
  218. $this->connect();
  219. }
  220. $this->db->simpleQuery($sql);
  221. return $this->db->getAffectedRows();
  222. }
  223. }