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.

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