Module Rel.Table

Table descriptions.

This module defines a type to describe tables, their indices and constraints.


type name = string

The type for table names.

type 'r primary_key = 'r Col.v list

The type for table primary keys. The columns that make up the primary key.

type 'r unique_key

The type for table unique keys. See Unique_key.

type 'r foreign_key

The type for table foreign keys. See Foreign_key.

type 'r index

The type for table indices. See Index.

type 'r param = ..

The type for extensible table parameters. Warning this may be removed in the future.

type 'r t

The type for tables with rows represented by OCaml values of type 'r.

type v =
  1. | V : 'r t -> v

The type for existential tables.

val v : ?params:'r param list -> ?indices:'r index list -> ?foreign_keys:'r foreign_key list -> ?unique_keys:'r unique_key list -> ?primary_key:'r primary_key -> name -> 'r Row.t -> 'r t

v name row is a table with given parameters. See accessors for semantics.

Note. For now the module does not check that the columns mentioned in the parameters actually belong to row. Do not rely on this, Invalid_argument may be raised in the future.

val name : 'r t -> name

name t is the name of t.

val name' : v -> name

name t is the name of t.

val row : 'r t -> 'r Row.t

row t is the row of t.

val cols : ?ignore:'r Col.v list -> 'r t -> 'r Col.v list

cols t is Row.cols (row t) with columns in ignore ommited from the result.

val primary_key : 'r t -> 'r primary_key option

primary_key t is the primary key of t (if any).

val unique_keys : 'r t -> 'r unique_key list

unique_keys t are the unique keys of t.

val foreign_keys : 'r t -> 'r foreign_key list

foreign_keys t are the foreign keys of t.

val indices : 'r t -> 'r index list

indices t are the indices of table t.

val params : 'r t -> 'r param list

name t are the parameters of t.

Unique keys

module Unique_key : sig ... end

Unique keys.

val unique_key : 'r Col.v list -> 'r unique_key

unique_key s is Unique_key.v.

Foreign keys

module Foreign_key : sig ... end

Foreign keys.

val foreign_key : ?on_delete:Foreign_key.action -> ?on_update:Foreign_key.action -> cols:'r Col.v list -> parent:('s t * 's Col.v list) -> unit -> 'r foreign_key

foreign_key is a convenience constructor for Foreign_key.v.

val self_foreign_key : ?on_delete:Foreign_key.action -> ?on_update:Foreign_key.action -> cols:'r Col.v list -> parent:'r Col.v list -> unit -> 'r foreign_key

self_foreign_key is a convenience constructor for Foreign_key.v.


module Index : sig ... end

Table index descriptions.

val index : ?unique:bool -> ?name:name -> 'r Col.v list -> 'r index

index is Index.v.


type 'r change =
  1. | Add_column_after : 'r Col.v * 'r Col.v option -> 'r change

    If the second column is None, in the first position.

  2. | Add_foreign_key : 'r foreign_key -> 'r change
  3. | Add_primary_key : 'r primary_key -> 'r change
  4. | Add_unique_key : 'r unique_key -> 'r change
  5. | Create_index : 'r index -> 'r change
  6. | Drop_column : -> 'r change
  7. | Drop_foreign_key : 'a foreign_key -> 'r change
  8. | Drop_index : -> 'r change
  9. | Drop_primary_key : 'r change
  10. | Drop_unique_key : 'a unique_key -> 'r change
  11. | Set_column_default : 'r Col.v -> 'r change

    The given column default changed.

  12. | Set_column_type : 'r Col.v * 'b Col.v -> 'r change

    The given column type changed, the second column is the old column. The default should also be changed.

  13. | Set_column_pos_after : 'r Col.v * 'r Col.v option -> 'r change

    If the second column is None, in the first position.


The type for table changes.

val changes : src:'a t -> dst:'r t -> 'r change list

changes ~src ~dst is the list of structural changes to bring table src to dst.

This function does not handle renames which should be performed before (also the table name is ignored). Any name found in src (resp. dst) and absent in dst (resp. src) will result in a column drop (resp. add).

See also Schema.changes which integrates renames.

XXX. Maybe we could surface the renaming business.

val pp_change : Stdlib.Format.formatter -> 'r change -> unit

pp_change ppf c formats change c in a pseudo, non-executable, SQL.


val sort : v list -> (v list, v list) Stdlib.result

sort ts sorts table ts in dependency order. A table t depends on a table s if t refers to s via a foreign key.

If table dependencies are cyclic, the function errors with a cycle. For this function Self table dependencies do not count as cycles.

Raises Invalid_argument if there are two tables with the same name in ts or if ts is not closed over dependencies.