<?php

	/*
	*	Gekko - Open Source Web Development Framework
	*	------------------------------------------------------------------------
	*	Copyright (C) 2004-2005, J. Carlos Nieto <xiam@users.sourceforge.net>
	*	This program is Free Software.
	*
	*	@package	Gekko
	*	@subpackage /lib
	*	@license	http://www.gnu.org/copyleft/gpl.html GNU/GPL License 2.0
	*	@author		J. Carlos Nieto <xiam@users.sf.net>
	*	@version	CVS: $Id:$
	*	@link		http://www.gekkoware.org
	*/

	if (!defined("IN-GEKKO")) die("Get a life!");

	define("DB_DEFAULT_TYPE", "mysql");
	
	// database error definitions
	define("DB_ERR_QUERY_SYNTAX", 0);
	define("DB_ERR_LOADING_DRIVER", 1);

	Class Database_Common {
		
		var $driver, $type, $link, $host, $user, $pass, $name, $query, $result, $errstr;
		
		function Database_Common () {
			return true;
		}
		function error($err = DB_ERR_QUERY_SYNTAX, $file = null, $line = null) {
			switch ($err) {
				case DB_ERR_QUERY_SYNTAX:
					$errstr = "SQL query syntax error in \"$file\" at line $line.\n-> ".Database::get_error().".\n";
					break;
				case DB_ERR_LOADING_DRIVER:
					$errstr = "Error while loading database driver.\n";
					break;
			}
			trigger_error("$errstr", E_USER_ERROR);
		}
		// returns key value from $dbinfo global variable.
		function info($key) {
			return $GLOBALS["dbinfo"][$key];
		}
	}

	function db_load_driver() {
		$driver = GEKKO_ROOT."lib/db/".$GLOBALS["dbinfo"]['type'].".driver";
		if (file_exists($driver)) {
			require $driver;
		} else {
			trigger_error("Error loading Database Driver", E_USER_ERROR);
		}
	}

	function db_init () {
		global $db;
		if (!$db) {
			$db = new Database;
			$db->open(
				$GLOBALS["dbinfo"]["host"],
				$GLOBALS["dbinfo"]["user"],
				$GLOBALS["dbinfo"]["pass"],
				$GLOBALS["dbinfo"]["name"]
			) or trigger_error("Couldn't Connect to Database!", E_USER_ERROR);
		}
		return true;
	}
	function db_exit () {
		Global $db;
		return $db->close();
	}
	function db_table($name) {
		Global $dbinfo;
		return "`{$dbinfo["pref"]}$name`";
	}

	// loading database driver
	if (!defined("IN-INSTALL")) {
		db_load_driver();
	}
?>
