Updated classes, fixed slashing issue and maybe reduced overhead a little, perhaps

git-svn-id: file:///svn/phpbb/trunk@1624 89ea8834-ac86-4346-8a33-228a782c2dd0
This commit is contained in:
Paul S. Owen 2001-12-18 01:13:37 +00:00
parent 460334ea59
commit 4764328c9f
2 changed files with 361 additions and 453 deletions

View file

@ -28,42 +28,33 @@ class sql_db
{
var $db_connect_id;
var $query_result;
var $query_resultset;
var $query_numrows;
var $result_ids;
var $result;
var $next_id;
var $row;
var $row_index;
var $num_rows;
var $current_row;
var $field_names;
var $field_types;
var $result_rowset;
var $num_queries = 0;
//
// Constructor
//
function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = false)
function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true)
{
$this->persistency = $persistency;
$this->server = $sqlserver;
$this->user = $sqluser;
$this->password = $sqlpassword;
$this->dbname = $database;
$this->server = $sqlserver;
if($this->persistency)
{
$this->db_connect_id = odbc_pconnect($this->server, "", "");
}
else
{
$this->db_connect_id = odbc_connect($this->server, "", "");
}
$this->db_connect_id = ($this->persistency) ? odbc_pconnect($this->server, $this->user, $this->password) : odbc_connect($this->server, $this->user, $this->password);
if($this->db_connect_id)
{
return $this->db_connect_id;
}
else
{
return false;
}
return ( $this->db_connect_id ) ? $this->db_connect_id : false;
}
//
// Other base methods
@ -72,12 +63,21 @@ class sql_db
{
if($this->db_connect_id)
{
if($this->query_result)
if( $this->in_transaction )
{
@odbc_free_result($this->query_result);
@odbc_commit($this->db_connect_id);
}
$result = @odbc_close($this->db_connect_id);
return $result;
if( count($this->result_rowset) )
{
unset($this->result_rowset);
unset($this->field_names);
unset($this->field_types);
unset($this->num_rows);
unset($this->current_row);
}
return @odbc_close($this->db_connect_id);
}
else
{
@ -85,114 +85,120 @@ class sql_db
}
}
//
// Query method
//
function sql_query($query = "", $transaction = FALSE)
{
//
// Remove any pre-existing queries
//
unset($this->query_result);
unset($this->row);
if( $query != "" )
{
$this->num_queries++;
$query = str_replace("LOWER(", "LCASE(", $query);
if(!eregi("^INSERT ",$query))
if( $transaction == BEGIN_TRANSACTION )
{
if(eregi("LIMIT", $query))
if( !odbc_autocommit($this->db_connect_id, false) )
{
preg_match("/^(.*)LIMIT ([0-9]+)[, ]*([0-9]+)*/s", $query, $limits);
return false;
}
$this->in_transaction = TRUE;
}
if( preg_match("/^SELECT(.*?)(LIMIT ([0-9]+)[, ]*([0-9]+)*)?$/s", $query, $limits) )
{
$query = $limits[1];
if($limits[3])
if( !empty($limits[2]) )
{
$row_offset = $limits[2];
$num_rows = $limits[3];
}
else
{
$row_offset = 0;
$num_rows = $limits[2];
$row_offset = ( $limits[4] ) ? $limits[3] : "";
$num_rows = ( $limits[4] ) ? $limits[4] : $limits[3];
$query = "TOP " . ( $row_offset + $num_rows ) . $query;
}
$this->query_result = odbc_exec($this->db_connect_id, $query);
$this->result = odbc_exec($this->db_connect_id, "SELECT $query");
$query_limit_offset = $row_offset;
$this->result_numrows[$this->query_result] = $num_rows;
}
else
if( $this->result )
{
$this->query_result = odbc_exec($this->db_connect_id, $query);
$row_offset = 0;
$this->result_numrows[$this->query_result] = 5E6;
if( empty($this->field_names[$this->result]) )
{
for($i = 1; $i < odbc_num_fields($this->result) + 1; $i++)
{
$this->field_names[$this->result][] = odbc_field_name($this->result, $i);
$this->field_types[$this->result][] = odbc_field_type($this->result, $i);
}
}
$result_id = $this->query_result;
if($this->query_result && eregi("^SELECT", $query))
{
$this->current_row[$this->result] = 0;
$this->result_rowset[$this->result] = array();
for($i = 1; $i < odbc_num_fields($result_id)+1; $i++)
$row_outer = ( isset($row_offset) ) ? $row_offset + 1 : 1;
$row_outer_max = ( isset($num_rows) ) ? $num_rows + 1 : 1E9;
$row_inner = 0;
while( odbc_fetch_row($this->result, $row_outer) && $row_outer < $row_outer_max )
{
$this->result_field_names[$result_id][] = odbc_field_name($result_id, $i);
for($j = 0; $j < count($this->field_names[$this->result]); $j++)
{
$this->result_rowset[$this->result][$row_inner][$this->field_names[$this->result][$j]] = stripslashes(odbc_result($this->result, $j + 1));
}
$i = $row_offset + 1;
$k = 0;
while(odbc_fetch_row($result_id, $i) && $k < $this->result_numrows[$result_id])
{
for($j = 1; $j < count($this->result_field_names[$result_id])+1; $j++)
{
$this->result_rowset[$result_id][$k][$this->result_field_names[$result_id][$j-1]] = odbc_result($result_id, $j);
}
$i++;
$k++;
$row_outer++;
$row_inner++;
}
$this->result_numrows[$result_id] = $k;
$this->row_index[$result_id] = 0;
$this->num_rows[$this->result] = count($this->result_rowset[$this->result]);
odbc_free_result($this->result);
}
else
}
else if( eregi("^INSERT ", $query) )
{
$this->result_numrows[$result_id] = @odbc_num_rows($result_id);
$this->row_index[$result_id] = 0;
$this->result = odbc_exec($this->db_connect_id, str_replace("\'", "''", $query));
if( $this->result )
{
$result_id = odbc_exec($this->db_connect_id, "SELECT @@IDENTITY");
if( $result_id )
{
if( odbc_fetch_row($result_id) )
{
$this->next_id[$this->db_connect_id] = odbc_result($result_id, 1);
$this->affected_rows[$this->db_connect_id] = odbc_num_rows($this->result);
}
}
}
}
else
{
if(eregi("^(INSERT|UPDATE) ", $query))
$this->result = odbc_exec($this->db_connect_id, str_replace("\'", "''", $query));
if( $this->result )
{
$query = preg_replace("/\\\'/s", "''", $query);
$this->affected_rows[$this->db_connect_id] = odbc_num_rows($this->result);
}
}
$this->query_result = odbc_exec($this->db_connect_id, $query);
if($this->query_result)
if( !$this->result )
{
$sql_id = "SELECT @@IDENTITY";
$id_result = odbc_exec($this->db_connect_id, $sql_id);
if($id_result)
if( $this->in_transaction )
{
$row_result = odbc_fetch_row($id_result);
if($row_result)
odbc_rollback($this->db_connect_id);
odbc_autocommit($this->db_connect_id, true);
$this->in_transaction = FALSE;
}
return false;
}
if( $transaction == END_TRANSACTION && $this->in_transaction )
{
$this->next_id[$this->query_result] = odbc_result($id_result, 1);
}
}
odbc_commit($this->db_connect_id);
odbc_autocommit($this->db_connect_id, true);
$this->in_transaction = FALSE;
}
$this->query_limit_offset[$this->query_result] = 0;
$this->result_numrows[$this->query_result] = 0;
}
return $this->query_result;
return $this->result;
}
else
{
@ -207,141 +213,90 @@ class sql_db
{
if( !$query_id )
{
$query_id = $this->query_result;
}
if($query_id)
{
return $this->result_numrows[$query_id];
}
else
{
return false;
}
}
function sql_affectedrows($query_id = 0)
{
if(!$query_id)
{
$query_id = $this->query_result;
}
if($query_id)
{
return $this->result_numrows[$query_id];
}
else
{
return false;
$query_id = $this->result;
}
return ( $query_id ) ? $this->num_rows[$query_id] : false;
}
function sql_numfields($query_id = 0)
{
if( !$query_id )
{
$query_id = $this->query_result;
}
if($query_id)
{
$result = count($this->result_field_names[$query_id]);
return $result;
}
else
{
return false;
$query_id = $this->result;
}
return ( $query_id ) ? count($this->field_names[$query_id]) : false;
}
function sql_fieldname($offset, $query_id = 0)
{
if( !$query_id )
{
$query_id = $this->query_result;
}
if($query_id)
{
$result = $this->result_field_names[$query_id][$offset];
return $result;
}
else
{
return false;
$query_id = $this->result;
}
return ( $query_id ) ? $this->field_names[$query_id][$offset] : false;
}
function sql_fieldtype($offset, $query_id = 0)
{
if( !$query_id )
{
$query_id = $this->query_result;
}
if($query_id)
{
$result = @odbc_field_type($query_id, $offset);
return $result;
}
else
{
return false;
$query_id = $this->result;
}
return ( $query_id ) ? $this->field_types[$query_id][$offset] : false;
}
function sql_fetchrow($query_id = 0)
{
if( !$query_id )
{
$query_id = $this->query_result;
$query_id = $this->result;
}
if( $query_id )
{
if($this->row_index[$query_id] < $this->result_numrows[$query_id])
{
$result = $this->result_rowset[$query_id][$this->row_index[$query_id]];
$this->row_index[$query_id]++;
return $result;
}
else
{
return false;
}
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;
}
else
{
return false;
}
}
function sql_fetchrowset($query_id = 0)
{
if( !$query_id )
{
$query_id = $this->query_result;
$query_id = $this->result;
}
if( $query_id )
{
$this->row_index[$query_id] = $this->result_numrows[$query_id];
return $this->result_rowset[$query_id];
return ( $this->num_rows[$query_id] ) ? $this->result_rowset[$query_id] : false;
}
else
{
return false;
}
}
function sql_fetchfield($field, $row = -1, $query_id = 0)
{
if( !$query_id )
{
$query_id = $this->query_result;
$query_id = $this->result;
}
if( $query_id )
{
if($row < $this->result_numrows[$query_id])
if( $row < $this->num_rows[$query_id] )
{
if($row == -1)
{
$getrow = $this->row_index[$query_id]-1;
}
else
{
$getrow = $row;
}
return $this->result_rowset[$query_id][$getrow][$this->result_field_names[$query_id][$field]];
$getrow = ($row == -1) ? $this->current_row[$query_id] - 1 : $row;
return $this->result_rowset[$query_id][$getrow][$this->field_names[$query_id][$field]];
}
else
{
@ -353,15 +308,17 @@ class sql_db
return false;
}
}
function sql_rowseek($offset, $query_id = 0)
{
if( !$query_id )
{
$query_id = $this->query_result;
$query_id = $this->result;
}
if( $query_id )
{
$this->row_index[$query_id] = 0;
$this->current_row[$query_id] = $offset - 1;
return true;
}
else
@ -369,43 +326,39 @@ class sql_db
return false;
}
}
function sql_nextid($query_id = 0)
function sql_nextid()
{
if(!$query_id)
return ( $this->next_id[$this->db_connect_id] ) ? $this->next_id[$this->db_connect_id] : false;
}
function sql_affectedrows()
{
$query_id = $this->query_result;
}
if($query_id)
{
return $this->next_id[$query_id];
}
else
{
return false;
}
return ( $this->affected_rows[$this->db_connect_id] ) ? $this->affected_rows[$this->db_connect_id] : false;
}
function sql_freeresult($query_id = 0)
{
if( !$query_id )
{
$query_id = $this->query_result;
}
if($query_id)
{
$result = @odbc_free_result($query_id);
return $result;
}
else
{
return false;
$query_id = $this->result;
}
unset($this->num_rows[$query_id]);
unset($this->current_row[$query_id]);
unset($this->result_rowset[$query_id]);
unset($this->field_names[$query_id]);
unset($this->field_types[$query_id]);
return true;
}
function sql_error($query_id = 0)
{
// $result['code'] = @odbc_error($this->db_connect_id);
// $result['message'] = @odbc_errormsg($this->db_connect_id);
$error['code'] = odbc_error($this->db_connect_id);
$error['message'] = odbc_errormsg($this->db_connect_id);
return "";
return $error;
}
} // class sql_db

View file

@ -28,12 +28,16 @@ class sql_db
{
var $db_connect_id;
var $query_result;
var $query_resultset;
var $query_numrows;
var $result;
var $next_id;
var $row;
var $row_index;
var $num_rows;
var $current_row;
var $field_names;
var $field_types;
var $result_rowset;
var $num_queries = 0;
//
@ -42,29 +46,14 @@ class sql_db
function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true)
{
$this->persistency = $persistency;
$this->server = $sqlserver;
$this->user = $sqluser;
$this->password = $sqlpassword;
$this->dbname = $database;
$this->server = $sqlserver;
$this->db_connect_id = ($this->persistency) ? odbc_pconnect($this->server, $this->user, $this->password) : odbc_connect($this->server, $this->user, $this->password);
if($this->persistency)
{
$this->db_connect_id = odbc_pconnect($this->server, $this->user, $this->password);
}
else
{
$this->db_connect_id = odbc_connect($this->server, $this->user, $this->password);
}
if($this->db_connect_id)
{
return $this->db_connect_id;
}
else
{
return false;
}
return ( $this->db_connect_id ) ? $this->db_connect_id : false;
}
//
// Other base methods
@ -73,12 +62,21 @@ class sql_db
{
if($this->db_connect_id)
{
if($this->query_result)
if( $this->in_transaction )
{
@odbc_free_result($this->query_result);
@odbc_commit($this->db_connect_id);
}
$result = @odbc_close($this->db_connect_id);
return $result;
if( count($this->result_rowset) )
{
unset($this->result_rowset);
unset($this->field_names);
unset($this->field_types);
unset($this->num_rows);
unset($this->current_row);
}
return @odbc_close($this->db_connect_id);
}
else
{
@ -86,111 +84,120 @@ class sql_db
}
}
//
// Query method
//
function sql_query($query = "", $transaction = FALSE)
{
//
// Remove any pre-existing queries
//
unset($this->query_result);
unset($this->row);
if( $query != "" )
{
$this->num_queries++;
if(!eregi("^INSERT ",$query))
if( $transaction == BEGIN_TRANSACTION )
{
if(eregi("LIMIT", $query))
if( !odbc_autocommit($this->db_connect_id, false) )
{
preg_match("/^(.*)LIMIT ([0-9]+)[, ]*([0-9]+)*/s", $query, $limits);
return false;
}
$this->in_transaction = TRUE;
}
if( preg_match("/^SELECT(.*?)(LIMIT ([0-9]+)[, ]*([0-9]+)*)?$/s", $query, $limits) )
{
$query = $limits[1];
if($limits[3])
if( !empty($limits[2]) )
{
$row_offset = $limits[2];
$num_rows = $limits[3];
}
else
{
$row_offset = 0;
$num_rows = $limits[2];
$row_offset = ( $limits[4] ) ? $limits[3] : "";
$num_rows = ( $limits[4] ) ? $limits[4] : $limits[3];
$query = "TOP " . ( $row_offset + $num_rows ) . $query;
}
$this->query_result = odbc_exec($this->db_connect_id, $query);
$this->result = odbc_exec($this->db_connect_id, "SELECT $query");
$query_limit_offset = $row_offset;
$this->result_numrows[$this->query_result] = $num_rows;
}
else
if( $this->result )
{
$this->query_result = odbc_exec($this->db_connect_id, $query);
$row_offset = 0;
$this->result_numrows[$this->query_result] = 5E6;
if( empty($this->field_names[$this->result]) )
{
for($i = 1; $i < odbc_num_fields($this->result) + 1; $i++)
{
$this->field_names[$this->result][] = odbc_field_name($this->result, $i);
$this->field_types[$this->result][] = odbc_field_type($this->result, $i);
}
}
$result_id = $this->query_result;
if($this->query_result && eregi("^SELECT", $query))
{
$this->current_row[$this->result] = 0;
$this->result_rowset[$this->result] = array();
for($i = 1; $i < odbc_num_fields($result_id)+1; $i++)
$row_outer = ( isset($row_offset) ) ? $row_offset + 1 : 1;
$row_outer_max = ( isset($num_rows) ) ? $num_rows + 1 : 1E9;
$row_inner = 0;
while( odbc_fetch_row($this->result, $row_outer) && $row_outer < $row_outer_max )
{
$this->result_field_names[$result_id][] = odbc_field_name($result_id, $i);
for($j = 0; $j < count($this->field_names[$this->result]); $j++)
{
$this->result_rowset[$this->result][$row_inner][$this->field_names[$this->result][$j]] = stripslashes(odbc_result($this->result, $j + 1));
}
$i = $row_offset + 1;
$k = 0;
while(odbc_fetch_row($result_id, $i) && $k < $this->result_numrows[$result_id])
{
for($j = 1; $j < count($this->result_field_names[$result_id])+1; $j++)
{
$this->result_rowset[$result_id][$k][$this->result_field_names[$result_id][$j-1]] = odbc_result($result_id, $j);
}
$i++;
$k++;
$row_outer++;
$row_inner++;
}
$this->result_numrows[$result_id] = $k;
$this->row_index[$result_id] = 0;
$this->num_rows[$this->result] = count($this->result_rowset[$this->result]);
}
else
}
else if( eregi("^INSERT ", $query) )
{
$this->result_numrows[$result_id] = @odbc_num_rows($result_id);
$this->row_index[$result_id] = 0;
$this->result = odbc_exec($this->db_connect_id, str_replace("\'", "''", $query));
if( $this->result )
{
$result_id = odbc_exec($this->db_connect_id, "SELECT @@IDENTITY");
if( $result_id )
{
if( odbc_fetch_row($result_id) )
{
$this->next_id[$this->db_connect_id] = odbc_result($result_id, 1);
$this->affected_rows[$this->db_connect_id] = odbc_num_rows($this->result);
}
}
}
}
else
{
if(eregi("^(INSERT|UPDATE) ", $query))
{
$query = preg_replace("/\\\'/s", "''", $query);
}
$this->query_result = odbc_exec($this->db_connect_id, $query);
$this->result = odbc_exec($this->db_connect_id, str_replace("\'", "''", $query));
if($this->query_result)
if( $this->result )
{
$sql_id = "SELECT @@IDENTITY";
$id_result = odbc_exec($this->db_connect_id, $sql_id);
if($id_result)
{
$row_result = odbc_fetch_row($id_result);
if($row_result)
{
$this->next_id[$this->query_result] = odbc_result($id_result, 1);
}
$this->affected_rows[$this->db_connect_id] = odbc_num_rows($this->result);
}
}
$this->query_limit_offset[$this->query_result] = 0;
$this->result_numrows[$this->query_result] = 0;
if( !$this->result )
{
if( $this->in_transaction )
{
odbc_rollback($this->db_connect_id);
odbc_autocommit($this->db_connect_id, true);
$this->in_transaction = FALSE;
}
return $this->query_result;
return false;
}
if( $transaction == END_TRANSACTION && $this->in_transaction )
{
odbc_commit($this->db_connect_id);
odbc_autocommit($this->db_connect_id, true);
$this->in_transaction = FALSE;
}
odbc_free_result($this->result);
return $this->result;
}
else
{
@ -205,140 +212,90 @@ class sql_db
{
if( !$query_id )
{
$query_id = $this->query_result;
}
if($query_id)
{
return $this->result_numrows[$query_id];
}
else
{
return false;
}
}
function sql_affectedrows($query_id = 0)
{
if(!$query_id)
{
$query_id = $this->query_result;
}
if($query_id)
{
return $this->result_numrows[$query_id];
}
else
{
return false;
$query_id = $this->result;
}
return ( $query_id ) ? $this->num_rows[$query_id] : false;
}
function sql_numfields($query_id = 0)
{
if( !$query_id )
{
$query_id = $this->query_result;
}
if($query_id)
{
$result = count($this->result_field_names[$query_id]);
return $result;
}
else
{
return false;
$query_id = $this->result;
}
return ( $query_id ) ? count($this->field_names[$query_id]) : false;
}
function sql_fieldname($offset, $query_id = 0)
{
if( !$query_id )
{
$query_id = $this->query_result;
}
if($query_id)
{
$result = $this->result_field_names[$query_id][$offset];
return $result;
}
else
{
return false;
$query_id = $this->result;
}
return ( $query_id ) ? $this->field_names[$query_id][$offset] : false;
}
function sql_fieldtype($offset, $query_id = 0)
{
if( !$query_id )
{
$query_id = $this->query_result;
}
if($query_id)
{
$result = @odbc_field_type($query_id, $offset);
return $result;
}
else
{
return false;
$query_id = $this->result;
}
return ( $query_id ) ? $this->field_types[$query_id][$offset] : false;
}
function sql_fetchrow($query_id = 0)
{
if( !$query_id )
{
$query_id = $this->query_result;
$query_id = $this->result;
}
if( $query_id )
{
if($this->row_index[$query_id] < $this->result_numrows[$query_id])
{
$result = $this->result_rowset[$query_id][$this->row_index[$query_id]];
$this->row_index[$query_id]++;
return $result;
}
else
{
return false;
}
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;
}
else
{
return false;
}
}
function sql_fetchrowset($query_id = 0)
{
if( !$query_id )
{
$query_id = $this->query_result;
$query_id = $this->result;
}
if( $query_id )
{
$this->row_index[$query_id] = $this->result_numrows[$query_id];
return $this->result_rowset[$query_id];
return ( $this->num_rows[$query_id] ) ? $this->result_rowset[$query_id] : false;
}
else
{
return false;
}
}
function sql_fetchfield($field, $row = -1, $query_id = 0)
{
if( !$query_id )
{
$query_id = $this->query_result;
$query_id = $this->result;
}
if( $query_id )
{
if($row < $this->result_numrows[$query_id])
if( $row < $this->num_rows[$query_id] )
{
if($row == -1)
{
$getrow = $this->row_index[$query_id]-1;
}
else
{
$getrow = $row;
}
$getrow = ( $row == -1 ) ? $this->current_row[$query_id] - 1 : $row;
return $this->result_rowset[$query_id][$getrow][$this->result_field_names[$query_id][$field]];
return $this->result_rowset[$query_id][$getrow][$this->field_names[$query_id][$field]];
}
else
@ -351,15 +308,17 @@ class sql_db
return false;
}
}
function sql_rowseek($offset, $query_id = 0)
{
if( !$query_id )
{
$query_id = $this->query_result;
$query_id = $this->result;
}
if( $query_id )
{
$this->row_index[$query_id] = 0;
$this->current_row[$query_id] = $offset - 1;
return true;
}
else
@ -367,43 +326,39 @@ class sql_db
return false;
}
}
function sql_nextid($query_id = 0)
function sql_nextid()
{
if(!$query_id)
return ( $this->next_id[$this->db_connect_id] ) ? $this->next_id[$this->db_connect_id] : false;
}
function sql_affectedrows()
{
$query_id = $this->query_result;
}
if($query_id)
{
return $this->next_id[$query_id];
}
else
{
return false;
}
return ( $this->affected_rows[$this->db_connect_id] ) ? $this->affected_rows[$this->db_connect_id] : false;
}
function sql_freeresult($query_id = 0)
{
if( !$query_id )
{
$query_id = $this->query_result;
$query_id = $this->result;
}
if($query_id)
{
$result = @odbc_free_result($query_id);
return $result;
}
else
{
return false;
}
}
function sql_error($query_id = 0)
{
// $result['code'] = @odbc_error($this->db_connect_id);
// $result['message'] = @odbc_errormsg($this->db_connect_id);
return "";
unset($this->num_rows[$query_id]);
unset($this->current_row[$query_id]);
unset($this->result_rowset[$query_id]);
unset($this->field_names[$query_id]);
unset($this->field_types[$query_id]);
return true;
}
function sql_error()
{
$error['code'] = odbc_error($this->db_connect_id);
$error['message'] = odbc_errormsg($this->db_connect_id);
return $error;
}
} // class sql_db