Location: PHPKode > projects > Obsessive Website Statistics > ows/include/db.inc.php
<?php
/*
	$Id: db.inc.php 78 2007-08-02 23:35:43Z randomperson83 $

	Obsessive Web Statistics
    Copyright (C) 2007 Dustin Spicuzza <hide@address.com>

    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 3 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, see <http://www.gnu.org/licenses/>.

	Wrapper functions to allow us to use postgre and mysql, without using PEAR::DB or something like
	that... currently supports MySQL, Postgre. 
	
	TODO: Re-evaluate usefulness of this
	
*/


function db_connect($server, $username, $password, $db_name){
	
	global $cfg;
	switch($cfg['db_type']){
	
		case "mysql": 	
			$ret=mysql_connect($server, $username, $password);
			if(!mysql_select_db($db_name)) 
				return false;
			return $ret;
		
		case "postgre": 
			return pg_connect("host=".$server." user=".$username." password=".$password." dbname=".$db_name);
		
		default: 
			die("Database type not specified in configuration.");
	}
}

// php doesn't support function overloading :(
function db_close($link = null){

	global $cfg;
	if ($link){
		switch($cfg['db_type']){
			case "mysql": 	
				return mysql_close($link);
				
			case "postgre":	
				return pg_close($link);
				
			default: 
				die("Database type not specified in configuration.");
		}
	}else{
		switch($cfg['db_type']){
			case "mysql": 	
				return mysql_close();
				
			case "postgre":	
				return pg_close();
				
			default: 
				die("Database type not specified in configuration.");
		}
	}
}

// additional parameter for installer
function db_query($query, $nowdebug = false){

	global $cfg;
	
	if ($cfg['debug'] == true || $nowdebug == true)
		$cfg['db_last_sql_query'] = $query;
	
	if (isset($cfg['db_queries']))
		$cfg['db_queries'] += 1;
	else
		$cfg['db_queries'] = 1;
	
	switch($cfg['db_type']){
	
		case "mysql": 	
			return mysql_query($query);
			
		case "postgre": 
			return pg_query($query);
		
		default: 
			die("Database type not specified in configuration.");
	}
}

function db_num_rows($result){

	global $cfg;
	switch($cfg['db_type']){
	
		case "mysql": 	
			return mysql_num_rows($result);
			
		case "postgre":
			return pg_num_rows($result);
			
		default: 
			die("Database type not specified in configuration.");
	}
}

function db_fetch_row($result){
	
	global $cfg;
	switch($cfg['db_type']){
		case "mysql": 	
			return mysql_fetch_row($result);
			
		case "postgre": 
			return pg_fetch_row($result);
			
		default: 
			die("Database type not specified in configuration.");
	}
}

function db_fetch_array($result){

	global $cfg;
	switch($cfg['db_type']){
		case "mysql": 	
			return mysql_fetch_array($result);
		
		case "postgre": 
			return pg_fetch_array($result);
		default: 
			die("Database type not specified in configuration.");
	}
}

function db_get_last_id($table,$field){
	global $cfg;
	switch($cfg['db_type']){
		case "mysql":
			
			if (db_has_rows($result = mysql_query("SELECT LAST_INSERT_ID()")) && $row = mysql_fetch_row($result))
				return $row[0];
				
			return false;
		
		case "postgre":
			// im not 100% confident about this, but theres not a better way
			if (!db_has_rows($result = pg_query("SELECT currval('" . pg_escape_string($table) . "_" . pg_escape_string($field) . "_seq')")) && $row = pg_fetch_row($result))
				return $row[0];
				
			return false;
		
		default: 
			die("Database type not specified in configuration.");
	}
}

function db_fetch_assoc($result){
	global $cfg;
	switch($cfg['db_type']){
		case "mysql": 	
			return mysql_fetch_assoc($result);
		
		case "postgre": 
			return pg_fetch_assoc($result);
		default: 
			die("Database type not specified in configuration.");
	}
}

function db_escape_string($unescaped_string){
	
	global $cfg;
	switch($cfg['db_type']){
		case "mysql": 	
			return mysql_escape_string($unescaped_string);
			
		case "postgre": 
			return pg_escape_string($unescaped_string);
			
		default: 
			die("Database type not specified in configuration.");
	}
}

// intended to be used with array_walk
function db_total_escape(&$item,$key){
	$item = db_escape_string($item);
}

function db_affected_rows($result){
	
	global $cfg;
	switch($cfg['db_type']){
		case "mysql": 	
			return mysql_affected_rows();
			
		case "postgre": 
			return pg_affected_rows($result);
			
		default: 
			die("Database type not specified in configuration.");
	}
}

function db_error(){

	global $cfg;
	switch($cfg['db_type']){
		case "mysql": 	
			return mysql_error();
			
		case "postgre": 
			return pg_last_error();
			
		default: 
			die("Database type not specified in configuration.");
	}
}

// functions needed to specify database-specific items (timestamps and such!)
function db_get_timestamp_query($field_name){
	global $cfg;
	switch ($cfg['db_type']){
		case "mysql":
			return "UNIX_TIMESTAMP($field_name)";
		
		case "postgre":
			// which way  is correct?
			//return "EXTRACT(EPOCH FROM TIMESTAMP $field_name)";
			return "EXTRACT(EPOCH FROM $field_name)";
		
		default:
			die("Database type not specified in configuration.");
	}
}

// transactional code
function db_begin_transaction(){

	global $cfg;
	if ($cfg['enable_transactions'] == true)
		return db_query("BEGIN");
	return true;
}

function db_commit_transaction(){
	
	global $cfg;
	if ($cfg['enable_transactions'] == true)
		return db_query("COMMIT");
	return true;
}

// if $error_msg is specified, this function will always return false. Otherwise,
// it will return whatever the query value is.. generally a true/false value
function db_rollback_transaction($error_msg = ''){

	global $cfg;
	if ($cfg['enable_transactions'] == true){
		$result = db_query("ROLLBACK");
		
		if ($error_msg == '')
			return $result;
		
		if (!$result)
			$error_msg .= " Could not rollback SQL transaction! Some changes may already have been made to the database.";

		return show_error($error_msg);	
	}
	
	if ($error_msg != '')
		return show_error($error_msg . " Could not rollback SQL transaction! Some changes may already have been made to the database.");
	
	return false;
}

// Use this to check if there is more than zero rows, and a valid returned result.
//
// If $show_error = true, then if there is an SQL error ($result == false) then
// it will display the SQL error 
//
function db_has_rows($result,$show_error = true){

	if (db_is_valid_result($result,$show_error) && db_num_rows($result) > 0)
		return true;
		
	return false;
}

// used primarily in admininistrative functions to show SQL errors in a uniform manner
function db_is_valid_result($result,$show_error = true){

	global $cfg;

	if (!$result && $show_error){
		$err = db_error();
		if ($err == "")
			$err = "No SQL error occurred. This may be an invalid error message!";
		
		if (isset($cfg['db_last_sql_query']))
			if (php_sapi_name() == 'cli')
				$err .= "\n\nLast SQL Query:\n" . htmlentities($cfg['db_last_sql_query']) . "\n\n";
			else
				$err .= "<br /><br /><strong>Last SQL Query:</strong><br />" . htmlentities($cfg['db_last_sql_query']);
		
		if (php_sapi_name() == 'cli')
			show_error("SQL Message: $err", false, false, false);
		else
			show_error("<strong>SQL Message:</strong> $err </p>");
	}
	return $result;
}

// returns the version info if it can
function db_version(){

	global $cfg;
	
	switch ($cfg['db_type']){
		case "mysql":
			return mysql_get_server_info();
		
		case "postgre":
			if (!function_exists('pg_version'))
				return "N/A";
				
			$version = pg_version();
			return $version['server_version'];
		
		default:
			die("Database type not specified in configuration.");
	}

}

?>
Return current item: Obsessive Website Statistics