<?php

/**
 * SQL Dialect abstraction
 *
 * Abstracts out and hermetises SQL implementation differences.
 * Provides correct objects quoting scheme, boolean values and reformats basic querries.
 *
 * @version DR1.6
 */
abstract class SQLDialect {



	abstract public function getTrue();



	abstract public function getFalse();



	abstract public function getQuotes();



	/**
	 * Reformats SELECT querry
	 *
	 * Returns SQL query for given dialect. Provide SELECT...FROM...[WHERE] expression
	 * and optionally ordering, offset and limit information to generate valid query.
	 *
	 * @param $sql string SQL query
	 * @param $order string ordering column
	 * @param $descending boolean
	 * @param $offset int
	 * @param $limit int
	 */
	abstract public function getSQL($sql, $order, $descending, $offset, $limit);



	public function getInsert($tableName, $record) {
		$q = $this->getQuotes();
		$keys = array();
		$vals = array();
		foreach ($record as $key => $value) {
			$keys[] = $q . $key . $q;
			$vals[] = ($value === null)
				? 'NULL'
				: "'$value'";
		}
		$keysList = implode(', ', $keys);
		$valsList = implode(', ', $vals);
		return "INSERT INTO $tableName ($keysList) VALUES ($valsList)";
	}



	public function getUpdate($tableName, $record, $primaryKey, $keyValue) {
		$q = $this->getQuotes();
		$updates = array();
		foreach ($record as $key => $value) {
			$updates[] = ($value === null)
				? $q . $key . $q . '= NULL'
				: $q . $key . $q . ' = \'' . $value . '\'';
		}
		$updatesList = implode(', ', $updates);
		return "UPDATE $tableName SET $updatesList WHERE $primaryKey = '$keyValue'";
	}



	public function getDelete($tableName, $key, $keyValue) {
		$q = $this->getQuotes();
		$tableName = $q . $tableName . $q;
		$key = $q . $key . $q;
		return "DELETE FROM $tableName WHERE $key = '$keyValue'";
	}



} // class SQLDialect

?>