You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.1 KiB

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage db
  7. * @since 2010-02-17
  8. */
  9. /**
  10. * @property MySQLi $connection
  11. */
  12. class MySQLiDriver extends SqlDbDriver
  13. {
  14. public function insert($table, $bind, $on_duplicate = array())
  15. {
  16. $columns = array();
  17. foreach ($bind as $col => $val) {
  18. $columns[] = $this->quoteIdentifier($col);
  19. }
  20. $values = array_values($bind);
  21. if ($on_duplicate) {
  22. $update = array();
  23. foreach ($on_duplicate as $col => $val) {
  24. $update[] = $this->quoteIdentifier($col) . '=' . $this->quote($val);
  25. }
  26. $on_duplicate = ' ON DUPLICATE KEY UPDATE ' . implode(', ', $update);
  27. }
  28. $sql = 'INSERT INTO ' . $this->quoteIdentifier($table)
  29. . ' (' . implode(', ', $columns) . ') VALUES (' . $this->quote($values) . ')'
  30. . (($on_duplicate) ? $on_duplicate : '');
  31. return $this->query($sql)->affectedRows();
  32. }
  33. /**
  34. * @param mixed $sql
  35. * @return MySQLiStatement
  36. */
  37. public function prepare($sql)
  38. {
  39. return new MySQLiStatement($this, $sql);
  40. }
  41. public function getInsertId($table = null, $key = null)
  42. {
  43. return $this->connection->insert_id;
  44. }
  45. public function isConnected()
  46. {
  47. return ($this->connection instanceof MySQLi);
  48. }
  49. public function disconnect()
  50. {
  51. if ($this->isConnected()) {
  52. $this->connection->close();
  53. }
  54. $this->connection = null;
  55. }
  56. protected function connect()
  57. {
  58. if ($this->connection) {
  59. return;
  60. }
  61. $port = isset($this->config['port']) ? (int) $this->config['port'] : null;
  62. $this->connection = mysqli_init();
  63. @mysqli_real_connect($this->connection,
  64. $this->config['hostname'],
  65. $this->config['username'],
  66. $this->config['password'],
  67. $this->config['database'],
  68. $port);
  69. // Connection errors check
  70. if (mysqli_connect_error()) {
  71. throw new GeneralException(mysqli_connect_error(), mysqli_connect_errno());
  72. }
  73. $charset = (!empty($this->config['charset'])) ? $this->config['charset'] : 'utf8';
  74. $this->connection->set_charset($charset);
  75. }
  76. protected function driverQuote($value)
  77. {
  78. if (is_int($value) || is_float($value)) {
  79. return $value;
  80. }
  81. if (is_bool($value)) {
  82. return (int) $value;
  83. }
  84. if ($value === null) {
  85. return 'NULL';
  86. }
  87. $this->connect();
  88. return '\'' . $this->connection->real_escape_string($value) . '\'';
  89. }
  90. protected function driverBeginTransaction()
  91. {
  92. $this->connect();
  93. $this->connection->autocommit(false);
  94. }
  95. protected function driverCommitTransaction()
  96. {
  97. $this->connection->commit();
  98. $this->connection->autocommit(true);
  99. }
  100. protected function driverRollbackTransaction()
  101. {
  102. $this->connection->rollback();
  103. $this->connection->autocommit(true);
  104. }
  105. }