veil_query.c File Reference


Detailed Description

Functions to simplify SPI-based queries. These are way more sophisticated than veil really needs but are nice and generic.

     Author:       Marc Munro
     Copyright (c) 2005 - 2011 Marc Munro
     License:      BSD

Definition in file veil_query.c.

#include <stdio.h>
#include "postgres.h"
#include "catalog/pg_type.h"
#include "executor/spi.h"
#include "veil_version.h"
#include "access/xact.h"
#include "veil_funcs.h"

Include dependency graph for veil_query.c:

Go to the source code of this file.

Defines

#define FETCH_SIZE   20
 The number of records to fetch in one go from the query executor.

Typedefs

typedef bool( Fetch_fn )(HeapTuple, TupleDesc, void *)
 A Fetch_fn is a function that processes records, one at a time, returned from a query.

Functions

int vl_spi_connect (bool *p_pushed)
 If already connected in this session, push the current connection, and get a new one.
int vl_spi_finish (bool pushed)
 Reciprocal function for vl_spi_connect().
static void prepare_query (const char *qry, int nargs, Oid *argtypes, Datum *args, bool read_only, void **saved_plan)
 Prepare a query for query().
static int query (const char *qry, int nargs, Oid *argtypes, Datum *args, bool read_only, void **saved_plan, Fetch_fn process_row, void *fn_param)
 Prepare and execute a query.
static bool fetch_one_bool (HeapTuple tuple, TupleDesc tupdesc, void *p_result)
 Fetch_fn function for processing a single row of a single integer for query.
static bool fetch_one_str (HeapTuple tuple, TupleDesc tupdesc, void *p_result)
 Fetch_fn function for processing a single row of a single integer for query.
bool vl_bool_from_query (const char *qry, bool *result)
 Executes a query that returns a single bool value.
static bool str_from_oid_query (const char *qry, const Oid param, char *result)
 Executes a query by oid, that returns a single string value.
bool vl_db_exists (Oid db_id)
 Determine whether the given oid represents an existing database or not.
static bool exec_init_fn (HeapTuple tuple, TupleDesc tupdesc, void *p_param)
 Fetch_fn function for executing registered veil_init() functions for query.
int vl_call_init_fns (bool param)
 Identify any registered init_functions and execute them.


Function Documentation

int vl_spi_connect ( bool *  p_pushed  ) 

If already connected in this session, push the current connection, and get a new one.

We are already connected, if:

  • are within a query
  • and the current transaction id matches the saved transaction id

Definition at line 46 of file veil_query.c.

Referenced by ensure_init(), and veil_init().

static void prepare_query ( const char *  qry,
int  nargs,
Oid *  argtypes,
Datum *  args,
bool  read_only,
void **  saved_plan 
) [static]

Prepare a query for query().

This creates and executes a plan. The caller must have established SPI_connect. It is assumed that no parameters to the query will be null.

Parameters:
qry The text of the SQL query to be performed.
nargs The number of input parameters ($1, $2, etc) to the query
argtypes Pointer to an array containing the OIDs of the data
args Actual parameters types of the parameters
read_only Whether the query should be read-only or not
saved_plan Adress of void pointer into which the query plan will be saved. Passing the same void pointer on a subsequent call will cause the saved query plan to be re-used.

Definition at line 86 of file veil_query.c.

Referenced by query().

static int query ( const char *  qry,
int  nargs,
Oid *  argtypes,
Datum *  args,
bool  read_only,
void **  saved_plan,
Fetch_fn  process_row,
void *  fn_param 
) [static]

Prepare and execute a query.

Query execution consists of a call to process_row for each returned record. Process_row can return a single value to the caller of this function through the fn_param parameter. It is the caller's responsibility to establish an SPI connection with SPI_connect. It is assumed that no parameters to the query, and no results will be null.

Parameters:
qry The text of the SQL query to be performed.
nargs The number of input parameters ($1, $2, etc) to the query
argtypes Pointer to an array containing the OIDs of the data
args Actual parameters types of the parameters
read_only Whether the query should be read-only or not
saved_plan Adress of void pointer into which the query plan will be saved. Passing the same void pointer on a subsequent call will cause the saved query plan to be re-used.
process_row The Fetch_fn function to be called for each fetched row to process it. If this is null, we simply count the row, doing no processing on the tuples returned.
fn_param An optional parameter to the process_row function. This may be used to return a value to the caller.
Returns:
The total number of records fetched and processed by process_row.

Definition at line 153 of file veil_query.c.

References prepare_query().

Referenced by str_from_oid_query(), and vl_bool_from_query().

Here is the call graph for this function:

static bool fetch_one_bool ( HeapTuple  tuple,
TupleDesc  tupdesc,
void *  p_result 
) [static]

Fetch_fn function for processing a single row of a single integer for query.

Parameters:
tuple The row to be processed
tupdesc Descriptor for the types of the fields in the tuple.
p_result Pointer to an int4 variable into which the value returned from the query will be placed.
Returns:
false. This causes query to terminate after processing a single row.

Definition at line 196 of file veil_query.c.

Referenced by vl_bool_from_query().

static bool fetch_one_str ( HeapTuple  tuple,
TupleDesc  tupdesc,
void *  p_result 
) [static]

Fetch_fn function for processing a single row of a single integer for query.

Parameters:
tuple The row to be processed
tupdesc Descriptor for the types of the fields in the tuple.
p_result Pointer to an int4 variable into which the value returned from the query will be placed.
Returns:
false. This causes query to terminate after processing a single row.

Definition at line 216 of file veil_query.c.

Referenced by str_from_oid_query().

bool vl_bool_from_query ( const char *  qry,
bool *  result 
)

Executes a query that returns a single bool value.

Parameters:
qry The text of the query to be performed.
result Variable into which the result of the query will be placed.
Returns:
true if the query returned a record, false otherwise.

Definition at line 234 of file veil_query.c.

References fetch_one_bool(), and query().

Referenced by ensure_init(), and veil_init().

Here is the call graph for this function:

static bool str_from_oid_query ( const char *  qry,
const Oid  param,
char *  result 
) [static]

Executes a query by oid, that returns a single string value.

Parameters:
qry The text of the query to be performed.
param The oid of the row to be fetched.
result Variable into which the result of the query will be placed.
Returns:
true if the query returned a record, false otherwise.

Definition at line 255 of file veil_query.c.

References fetch_one_str(), and query().

Here is the call graph for this function:

bool vl_db_exists ( Oid  db_id  ) 

Determine whether the given oid represents an existing database or not.

Parameters:
db_id Oid of the database in which we are interested.
Returns:
True if the database exists.

Definition at line 277 of file veil_query.c.

Referenced by get_shmem_context().

static bool exec_init_fn ( HeapTuple  tuple,
TupleDesc  tupdesc,
void *  p_param 
) [static]

Fetch_fn function for executing registered veil_init() functions for query.

Parameters:
tuple The row to be processed
tupdesc Descriptor for the types of the fields in the tuple.
p_param Pointer to a boolean value which is the value of the argument to the init function being called.
Returns:
true. This allows query to process further rows.

Definition at line 296 of file veil_query.c.

int vl_call_init_fns ( bool  param  ) 

Identify any registered init_functions and execute them.

Parameters:
param The boolean parameter to be passed to each init_function.
Returns:
The number of init_functions executed.

Definition at line 341 of file veil_query.c.

Referenced by veil_int4array_get().


Generated on Mon Sep 12 15:26:54 2011 for Veil by  doxygen 1.5.6