2 /***************************************************************************
5 * begin : Saturday, Feb 13, 2001
6 * copyright : (C) 2001 The phpBB Group
7 * email : supportphpbb.com
9 * $Id: mssql.php,v 1.22.2.4 2006/03/09 19:57:37 grahamje Exp $
11 ***************************************************************************/
13 /***************************************************************************
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 ***************************************************************************/
22 if(!defined("SQL_LAYER"))
25 define("SQL_LAYER","mssql");
34 var $in_transaction = 0;
37 var $rowset = array();
39 var $query_limit_success;
46 function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true)
48 $this->persistency = $persistency;
49 $this->user = $sqluser;
50 $this->password = $sqlpassword;
51 $this->server = $sqlserver;
52 $this->dbname = $database;
54 $this->db_connect_id = ( $this->persistency ) ? @mssql_pconnect($this->server, $this->user, $this->password) : @mssql_connect($this->server, $this->user, $this->password);
56 if( $this->db_connect_id && $this->dbname != "" )
58 if( !mssql_select_db($this->dbname, $this->db_connect_id) )
60 mssql_close($this->db_connect_id);
65 return $this->db_connect_id;
73 if($this->db_connect_id)
76 // Commit any remaining transactions
78 if( $this->in_transaction )
80 @mssql_query("COMMIT", $this->db_connect_id);
83 return @mssql_close($this->db_connect_id);
95 function sql_query($query = '', $transaction = FALSE)
98 // Remove any pre-existing queries
100 unset($this->result);
105 $this->num_queries++;
107 if ( $transaction == BEGIN_TRANSACTION && !$this->in_transaction )
109 if ( !@mssql_query('BEGIN TRANSACTION', $this->db_connect_id) )
113 $this->in_transaction = TRUE;
117 // Does query contain any LIMIT code? If so pull out relevant start and num_results
118 // This isn't terribly easy with MSSQL, whatever you do will potentially impact
119 // performance compared to an 'in-built' limit
121 // Another issue is the 'lack' of a returned true value when a query is valid but has
122 // no result set (as with all the other DB interfaces). It seems though that it's
123 // 'fair' to say that if a query returns a false result (ie. no resource id) then the
124 // SQL was valid but had no result set. If the query returns nothing but the rowcount
125 // returns something then there's a problem. This may well be a false assumption though
126 // ... needs checking under Windows itself.
128 if( preg_match('#^SELECT(.*?)(LIMIT ([0-9]+)[, ]*([0-9]+)*)?$#s', $query, $limits) )
132 if( !empty($limits[2]) )
134 $row_offset = ( $limits[4] ) ? $limits[3] : "";
135 $num_rows = ( $limits[4] ) ? $limits[4] : $limits[3];
137 $query = 'TOP ' . ( $row_offset + $num_rows ) . $query;
140 $this->result = @mssql_query("SELECT $query", $this->db_connect_id);
144 $this->limit_offset[$this->result] = ( !empty($row_offset) ) ? $row_offset : 0;
146 if( $row_offset > 0 )
148 @mssql_data_seek($this->result, $row_offset);
152 else if( preg_match('#^INSERT #i', $query) )
154 if( @mssql_query($query, $this->db_connect_id) )
156 $this->result = time() + microtime();
158 $result_id = @mssql_query('SELECT @@IDENTITY AS id, @@ROWCOUNT as affected', $this->db_connect_id);
161 if( $row = @mssql_fetch_array($result_id) )
163 $this->next_id[$this->db_connect_id] = $row['id'];
164 $this->affected_rows[$this->db_connect_id] = $row['affected'];
171 if( @mssql_query($query, $this->db_connect_id) )
173 $this->result = time() + microtime();
175 $result_id = @mssql_query('SELECT @@ROWCOUNT as affected', $this->db_connect_id);
178 if( $row = @mssql_fetch_array($result_id) )
180 $this->affected_rows[$this->db_connect_id] = $row['affected'];
188 if( $this->in_transaction )
190 @mssql_query('ROLLBACK', $this->db_connect_id);
191 $this->in_transaction = FALSE;
197 if( $transaction == END_TRANSACTION && $this->in_transaction )
199 $this->in_transaction = FALSE;
201 if( !@mssql_query('COMMIT', $this->db_connect_id) )
203 @mssql_query("ROLLBACK", $this->db_connect_id);
208 return $this->result;
212 if( $transaction == END_TRANSACTION && $this->in_transaction )
214 $this->in_transaction = FALSE;
216 if( !@mssql_query('COMMIT', $this->db_connect_id) )
218 @mssql_query('ROLLBACK', $this->db_connect_id);
228 // Other query methods
230 function sql_numrows($query_id = 0)
234 $query_id = $this->result;
239 return ( !empty($this->limit_offset[$query_id]) ) ? @mssql_num_rows($query_id) - $this->limit_offset[$query_id] : @mssql_num_rows($query_id);
247 function sql_numfields($query_id = 0)
251 $query_id = $this->result;
254 return ( $query_id ) ? @mssql_num_fields($query_id) : false;
257 function sql_fieldname($offset, $query_id = 0)
261 $query_id = $this->result;
264 return ( $query_id ) ? @mssql_field_name($query_id, $offset) : false;
267 function sql_fieldtype($offset, $query_id = 0)
271 $query_id = $this->result;
274 return ( $query_id ) ? @mssql_field_type($query_id, $offset) : false;
277 function sql_fetchrow($query_id = 0)
281 $query_id = $this->result;
288 $row = @mssql_fetch_array($query_id);
290 while( list($key, $value) = @each($row) )
292 $row[$key] = ($value === ' ') ? '' : stripslashes($value);
304 function sql_fetchrowset($query_id = 0)
308 $query_id = $this->result;
316 while( $row = @mssql_fetch_array($query_id))
318 while( list($key, $value) = @each($row) )
320 $rowset[$i][$key] = ($value === ' ') ? '' : stripslashes($value);
334 function sql_fetchfield($field, $row = -1, $query_id)
338 $query_id = $this->result;
345 if( $this->limit_offset[$query_id] > 0 )
347 $result = ( !empty($this->limit_offset[$query_id]) ) ? @mssql_result($this->result, ($this->limit_offset[$query_id] + $row), $field) : false;
351 $result = @mssql_result($this->result, $row, $field);
356 if( empty($this->row[$query_id]) )
358 $this->row[$query_id] = @mssql_fetch_array($query_id);
359 $result = ($this->row[$query_id][$field] === ' ') ? '' : stripslashes($this->row[$query_id][$field]);
371 function sql_rowseek($rownum, $query_id = 0)
375 $query_id = $this->result;
380 return ( !empty($this->limit_offset[$query_id]) ) ? @mssql_data_seek($query_id, ($this->limit_offset[$query_id] + $rownum)) : @mssql_data_seek($query_id, $rownum);
388 function sql_nextid()
390 return ( $this->next_id[$this->db_connect_id] ) ? $this->next_id[$this->db_connect_id] : false;
393 function sql_affectedrows()
395 return ( $this->affected_rows[$this->db_connect_id] ) ? $this->affected_rows[$this->db_connect_id] : false;
398 function sql_freeresult($query_id = 0)
402 $query_id = $this->result;
405 return ( $query_id ) ? @mssql_free_result($query_id) : false;
408 function sql_error($query_id = 0)
410 $result['message'] = @mssql_get_last_message();