MySQLi.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /** @package verysimple::DB::DataDriver */
  3. require_once("IDataDriver.php");
  4. require_once("verysimple/DB/ISqlFunction.php");
  5. require_once("verysimple/DB/DatabaseException.php");
  6. require_once("verysimple/DB/DatabaseConfig.php");
  7. /**
  8. * An implementation of IDataDriver that communicates with
  9. * a MySQL server. This is one of the native drivers
  10. * supported by Phreeze
  11. *
  12. * @package verysimple::DB::DataDriver
  13. * @author VerySimple Inc. <[email protected]>
  14. * @copyright 1997-2010 VerySimple Inc.
  15. * @license http://www.gnu.org/licenses/lgpl.html LGPL
  16. * @version 1.0
  17. */
  18. class DataDriverMySQLi implements IDataDriver
  19. {
  20. /** @var characters that will be escaped */
  21. static $BAD_CHARS = array("\\","\0","\n","\r","\x1a","'",'"');
  22. /** @var characters that will be used to replace bad chars */
  23. static $GOOD_CHARS = array("\\\\","\\0","\\n","\\r","\Z","\'",'\"');
  24. /**
  25. * @inheritdocs
  26. */
  27. function GetServerType()
  28. {
  29. return "MySQLi";
  30. }
  31. function Ping($connection)
  32. {
  33. return mysqli_ping($connection);
  34. }
  35. /**
  36. * @inheritdocs
  37. */
  38. function Open($connectionstring,$database,$username,$password,$charset='',$bootstrap='')
  39. {
  40. if (!function_exists("mysqli_connect")) throw new DatabaseException('mysqli extension is not enabled on this server.',DatabaseException::$CONNECTION_ERROR);
  41. // if the port is provided in the connection string then strip it out and provide it as a separate param
  42. $hostAndPort = explode(":",$connectionstring);
  43. $host = $hostAndPort[0];
  44. $port = count($hostAndPort) > 1 ? $hostAndPort[1] : null;
  45. $connection = mysqli_connect($host, $username, $password, $database, $port);
  46. if ( mysqli_connect_errno() )
  47. {
  48. throw new DatabaseException("Error connecting to database: " . mysqli_connect_error(),DatabaseException::$CONNECTION_ERROR);
  49. }
  50. if ($charset)
  51. {
  52. mysqli_set_charset($connection,$charset);
  53. if ( mysqli_connect_errno() )
  54. {
  55. throw new DatabaseException("Unable to set charset: " . mysqli_connect_error(),DatabaseException::$CONNECTION_ERROR);
  56. }
  57. }
  58. if ($bootstrap)
  59. {
  60. $statements = explode(';',$bootstrap);
  61. foreach ($statements as $sql)
  62. {
  63. try
  64. {
  65. $this->Execute($connection, $sql);
  66. }
  67. catch (Exception $ex)
  68. {
  69. throw new DatabaseException("problem with bootstrap sql: " . $ex->getMessage(),DatabaseException::$ERROR_IN_QUERY);
  70. }
  71. }
  72. }
  73. return $connection;
  74. }
  75. /**
  76. * @inheritdocs
  77. */
  78. function Close($connection)
  79. {
  80. @mysqli_close($connection); // ignore warnings
  81. }
  82. /**
  83. * @inheritdocs
  84. */
  85. function Query($connection,$sql)
  86. {
  87. if ( !$rs = @mysqli_query($connection,$sql) )
  88. {
  89. throw new DatabaseException(mysqli_error($connection),DatabaseException::$ERROR_IN_QUERY);
  90. }
  91. return $rs;
  92. }
  93. /**
  94. * @inheritdocs
  95. */
  96. function Execute($connection,$sql)
  97. {
  98. if ( !$result = @mysqli_query($connection,$sql) )
  99. {
  100. throw new DatabaseException(mysqli_error($connection),DatabaseException::$ERROR_IN_QUERY);
  101. }
  102. return mysqli_affected_rows($connection);
  103. }
  104. /**
  105. * @inheritdocs
  106. */
  107. function Fetch($connection,$rs)
  108. {
  109. return mysqli_fetch_assoc($rs);
  110. }
  111. /**
  112. * @inheritdocs
  113. */
  114. function GetLastInsertId($connection)
  115. {
  116. return (mysqli_insert_id($connection));
  117. }
  118. /**
  119. * @inheritdocs
  120. */
  121. function GetLastError($connection)
  122. {
  123. return mysqli_error($connection);
  124. }
  125. /**
  126. * @inheritdocs
  127. */
  128. function Release($connection,$rs)
  129. {
  130. mysqli_free_result($rs);
  131. }
  132. /**
  133. * @inheritdocs
  134. * this method currently uses replacement and not mysqli_real_escape_string
  135. * so that a database connection is not necessary in order to escape.
  136. * this way cached queries can be used without connecting to the DB server
  137. */
  138. function Escape($val)
  139. {
  140. return str_replace(self::$BAD_CHARS, self::$GOOD_CHARS, $val);
  141. // return mysqli_real_escape_string($val);
  142. }
  143. /**
  144. * @inheritdocs
  145. */
  146. public function GetQuotedSql($val)
  147. {
  148. if ($val === null) return DatabaseConfig::$CONVERT_NULL_TO_EMPTYSTRING ? "''" : 'NULL';
  149. if ($val instanceof ISqlFunction) return $val->GetQuotedSql($this);
  150. return "'" . $this->Escape($val) . "'";
  151. }
  152. /**
  153. * @inheritdocs
  154. */
  155. function GetTableNames($connection, $dbname, $ommitEmptyTables = false)
  156. {
  157. $sql = "SHOW TABLE STATUS FROM `" . $this->Escape($dbname) . "`";
  158. $rs = $this->Query($connection,$sql);
  159. $tables = array();
  160. while ( $row = $this->Fetch($connection,$rs) )
  161. {
  162. if ( $ommitEmptyTables == false || $rs['Data_free'] > 0 )
  163. {
  164. $tables[] = $row['Name'];
  165. }
  166. }
  167. return $tables;
  168. }
  169. /**
  170. * @inheritdocs
  171. */
  172. function Optimize($connection,$table)
  173. {
  174. $result = "";
  175. $rs = $this->Query($connection,"optimize table `". $this->Escape($table)."`");
  176. while ( $row = $this->Fetch($connection,$rs) )
  177. {
  178. $tbl = $row['Table'];
  179. if (!isset($results[$tbl])) $results[$tbl] = "";
  180. $result .= trim($results[$tbl] . " " . $row['Msg_type'] . "=\"" . $row['Msg_text'] . "\"");
  181. }
  182. return $result;
  183. }
  184. /**
  185. * @inheritdocs
  186. */
  187. function StartTransaction($connection)
  188. {
  189. $this->Execute($connection, "SET AUTOCOMMIT=0");
  190. $this->Execute($connection, "START TRANSACTION");
  191. }
  192. /**
  193. * @inheritdocs
  194. */
  195. function CommitTransaction($connection)
  196. {
  197. $this->Execute($connection, "COMMIT");
  198. $this->Execute($connection, "SET AUTOCOMMIT=1");
  199. }
  200. /**
  201. * @inheritdocs
  202. */
  203. function RollbackTransaction($connection)
  204. {
  205. $this->Execute($connection, "ROLLBACK");
  206. $this->Execute($connection, "SET AUTOCOMMIT=1");
  207. }
  208. }
  209. ?>