Chapter 7. Interaction with SQL Databases

Table of Contents

JDBC Functions
Running Queries
Database Vendor Support
HSQLDB
Microsoft SQL Server Support
Oracle
PostgreSQL
Adding Support for Other Vendors

The SQL library performs automatic conversion between scheme and database types. Since the exact conversion table depends on the vendor, a mechanism is provided to add support for other vendors though vendor-specific modules.

Note

This library is not yet complete, but it covers the most frequently used functionalities. It should be considered useful but immature; in particular, a few names and function signatures are subject to change.

Requires: (import sql/jdbc)
Located in: siscweb-sql.jar

This module provides functions to load drivers, obtain connections, and execute procedures within a transactional context.

Requires: (import sql/query)
Located in: siscweb-sql.jar

This module provides functions to execute queries and map through result sets.

Note

JDBC ResultSets are translated to lazy lists (in the sense of srfi-45) of SISC hashtables. When the last element of the list is fetched (or in case of error during fetching), the underlying JDBC ResultSet and Statement are automatically closed. In case of premature escape from the jdbc/call/conn context, intentional or not, the JDBC ResultSet and Statement are closed only upon garbage collection. This can be a particularly insidious problem when using pooled connections, and will be fixed in the future.

procedure: (sql/execute connection sql-query [value] ...) => number/resultset

Executes sql-query through the given connection.

A number of optional values can be specified; these will be bound in order to the placeholders in the query. Date, time and timestamps from srfi-19 can be used to bind to SQL DATE, TIME and TIMESTAMP types.

Returns the number of rows updated in case of an INSERT/UPDATE/DELETE query statement, and a result set in case of a SELECT statement. Result sets are lazy lists in the sense of srfi-45. Each element of the list is a hashtable (field-name => value). Multiple result sets are not supported.

          
(sql/execute conn "SELECT * FROM my_table WHERE id = ?" 12345)
            
        

SISCweb includes a number of vendor-specific modules that map Scheme data types onto SQL types and provide extra functionalities, usually in the area of BLOB handling.

Adding support for new vendors is described in the section called “Adding Support for Other Vendors”