commit da2af9256f02de6ae2bb854fe305e44dff909b56
parent b5ab4b9d068d96344f35d1b2ed3b3fc631ca69fa
Author: Tomas Hlavaty <tom@logand.com>
Date: Thu, 19 Apr 2012 00:37:18 +0200
dbquery-pg|mysql|sqlite proxies added
Diffstat:
A | Makefile | | | 18 | ++++++++++++++++++ |
A | dbquery-mysql.c | | | 68 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | dbquery-pg.c | | | 58 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | dbquery-sqlite.c | | | 58 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 files changed, 202 insertions(+), 0 deletions(-)
diff --git a/Makefile b/Makefile
@@ -0,0 +1,18 @@
+CFLAGS = -Os -W -Wall
+
+all:
+
+dbquery-pg: dbquery-pg.c
+ $(CC) $(CFLAGS) -o $@ $< -lpq
+ strip $@
+
+dbquery-mysql: dbquery-mysql.c
+ $(CC) $(CFLAGS) -o $@ $< `mysql_config --cflags --libs`
+ strip $@
+
+dbquery-sqlite: dbquery-sqlite.c
+ $(CC) $(CFLAGS) -o $@ $< -lsqlite3
+ strip $@
+
+clean:
+ rm -f dbquery-pg dbquery-mysql dbquery-sqlite
diff --git a/dbquery-mysql.c b/dbquery-mysql.c
@@ -0,0 +1,68 @@
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <mysql/mysql.h>
+
+static MYSQL *conn;
+
+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 void query(char *q) {
+ if(mysql_query(conn, q)) die(0);
+ MYSQL_RES *z = mysql_store_result(conn);
+ int i, j;
+ MYSQL_FIELD *f;
+ printf("((");
+ for(j = 0; (f = mysql_fetch_field(z)); j++) {
+ if(0 < j) printf(" ");
+ printf("\"%s\"", f->name);
+ }
+ printf(")\n (");
+ mysql_field_seek(z, 0);
+ for(j = 0; (f = mysql_fetch_field(z)); j++) {
+ if(0 < j) printf(" ");
+ printf("%d", f->type);
+ }
+ printf(")");
+ MYSQL_ROW r;
+ for(i = 0; (r = mysql_fetch_row(z)); i++) {
+ printf("\n (");
+ mysql_field_seek(z, 0);
+ for(j = 0; (f = mysql_fetch_field(z)); j++) {
+ if(0 < j) printf(" ");
+ char *v = r[j];
+ if(!v)
+ printf("NIL");
+ else
+ switch(f->type) {
+ case 246: // float
+ case 8: printf("%s", v); break; // int
+ default:
+ printf("\"%s\"", v);
+ }
+ }
+ printf(")");
+ }
+ printf(")\n");
+ mysql_free_result(z);
+}
+
+int main(int argc, char **argv) {
+ if(argc < 6) 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]);
+ return 0;
+}
diff --git a/dbquery-pg.c b/dbquery-pg.c
@@ -0,0 +1,58 @@
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <postgresql/libpq-fe.h>
+
+static PGconn *conn;
+
+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 void query(char *q) {
+ PGresult *z = PQexec(conn, q);
+ if(!z) die("error: query failed");
+ int i, j, n = PQntuples(z), m = PQnfields(z);
+ printf("((");
+ for(j = 0; j < m; j++) {
+ if(0 < j) printf(" ");
+ printf("\"%s\"", PQfname(z, j));
+ }
+ printf(")\n (");
+ for(j = 0; j < m; j++) {
+ if(0 < j) printf(" ");
+ printf("%d", PQftype(z, j));
+ }
+ printf(")");
+ for(i = 0; i < n; i++) {
+ printf("\n (");
+ for(j = 0; j < m; j++) {
+ if(0 < j) printf(" ");
+ if(PQgetisnull(z, i, j))
+ printf("NIL");
+ else
+ switch(PQftype(z, j)) {
+ case 1700: // float
+ case 23: printf("%s", PQgetvalue(z, i, j)); break; // int
+ default:
+ printf("\"%s\"", PQgetvalue(z, i, j));
+ }
+ }
+ printf(")");
+ }
+ printf(")\n");
+}
+
+int main(int argc, char **argv) {
+ if(argc < 3) die("usage: %s conninfo query", argv[0]);
+ if(!(conn = PQconnectdb(argv[1])) || (PQstatus(conn) != CONNECTION_OK))
+ die("error: connection failed");
+ query(argv[2]);
+ return 0;
+}
diff --git a/dbquery-sqlite.c b/dbquery-sqlite.c
@@ -0,0 +1,58 @@
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <sqlite3.h>
+
+static sqlite3 *conn;
+
+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 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);
+ printf("((");
+ for(j = 0; j < m; j++) {
+ if(0 < j) printf(" ");
+ printf("\"%s\"", sqlite3_column_name(z, j));
+ }
+ printf(")\n (");
+ for(j = 0; j < m; j++) {
+ if(0 < j) printf(" ");
+ printf("%d", sqlite3_column_type(z, j));
+ }
+ printf(")");
+ for(i = 0; SQLITE_ROW == sqlite3_step(z); i++) {
+ printf("\n (");
+ for(j = 0; j < m; j++) {
+ if(0 < j) printf(" ");
+ if(!sqlite3_column_text(z, j))
+ printf("NIL");
+ else
+ switch(sqlite3_column_type(z, j)) {
+ // case 246: // float
+ case 5: printf("%d", sqlite3_column_int(z, j)); break; // int
+ default:
+ printf("\"%s\"", sqlite3_column_text(z, j));
+ }
+ }
+ printf(")");
+ }
+ printf(")\n");
+ sqlite3_finalize(z);
+}
+
+int main(int argc, char **argv) {
+ if(argc < 3) die("usage: %s db query", argv[0]);
+ if(sqlite3_open(argv[1], &conn)) die("error: sqlite3_open failed");
+ query(argv[2]);
+ return 0;
+}