Module Rel.Table

Table definitions.

A table is defined by a name and a row definition.

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

Tables

type 'r param = ..

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

type name = string

The type for table names.

type 'r t

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

type def =
  1. | Def : 'r t -> def

The type for existential tables.

module Primary_key : sig ... end

Primary keys definitions.

module Unique_key : sig ... end

Unique keys definitions.

module Foreign_key : sig ... end

Foreign keys definitions.

module Index : sig ... end

Table index definitionss.

val make : ?params:'r param list -> ?indices:'r Index.t list -> ?foreign_keys:'r Foreign_key.t list -> ?unique_keys:'r Unique_key.t list -> ?primary_key:'r Primary_key.t -> name -> 'r Row.t -> 'r t

make name row is a table definition with:

  • name, the name of the table.
  • row, a description of its columns.
  • primary_key, a description of its primary key constraint (if any).
  • unique_keys, a description of unique key constraints (if any).
  • foreign_keys, a description of foreign keys (if any).
  • indices, a description of table indices (if any).
  • params, a list of parameters for the table

Note. For now the module does not check that the columns mentioned in the various constraints 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' : def -> 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.def list -> 'r t -> 'r Col.def list

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

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

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

val unique_keys : 'r t -> 'r Unique_key.t list

unique_keys t are the unique keys of t.

val foreign_keys : 'r t -> 'r Foreign_key.t list

foreign_keys t are the foreign keys of t.

val indices : 'r t -> 'r Index.t list

indices t are the indices of table t.

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

name t are the parameters of t.

Changes

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

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

    *)
  2. | Add_foreign_key : 'r Foreign_key.t -> 'r change
  3. | Add_primary_key : 'r Primary_key.t -> 'r change
  4. | Add_unique_key : 'r Unique_key.t -> 'r change
  5. | Create_index : 'r Index.t -> 'r change
  6. | Drop_column : Col.name -> 'r change
  7. | Drop_foreign_key : 'a Foreign_key.t -> 'r change
  8. | Drop_index : Index.name -> 'r change
  9. | Drop_primary_key : 'r change
  10. | Drop_unique_key : 'a Unique_key.t -> 'r change
  11. | Set_column_default : 'r Col.def -> 'r change
    (*

    The given column default changed.

    *)
  12. | Set_column_type : 'r Col.def * 'b Col.def -> '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.def * 'r Col.def 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.

Dependencies

val sort : def list -> (def list, def 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.