Module Rel_sql

SQL helpers.

This module provides mecanism to type and bind parameters of raw SQL statements and high-level functions to generate SQL statements from Rel representations.

Statements

module Stmt : sig ... end

Typed SQL statements.

Dialects

module Syntax : sig ... end

Standard SQL syntax fragments.

type insert_or_action = [
  1. | `Abort
  2. | `Fail
  3. | `Ignore
  4. | `Replace
  5. | `Rollback
]
module type DIALECT = sig ... end

SQL satements in a given dialect.

type dialect = (module DIALECT)

The type for SQL dialects.

Inserting, updating and deleting

val insert_into : dialect -> ?or_action:insert_or_action -> ?schema:Rel.Schema.name -> ?ignore:'r Rel.Col.def list -> 'r Rel.Table.t -> 'r -> unit Stmt.t

insert_into d ~ignore t is an INSERT INTO statement which inserts i t values draw from an value values drawn from a provided OCaml table row. Columns mentioned in col of the row are ignored for the insertion. insert_or_action specifies a corresponding INSERT OR.

val insert_into_cols : dialect -> ?schema:Rel.Schema.name -> ?ignore:'r Rel.Col.def list -> 'r Rel.Table.t -> 'r Rel.Col.value list -> unit Stmt.t

insert_into_cols is like insert_into but uses the given column values for the insertion.

val update : dialect -> ?schema:Rel.Schema.name -> 'r Rel.Table.t -> set:'r Rel.Col.value list -> where:'r Rel.Col.value list -> unit Stmt.t

update_cols d t ~set:cols ~where is an UPDATE statement which updates columns values cols of the rows of t where columns have all the values in where (AND). FIXME. The where should become ('r Bag.t -> bool value).

val delete_from : dialect -> ?schema:Rel.Schema.name -> 'r Rel.Table.t -> where:'r Rel.Col.value list -> unit Stmt.t

delete_from d t ~where is a DELETE FROM statement which deletes rows where columns have all the values in where (AND). FIXME. The where should become ('r Bag.t -> bool value).

Data definition statements

Tables

val create_table : dialect -> ?schema:Rel.Schema.name -> ?if_not_exists:unit -> 'a Rel.Table.t -> unit Stmt.t

create_table d t is a CREATE TABLE statement for t. The table is created in schema if specified. The statement is CREATE TABLE IF NOT EXISTS when ~if_not_exists:() is given.

val drop_table : dialect -> ?schema:Rel.Schema.name -> ?if_exists:unit -> 'a Rel.Table.t -> unit Stmt.t

drop_table d t is a DROP TABLE statement for t. The dropped table is in schema if specified. The statement is DROP TABLE IF EXISTS when ~if_exists:() is given.

Indices

val create_index : dialect -> ?schema:Rel.Schema.name -> ?if_not_exists:unit -> 'a Rel.Table.t -> 'a Rel.Table.Index.t -> unit Stmt.t

create_index d t i is a CREATE INDEX statement for i on table t in schema schema. The statement is CREATE INDEX IF NOT EXISTS when ~if_not_exists:() is given.

val drop_index : dialect -> ?schema:Rel.Schema.name -> ?if_exists:unit -> 'a Rel.Table.t -> 'a Rel.Table.Index.t -> unit Stmt.t

drop_index d t i is a DROP INDEX statement to drop index i of table t. The index and table are in schema if specified. The statement is DROP INDEX IF EXISTS when ~if_exists:() is given.

Schemas

val create_schema : dialect -> Rel.Schema.t -> unit Stmt.t list

create_schema_stmts d s is the sequence of statements to create schema s. This creates tables and their indices, all of which should not exist. Use drop_schema to remove previous definitions.

val drop_schema : dialect -> ?if_exists:unit -> Rel.Schema.t -> unit Stmt.t list

drop_schema_stmts d s is the sequence of statementes to drop schema s. All definitions need to exist unless ~if_exists:() is provided. This drops all tables (which should drop their indices aswell) in reverse order of Rel.Schema.tables; if you have recursive table dependencies you may have to disable foreign keys before executing the statment.

val schema_changes : dialect -> ?schema:Rel.Schema.name -> Rel.Schema.change list -> bool * unit Stmt.t list

schema_change_stmts d cs is the sequence of statements to perform the changes cs. The boolean indicates if this should be performed in a transaction.

Warning. In the Rel_sqlite3.dialect, this may set foreign keys on, if you have them off you may want to set it them off again afterwards.