-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlago_driver.c
More file actions
111 lines (93 loc) · 2.54 KB
/
lago_driver.c
File metadata and controls
111 lines (93 loc) · 2.54 KB
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
/*
Copyright (C) 2000-2005 Ulric Eriksson <ulric@siag.nu>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the Licence, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.
*/
#ifdef HAVE_LIBLAGO
/* Highly experimental! */
#include <stdio.h>
#include <stdlib.h>
#include <lago.h>
#include "common.h"
#include "sdb.h"
static int store_result(LRST resultset,
int (*callback)(int, char **, void *), void *closure)
{
int i, ncols;
char **p;
int row = 0;
/*printf("store_result()\n");
*/
if (resultset == 0) return -1;
ncols = Lgetncols(resultset);
/*printf("ncols = %d\n", ncols);
*/
if (ncols == 0) return 0;
p = sdb_malloc(ncols*sizeof *p);
while (Lfetch(resultset) == LFETCH_MORE) {
for (i = 1; i <= ncols; i++) {
p[i-1] = (char *)Lgetasstr(resultset, i);
}
(*callback)(ncols, p, closure);
row++;
}
sdb_free(p);
return row;
}
static int lago_driver(void *pdb, char *d, char *q,
int (*callback)(int, char **, void *), void *closure)
{
char *username = sdb_url_value(d, "uid");
char *password = sdb_url_value(d, "pwd");
char *database = sdb_url_value(d, "db");
char *port = sdb_url_value(d, "port");
char *host = sdb_url_value(d, "host");
int n;
LCTX context = Lnewctx();
LRST resultset;
int res;
n = Lconnect(context, host, port, database, username, password);
if (n) {
Ldelctx(context);
return -1;
}
resultset = Lquery(context, q);
if (resultset == 0) {
Ldelctx(context);
return -1;
}
if (Lgeterrcode(context)) {
fprintf(stderr, "Error: Native %05d SQLSTATE %s\n",
Lgeterrcode(context), Lgetsqlstate(context));
fprintf(stderr, " %s\n", Lgeterrmsg(context));
return -1;
}
res = store_result(resultset, callback, closure);
Ldelctx(context);
sdb_free(username);
sdb_free(database);
sdb_free(host);
sdb_free(password);
sdb_free(port);
return res;
}
void sdb_init_lago(void)
{
sdb_register_driver("lago", lago_driver, NULL, NULL);
}
#else
void sdb_init_lago(void)
{
;
}
#endif /* LAGO */