SML# Document Version 3.7.1
12 SML# feature: seamless SQL integration

12.3 Query execution

Database can be accessed by applying query function defined by _sql x => exp expression to a database connection. For this purpose, SML# provides the following constructs.

  • Database server expressions

    _sqlserver serverLocation : τ

    This expression locates a database server. serverLocation is the location information of a database server. Its concrete syntax is determined by the database system to be connected. τ is the type of the database to be connected. It describes the table names and their types using the syntax of record types. Evaluating this expression always succeeds and yields a database server object of type τ SQL.server, which contains the database server information. See Section 22.3 for details.

  • Database connection primitive.

    SQL.connect : [’a. ’a SQL.server -> ’a SQL.conn]

    This primitive takes a database server value, extract the database location stored in the value, attempts to connect to the database, and if successful it checks that the connected database indeed has the structure described by τ, and then return a database connection object of type τ SQL.conn.

  • Database query execution. The _sql expression itself is a function that executes its corresponding query on a database. By applying a connection to a query function, the query is executed on a database through given connection. The result is returned as a cursor of type τ SQL.cursor.

  • Conversion of the query result.

    SQL.fetchAll : [’a. ’a SQL.cursor -> ’a list]
    SQL.fetch : [’a. ’a SQL.cursor -> (’a * ’a SQL.rel) option]

    SQL.fetch fetches the first tuple at the cursor and forward the cursor to the next tuple. SQL.fetchAll reads all tuples after the cursor and converts them to a list of records.

  • Post processing.

    SQL.closeCursor : [’a. ’a SQL.cursor -> unit]
    SQL.closeConn : [’a. ’a SQL.conn -> unit]

    SQL.closeCursor terminates the query and SQL.closeConn close a database connection.