Location: PHPKode > projects > KISS > PDOSqlCommand.php
<?php
/**
*
* @author ÂíÌÎ <hide@address.com>
* @version v 2.1 alpha 2004/10/27
* @package Core_Class
*/

/**
* ͨÓÃÊý¾Ý¿â³éÏó²ã£¬ÓÅ»¯ÐÔÄÜ£¬Ê¹ÓÃÁË Singleton Éè¼Æģʽ
*/
abstract class KISS_PDOSqlCommand extends BaseObject {
	public $mLink;
	public $mDatabaseHost;
	public $mDatabaseUsername;
	public $mDatabasePassword;
	public $mDatabaseName;
	public $mDatabasePort;

	private  $mQueryArray = array();
	private $uniqueKey;

	static $theInstances = array();

	function &getInstance($pDBConfig = 0) {
		if(empty(SqlCommand::$theInstances[$pDBConfig])) {
			$registry = &Registry::instance();
			$db_configs = $registry->getEntry('database_connections');
			$dbconfig = $db_configs[$pDBConfig];
			$class_name = $dbconfig['DatabaseType'].'Command';
			if (!class_exists($class_name)) {
				die('²»Ö§³Ö¸ÃÊý¾Ý¿âÀàÐÍ£º'.$class_name.'!<br>Ö§³ÖÀàÐÍ:MySql MsSql');
			}
			SqlCommand::$theInstances[$pDBConfig] = new $class_name;
			SqlCommand::$theInstances[$pDBConfig]->resetDB($db_configs[$pDBConfig]);
		}
		return SqlCommand::$theInstances[$pDBConfig];
	}

	function __construct($pDBConfig = "") {
		parent::__construct();
	}

	function __destruct() {
		$this->closeDBForce();
	}

	function resetDB ($DBConfig) {
		$this->mDatabaseHost = $DBConfig['DatabaseHost'];
		$this->mDatabaseUsername = $DBConfig['DatabaseUsername'];
		$this->mDatabasePassword = $DBConfig['DatabasePassword'];
		$this->mDatabaseName = $DBConfig['DatabaseName'];
		$this->mDatabasePort = $DBConfig['DatabasePort'];

		$this->closeDBForce ();
	}

	function connectDB () {
		if(!is_resource($this->mLink)) {
			$this->mLink = $this->db_connect();
		}
	}

	function closeDBForce () {
		$this->connection = null;
	}

	function ExecuteNonQuery ($sql) {
		return $this->connection->exec($sql);
	}

	function ExecuteInsertQuery ($sql) {
		if($this->connection->exec($sql) > 0) {
			return $this->connection->lastInsertId();
		}
		else {
			return 0;
		}
	}

	function ExecuteCountQuery ($pSql) {
		$count_sql = preg_replace("/select.+?\sfrom\s/i", "SELECT count(*) FROM ", $pSql);
		return $this->ExecuteScalar($count_sql);
	}

	function ExecuteArrayQuery ($sql, $pPageNo=0, $pPageSize = 10, $pResultType = 'both') {
		if($pPageNo > 0) {
			$result = $this->PreparePagedArrayQuery($sql, $pPageNo, $pPageSize, $pResultType);
		}
		else {
			return $this->connection->query($sql);
		}
	}

	function ExecuteIteratorQuery ($sql, $pPageNo=0, $pPageSize = 10, $pResultType = 'both') {
		return $this->ExecuteArrayQuery($sql, $pPageNo, $pPageSize, $pResultType);
	}

	/**
	* Ìṩ Cache »º³åµÄÊý¾Ý·µ»Ø²éѯ,ÓÉÓÚÒª¼ì²âÊÜÓ°ÏìµÄ Table µÄÐÞ¸Äʱ¼ä,ºÍ¸üРCache Îļþ,»áÓÐÒ»µãµÄÐÔÄÜËðʧ,ÊʺϺÄʱ½Ï´óµÄ²éѯ
	*/
	function ExecuteCacheArrayQuery($sql, $pPageNo=0, $pPageSize = 10, $pResultType = 'both', $pTableName) {
		/**
		* ²éѯÊÜÓ°ÏìµÄ Table µÄÐÞ¸Äʱ¼ä,È·¶¨ Cache ÎļþµÄÃû×Ö
		*/
		$status_update_time = "";
		for($i=0; $i<count($pTableName); $i++) {
			$status_sql = "SHOW TABLE STATUS LIKE '{$pTableName[$i]}'";
			$status = $this->ExecuteArrayQuery($status_sql);
			$status_update_time .= $status[0]['Update_time'];
		}

		/**
		* ¼ì²é Cache Îļþ,Èç¹û´æÔÚ¾Í´Ó Cache Îļþ¶ÁÈ¡,·ñÔòÔÙ´ÓÊý¾Ý¿â¶ÁÈ¡
		*/
		if(Cache::haveSqlCache($sql,$status_update_time)) {
			return Cache::getSqlCache($sql,$status_update_time);
		}
		return $this->ExecuteArrayQuery($sql, $pPageNo, $pPageSize, $pResultType);
	}

	function ExecuteStringQuery ($sql) {
		die('Please Use:Util::Array2String()');
	}

	function ExecuteHashQuery ($sql) {
		die('Please Use:Util::Array2Hash()');
	}

	function ExecuteScalar ($sql) {
		$sth = $this->connection->prepare($sql);
		$sth->execute();
		return $sth->fetchColumn();
	}

	abstract function getTableFieldHash($pTable);

	abstract function getTablePrimeKey($pTable);

	public abstract function db_insert_id() ;

	public abstract function db_affected_rows($pResult);

	public abstract function db_num_rows($pResult) ;

	public abstract function db_fetch_array($pResult ,$pResultType = 'both');

	public abstract function db_free_result($pResult);

	public abstract function db_query($pQuery);

	public abstract function db_connect();

	public abstract function db_close();

	public abstract function db_data_seek($result_identifier, $row_number);

	public abstract function PreparePagedArrayQuery ($sql, $pPageNo=0, $pPageSize = 10);
}
?>
Return current item: KISS