/database/DbOdbc.class.php

https://github.com/palmic/2yaml · PHP · 213 lines · 161 code · 25 blank · 27 comment · 26 complexity · 808bab469bca6f86bf09eb0b4067c141 MD5 · raw file

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