<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * TSM database connection.
 *
 * @package    PTA
 * @subpackage TSM
 * @category   Drivers
 * @author     Deon George
 * @copyright  (c) 2010 phpTSMadmin Development Team
 * @license    http://phptsmadmin.sf.net/license.html
 */
abstract class Database_TSM extends Database {
	// Database in use by each connection
	protected static $_current_databases = array();

	// Use SET NAMES to set the character set
	protected static $_set_names;

	// Identifier for this connection within the PHP driver
	protected $_connection_id;

	// TSM does not use a backtick for identifiers
	protected $_identifier = '';

	// Our TSM Message Format
	protected $msg_format = '[A-Z]{3}[0-9]{4}[I|E|W|S]';

	// Our Message Codes stored in the last query
	protected $_query_msg_codes = array();

	// Our return code error messages we can ignore
	protected $ignore_codes = array(
		'ANS8001I', // Return code %s.
	);
	// Our return code error messages we can ignore
	protected $nodata_codes = array(
		'ANR2034E', // SELECT: No match found using this criteria.
	);

	protected $execute_stdout = '';
	protected $execute_stderr = '';
	protected $execute_rc = NULL;

	// Our required abstract methods
	public function begin($mode = NULL) {}
	public function commit() {}
	public function rollback() {}
	public function set_charset($charset) {}
	public function list_tables($like = NULL) {}

	// Required methods
	abstract protected function execute($sql);

	/**
	 * Return the caching defined in the current configuration.
	 *
	 *     $cache_time = $db->caching("table");
	 *
	 * @return  string
	 */
	public function caching($table) {
		return ($this->_config['caching'] AND isset($this->_config['cache'][$table])) ? $this->_config['cache'][$table] : FALSE;
	}

	private function clear() {
		$this->_query_msg_codes = array();
	}

	public function query($type, $sql, $as_object = FALSE, array $params = NULL) {
		// Make sure the database is connected
		if (! $this->_connection && ! $this->connect())
			return new Database_TSM_Result(array(),'',$as_object,$params);

		$this->clear();

		if ( ! empty($this->_config['profiling']))
			// Benchmark this query for the current instance
			$benchmark = Profiler::start("Database ({$this->_instance})", $sql);

		// Execute the query
		$result = $this->execute($sql);

		if ($this->execute_stderr AND $this->execute_rc) {
			// This benchmark is worthless
			if (isset($benchmark))
				Profiler::delete($benchmark);

			SystemMessage::TSM('error',sprintf('%s (%s)',$this->execute_stdout,$this->execute_stderr),$sql);
		}

		if (isset($benchmark))
			Profiler::stop($benchmark);

		// Set the last query
		$this->last_query = $sql;

		if ($type === Database::SELECT)
			// Return an iterator of results
			return new Database_TSM_Result($result, $sql, $as_object, $params);
		elseif ($type === Database::INSERT)
			throw new Kohana_Exception('Database INSERTS are not supported');
		elseif ($type === Database::SHOW)
			return new Database_TSM_Show($result, $sql, $as_object, $params);
		elseif ($type === Database::SET)
			return new Database_TSM_Set($result, $sql, $as_object, $params);
	}

	public function escape($value) {
		// SQL standard is to use single-quotes for all values
		return "'$value'";
	}
}
?>