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
-type affected_rows() :: integer().
-type db() :: atom().
-type field_type() :: {uuid, undefined} | {text, string_length()} | {integer, integer_range()}.
-type insert_id() :: term().
-type return_type() :: dict | list | proplist | tuple | insert | update | map.
-type sql() :: iolist().
-type string_length() :: integer().
-type t_dict() :: dict:dict().
Functions
-spec commit() -> ok.
-spec connect() -> db().
Establishes a connection to the appropriate database.
Establishes a connection to the named database.
-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)
Stores the database name in the process dictionary
-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
-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
Decodes a base64 string into the relevant erlang term.
-spec delete(Table :: table(), ID :: value()) -> affected_rows().
Deletes rows from Table where the primary_key = ID
-spec delete(Table :: table(), KeyField :: field(), ID :: value()) -> affected_rows().
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
-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.
-spec field_type(table(), field()) -> field_type().
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.
-spec lookup() -> db().
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
-spec pl(Table :: table(), Data :: proplist_or_map()) -> insert_id().
-spec pl(Table :: table(), KeyField :: field(), Data :: proplist_or_map()) -> insert_id().
Get first record as a proplist
-spec pli(Table :: table(), Data :: proplist_or_map()) -> insert_id().
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
-spec plu(Table :: table(), PropList :: proplist_or_map()) -> affected_rows().
Updates a row from the proplist based on the primary_key in the Table
-spec plu(Table :: table(), KeyField :: field(), PropList :: proplist()) -> affected_rows().
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
-spec qu(Q :: sql()) -> affected_rows().
-spec qu(Q :: sql(), ParamList :: [value()]) -> affected_rows().
-spec save(Table :: table(), Data :: proplist_or_map()) -> insert_id().
-spec save(Table :: table(), KeyField :: field(), Data0 :: proplist_or_map()) -> insert_id().
-spec start() -> ok.
starts the actual database driver, if necessary
-spec start_trans() -> ok.
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
-spec trans(Fun :: fun()) -> ok.