Module Ask.Sql

SQL helpers.

Statements

module Stmt : sig ... end

Typed SQL statements.

Schema definition

type Col.param +=
| Col of string
| Col_constraint of string
| Col_references : 'r Table.t * ('r'a) Col.t -> Col.param

The type for column parameters. See also Col.param.

  • Col sql is the full SQL column definition between the name and the comma (includes the type). All other parameters are ignored, you are in control.
  • Col_constraint sql appends sql at the end of the column definition. Can be repeated.
  • Col_references (t, c) declares that the column references column c in t.
type Table.param +=
| Table of string
| Table_constraint of string(*

The type for table parameters.

  • Table sql is the full SQL table definition between the (). All other parameters are ignored, you are in control.
  • Table_constraint sql is an SQL table contraint added at the end of the table definition. Can be repeated.
*)
val drop_table : ?schema:string -> ?if_exists:bool -> 'a Table.t -> unit Stmt.t

drop_table ~if_exist ~schema t is an SQL DROP TABLE statement to drops table t of schema schema. If if_exists is true (default) no error is reported if the table does not exist. FIXME the default is the converse of SQL maybe that's not a a good idea.

val create_table : ?schema:string -> ?if_not_exists:bool -> 'a Table.t -> unit Stmt.t

create_table t is an SQL CREATE TABLE statement for t. If if_not_exists is true (default) the corresponding sentence is added to the create statement. FIXME the default is the converse of SQL maybe that's not a good idea.

val create_schema : ?schema:string -> ?drop_tables:bool -> Table.v list -> unit Stmt.t

create_schema ~drop_tables ts are multiple SQL statements to create the tables ts if they don't exist. If drop is true (defaults to false) the table ts are dropped before if they exist.

Make sure to use Ask_sqlite3.exec otherwise only the first statement gets executed, wrapping the whole thing in Ask_sqlite3.with_transaction is a good idea as well.

Inserting and updating

val insert_row_into : ?schema:string -> ?ignore:'r Col.v list -> 'r Table.t -> 'r -> unit Stmt.t

insert_row_into ~ignore t is an SQL 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.

Band aid

Remove these things. The where part should be fully integrated with the query language.

val update_rows : ?schema:string -> 'r Table.t -> 'r Col.value list -> where:'r Col.value list -> unit Stmt.t

update_cols t cols ~where updates columns values col of the rows of t where columns have all the values in where (AND).

val delete_rows : ?schema:string -> 'r Table.t -> where:'r Col.value list -> unit Stmt.t

delete_rows t ~where deletes rows where columns have all the values in where (AND).

val table_rows : ?schema:string -> ?where:string -> 'r Table.t -> ('r Stmt.t Stmt.func -> 'ret Stmt.func) -> 'ret

select_rows t func ~where are the rows of table t. func row can be used to bind parameters of where (defaults to "TRUE"). For example:

select_rows t Stmt.(func row -> int @-> row) ~where:"id = ?1"

Bags

TODO remove these two fun.

val normalize : ('a'e) Bag.t -> ('a'e) Askt.bag
val of_bag : ('a'e) Bag.t -> string
module Bag : sig ... end

FIXME. Return type is unsafe. Add the parameter and match it on func.