PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/db/DbMySQLi.php

https://github.com/netplayer/PrestaShop
PHP | 251 lines | 145 code | 30 blank | 76 comment | 23 complexity | fac6b68536a7c352fd06f063c9b845bc MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /*
  3. * 2007-2014 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2014 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. /**
  27. * @since 1.5.0
  28. */
  29. class DbMySQLiCore extends Db
  30. {
  31. /**
  32. * @see DbCore::connect()
  33. */
  34. public function connect()
  35. {
  36. if (strpos($this->server, ':') !== false)
  37. {
  38. list($server, $port) = explode(':', $this->server);
  39. $this->link = @new mysqli($server, $this->user, $this->password, $this->database, $port);
  40. }
  41. else
  42. $this->link = @new mysqli($this->server, $this->user, $this->password, $this->database);
  43. // Do not use object way for error because this work bad before PHP 5.2.9
  44. if (mysqli_connect_error())
  45. throw new PrestaShopDatabaseException(sprintf(Tools::displayError('Link to database cannot be established: %s'), mysqli_connect_error()));
  46. // UTF-8 support
  47. if (!$this->link->query('SET NAMES \'utf8\''))
  48. throw new PrestaShopDatabaseException(Tools::displayError('PrestaShop Fatal error: no utf-8 support. Please check your server configuration.'));
  49. return $this->link;
  50. }
  51. public static function createDatabase($host, $user, $password, $dbname, $dropit = false)
  52. {
  53. if (strpos($host, ':') !== false)
  54. {
  55. list($host, $port) = explode(':', $host);
  56. $link = @new mysqli($host, $this->user, $this->password, null, $port);
  57. }
  58. else
  59. $link = @new mysqli($host, $user, $password);
  60. $success = $link->query('CREATE DATABASE `'.str_replace('`', '\\`', $dbname).'`');
  61. if ($dropit && ($link->query('DROP DATABASE `'.str_replace('`', '\\`', $dbname).'`') !== false))
  62. return true;
  63. return $success;
  64. }
  65. /**
  66. * @see DbCore::disconnect()
  67. */
  68. public function disconnect()
  69. {
  70. @$this->link->close();
  71. }
  72. /**
  73. * @see DbCore::_query()
  74. */
  75. protected function _query($sql)
  76. {
  77. return $this->link->query($sql);
  78. }
  79. /**
  80. * @see DbCore::nextRow()
  81. */
  82. public function nextRow($result = false)
  83. {
  84. if (!$result)
  85. $result = $this->result;
  86. if (!is_object($result))
  87. return false;
  88. return $result->fetch_assoc();
  89. }
  90. /**
  91. * @see DbCore::_numRows()
  92. */
  93. protected function _numRows($result)
  94. {
  95. return $result->num_rows;
  96. }
  97. /**
  98. * @see DbCore::Insert_ID()
  99. */
  100. public function Insert_ID()
  101. {
  102. return $this->link->insert_id;
  103. }
  104. /**
  105. * @see DbCore::Affected_Rows()
  106. */
  107. public function Affected_Rows()
  108. {
  109. return $this->link->affected_rows;
  110. }
  111. /**
  112. * @see DbCore::getMsgError()
  113. */
  114. public function getMsgError($query = false)
  115. {
  116. return $this->link->error;
  117. }
  118. /**
  119. * @see DbCore::getNumberError()
  120. */
  121. public function getNumberError()
  122. {
  123. return $this->link->errno;
  124. }
  125. /**
  126. * @see DbCore::getVersion()
  127. */
  128. public function getVersion()
  129. {
  130. return $this->getValue('SELECT VERSION()');
  131. }
  132. /**
  133. * @see DbCore::_escape()
  134. */
  135. public function _escape($str)
  136. {
  137. return $this->link->real_escape_string($str);
  138. }
  139. /**
  140. * @see DbCore::set_db()
  141. */
  142. public function set_db($db_name)
  143. {
  144. return $this->link->query('USE `'.bqSQL($db_name).'`');
  145. }
  146. /**
  147. * @see Db::hasTableWithSamePrefix()
  148. */
  149. public static function hasTableWithSamePrefix($server, $user, $pwd, $db, $prefix)
  150. {
  151. $link = @new mysqli($server, $user, $pwd, $db);
  152. if (mysqli_connect_error())
  153. return false;
  154. $sql = 'SHOW TABLES LIKE \''.$prefix.'%\'';
  155. $result = $link->query($sql);
  156. return (bool)$result->fetch_assoc();
  157. }
  158. /**
  159. * @see Db::checkConnection()
  160. */
  161. public static function tryToConnect($server, $user, $pwd, $db, $newDbLink = true, $engine = null, $timeout = 5)
  162. {
  163. $link = mysqli_init();
  164. if (!$link)
  165. return -1;
  166. if (!$link->options(MYSQLI_OPT_CONNECT_TIMEOUT, $timeout))
  167. return 1;
  168. // There is an @ because mysqli throw a warning when the database does not exists
  169. if (!@$link->real_connect($server, $user, $pwd, $db))
  170. return (mysqli_connect_errno() == 1049) ? 2 : 1;
  171. $link->close();
  172. return 0;
  173. }
  174. public function getBestEngine()
  175. {
  176. $value = 'InnoDB';
  177. $sql = 'SHOW VARIABLES WHERE Variable_name = \'have_innodb\'';
  178. $result = $this->link->query($sql);
  179. if (!$result)
  180. $value = 'MyISAM';
  181. $row = $result->fetch_assoc();
  182. if (!$row || strtolower($row['Value']) != 'yes')
  183. $value = 'MyISAM';
  184. /* MySQL >= 5.6 */
  185. $sql = 'SHOW ENGINES';
  186. $result = $this->link->query($sql);
  187. while ($row = $result->fetch_assoc())
  188. if ($row['Engine'] == 'InnoDB')
  189. {
  190. if (in_array($row['Support'], array('DEFAULT', 'YES')))
  191. $value = 'InnoDB';
  192. break;
  193. }
  194. return $value;
  195. }
  196. public static function checkCreatePrivilege($server, $user, $pwd, $db, $prefix, $engine = null)
  197. {
  198. $link = @new mysqli($server, $user, $pwd, $db);
  199. if (mysqli_connect_error())
  200. return false;
  201. $sql = '
  202. CREATE TABLE `'.$prefix.'test` (
  203. `test` tinyint(1) unsigned NOT NULL
  204. ) ENGINE=MyISAM';
  205. $result = $link->query($sql);
  206. if (!$result)
  207. return $link->error;
  208. $link->query('DROP TABLE `'.$prefix.'test`');
  209. return true;
  210. }
  211. /**
  212. * @see Db::checkEncoding()
  213. */
  214. static public function tryUTF8($server, $user, $pwd)
  215. {
  216. $link = @new mysqli($server, $user, $pwd, $db);
  217. $ret = $link->query("SET NAMES 'UTF8'");
  218. $link->close();
  219. return $ret;
  220. }
  221. }