z_db_table (zotonic_core v1.0.0-rc.17)

Functions to create, update or drop database tables.

Summary

Functions

Alter a table so that it matches the given column definitions. If the table doesn't exist then it is created. RESTRICTIONS: does NOT change the primary key and unique constraint of existing columns. If the table doesn't exist then it is created. Be careful when adding columns that are not nullable, if the table contains data then adding those columns will fail.

Check if a name is a valid SQL database name. Crashes when invalid

Check if a name is a valid SQL schema name. Crashes when invalid

Check if a name is a valid SQL table name. Crashes when invalid

Fetch the column definition for the given column.

Fetch the column definition for the given column.

Return a property list with all columns of the table. (example: [{id,int4,modifier},...])

Ensure that a table with the given columns exists, if the table exists then add, modify or drop columns. The 'id' (with type serial) column _must_ be defined when creating the table.

Make sure that a table is dropped, only when the table exists

Quote a table name so that it is safe to use in SQL queries.

Check the information schema if a certain table exists in the context database.

Functions

alter_table(Name, Cols, Context)

-spec alter_table(Name, Cols, Context) -> ok | {error, Reason}
                     when
                         Name :: z_db:table_name(),
                         Cols ::
                             [#column_def{name :: atom(),
                                          type :: string() | binary(),
                                          length :: non_neg_integer() | undefined,
                                          is_nullable :: boolean(),
                                          is_array :: boolean(),
                                          default :: term(),
                                          primary_key :: boolean(),
                                          unique :: boolean()}],
                         Context :: z:context(),
                         Reason :: term().

Alter a table so that it matches the given column definitions. If the table doesn't exist then it is created. RESTRICTIONS: does NOT change the primary key and unique constraint of existing columns. If the table doesn't exist then it is created. Be careful when adding columns that are not nullable, if the table contains data then adding those columns will fail.

assert_database_name(T)

-spec assert_database_name(string()) -> true.

Check if a name is a valid SQL database name. Crashes when invalid

assert_schema_name(T)

-spec assert_schema_name(string()) -> true.

Check if a name is a valid SQL schema name. Crashes when invalid

assert_table_name(A)

-spec assert_table_name(z_db:table_name()) -> true.

Check if a name is a valid SQL table name. Crashes when invalid

column(Table, Column, Context)

-spec column(z_db:table_name(), z_db:column_name(), z:context()) ->
                {ok,
                 #column_def{name :: atom(),
                             type :: string() | binary(),
                             length :: non_neg_integer() | undefined,
                             is_nullable :: boolean(),
                             is_array :: boolean(),
                             default :: term(),
                             primary_key :: boolean(),
                             unique :: boolean()}} |
                {error, enoent}.

Fetch the column definition for the given column.

column(Schema, Table, Column0, Context)

-spec column(z_db:schema_name(), z_db:table_name(), z_db:column_name(), z:context()) ->
                {ok,
                 #column_def{name :: atom(),
                             type :: string() | binary(),
                             length :: non_neg_integer() | undefined,
                             is_nullable :: boolean(),
                             is_array :: boolean(),
                             default :: term(),
                             primary_key :: boolean(),
                             unique :: boolean()}} |
                {error, enoent}.

Fetch the column definition for the given column.

columns(Table, Context)

-spec columns(z_db:table_name(), z:context()) ->
                 [#column_def{name :: atom(),
                              type :: string() | binary(),
                              length :: non_neg_integer() | undefined,
                              is_nullable :: boolean(),
                              is_array :: boolean(),
                              default :: term(),
                              primary_key :: boolean(),
                              unique :: boolean()}].

Return a property list with all columns of the table. (example: [{id,int4,modifier},...])

columns(Schema, Table, Context)

-spec columns(z_db:schema_name(), z_db:table_name(), z:context()) ->
                 [#column_def{name :: atom(),
                              type :: string() | binary(),
                              length :: non_neg_integer() | undefined,
                              is_nullable :: boolean(),
                              is_array :: boolean(),
                              default :: term(),
                              primary_key :: boolean(),
                              unique :: boolean()}].

create_table(Name, Cols, Context)

-spec create_table(Name, Cols, Context) -> ok | {error, Reason}
                      when
                          Name :: z_db:table_name(),
                          Cols ::
                              [#column_def{name :: atom(),
                                           type :: string() | binary(),
                                           length :: non_neg_integer() | undefined,
                                           is_nullable :: boolean(),
                                           is_array :: boolean(),
                                           default :: term(),
                                           primary_key :: boolean(),
                                           unique :: boolean()}],
                          Context :: z:context(),
                          Reason :: term().

Ensure that a table with the given columns exists, if the table exists then add, modify or drop columns. The 'id' (with type serial) column _must_ be defined when creating the table.

drop_table(Table, Context)

-spec drop_table(z_db:table_name(), z:context()) -> ok.

Make sure that a table is dropped, only when the table exists

quoted_table_name(TableName)

-spec quoted_table_name(z_db:table_name()) -> {default | string(), string(), string()}.

Quote a table name so that it is safe to use in SQL queries.

table_exists(Table, Context)

-spec table_exists(z_db:table_name(), z:context()) -> boolean().

Check the information schema if a certain table exists in the context database.

table_exists(Schema, Table, Context)

-spec table_exists(z_db:schema_name(), z_db:table_name(), z:context()) -> boolean().