Module z_db

Interface to database, uses database definition from Context.

Copyright © 2009-2021 Marc Worrell

Authors: Marc Worrell (marc@worrell.nl).

Description

Interface to database, uses database definition from Context

Data Types

id()

id() = pos_integer()

parameter()

parameter() = tuple() | calendar:datetime() | atom() | string() | binary() | integer() | boolean() | float() | list()

parameters()

parameters() = [parameter()]

prop_key()

prop_key() = binary() | atom()

props()

props() = proplists:proplist() | props_map()

props_map()

props_map() = #{prop_key() => term()}

qmap_option()

qmap_option() = {keys, binary | atom} | {timeout, non_neg_integer()}

qmap_options()

qmap_options() = [qmap_option()]

query_error()

query_error() = nodb | enoent | epgsql:query_error() | term()

query_result()

query_result() = {ok, Columns::list(), Rows::list()} | {ok, Count::non_neg_integer(), Columns::list(), Rows::list()} | {ok, Count::non_neg_integer()} | {error, term()}

query_timeout()

query_timeout() = integer()

schema_name()

schema_name() = default | atom() | string()

sql()

sql() = string() | iodata()

table_name()

table_name() = atom() | string()

transaction_fun()

transaction_fun() = fun((z:context()) -> term())

Function Index

assert_table_name/1Check if a name is a valid SQL table name.
assoc/2Return property lists of the results of a query on the database in the Context.
assoc/3
assoc/4
assoc_props/2
assoc_props/3
assoc_props/4
assoc_props_row/2
assoc_props_row/3
assoc_row/2
assoc_row/3
column/3
column_names/2Return a list with the column names of a table.
column_names_bin/2
columns/2Return a property list with all columns of the table.
create_table/3Ensure that a table with the given columns exists, alter any existing table to add, modify or drop columns.
delete/3Delete a row from a table, the row must have a column with the name 'id'.
drop_schema/1
drop_table/2Make sure that a table is dropped, only when the table exists.
ensure_database/2
ensure_schema/2
equery/2
equery/3
equery/4
flush/1Flush all cached information about the database.
get_current_props/3
has_connection/1Check if we have database connection up and runnng.
insert/2Insert a new row in a table, use only default values.
insert/3Insert a row, setting the fields to the props.
prepare_cols/2Check if all cols are valid columns in the target table, move unknown properties to the props column (if exists).
prepare_database/1Create database and schema if they do not yet exist.
q/2
q/3
q/4
q1/2
q1/3
q1/4
q_row/2
q_row/3
qmap/2
qmap/3
qmap/4
qmap_props/2
qmap_props/3
qmap_props/4
qmap_props_row/2
qmap_props_row/3
qmap_props_row/4
qmap_row/2
qmap_row/3
qmap_row/4
quoted_table_name/1
schema_exists_conn/2Check whether schema exists.
select/3Read a row from a table, the row must have a column with the name 'id'.
squery/2
squery/3
table_exists/2Check the information schema if a certain table exists in the context database.
transaction/2Perform a function inside a transaction, do a rollback on exceptions.
transaction/3Perform a transaction with extra options.
transaction_clear/1Clear any transaction in the context, useful when starting a thread with this context.
update/4Update a row in a table, merging the props list with any new props values.
update_sequence/3Update the sequence of the ids in the table.

Function Details

assert_table_name/1

assert_table_name(A::table_name()) -> true

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

assoc/2

assoc(Sql::sql(), Context::z:context()) -> [proplists:proplist()]

Return property lists of the results of a query on the database in the Context

assoc/3

assoc(Sql::sql(), Parameters::list(), Context::z:context()) -> [proplists:proplist()]

assoc/4

assoc(Sql::sql(), Parameters::list(), Context::z:context(), Timeout::integer()) -> [proplists:proplist()]

assoc_props/2

assoc_props(Sql::sql(), Context::z:context()) -> [proplists:proplist()]

assoc_props/3

assoc_props(Sql::sql(), Parameters::list(), Context::z:context()) -> [proplists:proplist()]

assoc_props/4

assoc_props(Sql::sql(), Parameters::list(), Context::z:context(), Timeout::integer()) -> [proplists:proplist()]

assoc_props_row/2

assoc_props_row(Sql::string(), Context::z:context()) -> proplists:proplist() | undefined

assoc_props_row/3

assoc_props_row(Sql::string(), Parameters::list(), Context::z:context()) -> proplists:proplist() | undefined

assoc_row/2

assoc_row(Sql::string(), Context::z:context()) -> proplists:proplist()

assoc_row/3

assoc_row(Sql::string(), Parameters::parameters(), Context::z:context()) -> proplists:proplist() | undefined

column/3

column(Table::table_name(), Column::atom() | string(), Context::z:context()) -> {ok, #column_def{}} | {error, enoent}

column_names/2

column_names(Table::table_name(), Context::z:context()) -> [atom()]

Return a list with the column names of a table. The names are sorted.

column_names_bin/2

column_names_bin(Table::table_name(), Context::z:context()) -> [binary()]

columns/2

columns(Table::table_name(), Context::z:context()) -> [#column_def{}]

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

create_table/3

create_table(Table::table_name(), Cols::list(), Context::z:context()) -> ok

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

delete/3

delete(Table::table_name(), Id::integer(), Context::z:context()) -> {ok, RowsDeleted::non_neg_integer()} | {error, term()}

Delete a row from a table, the row must have a column with the name 'id'

drop_schema/1

drop_schema(Context) -> any()

drop_table/2

drop_table(Table::table_name(), Context::z:context()) -> ok

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

ensure_database/2

ensure_database(Site, Options) -> any()

ensure_schema/2

ensure_schema(Site, Options) -> any()

equery/2

equery(Sql, Context) -> any()

equery/3

equery(Sql, Parameters, Context) -> any()

equery/4

equery(Sql::sql(), Parameters::parameters(), Context::z:context(), Timeout::integer()) -> query_result()

flush/1

flush(Context) -> any()

Flush all cached information about the database.

get_current_props/3

get_current_props(Table, Id, Context) -> any()

has_connection/1

has_connection(Site::z:context() | atom()) -> boolean()

Check if we have database connection up and runnng

insert/2

insert(Table::table_name(), Context::z:context()) -> {ok, pos_integer() | undefined} | {error, term()}

Insert a new row in a table, use only default values.

insert/3

insert(Table::table_name(), Parameters::props(), Context::z:context()) -> {ok, integer() | undefined} | {error, term()}

Insert a row, setting the fields to the props. Unknown columns are serialized in the props column. When the table has an 'id' column then the new id is returned.

prepare_cols/2

prepare_cols(Cols, Props) -> any()

Check if all cols are valid columns in the target table, move unknown properties to the props column (if exists).

prepare_database/1

prepare_database(Context::z:context()) -> ok | {error, term()}

Create database and schema if they do not yet exist

q/2

q(Sql, Context) -> any()

q/3

q(Sql, Parameters, Context) -> any()

q/4

q(Sql, Parameters, Context, Timeout) -> any()

q1/2

q1(Sql, Context) -> any()

q1/3

q1(Sql, Parameters, Context) -> any()

q1/4

q1(Sql::sql(), Parameters::parameters(), Context::#context{}, Timeout::pos_integer()) -> term() | undefined

q_row/2

q_row(Sql, Context) -> any()

q_row/3

q_row(Sql, Args, Context) -> any()

qmap/2

qmap(Sql::sql(), Context::z:context()) -> {ok, [map()]} | {error, query_error()}

qmap/3

qmap(Sql::sql(), Args::parameters(), Context::z:context()) -> {ok, [map()]} | {error, query_error()}

qmap/4

qmap(Sql::sql(), Args::parameters(), Options::qmap_options(), Context::z:context()) -> {ok, [map()]} | {error, query_error()}

qmap_props/2

qmap_props(Sql::sql(), Context::z:context()) -> {ok, [map()]} | {error, query_error()}

qmap_props/3

qmap_props(Sql::sql(), Args::parameters(), Context::z:context()) -> {ok, [map()]} | {error, query_error()}

qmap_props/4

qmap_props(Sql::sql(), Args::parameters(), Options::list(), Context::z:context()) -> {ok, [map()]} | {error, query_error()}

qmap_props_row/2

qmap_props_row(Sql::sql(), Context::z:context()) -> {ok, map()} | {error, query_error()}

qmap_props_row/3

qmap_props_row(Sql::sql(), Args::parameters(), Context::z:context()) -> {ok, map()} | {error, query_error()}

qmap_props_row/4

qmap_props_row(Sql::sql(), Args::parameters(), Options::qmap_options(), Context::z:context()) -> {ok, map()} | {error, query_error()}

qmap_row/2

qmap_row(Sql::sql(), Context::z:context()) -> {ok, map()} | {error, query_error()}

qmap_row/3

qmap_row(Sql::sql(), Args::parameters(), Context::z:context()) -> {ok, map()} | {error, query_error()}

qmap_row/4

qmap_row(Sql::sql(), Args::parameters(), Options::qmap_options(), Context::z:context()) -> {ok, map()} | {error, query_error()}

quoted_table_name/1

quoted_table_name(TableName::table_name()) -> {default | string(), string(), string()}

schema_exists_conn/2

schema_exists_conn(Connection::epgsql:connection(), Schema::string()) -> boolean()

Check whether schema exists

select/3

select(Table::table_name(), Id::any(), Context::z:context()) -> {ok, Row::map()} | {error, term()}

Read a row from a table, the row must have a column with the name 'id'. The props column contents is merged with the other properties returned.

squery/2

squery(Sql, Context) -> any()

squery/3

squery(Sql, Context, Timeout) -> any()

table_exists/2

table_exists(Table::table_name(), Context::z:context()) -> boolean()

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

transaction/2

transaction(Function::transaction_fun(), Context::z:context()) -> any() | {error, term()}

Perform a function inside a transaction, do a rollback on exceptions

transaction/3

transaction(Function::transaction_fun(), Options::list(), Context::z:context()) -> any() | {error, term()}

Perform a transaction with extra options. Default retry on deadlock

transaction_clear/1

transaction_clear(Context) -> any()

Clear any transaction in the context, useful when starting a thread with this context.

update/4

update(Table::table_name(), Id::id(), Parameters::props(), Context::z:context()) -> {ok, RowsUpdated::integer()} | {error, term()}

Update a row in a table, merging the props list with any new props values

update_sequence/3

update_sequence(Table::table_name(), Ids::[integer()], Context::z:context()) -> any()

Update the sequence of the ids in the table. They will be renumbered according to their position in the id list.


Generated by EDoc