stuck with a bitch of a problem
[goDBhammer.git] / src / dbibridge.go
1 package dbibridge
2
3 // #include <stdio.h>
4 // #include <dbi/dbi.h>
5 import "C"
6
7 import (
8         "unsafe";
9         "fmt";
10 )
11
12 type DBIConnection struct {
13         dbconn unsafe.Pointer;
14         dbinit bool;
15 }
16
17 func DBICreate()(connection *DBIConnection)
18 {
19         var dbCon *DBIConnection;
20         
21         dbCon = new(DBIConnection);
22         dbCon.dbinit = false;
23         
24         return dbCon;
25 }
26
27 //func (thisDBConn *DBIConnection)DBIConnect(dbtype string, host string, username string, password string, database string)
28 // for some reason, the above doesnt work how i'd expect..
29 func DBIConnect(thisDBConn *DBIConnection, dbtype string, host string, username string, password string, database string)
30 {
31         C.dbi_initialize(nil);
32         
33         thisDBConn.dbconn = (unsafe.Pointer)(C.dbi_conn_new(C.CString(dbtype)));
34         
35         C.dbi_conn_set_option(thisDBConn.dbconn, C.CString("host"), C.CString(host));
36         C.dbi_conn_set_option(thisDBConn.dbconn, C.CString("username"), C.CString(username));
37         C.dbi_conn_set_option(thisDBConn.dbconn, C.CString("password"), C.CString(password));
38         C.dbi_conn_set_option(thisDBConn.dbconn, C.CString("dbname"), C.CString(database));
39         C.dbi_conn_connect(thisDBConn.dbconn);
40         
41         thisDBConn.dbinit = true;
42 }
43
44 func ExecSQL(thisDBConn *DBIConnection, SQL string)
45 {
46         fmt.Printf("%s;\n", SQL);
47         res := C.dbi_conn_query(thisDBConn.dbconn, C.CString(SQL));
48         
49         C.dbi_result_free((unsafe.Pointer)(res));
50 }
51
52 func DBIDisconnect(thisDBConn *DBIConnection)
53 {
54         C.dbi_conn_close(thisDBConn.dbconn);
55 }