|
|
@ -6,6 +6,7 @@ |
|
|
|
#include <stdio.h> |
|
|
|
#include <stdio.h> |
|
|
|
#include <string.h> |
|
|
|
#include <string.h> |
|
|
|
#include <stdarg.h> |
|
|
|
#include <stdarg.h> |
|
|
|
|
|
|
|
#include <stdlib.h> |
|
|
|
|
|
|
|
|
|
|
|
#include "libpq-fe.h" |
|
|
|
#include "libpq-fe.h" |
|
|
|
#include "halt.h" |
|
|
|
#include "halt.h" |
|
|
@ -16,31 +17,33 @@ |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#ifndef TRUE |
|
|
|
#ifndef TRUE |
|
|
|
#define TRUE 1 |
|
|
|
#define TRUE 1 |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#ifndef FALSE |
|
|
|
#ifndef FALSE |
|
|
|
#define FALSE 0 |
|
|
|
#define FALSE 0 |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
/* GLOBAL VARIABLES */ |
|
|
|
/* GLOBAL VARIABLES */ |
|
|
|
static PGconn *conn; |
|
|
|
static PGconn *conn; |
|
|
|
static PGresult *res = NULL; |
|
|
|
static PGresult *res = NULL; |
|
|
|
|
|
|
|
|
|
|
|
static int tuple; /* stores fetch location */ |
|
|
|
static int tuple; /* stores fetch location */ |
|
|
|
|
|
|
|
|
|
|
|
#define ON_ERROR_STOP 0 |
|
|
|
#define ON_ERROR_STOP 0 |
|
|
|
#define ON_ERROR_CONTINUE 1 |
|
|
|
#define ON_ERROR_CONTINUE 1 |
|
|
|
|
|
|
|
|
|
|
|
static int on_error_state = ON_ERROR_STOP; /* halt on errors? */ |
|
|
|
static int on_error_state = ON_ERROR_STOP; /* halt on errors? */ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int user_has_res = FALSE; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void add_res_tuple(void); |
|
|
|
|
|
|
|
static void get_res_tuple(void); |
|
|
|
|
|
|
|
static void del_res_tuple(void); |
|
|
|
|
|
|
|
|
|
|
|
static int in_result_block = FALSE;
|
|
|
|
|
|
|
|
static int was_get_unset_result = FALSE; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* |
|
|
|
|
|
|
|
* connectdb - returns PGconn structure |
|
|
|
* connectdb - returns PGconn structure |
|
|
|
* |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
PGconn * |
|
|
|
PGconn * |
|
|
|
connectdb(char *options) |
|
|
|
connectdb(char *options) |
|
|
@ -53,17 +56,14 @@ connectdb(char *options) |
|
|
|
return conn; |
|
|
|
return conn; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* |
|
|
|
|
|
|
|
* disconnectdb |
|
|
|
* disconnectdb |
|
|
|
* |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
void |
|
|
|
void |
|
|
|
disconnectdb() |
|
|
|
disconnectdb() |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (res != NULL && |
|
|
|
if (res != NULL && user_has_res == FALSE) |
|
|
|
in_result_block == FALSE && |
|
|
|
|
|
|
|
was_get_unset_result == FALSE) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
PQclear(res); |
|
|
|
PQclear(res); |
|
|
|
res = NULL; |
|
|
|
res = NULL; |
|
|
@ -72,20 +72,17 @@ disconnectdb() |
|
|
|
PQfinish(conn); |
|
|
|
PQfinish(conn); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* |
|
|
|
|
|
|
|
* doquery - returns PGresult structure |
|
|
|
* doquery - returns PGresult structure |
|
|
|
* |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
PGresult * |
|
|
|
PGresult * |
|
|
|
doquery(char *query) |
|
|
|
doquery(char *query) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (res != NULL && |
|
|
|
if (res != NULL && user_has_res == FALSE) |
|
|
|
in_result_block == FALSE && |
|
|
|
|
|
|
|
was_get_unset_result == FALSE) |
|
|
|
|
|
|
|
PQclear(res); |
|
|
|
PQclear(res); |
|
|
|
|
|
|
|
|
|
|
|
was_get_unset_result = FALSE; |
|
|
|
user_has_res = FALSE; |
|
|
|
res = PQexec(conn, query); |
|
|
|
res = PQexec(conn, query); |
|
|
|
|
|
|
|
|
|
|
|
if (on_error_state == ON_ERROR_STOP && |
|
|
|
if (on_error_state == ON_ERROR_STOP && |
|
|
@ -105,11 +102,10 @@ doquery(char *query) |
|
|
|
return res; |
|
|
|
return res; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* |
|
|
|
|
|
|
|
* fetch - returns tuple number (starts at 0), or the value END_OF_TUPLES |
|
|
|
* fetch - returns tuple number (starts at 0), or the value END_OF_TUPLES |
|
|
|
* NULL pointers are skipped |
|
|
|
* NULL pointers are skipped |
|
|
|
* |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
int |
|
|
|
int |
|
|
|
fetch(void *param,...) |
|
|
|
fetch(void *param,...) |
|
|
@ -142,8 +138,8 @@ fetch(void *param,...) |
|
|
|
return tuple++; |
|
|
|
return tuple++; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* |
|
|
|
|
|
|
|
* fetchwithnulls - returns tuple number (starts at 0), |
|
|
|
* fetchwithnulls - returns tuple number (starts at 0), |
|
|
|
* or the value END_OF_TUPLES |
|
|
|
* or the value END_OF_TUPLES |
|
|
|
* Returns TRUE or FALSE into null indicator variables |
|
|
|
* Returns TRUE or FALSE into null indicator variables |
|
|
@ -185,10 +181,19 @@ fetchwithnulls(void *param,...) |
|
|
|
return tuple++; |
|
|
|
return tuple++; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
* reset_fetch |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
void |
|
|
|
|
|
|
|
reset_fetch() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
tuple = 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* |
|
|
|
|
|
|
|
* on_error_stop |
|
|
|
* on_error_stop |
|
|
|
* |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
void |
|
|
|
void |
|
|
|
on_error_stop() |
|
|
|
on_error_stop() |
|
|
@ -196,10 +201,9 @@ on_error_stop() |
|
|
|
on_error_state = ON_ERROR_STOP; |
|
|
|
on_error_state = ON_ERROR_STOP; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* |
|
|
|
|
|
|
|
* on_error_continue |
|
|
|
* on_error_continue |
|
|
|
* |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
void |
|
|
|
void |
|
|
|
on_error_continue() |
|
|
|
on_error_continue() |
|
|
@ -209,92 +213,132 @@ on_error_continue() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* |
|
|
|
|
|
|
|
* get_result |
|
|
|
* get_result |
|
|
|
* |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
PGresult * |
|
|
|
PGresult * |
|
|
|
get_result() |
|
|
|
get_result() |
|
|
|
{ |
|
|
|
{ |
|
|
|
char *cmdstatus = PQcmdStatus(res); |
|
|
|
if (res == NULL) |
|
|
|
|
|
|
|
halt("get_result called with no result pointer used\n"); |
|
|
|
|
|
|
|
|
|
|
|
was_get_unset_result = TRUE; |
|
|
|
/* delete it if it is already there; we are about to re-add it */ |
|
|
|
|
|
|
|
del_res_tuple(); |
|
|
|
|
|
|
|
|
|
|
|
/* we have to store the fetch location somewhere */ |
|
|
|
/* we have to store the fetch location */ |
|
|
|
/* XXX THIS IS A NO-NO */ |
|
|
|
add_res_tuple(); |
|
|
|
cmdstatus[0] = NUL; |
|
|
|
|
|
|
|
memcpy(&cmdstatus[1], &tuple, sizeof(tuple)); |
|
|
|
user_has_res = TRUE; |
|
|
|
|
|
|
|
|
|
|
|
return res; |
|
|
|
return res; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* |
|
|
|
|
|
|
|
* set_result |
|
|
|
* set_result |
|
|
|
* |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
void |
|
|
|
void |
|
|
|
set_result(PGresult *newres) |
|
|
|
set_result(PGresult *newres) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
char *cmdstatus = PQcmdStatus(res); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (newres == NULL) |
|
|
|
if (newres == NULL) |
|
|
|
halt("set_result called with null result pointer\n"); |
|
|
|
halt("set_result called with null result pointer\n"); |
|
|
|
|
|
|
|
|
|
|
|
if (res != NULL && was_get_unset_result == FALSE) |
|
|
|
if (res != NULL && user_has_res == FALSE) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (in_result_block == FALSE) |
|
|
|
/*
|
|
|
|
PQclear(res); |
|
|
|
* Basically, throw away res. We can't return to it because the |
|
|
|
else |
|
|
|
* user doesn't have the res pointer. |
|
|
|
{ |
|
|
|
*/ |
|
|
|
/* XXX THIS IS A NO-NO */ |
|
|
|
del_res_tuple(); |
|
|
|
cmdstatus[0] = NUL; |
|
|
|
PQclear(res); |
|
|
|
memcpy(&cmdstatus[1], &tuple, sizeof(tuple)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
in_result_block = TRUE; |
|
|
|
user_has_res = FALSE; |
|
|
|
was_get_unset_result = FALSE; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cmdstatus = PQcmdStatus(newres); |
|
|
|
|
|
|
|
memcpy(&tuple, &cmdstatus[1], sizeof(tuple)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res = newres; |
|
|
|
res = newres; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
get_res_tuple(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* |
|
|
|
* Routines to store res/tuple mapping |
|
|
|
* unset_result |
|
|
|
* This is used to keep track of fetch locations while using get/set on |
|
|
|
* |
|
|
|
* result sets. |
|
|
|
|
|
|
|
* Auto-growing array is used, with empty slots marked by res == NULL |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
void |
|
|
|
|
|
|
|
unset_result(PGresult *oldres) |
|
|
|
static struct res_tuple |
|
|
|
{ |
|
|
|
{ |
|
|
|
char *cmdstatus = PQcmdStatus(oldres); |
|
|
|
PGresult *res; |
|
|
|
|
|
|
|
int tuple; |
|
|
|
|
|
|
|
} *res_tuple = NULL; |
|
|
|
|
|
|
|
|
|
|
|
if (oldres == NULL) |
|
|
|
static int res_tuple_len = 0; |
|
|
|
halt("unset_result called with null result pointer\n"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (in_result_block == FALSE) |
|
|
|
|
|
|
|
halt("Unset of result without being set.\n"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
was_get_unset_result = TRUE; |
|
|
|
/*
|
|
|
|
|
|
|
|
* add_res_tuple |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
static void |
|
|
|
|
|
|
|
add_res_tuple(void) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
int i, |
|
|
|
|
|
|
|
new_res_tuple_len = res_tuple_len ? res_tuple_len * 2 : 1; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < res_tuple_len; i++) |
|
|
|
|
|
|
|
/* Put it in an empty slot */ |
|
|
|
|
|
|
|
if (res_tuple[i].res == NULL) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
res_tuple[i].res = res; |
|
|
|
|
|
|
|
res_tuple[i].tuple = tuple; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Need to grow array */ |
|
|
|
|
|
|
|
res_tuple = realloc(res_tuple, new_res_tuple_len * sizeof(struct res_tuple)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* clear new elements */ |
|
|
|
|
|
|
|
for (i = res_tuple_len; i < new_res_tuple_len; i++) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
res_tuple[i].res = NULL; |
|
|
|
|
|
|
|
res_tuple[i].tuple = 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* XXX THIS IS A NO-NO */ |
|
|
|
/* recursion to add entry */ |
|
|
|
cmdstatus[0] = NUL; |
|
|
|
add_res_tuple(); |
|
|
|
memcpy(&cmdstatus[1], &tuple, sizeof(tuple)); |
|
|
|
|
|
|
|
in_result_block = FALSE; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* |
|
|
|
* get_res_tuple |
|
|
|
* reset_fetch |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
void |
|
|
|
static void |
|
|
|
reset_fetch() |
|
|
|
get_res_tuple(void) |
|
|
|
{ |
|
|
|
{ |
|
|
|
tuple = 0; |
|
|
|
int i; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < res_tuple_len; i++) |
|
|
|
|
|
|
|
if (res_tuple[i].res == res) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
tuple = res_tuple[i].tuple; |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
halt("get_res_tuple called with invalid result pointer\n"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
* del_res_tuple |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
static void |
|
|
|
|
|
|
|
del_res_tuple(void) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
int i; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < res_tuple_len; i++) |
|
|
|
|
|
|
|
if (res_tuple[i].res == res) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
res_tuple[i].res = NULL; |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|