Database independent interface

Defines a set of functions to access Database.

Example

(import (rnrs) (mosh dbi))
(let* ([conn   (dbi-connect "dbi:mysql:mysql:127.0.0.1:3306" "root" "")]
       [query  (dbi-prepare conn "select * from user where user = ?")]
       [result (dbi-execute query "root")]
       [getter (dbi-getter result)])
  (for-each
   (lambda (row)
     (display  (getter row "host")))
   (dbi-result->list result))
  (dbi-close conn))
Summary
Database independent interfaceDefines a set of functions to access Database.
(mosh dbi)Database independent interface library
How to write new dbd.mosh/dbd/mysql.ss and test/dbi.scm are good sample.
Variables
<connection>Connection class.
<dbd>Database driver class.
<result>Result of query class.
<query>Query class.
Functions
dbi-result->listReturns list of result rows.
dbi-getterReturns getter for <result> which is returned by dbi-execute.
dbi-closeDestroy the connection and free resources associated to the connection.
dbi-connectConnect to a database using a data source specified by dsn (data source name).
dbi-executeExecutes a query created by dbi-prepare.
dbi-prepareFrom a string representation of SQL statement sql, creates and returns a query object (an instance of <query> or its subclass) for the database connection conn Sql may contain parameter slots, denoted by ?.
dbi-doThis is a convenience procedure when you create a query and immediately execute it.

(mosh dbi)

Database independent interface library

Summary
How to write new dbd.mosh/dbd/mysql.ss and test/dbi.scm are good sample.
Variables
<connection>Connection class.
<dbd>Database driver class.
<result>Result of query class.
<query>Query class.
Functions
dbi-result->listReturns list of result rows.
dbi-getterReturns getter for <result> which is returned by dbi-execute.
dbi-closeDestroy the connection and free resources associated to the connection.
dbi-connectConnect to a database using a data source specified by dsn (data source name).
dbi-executeExecutes a query created by dbi-prepare.
dbi-prepareFrom a string representation of SQL statement sql, creates and returns a query object (an instance of <query> or its subclass) for the database connection conn Sql may contain parameter slots, denoted by ?.
dbi-doThis is a convenience procedure when you create a query and immediately execute it.

How to write new dbd.

mosh/dbd/mysql.ss and test/dbi.scm are good sample.

dbd SHOULD implement following functions

  • (dbd-connect <dbd-xxx> user password options) returns sub-class of <connection>
  • (dbd-execute <xxx-connection> sql) returns sub-class of <result>
  • (dbi-getter <xxxx-result>) returns getter closure.  (getter row name)
  • (dbi-result->list <xxxx-result>) returns list of rows
  • (dbi-close <xxx-connection> returns unspecified.

Variables

<connection>

Connection class.

<dbd>

Database driver class.

<result>

Result of query class.

<query>

Query class.

Functions

dbi-result->list

Returns list of result rows.

Prototype

(dbi-result->list result)

Parameters

resultan instance of <result> or its subclass returned by dbi-execute

Returns

list of result rows.

dbi-getter

Returns getter for <result> which is returned by dbi-execute.

Prototype

(dbi-getter result)

Parameters

resultan instance of <result> or its subclass returned by dbi-execute

Returns

getter closure. getter closure accept two arguments, row and column-name.

rowrow in results.
column-namename of column to retrieve.

dbi-close

Destroy the connection and free resources associated to the connection.

Prototype

(dbi-close conn)

Parameters

connan instance of <connection> or its subclass returned by dbi-connect

Returns

unspecified

dbi-connect

Connect to a database using a data source specified by dsn (data source name).

Dsn is a string with the following syntax

dbi:driver:options

Driver part names a specific driver.  You need to have the corresponding driver library, (mosh dbd driver), installed in your system.

For example, if dsn begins with “dbi:mysql:”, dbi-connect tries to load (mosh dbd mysql).

Prototype

(dbi-connect dsn user password)

Parameters

dsndata source name.
useruser name.
passworduser password.

Returns

Instance of <connection> class.

dbi-execute

Executes a query created by dbi-prepare.  You should pass the same number of parameters as the query expects.

If the issued query is select statement, dbi-execute returns an object represents a relation.  A relation encapsulates the values in rows and columns.  If the query is other types, such as create, insert or delete, the return value of the query closure is unspecified.

Prototype

(dbi-execute query . args)

Parameters

queryan instance of <query> or its subclass returned by dbi-prepare
argsarguments for the query

Returns

an instance of <result> or its subclass.

dbi-prepare

From a string representation of SQL statement sql, creates and returns a query object (an instance of <query> or its subclass) for the database connection conn Sql may contain parameter slots, denoted by ?.

dbi-prepare may be overloaded by dbd implmentation.

Prototype

(dbi-prepare conn sql)

Parameters

connan instance of <connecton> or its subclass returned by dbi-connect
sqlsql string

Returns

an instance of <query> or its subclass.

dbi-do

This is a convenience procedure when you create a query and immediately execute it.

Prototype

(dbi-do conn sql)

Parameters

connan instance of <connection> or its subclass returned by dbi-connect
sqlsql string

Returns

unspecified

Executes a query created by dbi-prepare.
Connect to a database using a data source specified by dsn (data source name).
From a string representation of SQL statement sql, creates and returns a query object (an instance of query or its subclass) for the database connection conn Sql may contain parameter slots, denoted by ?.
Close