source: trunk/client/class/db/mssql.php@ 400

Last change on this file since 400 was 232, checked in by luc, 10 years ago
  • inclusione del tema per mobile Futura2 - inclusione del widget per la ricerca dei candidati - inclusione della segnalazione dell'installazione - altre piccole migliorie.

Inserita classe e file mancanti

File size: 8.7 KB
Line 
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 *
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.
19 *
20 ***************************************************************************/
21
22if(!defined("SQL_LAYER"))
23{
24
25define("SQL_LAYER","mssql");
26
27class sql_db
28{
29
30 var $db_connect_id;
31 var $result;
32
33 var $next_id;
34 var $in_transaction = 0;
35
36 var $row = array();
37 var $rowset = array();
38 var $limit_offset;
39 var $query_limit_success;
40
41 var $num_queries = 0;
42
43 //
44 // Constructor
45 //
46 function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true)
47 {
48 $this->persistency = $persistency;
49 $this->user = $sqluser;
50 $this->password = $sqlpassword;
51 $this->server = $sqlserver;
52 $this->dbname = $database;
53
54 $this->db_connect_id = ( $this->persistency ) ? @mssql_pconnect($this->server, $this->user, $this->password) : @mssql_connect($this->server, $this->user, $this->password);
55
56 if( $this->db_connect_id && $this->dbname != "" )
57 {
58 if( !mssql_select_db($this->dbname, $this->db_connect_id) )
59 {
60 mssql_close($this->db_connect_id);
61 return false;
62 }
63 }
64
65 return $this->db_connect_id;
66 }
67
68 //
69 // Other base methods
70 //
71 function sql_close()
72 {
73 if($this->db_connect_id)
74 {
75 //
76 // Commit any remaining transactions
77 //
78 if( $this->in_transaction )
79 {
80 @mssql_query("COMMIT", $this->db_connect_id);
81 }
82
83 return @mssql_close($this->db_connect_id);
84 }
85 else
86 {
87 return false;
88 }
89 }
90
91
92 //
93 // Query method
94 //
95 function sql_query($query = '', $transaction = FALSE)
96 {
97 //
98 // Remove any pre-existing queries
99 //
100 unset($this->result);
101 unset($this->row);
102
103 if ( $query != '' )
104 {
105 $this->num_queries++;
106
107 if ( $transaction == BEGIN_TRANSACTION && !$this->in_transaction )
108 {
109 if ( !@mssql_query('BEGIN TRANSACTION', $this->db_connect_id) )
110 {
111 return false;
112 }
113 $this->in_transaction = TRUE;
114 }
115
116 //
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
120 //
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.
127 //
128 if( preg_match('#^SELECT(.*?)(LIMIT ([0-9]+)[, ]*([0-9]+)*)?$#s', $query, $limits) )
129 {
130 $query = $limits[1];
131
132 if( !empty($limits[2]) )
133 {
134 $row_offset = ( $limits[4] ) ? $limits[3] : "";
135 $num_rows = ( $limits[4] ) ? $limits[4] : $limits[3];
136
137 $query = 'TOP ' . ( $row_offset + $num_rows ) . $query;
138 }
139
140 $this->result = @mssql_query("SELECT $query", $this->db_connect_id);
141
142 if( $this->result )
143 {
144 $this->limit_offset[$this->result] = ( !empty($row_offset) ) ? $row_offset : 0;
145
146 if( $row_offset > 0 )
147 {
148 @mssql_data_seek($this->result, $row_offset);
149 }
150 }
151 }
152 else if( preg_match('#^INSERT #i', $query) )
153 {
154 if( @mssql_query($query, $this->db_connect_id) )
155 {
156 $this->result = time() + microtime();
157
158 $result_id = @mssql_query('SELECT @@IDENTITY AS id, @@ROWCOUNT as affected', $this->db_connect_id);
159 if( $result_id )
160 {
161 if( $row = @mssql_fetch_array($result_id) )
162 {
163 $this->next_id[$this->db_connect_id] = $row['id'];
164 $this->affected_rows[$this->db_connect_id] = $row['affected'];
165 }
166 }
167 }
168 }
169 else
170 {
171 if( @mssql_query($query, $this->db_connect_id) )
172 {
173 $this->result = time() + microtime();
174
175 $result_id = @mssql_query('SELECT @@ROWCOUNT as affected', $this->db_connect_id);
176 if( $result_id )
177 {
178 if( $row = @mssql_fetch_array($result_id) )
179 {
180 $this->affected_rows[$this->db_connect_id] = $row['affected'];
181 }
182 }
183 }
184 }
185
186 if( !$this->result )
187 {
188 if( $this->in_transaction )
189 {
190 @mssql_query('ROLLBACK', $this->db_connect_id);
191 $this->in_transaction = FALSE;
192 }
193
194 return false;
195 }
196
197 if( $transaction == END_TRANSACTION && $this->in_transaction )
198 {
199 $this->in_transaction = FALSE;
200
201 if( !@mssql_query('COMMIT', $this->db_connect_id) )
202 {
203 @mssql_query("ROLLBACK", $this->db_connect_id);
204 return false;
205 }
206 }
207
208 return $this->result;
209 }
210 else
211 {
212 if( $transaction == END_TRANSACTION && $this->in_transaction )
213 {
214 $this->in_transaction = FALSE;
215
216 if( !@mssql_query('COMMIT', $this->db_connect_id) )
217 {
218 @mssql_query('ROLLBACK', $this->db_connect_id);
219 return false;
220 }
221 }
222
223 return true;
224 }
225 }
226
227 //
228 // Other query methods
229 //
230 function sql_numrows($query_id = 0)
231 {
232 if( !$query_id )
233 {
234 $query_id = $this->result;
235 }
236
237 if( $query_id )
238 {
239 return ( !empty($this->limit_offset[$query_id]) ) ? @mssql_num_rows($query_id) - $this->limit_offset[$query_id] : @mssql_num_rows($query_id);
240 }
241 else
242 {
243 return false;
244 }
245 }
246
247 function sql_numfields($query_id = 0)
248 {
249 if( !$query_id )
250 {
251 $query_id = $this->result;
252 }
253
254 return ( $query_id ) ? @mssql_num_fields($query_id) : false;
255 }
256
257 function sql_fieldname($offset, $query_id = 0)
258 {
259 if( !$query_id )
260 {
261 $query_id = $this->result;
262 }
263
264 return ( $query_id ) ? @mssql_field_name($query_id, $offset) : false;
265 }
266
267 function sql_fieldtype($offset, $query_id = 0)
268 {
269 if(!$query_id)
270 {
271 $query_id = $this->result;
272 }
273
274 return ( $query_id ) ? @mssql_field_type($query_id, $offset) : false;
275 }
276
277 function sql_fetchrow($query_id = 0)
278 {
279 if( !$query_id )
280 {
281 $query_id = $this->result;
282 }
283
284 if( $query_id )
285 {
286 empty($row);
287
288 $row = @mssql_fetch_array($query_id);
289
290 while( list($key, $value) = @each($row) )
291 {
292 $row[$key] = ($value === ' ') ? '' : stripslashes($value);
293 }
294 @reset($row);
295
296 return $row;
297 }
298 else
299 {
300 return false;
301 }
302 }
303
304 function sql_fetchrowset($query_id = 0)
305 {
306 if( !$query_id )
307 {
308 $query_id = $this->result;
309 }
310
311 if( $query_id )
312 {
313 $i = 0;
314 empty($rowset);
315
316 while( $row = @mssql_fetch_array($query_id))
317 {
318 while( list($key, $value) = @each($row) )
319 {
320 $rowset[$i][$key] = ($value === ' ') ? '' : stripslashes($value);
321 }
322 $i++;
323 }
324 @reset($rowset);
325
326 return $rowset;
327 }
328 else
329 {
330 return false;
331 }
332 }
333
334 function sql_fetchfield($field, $row = -1, $query_id)
335 {
336 if( !$query_id )
337 {
338 $query_id = $this->result;
339 }
340
341 if( $query_id )
342 {
343 if( $row != -1 )
344 {
345 if( $this->limit_offset[$query_id] > 0 )
346 {
347 $result = ( !empty($this->limit_offset[$query_id]) ) ? @mssql_result($this->result, ($this->limit_offset[$query_id] + $row), $field) : false;
348 }
349 else
350 {
351 $result = @mssql_result($this->result, $row, $field);
352 }
353 }
354 else
355 {
356 if( empty($this->row[$query_id]) )
357 {
358 $this->row[$query_id] = @mssql_fetch_array($query_id);
359 $result = ($this->row[$query_id][$field] === ' ') ? '' : stripslashes($this->row[$query_id][$field]);
360 }
361 }
362
363 return $result;
364 }
365 else
366 {
367 return false;
368 }
369 }
370
371 function sql_rowseek($rownum, $query_id = 0)
372 {
373 if( !$query_id )
374 {
375 $query_id = $this->result;
376 }
377
378 if( $query_id )
379 {
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);
381 }
382 else
383 {
384 return false;
385 }
386 }
387
388 function sql_nextid()
389 {
390 return ( $this->next_id[$this->db_connect_id] ) ? $this->next_id[$this->db_connect_id] : false;
391 }
392
393 function sql_affectedrows()
394 {
395 return ( $this->affected_rows[$this->db_connect_id] ) ? $this->affected_rows[$this->db_connect_id] : false;
396 }
397
398 function sql_freeresult($query_id = 0)
399 {
400 if( !$query_id )
401 {
402 $query_id = $this->result;
403 }
404
405 return ( $query_id ) ? @mssql_free_result($query_id) : false;
406 }
407
408 function sql_error($query_id = 0)
409 {
410 $result['message'] = @mssql_get_last_message();
411 return $result;
412 }
413
414} // class sql_db
415
416} // if ... define
417
418?>
Note: See TracBrowser for help on using the repository browser.