PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/classes/db/DbPDO.php

https://github.com/netplayer/PrestaShop
PHP | 266 lines | 189 code | 16 blank | 61 comment | 8 complexity | 6486b41f7fc456f34380ba09571c965a 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 DbPDOCore extends Db
  30. {
  31. protected static function _getPDO($host, $user, $password, $dbname, $timeout = 5)
  32. {
  33. $dsn = 'mysql:';
  34. if ($dbname)
  35. $dsn .= 'dbname='.$dbname.';';
  36. if (preg_match('/^(.*):([0-9]+)$/', $host, $matches))
  37. $dsn .= 'host='.$matches[1].';port='.$matches[2];
  38. elseif (preg_match('#^.*:(/.*)$#', $host, $matches))
  39. $dsn .= 'unix_socket='.$matches[1];
  40. else
  41. $dsn .= 'host='.$host;
  42. return new PDO($dsn, $user, $password, array(PDO::ATTR_TIMEOUT => $timeout, PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true));
  43. }
  44. public static function createDatabase($host, $user, $password, $dbname, $dropit = false)
  45. {
  46. try {
  47. $link = DbPDO::_getPDO($host, $user, $password, false);
  48. $success = $link->exec('CREATE DATABASE `'.str_replace('`', '\\`', $dbname).'`');
  49. if ($dropit && ($link->exec('DROP DATABASE `'.str_replace('`', '\\`', $dbname).'`') !== false))
  50. return true;
  51. } catch (PDOException $e) {
  52. return false;
  53. }
  54. return $success;
  55. }
  56. /**
  57. * @see DbCore::connect()
  58. */
  59. public function connect()
  60. {
  61. try {
  62. $this->link = $this->_getPDO($this->server, $this->user, $this->password, $this->database, 5);
  63. } catch (PDOException $e) {
  64. die(sprintf(Tools::displayError('Link to database cannot be established: %s'), utf8_encode($e->getMessage())));
  65. }
  66. // UTF-8 support
  67. if ($this->link->exec('SET NAMES \'utf8\'') === false)
  68. die(Tools::displayError('PrestaShop Fatal error: no utf-8 support. Please check your server configuration.'));
  69. return $this->link;
  70. }
  71. /**
  72. * @see DbCore::disconnect()
  73. */
  74. public function disconnect()
  75. {
  76. unset($this->link);
  77. }
  78. /**
  79. * @see DbCore::_query()
  80. */
  81. protected function _query($sql)
  82. {
  83. return $this->link->query($sql);
  84. }
  85. /**
  86. * @see DbCore::nextRow()
  87. */
  88. public function nextRow($result = false)
  89. {
  90. if (!$result)
  91. $result = $this->result;
  92. if (!is_object($result))
  93. return false;
  94. return $result->fetch(PDO::FETCH_ASSOC);
  95. }
  96. /**
  97. * @see DbCore::_numRows()
  98. */
  99. protected function _numRows($result)
  100. {
  101. return $result->rowCount();
  102. }
  103. /**
  104. * @see DbCore::Insert_ID()
  105. */
  106. public function Insert_ID()
  107. {
  108. return $this->link->lastInsertId();
  109. }
  110. /**
  111. * @see DbCore::Affected_Rows()
  112. */
  113. public function Affected_Rows()
  114. {
  115. return $this->result->rowCount();
  116. }
  117. /**
  118. * @see DbCore::getMsgError()
  119. */
  120. public function getMsgError($query = false)
  121. {
  122. $error = $this->link->errorInfo();
  123. return ($error[0] == '00000') ? '' : $error[2];
  124. }
  125. /**
  126. * @see DbCore::getNumberError()
  127. */
  128. public function getNumberError()
  129. {
  130. $error = $this->link->errorInfo();
  131. return isset($error[1]) ? $error[1] : 0;
  132. }
  133. /**
  134. * @see DbCore::getVersion()
  135. */
  136. public function getVersion()
  137. {
  138. return $this->getValue('SELECT VERSION()');
  139. }
  140. /**
  141. * @see DbCore::_escape()
  142. */
  143. public function _escape($str)
  144. {
  145. $search = array("\\", "\0", "\n", "\r", "\x1a", "'", '"');
  146. $replace = array("\\\\", "\\0", "\\n", "\\r", "\Z", "\'", '\"');
  147. return str_replace($search, $replace, $str);
  148. }
  149. /**
  150. * @see DbCore::set_db()
  151. */
  152. public function set_db($db_name)
  153. {
  154. return $this->link->exec('USE '.pSQL($db_name));
  155. }
  156. /**
  157. * @see Db::hasTableWithSamePrefix()
  158. */
  159. public static function hasTableWithSamePrefix($server, $user, $pwd, $db, $prefix)
  160. {
  161. try {
  162. $link = DbPDO::_getPDO($server, $user, $pwd, $db, 5);
  163. } catch (PDOException $e) {
  164. return false;
  165. }
  166. $sql = 'SHOW TABLES LIKE \''.$prefix.'%\'';
  167. $result = $link->query($sql);
  168. return (bool)$result->fetch();
  169. }
  170. public static function checkCreatePrivilege($server, $user, $pwd, $db, $prefix, $engine = null)
  171. {
  172. try {
  173. $link = DbPDO::_getPDO($server, $user, $pwd, $db, 5);
  174. } catch (PDOException $e) {
  175. return false;
  176. }
  177. $sql = '
  178. CREATE TABLE `'.$prefix.'test` (
  179. `test` tinyint(1) unsigned NOT NULL
  180. ) ENGINE=MyISAM';
  181. $result = $link->query($sql);
  182. if (!$result)
  183. {
  184. $error = $link->errorInfo();
  185. return $error[2];
  186. }
  187. $link->query('DROP TABLE `'.$prefix.'test`');
  188. return true;
  189. }
  190. /**
  191. * @see Db::checkConnection()
  192. */
  193. public static function tryToConnect($server, $user, $pwd, $db, $newDbLink = true, $engine = null, $timeout = 5)
  194. {
  195. try {
  196. $link = DbPDO::_getPDO($server, $user, $pwd, $db, $timeout);
  197. } catch (PDOException $e) {
  198. return ($e->getCode() == 1049) ? 2 : 1;
  199. }
  200. unset($link);
  201. return 0;
  202. }
  203. public function getBestEngine()
  204. {
  205. $value = 'InnoDB';
  206. $sql = 'SHOW VARIABLES WHERE Variable_name = \'have_innodb\'';
  207. $result = $this->link->query($sql);
  208. if (!$result)
  209. $value = 'MyISAM';
  210. $row = $result->fetch();
  211. if (!$row || strtolower($row['Value']) != 'yes')
  212. $value = 'MyISAM';
  213. /* MySQL >= 5.6 */
  214. $sql = 'SHOW ENGINES';
  215. $result = $this->link->query($sql);
  216. while ($row = $result->fetch())
  217. if ($row['Engine'] == 'InnoDB')
  218. {
  219. if (in_array($row['Support'], array('DEFAULT', 'YES')))
  220. $value = 'InnoDB';
  221. break;
  222. }
  223. return $value;
  224. }
  225. /**
  226. * @see Db::checkEncoding()
  227. */
  228. public static function tryUTF8($server, $user, $pwd)
  229. {
  230. try {
  231. $link = DbPDO::_getPDO($server, $user, $pwd, false, 5);
  232. } catch (PDOException $e) {
  233. return false;
  234. }
  235. $result = $link->exec('SET NAMES \'utf8\'');
  236. unset($link);
  237. return ($result === false) ? false : true;
  238. }
  239. }