>  기사  >  php教程  >  Prepare statement

Prepare statement

PHP中文网
PHP中文网원래의
2016-05-25 17:03:311363검색

php代码

 query pairs 
 * Expired will be recompile automatically, any update on config should update ctime as well
 *
 * @author Anthony.chen 
 * 2010-2012 reserved
 */
class PSTMT{

	public static $instances = array();

	/**
	 * Loading the prepare statment from DB Connection
	 * The stmts is config linked to DB instance with 'stmts' k=>v array
	 *
	 * @return Boolean
	 */
	public static function prepare($db='default'){
		//Getting statment from config
		$_config = Pexcel::config('database')->$db;
		$_ctime = Arr::get($_config,'ctime',0);
		$_configStmts = Arr::get($_config,'stmts',NULL);
		if($_configStmts != NULL){//There is statments configured
			if(!isset(self::$instances[$db])){
				$_sql = 'select name , EXTRACT(EPOCH FROM prepare_time) as ctime from pg_prepared_statements';
				$_pstmts = DB::query(DB::SELECT,$_sql,true)->execute($db)->as_array();
				if(!$_pstmts){
					self::$instances[$db] = array();
				}else{
					//Log::debug('Before Commpiling,statement Found');
					foreach($_pstmts as $_pstmt){
						self::$instances[$db][$_pstmt->name] = $_pstmt->ctime;
					}
				}
			}
			//Compile the statments
			foreach($_configStmts as $stmtName => $stmtQuery){
				if(isset(self::$instances[$db][$stmtName])){
					if( self::$instances[$db][$stmtName] < $_ctime){
						//Log::debug($stmtName.&#39; Expires&#39;);
					}else{
						//Log::debug($stmtName.&#39; Exists&#39;);
						continue;
					}
				}
				self::compile($stmtName,$stmtQuery,$db);
			}

		}else{
			throw new Error_Exception(&#39;stmts not in config!&#39;);
		}
		return True;
	}

	/**
	 * Compile the prepared statment 
	 *
	 * @param String $stmtName, Statement name 
	 * @param String $stmtQuery, Statement query
	 * @param String $db ,Instance name of database
	 *
	 * @return Boolean
	 */
	public static function compile($stmtName, $stmtQuery,$db =&#39;default&#39;){
		if(isset(self::$instances[$db][$stmtName])){ //already Compiled
			//Doing Nothing
			//Log::debug(&#39;DEALLOCATE &#39;.$stmtName);
			DB::query(DB::UPDATE,&#39;DEALLOCATE &#39;.$stmtName)->execute($db);
		}
		$_ret = DB::query(DB::SELECT,$stmtQuery,False,NULL,$stmtName)->execute($db)->count();
		//Log::debug(__FUNCTION__.&#39;:compiling the pstat:&#39;.$stmtQuery);
		self::$instances[$db][$stmtName] = time();
		return True;
	}

	/**
	 * Execute the prepared statement
	 *
	 * @param String $stmtName, Statement Name
	 * @param Array $params , The parameter to be transfered into query
	 * @param Boolean $as_object, True to fetch result as object
	 * @param String $db, Database Instance name
	 *
	 * @return Array of result set
	 */

	public static function execute($stmtName,$params = array(),$as_object = TRUE,$db = &#39;default&#39;){
		if(isset(self::$instances[$db][$stmtName])){
			return DB::query(DB::SELECT,NULL,$as_object,$params,$stmtName)->execute($db);
		}else{
			$_config = Pexcel::config(&#39;database&#39;)->$db;
			$_configStmts = Arr::get($_config,&#39;stmts&#39;,NULL);
			//Compile the prepared statment 
			if(isset($_configStmts[$stmtName])){
				self::compile($stmtName,$_configStmts[$stmtName],$db);
			}else{
				return false;
			}
			return DB::query(DB::SELECT,NULL,$as_object,$params,$stmtName)->execute($db);
		}
	}
}

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.