package petrol

  1. Overview
  2. Docs

Provides an E-DSL for specifying SQL tables in OCaml.

type conflict_clause = [
  1. | `ABORT
  2. | `FAIL
  3. | `IGNORE
  4. | `REPLACE
  5. | `ROLLBACK
]
type foreign_conflict_clause = [
  1. | `CASCADE
  2. | `NO_ACTION
  3. | `RESTRICT
  4. | `SET_DEFAULT
  5. | `SET_NULL
]
type 'a constraint_

'a constraint_ represents a constraint on an SQL column or columns.

type 'a field

'a field represents an SQL table field definition.

val field : ?constraints:[ `Column ] constraint_ list -> string -> ty:'a Type.t -> 'a field

field ?constraints name ~ty constructs a new table column with name name and type ty.

constraints are a list of column constraints for the column.

Note name must be a valid SQL identifier - this is not checked by the function, but will cause an SQL error at runtime.

type 'a table =
  1. | [] : unit table
  2. | :: : ('a field * 'b table) -> ('a * 'b) table

'a table represents a heterogeneous list of fields in a SQL Table schema, where 'a captures the types of each element.

Like Expr.expr_list, if you have opened the Schema module, you can use vanilla list syntax to construct terms of this type.

val primary_key : ?name:string -> ?ordering:[ `ASC | `DESC ] -> ?on_conflict:conflict_clause -> ?auto_increment:bool -> unit -> [ `Column ] constraint_

primary_key ?name ?ordering ?on_conflict ?auto_increment () returns a new SQL column constraint that indicates that the column it is attached to must be the primary key.

name is an optional name for the constraint for debugging purposes.

ordering is the ordering of the primary key index.

on_conflict specifies how to handle conflicts.

auto_increment specifies whether the primary key should be automatically generated. (Note: not supported for Postgres databases.)

val table_primary_key : ?name:string -> ?on_conflict:conflict_clause -> string list -> [ `Table ] constraint_

table_primary_key ?name ?on_conflict cols returns a new SQL table constraint that specifies that the table it is attached to's primary key is over the columns in cols.

name is an optional name for the constraint for debugging purposes.

on_conflict specifies how to handle conflicts.

val not_null : ?name:string -> ?on_conflict:conflict_clause -> unit -> [ `Column ] constraint_

not_null ?name ?on_conflict () returns a new SQL column constraint that specifies that the column it is attached to's value must not be NULL.

name is an optional name for the constraint for debugging purposes.

on_conflict specifies how to handle conflicts.

val unique : ?name:string -> ?on_conflict:conflict_clause -> unit -> [ `Column ] constraint_

unique ?name ?on_conflict () returns a new SQL column constraint that specifies that the column it is attached to's values must be unique.

name is an optional name for the constraint for debugging purposes.

on_conflict specifies how to handle conflicts.

val table_unique : ?name:string -> ?on_conflict:conflict_clause -> string list -> [ `Table ] constraint_

unique ?name ?on_conflict cols returns a new SQL table constraint that specifies that the table it is attached to's values for the columns cols must be unique.

name is an optional name for the constraint for debugging purposes.

on_conflict specifies how to handle conflicts.

val foreign_key : ?name:string -> ?on_update:foreign_conflict_clause -> ?on_delete:foreign_conflict_clause -> table:table_name -> columns:'a Expr.expr_list -> unit -> [ `Column ] constraint_

foreign_key ?name ?on_update ?on_delete ~table ~columns () returns a new SQL column constraint that specifies that the column it is attached to's values must be a foreign key into the table table with columns columns.

name is an optional name for the constraint for debugging purposes.

on_update and on_delete specifies how to handle conflicts for updates and deletes respectively.

val table_foreign_key : ?name:string -> ?on_update:foreign_conflict_clause -> ?on_delete:foreign_conflict_clause -> table:table_name -> columns:'a Expr.expr_list -> string list -> [ `Table ] constraint_

table_foreign_key ?name ?on_update ?on_delete ~table ~columns cols returns a new SQL table constraint that specifies that the table it is attached to's values for the columns cols must be a foreign key into the table table with columns columns.

name is an optional name for the constraint for debugging purposes.

on_update and on_delete specifies how to handle conflicts for updates and deletes respectively..

OCaml

Innovation. Community. Security.