# Source for file Mysqli.php

Documentation is available at Mysqli.php

1. <?php
2. /*
3. * $Id: Mock.php 1080 2007-02-10 18:17:08Z romanb $
4. *
5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16. *
17. * This software consists of voluntary contributions made by many individuals
18. * and is licensed under the LGPL. For more information, see
19. * <http://www.phpdoctrine.com>.
20. */
21. /**
22. * Doctrine_Adapter_Mysqli
23. * This class is used for special testing purposes.
24. *
25. * @package Doctrine
26. * @subpackage Doctrine_Adapter
27. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
28. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
29. * @category Object Relational Mapping
30. * @link www.phpdoctrine.com
31. * @since 1.0
32. * @version $Revision: 1080 $
33. */
34. class Doctrine_Adapter_Mysqli extends Doctrine_Adapter
35. {
36. /**
37. * Creates a connection to the database.
38. *
39. * @return void
40. * @throws Doctrine_Adapter_Mysqli_Exception
41. */
42. protected function _connect()
43. {
44. if ($this->_connection) {
45. return;
46. }
47. // Suppress connection warnings here.
48. // Throw an exception instead.
49. @$this->_connection = new mysqli(
50. $this->_config['host'],
51. $this->_config['username'],
52. $this->_config['password'],
53. $this->_config['dbname']
54. );
55. if ($this->_connection === false || mysqli_connect_errno()) {
56. throw new Doctrine_Adapter_Mysqli_Exception(mysqli_connect_error());
57. }
58. }
60. /**
61. * Force the connection to close.
62. *
63. * @return void
64. */
65. public function closeConnection()
66. {
67. $this->_connection->close();
68. $this->_connection = null;
69. }
71. /**
72. * Prepare a statement and return a PDOStatement-like object.
73. *
74. * @param string $sql SQL query
75. * @return Doctrine_Statement_Mysqli
76. */
77. public function prepare($sql)
78. {
79. $this->_connect();
80. $stmt = new Doctrine_Statement_Mysqli($this, $sql);
81. $stmt->setFetchMode($this->_fetchMode);
82. return $stmt;
83. }
85. /**
86. * Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT column.
87. *
88. * As a convention, on RDBMS brands that support sequences
89. * (e.g. Oracle, PostgreSQL, DB2), this method forms the name of a sequence
90. * from the arguments and returns the last id generated by that sequence.
91. * On RDBMS brands that support IDENTITY/AUTOINCREMENT columns, this method
92. * returns the last value generated for such a column, and the table name
93. * argument is disregarded.
94. *
95. * MySQL does not support sequences, so $tableName and $primaryKey are ignored.
96. *
97. * @param string $tableName OPTIONAL Name of table.
98. * @param string $primaryKey OPTIONAL Name of primary key column.
99. * @return integer
100. */
101. public function lastInsertId($tableName = null, $primaryKey = null)
102. {
103. $mysqli = $this->_connection;
104. return $mysqli->insert_id;
105. }
107. /**
108. * Begin a transaction.
109. *
110. * @return void
111. */
112. protected function _beginTransaction()
113. {
114. $this->_connect();
115. $this->_connection->autocommit(false);
116. }
118. /**
119. * Commit a transaction.
120. *
121. * @return void
122. */
123. protected function _commit()
124. {
125. $this->_connect();
126. $this->_connection->commit();
127. $this->_connection->autocommit(true);
128. }
130. /**
131. * Roll-back a transaction.
132. *
133. * @return void
134. */
135. protected function _rollBack()
136. {
137. $this->_connect();
138. $this->_connection->rollback();
139. $this->_connection->autocommit(true);
140. }
141. }