PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/PNphpBB2/db/mssql.php

https://gitlab.com/bulwye/reliquerunt
PHP | 418 lines | 307 code | 63 blank | 48 comment | 53 complexity | d3371b6fc313998d5f61d4fc04bbbbec MD5 | raw file
  1. <?php
  2. /***************************************************************************
  3. * mssql.php
  4. * -------------------
  5. * begin : Saturday, Feb 13, 2001
  6. * copyright : (C) 2001 The phpBB Group
  7. * email : supportphpbb.com
  8. *
  9. * $Id: mssql.php,v 1.22.2.4 2006/03/09 19:57:37 grahamje Exp $
  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");
  23. class sql_db
  24. {
  25. var $db_connect_id;
  26. var $result;
  27. var $next_id;
  28. var $in_transaction = 0;
  29. var $row = array();
  30. var $rowset = array();
  31. var $limit_offset;
  32. var $query_limit_success;
  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->user = $sqluser;
  41. $this->password = $sqlpassword;
  42. $this->server = $sqlserver;
  43. $this->dbname = $database;
  44. $this->db_connect_id = ( $this->persistency ) ? @mssql_pconnect($this->server, $this->user, $this->password) : @mssql_connect($this->server, $this->user, $this->password);
  45. if( $this->db_connect_id && $this->dbname != "" )
  46. {
  47. if( !mssql_select_db($this->dbname, $this->db_connect_id) )
  48. {
  49. mssql_close($this->db_connect_id);
  50. return false;
  51. }
  52. }
  53. return $this->db_connect_id;
  54. }
  55. //
  56. // Other base methods
  57. //
  58. function sql_close()
  59. {
  60. if($this->db_connect_id)
  61. {
  62. //
  63. // Commit any remaining transactions
  64. //
  65. if( $this->in_transaction )
  66. {
  67. @mssql_query("COMMIT", $this->db_connect_id);
  68. }
  69. return @mssql_close($this->db_connect_id);
  70. }
  71. else
  72. {
  73. return false;
  74. }
  75. }
  76. //
  77. // Query method
  78. //
  79. function sql_query($query = '', $transaction = FALSE)
  80. {
  81. //
  82. // Remove any pre-existing queries
  83. //
  84. unset($this->result);
  85. unset($this->row);
  86. if ( $query != '' )
  87. {
  88. $this->num_queries++;
  89. if ( $transaction == BEGIN_TRANSACTION && !$this->in_transaction )
  90. {
  91. if ( !@mssql_query('BEGIN TRANSACTION', $this->db_connect_id) )
  92. {
  93. return false;
  94. }
  95. $this->in_transaction = TRUE;
  96. }
  97. //
  98. // Does query contain any LIMIT code? If so pull out relevant start and num_results
  99. // This isn't terribly easy with MSSQL, whatever you do will potentially impact
  100. // performance compared to an 'in-built' limit
  101. //
  102. // Another issue is the 'lack' of a returned true value when a query is valid but has
  103. // no result set (as with all the other DB interfaces). It seems though that it's
  104. // 'fair' to say that if a query returns a false result (ie. no resource id) then the
  105. // SQL was valid but had no result set. If the query returns nothing but the rowcount
  106. // returns something then there's a problem. This may well be a false assumption though
  107. // ... needs checking under Windows itself.
  108. //
  109. if( preg_match('#^SELECT(.*?)(LIMIT ([0-9]+)[, ]*([0-9]+)*)?$#s', $query, $limits) )
  110. {
  111. $query = $limits[1];
  112. if( !empty($limits[2]) )
  113. {
  114. $row_offset = ( $limits[4] ) ? $limits[3] : "";
  115. $num_rows = ( $limits[4] ) ? $limits[4] : $limits[3];
  116. $query = 'TOP ' . ( $row_offset + $num_rows ) . $query;
  117. }
  118. $this->result = @mssql_query("SELECT $query", $this->db_connect_id);
  119. if( $this->result )
  120. {
  121. $this->limit_offset[$this->result] = ( !empty($row_offset) ) ? $row_offset : 0;
  122. if( $row_offset > 0 )
  123. {
  124. @mssql_data_seek($this->result, $row_offset);
  125. }
  126. }
  127. }
  128. else if( preg_match('#^INSERT #i', $query) )
  129. {
  130. if( @mssql_query($query, $this->db_connect_id) )
  131. {
  132. $this->result = time() + microtime();
  133. $result_id = @mssql_query('SELECT @@IDENTITY AS id, @@ROWCOUNT as affected', $this->db_connect_id);
  134. if( $result_id )
  135. {
  136. if( $row = @mssql_fetch_array($result_id) )
  137. {
  138. $this->next_id[$this->db_connect_id] = $row['id'];
  139. $this->affected_rows[$this->db_connect_id] = $row['affected'];
  140. }
  141. }
  142. }
  143. }
  144. else
  145. {
  146. if( @mssql_query($query, $this->db_connect_id) )
  147. {
  148. $this->result = time() + microtime();
  149. $result_id = @mssql_query('SELECT @@ROWCOUNT as affected', $this->db_connect_id);
  150. if( $result_id )
  151. {
  152. if( $row = @mssql_fetch_array($result_id) )
  153. {
  154. $this->affected_rows[$this->db_connect_id] = $row['affected'];
  155. }
  156. }
  157. }
  158. }
  159. if( !$this->result )
  160. {
  161. if( $this->in_transaction )
  162. {
  163. @mssql_query('ROLLBACK', $this->db_connect_id);
  164. $this->in_transaction = FALSE;
  165. }
  166. return false;
  167. }
  168. if( $transaction == END_TRANSACTION && $this->in_transaction )
  169. {
  170. $this->in_transaction = FALSE;
  171. if( !@mssql_query('COMMIT', $this->db_connect_id) )
  172. {
  173. @mssql_query("ROLLBACK", $this->db_connect_id);
  174. return false;
  175. }
  176. }
  177. return $this->result;
  178. }
  179. else
  180. {
  181. if( $transaction == END_TRANSACTION && $this->in_transaction )
  182. {
  183. $this->in_transaction = FALSE;
  184. if( !@mssql_query('COMMIT', $this->db_connect_id) )
  185. {
  186. @mssql_query('ROLLBACK', $this->db_connect_id);
  187. return false;
  188. }
  189. }
  190. return true;
  191. }
  192. }
  193. //
  194. // Other query methods
  195. //
  196. function sql_numrows($query_id = 0)
  197. {
  198. if( !$query_id )
  199. {
  200. $query_id = $this->result;
  201. }
  202. if( $query_id )
  203. {
  204. return ( !empty($this->limit_offset[$query_id]) ) ? @mssql_num_rows($query_id) - $this->limit_offset[$query_id] : @mssql_num_rows($query_id);
  205. }
  206. else
  207. {
  208. return false;
  209. }
  210. }
  211. function sql_numfields($query_id = 0)
  212. {
  213. if( !$query_id )
  214. {
  215. $query_id = $this->result;
  216. }
  217. return ( $query_id ) ? @mssql_num_fields($query_id) : false;
  218. }
  219. function sql_fieldname($offset, $query_id = 0)
  220. {
  221. if( !$query_id )
  222. {
  223. $query_id = $this->result;
  224. }
  225. return ( $query_id ) ? @mssql_field_name($query_id, $offset) : false;
  226. }
  227. function sql_fieldtype($offset, $query_id = 0)
  228. {
  229. if(!$query_id)
  230. {
  231. $query_id = $this->result;
  232. }
  233. return ( $query_id ) ? @mssql_field_type($query_id, $offset) : false;
  234. }
  235. function sql_fetchrow($query_id = 0)
  236. {
  237. if( !$query_id )
  238. {
  239. $query_id = $this->result;
  240. }
  241. if( $query_id )
  242. {
  243. empty($row);
  244. $row = @mssql_fetch_array($query_id);
  245. while( list($key, $value) = @each($row) )
  246. {
  247. $row[$key] = ($value === ' ') ? '' : stripslashes($value);
  248. }
  249. @reset($row);
  250. return $row;
  251. }
  252. else
  253. {
  254. return false;
  255. }
  256. }
  257. function sql_fetchrowset($query_id = 0)
  258. {
  259. if( !$query_id )
  260. {
  261. $query_id = $this->result;
  262. }
  263. if( $query_id )
  264. {
  265. $i = 0;
  266. empty($rowset);
  267. while( $row = @mssql_fetch_array($query_id))
  268. {
  269. while( list($key, $value) = @each($row) )
  270. {
  271. $rowset[$i][$key] = ($value === ' ') ? '' : stripslashes($value);
  272. }
  273. $i++;
  274. }
  275. @reset($rowset);
  276. return $rowset;
  277. }
  278. else
  279. {
  280. return false;
  281. }
  282. }
  283. function sql_fetchfield($field, $row = -1, $query_id)
  284. {
  285. if( !$query_id )
  286. {
  287. $query_id = $this->result;
  288. }
  289. if( $query_id )
  290. {
  291. if( $row != -1 )
  292. {
  293. if( $this->limit_offset[$query_id] > 0 )
  294. {
  295. $result = ( !empty($this->limit_offset[$query_id]) ) ? @mssql_result($this->result, ($this->limit_offset[$query_id] + $row), $field) : false;
  296. }
  297. else
  298. {
  299. $result = @mssql_result($this->result, $row, $field);
  300. }
  301. }
  302. else
  303. {
  304. if( empty($this->row[$query_id]) )
  305. {
  306. $this->row[$query_id] = @mssql_fetch_array($query_id);
  307. $result = ($this->row[$query_id][$field] === ' ') ? '' : stripslashes($this->row[$query_id][$field]);
  308. }
  309. }
  310. return $result;
  311. }
  312. else
  313. {
  314. return false;
  315. }
  316. }
  317. function sql_rowseek($rownum, $query_id = 0)
  318. {
  319. if( !$query_id )
  320. {
  321. $query_id = $this->result;
  322. }
  323. if( $query_id )
  324. {
  325. return ( !empty($this->limit_offset[$query_id]) ) ? @mssql_data_seek($query_id, ($this->limit_offset[$query_id] + $rownum)) : @mssql_data_seek($query_id, $rownum);
  326. }
  327. else
  328. {
  329. return false;
  330. }
  331. }
  332. function sql_nextid()
  333. {
  334. return ( $this->next_id[$this->db_connect_id] ) ? $this->next_id[$this->db_connect_id] : false;
  335. }
  336. function sql_affectedrows()
  337. {
  338. return ( $this->affected_rows[$this->db_connect_id] ) ? $this->affected_rows[$this->db_connect_id] : false;
  339. }
  340. function sql_freeresult($query_id = 0)
  341. {
  342. if( !$query_id )
  343. {
  344. $query_id = $this->result;
  345. }
  346. return ( $query_id ) ? @mssql_free_result($query_id) : false;
  347. }
  348. function sql_error($query_id = 0)
  349. {
  350. $result['message'] = @mssql_get_last_message();
  351. return $result;
  352. }
  353. } // class sql_db
  354. } // if ... define
  355. ?>