Module dado.sql

Release: $Id: sql.lua,v 1.9 2010-06-09 21:09:18 tomas Exp $

Functions

AND (tab) Composes a simple SQL AND-expression.
delete (tabname, cond) Builds a string with a DELETE command.
escape (s, char, sub) Escape a character or a character class in a string.
insert (tabname, contents) Builds a string with an INSERT command.
quote (s, quote, sub) Quote a value to be included in an SQL statement.
select (columns, tabname, cond, extra) Builds a string with a SELECT command.
subselect (columns, tabname, cond, extra) Builds a string with a SELECT command to be inserted into another SQL query.
update (tabname, contents, cond) Builds a string with an UPDATE command.


Functions

AND (tab)
Composes a simple SQL AND-expression. For complex expressions, write them explicitly. There is no OR-expression equivalent function (I don't know how to express it conveniently in Lua).

Parameters

  • tab: Table with key-value pairs representing equalities.

Return value:

String with the resulting expression.
delete (tabname, cond)
Builds a string with a DELETE command.

Parameters

  • tabname: String with table name.
  • cond: String with where-clause (and following SQL text).

Return value:

String with DELETE command.
escape (s, char, sub)
Escape a character or a character class in a string.

Parameters

  • s: String to be processed.
  • char: String with gsub's character class to be escaped inside the string (default = "%s").
  • sub: String with escape character (default = "\\").

Return value:

String or nil if no string was given.
insert (tabname, contents)
Builds a string with an INSERT command.

Parameters

  • tabname: String with table name.
  • contents: Table of elements to be inserted.

Return value:

String with INSERT command.
quote (s, quote, sub)
Quote a value to be included in an SQL statement. The exception is when the string is surrounded by "()"; in this case it won't be quoted.

Parameters

  • s: String or number.
  • quote: String with quote character (default = "'").
  • sub: String with escape character (default = "\\").

Return value:

String with prepared value.
select (columns, tabname, cond, extra)
Builds a string with a SELECT command. The existing arguments will be concatenated together to form the SQL statement. The string "select " is added as a prefix. If the tabname is given, the string " from " is added as a prefix. If the cond is given, the string " where " is added as a prefix.

Parameters

  • columns: String with columns list.
  • tabname: String with table name (optional).
  • cond: String with where-clause (optional).
  • extra: String with extra SQL text (optional).

Return value:

String with SELECT command.
subselect (columns, tabname, cond, extra)
Builds a string with a SELECT command to be inserted into another SQL query.

Parameters

  • columns: String with columns list.
  • tabname: String with table name.
  • cond: String with where-clause (and following SQL text).
  • extra: String with extra SQL text.

Return value:

String with SELECT command.
update (tabname, contents, cond)
Builds a string with an UPDATE command.

Parameters

  • tabname: String with table name.
  • contents: Table of elements to be updated.
  • cond: String with where-clause (and following SQL text).

Return value:

String with UPDATE command.

Valid XHTML 1.0!