From: Linas Vepstas <linasvepstas@gmail.com>
To: Maurizio Boriani <baux@member.fsf.org>
Cc: Guile User Mailing List <guile-user@gnu.org>
Subject: [PATCH 11/12] Guile-DBD-postgres: Avoid deprecated functions
Date: Fri, 19 Sep 2008 09:22:02 -0500 [thread overview]
Message-ID: <20080919142202.GK13684@linas.org> (raw)
In-Reply-To: <3ae3aa420809190645o2fe2b746id80f53c5cb123e5b@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 9710 bytes --]
Don't use deprecated guile gh_ functions. Also, move
some string handling functions around so as to avoid memory leak.
Also, avoid some casting. Casting is bad, it can hide errors
from the compiler.
Signed-off-by: Linas Vepstas <linasvepstas@gmail.com>
---
src/guile-dbd-postgresql.c | 67 ++++++++++++++++++++++-----------------------
1 file changed, 34 insertions(+), 33 deletions(-)
Index: guile-dbd-postgresql-2.0.0/src/guile-dbd-postgresql.c
===================================================================
--- guile-dbd-postgresql-2.0.0.orig/src/guile-dbd-postgresql.c 2008-09-16 19:31:37.000000000 -0500
+++ guile-dbd-postgresql-2.0.0/src/guile-dbd-postgresql.c 2008-09-16 19:46:13.000000000 -0500
@@ -23,7 +23,6 @@
#include <guile-dbi/guile-dbi.h>
#include <libguile.h>
-#include <guile/gh.h>
#include <postgresql/libpq-fe.h>
#include <string.h>
#include <errno.h>
@@ -59,7 +58,7 @@ __postgresql_make_g_db_handle(gdbi_db_ha
if(scm_equal_p(scm_string_p(dbh->constr), SCM_BOOL_F) == SCM_BOOL_T)
{
/* todo: error msg to be translated */
- dbh->status = (SCM) scm_cons(scm_from_int(1),
+ dbh->status = scm_cons(scm_from_int(1),
scm_from_locale_string("missing connection string"));
return;
}
@@ -70,11 +69,7 @@ __postgresql_make_g_db_handle(gdbi_db_ha
if (items >= 5 && items < 8)
{
char* port = NULL;
- char* user = gh_scm2newstr(scm_list_ref(cp_list,scm_from_int(0)),NULL);
- char* pass = gh_scm2newstr(scm_list_ref(cp_list,scm_from_int(1)),NULL);
- char* db = gh_scm2newstr(scm_list_ref(cp_list,scm_from_int(2)),NULL);
- char* ctyp = gh_scm2newstr(scm_list_ref(cp_list,scm_from_int(3)),NULL);
- char* loc = gh_scm2newstr(scm_list_ref(cp_list,scm_from_int(4)),NULL);
+ char* user, *pass, *db, *ctyp, *loc;
pgsqlP = (gdbi_pgsql_ds_t*)malloc(sizeof(gdbi_pgsql_ds_t));
pgsqlP->retn = 0;
@@ -86,18 +81,24 @@ __postgresql_make_g_db_handle(gdbi_db_ha
return;
}
+ user = scm_to_locale_string(scm_list_ref(cp_list,scm_from_int(0)));
+ pass = scm_to_locale_string(scm_list_ref(cp_list,scm_from_int(1)));
+ db = scm_to_locale_string(scm_list_ref(cp_list,scm_from_int(2)));
+ ctyp = scm_to_locale_string(scm_list_ref(cp_list,scm_from_int(3)));
+ loc = scm_to_locale_string(scm_list_ref(cp_list,scm_from_int(4)));
+
pgsqlP->pgsql = NULL;
pgsqlP->res = NULL;
if (strcmp(ctyp,"tcp") == 0)
{
- port = (char*) gh_scm2newstr(scm_list_ref(cp_list,scm_from_int(5)),NULL);
+ port = scm_to_locale_string(scm_list_ref(cp_list,scm_from_int(5)));
pgsqlP->pgsql = (PGconn*) PQsetdbLogin(loc,port,NULL,NULL,db,user,pass);
if (items == 7)
{
- char* sretn = gh_scm2newstr(scm_list_ref(cp_list,scm_from_int(6)),
- NULL);
+ char* sretn = scm_to_locale_string(scm_list_ref(cp_list,scm_from_int(6)));
pgsqlP->retn = atoi(sretn);
+ free (sretn);
}
}
else
@@ -105,9 +106,9 @@ __postgresql_make_g_db_handle(gdbi_db_ha
pgsqlP->pgsql = (PGconn*) PQsetdbLogin(loc,NULL,NULL,NULL,db,user,pass);
if (items == 6)
{
- char* sretn = gh_scm2newstr(scm_list_ref(cp_list,scm_from_int(5)),
- NULL);
+ char* sretn = scm_to_locale_string(scm_list_ref(cp_list,scm_from_int(5)));
pgsqlP->retn = atoi(sretn);
+ free (sretn);
}
}
@@ -139,7 +140,7 @@ __postgresql_make_g_db_handle(gdbi_db_ha
if(PQstatus(pgsqlP->pgsql) == CONNECTION_BAD)
{
- dbh->status = (SCM) scm_cons(scm_from_int(1),
+ dbh->status = scm_cons(scm_from_int(1),
scm_from_locale_string(PQerrorMessage(pgsqlP->pgsql)));
PQfinish(pgsqlP->pgsql);
pgsqlP->pgsql = NULL;
@@ -151,7 +152,7 @@ __postgresql_make_g_db_handle(gdbi_db_ha
else
{
/* todo: error msg to be translated */
- dbh->status = (SCM) scm_cons(scm_from_int(0),
+ dbh->status = scm_cons(scm_from_int(0),
scm_from_locale_string("db connected"));
dbh->db_info = pgsqlP;
dbh->closed = SCM_BOOL_F;
@@ -161,7 +162,7 @@ __postgresql_make_g_db_handle(gdbi_db_ha
else
{
/* todo: error msg to be translated */
- dbh->status = (SCM) scm_cons(scm_from_int(1),
+ dbh->status = scm_cons(scm_from_int(1),
scm_from_locale_string("invalid connection string"));
dbh->db_info = NULL;
dbh->closed = SCM_BOOL_T;
@@ -181,14 +182,14 @@ __postgresql_close_g_db_handle(gdbi_db_h
if (pgsqlP == NULL)
{
/* todo: error msg to be translated */
- dbh->status = (SCM) scm_cons(scm_from_int(1),
+ dbh->status = scm_cons(scm_from_int(1),
scm_from_locale_string("dbd info not found"));
return;
}
else if (pgsqlP->pgsql == NULL)
{
/* todo: error msg to be translated */
- dbh->status = (SCM) scm_cons(scm_from_int(1),
+ dbh->status = scm_cons(scm_from_int(1),
scm_from_locale_string("dbi connection already closed"));
free(dbh->db_info);
dbh->db_info = NULL;
@@ -208,7 +209,7 @@ __postgresql_close_g_db_handle(gdbi_db_h
/* todo: error msg to be translated */
dbh->closed = SCM_BOOL_T;
- dbh->status = (SCM) scm_cons(scm_from_int(0),
+ dbh->status = scm_cons(scm_from_int(0),
scm_from_locale_string("dbi closed"));
return;
}
@@ -224,7 +225,7 @@ __postgresql_query_g_db_handle(gdbi_db_h
if(dbh->db_info == NULL)
{
/* todo: error msg to be translated */
- dbh->status = (SCM) scm_cons(scm_from_int(1),
+ dbh->status = scm_cons(scm_from_int(1),
scm_from_locale_string("invalid dbi connection"));
return;
}
@@ -232,7 +233,7 @@ __postgresql_query_g_db_handle(gdbi_db_h
if (query == NULL)
{
/* todo: error msg to be translated */
- dbh->status = (SCM) scm_cons(scm_from_int(1),
+ dbh->status = scm_cons(scm_from_int(1),
scm_from_locale_string("invalid dbi query"));
return;
}
@@ -257,14 +258,14 @@ __postgresql_query_g_db_handle(gdbi_db_h
if (err == 1)
{
- dbh->status = (SCM) scm_cons(scm_from_int(0),
+ dbh->status = scm_cons(scm_from_int(0),
scm_from_locale_string("query ok"));
pgsqlP->lget = 0;
return;
}
else
{
- dbh->status = (SCM) scm_cons(scm_from_int(1),
+ dbh->status = scm_cons(scm_from_int(1),
scm_from_locale_string(PQerrorMessage(pgsqlP->pgsql)));
return;
}
@@ -285,7 +286,7 @@ __postgresql_getrow_g_db_handle(gdbi_db_
if(dbh->db_info == NULL)
{
/* todo: error msg to be translated */
- dbh->status = (SCM) scm_cons(scm_from_int(1),
+ dbh->status = scm_cons(scm_from_int(1),
scm_from_locale_string("invalid dbi connection"));
return (SCM_BOOL_F);
}
@@ -294,7 +295,7 @@ __postgresql_getrow_g_db_handle(gdbi_db_
if (pgsqlP->res == NULL &&
(pgsqlP->res = PQgetResult(pgsqlP->pgsql)) == NULL)
{
- dbh->status = (SCM) scm_cons(scm_from_int(0),
+ dbh->status = scm_cons(scm_from_int(0),
scm_from_locale_string("row end"));
pgsqlP->lget = 0;
return (SCM_BOOL_F);
@@ -314,14 +315,14 @@ __postgresql_getrow_g_db_handle(gdbi_db_
type == 1700 ||
type == 26 )
{
- value_str = (char*) strndup(PQgetvalue(pgsqlP->res,pgsqlP->lget,f),
+ value_str = strndup(PQgetvalue(pgsqlP->res,pgsqlP->lget,f),
PQgetlength(pgsqlP->res,pgsqlP->lget,f));
value = scm_from_long(atoi(value_str));
}
else if (type == 700 ||
type == 701 )
{
- value_str = (char*) strndup(PQgetvalue(pgsqlP->res,pgsqlP->lget,f),
+ value_str = strndup(PQgetvalue(pgsqlP->res,pgsqlP->lget,f),
PQgetlength(pgsqlP->res,pgsqlP->lget,f));
value = scm_from_double(atof(value_str));
}
@@ -331,13 +332,13 @@ __postgresql_getrow_g_db_handle(gdbi_db_
type == 702 ||
(type >= 1042 && type <= 1114))
{
- value_str = (char*) strndup(PQgetvalue(pgsqlP->res,pgsqlP->lget,f),
+ value_str = strndup(PQgetvalue(pgsqlP->res,pgsqlP->lget,f),
PQgetlength(pgsqlP->res,pgsqlP->lget,f));
- value = (SCM)scm_from_locale_string(value_str);
+ value = scm_from_locale_string(value_str);
}
else
{
- dbh->status = (SCM) scm_cons(scm_from_int(1),
+ dbh->status = scm_cons(scm_from_int(1),
scm_from_locale_string("unknown field type"));
pgsqlP->lget++;
return SCM_EOL;
@@ -362,12 +363,12 @@ __postgresql_getrow_g_db_handle(gdbi_db_
case PGRES_FATAL_ERROR:
if (pgsqlP->res == NULL)
{
- dbh->status = (SCM) scm_cons(scm_from_int(0),
+ dbh->status = scm_cons(scm_from_int(0),
scm_from_locale_string("row end"));
}
else
{
- dbh->status = (SCM) scm_cons(scm_from_int(1),
+ dbh->status = scm_cons(scm_from_int(1),
scm_from_locale_string(PQresStatus(PQresultStatus(pgsqlP->res))));
}
break;
@@ -376,11 +377,11 @@ __postgresql_getrow_g_db_handle(gdbi_db_
case PGRES_TUPLES_OK:
case PGRES_COPY_OUT:
case PGRES_COPY_IN:
- dbh->status = (SCM) scm_cons(scm_from_int(0),
+ dbh->status = scm_cons(scm_from_int(0),
scm_from_locale_string(PQresStatus(PQresultStatus(pgsqlP->res))));
break;
default:
- dbh->status = (SCM) scm_cons(scm_from_int(0),
+ dbh->status = scm_cons(scm_from_int(0),
scm_from_locale_string("unknown return query status"));
break;
}
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 481 bytes --]
next prev parent reply other threads:[~2008-09-19 14:22 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-09-19 13:45 PATCH [0/12]: Guile-DBI and Guile-DBD-Postgres Linas Vepstas
2008-09-19 14:00 ` [PATCH 1/12] Guile-DBI: Avoid multiple init Linas Vepstas
2008-09-19 14:05 ` [PATCH 2/12] Guile-DBI: minor cleanup Linas Vepstas
2008-09-19 14:06 ` [PATCH 3/12] Guile-DBI: Fix memory leak Linas Vepstas
2008-09-19 14:09 ` [PATCH 4/12] Guile-DBII: replace deprecated gh_ functions Linas Vepstas
2008-09-19 14:11 ` [PATCH 6/12] Guile-DBI: Fix crash, avoid recursive free Linas Vepstas
2008-09-19 14:13 ` [PATCH 5/12] Guile-DBI: Use remember_upto_here Linas Vepstas
2008-09-19 14:15 ` [PATCH 7/12] Guile-DBI: Don't cast away const Linas Vepstas
2008-09-19 14:17 ` [PATCH 8/12] Guile-DBD-postgres: Fix handling of float point columns Linas Vepstas
2008-09-19 14:19 ` [PATCH 9/12] Guile-DBD-postgres: Avoid discouraged functions Linas Vepstas
2008-09-19 14:20 ` [PATCH 10/12] Guile-DBD-postgres: Fix off-by-one when fetching rows Linas Vepstas
2008-09-19 14:22 ` Linas Vepstas [this message]
2008-09-19 14:23 ` [PATCH 12/12] Guile-DBD-postgres: Crash in GC Linas Vepstas
2008-09-19 23:00 ` PATCH [0/12]: Guile-DBI and Guile-DBD-Postgres Neil Jerram
2008-09-19 23:22 ` Greg Troxel
2008-09-19 23:24 ` Greg Troxel
2008-09-20 0:19 ` Linas Vepstas
2008-09-20 13:25 ` Greg Troxel
2008-09-20 15:43 ` Linas Vepstas
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20080919142202.GK13684@linas.org \
--to=linasvepstas@gmail.com \
--cc=baux@member.fsf.org \
--cc=guile-user@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).