dbquery

Query RDBMS and return S-expression
git clone https://logand.com/git/dbquery.git/
Log | Files | Refs | README

commit a2f992a470f95afc95f34d8dc6748ffbfed9bc9f
parent 40f603fbe241e0f546ebe92051e64a3a18901b12
Author: Tomas Hlavaty <tom@logand.com>
Date:   Tue,  5 Mar 2013 21:39:18 +0100

add trivial repl to dbquery-*.c, now possible to reuse db connection

Diffstat:
MMakefile | 16++++++++++------
Mdbquery-mysql.c | 22+++++++---------------
Mdbquery-pg.c | 24++++++++----------------
Mdbquery-sqlite.c | 22+++++++---------------
4 files changed, 32 insertions(+), 52 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,17 +1,21 @@ CFLAGS = -Os -W -Wall all: + echo "choose a backend driver: dbquery-pg|mysql|sqlite" -dbquery-pg: dbquery-pg.c - $(CC) $(CFLAGS) -o $@ $< -lpq +.c.o: + $(CC) $(CFLAGS) -c -o $@ $< + +dbquery-pg: dbquery-pg.c common.o + $(CC) $(CFLAGS) -o $@ $< common.o -lpq strip $@ -dbquery-mysql: dbquery-mysql.c - $(CC) $(CFLAGS) -o $@ $< `mysql_config --cflags --libs` +dbquery-mysql: dbquery-mysql.c common.o + $(CC) $(CFLAGS) -o $@ $< common.o `mysql_config --cflags --libs` strip $@ -dbquery-sqlite: dbquery-sqlite.c - $(CC) $(CFLAGS) -o $@ $< -lsqlite3 +dbquery-sqlite: dbquery-sqlite.c common.o + $(CC) $(CFLAGS) -o $@ $< common.o -lsqlite3 strip $@ clean: diff --git a/dbquery-mysql.c b/dbquery-mysql.c @@ -4,21 +4,12 @@ #include <stdarg.h> #include <mysql/mysql.h> -static MYSQL *conn; +void die(const char *format, ...); +void repl(); -static void die(const char *format, ...) { - va_list argv; - va_start(argv, format); - if(format) - vfprintf(stderr, format, argv); - else - fprintf(stderr, "error %u: %s", mysql_errno(conn), mysql_error(conn)); - va_end(argv); - fprintf(stderr, "\n"); - exit(-1); -} +static MYSQL *conn; -static void query(char *q) { +void query(char *q) { if(mysql_query(conn, q)) die(0); MYSQL_RES *z = mysql_store_result(conn); int i, j; @@ -55,14 +46,15 @@ static void query(char *q) { printf(")"); } printf(")\n"); + fflush(stdout); mysql_free_result(z); } int main(int argc, char **argv) { - if(argc < 6) die("usage: %s host user password db query", argv[0]); + if(argc < 5) die("usage: %s host user password db [query]", argv[0]); if(!(conn = mysql_init(NULL))) die(0); if(!mysql_real_connect(conn, argv[1], argv[2], argv[3], argv[4], 0, NULL, 0)) die(0); - query(argv[5]); + if(argc < 6) repl(); else query(argv[5]); return 0; } diff --git a/dbquery-pg.c b/dbquery-pg.c @@ -1,21 +1,12 @@ -#include <stdlib.h> -#include <string.h> #include <stdio.h> -#include <stdarg.h> -#include <postgresql/libpq-fe.h> +#include <libpq-fe.h> -static PGconn *conn; +void die(const char *format, ...); +void repl(); -static void die(const char *format, ...) { - va_list argv; - va_start(argv, format); - vfprintf(stderr, format, argv); - va_end(argv); - fprintf(stderr, "\n"); - exit(-1); -} +static PGconn *conn; -static void query(char *q) { +void query(char *q) { PGresult *z = PQexec(conn, q); if(!z) die("error: query failed"); int i, j, n = PQntuples(z), m = PQnfields(z); @@ -47,12 +38,13 @@ static void query(char *q) { printf(")"); } printf(")\n"); + fflush(stdout); } int main(int argc, char **argv) { - if(argc < 3) die("usage: %s conninfo query", argv[0]); + if(argc < 2) die("usage: %s conninfo [query]", argv[0]); if(!(conn = PQconnectdb(argv[1])) || (PQstatus(conn) != CONNECTION_OK)) die("error: connection failed"); - query(argv[2]); + if(argc < 3) repl(); else query(argv[2]); return 0; } diff --git a/dbquery-sqlite.c b/dbquery-sqlite.c @@ -1,21 +1,12 @@ -#include <stdlib.h> -#include <string.h> #include <stdio.h> -#include <stdarg.h> #include <sqlite3.h> -static sqlite3 *conn; +void die(const char *format, ...); +void repl(); -static void die(const char *format, ...) { - va_list argv; - va_start(argv, format); - vfprintf(stderr, format, argv); - va_end(argv); - fprintf(stderr, "\n"); - exit(-1); -} +static sqlite3 *conn; -static void query(char *q) { +void query(char *q) { sqlite3_stmt *z; if(sqlite3_prepare_v2(conn, q, -1, &z, 0)) die("error: sqlite3_prepare_v2 failed"); int i, j, m = sqlite3_column_count(z); @@ -47,12 +38,13 @@ static void query(char *q) { printf(")"); } printf(")\n"); + fflush(stdout); sqlite3_finalize(z); } int main(int argc, char **argv) { - if(argc < 3) die("usage: %s db query", argv[0]); + if(argc < 2) die("usage: %s db [query]", argv[0]); if(sqlite3_open(argv[1], &conn)) die("error: sqlite3_open failed"); - query(argv[2]); + if(argc < 3) repl(); else query(argv[2]); return 0; }