2 /***************************************************************************
5 * begin : Saturday, Feb 13, 2001
6 * copyright : (C) 2001 The phpBB Group
7 * email : support@phpbb.com
9 * $Id: mssql-odbc.php,v 1.7 2002/03/20 17:48:30 psotfx 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-odbc");
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();
46 function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true)
48 $this->persistency = $persistency;
49 $this->server = $sqlserver;
50 $this->user = $sqluser;
51 $this->password = $sqlpassword;
52 $this->dbname = $database;
54 $this->db_connect_id = ($this->persistency) ? odbc_pconnect($this->server, $this->user, $this->password) : odbc_connect($this->server, $this->user, $this->password);
56 return ( $this->db_connect_id ) ? $this->db_connect_id : false;
63 if($this->db_connect_id)
65 if( $this->in_transaction )
67 @odbc_commit($this->db_connect_id);
70 if( count($this->result_rowset) )
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);
79 return @odbc_close($this->db_connect_id);
90 function sql_query($query = "", $transaction = FALSE)
96 if( $transaction == BEGIN_TRANSACTION && !$this->in_transaction )
98 if( !odbc_autocommit($this->db_connect_id, false) )
102 $this->in_transaction = TRUE;
105 if( preg_match("/^SELECT(.*?)(LIMIT ([0-9]+)[, ]*([0-9]+)*)?$/s", $query, $limits) )
109 if( !empty($limits[2]) )
111 $row_offset = ( $limits[4] ) ? $limits[3] : "";
112 $num_rows = ( $limits[4] ) ? $limits[4] : $limits[3];
114 $query = "TOP " . ( $row_offset + $num_rows ) . $query;
117 $this->result = odbc_exec($this->db_connect_id, "SELECT $query");
121 if( empty($this->field_names[$this->result]) )
123 for($i = 1; $i < odbc_num_fields($this->result) + 1; $i++)
125 $this->field_names[$this->result][] = odbc_field_name($this->result, $i);
126 $this->field_types[$this->result][] = odbc_field_type($this->result, $i);
130 $this->current_row[$this->result] = 0;
131 $this->result_rowset[$this->result] = array();
133 $row_outer = ( isset($row_offset) ) ? $row_offset + 1 : 1;
134 $row_outer_max = ( isset($num_rows) ) ? $row_offset + $num_rows + 1 : 1E9;
137 while( odbc_fetch_row($this->result, $row_outer) && $row_outer < $row_outer_max )
139 for($j = 0; $j < count($this->field_names[$this->result]); $j++)
141 $this->result_rowset[$this->result][$row_inner][$this->field_names[$this->result][$j]] = stripslashes(odbc_result($this->result, $j + 1));
148 $this->num_rows[$this->result] = count($this->result_rowset[$this->result]);
152 else if( eregi("^INSERT ", $query) )
154 $this->result = odbc_exec($this->db_connect_id, $query);
158 $result_id = odbc_exec($this->db_connect_id, "SELECT @@IDENTITY");
161 if( odbc_fetch_row($result_id) )
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);
171 $this->result = odbc_exec($this->db_connect_id, $query);
175 $this->affected_rows[$this->db_connect_id] = odbc_num_rows($this->result);
181 if( $this->in_transaction )
183 odbc_rollback($this->db_connect_id);
184 odbc_autocommit($this->db_connect_id, true);
185 $this->in_transaction = FALSE;
191 if( $transaction == END_TRANSACTION && $this->in_transaction )
193 $this->in_transaction = FALSE;
195 if ( !odbc_commit($this->db_connect_id) )
197 odbc_rollback($this->db_connect_id);
198 odbc_autocommit($this->db_connect_id, true);
201 odbc_autocommit($this->db_connect_id, true);
204 odbc_free_result($this->result);
206 return $this->result;
210 if( $transaction == END_TRANSACTION && $this->in_transaction )
212 $this->in_transaction = FALSE;
214 if ( !@odbc_commit($this->db_connect_id) )
216 odbc_rollback($this->db_connect_id);
217 odbc_autocommit($this->db_connect_id, true);
220 odbc_autocommit($this->db_connect_id, true);
228 // Other query methods
230 function sql_numrows($query_id = 0)
234 $query_id = $this->result;
237 return ( $query_id ) ? $this->num_rows[$query_id] : false;
240 function sql_numfields($query_id = 0)
244 $query_id = $this->result;
247 return ( $query_id ) ? count($this->field_names[$query_id]) : false;
250 function sql_fieldname($offset, $query_id = 0)
254 $query_id = $this->result;
257 return ( $query_id ) ? $this->field_names[$query_id][$offset] : false;
260 function sql_fieldtype($offset, $query_id = 0)
264 $query_id = $this->result;
267 return ( $query_id ) ? $this->field_types[$query_id][$offset] : false;
270 function sql_fetchrow($query_id = 0)
274 $query_id = $this->result;
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;
287 function sql_fetchrowset($query_id = 0)
291 $query_id = $this->result;
296 return ( $this->num_rows[$query_id] ) ? $this->result_rowset[$query_id] : false;
304 function sql_fetchfield($field, $row = -1, $query_id = 0)
308 $query_id = $this->result;
313 if( $row < $this->num_rows[$query_id] )
315 $getrow = ( $row == -1 ) ? $this->current_row[$query_id] - 1 : $row;
317 return $this->result_rowset[$query_id][$getrow][$this->field_names[$query_id][$field]];
331 function sql_rowseek($offset, $query_id = 0)
335 $query_id = $this->result;
340 $this->current_row[$query_id] = $offset - 1;
349 function sql_nextid()
351 return ( $this->next_id[$this->db_connect_id] ) ? $this->next_id[$this->db_connect_id] : false;
354 function sql_affectedrows()
356 return ( $this->affected_rows[$this->db_connect_id] ) ? $this->affected_rows[$this->db_connect_id] : false;
359 function sql_freeresult($query_id = 0)
363 $query_id = $this->result;
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]);
377 $error['code'] = odbc_error($this->db_connect_id);
378 $error['message'] = odbc_errormsg($this->db_connect_id);