Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first commit

  • Loading branch information...
commit 29cf9c33d643aef88729b579996a851fd4a8ea31 0 parents
@oibore authored
6 README
@@ -0,0 +1,6 @@
+PostgreSQL pkg for golang
+
+$ cd ./src
+$ make
+$ ./runner
+
23 src/Makefile
@@ -0,0 +1,23 @@
+#
+#
+#
+
+include $(GOROOT)/src/Make.$(GOARCH)
+
+TARG=pg
+
+CGOFILES=pg.go
+CGO_LDFLAGS=pg_wrapper.o -lpq
+CLEANERFILES+=runner
+
+include $(GOROOT)/src/Make.pkg
+
+all: runner
+
+runner: pg_wrapper.o install runner.go
+ $(GC) runner.go
+ $(LD) -o $@ runner.$O
+
+pg_wrapper.o: pg_wrapper.c
+ gcc -fPIC -O2 -o pg_wrapper.o -c pg_wrapper.c
+
51 src/pg.go
@@ -0,0 +1,51 @@
+package pg
+
+// #include "pg_wrapper.h"
+import "C"
+
+import (
+ "unsafe";
+)
+
+func Connect(conninfo string) unsafe.Pointer {
+ conn := C.PgConnectDb(C.CString(conninfo));
+ return conn;
+}
+
+func Close(conn unsafe.Pointer) {
+ C.PgFinish(conn);
+}
+
+func Status(conn unsafe.Pointer) int {
+ status := C.PgStatus(conn);
+ return int(status);
+}
+
+func Exec(conn unsafe.Pointer, command string) unsafe.Pointer {
+ res := C.PgExec(conn, C.CString(command));
+ return res;
+}
+
+func GetResult(conn unsafe.Pointer) unsafe.Pointer {
+ return C.PgGetResult(conn);
+}
+
+func NFields(res unsafe.Pointer) int {
+ return int(C.PgNFields(res));
+}
+
+func NTuples(res unsafe.Pointer) int {
+ return int(C.PgNTuples(res));
+}
+
+func GetIsNull(res unsafe.Pointer, row_number int, column_number int) int {
+ return int(C.PgGetIsNull(res, _C_int(row_number),
+ _C_int(column_number)));
+}
+
+func GetValue(res unsafe.Pointer, row_number int, column_number int) string {
+ value := C.GoString(C.PgGetValue(res, _C_int(row_number),
+ _C_int(column_number)));
+ return value;
+}
+
48 src/pg_wrapper.c
@@ -0,0 +1,48 @@
+#include <libpq-fe.h>
+#include "pg_wrapper.h"
+
+void *PgConnectDb(const char *conninfo)
+{
+ PGconn *conn = PQconnectdb(conninfo);
+ return conn;
+}
+
+void PgFinish(void *conn)
+{
+ PQfinish(conn);
+}
+
+int PgStatus(void *conn)
+{
+ return PQstatus(conn);
+}
+
+void *PgExec(void *conn, const char *command)
+{
+ return PQexec(conn, command);
+}
+
+void *PgGetResult(void *conn)
+{
+ return PQgetResult(conn);
+}
+
+int PgNFields(void *res)
+{
+ return PQnfields(res);
+}
+
+int PgNTuples(void *res)
+{
+ return PQntuples(res);
+}
+
+int PgGetIsNull(void *res, int row_number, int column_number)
+{
+ return PQgetisnull(res, row_number, column_number);
+}
+
+char *PgGetValue(void *res, int row_number, int column_number)
+{
+ return PQgetvalue(res, row_number, column_number);
+}
15 src/pg_wrapper.h
@@ -0,0 +1,15 @@
+#ifndef __PG_WRAPPER_H__
+#define __PG_WRAPPER_H__
+
+void *PgConnectDb(const char* conninfo);
+void PgFinish(void *conn);
+int PgStatus(void *conn);
+void *PgExec(void *conn, const char *command);
+void *PgGetResult(void *conn);
+int PgNFields(void *res);
+int PgNTuples(void *res);
+int PgGetIsNull(void *res, int row_number, int column_number);
+char *PgGetValue(void *res, int row_number, int column_number);
+
+#endif /* __PG_WRAPPER_H__ */
+
25 src/runner.go
@@ -0,0 +1,25 @@
+package main
+
+import (
+ "fmt";
+ "pg";
+)
+
+func main() {
+ conninfo := "dbname=testdb";
+ conn := pg.Connect(conninfo);
+
+ status := pg.Status(conn);
+ fmt.Printf("conninfo=%s, status=%d\n", conninfo, status);
+
+ res := pg.Exec(conn, "select * from users;");
+ //res := pg.GetResult(conn);
+
+ fileds := pg.NFields(res);
+ fmt.Printf("fields = %d\n", fileds);
+
+ value := pg.GetValue(res, 0, 0);
+ fmt.Printf("value = %s\n", value);
+
+ pg.Close(conn);
+}
2  work/Makefile
@@ -0,0 +1,2 @@
+pgconn: pgconn.c
+ gcc -fPIC -O2 -o pgconn pgconn.c -lpq
BIN  work/pgconn
Binary file not shown
23 work/pgconn.c
@@ -0,0 +1,23 @@
+#include <libpq-fe.h>
+
+int main(void)
+{
+ char *feedback;
+
+ //char *conninfo = "host = localhost dbname = testdb";
+ char *conninfo = "dbname=testdb";
+ PGconn *conn = PQconnectdb(conninfo);
+ switch(PQstatus(conn))
+ {
+ case CONNECTION_OK:
+ feedback = "Connected to server...\n";
+ break;
+ case CONNECTION_BAD:
+ feedback = "Failed...\n";
+ break;
+ }
+
+ printf( "%s\n", feedback);
+ PQfinish(conn);
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.