I'm trying to figure out the best way to manage odbc database connections in my
system and I haven't been happy with what I've come up with so far. Below is a
generic version of my code. What I'd like is for the other parts of my code to
be able to call db_query(Column) bound as a test, but also unbound and able to
be backtracked through to return multiple answers. The problem of course is
that once you go through the disconnect you can't backtrack into the query again
for the next value.
I'd like to be able to call db_query(C). from the SWI command line, have it
return a value, then be able to use ';' to find the next result, but it seems
that I've have to expose the detail of managing the db connection to do so. It
seems like I'm looking for the ability to register a callback for the db_query
predicate that always runs the disconnect_db code when we know that we're not
going to backtrack through it anymore.
db_query(Column):-
connect_db(C),
bare_db_query(C,Column),
disconnect_db(C).
bare_db_query(C, Column):-
odbc_query(C,'SELECT * from my_table', row(Column)).
connect_db(C) :-
odbc_connect('dbname', C, [ user(dbuser), password(dbpassword),
open(multiple) ]).
disconnect_db(C) :-
odbc_disconnect(C).
Has anyone come up with a better way of dealing with this?
Steve
_______________________________________________
SWI-Prolog mailing list
SWI-Prolog@...
https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog