Thread: [phpMP-CVS] CVS: phpMP/admin/db index.html,NONE,1.1 mysql.php,NONE,1.1 postgresql.php,NONE,1.1
Status: Pre-Alpha
Brought to you by:
heimidal
From: Adam A. <ra...@us...> - 2003-10-15 03:30:16
|
Update of /cvsroot/phpmp/phpMP/admin/db In directory sc8-pr-cvs1:/tmp/cvs-serv28599/core/db Added Files: index.html mysql.php postgresql.php Log Message: Fresh start... --- NEW FILE: index.html --- --- NEW FILE: mysql.php --- <?php /* * phpMP - The PHP Modular Portal System * Copyright (C) 2002-2003 Brian Rose and the phpMP group * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * $Id: mysql.php,v 1.1 2003/10/15 03:30:10 rasadam Exp $ * */ if(!defined('PHPMP')) { die; } // MySQL 3.x/4.0 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 = @mysql_pconnect($host, $username, $password); } else { $this->db_connection_id = @mysql_connect($host, $username, $password); } if( !$this->db_connection_id ) { if( !is_null($error_handling) ) { // to db error handler (ERROR_FLAG, $this->sql_error($this->db_connection_id), $this->sql_errno($this->db_connection_id)); } else { return false; } } else { $db_select = @mysql_select_db($database, $this->db_connection_id); if( !$db_select ) { if( !is_null($error_handling) ) { // to db error handler (ERROR_FLAG, $this->sql_error($this->db_connection_id), $this->sql_errno($this->db_connection_id)); } else { return false; } } return $this->db_connection_id; } } 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 @mysql_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 = @mysql_query($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 = NULL, $type = MYSQL_BOTH) { if( is_null($db_query_id) ) { $db_query_id = $this->db_query_result; } return @mysql_fetch_array($db_query_id, $type); } // Select all rows in one big array function sql_fetch_rowset($db_query_id = NULL, $type = MYSQL_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 @mysql_num_rows($db_query_id); } // Get last ID generated from an INSERT query function sql_insert_id($db_query_id = NULL) { if( is_null($db_query_id) ) { $db_query_id = $this->db_query_result; } return @mysql_insert_id($this->db_connection_id); } // 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 @mysql_free_result($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 @mysql_num_fields($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 @mysql_field_name($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 @mysql_field_type($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 @mysql_affected_rows($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 @mysql_error($db_connection_id); } // SQL Error number from connection function sql_errno($db_connection_id = NULL) { if( is_null($db_connection_id) ) { $db_connection_id = $this->db_connect_id; } return @mysql_errno($db_connection_id); } } // End of sql_db ?> --- NEW FILE: postgresql.php --- <?php /* * phpMP - The PHP Modular Portal System * Copyright (C) 2002-2003 Brian Rose and the phpMP group * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * $Id: postgresql.php,v 1.1 2003/10/15 03:30:10 rasadam Exp $ * */ if(!defined('PHPMP')) { die; } // 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; } } return $this->db_connection_id; } 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 function sql_errno($connection = NULL) { if( is_null($connection) ) { $connection = $this->db_connection_id; } // With postgres, there is no unified error number system. We will need // to get the number based on the message // 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 static $pgsql_errors; // If we didn't do this before if( empty($pgsql_errors) ) { // This is an array of error messages that PostgreSQL has error numbers for $pgsql_errors = Array( '/Out of memory in line [0-9]+/' => -12, '/Data not found line [0-9]+/' => -100, '/Unsupported type .* on line ([0-9])+/' => -200, '/Too many arguments line [0-9]+/' => -201, '/Too few arguments line [0-9]+/' => -202, '/Too many matches line [0-9]+/' => -203, '/Not correctly formatted int type: .* line [0-9]+/' => -204, '/Not correctly formatted unsigned type: .* line [0-9]+/' => -205, '/Not correctly formatted floating-point type: .* line [0-9]+/' => -206, '/Unable to convert .* to bool on line [0-9]+/' => -207, '/Empty query line [0-9]+/' => -208, '/NULL value without indicator in line [0-9]+/' => -209, '/Variable is not an array in line [0-9]+/' => -210, '/Data read from backend is not an array in line [0-9]+/' => -211, '/No such connection .* in line [0-9]+/' => -220, '/Not connected in line [0-9]+/' => -221, '/Invalid statement name .* in line [0-9]+/' => -230, '/Descriptor .* not found in line [0-9]+/' => -240, '/Descriptor index out of range in line [0-9]+/' => -241, '/Descriptor .* not found in line [0-9]+/' => -242, '/Variable is not a numeric type in line [0-9]+/' => -243, '/Variable is not a character type in line [0-9]+/' => -244, '/Postgres error: .* line [0-9]+/' => -400, '/Error in transaction processing line [0-9]+/' => -401, '/Could not connect to database .* in line [0-9]+/' => -402, ); } // Lets juggle the error messages foreach ($pgsql_errors as $error => $number) { if( preg_match($error, $last_error)) { // return appropriate number for this error return $number; } } // No error number exists, so 0 will do return NULL; } } // End of sql_db ?> |