/forum/db/mssql-odbc.php

https://github.com/batl/sp · PHP · 387 lines · 293 code · 64 blank · 30 comment · 49 complexity · 9a800d7122e0c2b286722ee3305a8bca MD5 · raw file

  1. <?php
  2. /***************************************************************************
  3. * mssql-odbc.php
  4. * -------------------
  5. * begin : Saturday, Feb 13, 2001
  6. * copyright : (C) 2001 The phpBB Group
  7. * email : support@phpbb.com
  8. *
  9. * $Id: mssql-odbc.php 2380 2002-03-20 17:48:30Z psotfx $
  10. *
  11. ***************************************************************************/
  12. /***************************************************************************
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. ***************************************************************************/
  20. if(!defined("SQL_LAYER"))
  21. {
  22. define("SQL_LAYER","mssql-odbc");
  23. class sql_db
  24. {
  25. var $db_connect_id;
  26. var $result;
  27. var $next_id;
  28. var $num_rows = array();
  29. var $current_row = array();
  30. var $field_names = array();
  31. var $field_types = array();
  32. var $result_rowset = array();
  33. var $num_queries = 0;
  34. //
  35. // Constructor
  36. //
  37. function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true)
  38. {
  39. $this->persistency = $persistency;
  40. $this->server = $sqlserver;
  41. $this->user = $sqluser;
  42. $this->password = $sqlpassword;
  43. $this->dbname = $database;
  44. $this->db_connect_id = ($this->persistency) ? odbc_pconnect($this->server, $this->user, $this->password) : odbc_connect($this->server, $this->user, $this->password);
  45. return ( $this->db_connect_id ) ? $this->db_connect_id : false;
  46. }
  47. //
  48. // Other base methods
  49. //
  50. function sql_close()
  51. {
  52. if($this->db_connect_id)
  53. {
  54. if( $this->in_transaction )
  55. {
  56. @odbc_commit($this->db_connect_id);
  57. }
  58. if( count($this->result_rowset) )
  59. {
  60. unset($this->result_rowset);
  61. unset($this->field_names);
  62. unset($this->field_types);
  63. unset($this->num_rows);
  64. unset($this->current_row);
  65. }
  66. return @odbc_close($this->db_connect_id);
  67. }
  68. else
  69. {
  70. return false;
  71. }
  72. }
  73. //
  74. // Query method
  75. //
  76. function sql_query($query = "", $transaction = FALSE)
  77. {
  78. if( $query != "" )
  79. {
  80. $this->num_queries++;
  81. if( $transaction == BEGIN_TRANSACTION && !$this->in_transaction )
  82. {
  83. if( !odbc_autocommit($this->db_connect_id, false) )
  84. {
  85. return false;
  86. }
  87. $this->in_transaction = TRUE;
  88. }
  89. if( preg_match("/^SELECT(.*?)(LIMIT ([0-9]+)[, ]*([0-9]+)*)?$/s", $query, $limits) )
  90. {
  91. $query = $limits[1];
  92. if( !empty($limits[2]) )
  93. {
  94. $row_offset = ( $limits[4] ) ? $limits[3] : "";
  95. $num_rows = ( $limits[4] ) ? $limits[4] : $limits[3];
  96. $query = "TOP " . ( $row_offset + $num_rows ) . $query;
  97. }
  98. $this->result = odbc_exec($this->db_connect_id, "SELECT $query");
  99. if( $this->result )
  100. {
  101. if( empty($this->field_names[$this->result]) )
  102. {
  103. for($i = 1; $i < odbc_num_fields($this->result) + 1; $i++)
  104. {
  105. $this->field_names[$this->result][] = odbc_field_name($this->result, $i);
  106. $this->field_types[$this->result][] = odbc_field_type($this->result, $i);
  107. }
  108. }
  109. $this->current_row[$this->result] = 0;
  110. $this->result_rowset[$this->result] = array();
  111. $row_outer = ( isset($row_offset) ) ? $row_offset + 1 : 1;
  112. $row_outer_max = ( isset($num_rows) ) ? $row_offset + $num_rows + 1 : 1E9;
  113. $row_inner = 0;
  114. while( odbc_fetch_row($this->result, $row_outer) && $row_outer < $row_outer_max )
  115. {
  116. for($j = 0; $j < count($this->field_names[$this->result]); $j++)
  117. {
  118. $this->result_rowset[$this->result][$row_inner][$this->field_names[$this->result][$j]] = stripslashes(odbc_result($this->result, $j + 1));
  119. }
  120. $row_outer++;
  121. $row_inner++;
  122. }
  123. $this->num_rows[$this->result] = count($this->result_rowset[$this->result]);
  124. }
  125. }
  126. else if( eregi("^INSERT ", $query) )
  127. {
  128. $this->result = odbc_exec($this->db_connect_id, $query);
  129. if( $this->result )
  130. {
  131. $result_id = odbc_exec($this->db_connect_id, "SELECT @@IDENTITY");
  132. if( $result_id )
  133. {
  134. if( odbc_fetch_row($result_id) )
  135. {
  136. $this->next_id[$this->db_connect_id] = odbc_result($result_id, 1);
  137. $this->affected_rows[$this->db_connect_id] = odbc_num_rows($this->result);
  138. }
  139. }
  140. }
  141. }
  142. else
  143. {
  144. $this->result = odbc_exec($this->db_connect_id, $query);
  145. if( $this->result )
  146. {
  147. $this->affected_rows[$this->db_connect_id] = odbc_num_rows($this->result);
  148. }
  149. }
  150. if( !$this->result )
  151. {
  152. if( $this->in_transaction )
  153. {
  154. odbc_rollback($this->db_connect_id);
  155. odbc_autocommit($this->db_connect_id, true);
  156. $this->in_transaction = FALSE;
  157. }
  158. return false;
  159. }
  160. if( $transaction == END_TRANSACTION && $this->in_transaction )
  161. {
  162. $this->in_transaction = FALSE;
  163. if ( !odbc_commit($this->db_connect_id) )
  164. {
  165. odbc_rollback($this->db_connect_id);
  166. odbc_autocommit($this->db_connect_id, true);
  167. return false;
  168. }
  169. odbc_autocommit($this->db_connect_id, true);
  170. }
  171. odbc_free_result($this->result);
  172. return $this->result;
  173. }
  174. else
  175. {
  176. if( $transaction == END_TRANSACTION && $this->in_transaction )
  177. {
  178. $this->in_transaction = FALSE;
  179. if ( !@odbc_commit($this->db_connect_id) )
  180. {
  181. odbc_rollback($this->db_connect_id);
  182. odbc_autocommit($this->db_connect_id, true);
  183. return false;
  184. }
  185. odbc_autocommit($this->db_connect_id, true);
  186. }
  187. return true;
  188. }
  189. }
  190. //
  191. // Other query methods
  192. //
  193. function sql_numrows($query_id = 0)
  194. {
  195. if( !$query_id )
  196. {
  197. $query_id = $this->result;
  198. }
  199. return ( $query_id ) ? $this->num_rows[$query_id] : false;
  200. }
  201. function sql_numfields($query_id = 0)
  202. {
  203. if( !$query_id )
  204. {
  205. $query_id = $this->result;
  206. }
  207. return ( $query_id ) ? count($this->field_names[$query_id]) : false;
  208. }
  209. function sql_fieldname($offset, $query_id = 0)
  210. {
  211. if( !$query_id )
  212. {
  213. $query_id = $this->result;
  214. }
  215. return ( $query_id ) ? $this->field_names[$query_id][$offset] : false;
  216. }
  217. function sql_fieldtype($offset, $query_id = 0)
  218. {
  219. if( !$query_id )
  220. {
  221. $query_id = $this->result;
  222. }
  223. return ( $query_id ) ? $this->field_types[$query_id][$offset] : false;
  224. }
  225. function sql_fetchrow($query_id = 0)
  226. {
  227. if( !$query_id )
  228. {
  229. $query_id = $this->result;
  230. }
  231. if( $query_id )
  232. {
  233. return ( $this->num_rows[$query_id] && $this->current_row[$query_id] < $this->num_rows[$query_id] ) ? $this->result_rowset[$query_id][$this->current_row[$query_id]++] : false;
  234. }
  235. else
  236. {
  237. return false;
  238. }
  239. }
  240. function sql_fetchrowset($query_id = 0)
  241. {
  242. if( !$query_id )
  243. {
  244. $query_id = $this->result;
  245. }
  246. if( $query_id )
  247. {
  248. return ( $this->num_rows[$query_id] ) ? $this->result_rowset[$query_id] : false;
  249. }
  250. else
  251. {
  252. return false;
  253. }
  254. }
  255. function sql_fetchfield($field, $row = -1, $query_id = 0)
  256. {
  257. if( !$query_id )
  258. {
  259. $query_id = $this->result;
  260. }
  261. if( $query_id )
  262. {
  263. if( $row < $this->num_rows[$query_id] )
  264. {
  265. $getrow = ( $row == -1 ) ? $this->current_row[$query_id] - 1 : $row;
  266. return $this->result_rowset[$query_id][$getrow][$this->field_names[$query_id][$field]];
  267. }
  268. else
  269. {
  270. return false;
  271. }
  272. }
  273. else
  274. {
  275. return false;
  276. }
  277. }
  278. function sql_rowseek($offset, $query_id = 0)
  279. {
  280. if( !$query_id )
  281. {
  282. $query_id = $this->result;
  283. }
  284. if( $query_id )
  285. {
  286. $this->current_row[$query_id] = $offset - 1;
  287. return true;
  288. }
  289. else
  290. {
  291. return false;
  292. }
  293. }
  294. function sql_nextid()
  295. {
  296. return ( $this->next_id[$this->db_connect_id] ) ? $this->next_id[$this->db_connect_id] : false;
  297. }
  298. function sql_affectedrows()
  299. {
  300. return ( $this->affected_rows[$this->db_connect_id] ) ? $this->affected_rows[$this->db_connect_id] : false;
  301. }
  302. function sql_freeresult($query_id = 0)
  303. {
  304. if( !$query_id )
  305. {
  306. $query_id = $this->result;
  307. }
  308. unset($this->num_rows[$query_id]);
  309. unset($this->current_row[$query_id]);
  310. unset($this->result_rowset[$query_id]);
  311. unset($this->field_names[$query_id]);
  312. unset($this->field_types[$query_id]);
  313. return true;
  314. }
  315. function sql_error()
  316. {
  317. $error['code'] = odbc_error($this->db_connect_id);
  318. $error['message'] = odbc_errormsg($this->db_connect_id);
  319. return $error;
  320. }
  321. } // class sql_db
  322. } // if ... define
  323. ?>