<?php

	/*
	*	Gekko - Open Source Web Development Framework
	*	------------------------------------------------------------------------
	*	Copyright (C) 2004-2006, 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.sourceforge.net>
	*	@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::getError().".\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_SOURCE_DIR."lib/db/".$GLOBALS["dbinfo"]["type"].".driver";
		if (file_exists($driver)) {
			require $driver;
		} else {
			trigger_error("Database driver '{$driver}' doesn't exists!", E_USER_ERROR);
		}
	}

	function db_init () {
		$db =& $GLOBALS["db"];
		$dbinfo =& $GLOBALS["dbinfo"];
		if (!$db) {
			$db = new Database;
			$db->open (
				$dbinfo["host"],
				$dbinfo["user"],
				$dbinfo["pass"],
				$dbinfo["name"]
			) or trigger_error("Couldn't connect to database!", E_USER_ERROR);
		}
		return true;
	}

	function db_exit () {
		$db =& $GLOBALS["db"];
		cacheDump();
		return $db->close();
	}

	function db_table($name) {
		$dbinfo =& $GLOBALS["dbinfo"];
		return "`{$dbinfo["pref"]}$name`";
	}
	function db_get_field($table, $item_id, $field = "*") {
		$db =& $GLOBALS["db"];
		$return = false;

		$q = $db->query("SELECT {$field} FROM ".db_table($table)." WHERE id = '{$item_id}'",
			__FILE__, __LINE__, true);

		if ($db->numRows($q)) {
			$row = $db->fetchRow($q);
			if (is_array($row))
				$return = $row;
		}

		return $return ? ($field ? $return[$field] : $return) : false;
	}

	// sql date lock
	function db_datelock($field, $date = 0) {
		$time = timemark($date);
		if ($time)
			return "($field >= '".time2date($time[0])."' AND $field <= '".time2date($time[1])."')";
		return 1;
	}
	Class dbMacro {
		function swapItemOrder($table, $item_id, $new_order_id) {
			$db =& $GLOBALS["db"];

			$q = $db->query ("SELECT order_id FROM ".db_table($table)."
			WHERE id = '{$item_id}'",
			__FILE__, __LINE__, true);

			if ($db->numRows($q)) {

				$row = $db->fetchRow($q);

				if ($new_order_id != $row["order_id"]) {

					$db->query ("UPDATE ".db_table($table)."
					SET order_id = '{$row["order_id"]}'
					WHERE order_id = '{$new_order_id}'",
					__FILE__, __LINE__, true);

					$db->query ("UPDATE ".db_table($table)."
					SET order_id = '{$new_order_id}'
					WHERE id = '{$item_id}'",
					__FILE__, __LINE__, true);

				}

			}
		}
	}

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