Basic questions about PQprepare()

Поиск
Список
Период
Сортировка
От Alexander Farber
Тема Basic questions about PQprepare()
Дата
Msg-id 943abd910601271419y4ef9735die5fffcb245f00799@mail.gmail.com
обсуждение исходный текст
Ответы Re: Basic questions about PQprepare()  (Tom Lane <tgl@sss.pgh.pa.us>)
Список 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

В списке pgsql-general по дате отправления:

Предыдущее
От: Scott Marlowe
Дата:
Сообщение: Re: xml_valid function
Следующее
От: Oliver Fürst
Дата:
Сообщение: Re: Are rules transaction safe?