MySQL.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 DataDriverMySQL 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 "MySQL";
  30. }
  31. function Ping($connection)
  32. {
  33. return mysql_ping($connection);
  34. }
  35. /**
  36. * @inheritdocs
  37. */
  38. function Open($connectionstring,$database,$username,$password,$charset='',$bootstrap='')
  39. {
  40. if (!function_exists("mysql_connect")) throw new DatabaseException('mysql extension is not enabled on this server.',DatabaseException::$CONNECTION_ERROR);
  41. if ( !$connection = @mysql_connect($connectionstring, $username, $password) )
  42. {
  43. throw new DatabaseException("Error connecting to database: " . mysql_error(),DatabaseException::$CONNECTION_ERROR);
  44. }
  45. if (!@mysql_select_db($database, $connection))
  46. {
  47. throw new DatabaseException("Unable to select database " . $database,DatabaseException::$CONNECTION_ERROR);
  48. }
  49. if ($charset && !@mysql_set_charset($charset,$connection))
  50. {
  51. throw new DatabaseException("Unable to set charset " . $charset,DatabaseException::$CONNECTION_ERROR);
  52. }
  53. if ($bootstrap)
  54. {
  55. $statements = explode(';',$bootstrap);
  56. foreach ($statements as $sql)
  57. {
  58. try
  59. {
  60. $this->Execute($connection, $sql);
  61. }
  62. catch (Exception $ex)
  63. {
  64. throw new DatabaseException("Connection Bootstrap Error: " . $ex->getMessage(),DatabaseException::$ERROR_IN_QUERY);
  65. }
  66. }
  67. }
  68. return $connection;
  69. }
  70. /**
  71. * @inheritdocs
  72. */
  73. function Close($connection)
  74. {
  75. @mysql_close($connection); // ignore warnings
  76. }
  77. /**
  78. * @inheritdocs
  79. */
  80. function Query($connection,$sql)
  81. {
  82. if ( !$rs = @mysql_query($sql, $connection) )
  83. {
  84. throw new DatabaseException(mysql_error(),DatabaseException::$ERROR_IN_QUERY);
  85. }
  86. return $rs;
  87. }
  88. /**
  89. * @inheritdocs
  90. */
  91. function Execute($connection,$sql)
  92. {
  93. if ( !$result = @mysql_query($sql, $connection) )
  94. {
  95. throw new DatabaseException(mysql_error(),DatabaseException::$ERROR_IN_QUERY);
  96. }
  97. return mysql_affected_rows($connection);
  98. }
  99. /**
  100. * @inheritdocs
  101. */
  102. public function GetQuotedSql($val)
  103. {
  104. if ($val === null) return DatabaseConfig::$CONVERT_NULL_TO_EMPTYSTRING ? "''" : 'NULL';
  105. if ($val instanceof ISqlFunction) return $val->GetQuotedSql($this);
  106. return "'" . $this->Escape($val) . "'";
  107. }
  108. /**
  109. * @inheritdocs
  110. */
  111. function Fetch($connection,$rs)
  112. {
  113. return mysql_fetch_assoc($rs);
  114. }
  115. /**
  116. * @inheritdocs
  117. */
  118. function GetLastInsertId($connection)
  119. {
  120. return (mysql_insert_id($connection));
  121. }
  122. /**
  123. * @inheritdocs
  124. */
  125. function GetLastError($connection)
  126. {
  127. return mysql_error($connection);
  128. }
  129. /**
  130. * @inheritdocs
  131. */
  132. function Release($connection,$rs)
  133. {
  134. mysql_free_result($rs);
  135. }
  136. /**
  137. * @inheritdocs
  138. * this method currently uses replacement and not mysql_real_escape_string
  139. * so that a database connection is not necessary in order to escape.
  140. * this way cached queries can be used without connecting to the DB server
  141. */
  142. function Escape($val)
  143. {
  144. return str_replace(self::$BAD_CHARS, self::$GOOD_CHARS, $val);
  145. // return mysql_real_escape_string($val);
  146. }
  147. /**
  148. * @inheritdocs
  149. */
  150. function GetTableNames($connection, $dbname, $ommitEmptyTables = false)
  151. {
  152. $sql = "SHOW TABLE STATUS FROM `" . $this->Escape($dbname) . "`";
  153. $rs = $this->Query($connection,$sql);
  154. $tables = array();
  155. while ( $row = $this->Fetch($connection,$rs) )
  156. {
  157. if ( $ommitEmptyTables == false || $rs['Data_free'] > 0 )
  158. {
  159. $tables[] = $row['Name'];
  160. }
  161. }
  162. return $tables;
  163. }
  164. /**
  165. * @inheritdocs
  166. */
  167. function Optimize($connection,$table)
  168. {
  169. $result = "";
  170. $rs = $this->Query($connection,"optimize table `". $this->Escape($table)."`");
  171. while ( $row = $this->Fetch($connection,$rs) )
  172. {
  173. $tbl = $row['Table'];
  174. if (!isset($results[$tbl])) $results[$tbl] = "";
  175. $result .= trim($results[$tbl] . " " . $row['Msg_type'] . "=\"" . $row['Msg_text'] . "\"");
  176. }
  177. return $result;
  178. }
  179. /**
  180. * @inheritdocs
  181. */
  182. function StartTransaction($connection)
  183. {
  184. $this->Execute($connection, "SET AUTOCOMMIT=0");
  185. $this->Execute($connection, "START TRANSACTION");
  186. }
  187. /**
  188. * @inheritdocs
  189. */
  190. function CommitTransaction($connection)
  191. {
  192. $this->Execute($connection, "COMMIT");
  193. $this->Execute($connection, "SET AUTOCOMMIT=1");
  194. }
  195. /**
  196. * @inheritdocs
  197. */
  198. function RollbackTransaction($connection)
  199. {
  200. $this->Execute($connection, "ROLLBACK");
  201. $this->Execute($connection, "SET AUTOCOMMIT=1");
  202. }
  203. }
  204. ?>