@ -46,15 +46,16 @@
static HTAB * prepared_queries = NULL ;
static void InitQueryHashTable ( void ) ;
static ParamListInfo EvaluateParams ( PreparedStatement * pstmt , List * params ,
const char * queryString , EState * estate ) ;
static ParamListInfo EvaluateParams ( ParseState * pstate ,
PreparedStatement * pstmt , List * params ,
EState * estate ) ;
static Datum build_regtype_array ( Oid * param_types , int num_params ) ;
/*
* Implements the ' PREPARE ' utility statement .
*/
void
PrepareQuery ( PrepareStmt * stmt , const char * queryString ,
PrepareQuery ( ParseState * pstate , PrepareStmt * stmt ,
int stmt_location , int stmt_len )
{
RawStmt * rawstmt ;
@ -90,7 +91,7 @@ PrepareQuery(PrepareStmt *stmt, const char *queryString,
* Create the CachedPlanSource before we do parse analysis , since it needs
* to see the unmodified raw parse tree .
*/
plansource = CreateCachedPlan ( rawstmt , queryString ,
plansource = CreateCachedPlan ( rawstmt , pstate - > p_sourcetext ,
CreateCommandTag ( stmt - > query ) ) ;
/* Transform list of TypeNames to array of type OIDs */
@ -98,16 +99,8 @@ PrepareQuery(PrepareStmt *stmt, const char *queryString,
if ( nargs )
{
ParseState * pstate ;
ListCell * l ;
/*
* typenameTypeId wants a ParseState to carry the source query string .
* Is it worth refactoring its API to avoid this ?
*/
pstate = make_parsestate ( NULL ) ;
pstate - > p_sourcetext = queryString ;
argtypes = ( Oid * ) palloc ( nargs * sizeof ( Oid ) ) ;
i = 0 ;
@ -125,7 +118,7 @@ PrepareQuery(PrepareStmt *stmt, const char *queryString,
* passed in from above us will not be visible to it ) , allowing
* information about unknown parameters to be deduced from context .
*/
query = parse_analyze_varparams ( rawstmt , queryString ,
query = parse_analyze_varparams ( rawstmt , pstate - > p_sourcetext ,
& argtypes , & nargs ) ;
/*
@ -189,16 +182,11 @@ PrepareQuery(PrepareStmt *stmt, const char *queryString,
* indicated by passing a non - null intoClause . The DestReceiver is already
* set up correctly for CREATE TABLE AS , but we still have to make a few
* other adjustments here .
*
* Note : this is one of very few places in the code that needs to deal with
* two query strings at once . The passed - in queryString is that of the
* EXECUTE , which we might need for error reporting while processing the
* parameter expressions . The query_string that we copy from the plan
* source is that of the original PREPARE .
*/
void
ExecuteQuery ( ExecuteStmt * stmt , IntoClause * intoClause ,
const char * queryString , ParamListInfo params ,
ExecuteQuery ( ParseState * pstate ,
ExecuteStmt * stmt , IntoClause * intoClause ,
ParamListInfo params ,
DestReceiver * dest , char * completionTag )
{
PreparedStatement * entry ;
@ -229,8 +217,7 @@ ExecuteQuery(ExecuteStmt *stmt, IntoClause *intoClause,
*/
estate = CreateExecutorState ( ) ;
estate - > es_param_list_info = params ;
paramLI = EvaluateParams ( entry , stmt - > params ,
queryString , estate ) ;
paramLI = EvaluateParams ( pstate , entry , stmt - > params , estate ) ;
}
/* Create a new portal to run the query in */
@ -314,9 +301,9 @@ ExecuteQuery(ExecuteStmt *stmt, IntoClause *intoClause,
/*
* EvaluateParams : evaluate a list of parameters .
*
* pstate : parse state
* pstmt : statement we are getting parameters for .
* params : list of given parameter expressions ( raw parser output ! )
* queryString : source text for error messages .
* estate : executor state to use .
*
* Returns a filled - in ParamListInfo - - this can later be passed to
@ -324,13 +311,12 @@ ExecuteQuery(ExecuteStmt *stmt, IntoClause *intoClause,
* during query execution .
*/
static ParamListInfo
EvaluateParams ( PreparedStatement * pstmt , List * params ,
const char * queryString , EState * estate )
EvaluateParams ( ParseState * pstate , P reparedStatement * pstmt , List * params ,
EState * estate )
{
Oid * param_types = pstmt - > plansource - > param_types ;
int num_params = pstmt - > plansource - > num_params ;
int nparams = list_length ( params ) ;
ParseState * pstate ;
ParamListInfo paramLI ;
List * exprstates ;
ListCell * l ;
@ -354,9 +340,6 @@ EvaluateParams(PreparedStatement *pstmt, List *params,
*/
params = copyObject ( params ) ;
pstate = make_parsestate ( NULL ) ;
pstate - > p_sourcetext = queryString ;
i = 0 ;
foreach ( l , params )
{
@ -648,6 +631,11 @@ ExplainExecuteQuery(ExecuteStmt *execstmt, IntoClause *into, ExplainState *es,
/* Evaluate parameters, if any */
if ( entry - > plansource - > num_params )
{
ParseState * pstate ;
pstate = make_parsestate ( NULL ) ;
pstate - > p_sourcetext = queryString ;
/*
* Need an EState to evaluate parameters ; must not delete it till end
* of query , in case parameters are pass - by - reference . Note that the
@ -656,8 +644,8 @@ ExplainExecuteQuery(ExecuteStmt *execstmt, IntoClause *into, ExplainState *es,
*/
estate = CreateExecutorState ( ) ;
estate - > es_param_list_info = params ;
paramLI = EvaluateParams ( entry , execstmt - > params ,
queryString , estate ) ;
paramLI = EvaluateParams ( pstate , entry , execstmt - > params , estate ) ;
}
/* Replan if needed, and acquire a transient refcount */