background image

 

*

DB

 

*/

 

protected

 

$db;

 

/**

 

*

true

 

*/

 

protected

 

$stored_procedure

 

=

 

false;

 

/**

 

*

query

 

*/

 

private

 

$query;

 

/**

 

*

SQL

 

*/

 

private   static   $QUOTE_MATCH   =   "/(".*(?db   =   $db;

}
public

 

function

 

prepare($query)

{
 

$this->stored_procedure

 

=

 

true;

}
public

 

function

 

compile($args)

{}
public

 

function

 

execute($query)

{}

prepare

  为使用例 1 中的模板,你要做的第一件事是构建好 prepare()函数,为确保无带引
号的字符被偶然解析为占位符,函数应该移除 query 内所有字符串,并把它们临时存储
在一个数组内。而字符串本身也会被占位符取代,其通常被识别为不应该在 SQL 语句中
出现的的字符串序列。在 query 的编译期间,过程占位符会首先被替换,接着把字符串
放回 query 中,这是通过 preg_replace()函数,和另一个用作 preg_replace()函数的
helper

 

 

2

prepare()

/**
*

 

query

*

 

@param

 

string

 

$query

 

Prepared

 

query

 

text

*

 

@return

 

void

*/
public

 

function

 

prepare($query)

{
 

$this->stored_procedure

 

=

 

true;

 

$this->quote_store  

=  

array();

 

//

 

$this->query

 

=  

preg_replace(self::$QUOTE_MATCH,

 

'$this-

>sql_quote_replace("1"?"1":'2')',

 

$query);