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 $
Functions to simplify SPI-based queries.
These are way more sophisticated than veil really needs but are nice and generic.
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 |
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 () |
int | vl_spi_finish () |
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_int (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_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. | |
static bool | int_from_query (const char *qry, int4 *result) |
Executes a query that returns a single int4 value. | |
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. | |
static void | check_type (Oid oid, Oid expected_oid, const char *msg) |
Raise an error if type is not as expected. | |
bool | vl_db_exists (Oid db_id) |
Determine whether the given oid represents an existing database or not. | |
Variables | |
static int4 | query_depth = 0 |
Counter to assess depth of recursive spi calls, so that we can sensibly and safely use spi_push and spi_pop when appropriate. | |
static TransactionId | connection_xid = 0 |
State variable used to assess whther query_depth may have been left in an invalid state following an error being raised. |
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.
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.
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. |
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 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.
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. |
Definition at line 220 of file veil_query.c.
Referenced by int_from_query().
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.
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. |
Definition at line 239 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.
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. |
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.
qry | The text of the query to be performed. | |
result | Variable into which the result of the query will be placed. |
Definition at line 276 of file veil_query.c.
References fetch_one_int(), and query().
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.
qry | The text of the query to be performed. | |
result | Variable into which the result of the query will be placed. |
Definition at line 296 of file veil_query.c.
References fetch_one_bool(), and query().
Referenced by ensure_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.
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. |
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:
static void check_type | ( | Oid | oid, | |
Oid | expected_oid, | |||
const char * | msg | |||
) | [static] |
Raise an error if type is not as expected.
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:
bool vl_db_exists | ( | Oid | db_id | ) |
Determine whether the given oid represents an existing database or not.
dbid | Oid of the database in which we are interested. |
Definition at line 379 of file veil_query.c.
Referenced by get_shmem_context().