Debian Codegraph @

ipl/packs/icondb/mysqldb.c

raw
1
2 /*-----------------3/27/2007 11:23AM-----------------
3 * loadable C function mysqldb for icon access to
4 * a mySQL database from linux, by Carl Sturtivant.
5 * (This also built on solaris.)
6 *
7 * This should be Garbage Collection safe except
8 * under very extreme memory shortages.
9 *
10 * Requires a mySQL installation to build.
11 * I used the following from bash:
12
13 CFG=/usr/bin/mysql_config
14 sh -c "gcc -shared -o mysqldb.so -fPIC `$CFG --cflags` mysqldb.c `$CFG --libs`"
15
16 * for details about calling mysqldb, see below.
17 * --------------------------------------------------*/
18
19 #include <stdio.h>
20 #include <string.h>
21
22 /* http://dev.mysql.com/doc/refman/5.0/en/c.html */
23 /* #include "/usr/include/mysql/mysql.h" */
24 #include <mysql.h>
25
26
27 #include "icall.h"
28
29
30 /* macros obtained by modifying some from icall.h */
31
32 #define Mkinteger(i, dp) \
33 do { (dp)->dword = D_Integer; (dp)->vword = (i); } while(0)
34
35 #define Mkstring(s, dp) \
36 do { word n = strlen(s); \
37 (dp)->dword = n; (dp)->vword = (word)alcstr(s,n); } while(0)
38
39 /* ensure that return to icon removes our tended descriptors from the list */
40 #define ReturnDescriptor(d) do { gcu_aware_pop(); return ( argv[0] = (d), 0 ); } while(0)
41 #define ReturnError(d, n) do { gcu_aware_pop(); return ( argv[0] = (d), n ); } while(0)
42
43
44 /****************start of Garbage Collection Utilities****************/
45
46 /* Structure for chaining descriptors to be tended properly by GC (rstructs.h) */
47 struct tend_desc {
48 struct tend_desc *previous;
49 int num;
50 descriptor d[1]; /* actual size is in num */
51 };
52 typedef struct tend_desc gcu_tended;
53
54 /* global chain of such structures used by iconx (rinit.r) */
55 extern gcu_tended *tend;
56
57 /* int parameter to pass to gcu_initialize */
58 #define gcu_max(vars) ( (sizeof(vars) - sizeof(gcu_tended) )/sizeof(descriptor) )
59
60 /* initialize all descriptors to &null and assign the number */
61 static void gcu_initialize(int maxindex, void *descriptors) {
62 int i;
63 gcu_tended *desc = (gcu_tended *)descriptors;
64 desc->num = maxindex+1;
65 for( i = 0; i <= maxindex; ++i ) (desc->d)[i] = nulldesc;
66 }
67
68 /* add descriptors in a gcu_tended structure to the tended list */
69 static void gcu_aware_push(void *descriptors) {
70 gcu_tended *desc = (gcu_tended *)descriptors;
71 desc->previous = tend;
72 tend = descriptors;
73 }
74
75 /* remove descriptors in a gcu_tended structure from the tended list */
76 static void gcu_aware_pop() {
77 tend = tend->previous;
78 }
79
80 /****************end of Garbage Collection utilities****************/
81
82
83 /****************start of list utilities****************/
84
85 int Zlist(descriptor argv[]); /* resolved in iconx: icon function list(i,X):L */
86 int Osubsc(descriptor argv[]); /* resolved in iconx: icon operator L[i]:v */
87 int Oasgn(descriptor argv[]); /* resolved in iconx: icon operator v:=X */
88
89 typedef int (*iconfunction)(descriptor argv[]);
90
91 /* safely call an icon built-in function or operator with two arguments from C. */
92 static descriptor iconcall2(iconfunction F, descriptor x1, descriptor x2) {
93 struct { /* structure like struct tend_desc with extra descriptors at the bottom */
94 gcu_tended other; /* vital: used to chain onto the tend list */
95 descriptor stack[3]; /* GC is aware of these once this struct is pushed onto the tend list */
96 } tended;
97 gcu_initialize( gcu_max(tended), &tended ); /* vital: call before icon may be made aware of this */
98 gcu_aware_push( &tended ); /* GC is now aware of tended.stack */
99 tended.stack[0] = nulldesc;
100 tended.stack[1] = x1;
101 tended.stack[2] = x2;
102 F(tended.stack); /* No error handling for the uses below */
103 gcu_aware_pop(); /* vital: GC is now unaware of tended.stack */
104 return tended.stack[0];
105 }
106
107 /* returns list(n, &null) --- allocates memory */
108 static descriptor newlist(int length) {
109 descriptor len;
110 Mkinteger(length, &len);
111 return iconcall2( &Zlist, len, nulldesc );
112 }
113
114 /* returns list[index] := value */
115 static descriptor assign(descriptor list, int index, descriptor value) {
116 descriptor i;
117 Mkinteger(index, &i);
118 return iconcall2( &Oasgn, iconcall2(&Osubsc, list, i), value );
119 }
120
121 /* returns .list[index] */
122 static descriptor subscript(descriptor list, int index) {
123 descriptor i, result;
124 Mkinteger(index, &i);
125 result = iconcall2(&Osubsc, list, i);
126 /* result of an icon subscripting operation is a variable */
127 deref(&result, &result); /* deref resolved in iconx */
128 return result;
129 }
130
131 /****************end of list utilities****************/
132
133
134 /* make icon list of mysql error information */
135 static descriptor error_info(int mysqlNumber, const char * mysqlError) {
136 descriptor number;
137 struct {
138 gcu_tended other;
139 descriptor text, ls;
140 } tended;
141 gcu_initialize( gcu_max(tended), &tended );
142 gcu_aware_push( &tended );
143 tended.ls = newlist(2);
144 Mkinteger(mysqlNumber, &number);
145 Mkstring((char *)mysqlError, &tended.text);
146 assign( tended.ls, 1, number );
147 assign( tended.ls, 2, tended.text );
148 gcu_aware_pop();
149 return tended.ls;
150 }
151
152 /* make mySQL row retrieved from query results into icon list */
153 static descriptor convertrow(MYSQL_ROW row, int numfields) {
154 int i;
155 struct {
156 gcu_tended other;
157 descriptor x, ls;
158 } tended;
159 gcu_initialize( gcu_max(tended), &tended );
160 gcu_aware_push( &tended );
161 tended.ls = newlist(numfields);
162 for( i = 1; i <= numfields; ++i ) {
163 if( row[i-1] ) Mkstring( row[i-1], &tended.x );
164 else tended.x = nulldesc;
165 assign( tended.ls, i, tended.x );
166 }
167 gcu_aware_pop();
168 return tended.ls;
169 }
170
171 /*--------------------------------------------------
172 * Called with a list, mysqldb attempts to connect.
173 * Only one database can be connected to at a time.
174 * Needs the database name, username, password,
175 * and optionally the host, and if so optionally
176 * the port number, all passed in a list. The host
177 * defaults to localhost, and the port number to
178 * the default port number for mySQL.
179 *
180 * Called with a string, mysqldb attempts to
181 * execute that string as a mySQL query.
182 *
183 * Called with no parameters, mysqldb closes
184 * the connection if it is open.
185 *
186 * Returns a list of lists for a SELECT query, or
187 * the number of rows affected for other queries.
188 * Otherwise, fails if everything works, returns
189 * error information if not, except if incorrect
190 * argument types are supplied, in which case the
191 * result is an error.
192 * --------------------------------------------------*/
193 int mysqldb(int argc, descriptor argv[]) {
194 static MYSQL dbh; /* connection sticks around between calls */
195 static int connected = 0;
196
197 MYSQL_RES *result;
198 MYSQL_ROW row;
199 char *querystring, *hoststring,
200 *databasestring, *userstring, *passwordstring;
201 int i, len, rowsize, portnum;
202 struct {
203 gcu_tended other;
204 descriptor ls, host, port, database, user, password, answer;
205 } tended;
206 gcu_initialize( gcu_max(tended), &tended );
207 gcu_aware_push( &tended );
208
209
210 if( argc == 0 ) { /* close connection */
211 if( connected ) mysql_close(&dbh);
212 connected = 0;
213 gcu_aware_pop();
214 Fail;
215 } /* end close connection */
216
217 if( argc >= 1 && IconType(argv[1]) == 'L' ) { /* connect to MySQL */
218 if( connected )
219 ReturnDescriptor( error_info(-1, "mysqldb: already connected") );
220 if( !mysql_init(&dbh) )
221 ReturnDescriptor( error_info(-1, "mysqldb: cannot initialize mySQL!") );
222
223 tended.ls = argv[1];
224 hoststring = "localhost"; /* host defaults to localhost */
225 portnum = 0; /* port defaults to 0 giving the mySQL default */
226
227 switch( ListLen(tended.ls) ) {
228 default:
229 ReturnDescriptor( error_info(-1, "mysqldb: list of dbname, user, pwd, [host, [port]] expected") );
230 case 5 :
231 tended.port = subscript(tended.ls, 5);
232 if( !cnv_int(&tended.port,&tended.port) ) ReturnError(tended.port,101);
233 portnum = IntegerVal(tended.port);
234 case 4 :
235 tended.host = subscript(tended.ls, 4);
236 if ( !cnv_str(&tended.host,&tended.host) ) ReturnError(tended.host,103);
237 hoststring = StringVal(tended.host);
238 case 3 :
239 tended.password = subscript(tended.ls, 3);
240 if ( !cnv_str(&tended.password,&tended.password) ) ReturnError(tended.password,103);
241 passwordstring = StringVal(tended.password);
242 tended.user = subscript(tended.ls, 2);
243 if ( !cnv_str(&tended.user,&tended.user) ) ReturnError(tended.user,103);
244 userstring = StringVal(tended.user);
245 tended.database = subscript(tended.ls, 1);
246 if ( !cnv_str(&tended.database,&tended.database) ) ReturnError(tended.database,103);
247 databasestring = StringVal(tended.database);
248 }
249
250 if( mysql_real_connect(&dbh, hoststring, userstring,
251 passwordstring, databasestring, portnum, NULL, 0) ) {
252 connected = 1;
253 gcu_aware_pop();
254 Fail;
255 }
256 else ReturnDescriptor( error_info(mysql_errno(&dbh), mysql_error(&dbh)) );
257 } /* end connect to MySQL */
258
259 if( argc >= 1 && IconType(argv[1]) == 's' ) { /* execute a query */
260 if( !connected )
261 ReturnDescriptor( error_info(-1, "mysqldb: not connected") );
262 querystring = StringVal(argv[1]);
263
264 if( mysql_query(&dbh, querystring) )
265 ReturnDescriptor( error_info(mysql_errno(&dbh), mysql_error(&dbh)) );
266
267 result = mysql_store_result(&dbh);
268
269 if( !result ) /* not a SELECT query or some sort of error */
270 if( mysql_field_count(&dbh) != 0 )
271 ReturnDescriptor( error_info(mysql_errno(&dbh), mysql_error(&dbh)) );
272 else { /* not a SELECT query */
273 gcu_aware_pop();
274 RetInteger( mysql_affected_rows(&dbh) );
275 }
276
277 /* SELECT query */
278 tended.answer = newlist( mysql_num_rows(result) );
279 rowsize = mysql_num_fields(result);
280 i = 0;
281 while( row = mysql_fetch_row(result) )
282 assign( tended.answer, ++i, convertrow(row, rowsize) );
283 mysql_free_result(result);
284 ReturnDescriptor(tended.answer);
285 } /* end execute a query */
286
287 /* wrong argument type to mysqldb */
288 ReturnError(argv[1], 110); /* list or string expected */
289 }