File: pg_inter.cc

package info (click to toggle)
kaya 0.4.4-6.2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,200 kB
  • ctags: 2,015
  • sloc: cpp: 9,556; haskell: 7,253; sh: 3,060; yacc: 910; makefile: 816; perl: 90
file content (292 lines) | stat: -rw-r--r-- 6,647 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292

#include "pg_inter.h"
#include <VMState.h>
#include <cstdlib>
#include <KayaAPI.h>
#include <cstring>

void* pg_connect(wchar_t* rawconninfo)
{
  char* conninfo = wctostr(rawconninfo);
    PGconn* pc = PQconnectdb(conninfo);
    PGCon * pgc = new PGCon();
    pgc->con = pc;
    pgc->ok = 1;
    
//    cout << pc << "," << pgc << endl;
//    cout << PQstatus(pc) << endl;

    if (pc==NULL || PQstatus(pc) == CONNECTION_BAD)
    {
	pgc->ok = 0;
    }
    return (void*)pgc;
}

bool pg_ok(void* conn)
{
    PGCon* pgc = (PGCon*)conn;
//    cout << pgc->ok << endl;
    return pgc->ok;
}

wchar_t* pg_getError(void* conn)
{
    PGconn* pgc = ((PGCon*)conn)->con;
    return strtowc(PQerrorMessage(pgc));
}

DBtype getDBtype(wchar_t* rawval)
{
  char* val = wctostr(rawval);
    if (strncmp(val,"int",3)==0 && val[3]!='8') { // limit to 32 bit
	return DBINT;
    }
    else if (strncmp(val,"float",5)==0) {
	return DBFLOAT;
    }
    else if (strcmp(val,"bool")==0) {
	return DBBOOL;
    }
    else {
	return DBTEXT;
    }
}

// Work out what OIDs mean what types, so we can build the right structure
// kaya-side.
void pg_getTypeIDs(VMState* vm, PGCon* pgc)
{
    if (pgc->typids.size()!=0) {
	return; // Already done it
    }

    PGresult *res = PQexec(pgc->con,"SELECT oid,* FROM pg_type");
    int numrows = PQntuples(res);

    for(int i = 0; i<numrows; i++) {
	char* val = PQgetvalue(res,i,1); // Get type name
	int oid = atoi(PQgetvalue(res,i,0)); // Get oid
	pgc->typids[oid] = getDBtype(strtowc(val));
//	printf("%d: %s\n", oid, val);
    }
}

void* pg_exec(void* vmptr,void* conn,wchar_t* rawquery, KayaValue err)
{
  char* query = wctostr(rawquery);
    VMState* vm = (VMState*)vmptr;
    PGCon* pgc = (PGCon*)conn;

    pg_getTypeIDs(vm,pgc);

    PGresult *res = PQexec(pgc->con,query);
    if (res==NULL) {
	KayaSetString(err, KSTRING(PQerrorMessage(pgc->con)));
	return NULL;
    }
    if ((PQresultStatus(res)!=PGRES_COMMAND_OK)
	&& (PQresultStatus(res) != PGRES_TUPLES_OK)) {
	KayaSetString(err, KSTRING(PQresultErrorMessage(res)));
	return NULL;
    }
    
    int numrows = PQntuples(res);
    int numflds = PQnfields(res);
    
    KayaArray col_names = newKayaArray(numflds);
    for (int i = 0; i<numflds; i++)
	KayaArrayPush(col_names, KayaString(strtowc(PQfname(res,i))));

    KayaArray resarray = newKayaArray(numrows);

    for(int i = 0; i<numrows; i++) {
	KayaArray row = newKayaArray(numflds);
	for(int j = 0; j<numflds; j++) {
	    char* val = PQgetvalue(res,i,j);
	    KayaValue pv,fld;

            Oid ty = PQftype(res,j);

	    switch(pgc->typids[ty]) {
	    case DBINT:
	    /// If it's an int, make a DBInt(val)
		pv = KayaInt(atoi(val));
		fld = KayaUnion(1,1);
		KayaUnionSetArg(fld,0,pv);
		KayaArrayPush(row,fld);
		break;
	    case DBFLOAT:
	    /// If it's a float, make a DBFloat(val)
		pv = KayaFloat(atof(val));
		fld = KayaUnion(2,1);
		KayaUnionSetArg(fld,0,pv);
		KayaArrayPush(row,fld);
		break;
	    case DBBOOL:
            /// If it's a bool, make a DBBool(val)
		if (val[0]=='t') {
		    pv = KayaInt(1);
		} else {
		    pv = KayaInt(0);
		}
		fld = KayaUnion(4,1);
		KayaUnionSetArg(fld,0,pv);
		KayaArrayPush(row,fld);
		break;
	    case DBTIME:
		/// TODO: If it's a date, parse it and make a timestamp
	    default:
		/// Default is to make a DBText
		pv = KayaString(strtowc(val));
		fld = KayaUnion(0,1);
		KayaUnionSetArg(fld,0,pv);
		KayaArrayPush(row,fld);
	    }
	}
	KayaArrayPush(resarray,KayaArrayVal(row));
    }

    PGRes* pgr = new PGRes();
    pgr -> res_table = resarray;
    pgr -> rows = numrows;
    pgr -> cols = numflds;
    pgr -> col_names = col_names;
    PQclear(res);
    return pgr;
}

Array* pg_getstrs(void* res)
{
    return ((PGRes*)res)->res_table;
}

int pg_numrows(void* res)
{
    return ((PGRes*)res)->rows;
}

int pg_numcols(void* res)
{
    return ((PGRes*)res)->cols;
}

Array* pg_colnames(void* res)
{
    return ((PGRes*)res)->col_names;
}

void pg_close(void* conn)
{
    PQfinish(((PGCon*)conn)->con);
}

// not genuine prepared statements for now
void* do_pg_prepare(wchar_t* query) {
  return (void*)CSTRING(query);
}

void* pg_execp(void* vmptr,void* conn,void* queryptr, KayaValue err, KArray params) {
    char* query = (char*)queryptr;
    VMState* vm = (VMState*)vmptr;
    PGCon* pgc = (PGCon*)conn;

    pg_getTypeIDs(vm,pgc);

    int sz = KayaArraySize(params);
    char** cparams;
    if (sz == 0) {
      cparams = NULL;
    } else {
      cparams = (char**)malloc(sizeof(char*)*sz);
      for (int i=0;i<sz;i++) {
	KValue val = KayaArrayLookup(params,i);
	if (KayaUnionGetTag(val) == 0) {
	  cparams[i] = NULL;
	} else {
	  cparams[i] = CSTRING(KayaGetString(KayaUnionGetArg(val,0)));
	}
      }
    }

    PGresult *res = PQexecParams(pgc->con,query,
				 sz,// number of params
				 NULL, // untyped
				 cparams, // params
				 NULL, // text params
				 NULL, // text params
				 0
				 );
    if (cparams != NULL) {
      free(cparams);
    }

    if (res==NULL) {
	KayaSetString(err, KSTRING(PQerrorMessage(pgc->con)));
	return NULL;
    }
    if ((PQresultStatus(res)!=PGRES_COMMAND_OK)
	&& (PQresultStatus(res) != PGRES_TUPLES_OK)) {
	KayaSetString(err, KSTRING(PQresultErrorMessage(res)));
	return NULL;
    }
    
    int numrows = PQntuples(res);
    int numflds = PQnfields(res);
    
    KayaArray resarray = newKayaArray(numrows);

    for(int i = 0; i<numrows; i++) {
	KayaArray row = newKayaArray(numflds);
	for(int j = 0; j<numflds; j++) {
	    char* val = PQgetvalue(res,i,j);
	    KayaValue pv,fld;

            Oid ty = PQftype(res,j);

	    switch(pgc->typids[ty]) {
	    case DBINT:
	    /// If it's an int, make a DBInt(val)
		pv = KayaInt(atoi(val));
		fld = KayaUnion(1,1);
		KayaUnionSetArg(fld,0,pv);
		KayaArrayPush(row,fld);
		break;
	    case DBFLOAT:
	    /// If it's a float, make a DBFloat(val)
		pv = KayaFloat(atof(val));
		fld = KayaUnion(2,1);
		KayaUnionSetArg(fld,0,pv);
		KayaArrayPush(row,fld);
		break;
	    case DBBOOL:
            /// If it's a bool, make a DBBool(val)
		if (val[0]=='t') {
		    pv = KayaInt(1);
		} else {
		    pv = KayaInt(0);
		}
		fld = KayaUnion(4,1);
		KayaUnionSetArg(fld,0,pv);
		KayaArrayPush(row,fld);
		break;
	    case DBTIME:
		/// TODO: If it's a date, parse it and make a timestamp
	    default:
		/// Default is to make a DBText
		pv = KayaString(strtowc(val));
		fld = KayaUnion(0,1);
		KayaUnionSetArg(fld,0,pv);
		KayaArrayPush(row,fld);
	    }
	}
	KayaArrayPush(resarray,KayaArrayVal(row));
    }

    PGRes* pgr = new PGRes();
    pgr -> res_table = resarray;
    pgr -> rows = numrows;
    pgr -> cols = numflds;
    PQclear(res);
    return pgr;
}