Source for file db-odbc.php

Documentation is available at db-odbc.php

  1. <?php
  2. /* ******************************************************************** */
  3. /* CATALYST PHP Source Code */
  4. /* -------------------------------------------------------------------- */
  5. /* This program is free software; you can redistribute it and/or modify */
  6. /* it under the terms of the GNU General Public License as published by */
  7. /* the Free Software Foundation; either version 2 of the License, or */
  8. /* (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU General Public License for more details. */
  14. /* */
  15. /* You should have received a copy of the GNU General Public License */
  16. /* along with this program; if not, write to: */
  17. /* The Free Software Foundation, Inc., 59 Temple Place, Suite 330, */
  18. /* Boston, MA 02111-1307 USA */
  19. /* -------------------------------------------------------------------- */
  20. /* */
  21. /* Filename: db-odbc.php */
  22. /* Author: Paul Waite */
  23. /* Description: Definitions for ODBC database access. */
  24. /* */
  25. /* ******************************************************************** */
  26. /** @package database */* ODBC database interface
  27. * This is a database interface class. It is an impedance-matcher
  28. * between the high-level Phplib functions for accessing data, and
  29. * the specific functions suplpied by Php to access a particular
  30. * flavour of databse such as Postgres, MS-SQL Server, Sybase etc.
  31. * @package database
  32. * @access private
  33. */
  34. class db_odbc extends database {
  35. /** Constructor */
  36.  
  37. function db_odbc($name="", $user="", $passwd="", $host="", $port=0, $enc="", $datestyle="") {
  38. $this->database($name, $user, $passwd, $host, $port, $enc, $datestyle);
  39. $this->type = "odbc";
  40. }
  41. // ....................................................................
  42. /**
  43. * Connect to the database.
  44. * @param boolean $persistent Whether to connect persistently or not
  45. * @return boolean Status true if connected successfully
  46. */
  47. function connect($persistent=NOT_PERSISTENT) {
  48. if (!$this->connected) {
  49. $dsn = "";
  50. $server = "";
  51. if ($this->host != "") $server .= $this->host;
  52. if ($this->port != 0) $server .= ":" . $this->port;
  53. if ($server != "") $dsn .= "SERVER=$server;";
  54. $dsn .= "DATABASE=" . $this->name . ";";
  55. if ($persistent)
  56. $this->dbid = odbc_pconnect($dsn, $this->user, $this->passwd, SQL_CUR_USE_ODBC);
  57. else
  58. $this->dbid = odbc_connect($dsn, $this->user, $this->passwd, SQL_CUR_USE_ODBC);
  59. if ($this->dbid) {
  60. $this->connected = true;
  61. }
  62. }
  63. return $this->connected;
  64. }
  65. // ....................................................................
  66. /** Disconnect from the database, if connected. */
  67.  
  68. function disconnect() {
  69. if (odbc_close($this->dbid)) {
  70. $this->connected = false;
  71. }
  72. }
  73. // ....................................................................
  74. /**
  75. * Execute a query on the connected database.
  76. * @param string $sql The SQL query to execute on the database
  77. * @return resource A database query resource ID, or false if query failed
  78. */
  79. function query($sql) {
  80. $sql = $this->convert_boolean_syntax($sql);
  81. $this->timer->restart();
  82. $rid = odbc_exec($this->dbid, $sql);
  83. $this->timer->stop();
  84. $this->executable_sql = $sql;
  85. $this->rid = $rid;
  86. $this->query_report();
  87. return $rid;
  88. }
  89. // ....................................................................
  90. /**
  91. * Return the number of rows returned by a SELECT query.
  92. * @param resource $rid The resource ID for the executed query
  93. * @return integer The number of rows returned by the query
  94. */
  95. function numrows($rid) {
  96. return odbc_num_rows($rid);
  97. }
  98. // ....................................................................
  99. /**
  100. * Return the number of rows affected by a query.
  101. * @param resource $rid The resource ID for the executed query
  102. * @return integer The number of rows affected by the query
  103. */
  104. function affectedrows($rid) {
  105. return odbc_num_rows($rid);
  106. }
  107. // ....................................................................
  108. /**
  109. * Free a resource.
  110. * @param resource $rid The resource ID for the executed query
  111. */
  112. function freeresult($rid) {
  113. odbc_free_result($rid);
  114. }
  115. // ....................................................................
  116. /**
  117. * Return the last error message.
  118. * @return string The last error message which was generated
  119. */
  120. function errormessage() {
  121. return odbc_errormsg($this->dbid);
  122. }
  123. // ....................................................................
  124. /**
  125. * Return the specified row, as a standard (enumerated) array of
  126. * field values.
  127. * @param resource $rid The resource ID for the executed query
  128. * @param integer $rowno Row number (zero-based) of row to return
  129. * @return array Enumerated array of field values
  130. */
  131. function fetch_row($rid, $rowno) {
  132. return odbc_fetch_row($rid, $rowno);
  133. }
  134. // ....................................................................
  135. /**
  136. * Return the specified row, as an associative array of fields
  137. * in a fieldname => value format.
  138. * @param resource $rid The resource ID for the executed query
  139. * @param integer $rowno Row number (zero-based) of row to return
  140. * @return array Associative array of field values, keyed by fieldname
  141. */
  142. function fetch_array($rid, $rowno) {
  143. $ret = array();
  144. $row = array();
  145. if(odbc_fetch_into($rid, $ret, $rowno)) {
  146. while (list($key, $value) = each($ret)) {
  147. $row[odbc_field_name($rid, $key+1)] = $value;
  148. }
  149. return $row;
  150. } else return false;
  151. }
  152. }
  153.  
  154. // ----------------------------------------------------------------------
  155. // Ensure ODBC Php module is present..
  156.  
  157. if (!extension_loaded("odbc")) {
  158. if (!dl("odbc.so")) {
  159. exit;
  160. }
  161. }
  162. // ----------------------------------------------------------------------
  163. ?>

Documentation generated by phpDocumentor 1.3.0RC3