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, 2006 Marc Munro
     License:      BSD
 $Id: veil_query.c,v 1.5 2007/07/31 22:18:27 bloodnok Exp $

Definition in file veil_query.c.

#include <stdio.h>
#include "postgres.h"
#include "executor/spi.h"
#include "veil_funcs.h"
#include "veil_version.h"

Include dependency graph for veil_query.c:

Go to the source code of this file.

Defines

#define FETCH_SIZE   20

Typedefs

typedef bool( Fetch_fn )(HeapTuple, TupleDesc, void *)

Functions

int vl_spi_connect ()
int vl_spi_finish ()
static void prepare_query (const char *qry, int nargs, Oid *argtypes, Datum *args, bool read_only, void **saved_plan)
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 bool fetch_one_int (HeapTuple tuple, TupleDesc tupdesc, void *p_result)
static bool fetch_one_bool (HeapTuple tuple, TupleDesc tupdesc, void *p_result)
static bool fetch_one_str (HeapTuple tuple, TupleDesc tupdesc, void *p_result)
static bool int_from_query (const char *qry, int4 *result)
bool vl_bool_from_query (const char *qry, bool *result)
static bool str_from_oid_query (const char *qry, const Oid param, char *result)
static void check_type (Oid oid, Oid expected_oid, const char *msg)
bool vl_db_exists (Oid db_id)

Variables

static int4 query_depth = 0
static TransactionId connection_xid = 0


Define Documentation

#define FETCH_SIZE   20

The number of records to fetch in one go from the query executor.

Definition at line 33 of file veil_query.c.


Typedef Documentation

typedef bool( Fetch_fn)(HeapTuple, TupleDesc, void *)

A Fetch_fn is a function that processes records, one at a time, returned from a query.

Definition at line 26 of file veil_query.c.


Function Documentation

static void check_type ( Oid  oid,
Oid  expected_oid,
const char *  msg 
) [static]

Raise an error if type is not as expected.

Parameters:
oid Type oid of value.
expected_oid Type oid of value that we are expecting.
msg Supplementary text for error message if the types do not match.

Definition at line 339 of file veil_query.c.

References str_from_oid_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 239 of file veil_query.c.

Referenced by vl_bool_from_query().

static bool fetch_one_int ( 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 220 of file veil_query.c.

Referenced by int_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 258 of file veil_query.c.

Referenced by str_from_oid_query().

static bool int_from_query ( const char *  qry,
int4 *  result 
) [static]

Executes a query that returns a single int4 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 276 of file veil_query.c.

References fetch_one_int(), and query().

Here is the call graph for this function:

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 109 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 177 of file veil_query.c.

References prepare_query(), and query_depth.

Referenced by int_from_query(), str_from_oid_query(), and vl_bool_from_query().

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 317 of file veil_query.c.

References fetch_one_str(), and query().

Referenced by check_type().

Here is the call graph for this function:

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 296 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:

bool vl_db_exists ( Oid  db_id  ) 

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

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

Definition at line 379 of file veil_query.c.

Referenced by get_shmem_context().


Variable Documentation

TransactionId connection_xid = 0 [static]

State variable used to assess whther query_depth may have been left in an invalid state following an error being raised.

Definition at line 46 of file veil_query.c.

int4 query_depth = 0 [static]

Counter to assess depth of recursive spi calls, so that we can sensibly and safely use spi_push and spi_pop when appropriate.

Definition at line 40 of file veil_query.c.

Referenced by query().


Generated on Tue Mar 11 10:08:37 2008 for Veil by  doxygen 1.5.4