[mercury-users] Strange problem
Pieter Laeremans
pieter at kotnet.org
Thu Jul 3 10:13:29 AEST 2003
Hello,
I'm trying to create a module that enables me to store and retrieve
data in a postgres database.
So I created a module psql.m and used the foreign language interface
to include the C-library.
But when I compile and run the program using just a main predicate
that only contains an "io__nl" I get a segmentation fault.
When I tested the program with an io__write_string as the first
statement, there didn't appeared any output.
Can you give me a clue about how I should debug this ?
thanks in advance,
Pieter
-----------[Mmakefile]-------------
MLLIBS=-lpq
MLFLAGS=--make-shared-lib
GRADE=hlc.gc
-----------[psql.m]-------------------
:- module psql.
:- interface.
:- import_module io.
:- type host == string.
:- type port == int.
:- type options == string.
:- type database == string.
:- type user == string.
:- type password == string.
:- type connection.
:- type connection_result --->
ok(connection);
error.
:- pred create_connection(host::in,port::in, database::in, user::in, password::in, connection_result::uo, io__state::di, io__state::uo ) is det.
:- pred create_connection(database::in, connection_result::out, io__state::di, io__state::uo) is det.
:- pred close_connection(connection::di, io__state::di, io__state::uo) is det.
:- pred main(io__state::di, io__state::uo) is det.
:- implementation.
:- import_module string.
main-->
io__nl.
% io__write_string("PSQL module testing...\n"),
% create_connection("cudi", Result),
% { Result = ok(Connection) ->
% B = "Connectie gelukt!\n"
% ;
% B = "Connectie mislukt!\n"
% },
% io__write_string(B),
%%:- type connection == c_pointer.
:- pragma foreign_decl("C", "
#include <stdio.h>
#include <postgresql/libpq-fe.h>
").
% A connection is equal to this type:
:- pragma foreign_type("C", connection, "PGconn*").
:- pred pg_connectdb(string::in, connection::uo, int::out, io__state::di, io__state::uo) is det.
:- pragma foreign_proc("C", pg_connectdb(ConnectionString::in, Connection::uo, Errno::out, IO0::di, IO::uo), [promise_pure], "
Connection = PQconnectdb(ConnectionString);
if (PQstatus(Connection) == CONNECTION_BAD){
PQfinish(Connection);
Errno = 1;
}else{
Errno = 0;
}
IO = IO0;
").
create_connection(Host, Port, Database, User, Password, ConnectionResult) -->
{ConnectionString = "host='" ++ Host ++ "'port='" ++ string__int_to_string(Port)
++ "'dbname='" ++ Database ++ "'user='" ++ User ++ "'Password='" ++ Password ++ "'"},
io__write_string("We gaan een connectie maken\n"),
make_connection(ConnectionString, ConnectionResult).
create_connection(Database, ConnectionResult) -->
{ConnectionString = "dbname='" ++ Database ++ "'"},
make_connection(ConnectionString, ConnectionResult).
:- pred make_connection(string::in, connection_result::uo, io__state::di, io__state::uo) is det.
make_connection(ConnectionString, ConnectionResult) -->
pg_connectdb(ConnectionString, Connection, Errno),
{ Errno = 0 ->
ConnectionResult = ok(Connection)
;
ConnectionResult = error
}.
:- pragma foreign_proc("C", close_connection(Connection::di, IO0::di, IO::uo), [promise_pure],
"
PQfinish(Connection);
IO=IO0;
").
--------------------------------------------------------------------------
mercury-users mailing list
post: mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe: Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------
More information about the users
mailing list