Search Postgresql Archives

Basic questions about PQprepare()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux