Return value of function - Mailing list pgsql-novice

From Masakazu Ichimura
Subject Return value of function
Date
Msg-id 20020326095323.51CC.M.ICHIMURA@nihontechno.co.jp
Whole thread Raw
List pgsql-novice
Hi

I'm using PostgreSQL7.1.3.
I want to compare speed of calling function and direct access.

Direct access I write is below
----------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include "libpq-fe.h"

void exit_nicely(PGconn *conn)
{
    PQfinish(conn);
    exit(1);
}

int main()
{
    char    *pghost;
    char    *pgport;
    char    *pgoptions;
    char    *pgtty;
    char    *dbName;

    PGconn        *conn;
    PGresult    *res;
/* Set Parameter */
    pghost        = NULL;
    pgport        = NULL;
    pgoptions    = NULL;
    pgtty        = NULL;
    dbname        = "imr";
/* Connect to DB */
    conn = PGsetdb(pghost, pgport, pgoptions, pgtty, dbName);

    if(PQstatus(conn) == CONNECTION_BAD)
    {
        fprintf(stderr, "Connection database '%s' failed.\n", dbName);
        fprintf(stderr, "%s", PQerrorMessage(conn));
        exit_nicely(conn);
    }
/* Begining transaction */
    res = PQexec(conn, "BEGIN");
    if( !res || PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "BEGIN command failed\n");
        PQclear(res);
        exit_nicely(conn);
    }
    PQclear(res);
/* Declare a cursor */
    res = PQexec(conn, "DECLARE mycursor CURSOR FOR select * FROM test1");
    if( !res || PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "DECLARE CURSOR command failed\n");
        PQclear(res);
        return(-1);
    }
    PQclear(res);
/* Fetch */
    res = PQexec(conn, "FETCH ALL in mycursor");
    if( !res || PQresultStatus(res) != PGRES_TUPLES_OK)
    {
        fprintf(stderr, "FETCH ALL command didn't return tuples properly\n");
        PQclear(res);
        return(-1);
    }
/* Commit transaction */
    res = PQexec(conn, "COMMIT");
    PQclear(res);

    PQfinish(conn);
    return 0;
}
----------------------------------------------------------------

I want to rewrite 'DECLERE' and 'FETCH' part of this program
with function,like 'res = PQexec(conn, function_name);'.

But What RETURN value I should use in function?
Can function return multi rows?

Thank you.


pgsql-novice by date:

Previous
From: Jason Earl
Date:
Subject: Re: Searching for all records but integer field is causing
Next
From: Richard Ray
Date:
Subject: Retrieving archives