<?php
/*
 * @version			$Id: index.php 10381 2008-06-01 03:35:53Z $
 * @package			Carteasy
 * @copyright		Copyright (C) 2005 - 2011 Wlanium
 * @license			proprietary
 * @author			Thomas Bartelt
 * Carteasy is a web shop system
 */

class Database {

	protected function __construct() {}

	private function __clone() {}

	public static function update($table, $data, $where) {
		$set_line = "";
		if (is_array($data)) {
			$size = count($data);
		} else {
			$size = count($data);
		}
		$i = 1;

		foreach ($data as $key => $value) {
			$set_line .= $key;
			$set_line .= "=";

			if (is_numeric($value)) {
				$set_line .= $value;
			} else {
				$set_line .= "'".$value."'";
			}

			if ($i < $size) {
				$set_line .= ", ";
				$i++;
			}
		}

		$sql = "UPDATE $table ";
		$sql .= "SET ".$set_line." ";

		if ($where) {
			$sql .= " ".$where;
		}

		return $sql;
	} // end update

	public static function insert($table, $data) {
		$db = Registry::get('base')->db;

		$data_line = "";
		$value_line = "";

		if (is_array($data)) {
			$size = count($data);
		} else {
			$size = count((array)$data);
		}
		$i = 1;

		foreach ($data as $key => $value) {
			$data_line .= $key;
			$value_line .= "'".$db->real_escape_string($value)."'";

			if ($i < $size) {
				$data_line .= ", ";
				$value_line .= ", ";
				$i++;
			}
		}

		$sql = "INSERT INTO $table(".$data_line.") ";
		$sql .= "VALUES (".$value_line.");";

		return $sql;
	} // end insert

	public function delete() {

	} // end delete

	public static function clean($data) {
		foreach ($data as $key => $value) {
			if ($value == '') {
				if (is_array($data)) {
					unset($data[$key]);
				} else {
					unset($data->$key);
				}
			}
		}

		return $data;
	} // end clan_data

	// replaces clean, clean must die
	public static function validate($data, $schema) {

	} // end validate

	public static function where($text, $clause) {
		if ($text == '') {
			$text .= ' WHERE '.$clause;
		} else {
			$text .= ' AND '.$clause;
		}

		return $text;
	} // end where

} // end Database

?>