What is DBForge ?

DBForge is a tool to describe database schemas and generate OCaml code to access these databases.


The idea of the tool is based on the following problem: The creation of functions executing SQL queries

  • is repetitive: translation of parameters into strings and concatenation of strings to create the query, execution of the query, test on the return value, translation of the returned data (for select queries) into the correct OCaml type,
  • is error prone: an error in the concatenation of strings to create the query (for example a missing blank) or in the concatenated strings will cause an error at run time,
  • does not allow a high maintainability: indeed, when the database schema is modified, one must verify that each query is still correct against the new database schema.

To solve these problems, we want to

  • automatically generate the functions executing basic queries on each table,
  • allow the user to define complex queries, using the SQL language ; the OCaml functions executing these queries being automatically generated, we want to guarantee at compile time that execution of these queries will not fail at run time (because of errors like syntax errors or referencing a non-existent column of a table, for example),
  • automatically verify and invalidate some queries when the database schema is modified.

To do so, we must have the following information about each colum of each table:

  • its name,
  • its type definition for the target DBMS,
  • some attributes like whether the column is nullable, whether it's a key for this table,
  • its OCaml type in the source code we want to generate,
  • the functions to use to translate DBMS (resp. OCaml) values into OCaml (resp. DBMS) values.

The list of tables with the above information for each column is what we call the database schema.

The DBForge tool allows to edit such a schema in a graphical user interface, and generates the corresponding OCaml code. The user can also define SQL queries to be embedded in OCaml functions in the generared code. For now, only "select" and "union" queries are accepted.


The distribution archive contains an example in the directory src/example. You will find several files.

Makefile is the file used to compile with make.

base.xml is the file containing the schema. It can be edited with the graphical interface, launched by the following command:

dbforge-gtk[.byte] base.xml

The OCaml code is generated from this file with the follownig command:

dbforge[.byte] -o base.ml base.xml

The file base.ml then contains the generated OCaml code. This code uses two modules installed with DBForge: Dbf_sql_driver and Dbf_sql_misc. You can get the module interface of the generated file with the following command:

ocamlfind ocamlc -package dbforge.mysql -i base.ml > base.mli

The generated code is composed of functors, one per table and one more for the additional queries defined in the schema. These functors take in parameter a module of type Dbf_sql_driver.SqlDriver, which allows to abstract the underlying database system, by providing a common interface.

example.ml is the main file of the example. It uses the module Dbf_mysql, compiled and installed with DBForge. This module has the type Dbf_sql_driver.SqlDriver and can therefore be passed in parameter to the functors of the generated code in file base.ml. The modules obtained then offer the functions to access the database.


DBForge is hosted on Github.


  • 2.0.1 [2013-02-13]: Minor change in Makefile targets to build well in opam.
  • 2.0 [2012-04-12]: first release after extraction from Cameleon.


DBForge requires the following libraries to be installed:

To compile and install:

  • tar xvfz dbforge-XXX.tar.gz
  • cd dbforge-XXX
  • make all install

This will install the following findlib packages: dbforge, dbforge.gtk, dbforge.sql, dbforge.mysql and sqml

Executables (dbforge[.byte], dbforge-gtk[.byte]) will be installed in ocaml bin directory.


DBForge is released under the LGPL v3.

Author and contact

Maxence Guesdon <Maxence 'DOT' Guesdon 'AT' inria 'DOT' fr> with Pierre-Yves Strub and Nadji Gauthier.