MysqliConnection.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Driver\Mysqli;
  20. use Doctrine\DBAL\Driver\Connection as Connection;
  21. /**
  22. * @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
  23. */
  24. class MysqliConnection implements Connection
  25. {
  26. /**
  27. * @var \mysqli
  28. */
  29. private $_conn;
  30. public function __construct(array $params, $username, $password, array $driverOptions = array())
  31. {
  32. $port = isset($params['port']) ? $params['port'] : ini_get('mysqli.default_port');
  33. $socket = isset($params['unix_socket']) ? $params['unix_socket'] : ini_get('mysqli.default_socket');
  34. $this->_conn = mysqli_init();
  35. if (!$this->_conn->real_connect($params['host'], $username, $password, $params['dbname'], $port, $socket)) {
  36. throw new MysqliException($this->_conn->connect_error, $this->_conn->connect_errno);
  37. }
  38. if (isset($params['charset'])) {
  39. $this->_conn->set_charset($params['charset']);
  40. }
  41. }
  42. /**
  43. * Retrieve mysqli native resource handle.
  44. *
  45. * Could be used if part of your application is not using DBAL
  46. *
  47. * @return mysqli
  48. */
  49. public function getWrappedResourceHandle()
  50. {
  51. return $this->_conn;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function prepare($prepareString)
  57. {
  58. return new MysqliStatement($this->_conn, $prepareString);
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function query()
  64. {
  65. $args = func_get_args();
  66. $sql = $args[0];
  67. $stmt = $this->prepare($sql);
  68. $stmt->execute();
  69. return $stmt;
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function quote($input, $type=\PDO::PARAM_STR)
  75. {
  76. return "'". $this->_conn->escape_string($input) ."'";
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function exec($statement)
  82. {
  83. $this->_conn->query($statement);
  84. return $this->_conn->affected_rows;
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function lastInsertId($name = null)
  90. {
  91. return $this->_conn->insert_id;
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function beginTransaction()
  97. {
  98. $this->_conn->query('START TRANSACTION');
  99. return true;
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function commit()
  105. {
  106. return $this->_conn->commit();
  107. }
  108. /**
  109. * {@inheritdoc}non-PHPdoc)
  110. */
  111. public function rollBack()
  112. {
  113. return $this->_conn->rollback();
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function errorCode()
  119. {
  120. return $this->_conn->errno;
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function errorInfo()
  126. {
  127. return $this->_conn->error;
  128. }
  129. }