Basic questions about PQprepare()

From: Alexander Farber <alexander(dot)farber(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Basic questions about PQprepare()
Date: 2006-01-27 22:19:26
Message-ID: 943abd910601271419y4ef9735die5fffcb245f00799@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello,

I'm trying to write an Apache 1.3.29 module connecting to PostgreSQL 8.1.0
on OpenBSD -current and have few probably simple questions:

When an Apache child is initialized, I'd like to establish connection to
the database and to prepare 2 queries. And then later in the repeating
response phase I'd like to execute those prepared queries.

1) If PQconnectdb fails, do I still need to PQfinish the returned pointer?

cfg->conn = PQconnectdb(cfg->conninfo);
if (NULL == cfg->conn) {
ap_log_error(APLOG_MARK, APLOG_ERR, s,
"Connection to database '%s' failed: out of memory",
cfg->conninfo);
exit(1);
}
if (PQstatus(cfg->conn) != CONNECTION_OK) {
ap_log_error(APLOG_MARK, APLOG_ERR, s,
"Connection to database '%s' failed: %s",
cfg->conninfo, PQerrorMessage(cfg->conn));
PQfinish(cfg->conn);
exit(1);
}

2) Similar, if PQprepare fails, do I still need to PQclear its result?
And what value is returned on PQprepare success, is it always
PGRES_COMMAND_OK (I've got that value, but will it always be so)?

#define SQL_BANNED_USER \
"select message, expire from bans where username = $1 and " \
"(expire is null or expire > extract(epoch from localtime))"

res = PQprepare(cfg->conn, "sql_banned_user", SQL_FIND_USER, 1, NULL);
if (NULL == res) {
ap_log_error(APLOG_MARK, APLOG_ERR, s,
"Preparing statement '%s' failed: out of memory",
SQL_BANNED_USER);
PQfinish(cfg->conn);
exit(1);
}
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
ap_log_error(APLOG_MARK, APLOG_ERR, s,
"Preparing statement '%s' failed: %s",
SQL_BANNED_USER, PQerrorMessage(cfg->conn));
PQclear(res);
PQfinish(cfg->conn);
exit(1);
}

3) Do I have to PQclear(res) inbetween if I want to prepare another query?

4) How do I set the last PQprepare argument, the const Oid *paramTypes?
The FAQ says an OID is a unique int. I'm confused how to use it here.
For example I know that the argument to my prepared statement will be
a string (a username). What is the OID then?

I couldn't find any good examples with PQprepare() yet,
does anybody please have a pointer to nice short examples?

Regards
Alex

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Oliver Fürst 2006-01-27 22:34:35 Re: Are rules transaction safe?
Previous Message Scott Marlowe 2006-01-27 21:37:53 Re: xml_valid function