Module dado
Dado is a set of facilities implemented over LuaSQL connection objects.
The module's goal is to simplify the most used database operations.
Info:
- Release: $Id: dado.lua,v 1.26 2017/04/05 19:14:07 tomas Exp $
Functions
assertexec (self, stmt) | Executes a SQL statement raising an error if something goes wrong. |
commit (self) | Commits the current transaction. |
rollback (self) | Rolls back the current transaction. |
setautocommit (self, bool) | Turn autocommit mode on or off. |
nextval (self, seq, field) | Obtains next value from a sequence. |
delete (self, tabname, cond) | Deletes a row. |
insert (self, tabname, contents) | Inserts a new row. |
update (self, tabname, contents, cond) | Updates existing rows. |
select (self, columns, tabname, cond, extra, mode) | Retrieves rows. |
selectall (self, columns, tabname, cond, extra, mode) | Retrieves all rows. |
wrap_connection (conn, key) | Wraps a database connection into a connection object. |
connect (dbname, dbuser, dbpass, driver) | Tries to use a connection in the cache or opens a new one. |
close (self) | Closes the connection and invalidates the object. |
Functions
- assertexec (self, stmt)
-
Executes a SQL statement raising an error if something goes wrong.
Parameters:
- self Dado Object.
- stmt String with SQL statement.
Returns:
-
Cursor or number of rows affected by the command
(it never returns nil,errmsg).
- commit (self)
-
Commits the current transaction.
Parameters:
- self Dado Object.
- rollback (self)
-
Rolls back the current transaction.
Parameters:
- self Dado Object.
- setautocommit (self, bool)
-
Turn autocommit mode on or off.
Parameters:
- self Dado Object.
- bool Boolean indicating to turn autocommit on (true) or off (false).
- nextval (self, seq, field)
-
Obtains next value from a sequence.
Parameters:
- self Dado Object.
- seq String with sequence name (complete name or just the name of the table).
- field String (optional) with the name of the primary key associated with the sequence.
Returns:
-
String with next sequence value.
- delete (self, tabname, cond)
-
Deletes a row.
Parameters:
- self Dado Object.
- tabname String with table name.
- cond String with where-clause (and following SQL text).
Returns:
-
Number of rows affected.
See also:
- insert (self, tabname, contents)
-
Inserts a new row.
Parameters:
- self Dado Object.
- tabname String with table name.
- contents Table with field-value pairs.
Returns:
-
Number of rows affected.
See also:
- update (self, tabname, contents, cond)
-
Updates existing rows.
Parameters:
- self Dado Object.
- tabname String with table name.
- contents Table with field-value pairs.
- cond String with where-clause (and following SQL text).
Returns:
-
Number of rows affected.
See also:
- select (self, columns, tabname, cond, extra, mode)
-
Retrieves rows.
Creates an iterator over the result of a query.
This iterator could be used in for-loops.
Parameters:
- self Dado Object.
- columns String with fields list.
- tabname String with table name.
- cond String with where-clause (and following SQL text).
- extra String with extra SQL text (to be used when there is no where-clause).
- mode String or Table indicating fetch mode (according to LuaSQL mode parameter of fetch method); if it is a table, the result will be stored inside it and the actual mode will be 'a'; in both cases, the function should return a table with the result set.
Returns:
- Iterator over the result set.
- Cursor object (to allow explicit closing).
See also:
- selectall (self, columns, tabname, cond, extra, mode)
-
Retrieves all rows.
Parameters:
- self Dado Object.
- columns String with fields list.
- tabname String with table name.
- cond String with where-clause (and following SQL text).
- extra String with extra SQL text.
- mode String indicating fetch mode (default == 'a').
Returns:
-
Table with the entire result set.
See also:
- wrap_connection (conn, key)
-
Wraps a database connection into a connection object.
Parameters:
- conn Object with LuaSQL database connection.
- key String with the key to the database object in the cache.
Returns:
-
Dado Connection.
- connect (dbname, dbuser, dbpass, driver)
-
Tries to use a connection in the cache or opens a new one.
Parameters:
- dbname String with database name.
- dbuser String with database username (optional).
- dbpass String with database user password (optional).
- driver String with LuaSQL's driver (default = "postgres").
Returns:
-
Connection.
- close (self)
-
Closes the connection and invalidates the object.
Parameters:
- self Dado Object.