File: mysql.c

package info (click to toggle)
www-sql 0.5.7-20
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 652 kB
  • ctags: 769
  • sloc: ansic: 6,053; lex: 200; sh: 152; yacc: 151; makefile: 116; sql: 41
file content (449 lines) | stat: -rw-r--r-- 11,351 bytes parent folder | download
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
     WWW-SQL - parses HTML files and inserts information from MySQL databases
    Copyright (C) 1997  James Henstridge <james@daa.com.au>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
/*   mysql.c
 * contains all the database specific functions for www-sql.  At the moment,
 * this is the only such file, but in the future, there may be implementations
 * for other database systems.
 */

#include <mysql.h>

#include "cgi.h"
#include "cmds.h"

#define checkConn(f) if (conn == NULL) {\
                  fprintf(yyout, "<p>%s: no connection established</p>\n", f);\
                       return; }

/* Define the query info structure.  To enable multiple queries, we make
 * a linked list.
 */
typedef struct qh_struct {
  char *name;
  MYSQL_RES *res;
  MYSQL_ROW row;
  struct qh_struct *next;
} qHandle;

qHandle *firstQuery = NULL;
MYSQL *conn = NULL;

#define isNum(n) (n >= '0' && n <= '9')

/* These functions are helpers for print_loop */
int dbLoopSetup(char *name, void **data) {
  qHandle *qh;

  qh = firstQuery;
  while (qh != NULL) {
    if (!strcmp(qh->name, name)) break;
    qh = qh->next;
  }
  if (qh == NULL) {
    fprintf(yyout, "<p><b>print_loop</b>: couldn't find query handle %s</p>\n",
	    name);
    return -1;
  }
  *data = qh;
  mysql_data_seek(qh->res, 0);
  qh->row = mysql_fetch_row(qh->res);
  return (qh->row != NULL);
}

int dbLoopNext(void *data) {
  qHandle *qh = (qHandle *)data;

  qh->row = mysql_fetch_row(qh->res);
  return (qh->row != NULL);
}

/* This function is used as a hook to clean up any database related stuff
 * when www-sql closes */
void dbCleanUp() {
  if (conn != NULL)
    mysql_close(conn);
}

/* This function acts is used as a hook by substVars to do expansions of
 * query fields. */
char *substField(char *inp, int *toklen) {
  qHandle *qh;
  int len, len2, i;

  for (qh = firstQuery; qh != NULL; qh = qh->next)
    if (!strncmp(inp, qh->name, len = strlen(qh->name)) && inp[len] == '.') {
      len++;
      if (isNum(inp[len])) {
	i = atoi(&inp[len]);
	while (isNum(inp[len])) len++;
      } else {
	/* added a fix so that www-sql doesn't get it wrong if you have one
	 * variable that's name is a prefix of an other.
	 * Thanks Eduardo Trapani <etrapani@unesco.org.uy>
	 * I have implemented similar fixes in pgsql.c and cmds.c */
	int field = mysql_num_fields(qh->res), longest = 0; 
	for (i = 0; i < mysql_num_fields(qh->res); i++)
	  if (!strncmp(&inp[len], mysql_fetch_fields(qh->res)[i].name,
		       len2 = strlen(mysql_fetch_fields(qh->res)[i].name)) &&
	      len2 > longest) {
	    longest = len2;
	    field = i;
	  }
	len += longest;
	i = field;
      }
      if (i >= mysql_num_fields(qh->res) || qh->row == NULL) {
	*toklen = 0;
        return "Column out of range: ";
      }
      *toklen = len;
      if (qh->row[i] == NULL)
	return "";
      else
	return qh->row[i];
    }
  *toklen = 0;
  return NULL;
}

void cmdConnect(int argc, char *argv[]) {
  char *host = NULL, *user = SQL_USER, *pass = SQL_PASS;
  int h=0, u=0, p=0;

  if (conn != NULL)
    mysql_close(conn);
  if (argc > 2) {
    user = substVars(argv[1]);
    pass = substVars(argv[2]);
    u=1; p=1;
  } else if (argc > 1) {
    user = substVars(argv[1]);
    pass = NULL;
    u=1;
  }
  if (argc > 0) {
    host = substVars(argv[0]);
    h=1;
  }

  conn = xmalloc(sizeof(MYSQL));
  mysql_real_connect(conn, host, user, pass, 0, 0, 0, 0);
#ifdef mysql_errno
  if (mysql_errno(conn)) {
    fprintf(yyout, "<p>Connect: %s</p>\n", mysql_error(conn));
    mysql_close(conn);
    free(conn);
    conn = NULL;
  }
#endif
  if (h) free(host);
  if (u) free(user);
  if (p) free(pass);
}

void cmdClose(int argc, char *argv[]) {
  if (conn != NULL) {
    mysql_close(conn);
    free(conn);
    conn = NULL;
  }
}

void cmdDatabase(int argc, char *argv[]) {
  char *tmp;

  checkNumArgs(1,"database");
  checkConn("database");
  tmp = substVars(argv[0]);
  if (mysql_select_db(conn, tmp)) {
    fprintf(yyout, "<p>Database: %s</p>\n", mysql_error(conn));
  }
  free(tmp);
}

void cmdQuery(int argc, char *argv[]) {
  char *tmp, buf[21];
  qHandle *qh;
  MYSQL_RES *res;

  checkNumArgs(1,"query");
  checkConn("query");
  tmp = substVars(argv[0]);
  if (mysql_query(conn, tmp) < 0) {
    fprintf(yyout, "<p>Query failed: %s</p>\n", mysql_error(conn));
    free(tmp);
    setVar("AFFECTED_ROWS", "0");
    setVar("INSERT_ID", "-1");
    return;
  }
  free(tmp);
  sprintf(buf, "%ld", mysql_affected_rows(conn));
  setVar("AFFECTED_ROWS", buf);
  sprintf(buf, "%ld", mysql_insert_id(conn));
  setVar("INSERT_ID", buf);
  /* if the query has no body, don't set up query handle (ie for INSERT) */
  if ((res = mysql_store_result(conn)) == NULL) {
    return;
  }

  if (argc < 2) {
    mysql_free_result(res);
    fprintf(yyout, "<p>Query: query requires a handle name</p>\n");
    return;
  }
  qh = xmalloc(sizeof(qHandle));

  qh->name = xstrdup(argv[1]);
  qh->res = res;
  qh->row = mysql_fetch_row(qh->res);
  qh->next = firstQuery;
  firstQuery = qh;

  sprintf(buf, "%d", mysql_num_fields(qh->res));
  setVar("NUM_FIELDS", buf);
  sprintf(buf, "%ld", mysql_num_rows(qh->res));
  setVar("NUM_ROWS", buf);
}

void cmdFree(int argc, char *argv[]) {
  qHandle *qh, *prev, phony = {"", NULL, NULL, NULL};

  phony.next = firstQuery;
  checkNumArgs(1,"free");

  prev = &phony;
  qh = firstQuery;
  while (qh != NULL) {
    if (!strcasecmp(argv[0], qh->name)) {
      mysql_free_result(qh->res);
      free(qh->name);
      prev->next = qh->next;
      free(qh);
      qh = prev->next;
    } else {
      prev = qh;
      qh = qh->next;
    }
  }
  firstQuery = phony.next;
}

void cmdPrintRows(int argc, char *argv[]) {
  qHandle *qh;
  char *tmp;

  checkNumArgs(2,"print_rows");
  checkConn("print_rows");

  qh = firstQuery;
  while (qh != NULL) {
    if (!strcmp(qh->name, argv[0])) break;
    qh = qh->next;
  }
  if (qh == NULL) {
    fprintf(yyout, "<p>print_rows: query handle %s not found</p>\n", argv[0]);
    return;
  }

  while (qh->row) {
    tmp = substVars(argv[1]);
    fprintf(yyout, "%s", tmp);
    free(tmp);
    qh->row = mysql_fetch_row(qh->res);
  }
}

void cmdFetch(int argc, char *argv[]) {
  qHandle *qh;

  checkNumArgs(1,"fetch");
  checkConn("fetch");

  qh = firstQuery;
  while (qh != NULL) {
    if (!strcmp(qh->name, argv[0])) break;
    qh = qh->next;
  }
  if (qh == NULL) {
    fprintf(yyout, "<p>fetch: query handle not found\n</p>");
    return;
  }

  qh->row = mysql_fetch_row(qh->res);
}

void cmdSeek(int argc, char *argv[]) {
  qHandle *qh;

  checkNumArgs(2,"seek");
  checkConn("seek");

  qh = firstQuery;
  while (qh != NULL) {
    if (!strcmp(qh->name, argv[0])) break;
    qh = qh->next;
  }
  if (qh == NULL) {
    fprintf(yyout, "<p>seek: query handle not found</p>\n");
    return;
  }

  mysql_data_seek(qh->res, atoi(argv[1]));
  qh->row = mysql_fetch_row(qh->res);
}

#ifndef IS_NUM
/* some peoples mysql header files don't seem to define this... */
# define IS_NUM(t) ((t) <= FIELD_TYPE_INT24)
#endif

/* This command was contributed by Martin Maisey <M.J.Maisey@webname.com>
 * (with slight modifications by me (James). */
void cmdQtable(int argc, char *argv[]) {
  qHandle *qh;
  unsigned int i;

  checkNumArgs(1,"qtable");
  checkConn("qtable");

  qh = firstQuery;
  while (qh != NULL) {
    if (!strcmp(qh->name, argv[0])) break;
    qh = qh->next;
  }
  if (qh == NULL) {
    fprintf(yyout, "<p>qtable: query handle not found</p>\n");
    return;
  }

  if (argc > 1 && !strcasecmp(argv[1], "borders"))
    fprintf(yyout, "<table border>\n");
  else
    fprintf(yyout, "<table>\n");
  /* Output field names */
  fprintf(yyout, "<tr>");
  for (i = 0; i < mysql_num_fields(qh->res); i++)
    fprintf(yyout, "<th>%s</th> ", mysql_fetch_fields(qh->res)[i].name);
  fprintf(yyout, "</tr>\n");
  mysql_data_seek(qh->res, 0);
  qh->row = mysql_fetch_row(qh->res);

  /* Output rows */
  while (qh->row) {
    fprintf(yyout, "<tr>");
    for (i = 0; i < mysql_num_fields(qh->res); i++)
      if (IS_NUM(mysql_fetch_fields(qh->res)[i].type)) /*right align numbers*/
        fprintf(yyout, "<td align=right>%s</td> ", qh->row[i]);
      else
        fprintf(yyout, "<td>%s</td> ", qh->row[i]);
    fprintf(yyout, "</tr>\n");
    qh->row = mysql_fetch_row(qh->res);
  }
  fprintf(yyout, "</table>\n");
}

/* This command was contributed by Martin Maisey <M.J.Maisey@webname.com> */
void cmdQlongform(int argc, char *argv[]) {
  qHandle *qh;
  unsigned int i;

  checkNumArgs(1,"qlongform");
  checkConn("qlongform");

  qh = firstQuery;
  while (qh != NULL) {
    if (!strcmp(qh->name, argv[0])) break;
    qh = qh->next;
  }
  if (qh == NULL) {
    fprintf(yyout, "<p>qlongform: query handle not found</p>\n");
    return;
  }

  /* Move to first row */
  mysql_data_seek(qh->res, 0);
  qh->row = mysql_fetch_row(qh->res);

  /* Output rows */
  while (qh->row) {
    for (i = 0; i < mysql_num_fields(qh->res); i++)
      fprintf(yyout, "<b>%s:</b> %s<br>\n",mysql_fetch_fields(qh->res)[i].name,
                      qh->row[i]);
    fprintf(yyout, "<p>\n");
    qh->row = mysql_fetch_row(qh->res);
  }
}

/* print a <select> style list box from a query.  For use in forms.
   Requires that the first column contain values for the form variable,
   and the second have labels for the list. <james@daa.com.au> */
void cmdQselect(int argc, char *argv[]) {
  qHandle *qh;
  int defaultGiven = 0;
  char *default_v = NULL;

  checkNumArgs(2,"qselect");
  checkConn("qselect");

  qh = firstQuery;
  while (qh != NULL) {
    if (!strcmp(qh->name, argv[0])) break;
    qh = qh->next;
  }
  if (qh == NULL) {
    fprintf(yyout, "<p>qselect: query handle not found</p>\n");
    return;
  }
  if (mysql_num_fields(qh->res) < 2) {
    fprintf(yyout, "<p>qselect: not enough fields in table</p>\n");
    return;
  }
  if (argc >= 3) {
    defaultGiven = 1;
    default_v = argv[2];
  }
  mysql_data_seek(qh->res, 0);
  qh->row = mysql_fetch_row(qh->res);
  fprintf(yyout, "<select name=\"%s\">\n", argv[1]);
  while (qh->row) {
    if (defaultGiven && !strcmp(qh->row[0], default_v))
      fprintf(yyout, "<option value=\"%s\" selected>%s\n", qh->row[0],
	      qh->row[1]);
    else
      fprintf(yyout, "<option value=\"%s\">%s\n", qh->row[0], qh->row[1]);
    qh->row = mysql_fetch_row(qh->res);
  }
  fprintf(yyout, "</select>");
}

commands db_funcs = {
  {"connect",    cmdConnect},
  {"close",      cmdClose},
  {"database",   cmdDatabase},
  {"query",      cmdQuery},
  {"free",       cmdFree},
  {"print_rows", cmdPrintRows},
  {"fetch",      cmdFetch},
  {"seek",       cmdSeek},
  {"qtable",     cmdQtable},
  {"qlongform",  cmdQlongform},
  {"qselect",    cmdQselect},
  {NULL, NULL}
};