summaryrefslogtreecommitdiffabout
path: root/src/pgsql.c
authorSergey Poznyakoff <gray@gnu.org.ua>2010-03-06 14:19:44 (GMT)
committer Sergey Poznyakoff <gray@gnu.org.ua>2010-03-06 14:20:47 (GMT)
commit1c2f2ebfe42aee715d96768bd15ea0e6f949a444 (patch) (side-by-side diff)
tree5fbe599fe29966322ab1ddd9a65d35a6331893d8 /src/pgsql.c
parent6e2269bce3e0104fbc6cbf62ca8107ec437d77f9 (diff)
downloadgamma-1c2f2ebfe42aee715d96768bd15ea0e6f949a444.tar.gz
gamma-1c2f2ebfe42aee715d96768bd15ea0e6f949a444.tar.bz2
Provide new connection/disconnection interfaces.
* src/Makefile.am: Rename the library to libgamma-sql. * src/gsql_conn.c (sql_connect_print): Avoid coredumps on NULL values. (sql-connect): Rename to sql-connection-open and rewrite. Connection parameters are given by a single argument, which is a list of conses. Each cons is a pair (kw . value), and sets a single parameter. (sql-query): Do not use internal Guile functions. (sql-connect-close): Rename to sql-close-connection. * gsql_lib.c (gamma_parmlist_parse): New function. (gamma_cvt_string, gamma_cvt_int): New functions. * guile-sql.h (sql_iface)<connect>: Change signature. (gamma_parmdcl): New struct. (GAMMA_PARMLIST_IGNORE_UNKNOWN) (GAMMA_PARMLIST_WARN_UNKNOWN): New constants. (gamma_parmlist_parse): New proto. (gamma_cvt_string, gamma_cvt_int): New protos. * examples/whoisd.scm: Update. * src/mysql.c (s_mysql_connect): Rewrite for the new calling convention. * src/pgsql.c (s_pgsql_connect): Likewise. * src/sql.sci: Update library name. (sql-connect, sql-connect-close): New public functions.
Diffstat (limited to 'src/pgsql.c') (more/less context) (ignore whitespace changes)
-rw-r--r--src/pgsql.c39
1 files changed, 31 insertions, 8 deletions
diff --git a/src/pgsql.c b/src/pgsql.c
index 011a4b7..398cacc 100644
--- a/src/pgsql.c
+++ b/src/pgsql.c
@@ -38,26 +38,49 @@ s_pgsql_free(struct sql_connect *conn)
}
static SCM
-s_pgsql_connect (const char *hostname, int port,
- const char *dbname, const char *user, const char *pass,
- const char *why)
+s_pgsql_connect (SCM parmlist, const char *func_name)
{
+ char *hostname = NULL;
+ char *port = NULL;
+ char *dbname = NULL;
+ char *user = NULL;
+ char *pass = NULL;
+ struct gamma_parmdcl dcltab[] = {
+ { "iface", NULL, NULL },
+ { "host", &hostname, gamma_cvt_string },
+ { "port", &port, gamma_cvt_string },
+ { "db", &dbname, gamma_cvt_string },
+ { "user", &user, gamma_cvt_string },
+ { "pass", &pass, gamma_cvt_string },
+ { NULL }
+ };
+
PGconn *pgconn;
- char buf[24];
SCM smob;
struct sql_connect *conn;
- snprintf(buf, sizeof buf, "%d", port);
- pgconn = PQsetdbLogin(hostname, buf, NULL, NULL, dbname, user, pass);
+ pgconn = PQsetdbLogin(hostname, port, NULL, NULL, dbname, user, pass);
+
if (PQstatus(pgconn) == CONNECTION_BAD) {
- SCM args = scm_list_2(scm_makfrom0str("Cannot connect to the database"),
- scm_makfrom0str(PQerrorMessage(pgconn)));
+ SCM args;
+
+ free(hostname);
+ free(port);
+ free(user);
+ free(pass);
+
+ args = scm_list_2(scm_makfrom0str("Cannot connect to the database"),
+ scm_makfrom0str(PQerrorMessage(pgconn)));
PQfinish(pgconn);
scm_throw(gsql_error, args);
}
smob = sql_connect_create("pgsql");
conn = (struct sql_connect *)SCM_CDR(smob);
+ conn->hostname = hostname;
+ conn->port = atoi (port);
+ conn->username = user;
+ conn->database = dbname;
conn->data = pgconn;
return smob;
}

Return to:

Send suggestions and report system problems to the System administrator.