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.

103 lines
2.7 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 DbDriver
  15. {
  16. /**
  17. * @param mixed $sql
  18. * @return DbStatement
  19. */
  20. public function prepare($sql)
  21. {
  22. return new MySQLiStatement($this, $sql);
  23. }
  24. public function getInsertId($table = null, $key = null)
  25. {
  26. return $this->connection->insert_id;
  27. }
  28. public function isConnected()
  29. {
  30. return ($this->connection instanceof MySQLi);
  31. }
  32. public function disconnect()
  33. {
  34. if ($this->isConnected()) {
  35. $this->connection->close();
  36. }
  37. $this->connection = null;
  38. }
  39. protected function connect()
  40. {
  41. if ($this->connection) {
  42. return;
  43. }
  44. $port = isset($this->config['port']) ? (int) $this->config['port'] : null;
  45. $this->connection = mysqli_init();
  46. $connected = @mysqli_real_connect($this->connection,
  47. $this->config['hostname'],
  48. $this->config['username'],
  49. $this->config['password'],
  50. $this->config['database'],
  51. $port);
  52. // Connection errors check
  53. if (mysqli_connect_error()) {
  54. throw new Exception(mysqli_connect_error(), mysqli_connect_errno());
  55. }
  56. $charset = (!empty($this->config['charset'])) ? $this->config['charset'] : 'utf8';
  57. $this->connection->set_charset($charset);
  58. }
  59. protected function driverQuote($value)
  60. {
  61. if (is_int($value) || is_float($value)) {
  62. return $value;
  63. }
  64. if (is_bool($value)) {
  65. var_dump($value);
  66. return (int) $value;
  67. }
  68. if ($value === null) {
  69. return 'NULL';
  70. }
  71. $this->connect();
  72. return '\'' . $this->connection->real_escape_string($value) . '\'';
  73. }
  74. protected function driverBeginTransaction()
  75. {
  76. $this->connect();
  77. $this->connection->autocommit(false);
  78. }
  79. protected function driverCommitTransaction()
  80. {
  81. $this->connection->commit();
  82. $this->connection->autocommit(true);
  83. }
  84. protected function driverRollbackTransaction()
  85. {
  86. $this->connection->rollback();
  87. $this->connection->autocommit(true);
  88. }
  89. }