/database/class.DbOdbc.php

https://github.com/palmic/2yaml · PHP · 217 lines · 163 code · 25 blank · 29 comment · 28 complexity · d4e05b64cb70af694b296a6cfc9a1b9d MD5 · raw file

  1. <?php
  2. /**
  3. * Database platform implementation for ODBC.
  4. * @author Michal Palma <palmic at email dot cz>
  5. * @package DbControl
  6. * @version 1.5
  7. * @date 2006-01-11
  8. */
  9. class DbOdbc extends DbPlatform
  10. {
  11. //== Attributes ======================================================================
  12. //== constructors ====================================================================
  13. public function DbOdbc(DbParametersMessenger $DbParametersMessenger, DbStateHandler $dbStateHandler) {
  14. $this->dbParametersMessenger = $DbParametersMessenger;
  15. $this->dbStateHandler = $dbStateHandler;
  16. }
  17. //== destructors =====================================================================
  18. public function __destruct() {
  19. if ($this->activeTransaction) {
  20. if ($this->autoCommit) {
  21. $this->transactionCommit();
  22. }
  23. else {
  24. $this->transactionRollback();
  25. }
  26. }
  27. @odbc_close($this->connection);
  28. }
  29. //== public functions ================================================================
  30. public function fetchAssoc($result) {
  31. if (!is_resource($result)) {
  32. throw new DbControlException("Ilegal parameter result. Must be valid result resource.");
  33. }
  34. else {
  35. # odbc_fetch_array returns the same array like forexample mysql_fetch_assoc (array associated by colnames)
  36. return $row = odbc_fetch_array($result);
  37. }
  38. }
  39. public function getLastId() {
  40. try {
  41. $result = $this->query("SELECT LAST_INSERT_ID()");
  42. $id = @odbc_result($result, 1);
  43. }
  44. catch (Exception $e) {
  45. throw new DbControlException("Error in trying to acquire Last inserted id.\n" . $e->getMessage(), $e->getCode());
  46. }
  47. return $id;
  48. }
  49. public function selectDb(/*string*/ $dbName) {
  50. if (!is_string($dbName)) {
  51. throw new DbControlException("Ilegal parameter dbName. Must be string.");
  52. }
  53. try {
  54. $this->query("USE ". $dbName);
  55. }
  56. catch (Exception $e) {
  57. throw $e;
  58. }
  59. }
  60. public function query(/*string*/ $query) {
  61. if (!is_string($query)) {
  62. throw new DbControlException("Ilegal parameter query. Must be string.");
  63. }
  64. if (!$result = @odbc_exec($this->getConnection(), $query)) {
  65. $this->throwOdbcException();
  66. }
  67. return $result;
  68. }
  69. public function getNumRows(/*resource*/ $result) {
  70. if (!is_resource($result)) {
  71. throw new DbControlException("Ilegal parameter result. Must be valid result resource.");
  72. }
  73. else {
  74. return @odbc_num_rows($result);
  75. }
  76. }
  77. public function getColnames(/*resource*/ $result) {
  78. if (!is_resource($result)) {
  79. throw new DbControlException("Ilegal parameter result. Must be valid result resource.");
  80. }
  81. if (!$numFields = @odbc_num_fields($result)) {
  82. throw new DbControlException("No Column in result.");
  83. }
  84. for ($i = 0; $i < $numFields; $i++) {
  85. if (!$colname = @odbc_field_name($result, $i + 1)) {
  86. $this->throwOdbcException("Colnames reading error.");
  87. }
  88. $colnames[$i] = $colname;
  89. }
  90. return $colnames;
  91. }
  92. public function transactionStart(/*boolean*/ $autoCommit) {
  93. if (!is_bool($autoCommit)) {
  94. throw new DbControlException("Ilegal parameter autoCommit. Must be boolean.");
  95. }
  96. if (!$this->activeTransaction) {
  97. try {
  98. $this->query("BEGIN WORK");
  99. }
  100. catch (Exception $e) {
  101. throw $e;
  102. }
  103. $this->autoCommit = $autoCommit;
  104. $this->activeTransaction = true;
  105. }
  106. else {
  107. throw new DbControlException("Multiple transactions are not supported.");
  108. }
  109. }
  110. public function transactionCommit() {
  111. if ($this->activeTransaction) {
  112. try {
  113. $this->query("COMMIT");
  114. }
  115. catch (Exception $e) {
  116. throw $e;
  117. }
  118. $this->activeTransaction = false;
  119. }
  120. else {
  121. throw new DbControlException("No transaction active.");
  122. }
  123. }
  124. public function transactionRollback() {
  125. if ($this->activeTransaction) {
  126. try {
  127. $this->query("ROLLBACK");
  128. }
  129. catch (Exception $e) {
  130. throw $e;
  131. }
  132. $this->activeTransaction = false;
  133. }
  134. else {
  135. throw new DbControlException("No transaction active.");
  136. }
  137. }
  138. //== protected functions =============================================================
  139. protected function connect() {
  140. if(!is_resource($this->connection)) {
  141. $cursorType = "SQL_CUR_USE_ODBC";
  142. $this->connection = @odbc_pconnect($this->dbParametersMessenger->dsn, $this->dbParametersMessenger->loginName, $this->dbParametersMessenger->loginPassword, $cursorType);
  143. if (!is_resource($this->connection)) {
  144. # Exception code -1 for connection error
  145. throw new DbControlException("Cant connect to database ODBC.\nhost = ". $this->dbParametersMessenger->loginHost ."\nDSN = ". $this->dbParametersMessenger->dsn ."\ncursor type = ". $cursorType, -1);
  146. }
  147. //setting prior charset for connection with ODBC just after connection is needeed from last versions (Espetialy for non-English signs)
  148. # Caution: Using $this->query() may rewrite query that is waiting for execute
  149. if (!@odbc_exec($this->connection, "SET NAMES '". $this->dbStateHandler->getCharset() ."';")) {
  150. $this->throwOdbcException("Cannot set charset of DB session.");
  151. }
  152. //setting default task database schema - if defined
  153. # Caution: Using $this->query() may rewrite query that is waiting for execute
  154. if (strlen($schema = $this->dbParametersMessenger->schema) > 0) {
  155. if (!@odbc_exec($this->connection, "USE $schema;")) {
  156. $this->throwOdbcException("Cannot select default database schema '$schema'.");
  157. }
  158. }
  159. }
  160. }
  161. /**
  162. * getter for connection
  163. * @return valid connection resource
  164. */
  165. protected function getConnection() {
  166. try {
  167. $this->connect();
  168. }
  169. catch (Exception $e) {
  170. throw $e;
  171. }
  172. return $this->connection;
  173. }
  174. /**
  175. * Throws Exception with ODBC error infos
  176. * @return void
  177. */
  178. protected function throwOdbcException(/*string*/ $addToMessage = "") {
  179. if (is_string($addToMessage)) {
  180. $message = $addToMessage ." \n". @odbc_errormsg($this->connection);
  181. }
  182. else {
  183. $message = @odbc_errormsg($this->connection);
  184. }
  185. # ODBC error code can be invalid for Exception code..
  186. $message = "Message: ". $message ."\nCode: ". @odbc_error($this->connection);
  187. throw new DbControlException($message);
  188. }
  189. }
  190. ?>