Go to the first, previous, next, last section, table of contents.


SQL persistent database interface

Author(s): I. Caballero, D. Cabeza, J.M. Gómez, M. Hermenegildo, J. F. Morales, and M. Carro, clip@dia.fi.upm.es, http://www.clip.dia.fi.upm.es/, The CLIP Group, Facultad de Informática, Universidad Politécnica de Madrid.

Version: 1.10#7 (2006/4/26, 19:22:13 CEST)

Version of last change: 1.9#113 (2003/11/27, 20:56:6 CET)

The purpose of this library is to implement an instance of the generic concept of persistent predicates, where external relational databases are used for storage (see the documentation of the persdb library and [CHGT98,Par97] for details). To this end, this library exports SQL persistent versions of the assertz_fact/1, retract_fact/1 and retractall_fact/1 builtin predicates. Persistent predicates also allow concurrent updates from several programs, since each update is atomic.

The notion of persistence provides a very natural and transparent way to access database relations from a Prolog program. Stub definitions are provided for such predicates which access the database when the predicate is called (using the db_client library). A Prolog to SQL translator is used to generate the required SQL code dynamically (see library pl2sql).

This library also provides facilities for reflecting more complex views of the database relations as Prolog predicates. Such views can be constructed as conjunctions, disjunctions, projections, etc. of database relations. Also, SQL-like aggregation operations are supported.

Implementation of the Database Interface

The architecture of the low-level implementation of the database interface was defined with two goals in mind:

In order to allow the flexibility mentioned above, a client-sever architecture was chosen. At the server side, a MySQL server connects to the databases using the MySQL. At the client side, a MySQL client interface connects to this server. The server daemon (mysqld) should be running in the server machine; check your MySQL documentation on how to do that.

After the connection is established a client can send commands to the mediator server which will pass them to the corresponding database server, and then the data will traverse in the opposite direction. These messages include logging on and off from the database, sending SQL queries, and receiving the responses.

The low level implementation of the current library is accomplished by providing abstraction levels over the MySQL interface library. These layers of abstraction implement the persistent predicate view, build the appropriate commands for the database using a translator of Prolog goals to SQL commands, issue such commands using the mediator send/receive procedures, parse the responses, and present such responses to the Prolog engine via backtracking.

Example(s)


Usage and interface (persdbrt_mysql)

Documentation on exports (persdbrt_mysql)

PREDICATE: init_sql_persdb/0:

Usage:

PREDICATE: dbassertz_fact/1:

Usage: dbassertz_fact(+Fact)

PREDICATE: dbretract_fact/1:

Usage: dbretract_fact(+Fact)

PREDICATE: dbcurrent_fact/1:

Usage: dbcurrent_fact(+Fact)

PREDICATE: dbretractall_fact/1:

Usage: dbretractall_fact(+Fact)

PREDICATE: make_sql_persistent/3:

Meta-predicate with arguments: make_sql_persistent(addmodule,?,?).

Usage: make_sql_persistent(PrologPredTypes, TableAttributes, Keyword)

PREDICATE: dbfindall/4:

Meta-predicate with arguments: dbfindall(?,?,goal,?).

Usage: dbfindall(+DBId, +Pattern, +ComplexGoal, -Results)

PREDICATE: dbcall/2:

Usage: dbcall(+DBId, +ComplexGoal)

PREDICATE: sql_query/3:

Usage: sql_query(+DBId, +SQLString, AnswerTableTerm)

PREDICATE: sql_get_tables/2:

Usage 1: sql_get_tables(+Location, -Tables)

Usage 2: sql_get_tables(+DbConnection, -Tables)

PREDICATE: sql_table_types/3:

Usage 1: sql_table_types(+Location, +Table, -AttrTypes)

Usage 2: sql_table_types(+DbConnection, +Table, -AttrTypes)

REGTYPE: socketname/1:

Usage: socketname(IPP)

REGTYPE: dbname/1:

Usage: dbname(DBId)

REGTYPE: user/1:

Usage: user(User)

REGTYPE: passwd/1:

Usage: passwd(Passwd)

REGTYPE: projterm/1:

Usage: projterm(DBProjTerm)

REGTYPE: querybody/1:

Usage: querybody(DBGoal)

(UNDOC_REEXPORT): sqltype/1:

Imported from sqltypes (see the corresponding documentation for details).

Documentation on multifiles (persdbrt_mysql)

PREDICATE: sql_persistent_location/2:

Relates names of locations (the Keywords) with descriptions of such locations (Locations).

The predicate is multifile.

The predicate is of type dynamic.

Usage: sql_persistent_location(Keyword, DBLocation)

Documentation on internals (persdbrt_mysql)

REGTYPE: tuple/1:
tuple(T) :-
        list(T,atm).
tuple(T) :-
        list(T,atm).

Usage: tuple(T)

REGTYPE: dbconnection/1:

Usage: dbconnection(H)

DECLARATION: sql_persistent/3:

Usage: :- sql_persistent(PrologPredTypes, TableAttributes, Keyword).

PREDICATE: db_query/4:

Usage: db_query(+DBId, +ProjTerm, +Goal, ResultTerm)

PREDICATE: db_query_one_tuple/4:

Usage: db_query_one_tuple(+DBId, +ProjTerm, +Goal, ResultTerm)

PREDICATE: sql_query_one_tuple/3:

Usage: sql_query_one_tuple(+DBId, +SQLString, ResultTuple)

Known bugs and planned improvements (persdbrt_mysql)


Go to the first, previous, next, last section, table of contents.