Ch.10 SML# feature: seamless SQL integration

§ 10.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.

  • 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.

  • Syntax for executing database query.

    _sqleval query connection

    This primitive sends the query to the connection connection for evaluation and obtains the result of type τ SQL.rel.

  • Conversion of the query result.

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

    SQL.fetchAll converts the relation to a list of records. SQL.fetch fetches the first tuple in the relation and convert it to a record.

  • Post processing.

    SQL.closeRel : ['a. 'a SQL.rel -> unit]
    SQL.closeCon : ['a. 'a SQL.conn -> unit]

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