aboutsummaryrefslogtreecommitdiff
path: root/src/pgsql.c
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2018-08-22 09:49:59 +0300
committerSergey Poznyakoff <gray@gnu.org>2018-08-22 11:23:16 +0300
commitf92dfd663dc50d9aac704c8df5ca48f5e35b8571 (patch)
tree48542f3bb6c0961db678cad49c0ce94d70793f6d /src/pgsql.c
parent6abb2548315b9db2925194e54f09f01126182f81 (diff)
downloadgamma-f92dfd663dc50d9aac704c8df5ca48f5e35b8571.tar.gz
gamma-f92dfd663dc50d9aac704c8df5ca48f5e35b8571.tar.bz2
Change library loading algorithm.
If the environment variable LD_LIBRARY_PATH is set, the loadable modules are first looked up in the directories listed in that variable, and then in the extension installation directory. Two attempts are made for each directory: first the library name with the -v-VERSION suffix is tried, then the library name without that suffix. * src: Rename to gamma * Makefile.am (SUBDIRS): Update. * configure.ac: Update. * scripts/bootstrap (srcdir, makefile): Update. * gamma/loader.sci: New file. * gamma/Makefile.am: Build loader.scm * src/sql.sci: Remove. * gamma/sql.scm: New file. * modules/sql (scm): Update. * src/syslog.sci: Remove. * gamma/syslog.scm: New file. * modules/syslog (scm): Update. * src/expat.sci: Remove. * gamma/expat.scm: New file. * modules/expat (scm): Update.
Diffstat (limited to 'src/pgsql.c')
-rw-r--r--src/pgsql.c180
1 files changed, 0 insertions, 180 deletions
diff --git a/src/pgsql.c b/src/pgsql.c
deleted file mode 100644
index 1421c40..0000000
--- a/src/pgsql.c
+++ /dev/null
@@ -1,180 +0,0 @@
1/* This file is part of Gamma.
2 Copyright (C) 2002, 2010 Sergey Poznyakoff
3
4 Gamma is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 Gamma is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with Gamma. If not, see <http://www.gnu.org/licenses/>. */
16
17#ifdef HAVE_CONFIG_H
18# include <config.h>
19#endif
20#include <string.h>
21#include <guile-sql.h>
22#include <libpq-fe.h>
23
24static size_t
25s_pgsql_free(struct sql_connect *conn)
26{
27 PGconn *pgconn = (PGconn*) conn->data;
28 if (pgconn)
29 PQfinish(pgconn);
30 return 0;
31}
32
33static SCM
34s_pgsql_connect(SCM parmlist, const char *func_name)
35{
36 char *hostname = NULL;
37 char *port = NULL;
38 char *dbname = NULL;
39 char *user = NULL;
40 char *pass = NULL;
41 struct gamma_parmdcl dcltab[] = {
42 { "iface", NULL, NULL },
43 { "host", &hostname, gamma_cvt_string },
44 { "port", &port, gamma_cvt_string },
45 { "db", &dbname, gamma_cvt_string },
46 { "user", &user, gamma_cvt_string },
47 { "pass", &pass, gamma_cvt_string },
48 { NULL }
49 };
50
51 PGconn *pgconn;
52 SCM smob;
53 struct sql_connect *conn;
54
55 gamma_parmlist_parse (parmlist, dcltab, 0, func_name);
56
57 pgconn = PQsetdbLogin(hostname, port, NULL, NULL, dbname, user, pass);
58
59 if (PQstatus(pgconn) == CONNECTION_BAD) {
60 SCM args, pmsg;
61
62 free(hostname);
63 free(port);
64 free(user);
65 free(pass);
66
67 pmsg = scm_from_locale_string(PQerrorMessage(pgconn));
68 args = scm_list_1(scm_from_locale_string("Cannot connect to the database"));
69 PQfinish(pgconn);
70 scm_error(gamma_sql_error, func_name,
71 "~A",
72 args,
73 scm_list_2(SCM_BOOL_F, pmsg));
74 }
75
76 smob = sql_connect_create("pgsql");
77 conn = (struct sql_connect *)SCM_CDR(smob);
78 conn->hostname = hostname;
79 conn->port = port ? atoi (port) : 0;
80 conn->username = user;
81 conn->database = dbname;
82 conn->data = pgconn;
83 return smob;
84}
85
86static SCM
87result_to_list(PGresult *res)
88{
89 int i, j;
90 int ntuples = PQntuples(res);
91 int nfields = PQnfields(res);
92 SCM row_head = SCM_EOL, row_tail = SCM_EOL;
93
94 for (i = 0; i < ntuples; i++) {
95 SCM new_row;
96 SCM head = SCM_EOL, tail;
97
98 for (j = 0; j < nfields; j++) {
99 char *val = PQgetvalue(res, i, j);
100 SCM new_elt = scm_cons(val ?
101 scm_from_locale_string(val) :
102 SCM_BOOL_F, SCM_EOL);
103 if (head == SCM_EOL)
104 head = new_elt;
105 else
106 SCM_SETCDR(tail, new_elt);
107 tail = new_elt;
108 }
109
110 new_row = scm_cons(head, SCM_EOL);
111
112 if (row_head == SCM_EOL)
113 row_head = new_row;
114 else
115 SCM_SETCDR(row_tail, new_row);
116 row_tail = new_row;
117 }
118 return row_head;
119}
120
121static SCM
122s_pgsql_query(struct sql_connect *conn, const char *query)
123{
124 PGconn *pgconn = (PGconn*) conn->data;
125 PGresult *res;
126 SCM cell = SCM_EOL;
127 ExecStatusType stat;
128
129 res = PQexec(pgconn, query);
130 if (!res)
131 scm_error(gamma_sql_error, "sql-query",
132 "~A",
133 scm_list_1(scm_from_locale_string("Error executing PostgreSQL query")),
134 scm_list_2(SCM_BOOL_F,
135 scm_from_locale_string(PQerrorMessage(pgconn))));
136
137 stat = PQresultStatus(res);
138
139 switch (stat) {
140 case PGRES_COMMAND_OK:
141 /* Successful completion of a command returning no data */
142 cell = scm_from_ulong(strtoul(PQcmdTuples(res), NULL, 0));
143 break;
144 case PGRES_TUPLES_OK:
145 /* The query successfully executed */
146 cell = result_to_list(res);
147 PQclear(res);
148 break;
149 default:
150 scm_error(gamma_sql_error, "sql-query",
151 "~A",
152 scm_list_1(scm_from_locale_string("PostgreSQL error")),
153 scm_list_2(scm_from_uint(stat),
154 scm_from_locale_string(PQresStatus(stat))));
155 }
156
157 return cell;
158}
159
160static void
161s_pgsql_close(struct sql_connect *conn)
162{
163 PGconn *pgconn = (PGconn*) conn->data;
164 if (pgconn) {
165 PQfinish(pgconn);
166 conn->data = NULL;
167 }
168}
169
170struct sql_iface pgsql_iface = {
171 "pgsql",
172 NULL, /* mark */
173 s_pgsql_free,
174 s_pgsql_connect,
175 s_pgsql_close,
176 s_pgsql_query,
177};
178
179
180

Return to:

Send suggestions and report system problems to the System administrator.