MysqliConnection.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 MIT license. 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. /**
  31. * @param array $params
  32. * @param string $username
  33. * @param string $password
  34. * @param array $driverOptions
  35. *
  36. * @throws \Doctrine\DBAL\Driver\Mysqli\MysqliException
  37. */
  38. public function __construct(array $params, $username, $password, array $driverOptions = array())
  39. {
  40. $port = isset($params['port']) ? $params['port'] : ini_get('mysqli.default_port');
  41. $socket = isset($params['unix_socket']) ? $params['unix_socket'] : ini_get('mysqli.default_socket');
  42. $this->_conn = mysqli_init();
  43. if ( ! $this->_conn->real_connect($params['host'], $username, $password, $params['dbname'], $port, $socket)) {
  44. throw new MysqliException($this->_conn->connect_error, $this->_conn->connect_errno);
  45. }
  46. if (isset($params['charset'])) {
  47. $this->_conn->set_charset($params['charset']);
  48. }
  49. }
  50. /**
  51. * Retrieves mysqli native resource handle.
  52. *
  53. * Could be used if part of your application is not using DBAL.
  54. *
  55. * @return \mysqli
  56. */
  57. public function getWrappedResourceHandle()
  58. {
  59. return $this->_conn;
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function prepare($prepareString)
  65. {
  66. return new MysqliStatement($this->_conn, $prepareString);
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function query()
  72. {
  73. $args = func_get_args();
  74. $sql = $args[0];
  75. $stmt = $this->prepare($sql);
  76. $stmt->execute();
  77. return $stmt;
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function quote($input, $type=\PDO::PARAM_STR)
  83. {
  84. return "'". $this->_conn->escape_string($input) ."'";
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function exec($statement)
  90. {
  91. $this->_conn->query($statement);
  92. return $this->_conn->affected_rows;
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function lastInsertId($name = null)
  98. {
  99. return $this->_conn->insert_id;
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function beginTransaction()
  105. {
  106. $this->_conn->query('START TRANSACTION');
  107. return true;
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function commit()
  113. {
  114. return $this->_conn->commit();
  115. }
  116. /**
  117. * {@inheritdoc}non-PHPdoc)
  118. */
  119. public function rollBack()
  120. {
  121. return $this->_conn->rollback();
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function errorCode()
  127. {
  128. return $this->_conn->errno;
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function errorInfo()
  134. {
  135. return $this->_conn->error;
  136. }
  137. }