[phpMP-CVS] CVS: phpMP/core/dba mysql.php,1.15,1.16 postgresql.php,1.4,1.5
Status: Pre-Alpha
Brought to you by:
heimidal
|
From: Adam A. <ra...@us...> - 2003-10-09 05:16:40
|
Update of /cvsroot/phpmp/phpMP/core/dba
In directory sc8-pr-cvs1:/tmp/cvs-serv5771/core/dba
Modified Files:
mysql.php postgresql.php
Log Message:
PostgreSQL should be as functional as MySQL (if that says much). Removed some functions, we can rework them later if we need them.
Index: mysql.php
===================================================================
RCS file: /cvsroot/phpmp/phpMP/core/dba/mysql.php,v
retrieving revision 1.15
retrieving revision 1.16
diff -C2 -r1.15 -r1.16
*** mysql.php 9 Oct 2003 04:35:58 -0000 1.15
--- mysql.php 9 Oct 2003 05:16:37 -0000 1.16
***************
*** 27,30 ****
--- 27,31 ----
{
var $db_connection_id;
+ var $db_query;
var $db_query_result = 0;
var $db_query_count = 0;
***************
*** 34,44 ****
function sql_connect($hostname, $port, $username, $password, $database, $persistant = NULL, $error_handling = NULL )
{
- $this->hostname = $hostname;
- $this->port = $port;
- $this->username = $username;
- $this->password = $password;
- $this->database = $database;
- $this->persistant = $persistant;
-
if( !is_null($this->persistant) )
{
--- 35,38 ----
***************
*** 91,95 ****
if($this->db_query_result)
{
! @mysql_free_result($this->db_query_result);
}
--- 85,89 ----
if($this->db_query_result)
{
! @$this->sql_free_result($this->db_query_result);
}
***************
*** 99,112 ****
// Query database
! function sql_query($sql, $sql_explain = NULL, $line = NULL, $file = NULL )
{
$this->sql_free_result($this->db_query_result); // remove previous query result
!
! if(! empty($sql))
{
! $this->db_query_count++;
!
! $this->db_query_result = @mysql_query($sql, $this->db_connection_id);
}
if( $this->db_query_result)
--- 93,109 ----
// Query database
! // If you pass no query, it will try to execute the last query again
! function sql_query($sql = '', $sql_explain = NULL, $line = NULL, $file = NULL )
{
$this->sql_free_result($this->db_query_result); // remove previous query result
!
! if( !empty($sql) )
{
! $this->db_query = $sql;
}
+
+ $this->db_query_count++;
+
+ $this->db_query_result = @mysql_query($this->db_query, $this->db_connection_id);
if( $this->db_query_result)
***************
*** 129,133 ****
// Select a row as an Associative, Numeric or Both (array)
! function sql_fetch_row($db_query_id = NULL, $type = MYSQL_BOTH)
{
if( is_null($db_query_id) )
--- 126,130 ----
// Select a row as an Associative, Numeric or Both (array)
! function sql_fetch_row($db_query_id = NULL, $row = NULL, $type = MYSQL_BOTH)
{
if( is_null($db_query_id) )
***************
*** 151,155 ****
$this->db_rowset[$this->db_query_result] = Array();
! while($row = @mysql_fetch_array($db_query_id, $type))
{
$this->db_rowset[$this->db_query_result][] = $row;
--- 148,152 ----
$this->db_rowset[$this->db_query_result] = Array();
! while( $row = @this->sql_fetch_row($db_query_id, NULL, $type) )
{
$this->db_rowset[$this->db_query_result][] = $row;
***************
*** 227,248 ****
return @mysql_field_type($db_query_id, $field_offset);
- }
-
- // Gets column information and returns it object forum
- function sql_fetch_field($db_query_id = NULL, $field_offset = NULL)
- {
- if( is_null($db_query_id) )
- {
- $db_query_id = $this->db_query_result;
- }
-
- if( is_null($field_offset) )
- {
- return @mysql_fetch_field($db_query_id);
- }
- else
- {
- return @mysql_fetch_field($db_query_id, $field_offset);
- }
}
--- 224,227 ----
Index: postgresql.php
===================================================================
RCS file: /cvsroot/phpmp/phpMP/core/dba/postgresql.php,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -r1.4 -r1.5
*** postgresql.php 27 Sep 2003 03:42:22 -0000 1.4
--- postgresql.php 9 Oct 2003 05:16:37 -0000 1.5
***************
*** 23,56 ****
*/
class sql_db
{
var $db_connection_id;
var $db_query_result = 0;
var $db_query_count = 0;
- var $db_row = array();
var $db_rowset = array();
! //
! // CONSTRUCTOR - sql_connect
! //
! function sql_db($hostname, $port, $username, $password, $database, $persistant)
! {
! $this->hostname = $hostname;
! $this->port = $port;
! $this->username = $username;
! $this->password = $password;
! $this->database = $database;
! $this->persistant = $persistant;
! if( $this->persistant)
{
! $this->db_connection_id = pg_pconnect("host=$this->hostname port=$this->port user=$this->username password=$this->password dbname=$this->database");
}
else
{
! $this->db_connection_id = pg_connect("host=$this->hostname port=$this->port user=$this->username password=$this->password dbname=$this->database");
}
}
// Attempt to get PostgreSQL Error code
// Blame me (R45) if this fails
--- 23,257 ----
*/
+ // PostgreSQL 7.x Database API
+
class sql_db
{
var $db_connection_id;
+ var $db_query;
var $db_query_result = 0;
var $db_query_count = 0;
var $db_rowset = array();
! // Open database connection, select database
! function sql_connect($hostname, $port, $username, $password, $database, $persistant = NULL, $error_handling = NULL )
! {
! if( !is_null($this->persistant) )
! {
! $this->db_connection_id = pg_pconnect("host=$hostname port=$port user=$username password=$password dbname=$database");
! }
! else
! {
! $this->db_connection_id = pg_connect("host=$hostname port=$port user=$username password=$password dbname=$database");
! }
!
! if( !$this->db_connection_id )
! {
! if( !is_null($error_handling) )
! {
! // to db error handler (ERROR_FLAG, $this->sql_error($this->db_connection_id), NULL);
! }
! else
! {
! return false;
! }
! }
! }
!
! function sql_close($db_connect_id = NULL)
! {
! if( is_null($db_connect_id) )
! {
! $db_connect_id = $this->db_connection_id;
! }
!
! if($db_connect_id != FALSE)
! {
! if($this->db_query_result)
! {
! $this->sql_free_result($this->db_query_result);
! }
!
! return @pg_close($db_connect_id);
! }
! }
!
! // Query database
! // If you pass no query, it will try to execute the last query again
! function sql_query($sql = '', $sql_explain = NULL, $line = NULL, $file = NULL )
! {
! $this->sql_free_result($this->db_query_result); // remove previous query result
!
! if( !empty($sql) )
! {
! $this->db_query = $sql;
! }
!
! $this->db_query_count++;
!
! $this->db_query_result = @pg_exec($this->db_query, $this->db_connection_id);
! if( $this->db_query_result)
{
! unset($this->db_rowset[$this->db_query_result]);
! return $this->db_query_result;
}
else
{
! if( !is_null($sql_explain) )
! {
! // to db error handler (ERROR_FLAG, $this->sql_error($this->db_connection_id), $this->sql_errno($this->db_connection_id), $sql_explain, $line, $file);
! }
! else
! {
! return false;
! }
! }
! }
!
! // Select a row as an Associative, Numeric or Both (array)
! function sql_fetch_row($db_query_id = NULL, $row_number = NULL, $type = PGSQL_BOTH)
! {
! if( is_null($db_query_id) )
! {
! $db_query_id = $this->db_query_result;
! }
!
! if ( !is_null($row_number) )
! {
! return @pg_fetch_array($db_query_id, $row_number, $type);
! }
! else
! {
! return @pg_fetch_array($db_query_id);
! }
! }
!
! // Select all rows in one big array
! function sql_fetch_rowset($db_query_id = NULL, $type = PGSQL_BOTH)
! {
! if( is_null($db_query_id) )
! {
! $db_query_id = $this->db_query_result;
! }
!
! unset($this->db_rowset[$this->db_query_result]);
!
! $this->db_rowset[$this->db_query_result] = Array();
!
! while( $row = $this->sql_fetch_row($db_query_id, NULL, $type) )
! {
! $this->db_rowset[$this->db_query_result][] = $row;
! }
!
! return $this->db_rowset[$this->db_query_result];
! }
!
! // Number of rows from the last select query. Shouldn't be used for
! // Firebird compatability (hopefully an ibase_num_rows function will
! // be soon)
! function sql_num_rows($db_query_id = NULL)
! {
! if( is_null($db_query_id) )
! {
! $db_query_id = $this->db_query_result;
}
+
+ return @pg_numrows($db_query_id);
}
+ // Get last ID generated from an INSERT query
+ // We will base this ID on the last query. The only way this function
+ // to work is to execute this right after you run the INSERT query.
+ function sql_insert_id($db_query_id = NULL)
+ {
+ // Regex used from phpBB 2.0.x
+ if( preg_match("/^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)/is", $this->db_query, $tablename) )
+ {
+ $sequence_result = $this->sql_query("SELECT currval('".$tablename[1]."_seq') AS last_id");
+
+ if( !$sequence_data )
+ {
+ return false;
+ }
+
+ $sequence_data = $this->sql_fetch_row($sequence_result, 0, PGSQL_ASSOC);
+
+ if( $sequence_data )
+ {
+ return $sequence_data['last_id'];
+ }
+ }
+
+ return false;
+ }
+
+ // Free result resource
+ function sql_free_result($db_query_id = NULL)
+ {
+ if( is_null($db_query_id) )
+ {
+ $db_query_id = $this->db_query_result;
+ }
+
+ unset($this->db_rowset[$db_query_id]);
+
+ return @pg_freeresult($db_query_id);
+ }
+
+ // Number of fields selected in last query
+ function sql_num_fields($db_query_id = NULL)
+ {
+ if( is_null($db_query_id) )
+ {
+ $db_query_id = $this->db_query_result;
+ }
+
+ return @pg_numfields($db_query_id);
+ }
+
+ // Name of a specific field from a result
+ function sql_field_name($db_query_id = NULL, $field_offset)
+ {
+ if( is_null($db_query_id) )
+ {
+ $db_query_id = $this->db_query_result;
+ }
+
+ return @pg_fieldname($db_query_id, $field_offset);
+ }
+
+ // Field type from a result
+ function sql_field_type($db_query_id = NULL, $field_offset)
+ {
+ if( is_null($db_query_id) )
+ {
+ $db_query_id = $this->db_query_result;
+ }
+
+ return @pg_fieldtype($db_query_id, $field_offset);
+ }
+
+ // Amount of rows affected in last query
+ function sql_affected_rows()
+ {
+ if( is_null($db_query_id) )
+ {
+ $db_query_id = $this->db_query_result;
+ }
+
+ return @pg_cmdtuples($this->db_connection_id);
+ }
+
+ // SQL Error message from connection
+ function sql_error($db_connection_id = NULL)
+ {
+ if( is_null($db_connection_id) )
+ {
+ $db_connection_id = $this->db_connect_id;
+ }
+
+ return @pg_errormessage($db_connection_id);
+ }
+
// Attempt to get PostgreSQL Error code
// Blame me (R45) if this fails
***************
*** 66,70 ****
// We'll get the last message here, feel free to adjust this Booster
// if you want it as an object variable created elsewhere
! $last_error = @pg_errormessage($connection);
// So we don't have to set the error messages again
--- 267,271 ----
// We'll get the last message here, feel free to adjust this Booster
// if you want it as an object variable created elsewhere
! $last_error = $this->sql_error($connection);
// So we don't have to set the error messages again
***************
*** 115,120 ****
// No error number exists, so 0 will do
! return 0;
}
! }
! ?>
--- 316,323 ----
// No error number exists, so 0 will do
! return NULL;
}
!
! } // End of sql_db
!
! ?>
\ No newline at end of file
|