Source for file db-mssql-server.php

Documentation is available at db-mssql-server.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-mssql_server.php */
  22. /* Author: Paul Waite */
  23. /* Description: Definitions for MICROSOSFT SQL SERVER database access. */
  24. /* */
  25. /* ******************************************************************** */
  26. /** @package database */* MSSQL 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_mssql_server extends database {
  35. /** Constructor */
  36.  
  37. function db_mssql_server($name="", $user="", $passwd="", $host="", $port=0) {
  38. $this->database($name, $user, $passwd, $host, $port);
  39. $this->type = "mssql_server";
  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. if ($persistent)
  50. $this->dbid = mssql_pconnect($this->host, $this->user, $this->passwd);
  51. else
  52. $this->dbid = mssql_connect($this->host, $this->user, $this->passwd);
  53. if ($this->dbid) {
  54. if (mssql_select_db($this->name, $this->dbid)) {
  55. $this->connected = true;
  56. }
  57. }
  58. }
  59. return $this->connected;
  60. }
  61. // ....................................................................
  62. /** Disconnect from the database, if connected. */
  63.  
  64. function disconnect() {
  65. if (mssql_close($this->dbid)) {
  66. $this->connected = false;
  67. }
  68. }
  69. // ....................................................................
  70. /**
  71. * Execute a query on the connected database.
  72. * @param string $sql The SQL query to execute on the database
  73. * @return resource A database query resource ID, or false if query failed
  74. */
  75. function query($sql) {
  76. $sql = $this->convert_boolean_syntax($sql);
  77. $rid = mssql_query($sql, $this->dbid);
  78. $this->timer->stop();
  79. $this->executable_sql = $sql;
  80. $this->rid = $rid;
  81. $this->query_report();
  82. return $rid;
  83. }
  84. // ....................................................................
  85. /**
  86. * Return the number of rows returned by a SELECT query.
  87. * @param resource $rid The resource ID for the executed query
  88. * @return integer The number of rows returned by the query
  89. */
  90. function numrows($rid) {
  91. return mssql_num_rows($rid);
  92. }
  93. // ....................................................................
  94. /**
  95. * Return the number of rows affected by a query.
  96. * @param resource $rid The resource ID for the executed query
  97. * @return integer The number of rows affected by the query
  98. */
  99. function affectedrows($rid) {
  100. return mssql_rows_affected($rid);
  101. }
  102. // ....................................................................
  103. /**
  104. * Free a resource.
  105. * @param resource $rid The resource ID for the executed query
  106. */
  107. function freeresult($rid) {
  108. mssql_free_result($rid);
  109. }
  110. // ....................................................................
  111. /**
  112. * Return the last error message.
  113. * @return string The last error message which was generated
  114. */
  115. function errormessage() {
  116. return mssql_get_last_message();
  117. }
  118. // ....................................................................
  119. /**
  120. * Return the specified row, as a standard (enumerated) array of
  121. * field values.
  122. * @param resource $rid The resource ID for the executed query
  123. * @param integer $rowno Row number (zero-based) of row to return
  124. * @return array Enumerated array of field values
  125. */
  126. function fetch_row($rid, $rowno) {
  127. mssql_data_seek($rid, $rowno);
  128. return mssql_fetch_row($rid);
  129. }
  130. // ....................................................................
  131. /**
  132. * Return the specified row, as an associative array of fields
  133. * in a fieldname => value format.
  134. * @param resource $rid The resource ID for the executed query
  135. * @param integer $rowno Row number (zero-based) of row to return
  136. * @return array Associative array of field values
  137. */
  138. function fetch_array($rid, $rowno) {
  139. mssql_data_seek($rid, $rowno);
  140. return mssql_fetch_array($rid);
  141. }
  142. // ....................................................................
  143. /**
  144. * Given an Axyl SQL query object, build the SQL string from it
  145. * in suitable format for the currently connected database server.
  146. * @param pointer $sqlquery Pointer to an Axyl query object
  147. * @return string The SQL string built from the query object
  148. */
  149. function SQL(&$sqlquery) {
  150. $sql = "";
  151. switch (strtoupper($sqlquery->type)) {
  152. case "SELECT":
  153. $sql .= "SELECT ";
  154. if ($sqlquery->fields->total == 0) $sql .= "*";
  155. else $sql .= $sqlquery->fields->listed();
  156. $sql .= " FROM ";
  157. $sql .= $sqlquery->tables->listed();
  158. if ($sqlquery->where->total > 0) {
  159. $sql .= " WHERE ";
  160. $sql .= $sqlquery->where->listed(" ");
  161. }
  162. if ($sqlquery->groupby->total > 0) {
  163. $sql .= " GROUP BY ";
  164. $sql .= $sqlquery->groupby->listed();
  165. }
  166. if ($sqlquery->orderby->total > 0) {
  167. $sql .= " ORDER BY ";
  168. $sql .= $sqlquery->orderby->listed();
  169. }
  170. if ($sqlquery->limit > 0) {
  171. $sql .= " TOP $sqlquery->limit";
  172. }
  173. break;
  174.  
  175. case "INSERT":
  176. $sql .= "INSERT INTO ";
  177. $sql .= $sqlquery->tables->listed();
  178. if ($sqlquery->fields->total > 0) {
  179. $sql .= " (" . $sqlquery->fields->listed() . ")";
  180. }
  181. $sql .= " VALUES ";
  182. $sql .= "(" . $sqlquery->fields->values() . ")";
  183. break;
  184.  
  185. case "DELETE":
  186. $sql .= "DELETE FROM ";
  187. $sql .= $sqlquery->tables->listed();
  188. if ($sqlquery->where->total > 0) {
  189. $sql .= " WHERE ";
  190. $sql .= $sqlquery->where->listed(" ");
  191. }
  192. break;
  193.  
  194. case "UPDATE":
  195. $sql .= "UPDATE ";
  196. $sql .= $sqlquery->tables->listed();
  197. $sql .= " SET ";
  198. $sql .= $sqlquery->fields->equated();
  199. if ($sqlquery->where->total > 0) {
  200. $sql .= " WHERE ";
  201. $sql .= $sqlquery->where->listed(" ");
  202. }
  203. break;
  204. }
  205. // Render any NULL values..
  206. $sql = str_replace("'".NULLVALUE."'", "NULL", $sql);
  207.  
  208. // Return SQL we have built..
  209. return $sql;
  210. }
  211. }
  212.  
  213. // ----------------------------------------------------------------------
  214. // Ensure Sybase Php module is present..
  215.  
  216. if (!extension_loaded("sybase_ct")) {
  217. if (!dl("sybase_ct.so")) {
  218. exit;
  219. }
  220. }
  221. // ----------------------------------------------------------------------
  222. ?>

Documentation generated by phpDocumentor 1.3.0RC3