mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-18 19:03:45 +00:00
extras/curs/curs.m:
extras/curs/curs.panel.m:
Make panel a separate submodule of curs, not a nested submodule.
extras/base64/base64.m:
extras/curses/mcurses.basics.m:
extras/curses/mcurses.m:
extras/curses/mcurses.misc.m:
extras/curses/mcurses.user.m:
extras/gator/evolve.m:
extras/gator/genotype.m:
extras/gator/phenotype.m:
extras/gator/tausworthe3.m:
extras/monte/dots.m:
extras/monte/geom.m:
extras/monte/hg.m:
extras/monte/monte.m:
extras/monte/rnd.m:
extras/moose/grammar.m:
extras/moose/moose.m:
extras/mopenssl/mopenssl.m:
extras/net/echo.m:
extras/net/errno.m:
extras/net/getaddrinfo.m:
extras/net/net.m:
extras/net/netdb.m:
extras/net/sockets.m:
extras/net/streams.m:
extras/net/tcp.m:
extras/net/test_lookups.m:
extras/net/types.m:
extras/odbc/odbc.m:
extras/odbc/odbc_test.m:
extras/references/README:
extras/references/reference.m:
extras/references/scoped_update.m:
extras/solver_types/library/any.m:
extras/solver_types/library/any_array.m:
extras/solver_types/library/any_assoc_list.m:
extras/solver_types/library/any_list.m:
extras/solver_types/library/any_map.m:
extras/solver_types/library/any_tree234.m:
extras/solver_types/library/any_util.m:
extras/trail/trail.m:
extras/trailed_update/samples/interpreter.m:
extras/trailed_update/samples/vqueens.m:
extras/trailed_update/tests/var_test.m:
extras/trailed_update/tr_array.m:
extras/trailed_update/tr_store.m:
extras/trailed_update/trailed_update.m:
extras/trailed_update/unsafe.m:
extras/trailed_update/var.m:
Bring programming style up to date.
140 lines
4.2 KiB
Mathematica
140 lines
4.2 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
% vim: ft=mercury ts=4 sw=4 et
|
|
%-----------------------------------------------------------------------------%
|
|
% Sample program for odbc.m.
|
|
% Author: stayl
|
|
% This source file is hereby placed in the public domain. -stayl.
|
|
%
|
|
% Assumes that there is an ODBC data source "test" containing a table
|
|
% named "test".
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- module odbc_test.
|
|
:- interface.
|
|
|
|
:- import_module io.
|
|
|
|
:- pred main(io::di, io::uo) is cc_multi.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module odbc.
|
|
|
|
:- import_module exception.
|
|
:- import_module list.
|
|
:- import_module pair.
|
|
:- import_module string.
|
|
:- import_module univ.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
main(!IO) :-
|
|
odbc.data_sources(SourceResult - SourceMessages, !IO),
|
|
(
|
|
SourceResult = ok(Sources),
|
|
io.write_string("Available data source names:", !IO),
|
|
io.nl(!IO),
|
|
io.write_list(Sources, "\n", io.write, !IO),
|
|
io.nl(!IO)
|
|
;
|
|
SourceResult = error,
|
|
io.write_string("Error getting DSNs:", !IO),
|
|
io.nl(!IO)
|
|
),
|
|
io.write_list(SourceMessages, "\n", io.write, !IO),
|
|
io.nl(!IO),
|
|
odbc.transaction("test", "", "", odbc.tables(any, any, any),
|
|
TableResult - TableMessages, !IO),
|
|
(
|
|
TableResult = ok(Tables),
|
|
io.write_string("Available tables:", !IO),
|
|
io.nl(!IO),
|
|
io.write_list(Tables, "\n", io.write, !IO),
|
|
io.nl(!IO)
|
|
;
|
|
TableResult = error,
|
|
io.write_string("Error getting tables:", !IO),
|
|
io.nl(!IO)
|
|
),
|
|
io.write_list(TableMessages, "\n", io.write, !IO),
|
|
io.nl(!IO),
|
|
|
|
odbc.transaction("test", "", "", test_trans,
|
|
TransResult - TransMessages, !IO),
|
|
(
|
|
TransResult = ok(Results),
|
|
io.write_string("transaction ok: ", !IO),
|
|
list.length(Results, NumRows),
|
|
io.write_int(NumRows, !IO),
|
|
io.write_string(" result rows", !IO),
|
|
io.nl(!IO),
|
|
io.write_list(Results, "\n", io.write, !IO),
|
|
io.nl(!IO)
|
|
;
|
|
TransResult = error,
|
|
io.write_string("error in transaction:\n", !IO)
|
|
),
|
|
io.write_list(TransMessages, "\n", io.write, !IO),
|
|
io.nl(!IO),
|
|
|
|
try_io(odbc.transaction("test", "", "", test_trans_2),
|
|
ExceptionResult, !IO),
|
|
(
|
|
ExceptionResult = succeeded(Results2),
|
|
io.set_exit_status(1, !IO),
|
|
io.write_string("Error: expected exception, got results:", !IO),
|
|
io.write(Results2, !IO),
|
|
io.nl(!IO)
|
|
;
|
|
ExceptionResult = exception(Exception),
|
|
det_univ_to_type(Exception, ExceptionString),
|
|
io.write_string("Got exception: ", !IO),
|
|
io.write_string(ExceptionString, !IO),
|
|
io.nl(!IO)
|
|
).
|
|
|
|
:- pred test_trans(list(odbc.row)::out, odbc.state::di, odbc.state::uo) is det.
|
|
|
|
test_trans(Results, !DB) :-
|
|
odbc.solutions("select * from test", Results, !DB).
|
|
|
|
:- pred test_trans_2(list(odbc.row)::out, odbc.state::di, odbc.state::uo)
|
|
is det.
|
|
|
|
test_trans_2(Results, !DB) :-
|
|
odbc.solutions("select * from test", Results, !DB),
|
|
( if semidet_succeed then
|
|
throw("exception in test_trans_2")
|
|
else
|
|
true
|
|
).
|
|
|
|
:- pred output_results(list(odbc.row)::in, io::di, io::uo) is det.
|
|
|
|
output_results(Rows, !IO) :-
|
|
io.write_list(Rows, "\n", output_row, !IO).
|
|
|
|
:- pred output_row(odbc.row::in, io::di, io::uo) is det.
|
|
|
|
output_row(Row, !IO) :-
|
|
io.write_list(Row, " ", output_attribute, !IO).
|
|
|
|
:- pred output_attribute(odbc.attribute::in, io::di, io::uo) is det.
|
|
|
|
output_attribute(null, !IO) :-
|
|
io.write_string("<NULL>", !IO).
|
|
output_attribute(int(Int), !IO) :-
|
|
io.write_int(Int, !IO).
|
|
output_attribute(string(Str), !IO) :-
|
|
io.write_string(Str, !IO).
|
|
output_attribute(float(Float), !IO) :-
|
|
io.write_float(Float, !IO).
|
|
output_attribute(time(String), !IO) :-
|
|
io.write_string(String, !IO).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|