-
Notifications
You must be signed in to change notification settings - Fork 0
Home
An environment object is created by calling the driver's initialization function that is stored in the table returned when it was loaded, indexed with the same name as the driver (odbc, postgres etc). The following example, will try to create an environment object using the ODBC driver.
local driver = require"luasql.odbc"
local env = driver.odbc()
-
env:close()
-
Closes the environment env. Only successful if all connections pertaining to it were closed first. Returns: true in case of success; false when the object is already closed.
-
env:connect(conn_details)
-
Connects to a data source specified with details given in the
conn_details
tableStandard options are:
-
source
: The database path/name -
user
: User name to connect with -
password
: User password
Other options will be available specific to the driver used.
Returns: a connection object.
-
-
env:connect(sourcename[,username[,password]])
-
Connects to a data source specified in sourcename using username and password if they are supplied.
The sourcename may vary according to each driver. Some use a simple database name, like PostgreSQL, MySQL and SQLite; the ODBC driver expects the name of the DSN; the Oracle driver expects the service name; See also: PostgreSQL, and MySQL extensions.
Returns: a connection object.
A connection object contains specific attributes and parameters of a single data source connection. A connection object is created by calling the environment:connect method. Methods
-
conn:close()
-
Closes the connection conn. Only successful if all cursors pertaining to it have been closed and the connection is still open. Returns: true in case of success and false in case of failure.
-
conn:commit()
-
Commits the current transaction. This feature might not work on database systems that do not implement transactions. Returns: true in case of success and false when the operation could not be performed or when it is not implemented.
-
conn:prepare(statement)
-
Prepares a statement object ready for parametrised queries.
Returns: a statement object
-
conn:execute(statement[, parameters])
-
Executes the given SQL statement, using the optional parameters table (see Statement section below)
Returns: a cursor object if there are results, or the number of rows affected by the command otherwise.
-
conn:rollback()
-
Rolls back the current transaction. This feature might not work on database systems that do not implement transactions.
Returns: true in case of success and false when the operation could not be performed or when it is not implemented.
conn:setautocommit(boolean)
- Turns on or off the "auto commit" mode. This feature might not work on database systems that do not implement transactions. On database systems that do not have the concept of "auto commit mode", but do implement transactions, this mechanism is implemented by the driver.
Returns: true in case of success and false when the operation could not be performed or when it is not implemented.
A statement object represents a pre-prepared SQL command awaiting parameters to execute
-
stmt:execute([parameters])
-
Executes the statement, using the passed table of parameters
Because database engines differ on how parameters are specified in SQL commands the form of the parameters table will depend on the driver used; it may be a simple array of value, or a map of param names to values.
Returns: a cursor object if there are results, or the number of rows affected by the command otherwise.
-
stmt:getparamtypes()
-
Returns: a table of all required parameters and their types.
A cursor object contains methods to retrieve data resulting from an executed statement. A cursor object is created by using the connection:execute function. See also PostgreSQL and Oracle extensions. Methods
cur:close()
- Closes this cursor.
Returns: true in case of success and false when the object is already closed.
cur:fetch([table[,modestring]])
- Retrieves the next row of results.
If fetch is called without parameters, the results will be returned directly to the caller. If fetch is called with a table, the results will be copied into the table and the changed table will be returned. In this case, an optional modestring parameter can be used. It is just a string indicating how the resulting table should be constructed. The mode string can contain:
- "n" : the resulting table will have numerical indices (default)
- "a" : the resulting table will have alphanumerical indices
The numerical indices are the positions of the fields in the SELECT statement; the alphanumerical indices are the names of the fields.
The optional table parameter is a table that should be used to store the next row. This allows the use of a unique table for many fetches, which can improve the overall performance.
A call to fetch after the last row has already being returned will close the corresponding cursor. There is no guarantee about the types of the results: they may or may not be converted to adequate Lua types by the driver. In the current implementation, the PostgreSQL and MySQL drivers return all values as strings while the ODBC and Oracle drivers convert them to Lua types.
Returns: data, as above, or nil if there are no more rows. Note that this method could return nil as a valid result.
cur:getcolnames()
- Returns: a list (table) of column names.
cur:getcoltypes()
- Returns: a list (table) of column types.
Besides the basic functionality provided by all drivers, the Postgres driver also offers these extra features:
env:connect(sourcename[,username[,password[,hostname[,port]]]])
- In the PostgreSQL driver, this method adds two optional parameters that indicate the hostname and port to connect. Also, the first parameter can contain all connection information, as stated in the documentation for PQconnectdb function in the PostgreSQL manual (e.g. environment:connect("dbname= user=")) See also: environment objects Returns: a connection object
conn:escape(str)
- Escape especial characters in the given string according to the connection's character set. See also: Official documentation of function PQescapeStringConn Returns: the escaped string.
cur:numrows()
- See also: cursor objects Returns: the number of rows in the query result.
Besides the basic functionality provided by all drivers, the MySQL driver also offers these extra features:
-
env:connect(sourcename[,username[,password[,hostname[,port]]]])
-
In the MySQL driver, this method adds two optional parameters that indicate the hostname and port to connect. See also: environment objects
Returns: a connection object
-
conn:escape(str)
-
Escape especial characters in the given string according to the connection's character set.
See also: Official documentation of function mysql_real_escape_string
Returns: the escaped string.
-
conn:getlastautoid()
-
Obtains the value generated for an AUTO_INCREMENT column by the previous INSERT or UPDATE statement. See also: Official documentation of function mysql_insert_id for versions 4.0, 5.1 and 6.0
Returns: the number of the last value generated for an AUTO_INCREMENT column.
-
cur:numrows()
-
See also: cursor objects
Returns: the number of rows in the query result.
Notes:
This driver is compatible with versions 4.0, 4.1 and 5.0 of the MySQL API. Only from version 4.1 MySQL provides support for transactions by using BDB or INNODB tables. Therefore, with version 4.0 or without one of these types of tables, the methods commit, rollback and setautocommit will not work.
If you are using LuaSQL 2.0, cur:numrows()
is available only in version 2.0.2 or later.
Besides the basic functionality provided by all drivers, the Oracle driver also offers this extra feature:
-
cur:numrows()
-
See also: cursor objects
Returns: the number of rows in the query result.
Besides the basic functionality provided by all drivers, the SQLite3 driver also offers this extra feature:
-
env:connect(sourcename[,locktimeout])
-
In the SQLite3 driver, this method adds an optional parameter that indicate the amount of milisseconds to wait for a write lock if one cannot be obtained immediately.
See also: environment objects
Returns: a connection object
-
conn:escape(str)
-
Escape especial characters in the given string according to the connection's character set. See also: Official documentation of function sqlite3_mprintf
Returns: the escaped string.
-
env:connect(conn_details)
-
Extra options:
-
charset
: The character set to use with the connect (def: 'UTF8')(list of supported values can be found in the RDB$CHARACTER_SETS table)
-
dialect
: The SQL dialect used (def: 3)
-
-
conn:escape(str)
-
Escapes text so it can be safely embedded in SQL commands
Returns: the escaped string.
-
env:connect(conn_details)
-
Extra options:
-
dsn
: Used instead of usualsource
,user
,password
values.Allows full specification of the ODBC connection without needing a DSN entry in ODBCAD32/ODBC.INI, e.g.
con = env:connect{dsn=[[DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\work\my_db.accdb]]}
-
-
env:drivers()
-
Returns an array of available drivers and their attributes (ref: ODBC's
SQLDrivers
API)luasql = require"luasql.odbc" env = luasql.odbc() drv = assert(env:drivers()) for i,d in pairs(drv) do print("Driver : "..i, d.driver) for a,v in pairs(d.attr) do print("",a," =",v) end end
-
stmt:getparamtypes()
-
Apparently, not all ODBC drivers support
SQLDescribeParam
, so you may well not receive a table from this call