lem-mariadb is a MariaDB/MySQL library for the Lua Event Machine. It allows you to query MariaDB and MySQL databases without blocking other coroutines. It works with all versions of MariaDB and MySQL servers, but it needs the MariaDB client library, as the MySQL client library does not have a non-blocking interface.
Get the source and do
make
make install
This installs the library under /usr/local/lib/lua/5.1/
.
Use
make PREFIX=<your custom path> install
to install to <your custom path>/lib/lua/5.1/
.
Import the module using something like
local db = require 'lem.mariadb'
This sets db
to a table with a single function.
-
db.connect{host=HOST, user=USER, passwd=PASSWORD, db=SCHEMA, port=PORT, socket=SOCKET_PATH)
Connect to the database using the given parameters. If any parameter is omitted, a default is used. Returns a new database connection object on success, or otherwise
nil
followed by an error message (string) and error code (integer).
The metatable of database connection objects can be found under db.Connection. Prepared statements metatable (see below) is under db.PrepStmt.
Database connection objects has the following methods.
-
db:close()
Close the database connection.
Returns
true
on success or otherwisenil, 'already closed'
, if the connection was already closed. -
db:exec(query)
Execute an SQL query, and return the result as a Lua table with entries 1, 2, ... for each rows. Each row is again a Lua table with entries 1, 2, ... for each returned column. If there is no result set (eg. CREATE TABLE), the value
true
is returned instead of a Lua table.This method does not support placeholders (see db:prepare() / stmt:run() for that).
In case of error, nil is returned followed by an error message and error code.
If another coroutine is using this database connection the error message will be
'busy'
. If the connection is closed the error message will be'closed'
. -
db:prepare(query)
prepares an SQL statement. Returns a new prepared statement object, or nil followed by an error message and error code in case of error.
If another coroutine is using this database connection the error message will be
'busy'
. If the connection is closed the error message will be'closed'
.
Prepared statement objects have the following method:
-
stmt:run(...)
Executes the prepared statement with the given parameters. Each parameter is substituted for the corresponding placeholder '?' in the original SQL query that was prepared.
The result is returned as a Lua table with entries 1, 2, ... for each rows. Each row is again a Lua table with entries 1, 2, ... for each returned column. If there is no result set (eg. CREATE TABLE), the value
true
is returned instead of a Lua table.In case of error, nil is returned followed by an error message and error code.
If another coroutine is using this database connection the error message will be
'busy'
. If the connection is closed the error message will be'closed'
.
lem-mariadb is free software. It is distributed under the terms of the GNU Lesser General Public License.
Kristian Nielsen [email protected]