/database/DbMysql.class.php

https://github.com/palmic/2yaml · PHP · 238 lines · 179 code · 27 blank · 32 comment · 31 complexity · 28216e5661c91ac8f68708c546fb3af7 MD5 · raw file

  1. <?php
  2. /**
  3. * Database platform implementation for MySQL.
  4. * @author Michal Palma <palmic at centrum dot cz>
  5. * @copyleft (l) 2005-2006 Michal Palma
  6. * @package DbControl
  7. * @version 1.5
  8. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  9. * @date 2006-01-11
  10. */
  11. class DbMysql extends DbPlatform
  12. {
  13. //== Attributes ======================================================================
  14. //== constructors ====================================================================
  15. public function DbMysql(DbParametersMessenger $DbParametersMessenger, DbStateHandler $dbStateHandler) {
  16. if (!in_array("mysql", get_loaded_extensions())) {
  17. throw new DbControlException("Your PHP configuration does not support MySQL extension. Check php.ini.");
  18. }
  19. $this->dbParametersMessenger = $DbParametersMessenger;
  20. $this->dbStateHandler = $dbStateHandler;
  21. }
  22. //== destructors =====================================================================
  23. public function __destruct() {
  24. if ($this->activeTransaction) {
  25. if ($this->autoCommit) {
  26. $this->transactionCommit();
  27. }
  28. else {
  29. $this->transactionRollback();
  30. }
  31. }
  32. @mysql_close($this->connection);
  33. }
  34. //== public functions ================================================================
  35. public function fetchAssoc($result) {
  36. if (!is_resource($result)) {
  37. throw new DbControlException("Ilegal parameter result. Must be valid result resource.");
  38. }
  39. else {
  40. return @mysql_fetch_assoc($result);
  41. }
  42. }
  43. public function getLastId() {
  44. return @mysql_insert_id($this->getConnection());
  45. }
  46. public function selectDb(/*string*/ $dbName) {
  47. if (!is_string($dbName)) {
  48. throw new DbControlException("Ilegal parameter dbName. Must be string.");
  49. }
  50. try {
  51. $this->query("USE ". $dbName);
  52. }
  53. catch (Exception $e) {
  54. throw $e;
  55. }
  56. }
  57. public function query(/*string*/ $query) {
  58. if (!is_string($query)) {
  59. throw new DbControlException("Ilegal parameter query. Must be string.");
  60. }
  61. if (!$result = @mysql_query($query, $this->getConnection())) {
  62. $this->throwMysqlException();
  63. }
  64. return $result;
  65. }
  66. public function getNumRows(/*resource*/ $result) {
  67. if (!is_resource($result)) {
  68. throw new DbControlException("Ilegal parameter result. Must be valid result resource.");
  69. }
  70. else {
  71. return @mysql_num_rows($result);
  72. }
  73. }
  74. public function getColnames(/*resource*/ $result) {
  75. if (!is_resource($result)) {
  76. throw new DbControlException("Ilegal parameter result. Must be valid result resource.");
  77. }
  78. if (!$numFields = @mysql_num_fields($result)) {
  79. throw new DbControlException("No Column in result.");
  80. }
  81. for ($i = 0; $i < $numFields; $i++) {
  82. if (!$colname = @mysql_field_name($result, $i)) {
  83. $this->throwMysqlException("Colnames reading error.");
  84. }
  85. $colnames[$i] = $colname;
  86. }
  87. return $colnames;
  88. }
  89. public function getTableColumns(/*string*/ $schemaName, /*string*/ $tableName) {
  90. if (strlen($schemaName) < 1) {
  91. throw new DbControlException("Ilegal parameter schemaName. Must be string.");
  92. }
  93. if (strlen($tableName) < 1) {
  94. throw new DbControlException("Ilegal parameter tableName. Must be string.");
  95. }
  96. try {
  97. $dbSchema = "information_schema";
  98. $sql = "SELECT
  99. COLUMN_NAME AS name,
  100. DATA_TYPE AS type,
  101. CHARACTER_OCTET_LENGTH AS length
  102. FROM $dbSchema.COLUMNS
  103. WHERE TABLE_SCHEMA = '$schemaName'
  104. AND TABLE_NAME = '$tableName'";
  105. $result = $this->query($sql);
  106. while ($row = $this->fetchAssoc($result)) {
  107. $length = (is_numeric($row["length"]) && $row["length"] > 0) ? "(". $row["length"] .")" : "";
  108. $columns[$row["name"]]["type"] = $row["type"] . $length;
  109. }
  110. return $columns;
  111. }
  112. catch (Exception $e) {
  113. throw $e;
  114. }
  115. }
  116. public function transactionStart(/*boolean*/ $autoCommit) {
  117. if (!is_bool($autoCommit)) {
  118. throw new DbControlException("Ilegal parameter autoCommit. Must be boolean.");
  119. }
  120. if (!$this->activeTransaction) {
  121. try {
  122. $this->query("BEGIN WORK");
  123. }
  124. catch (Exception $e) {
  125. throw $e;
  126. }
  127. $this->autoCommit = $autoCommit;
  128. $this->activeTransaction = true;
  129. }
  130. else {
  131. throw new DbControlException("Multiple transactions are not supported.");
  132. }
  133. }
  134. public function transactionCommit() {
  135. if ($this->activeTransaction) {
  136. try {
  137. $this->query("COMMIT");
  138. }
  139. catch (Exception $e) {
  140. throw $e;
  141. }
  142. $this->activeTransaction = false;
  143. }
  144. else {
  145. throw new DbControlException("No transaction active.");
  146. }
  147. }
  148. public function transactionRollback() {
  149. if ($this->activeTransaction) {
  150. try {
  151. $this->query("ROLLBACK");
  152. }
  153. catch (Exception $e) {
  154. throw $e;
  155. }
  156. $this->activeTransaction = false;
  157. }
  158. else {
  159. throw new DbControlException("No transaction active.");
  160. }
  161. }
  162. //== protected functions =============================================================
  163. protected function connect() {
  164. if(!is_resource($this->connection)) {
  165. // we are not using port value in MYSQL connection - there was problems with that.
  166. // if (strlen($this->dbParametersMessenger->port) > 0) {
  167. // $hostString = $this->dbParametersMessenger->loginHost .":". $this->dbParametersMessenger->port;
  168. // }
  169. // else {
  170. // $hostString = $this->dbParametersMessenger->loginHost;
  171. // }
  172. $this->connection = @mysql_pconnect($hostString, $this->dbParametersMessenger->loginName, $this->dbParametersMessenger->loginPassword);
  173. if (!is_resource($this->connection)) {
  174. # Exception code -1 for connection error
  175. throw new DbControlException("Cant connect to database mysql.\nhost = ". $this->dbParametersMessenger->loginHost, -1);
  176. }
  177. //setting prior charset for connection with MySQL just after connection is needeed from last versions (Espetialy for non-English signs)
  178. # Caution: Using $this->query() may rewrite query that is waiting for execute
  179. if (!@mysql_query("SET NAMES '". $this->dbStateHandler->getCharset() ."';", $this->connection)) {
  180. $this->throwMysqlException("Cannot set charset of DB session.");
  181. }
  182. }
  183. }
  184. /**
  185. * getter for connection
  186. * @return valid connection resource
  187. */
  188. protected function getConnection() {
  189. try {
  190. $this->connect();
  191. }
  192. catch (Exception $e) {
  193. throw $e;
  194. }
  195. return $this->connection;
  196. }
  197. /**
  198. * Throws Exception with Mysql error infos
  199. * @return void
  200. */
  201. protected function throwMysqlException(/*string*/ $addToMessage = "") {
  202. if (is_string($addToMessage)) {
  203. $message = $addToMessage ." \n". @mysql_error($this->connection);
  204. }
  205. else {
  206. $message = @mysql_error($this->connection);
  207. }
  208. throw new DbControlException($message, @mysql_errno($this->connection));
  209. }
  210. }
  211. ?>