PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/gulliver/thirdparty/creole/drivers/mysqli/MySQLiConnection.php

https://bitbucket.org/ferOnti/processmaker
PHP | 293 lines | 162 code | 45 blank | 86 comment | 29 complexity | 75cd599dd05340d9e6b2d4d9abb0447c MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: MySQLiConnection.php,v 1.7 2004/09/18 09:29:22 sb Exp $
  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 please see
  19. * <http://creole.phpdb.org>.
  20. */
  21. require_once 'creole/Connection.php';
  22. require_once 'creole/common/ConnectionCommon.php';
  23. include_once 'creole/drivers/mysqli/MySQLiResultSet.php';
  24. /**
  25. * MySQLi implementation of Connection.
  26. *
  27. *
  28. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  29. * @version $Revision: 1.7 $
  30. * @package creole.drivers.mysqli
  31. */
  32. class MySQLiConnection extends ConnectionCommon implements Connection {
  33. /** Current database (used in mysqli_select_db()). */
  34. private $database;
  35. /**
  36. * Connect to a database and log in as the specified user.
  37. *
  38. * @param $dsn the data source name (see DB::parseDSN for syntax)
  39. * @param $flags Any conneciton flags.
  40. * @access public
  41. * @throws SQLException
  42. * @return void
  43. */
  44. public function connect($dsninfo, $flags = 0)
  45. {
  46. if (!extension_loaded('mysqli')) {
  47. throw new SQLException('mysqli extension not loaded');
  48. }
  49. $this->dsn = $dsninfo;
  50. $this->flags = $flags;
  51. $dbhost = null;
  52. if (isset($dsninfo['protocol']) && $dsninfo['protocol'] == 'unix') {
  53. $dbhost = ':' . $dsninfo['socket'];
  54. } else {
  55. $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost';
  56. if (!empty($dsninfo['port'])) {
  57. $dbhost .= ':' . $dsninfo['port'];
  58. }
  59. }
  60. $host = !empty($dsninfo['hostspec']) ? $dsninfo['hostspec'] : null;
  61. $user = !empty($dsninfo['username']) ? $dsninfo['username'] : null;
  62. $pw = !empty($dsninfo['password']) ? $dsninfo['password'] : null;
  63. $port = !empty($dsninfo['port']) ? $dsninfo['port'] : null;
  64. $socket = !empty($dsninfo['socket']) ? $dsninfo['socket'] : null;
  65. $database = !empty($dsninfo['database']) ? $dsninfo['database'] : null;
  66. $encoding = !empty($dsninfo['encoding']) ? $dsninfo['encoding'] : null;
  67. @ini_set('track_errors', true);
  68. $conn = mysqli_connect($host, $user, $pw, $database, $port, $socket);
  69. @ini_restore('track_errors');
  70. if (empty($conn)) {
  71. if (($err = @mysqli_error()) != '') {
  72. throw new SQLException("connect failed", $err);
  73. } elseif (empty($php_errormsg)) {
  74. throw new SQLException("connect failed");
  75. } else {
  76. throw new SQLException("connect failed", $php_errormsg);
  77. }
  78. }
  79. if ($dsninfo['database']) {
  80. if (!@mysqli_select_db($conn, $dsninfo['database'])) {
  81. switch(mysqli_errno($conn)) {
  82. case 1049:
  83. $exc = new SQLException("no such database", mysqli_error($conn));
  84. break;
  85. case 1044:
  86. $exc = new SQLException("access violation", mysqli_error($conn));
  87. break;
  88. default:
  89. $exc = new SQLException("cannot select database", mysqli_error($conn));
  90. }
  91. throw $exc;
  92. }
  93. // fix to allow calls to different databases in the same script
  94. $this->database = $dsninfo['database'];
  95. }
  96. $this->dblink = $conn;
  97. if ($encoding) {
  98. $this->executeUpdate("SET NAMES " . $encoding);
  99. }
  100. }
  101. /**
  102. * @see Connection::getDatabaseInfo()
  103. */
  104. public function getDatabaseInfo()
  105. {
  106. require_once 'creole/drivers/mysqli/metadata/MySQLiDatabaseInfo.php';
  107. return new MySQLiDatabaseInfo($this);
  108. }
  109. /**
  110. * @see Connection::getIdGenerator()
  111. */
  112. public function getIdGenerator()
  113. {
  114. require_once 'creole/drivers/mysqli/MySQLiIdGenerator.php';
  115. return new MySQLiIdGenerator($this);
  116. }
  117. /**
  118. * @see Connection::prepareStatement()
  119. */
  120. public function prepareStatement($sql)
  121. {
  122. require_once 'creole/drivers/mysqli/MySQLiPreparedStatement.php';
  123. return new MySQLiPreparedStatement($this, $sql);
  124. }
  125. /**
  126. * @see Connection::prepareCall()
  127. */
  128. public function prepareCall($sql) {
  129. throw new SQLException('MySQL does not support stored procedures.');
  130. }
  131. /**
  132. * @see Connection::createStatement()
  133. */
  134. public function createStatement()
  135. {
  136. require_once 'creole/drivers/mysqli/MySQLiStatement.php';
  137. return new MySQLiStatement($this);
  138. }
  139. /**
  140. * @see Connection::disconnect()
  141. */
  142. public function close()
  143. {
  144. $ret = mysqli_close($this->dblink);
  145. $this->dblink = null;
  146. return $ret;
  147. }
  148. /**
  149. * @see Connection::applyLimit()
  150. */
  151. public function applyLimit(&$sql, $offset, $limit)
  152. {
  153. if ( $limit > 0 ) {
  154. $sql .= " LIMIT " . ($offset > 0 ? $offset . ", " : "") . $limit;
  155. } else if ( $offset > 0 ) {
  156. $sql .= " LIMIT " . $offset . ", 18446744073709551615";
  157. }
  158. }
  159. /**
  160. * @see Connection::executeQuery()
  161. */
  162. public function executeQuery($sql, $fetchmode = null)
  163. {
  164. $this->lastQuery = $sql;
  165. if ($this->database) {
  166. if (!@mysqli_select_db($this->dblink, $this->database)) {
  167. throw new SQLException('No database selected', mysqli_error($this->dblink));
  168. }
  169. }
  170. $result = @mysqli_query($this->dblink, $sql);
  171. if (!$result) {
  172. throw new SQLException('Could not execute query', mysqli_error($this->dblink), $sql);
  173. }
  174. return new MySQLiResultSet($this, $result, $fetchmode);
  175. }
  176. /**
  177. * @see Connection::executeUpdate()
  178. */
  179. public function executeUpdate($sql)
  180. {
  181. $this->lastQuery = $sql;
  182. if ($this->database) {
  183. if (!@mysqli_select_db($this->dblink, $this->database)) {
  184. throw new SQLException('No database selected', mysqli_error($this->dblink));
  185. }
  186. }
  187. $result = @mysqli_query($this->dblink, $sql);
  188. if (!$result) {
  189. throw new SQLException('Could not execute update', mysqli_error($this->dblink), $sql);
  190. }
  191. return (int) mysqli_affected_rows($this->dblink);
  192. }
  193. /**
  194. * Start a database transaction.
  195. * @throws SQLException
  196. * @return void
  197. */
  198. protected function beginTrans()
  199. {
  200. if (!mysqli_autocommit($this->dblink, FALSE)) {
  201. throw new SQLException('Could not begin transaction', mysqli_error($this->dblink));
  202. }
  203. }
  204. /**
  205. * Commit the current transaction.
  206. * @throws SQLException
  207. * @return void
  208. */
  209. protected function commitTrans()
  210. {
  211. if ($this->database) {
  212. if (!@mysqli_select_db($this->dblink, $this->database)) {
  213. throw new SQLException('No database selected', mysqli_error($this->dblink));
  214. }
  215. }
  216. if (!mysqli_commit($this->dblink)) {
  217. throw new SQLException('Can not commit transaction', mysqli_error($this->dblink));
  218. }
  219. mysqli_autocommit($this->dblink, TRUE);
  220. }
  221. /**
  222. * Roll back (undo) the current transaction.
  223. * @throws SQLException
  224. * @return void
  225. */
  226. protected function rollbackTrans()
  227. {
  228. if ($this->database) {
  229. if (!@mysqli_select_db($this->dblink, $this->database)) {
  230. throw new SQLException('No database selected', mysqli_error($this->dblink));
  231. }
  232. }
  233. if (!mysqli_rollback($this->dblink)) {
  234. throw new SQLException('Could not rollback transaction', mysqli_error($this->dblink));
  235. }
  236. mysqli_autocommit($this->dblink, TRUE);
  237. }
  238. /**
  239. * Gets the number of rows affected by the data manipulation
  240. * query.
  241. *
  242. * @return int Number of rows affected by the last query.
  243. */
  244. public function getUpdateCount()
  245. {
  246. return (int) @mysqli_affected_rows($this->dblink);
  247. }
  248. }