Rel.Table
Table descriptions.
This module defines a type to describe tables, their indices and constraints.
type 'r primary_key = 'r Col.v list
The type for table primary keys. The columns that make up the primary key.
The type for table unique keys. See Unique_key
.
The type for table foreign keys. See Foreign_key
.
The type for table indices. See Index
.
The type for extensible table parameters. Warning this may be removed in the future.
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.
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
.
module Unique_key : sig ... end
Unique keys.
val unique_key : 'r Col.v list -> 'r unique_key
unique_key s
is Unique_key.v
.
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.
type 'r change =
| Add_column_after : 'r Col.v * 'r Col.v option -> 'r change
If the second column is None, in the first position.
*)| Add_foreign_key : 'r foreign_key -> 'r change
| Add_primary_key : 'r primary_key -> 'r change
| Add_unique_key : 'r unique_key -> 'r change
| Create_index : 'r index -> 'r change
| Drop_column : Col.name -> 'r change
| Drop_foreign_key : 'a foreign_key -> 'r change
| Drop_index : Index.name -> 'r change
| Drop_primary_key : 'r change
| Drop_unique_key : 'a unique_key -> 'r change
| Set_column_default : 'r Col.v -> 'r change
The given column default changed.
*)| 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.
*)| 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.
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.
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.