source: trunk/client/class/db/mssql-odbc.php@ 232

Last change on this file since 232 was 232, checked in by luc, 9 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.2 KB
Line 
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,v 1.7 2002/03/20 17:48:30 psotfx 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-odbc");
26
27class sql_db
28{
29
30 var $db_connect_id;
31 var $result;
32
33 var $next_id;
34
35 var $num_rows = array();
36 var $current_row = array();
37 var $field_names = array();
38 var $field_types = array();
39 var $result_rowset = array();
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->server = $sqlserver;
50 $this->user = $sqluser;
51 $this->password = $sqlpassword;
52 $this->dbname = $database;
53
54 $this->db_connect_id = ($this->persistency) ? odbc_pconnect($this->server, $this->user, $this->password) : odbc_connect($this->server, $this->user, $this->password);
55
56 return ( $this->db_connect_id ) ? $this->db_connect_id : false;
57 }
58 //
59 // Other base methods
60 //
61 function sql_close()
62 {
63 if($this->db_connect_id)
64 {
65 if( $this->in_transaction )
66 {
67 @odbc_commit($this->db_connect_id);
68 }
69
70 if( count($this->result_rowset) )
71 {
72 unset($this->result_rowset);
73 unset($this->field_names);
74 unset($this->field_types);
75 unset($this->num_rows);
76 unset($this->current_row);
77 }
78
79 return @odbc_close($this->db_connect_id);
80 }
81 else
82 {
83 return false;
84 }
85 }
86
87 //
88 // Query method
89 //
90 function sql_query($query = "", $transaction = FALSE)
91 {
92 if( $query != "" )
93 {
94 $this->num_queries++;
95
96 if( $transaction == BEGIN_TRANSACTION && !$this->in_transaction )
97 {
98 if( !odbc_autocommit($this->db_connect_id, false) )
99 {
100 return false;
101 }
102 $this->in_transaction = TRUE;
103 }
104
105 if( preg_match("/^SELECT(.*?)(LIMIT ([0-9]+)[, ]*([0-9]+)*)?$/s", $query, $limits) )
106 {
107 $query = $limits[1];
108
109 if( !empty($limits[2]) )
110 {
111 $row_offset = ( $limits[4] ) ? $limits[3] : "";
112 $num_rows = ( $limits[4] ) ? $limits[4] : $limits[3];
113
114 $query = "TOP " . ( $row_offset + $num_rows ) . $query;
115 }
116
117 $this->result = odbc_exec($this->db_connect_id, "SELECT $query");
118
119 if( $this->result )
120 {
121 if( empty($this->field_names[$this->result]) )
122 {
123 for($i = 1; $i < odbc_num_fields($this->result) + 1; $i++)
124 {
125 $this->field_names[$this->result][] = odbc_field_name($this->result, $i);
126 $this->field_types[$this->result][] = odbc_field_type($this->result, $i);
127 }
128 }
129
130 $this->current_row[$this->result] = 0;
131 $this->result_rowset[$this->result] = array();
132
133 $row_outer = ( isset($row_offset) ) ? $row_offset + 1 : 1;
134 $row_outer_max = ( isset($num_rows) ) ? $row_offset + $num_rows + 1 : 1E9;
135 $row_inner = 0;
136
137 while( odbc_fetch_row($this->result, $row_outer) && $row_outer < $row_outer_max )
138 {
139 for($j = 0; $j < count($this->field_names[$this->result]); $j++)
140 {
141 $this->result_rowset[$this->result][$row_inner][$this->field_names[$this->result][$j]] = stripslashes(odbc_result($this->result, $j + 1));
142 }
143
144 $row_outer++;
145 $row_inner++;
146 }
147
148 $this->num_rows[$this->result] = count($this->result_rowset[$this->result]);
149 }
150
151 }
152 else if( eregi("^INSERT ", $query) )
153 {
154 $this->result = odbc_exec($this->db_connect_id, $query);
155
156 if( $this->result )
157 {
158 $result_id = odbc_exec($this->db_connect_id, "SELECT @@IDENTITY");
159 if( $result_id )
160 {
161 if( odbc_fetch_row($result_id) )
162 {
163 $this->next_id[$this->db_connect_id] = odbc_result($result_id, 1);
164 $this->affected_rows[$this->db_connect_id] = odbc_num_rows($this->result);
165 }
166 }
167 }
168 }
169 else
170 {
171 $this->result = odbc_exec($this->db_connect_id, $query);
172
173 if( $this->result )
174 {
175 $this->affected_rows[$this->db_connect_id] = odbc_num_rows($this->result);
176 }
177 }
178
179 if( !$this->result )
180 {
181 if( $this->in_transaction )
182 {
183 odbc_rollback($this->db_connect_id);
184 odbc_autocommit($this->db_connect_id, true);
185 $this->in_transaction = FALSE;
186 }
187
188 return false;
189 }
190
191 if( $transaction == END_TRANSACTION && $this->in_transaction )
192 {
193 $this->in_transaction = FALSE;
194
195 if ( !odbc_commit($this->db_connect_id) )
196 {
197 odbc_rollback($this->db_connect_id);
198 odbc_autocommit($this->db_connect_id, true);
199 return false;
200 }
201 odbc_autocommit($this->db_connect_id, true);
202 }
203
204 odbc_free_result($this->result);
205
206 return $this->result;
207 }
208 else
209 {
210 if( $transaction == END_TRANSACTION && $this->in_transaction )
211 {
212 $this->in_transaction = FALSE;
213
214 if ( !@odbc_commit($this->db_connect_id) )
215 {
216 odbc_rollback($this->db_connect_id);
217 odbc_autocommit($this->db_connect_id, true);
218 return false;
219 }
220 odbc_autocommit($this->db_connect_id, true);
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 return ( $query_id ) ? $this->num_rows[$query_id] : false;
238 }
239
240 function sql_numfields($query_id = 0)
241 {
242 if( !$query_id )
243 {
244 $query_id = $this->result;
245 }
246
247 return ( $query_id ) ? count($this->field_names[$query_id]) : false;
248 }
249
250 function sql_fieldname($offset, $query_id = 0)
251 {
252 if( !$query_id )
253 {
254 $query_id = $this->result;
255 }
256
257 return ( $query_id ) ? $this->field_names[$query_id][$offset] : false;
258 }
259
260 function sql_fieldtype($offset, $query_id = 0)
261 {
262 if( !$query_id )
263 {
264 $query_id = $this->result;
265 }
266
267 return ( $query_id ) ? $this->field_types[$query_id][$offset] : false;
268 }
269
270 function sql_fetchrow($query_id = 0)
271 {
272 if( !$query_id )
273 {
274 $query_id = $this->result;
275 }
276
277 if( $query_id )
278 {
279 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;
280 }
281 else
282 {
283 return false;
284 }
285 }
286
287 function sql_fetchrowset($query_id = 0)
288 {
289 if( !$query_id )
290 {
291 $query_id = $this->result;
292 }
293
294 if( $query_id )
295 {
296 return ( $this->num_rows[$query_id] ) ? $this->result_rowset[$query_id] : false;
297 }
298 else
299 {
300 return false;
301 }
302 }
303
304 function sql_fetchfield($field, $row = -1, $query_id = 0)
305 {
306 if( !$query_id )
307 {
308 $query_id = $this->result;
309 }
310
311 if( $query_id )
312 {
313 if( $row < $this->num_rows[$query_id] )
314 {
315 $getrow = ( $row == -1 ) ? $this->current_row[$query_id] - 1 : $row;
316
317 return $this->result_rowset[$query_id][$getrow][$this->field_names[$query_id][$field]];
318
319 }
320 else
321 {
322 return false;
323 }
324 }
325 else
326 {
327 return false;
328 }
329 }
330
331 function sql_rowseek($offset, $query_id = 0)
332 {
333 if( !$query_id )
334 {
335 $query_id = $this->result;
336 }
337
338 if( $query_id )
339 {
340 $this->current_row[$query_id] = $offset - 1;
341 return true;
342 }
343 else
344 {
345 return false;
346 }
347 }
348
349 function sql_nextid()
350 {
351 return ( $this->next_id[$this->db_connect_id] ) ? $this->next_id[$this->db_connect_id] : false;
352 }
353
354 function sql_affectedrows()
355 {
356 return ( $this->affected_rows[$this->db_connect_id] ) ? $this->affected_rows[$this->db_connect_id] : false;
357 }
358
359 function sql_freeresult($query_id = 0)
360 {
361 if( !$query_id )
362 {
363 $query_id = $this->result;
364 }
365
366 unset($this->num_rows[$query_id]);
367 unset($this->current_row[$query_id]);
368 unset($this->result_rowset[$query_id]);
369 unset($this->field_names[$query_id]);
370 unset($this->field_types[$query_id]);
371
372 return true;
373 }
374
375 function sql_error()
376 {
377 $error['code'] = odbc_error($this->db_connect_id);
378 $error['message'] = odbc_errormsg($this->db_connect_id);
379
380 return $error;
381 }
382
383} // class sql_db
384
385} // if ... define
386
387?>
Note: See TracBrowser for help on using the repository browser.