Add C/PostgreSQL version
This commit is contained in:
parent
d45287920a
commit
a940bf1599
|
@ -0,0 +1,6 @@
|
|||
CFLAGS=-I/usr/include/postgresql -O3
|
||||
|
||||
all: dcb_c_psql
|
||||
|
||||
dcb_c_psql: dcb_c_psql.o
|
||||
$(CC) -o $@ $^ -lpq
|
|
@ -0,0 +1,88 @@
|
|||
/*
|
||||
* This program is based on
|
||||
* src/test/examples/testlibpq.c (example 33.1)
|
||||
* from section 33.21 of the PostgreSQL documentation
|
||||
* (https://www.postgresql.org/docs/13/libpq-example.html)
|
||||
*
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include "libpq-fe.h"
|
||||
|
||||
static void
|
||||
exit_nicely(PGconn *conn)
|
||||
{
|
||||
PQfinish(conn);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
const char *conninfo;
|
||||
PGconn *conn;
|
||||
PGresult *res;
|
||||
int nFields;
|
||||
int i,
|
||||
j;
|
||||
struct timespec ts0, ts1, ts2, ts3, ts4;
|
||||
|
||||
/*
|
||||
* If the user supplies a parameter on the command line, use it as the
|
||||
* conninfo string; otherwise default to setting dbname=postgres and using
|
||||
* environment variables or defaults for all other connection parameters.
|
||||
*/
|
||||
if (argc > 1)
|
||||
conninfo = argv[1];
|
||||
else
|
||||
conninfo = "";
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts0);
|
||||
|
||||
/* Make a connection to the database */
|
||||
conn = PQconnectdb(conninfo);
|
||||
|
||||
/* Check to see that the backend connection was successfully made */
|
||||
if (PQstatus(conn) != CONNECTION_OK)
|
||||
{
|
||||
fprintf(stderr, "Connection to database failed: %s",
|
||||
PQerrorMessage(conn));
|
||||
exit_nicely(conn);
|
||||
}
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts1);
|
||||
|
||||
res = PQexec(conn, "select current_timestamp");
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
{
|
||||
fprintf(stderr, "Select command failed: %s", PQerrorMessage(conn));
|
||||
PQclear(res);
|
||||
exit_nicely(conn);
|
||||
}
|
||||
|
||||
/* first, print out the attribute names */
|
||||
nFields = PQnfields(res);
|
||||
for (i = 0; i < nFields; i++)
|
||||
printf("%-15s", PQfname(res, i));
|
||||
printf("\n\n");
|
||||
|
||||
/* next, print out the rows */
|
||||
for (i = 0; i < PQntuples(res); i++)
|
||||
{
|
||||
for (j = 0; j < nFields; j++)
|
||||
printf("%-15s", PQgetvalue(res, i, j));
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
PQclear(res);
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts2);
|
||||
|
||||
/* close the connection to the database and cleanup */
|
||||
PQfinish(conn);
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts3);
|
||||
printf("%f connect\n", (ts1.tv_sec - ts0.tv_sec) + (ts1.tv_nsec - ts0.tv_nsec) * 1E-9);
|
||||
printf("%f session\n", (ts3.tv_sec - ts0.tv_sec) + (ts3.tv_nsec - ts0.tv_nsec) * 1E-9);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue