aboutsummaryrefslogtreecommitdiff
path: root/src/gsql_conn.c
blob: cdd2dae338e3a3cde7a2ec3b307315ac1f42adf1 (plain)
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
/* This file is part of guile-sql.
   Copyright (C) 2002 Sergey Poznyakoff

   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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <string.h>
#include <guile-sql.h>
#include <app.h>

int num_iface;
struct sql_iface sql_iftab[MAX_IFACES];

long sql_connect_tag;

/* SMOB functions: */
static SCM
sql_connect_mark (SCM connect_smob)
{
	struct sql_connect *conn = (struct sql_connect *)SCM_CDR(connect_smob);
	return sql_iftab[conn->iface].mark(conn);
}

static scm_sizet
sql_connect_free (SCM connect_smob)
{
	scm_sizet size = sizeof(struct sql_connect);
	struct sql_connect *conn = (struct sql_connect *)SCM_CDR(connect_smob);
	size += sql_iftab[conn->iface].free(conn);
	if (conn->hostname)
		free(conn->hostname);
	if (conn->username)
		free(conn->username);
	if (conn->database)
		free(conn->database);
	free(conn);
	return size;
}

static int
sql_connect_print (SCM connect_smob, SCM port, scm_print_state * pstate)
{
	struct sql_connect *conn = (struct sql_connect *)SCM_CDR(connect_smob);
	scm_puts("#<SQL connection (", port);
	scm_puts(sql_iftab[conn->iface].name, port);
	scm_puts(")", port);
	if (!conn->data)
		scm_puts("not connected", port);
	else {
		scm_puts(conn->username, port);
		scm_puts("@", port);
		scm_puts(conn->hostname, port);
		scm_puts(":", port);
		scm_intprint(conn->port, 10, port);
		scm_puts(" ", port);
		scm_puts(conn->database, port);
	};
	scm_puts (">", port);
	return 1;
}

int
sql_find_iface(char *name)
{	
	int iface;
	
	for (iface = 0; iface < num_iface; iface++) 
		if (strcmp(sql_iftab[iface].name, name) == 0)
			return iface;
	return -1;
}

SCM
sql_connect_create (char *name)
{
	struct sql_connect *conn;
	int iface = sql_find_iface(name);
	if (iface < 0) 
		scm_misc_error("sql_connect_create",
			       "Unknown SQL interface ~S",
			       SCM_LIST1(scm_makfrom0str(name)));
	
	conn = scm_must_malloc (sizeof (*conn), "sql_connect");
	memset(conn, 0, sizeof *conn);
	conn->iface = iface;
	SCM_RETURN_NEWSMOB (sql_connect_tag, conn);
}

int
scm_is_sql_connect (SCM scm)
{
	return SCM_NIMP (scm) && SCM_CAR (scm) == sql_connect_tag;
}

/* Interface */

SCM_DEFINE (sql_connect, "sql-connect", 5, 1, 0,
	    (SCM IFACE, SCM HOST, SCM PORT, SCM DB, SCM USER, SCM PASS),
	    "Connect to a database.")
#define FUNC_NAME s_sql_connect
{
	SCM smob;
	char *hostname;
	int port;
	char *dbname;
	char *user;
	char *pass;
	int iface;

	if (SCM_IMP(IFACE) && SCM_INUMP(IFACE)) 
		iface = SCM_INUM(IFACE);
	else if (SCM_STRINGP(IFACE)) 
		iface = sql_find_iface(SCM_CHARS(IFACE));
	else {
		SCM_ASSERT(IFACE == SCM_BOOL_T || IFACE == SCM_BOOL_F,
			   IFACE, SCM_ARG1, FUNC_NAME);
		iface = 0;
	}
	if (iface < 0 || iface >= num_iface) 
		scm_misc_error(FUNC_NAME,
			       "Argument ~S (~S) out of range",
			       SCM_LIST2(SCM_MAKINUM(1),
					 IFACE));
	
	SCM_ASSERT(SCM_STRINGP(HOST), HOST, SCM_ARG1, FUNC_NAME);
	hostname = SCM_ROCHARS(HOST);

	SCM_ASSERT(SCM_IMP(PORT) && SCM_INUMP(PORT),
		   PORT, SCM_ARG2, FUNC_NAME);
	port = SCM_INUM(PORT);
	
	SCM_ASSERT(SCM_STRINGP(DB), DB, SCM_ARG3, FUNC_NAME);
	dbname = SCM_ROCHARS(DB);

	SCM_ASSERT(SCM_STRINGP(USER), USER, SCM_ARG4, FUNC_NAME);
	user = SCM_ROCHARS(USER);

	if (SCM_UNBNDP(PASS))
		pass = NULL;
	else if (SCM_STRINGP(USER))
		pass = SCM_ROCHARS(PASS);
	
	smob = sql_iftab[iface].connect(hostname, port,
					dbname, user, pass,
					FUNC_NAME);
	if (smob != SCM_BOOL_F) {
		struct sql_connect *conn = (struct sql_connect *)SCM_CDR(smob);
		conn->hostname = strdup(hostname);
		conn->port = port;
		conn->username = strdup(user);
		conn->database = strdup(dbname);
	}
	return smob;
}
#undef FUNC_NAME

SCM_DEFINE (sql_connect_close, "sql-connect-close", 1, 0, 0,
	    (SCM CONN),
	    "Close connection to a database.")
#define FUNC_NAME s_sql_connect_close
{
	struct sql_connect *conn;
	SCM_ASSERT(scm_is_sql_connect(CONN), CONN, SCM_ARG1, FUNC_NAME);
	conn = (struct sql_connect *)SCM_CDR(CONN);
	sql_iftab[conn->iface].close(conn);
	return SCM_UNSPECIFIED;
}
#undef FUNC_NAME

SCM_DEFINE (sql_query, "sql-query", 2, 0, 0,
	    (SCM CONN, SCM QUERY),
	    "Run an SQL query")
#define FUNC_NAME s_sql_query
{
	struct sql_connect *conn;
	void *ptr;
	char *query;
	
	SCM_ASSERT(scm_is_sql_connect(CONN), CONN, SCM_ARG1, FUNC_NAME);
	SCM_ASSERT(SCM_STRINGP(QUERY), QUERY, SCM_ARG2, FUNC_NAME);
	conn = (struct sql_connect *)SCM_CDR(CONN);
	query = SCM_ROCHARS(QUERY);
	return sql_iftab[conn->iface].query(conn, query);
}
#undef FUNC_NAME

int
sql_register_iface(struct sql_iface *ifp)
{
	if (num_iface >= MAX_IFACES)
		scm_misc_error("sql_register_iface",
			       "Too many ifaces registered",
			       SCM_EOL);
	memcpy(&sql_iftab[num_iface], ifp, sizeof sql_iftab[0]);
	sql_iftab[num_iface].name = strdup(ifp->name);
	return num_iface++;
}

void
gsql_conn_init()
{
	sql_connect_tag = scm_make_smob_type ("sql_connect",
					      sizeof (struct sql_connect));
	scm_set_smob_mark (sql_connect_tag, sql_connect_mark);
	scm_set_smob_free (sql_connect_tag, sql_connect_free);
	scm_set_smob_print (sql_connect_tag, sql_connect_print);
#ifndef SCM_MAGIC_SNARFER
# include <gsql_conn.x>
#endif
}


Return to:

Send suggestions and report system problems to the System administrator.