Copyright © Yariv Sadan 2006-2007
Authors: Yariv Sadan (yarivvv@gmail.com) (http://yarivsblog.com).
This module implements the MySQL driver for ErlyDB.
This is an internal ErlyDB module that you normally shouldn't have to use directly. For most situations, all you have to know about this module is the options you can pass tostart/1, which
is called by erlydb:start/2.
esql() = {esql, term()}
An ErlSQL expression.
option() = {pool_id, atom()} | {allow_unsafe_statements, boolean()}
options() = [option()]
statement() = esql() | binary() | string()
| connect/5 | Call connect/7 with Port set to 3306 and Reconnect set to 'true'. |
| connect/7 | Add a connection to the connection pool. |
| connect/8 | Add a connection to the connection pool, with encoding specified. |
| connect/9 | Add a connection to the connection pool, with encoding specified. |
| execute/2 | Execute a statement that was previously prepared with prepare/2. |
| execute/3 | Execute a prepared statement with the list of parameters. |
| execute_select/2 | Execute a prepared statement and return the result as the select() function. |
| execute_select/3 | Execute a prepared statement with the list of parameters and return the result as the select() function. |
| execute_update/2 | Execute a prepared statement and return the result as the the update() function. |
| execute_update/3 | Execute a prepared statement with the list of parameters and and return the result as the the update() function. |
| get_default_pool_id/0 | Get the default connection pool name for the driver. |
| get_last_insert_id/2 | Get the id of the last inserted record. |
| get_metadata/1 | Get the table names and fields for the database. |
| prepare/2 | Register a prepared statement with the MySQL dispatcher. |
| q/1 | Execute a statement against the MySQL driver with the default options. |
| q/2 | Execute a statement directly against the MySQL driver. |
| q2/1 | Execute a (binary or string) statement against the MySQL driver using the default options. |
| q2/2 | Execute a (binary or string) statement against the MySQL driver. |
| select/2 | Execute a raw SELECT statement. |
| select_as/3 | Execute a SELECT statements for records belonging to the given module, returning all rows with additional data to support higher-level ErlyDB features. |
| start/1 | Start the MySQL dispatcher using the options property list. |
| start_link/1 | This function is similar to start/1, only it calls
mysql:start_link() instead of mysql:start(). |
| transaction/2 | Execute a group of statements in a transaction. |
| update/2 | Execute a DELETE or UPDATE statement. |
connect(PoolId::atom(), Hostname::string(), Username::string(), Password::string(), Database::string()) -> ok
Call connect/7 with Port set to 3306 and Reconnect set to 'true'. If the connection is lost, reconnection is attempted. The connection process is linked to the calling process.
connect(PoolId::atom(), Hostname::string, Port::integer(), Username::string(), Password::string(), Database::string(), Reconnect::boolean()) -> ok
Add a connection to the connection pool. If PoolId is 'undefined', the default pool, 'erlydb_mysql', is used. The connection process is linked to the calling process.
connect(PoolId::atom(), Hostname::string, Port::integer(), Username::string(), Password::string(), Database::string(), Encoding, Reconnect::boolean()) -> ok
Add a connection to the connection pool, with encoding specified. The connection process is linked to the calling process.
connect(PoolId::atom(), Hostname::string, Port::integer(), Username::string(), Password::string(), Database::string(), Encoding::string(), Reconnect::boolean(), LinkConnection::bool()) -> ok
Add a connection to the connection pool, with encoding specified. If LinkConnection == false, the connection will not be linked to the current process.
execute(Name::atom(), Options::options()) -> mysql_result()
Execute a statement that was previously prepared with prepare/2.
execute(Name::atom(), Params::[term()], Options::options()) -> mysql_result()
Execute a prepared statement with the list of parameters.
execute_select(Name::atom(), Options::options()) -> {ok, [row]} | {error, Err}
Execute a prepared statement and return the result as the select() function.
execute_select(Name::atom(), Params::[term()], Options::options()) -> {ok, [Row::tuple()]} | {error, Err}
Execute a prepared statement with the list of parameters and return the result as the select() function.
execute_update(Name::atom(), Options::options()) -> {ok, NumUpdated::integer()} | {error, Err}
Execute a prepared statement and return the result as the the update() function.
execute_update(Name::atom(), Params::[term()], Options::options()) -> {ok, NumUpdated::integer()} | {error, Err}
Execute a prepared statement with the list of parameters and and return the result as the the update() function.
get_default_pool_id() -> atom()
Get the default connection pool name for the driver.
get_last_insert_id(Table::undefined, Options::options()) -> term()
Get the id of the last inserted record.
get_metadata(Options::options()) -> gb_trees()
Get the table names and fields for the database.
prepare(Name::atom(), Stmt::iolist()) -> ok | {error, Err}
Register a prepared statement with the MySQL dispatcher. If the dispatcher has a prepared statement with the same name, the old statement is overwritten and the statement's version is incremented.
q(Statement::statement()) -> mysql_result()
Execute a statement against the MySQL driver with the default options. The connection default pool name ('erlydb_mysql') is used.
q(Statement::statement(), Options::options()) -> mysql_result() | exit({unsafe_statement, Statement})
Execute a statement directly against the MySQL driver. If Options contains the value {allow_unsafe_statements, true}, binary and string queries as well as ErlSQL queries with binary and/or string expressions are accepted. Otherwise, this function exits.
q2(Statement::string() | binary()) -> mysql_result()
Execute a (binary or string) statement against the MySQL driver using the default options. ErlyDB doesn't use this function, but it's sometimes convenient for testing.
q2(Statement::string() | binary(), Options::options()) -> mysql_result()
Execute a (binary or string) statement against the MySQL driver. ErlyDB doesn't use this function, but it's sometimes convenient for testing.
select(Statement::statement(), Options::options()) -> {ok, Rows::list()} | {error, Error}
Execute a raw SELECT statement.
select_as(Module::atom(), Statement::statement(), Options::options()) -> {ok, Rows} | {error, Error}
Execute a SELECT statements for records belonging to the given module, returning all rows with additional data to support higher-level ErlyDB features.
start(StartOptions::proplist()) -> ok | {error, Error}
Start the MySQL dispatcher using the options property list. The available options are:
pool_id (optional): an atom identifying the connection pool id.
An 'undefined' value indicates that the default connection
pool, i.e. 'erlydb_mysql', should be used.
hostname: a string indicating the database host name.
port (optional): an integer indicating the database port
('undefined' indicates the default MySQL port, 3306).
username: a string indicating the username.
password: a string indicating the password.
database: a string indicating the database name.
allow_unsafe_statements: a boolean value indicating whether the driver
should
accept string and/or binary SQL queries and query fragments. If you
set this value to
'true', ErlyDB lets you use string or binary Where and Extras expressions
in generated functions. For more information, see erlydb.
encoding: the character encoding MySQL will use.
poolsize: the number of connections to start.
start_link/1.
start_link(StartOptions::proplist()) -> ok | {error, Error}
This function is similar to start/1, only it calls
mysql:start_link() instead of mysql:start().
transaction(Fun::function(), Options::options()) -> {atomic, Result} | {aborted, Reason}
Execute a group of statements in a transaction. Fun is the function that implements the transaction. Fun can contain an arbitrary sequence of calls to the erlydb_mysql's query functions. If Fun crashes or returns or throws 'error' or {error, Err}, the transaction is automatically rolled back.
update(Statement::statement(), Options::options()) -> {ok, NumAffected} | {error, Err}
Execute a DELETE or UPDATE statement.