View Source sql_bridge (sql_bridge v0.7.1)

Summary

Functions

Establishes a connection to the appropriate database.

Establishes a connection to the named database.

Checks the process dictionary for an active database connection associated with this process. If none, then look it up from configuration and store it in the process dictionary. Note, this function does not actually establish a connection, only returns the name of the database that either is associated with the process or that should be associated with the process based on some criteria (for example, checking host headers)

Stores the database name in the process dictionary

Query from the specified Database pool (Db) This will connect to the specified Database Pool Type must be atoms: proplist, dict, list, or tuple Type can also be atom 'insert' in which case, it'll return the insert value

Same as db_q/3, but ParamList is safely inserted into the Query

Decodes a base64 string into the relevant erlang term.

Deletes rows from Table where the primary_key = ID

Deletes from Table where KeyField = ID

Get the first record as a dict

Converts a dict to a proplist, filtering out any fields not found in AcceptableFields

Same as q/1, but returns a list of dicts.

Same as q/2, but returns a list of dicts

Encodes an erlang term into a base64 string which can be safely inserted into a text field

Safely encodes text for insertion into a query. Replaces the atoms 'true' and 'false' with <<"1">> and <<"0">> respectively.

Takes a list of items and encodes them for SQL then returns a comma-separated list of them.

Returns true if Table has a record representing Key Value IDValue

Returns true if Table has a record where KeyField = IDValue

Get First Field of First record

Return a list of the values of the first field from the query.

This does the same as above, but determines the primary_key

Retrieves the value of Field from Table where the value of IDField == IDValue (e.g.: Select 'Field' from 'Table' where 'IDField'='IDValue'). If the query returns more than one record, only the first record's value is returned.

Removes from Proplist any fields that aren't found in the table 'Table'

Get the first record as a list of values.

Joins a list of iolists together by a delimiter.

Generates a SQL 'LIMIT' clause based on the PerPage Criteria and the Page, calculating the offset according to the provided values. Useful when building something that breaks a list into pages in an interface.

Checks the configuration for how we determine the database we're using and returns the database.

Get the first record as a map

Same as d1/, but returns a list of maps

Same as d1/, but returns a list of maps

Get first record as a proplist

Inserts a proplist into the table.

Same as q/1, but returns a list of proplists

Same as q/2, but returns a list of proplists

Updates a row from the proplist based on the primary_key in the Table

Update a row from proplist based on the Keyfield Keyfield on provided Table

Run the SQL query Q and return a list of lists, with each inner list representing one record in the return set.

Run the SQL query Q, with the the values of ParamList safely injected into the query replacing any instances of question marks (?), with each inner list representing one record in the return set (same as q/1).

Existance query, just returns true if the query Q returns anything other than an empty set.

A special Query function just for inserting. Inserts the record(s) and returns the insert_id

starts the actual database driver, if necessary

Get the first record as a tuple.

Same as q/1, but returns a list of tuples

Same as q/2, but returns a list of tuples

Types

affected_rows()

-type affected_rows() :: integer().

db()

-type db() :: atom().

field()

-type field() :: string() | atom().

field_type()

-type field_type() :: {uuid, undefined} | {text, string_length()} | {integer, integer_range()}.

insert_id()

-type insert_id() :: term().

integer_range()

-type integer_range() :: {integer(), integer()}.

proplist()

-type proplist() :: [{atom(), value()}].

proplist_or_map()

-type proplist_or_map() :: tuple() | proplist() | map().

return_type()

-type return_type() :: dict | list | proplist | tuple | insert | update | map.

return_value()

-type return_value() :: insert_id() | affected_rows() | [list() | tuple() | t_dict() | proplist()].

sql()

-type sql() :: iolist().

string_length()

-type string_length() :: integer().

t_dict()

-type t_dict() :: dict:dict().

table()

-type table() :: string() | atom().

value()

-type value() :: string() | binary() | atom() | integer() | float().

Functions

build_alias()

build_stringify()

commit()

-spec commit() -> ok.

connect()

-spec connect() -> db().

Establishes a connection to the appropriate database.

connect(DB)

-spec connect(db()) -> db().

Establishes a connection to the named database.

db()

-spec db() -> db().

Checks the process dictionary for an active database connection associated with this process. If none, then look it up from configuration and store it in the process dictionary. Note, this function does not actually establish a connection, only returns the name of the database that either is associated with the process or that should be associated with the process based on some criteria (for example, checking host headers)

db(DB)

-spec db(db()) -> db().

Stores the database name in the process dictionary

db_q(Type, Db, Q)

-spec db_q(Type :: return_type(), Db :: db(), Q :: sql()) ->
              insert_id() | affected_rows() | [list() | t_dict() | tuple() | proplist()].

Query from the specified Database pool (Db) This will connect to the specified Database Pool Type must be atoms: proplist, dict, list, or tuple Type can also be atom 'insert' in which case, it'll return the insert value

db_q(Type, Db, Q, ParamList)

-spec db_q(Type :: return_type(), Db :: db(), Q :: sql(), ParamList :: [value()]) ->
              insert_id() | affected_rows() | [list() | t_dict() | tuple() | proplist()].

Same as db_q/3, but ParamList is safely inserted into the Query

db_q/5

decode64(T)

-spec decode64(T :: any()) -> term().

Decodes a base64 string into the relevant erlang term.

delete(Table, ID)

-spec delete(Table :: table(), ID :: value()) -> affected_rows().

Deletes rows from Table where the primary_key = ID

delete(Table, KeyField, ID)

-spec delete(Table :: table(), KeyField :: field(), ID :: value()) -> affected_rows().

Deletes from Table where KeyField = ID

dfr(Q)

-spec dfr(Q :: sql()) -> t_dict() | not_found.

Get the first record as a dict

dfr(Q, ParamList)

-spec dfr(Q :: sql(), ParamList :: [value()]) -> t_dict() | not_found.

dict(Q)

dict(Q, P)

dict_to_proplist(SrcDict, AcceptableFields)

-spec dict_to_proplist(SrcDict :: t_dict(), AcceptableFields :: [field()]) -> proplist().

Converts a dict to a proplist, filtering out any fields not found in AcceptableFields

dicts(Q)

dicts(Q, P)

disable_logging()

dq(Q)

-spec dq(Q :: sql()) -> [t_dict()].

Same as q/1, but returns a list of dicts.

dq(Q, ParamList)

-spec dq(Q :: sql(), ParamList :: [value()]) -> [t_dict()].

Same as q/2, but returns a list of dicts

enable_logging()

encode64(T)

-spec encode64(T :: any()) -> string().

Encodes an erlang term into a base64 string which can be safely inserted into a text field

encode(V)

-spec encode(V :: any()) -> binary().

Safely encodes text for insertion into a query. Replaces the atoms 'true' and 'false' with <<"1">> and <<"0">> respectively.

encode_list(List)

-spec encode_list(List :: [value()]) -> iolist().

Takes a list of items and encodes them for SQL then returns a comma-separated list of them.

ensure_proplist/1

exists(Table, IDValue)

-spec exists(Table :: table(), IDValue :: value()) -> boolean().

Returns true if Table has a record representing Key Value IDValue

exists(Table, KeyField, IDValue)

-spec exists(Table :: table(), KeyField :: field(), IDValue :: value()) -> boolean().

Returns true if Table has a record where KeyField = IDValue

fffr(Q)

-spec fffr(Q :: sql()) -> value() | not_found.

fffr(Q, ParamList)

-spec fffr(Q :: sql(), ParamList :: [value()]) -> value() | not_found.

Get First Field of First record

ffl(Q)

-spec ffl(Q :: sql()) -> [value()].

ffl(Q, ParamList)

-spec ffl(Q :: sql(), ParamList :: [value()]) -> [value()].

Return a list of the values of the first field from the query.

field(Table, Field, Value)

-spec field(Table :: table(), Field :: field(), Value :: value()) -> value() | not_found.

This does the same as above, but determines the primary_key

field(Table, Field, IDField, IDValue)

-spec field(Table :: table(), Field :: field(), IDField :: field(), IDValue :: value()) ->
               value() | not_found.

Retrieves the value of Field from Table where the value of IDField == IDValue (e.g.: Select 'Field' from 'Table' where 'IDField'='IDValue'). If the query returns more than one record, only the first record's value is returned.

field_exists(Table0, Field)

field_type(Table0, Field)

-spec field_type(table(), field()) -> field_type().

fields(Table)

-spec fields(Table :: table()) -> [atom()].

filter_fields(Table, PropList)

-spec filter_fields(Table :: table(), PropList :: proplist()) -> proplist().

Removes from Proplist any fields that aren't found in the table 'Table'

fr(Q)

-spec fr(Q :: sql()) -> list() | not_found.

fr(Q, ParamList)

-spec fr(Q :: sql(), ParamList :: [value()]) -> list() | not_found.

Get the first record as a list of values.

insert(Table, Obj)

iolist_join(List, Delimiter)

-spec iolist_join(List :: [iolist()], Delimiter :: iolist()) -> iolist().

Joins a list of iolists together by a delimiter.

is_auto_increment(Table0, Field)

limit_clause(PerPage, Page)

-spec limit_clause(PerPage :: integer(), Page :: integer()) -> iolist().

Generates a SQL 'LIMIT' clause based on the PerPage Criteria and the Page, calculating the offset according to the provided values. Useful when building something that breaks a list into pages in an interface.

list(Q)

list(Q, P)

lists(Q)

lists(Q, P)

log(Time, DB, SQL, Params)

log_for_time(Secs)

lookup()

-spec lookup() -> db().

Checks the configuration for how we determine the database we're using and returns the database.

map(Q)

map(Q, P)

maps(Q)

maps(Q, P)

mfr(Q)

-spec mfr(Q :: sql()) -> map() | not_found.

mfr(Q, ParamList)

-spec mfr(Q :: sql(), ParamList :: [value()]) -> map() | not_found.

Get the first record as a map

mq(Q)

-spec mq(Q :: sql()) -> [map()].

Same as d1/, but returns a list of maps

mq(Q, ParamList)

-spec mq(Q :: sql(), ParamList :: [value()]) -> [map()].

Same as d1/, but returns a list of maps

offset(PerPage, Page)

-spec offset(PerPage :: integer(), Page :: integer()) -> Offset :: integer().

pl(Table, Data)

-spec pl(Table :: table(), Data :: proplist_or_map()) -> insert_id().

pl(Table, KeyField, Data)

-spec pl(Table :: table(), KeyField :: field(), Data :: proplist_or_map()) -> insert_id().

plfr(Q)

-spec plfr(Q :: sql()) -> proplist() | not_found.

plfr(Q, ParamList)

-spec plfr(Q :: sql(), ParamList :: [value()]) -> proplist() | not_found.

Get first record as a proplist

pli(Table, Data)

-spec pli(Table :: table(), Data :: proplist_or_map()) -> insert_id().

Inserts a proplist into the table.

plq(Q)

-spec plq(Q :: sql()) -> [proplist()].

Same as q/1, but returns a list of proplists

plq(Q, ParamList)

-spec plq(Q :: sql(), ParamList :: [value()]) -> [proplist()].

Same as q/2, but returns a list of proplists

plu(Table, PropList)

-spec plu(Table :: table(), PropList :: proplist_or_map()) -> affected_rows().

Updates a row from the proplist based on the primary_key in the Table

plu(Table, KeyField, PropList)

-spec plu(Table :: table(), KeyField :: field(), PropList :: proplist()) -> affected_rows().

Update a row from proplist based on the Keyfield Keyfield on provided Table

primary_key(Table0)

proplist(Q)

proplist(Q, P)

proplists(Q)

proplists(Q, P)

q(Q)

-spec q(Q :: sql()) -> [list()].

Run the SQL query Q and return a list of lists, with each inner list representing one record in the return set.

q(Q, ParamList)

-spec q(Q :: sql(), ParamList :: [value()]) -> [list()].

Run the SQL query Q, with the the values of ParamList safely injected into the query replacing any instances of question marks (?), with each inner list representing one record in the return set (same as q/1).

q_prep(Q, ParamList)

qexists(Q)

-spec qexists(Q :: sql()) -> boolean().

Existance query, just returns true if the query Q returns anything other than an empty set.

qexists(Q, ParamList)

-spec qexists(Q :: sql(), ParamList :: [value()]) -> boolean().

qi(Q)

-spec qi(Q :: sql()) -> insert_id().

A special Query function just for inserting. Inserts the record(s) and returns the insert_id

qi(Q, ParamList)

-spec qi(Q :: sql(), ParamList :: [value()]) -> insert_id().

qinsert(Q)

qinsert(Q, P)

qu(Q)

-spec qu(Q :: sql()) -> affected_rows().

qu(Q, ParamList)

-spec qu(Q :: sql(), ParamList :: [value()]) -> affected_rows().

qupdate(Q)

qupdate(Q, P)

remove_wrapping_quotes/1

rollback()

sanitize/1

sanitize_params(L)

save(Table, Data)

-spec save(Table :: table(), Data :: proplist_or_map()) -> insert_id().

save(Table, KeyField, Data0)

-spec save(Table :: table(), KeyField :: field(), Data0 :: proplist_or_map()) -> insert_id().

save_/2

save_/3

save_record(Table, Record, FieldMap)

-spec save_record(Table :: table(), Record :: tuple(), FieldMap :: [atom()]) -> insert_id().

save_record(Table, KeyField, Record, FieldMap)

-spec save_record(Table :: table(), KeyField :: field(), Record :: tuple(), FieldMap :: [atom()]) ->
                     insert_id().

start()

-spec start() -> ok.

starts the actual database driver, if necessary

start_trans()

-spec start_trans() -> ok.

table_and_db/1

table_fields(Table)

-spec table_fields(Table :: table()) -> [atom()].

tfr(Q)

-spec tfr(Q :: sql()) -> tuple() | not_found.

Get the first record as a tuple.

tfr(Q, ParamList)

-spec tfr(Q :: sql(), ParamList :: [value()]) -> tuple() | not_found.

to_bool/1

tq(Q)

-spec tq(Q :: sql()) -> [tuple()].

Same as q/1, but returns a list of tuples

tq(Q, ParamList)

-spec tq(Q :: sql(), ParamList :: [value()]) -> [tuple()].

Same as q/2, but returns a list of tuples

trans(Fun)

-spec trans(Fun :: fun()) -> ok.

tuple(Q)

tuple(Q, P)

tuples(Q)

tuples(Q, P)

update(Table, Obj)

update(Table, KeyField, Obj)

wrap_field(V)