summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilrik de Loose <wilrik@wilrik.nl>2008-05-07 14:53:17 (GMT)
committerWilrik de Loose <wilrik@wilrik.nl>2008-05-07 14:53:17 (GMT)
commitbbcc93fada48dfbee6b45eb73ba58f53b4f9d1e2 (patch)
treeeaaa9c36b5e261a3e4477864c51eed87a65f2503
parentf88154ffd368af3a63ddbed5516f51097721a248 (diff)
download2iv55-bbcc93fada48dfbee6b45eb73ba58f53b4f9d1e2.zip
2iv55-bbcc93fada48dfbee6b45eb73ba58f53b4f9d1e2.tar.gz
2iv55-bbcc93fada48dfbee6b45eb73ba58f53b4f9d1e2.tar.bz2
db queries uitvoerbaar + printf info (van select resultaat etc).
-rw-r--r--db_demo/db.c259
-rw-r--r--db_demo/db.h25
-rw-r--r--db_demo/db_demo.ncbbin494592 -> 887808 bytes
-rw-r--r--db_demo/db_demo.suobin13824 -> 19968 bytes
-rw-r--r--db_demo/db_demo.vcproj6
-rw-r--r--db_demo/libpq-fe.h510
-rw-r--r--db_demo/main.c54
-rw-r--r--db_demo/main.h9
-rw-r--r--db_demo/postgres_ext.h69
-rw-r--r--db_demo/reset.c70
-rw-r--r--db_demo/reset.h25
11 files changed, 297 insertions, 730 deletions
diff --git a/db_demo/db.c b/db_demo/db.c
new file mode 100644
index 0000000..3a58c3f
--- /dev/null
+++ b/db_demo/db.c
@@ -0,0 +1,259 @@
+#include <libpq-fe.h>
+#include <string.h>
+#include <stdlib.h>
+#include <math.h>
+#include "db.h"
+
+#define DB_MAX_FIELDS 64
+#define DB_MAX_CHAR 256
+
+static PGconn *g_pConnection = NULL;
+static PGresult *g_pResult = NULL;
+static char g_pError[DB_MAX_CHAR];
+
+
+static int db_clear_error(void)
+{
+ memset(&g_pError, 0, (size_t)(sizeof(char) * DB_MAX_CHAR));
+ return DB_OK;
+
+} // db_clear_error
+
+
+static int db_set_error(const char *f_pError)
+{
+ db_clear_error();
+ strcpy(g_pError, f_pError);
+ return DB_OK;
+
+} // db_set_error
+
+
+static void db_raise_warning(void)
+{
+ printf("Database exception!\n");
+ printf("%s", g_pError);
+
+} // db_raise_warning
+
+
+static void db_raise_error(void)
+{
+ db_raise_warning();
+ db_close();
+ exit(1);
+
+} // db_raise_error
+
+
+static char *db_get_error(void)
+{
+ return g_pError;
+
+} // db_get_error
+
+
+
+static int db_check_connection(void)
+{
+ char l_pError[DB_MAX_CHAR];
+
+ if (g_pConnection == NULL)
+ {
+ sprintf(l_pError, "Database connection lost!");
+ db_set_error(l_pError);
+ db_raise_error();
+
+ return DB_NO_CONNECTION;
+ }
+
+ return DB_OK;
+
+} // db_check_connection
+
+
+static int db_check_result(void)
+{
+ char l_pError[DB_MAX_CHAR];
+
+ if (g_pResult == NULL)
+ {
+ sprintf(l_pError, "Incorrect PostgreSQL result.");
+ db_set_error(l_pError);
+ db_raise_warning();
+
+ return DB_INCORRECT_RESULT;
+ }
+
+ return DB_OK;
+
+} // db_check_result
+
+
+int db_init(void)
+{
+ char l_pError[DB_MAX_CHAR];
+
+ g_pConnection = PQsetdbLogin(PGHOST, PGPORT, PGOPTIONS, PGTTY, DBNAME, LOGIN, PWD);
+
+ if (PQstatus(g_pConnection) == CONNECTION_BAD)
+ {
+ sprintf(l_pError, "Connection to database '%s' failed.\n%s", DBNAME, PQerrorMessage(g_pConnection));
+ db_set_error(l_pError);
+ db_raise_error();
+ return DB_NO_CONNECTION;
+ }
+
+ db_clear_error();
+
+ return DB_OK;
+
+} // db_init
+
+
+int db_close(void)
+{
+ PQclear(g_pResult);
+ PQfinish(g_pConnection);
+
+ return DB_OK;
+
+} // db_close
+
+
+int db_output(void)
+{
+ int l_iNTuples, l_iNFields;
+ char l_pText[DB_MAX_CHAR];
+ int l_iColumnWidth[DB_MAX_FIELDS];
+ int l_iMaxColumnWidth[DB_MAX_FIELDS];
+ int i, j, k, l;
+
+ // check the db connection
+ if (db_check_connection() != DB_OK)
+ {
+ exit(1);
+ return DB_COULD_NOT_OUTPUT;
+ }
+
+ // check the validity of the result
+ if (db_check_result() != DB_OK)
+ {
+ return DB_COULD_NOT_OUTPUT;
+ }
+
+ l_iNTuples = PQntuples(g_pResult);
+ l_iNFields = PQnfields(g_pResult);
+
+ // copy all tuple names incl. field values
+ for (i = 0; i < l_iNFields; i++)
+ {
+ k = 0;
+ for (j = 0; j < l_iNTuples; j++)
+ {
+ strcpy(l_pText, PQgetvalue(g_pResult, j, i));
+ l_iColumnWidth[k] = (int)strlen(l_pText);
+ k++;
+ }
+
+ strcpy(l_pText, PQfname(g_pResult, i));
+ l_iMaxColumnWidth[i] = (int)strlen(l_pText);
+ for (j = 1; j < l_iNTuples; j++)
+ {
+ l_iMaxColumnWidth[i] = max(l_iMaxColumnWidth[i], l_iColumnWidth[j]);
+ }
+ }
+
+ // print column names
+ for (i = 0; i < l_iNFields; i++)
+ {
+ int len;
+ if (i) printf("| ");
+
+ strcpy(l_pText, PQfname(g_pResult, i));
+ printf("%s", l_pText);
+ len = (int)strlen(PQfname(g_pResult, i)) - 1;
+
+ if (i < l_iNFields - 1)
+ {
+ for (k = 0; k < l_iMaxColumnWidth[i] - len; k++)
+ {
+ printf(" ");
+ }
+ }
+ }
+
+ printf("\n");
+
+ // print horizontal line
+ l = 0;
+ for (k = 0; k < l_iNFields; k++) l += l_iMaxColumnWidth[k];
+ l += (l_iNFields - 1) * 3;
+ for (k = 0; k < l; k++) printf("-");
+ printf("\n");
+
+ // print row values
+ for (i = 0; i < l_iNTuples; i++)
+ {
+ for (j = 0; j < l_iNFields; j++)
+ {
+ int len;
+ if (j) printf("| ");
+
+ strcpy(l_pText, PQgetvalue(g_pResult, i, j));
+ printf("%s ", l_pText);
+ len = (int)strlen(l_pText) - 1;
+
+ if (j < l_iNFields - 1)
+ {
+ for (k = 1; k < l_iMaxColumnWidth[j] - len; k++)
+ {
+ printf(" ");
+ }
+ }
+ }
+ printf("\n");
+ }
+
+ return DB_OK;
+
+} // db_output
+
+
+int db_execute(const char* f_pQuery)
+{
+ char l_pError[DB_MAX_CHAR];
+ char l_pCmdStatus[DB_MAX_CHAR];
+
+ // check the db connection
+ if (db_check_connection() != DB_OK)
+ {
+ exit(1);
+ return DB_COULD_NOT_EXECUTE;
+ }
+
+ g_pResult = PQexec(g_pConnection, f_pQuery);
+
+ if (g_pResult == NULL || (PQresultStatus(g_pResult) != PGRES_COMMAND_OK && PQresultStatus(g_pResult) != PGRES_TUPLES_OK))
+ {
+ sprintf(l_pError, "Unable to execute query: %s\n%s", f_pQuery, PQerrorMessage(g_pConnection));
+ db_set_error(l_pError);
+ db_raise_warning();
+
+ return DB_COULD_NOT_EXECUTE;
+ }
+
+ sprintf(l_pCmdStatus, "%s", PQcmdStatus(g_pResult));
+
+ if (!strcmp(l_pCmdStatus, "SELECT"))
+ {
+ db_output();
+ }
+ else
+ {
+ printf("%s\n", l_pCmdStatus);
+ }
+
+ return DB_OK;
+
+} // db_execute
diff --git a/db_demo/db.h b/db_demo/db.h
new file mode 100644
index 0000000..4b896d2
--- /dev/null
+++ b/db_demo/db.h
@@ -0,0 +1,25 @@
+#ifndef _DB_H
+#define _DB_H
+
+#define PGHOST "matchblox.schinagl.nl"
+#define PGPORT "5432"
+#define PGOPTIONS "\0"
+#define PGTTY "\0"
+#define DBNAME "2iv55"
+#define LOGIN "2iv55"
+#define PWD "uhtfiwwp"
+
+enum db_result
+{
+ DB_OK,
+ DB_NO_CONNECTION,
+ DB_COULD_NOT_EXECUTE,
+ DB_INCORRECT_RESULT,
+ DB_COULD_NOT_OUTPUT
+};
+
+int db_init(void);
+int db_close(void);
+int db_execute(const char*);
+
+#endif \ No newline at end of file
diff --git a/db_demo/db_demo.ncb b/db_demo/db_demo.ncb
index f24034d..5f33877 100644
--- a/db_demo/db_demo.ncb
+++ b/db_demo/db_demo.ncb
Binary files differ
diff --git a/db_demo/db_demo.suo b/db_demo/db_demo.suo
index 852290c..9cb28c4 100644
--- a/db_demo/db_demo.suo
+++ b/db_demo/db_demo.suo
Binary files differ
diff --git a/db_demo/db_demo.vcproj b/db_demo/db_demo.vcproj
index d4717c3..f0019b8 100644
--- a/db_demo/db_demo.vcproj
+++ b/db_demo/db_demo.vcproj
@@ -181,6 +181,10 @@
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
+ RelativePath=".\db.c"
+ >
+ </File>
+ <File
RelativePath=".\main.c"
>
</File>
@@ -191,7 +195,7 @@
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
- RelativePath=".\main.h"
+ RelativePath=".\db.h"
>
</File>
</Filter>
diff --git a/db_demo/libpq-fe.h b/db_demo/libpq-fe.h
deleted file mode 100644
index a675335..0000000
--- a/db_demo/libpq-fe.h
+++ /dev/null
@@ -1,510 +0,0 @@
-/*-------------------------------------------------------------------------
- *
- * libpq-fe.h
- * This file contains definitions for structures and
- * externs for functions used by frontend postgres applications.
- *
- * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
- * Portions Copyright (c) 1994, Regents of the University of California
- *
- * $PostgreSQL: pgsql/src/interfaces/libpq/libpq-fe.h,v 1.116.4.1 2006/05/21 20:20:05 tgl Exp $
- *
- *-------------------------------------------------------------------------
- */
-
-#ifndef LIBPQ_FE_H
-#define LIBPQ_FE_H
-
-#ifdef __cplusplus
-extern "C"
-{
-#endif
-
-#include <stdio.h>
-
-/*
- * postgres_ext.h defines the backend's externally visible types,
- * such as Oid.
- */
-#include "postgres_ext.h"
-
-/* SSL type is needed here only to declare PQgetssl() */
-#ifdef USE_SSL
-#include <openssl/ssl.h>
-#endif
-
-/* Application-visible enum types */
-
-typedef enum
-{
- /*
- * Although it is okay to add to this list, values which become unused
- * should never be removed, nor should constants be redefined - that
- * would break compatibility with existing code.
- */
- CONNECTION_OK,
- CONNECTION_BAD,
- /* Non-blocking mode only below here */
-
- /*
- * The existence of these should never be relied upon - they should
- * only be used for user feedback or similar purposes.
- */
- CONNECTION_STARTED, /* Waiting for connection to be made. */
- CONNECTION_MADE, /* Connection OK; waiting to send. */
- CONNECTION_AWAITING_RESPONSE, /* Waiting for a response from the
- * postmaster. */
- CONNECTION_AUTH_OK, /* Received authentication; waiting for
- * backend startup. */
- CONNECTION_SETENV, /* Negotiating environment. */
- CONNECTION_SSL_STARTUP, /* Negotiating SSL. */
- CONNECTION_NEEDED /* Internal state: connect() needed */
-} ConnStatusType;
-
-typedef enum
-{
- PGRES_POLLING_FAILED = 0,
- PGRES_POLLING_READING, /* These two indicate that one may */
- PGRES_POLLING_WRITING, /* use select before polling again. */
- PGRES_POLLING_OK,
- PGRES_POLLING_ACTIVE /* unused; keep for awhile for backwards
- * compatibility */
-} PostgresPollingStatusType;
-
-typedef enum
-{
- PGRES_EMPTY_QUERY = 0, /* empty query string was executed */
- PGRES_COMMAND_OK, /* a query command that doesn't return
- * anything was executed properly by the
- * backend */
- PGRES_TUPLES_OK, /* a query command that returns tuples was
- * executed properly by the backend,
- * PGresult contains the result tuples */
- PGRES_COPY_OUT, /* Copy Out data transfer in progress */
- PGRES_COPY_IN, /* Copy In data transfer in progress */
- PGRES_BAD_RESPONSE, /* an unexpected response was recv'd from
- * the backend */
- PGRES_NONFATAL_ERROR, /* notice or warning message */
- PGRES_FATAL_ERROR /* query failed */
-} ExecStatusType;
-
-typedef enum
-{
- PQTRANS_IDLE, /* connection idle */
- PQTRANS_ACTIVE, /* command in progress */
- PQTRANS_INTRANS, /* idle, within transaction block */
- PQTRANS_INERROR, /* idle, within failed transaction */
- PQTRANS_UNKNOWN /* cannot determine status */
-} PGTransactionStatusType;
-
-typedef enum
-{
- PQERRORS_TERSE, /* single-line error messages */
- PQERRORS_DEFAULT, /* recommended style */
- PQERRORS_VERBOSE /* all the facts, ma'am */
-} PGVerbosity;
-
-/* PGconn encapsulates a connection to the backend.
- * The contents of this struct are not supposed to be known to applications.
- */
-typedef struct pg_conn PGconn;
-
-/* PGresult encapsulates the result of a query (or more precisely, of a single
- * SQL command --- a query string given to PQsendQuery can contain multiple
- * commands and thus return multiple PGresult objects).
- * The contents of this struct are not supposed to be known to applications.
- */
-typedef struct pg_result PGresult;
-
-/* PGcancel encapsulates the information needed to cancel a running
- * query on an existing connection.
- * The contents of this struct are not supposed to be known to applications.
- */
-typedef struct pg_cancel PGcancel;
-
-/* PGnotify represents the occurrence of a NOTIFY message.
- * Ideally this would be an opaque typedef, but it's so simple that it's
- * unlikely to change.
- * NOTE: in Postgres 6.4 and later, the be_pid is the notifying backend's,
- * whereas in earlier versions it was always your own backend's PID.
- */
-typedef struct pgNotify
-{
- char *relname; /* notification condition name */
- int be_pid; /* process ID of server process */
- char *extra; /* notification parameter */
- /* Fields below here are private to libpq; apps should not use 'em */
- struct pgNotify *next; /* list link */
-} PGnotify;
-
-/* Function types for notice-handling callbacks */
-typedef void (*PQnoticeReceiver) (void *arg, const PGresult *res);
-typedef void (*PQnoticeProcessor) (void *arg, const char *message);
-
-/* Print options for PQprint() */
-typedef char pqbool;
-
-typedef struct _PQprintOpt
-{
- pqbool header; /* print output field headings and row
- * count */
- pqbool align; /* fill align the fields */
- pqbool standard; /* old brain dead format */
- pqbool html3; /* output html tables */
- pqbool expanded; /* expand tables */
- pqbool pager; /* use pager for output if needed */
- char *fieldSep; /* field separator */
- char *tableOpt; /* insert to HTML <table ...> */
- char *caption; /* HTML <caption> */
- char **fieldName; /* null terminated array of repalcement
- * field names */
-} PQprintOpt;
-
-/* ----------------
- * Structure for the conninfo parameter definitions returned by PQconndefaults
- *
- * All fields except "val" point at static strings which must not be altered.
- * "val" is either NULL or a malloc'd current-value string. PQconninfoFree()
- * will release both the val strings and the PQconninfoOption array itself.
- * ----------------
- */
-typedef struct _PQconninfoOption
-{
- char *keyword; /* The keyword of the option */
- char *envvar; /* Fallback environment variable name */
- char *compiled; /* Fallback compiled in default value */
- char *val; /* Option's current value, or NULL */
- char *label; /* Label for field in connect dialog */
- char *dispchar; /* Character to display for this field in
- * a connect dialog. Values are: ""
- * Display entered value as is "*"
- * Password field - hide value "D" Debug
- * option - don't show by default */
- int dispsize; /* Field size in characters for dialog */
-} PQconninfoOption;
-
-/* ----------------
- * PQArgBlock -- structure for PQfn() arguments
- * ----------------
- */
-typedef struct
-{
- int len;
- int isint;
- union
- {
- int *ptr; /* can't use void (dec compiler barfs) */
- int integer;
- } u;
-} PQArgBlock;
-
-/* ----------------
- * Exported functions of libpq
- * ----------------
- */
-
-/* === in fe-connect.c === */
-
-/* make a new client connection to the backend */
-/* Asynchronous (non-blocking) */
-extern PGconn *PQconnectStart(const char *conninfo);
-extern PostgresPollingStatusType PQconnectPoll(PGconn *conn);
-
-/* Synchronous (blocking) */
-extern PGconn *PQconnectdb(const char *conninfo);
-extern PGconn *PQsetdbLogin(const char *pghost, const char *pgport,
- const char *pgoptions, const char *pgtty,
- const char *dbName,
- const char *login, const char *pwd);
-
-#define PQsetdb(M_PGHOST,M_PGPORT,M_PGOPT,M_PGTTY,M_DBNAME) \
- PQsetdbLogin(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME, NULL, NULL)
-
-/* close the current connection and free the PGconn data structure */
-extern void PQfinish(PGconn *conn);
-
-/* get info about connection options known to PQconnectdb */
-extern PQconninfoOption *PQconndefaults(void);
-
-/* free the data structure returned by PQconndefaults() */
-extern void PQconninfoFree(PQconninfoOption *connOptions);
-
-/*
- * close the current connection and restablish a new one with the same
- * parameters
- */
-/* Asynchronous (non-blocking) */
-extern int PQresetStart(PGconn *conn);
-extern PostgresPollingStatusType PQresetPoll(PGconn *conn);
-
-/* Synchronous (blocking) */
-extern void PQreset(PGconn *conn);
-
-/* request a cancel structure */
-extern PGcancel *PQgetCancel(PGconn *conn);
-
-/* free a cancel structure */
-extern void PQfreeCancel(PGcancel *cancel);
-
-/* issue a cancel request */
-extern int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize);
-
-/* backwards compatible version of PQcancel; not thread-safe */
-extern int PQrequestCancel(PGconn *conn);
-
-/* Accessor functions for PGconn objects */
-extern char *PQdb(const PGconn *conn);
-extern char *PQuser(const PGconn *conn);
-extern char *PQpass(const PGconn *conn);
-extern char *PQhost(const PGconn *conn);
-extern char *PQport(const PGconn *conn);
-extern char *PQtty(const PGconn *conn);
-extern char *PQoptions(const PGconn *conn);
-extern ConnStatusType PQstatus(const PGconn *conn);
-extern PGTransactionStatusType PQtransactionStatus(const PGconn *conn);
-extern const char *PQparameterStatus(const PGconn *conn,
- const char *paramName);
-extern int PQprotocolVersion(const PGconn *conn);
-extern int PQserverVersion(const PGconn *conn);
-extern char *PQerrorMessage(const PGconn *conn);
-extern int PQsocket(const PGconn *conn);
-extern int PQbackendPID(const PGconn *conn);
-extern int PQclientEncoding(const PGconn *conn);
-extern int PQsetClientEncoding(PGconn *conn, const char *encoding);
-
-#ifdef USE_SSL
-/* Get the SSL structure associated with a connection */
-extern SSL *PQgetssl(PGconn *conn);
-#else
-extern void *PQgetssl(PGconn *conn);
-#endif
-
-/* Tell libpq whether it needs to initialize OpenSSL */
-extern void PQinitSSL(int do_init);
-
-/* Set verbosity for PQerrorMessage and PQresultErrorMessage */
-extern PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity);
-
-/* Enable/disable tracing */
-extern void PQtrace(PGconn *conn, FILE *debug_port);
-extern void PQuntrace(PGconn *conn);
-
-/* Override default notice handling routines */
-extern PQnoticeReceiver PQsetNoticeReceiver(PGconn *conn,
- PQnoticeReceiver proc,
- void *arg);
-extern PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
- PQnoticeProcessor proc,
- void *arg);
-
-/*
- * Used to set callback that prevents concurrent access to
- * non-thread safe functions that libpq needs.
- * The default implementation uses a libpq internal mutex.
- * Only required for multithreaded apps that use kerberos
- * both within their app and for postgresql connections.
- */
-typedef void (*pgthreadlock_t) (int acquire);
-
-extern pgthreadlock_t PQregisterThreadLock(pgthreadlock_t newhandler);
-
-/* === in fe-exec.c === */
-
-/* Simple synchronous query */
-extern PGresult *PQexec(PGconn *conn, const char *query);
-extern PGresult *PQexecParams(PGconn *conn,
- const char *command,
- int nParams,
- const Oid *paramTypes,
- const char *const * paramValues,
- const int *paramLengths,
- const int *paramFormats,
- int resultFormat);
-extern PGresult *PQprepare(PGconn *conn, const char *stmtName,
- const char *query, int nParams,
- const Oid *paramTypes);
-extern PGresult *PQexecPrepared(PGconn *conn,
- const char *stmtName,
- int nParams,
- const char *const * paramValues,
- const int *paramLengths,
- const int *paramFormats,
- int resultFormat);
-
-/* Interface for multiple-result or asynchronous queries */
-extern int PQsendQuery(PGconn *conn, const char *query);
-extern int PQsendQueryParams(PGconn *conn,
- const char *command,
- int nParams,
- const Oid *paramTypes,
- const char *const * paramValues,
- const int *paramLengths,
- const int *paramFormats,
- int resultFormat);
-extern int PQsendPrepare(PGconn *conn, const char *stmtName,
- const char *query, int nParams,
- const Oid *paramTypes);
-extern int PQsendQueryPrepared(PGconn *conn,
- const char *stmtName,
- int nParams,
- const char *const * paramValues,
- const int *paramLengths,
- const int *paramFormats,
- int resultFormat);
-extern PGresult *PQgetResult(PGconn *conn);
-
-/* Routines for managing an asynchronous query */
-extern int PQisBusy(PGconn *conn);
-extern int PQconsumeInput(PGconn *conn);
-
-/* LISTEN/NOTIFY support */
-extern PGnotify *PQnotifies(PGconn *conn);
-
-/* Routines for copy in/out */
-extern int PQputCopyData(PGconn *conn, const char *buffer, int nbytes);
-extern int PQputCopyEnd(PGconn *conn, const char *errormsg);
-extern int PQgetCopyData(PGconn *conn, char **buffer, int async);
-
-/* Deprecated routines for copy in/out */
-extern int PQgetline(PGconn *conn, char *string, int length);
-extern int PQputline(PGconn *conn, const char *string);
-extern int PQgetlineAsync(PGconn *conn, char *buffer, int bufsize);
-extern int PQputnbytes(PGconn *conn, const char *buffer, int nbytes);
-extern int PQendcopy(PGconn *conn);
-
-/* Set blocking/nonblocking connection to the backend */
-extern int PQsetnonblocking(PGconn *conn, int arg);
-extern int PQisnonblocking(const PGconn *conn);
-
-/* Force the write buffer to be written (or at least try) */
-extern int PQflush(PGconn *conn);
-
-/*
- * "Fast path" interface --- not really recommended for application
- * use
- */
-extern PGresult *PQfn(PGconn *conn,
- int fnid,
- int *result_buf,
- int *result_len,
- int result_is_int,
- const PQArgBlock *args,
- int nargs);
-
-/* Accessor functions for PGresult objects */
-extern ExecStatusType PQresultStatus(const PGresult *res);
-extern char *PQresStatus(ExecStatusType status);
-extern char *PQresultErrorMessage(const PGresult *res);
-extern char *PQresultErrorField(const PGresult *res, int fieldcode);
-extern int PQntuples(const PGresult *res);
-extern int PQnfields(const PGresult *res);
-extern int PQbinaryTuples(const PGresult *res);
-extern char *PQfname(const PGresult *res, int field_num);
-extern int PQfnumber(const PGresult *res, const char *field_name);
-extern Oid PQftable(const PGresult *res, int field_num);
-extern int PQftablecol(const PGresult *res, int field_num);
-extern int PQfformat(const PGresult *res, int field_num);
-extern Oid PQftype(const PGresult *res, int field_num);
-extern int PQfsize(const PGresult *res, int field_num);
-extern int PQfmod(const PGresult *res, int field_num);
-extern char *PQcmdStatus(PGresult *res);
-extern char *PQoidStatus(const PGresult *res); /* old and ugly */
-extern Oid PQoidValue(const PGresult *res); /* new and improved */
-extern char *PQcmdTuples(PGresult *res);
-extern char *PQgetvalue(const PGresult *res, int tup_num, int field_num);
-extern int PQgetlength(const PGresult *res, int tup_num, int field_num);
-extern int PQgetisnull(const PGresult *res, int tup_num, int field_num);
-
-/* Delete a PGresult */
-extern void PQclear(PGresult *res);
-
-/* For freeing other alloc'd results, such as PGnotify structs */
-extern void PQfreemem(void *ptr);
-
-/* Exists for backward compatibility. bjm 2003-03-24 */
-#define PQfreeNotify(ptr) PQfreemem(ptr)
-
-/* Define the string so all uses are consistent. */
-#define PQnoPasswordSupplied "fe_sendauth: no password supplied\n"
-
-/*
- * Make an empty PGresult with given status (some apps find this
- * useful). If conn is not NULL and status indicates an error, the
- * conn's errorMessage is copied.
- */
-extern PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status);
-
-
-/* Quoting strings before inclusion in queries. */
-extern size_t PQescapeStringConn(PGconn *conn,
- char *to, const char *from, size_t length,
- int *error);
-extern unsigned char *PQescapeByteaConn(PGconn *conn,
- const unsigned char *from, size_t from_length,
- size_t *to_length);
-extern unsigned char *PQunescapeBytea(const unsigned char *strtext,
- size_t *retbuflen);
-/* These forms are deprecated! */
-extern size_t PQescapeString(char *to, const char *from, size_t length);
-extern unsigned char *PQescapeBytea(const unsigned char *from, size_t from_length,
- size_t *to_length);
-
-
-
-/* === in fe-print.c === */
-
-extern void
-PQprint(FILE *fout, /* output stream */
- const PGresult *res,
- const PQprintOpt *ps); /* option structure */
-
-/*
- * really old printing routines
- */
-extern void
-PQdisplayTuples(const PGresult *res,
- FILE *fp, /* where to send the output */
- int fillAlign, /* pad the fields with spaces */
- const char *fieldSep, /* field separator */
- int printHeader, /* display headers? */
- int quiet);
-
-extern void
-PQprintTuples(const PGresult *res,
- FILE *fout, /* output stream */
- int printAttName, /* print attribute names */
- int terseOutput, /* delimiter bars */
- int width); /* width of column, if 0, use variable
- * width */
-
-
-/* === in fe-lobj.c === */
-
-/* Large-object access routines */
-extern int lo_open(PGconn *conn, Oid lobjId, int mode);
-extern int lo_close(PGconn *conn, int fd);
-extern int lo_read(PGconn *conn, int fd, char *buf, size_t len);
-extern int lo_write(PGconn *conn, int fd, char *buf, size_t len);
-extern int lo_lseek(PGconn *conn, int fd, int offset, int whence);
-extern Oid lo_creat(PGconn *conn, int mode);
-extern int lo_tell(PGconn *conn, int fd);
-extern int lo_unlink(PGconn *conn, Oid lobjId);
-extern Oid lo_import(PGconn *conn, const char *filename);
-extern int lo_export(PGconn *conn, Oid lobjId, const char *filename);
-
-/* === in fe-misc.c === */
-
-/* Determine length of multibyte encoded char at *s */
-extern int PQmblen(const unsigned char *s, int encoding);
-
-/* Determine display length of multibyte encoded char at *s */
-extern int PQdsplen(const unsigned char *s, int encoding);
-
-/* Get encoding id from environment variable PGCLIENTENCODING */
-extern int PQenv2encoding(void);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* LIBPQ_FE_H */
diff --git a/db_demo/main.c b/db_demo/main.c
index c28f273..37286f1 100644
--- a/db_demo/main.c
+++ b/db_demo/main.c
@@ -1,49 +1,11 @@
-#include <stdio.h>
-#include <time.h>
-#include <libpq-fe.h>
+#include "db.h"
-#include "main.h"
-
-int main(int argc, char *argv[]) {
- PGconn *conn;
- char SQL_query[512];
- PGresult *res;
- int ntuples, nfields;
- int i, j;
-
- conn = PQsetdbLogin(PGHOST, PGPORT, PGOPTIONS, PGTTY, DBNAME, LOGIN, PWD);
-
-
- if(CONNECTION_BAD == PQstatus(conn))
- {
- fprintf(stderr, "Connection to database '%s' failed.\n %s", DBNAME, PQerrorMessage(conn));
- PQfinish(conn);
- return 1;
- }
-
- sprintf(SQL_query, "SELECT * FROM testicle");
- res = PQexec(conn, SQL_query);
-
- if(res == NULL || PQresultStatus(res) != PGRES_TUPLES_OK)
- {
- printf("Unable to execute query: %s\n %s\n", SQL_query, PQerrorMessage(conn));
- }
- else
- {
- ntuples = PQntuples(res);
- nfields = PQnfields(res);
-
- for (i = 0; i < ntuples; i++)
- {
- for (j = 0; j < nfields; j++)
- {
- printf("Query result: %s - %s\n", PQfname(res, j), PQgetvalue(res, i, j));
- }
- }
- }
-
- PQclear(res);
- PQfinish(conn);
+int main(int argc, char *argv[])
+{
+ db_init();
+ db_execute("SELECT * FROM testicle ORDER BY teller");
+ db_close();
return 0;
-}
+
+} // main
diff --git a/db_demo/main.h b/db_demo/main.h
deleted file mode 100644
index 1064d61..0000000
--- a/db_demo/main.h
+++ /dev/null
@@ -1,9 +0,0 @@
-#define VERSION 0.0.1
-
-#define PGHOST "matchblox.schinagl.nl"
-#define PGPORT "5432"
-#define PGOPTIONS "\0"
-#define PGTTY "\0"
-#define DBNAME "2iv55"
-#define LOGIN "2iv55"
-#define PWD "uhtfiwwp"
diff --git a/db_demo/postgres_ext.h b/db_demo/postgres_ext.h
deleted file mode 100644
index efffad2..0000000
--- a/db_demo/postgres_ext.h
+++ /dev/null
@@ -1,69 +0,0 @@
-/*-------------------------------------------------------------------------
- *
- * postgres_ext.h
- *
- * This file contains declarations of things that are visible everywhere
- * in PostgreSQL *and* are visible to clients of frontend interface libraries.
- * For example, the Oid type is part of the API of libpq and other libraries.
- *
- * Declarations which are specific to a particular interface should
- * go in the header file for that interface (such as libpq-fe.h). This
- * file is only for fundamental Postgres declarations.
- *
- * User-written C functions don't count as "external to Postgres."
- * Those function much as local modifications to the backend itself, and
- * use header files that are otherwise internal to Postgres to interface
- * with the backend.
- *
- * $PostgreSQL: pgsql/src/include/postgres_ext.h,v 1.16 2004/08/29 05:06:55 momjian Exp $
- *
- *-------------------------------------------------------------------------
- */
-
-#ifndef POSTGRES_EXT_H
-#define POSTGRES_EXT_H
-
-/*
- * Object ID is a fundamental type in Postgres.
- */
-typedef unsigned int Oid;
-
-#ifdef __cplusplus
-#define InvalidOid (Oid(0))
-#else
-#define InvalidOid ((Oid) 0)
-#endif
-
-#define OID_MAX UINT_MAX
-/* you will need to include <limits.h> to use the above #define */
-
-
-/*
- * NAMEDATALEN is the max length for system identifiers (e.g. table names,
- * attribute names, function names, etc). It must be a multiple of
- * sizeof(int) (typically 4).
- *
- * NOTE that databases with different NAMEDATALEN's cannot interoperate!
- */
-#define NAMEDATALEN 64
-
-
-/*
- * Identifiers of error message fields. Kept here to keep common
- * between frontend and backend, and also to export them to libpq
- * applications.
- */
-#define PG_DIAG_SEVERITY 'S'
-#define PG_DIAG_SQLSTATE 'C'
-#define PG_DIAG_MESSAGE_PRIMARY 'M'
-#define PG_DIAG_MESSAGE_DETAIL 'D'
-#define PG_DIAG_MESSAGE_HINT 'H'
-#define PG_DIAG_STATEMENT_POSITION 'P'
-#define PG_DIAG_INTERNAL_POSITION 'p'
-#define PG_DIAG_INTERNAL_QUERY 'q'
-#define PG_DIAG_CONTEXT 'W'
-#define PG_DIAG_SOURCE_FILE 'F'
-#define PG_DIAG_SOURCE_LINE 'L'
-#define PG_DIAG_SOURCE_FUNCTION 'R'
-
-#endif
diff --git a/db_demo/reset.c b/db_demo/reset.c
deleted file mode 100644
index 6fd92f7..0000000
--- a/db_demo/reset.c
+++ /dev/null
@@ -1,70 +0,0 @@
-#include <stdio.h>
-#include <time.h>
-
-#include "libpq-fe.h"
-#include "reset.h"
-
-int main(int argc, char *argv[]) {
- int i;
- int options;
- int day;
- int month;
- int year;
- PGconn *conn;
- char SQL_query[512];
- PGresult *res;
-
- day = month = year = 0;
-
- conn = PQsetdbLogin(PGHOST, PGPORT, PGOPTIONS, PGTTY, DBNAME, LOGIN, PWD);
-
- if(CONNECTION_BAD == PQstatus(conn)) {
- fprintf(stderr, "Connection to database '%s' failed.\n %s", DBNAME, PQerrorMessage(conn));
- PQfinish(conn);
- return 1;
- }
-
- if(0 < day) {
- for(i = 0; i <= day; i++) {
- sprintf(SQL_query, "UPDATE counter_day SET \"%0.2d\" = 0;", i);
- res = PQexec(conn, SQL_query);
-
- if((NULL == res) || (PGRES_COMMAND_OK != PQresultStatus(res))) {
- printf("Unable to execute query: %s\n %s\n", SQL_query, PQerrorMessage(conn));
- PQclear(res);
- PQfinish(conn);
- return 1;
- }
- PQclear(res);
- }
- }
- if(0 < month) {
- for(i = 1; i <= month; i++) {
- sprintf(SQL_query, "UPDATE counter_month SET \"%0.2d\" = 0;", i);
- res = PQexec(conn, SQL_query);
-
- if((NULL == res) || (PGRES_COMMAND_OK != PQresultStatus(res))) {
- printf("Unable to execute query: %s\n %s\n", SQL_query, PQerrorMessage(conn));
- PQclear(res);
- PQfinish(conn);
- return 1;
- }
- PQclear(res);
- }
- }
- if(1 == year) {
- sprintf(SQL_query, "INSERT INTO counter_year VALUES (counter_year.year +1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);");
- res = PQexec(conn, SQL_query);
-
- if((NULL == res) || (PGRES_COMMAND_OK != PQresultStatus(res))) {
- printf("Unable to execute query: %s\n %s\n", SQL_query, PQerrorMessage(conn));
- PQclear(res);
- PQfinish(conn);
- return 1;
- }
- PQclear(res);
- }
- PQfinish(conn);
-
- return 0;
-}
diff --git a/db_demo/reset.h b/db_demo/reset.h
deleted file mode 100644
index 51dd0e0..0000000
--- a/db_demo/reset.h
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * reset.h
- * House of Mozart statistic database reset app (c) 2003 oliver
- *
- * GNU public license *FIXME*
- */
-
-#define VERSION 0.0.1
-
-#define PGHOST "matchblox.schinagl.nl"
-#define PGPORT "5432"
-#define PGOPTIONS "\0"
-#define PGTTY "\0"
-#define DBNAME "2iv55"
-#define LOGIN "2iv55"
-#define PWD "uhtfiwwp"
-
-#define SHORTHELP \
-"Not enough or incorrect arguments try -h for help\n"
-
-#define LONGHELP \
-" -h\tprint this help message\n \
- -d\tReset daily stats\n \
- -m\tReset monthly stats\n \
- -a\tReset All stats (month day)\n"