1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>LuaSQL: Database connectivity for the Lua programming language</title>
<link rel="stylesheet" href="doc.css" type="text/css"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<div id="container">
<div id="product">
<div id="product_logo">
<img alt="LuaSQL logo" src="luasql.png"/>
</div>
<div id="product_name"><big><strong>LuaSQL</strong></big></div>
<div id="product_description">Database connectivity for the Lua programming language</div>
</div> <!-- id="product" -->
<div id="main">
<div id="navigation">
<h1>LuaSQL</h1>
<ul>
<li><a href="index.html">Home</a>
</li>
<li><strong>Manual</strong>
<ul>
<li><a href="manual.html#introduction">Introduction</a></li>
<li><a href="manual.html#compiling">Compiling</a></li>
<li><a href="manual.html#installation">Installation</a></li>
<li><a href="manual.html#errors">Error handling</a></li>
<li><a href="manual.html#drivers">Drivers</a></li>
<li><a href="manual.html#environment_object">Environment</a></li>
<li><a href="manual.html#connection_object">Connection</a></li>
<li><a href="manual.html#cursor_object">Cursor</a></li>
<li><a href="manual.html#postgres_extensions">PostgreSQL</a></li>
<li><a href="manual.html#mysql_extensions">MySQL</a></li>
<li><a href="manual.html#oracle_extensions">Oracle</a></li>
<li><a href="manual.html#sqlite3_extensions">SQLite3</a></li>
</ul>
</li>
<li><a href="examples.html">Examples</a></li>
<li><a href="history.html">History</a></li>
<li><a href="http://github.com/keplerproject/luasql">Project</a>
<ul>
<li><a href="https://github.com/lunarmodules/luasql/issues">Bug Tracker</a></li>
</ul>
</li>
<li><a href="license.html">License</a></li>
</ul>
</div> <!-- id="navigation" -->
<div id="content">
<h2><a name="introduction"></a>Introduction</h2>
<p>LuaSQL is a simple interface from Lua to a number of database management systems.
It includes a set of drivers to some popular databases
(currently PostgreSQL, ODBC, MySQL, SQLite, Oracle, and ADO;
Interbase and Sybase are in our plans).
LuaSQL defines a simple object-oriented API.
All drivers should implement this common API,
but each one is free to offer extensions.</p>
<p>LuaSQL defines one single global variable,
a table called <code>luasql</code>.
This table is used to store the initialization methods of the
loaded drivers.
These methods are used to create an
<a href="#environment_object">environment object</a>
which is used to create a
<a href="#connection_object">connection object</a>.
A connection object can execute SQL statements and eventually
create a
<a href="#cursor_object">cursor object</a>
which is used to retrieve data.
</p>
<p>
LuaSQL is free software and uses the same
<a href="license.html">license</a>
as Lua 5.1.
</p>
<h2><a name="compiling"></a>Compiling</h2>
<p>LuaSQL is distributed as a set of C source files:
a pair of common source and header files (<code>luasql.h</code> and <code>luasql.c</code>);
and one source file for each driver.
Each driver should be compiled with the luasql.c file to generate a library.
This library can be linked to the application or dynamically loaded.
The initialization function is <code>luaopen_luasql<em>drivername</em></code> and it is a Lua
<a href="http://www.lua.org/manual/5.1/manual.html#pdf-require"><code>open-library</code></a> compatible function.
</p>
<h2><a name="installation"></a>Installation</h2>
<p>Since version 2.3, all LuaSQL drivers follow the package model of Lua 5.2.
All drivers should be "installed" in your <code>package.cpath</code> into a
folder called <code>luasql</code>.</p>
<p>In order to use LuaSQL with ADO, make sure that you have installed
<a href="http://www.tecgraf.puc-rio.br/~rcerq/luacom">LuaCOM 1.3</a>
for the appropriate version of Lua.</p>
<h2><a name="errors"></a>Error handling</h2>
<p>
LuaSQL is just an abstraction layer that communicates between Lua
and a database system.
Therefore errors can occur on both levels, that is,
inside the database client or inside LuaSQL driver.
</p>
<p>
Errors such as malformed SQL statements, unknown table names etc.
are called <em>database errors</em> and
will be reported by the function/method returning <code>nil</code> followed
by the error message provided by the database system.
Errors such as wrong parameters, absent connection, invalid objects etc.,
called <em>API errors</em>,
are usually program errors and so will raise a Lua error.
</p>
<p>
This behavior will be followed by all functions/methods
described in this document unless otherwise stated.
</p>
<h2><a name="drivers"></a>Drivers</h2>
<p>
A LuaSQL driver allows the use of the LuaSQL API with a database management system
that corresponds to the driver. To use a driver you have to load it.
The example below
</p>
<pre class="example">
local driver = require "luasql.odbc"
</pre>
<p>
loads the ODBC driver and returns a table with an entry with the name of the
driver (<code>odbc</code> in this case). Note that you can have more than one
driver loaded at the same time doing something like:
</p>
<pre class="example">
local odbc_driver = require "luasql.odbc"
local oci8_driver = require "luasql.oci8"
</pre>
<p>
This example also shows that the driver name not always correspond to the
Database name, but to the driver name in the file system. Since it refers to
the OCI8 API, the Oracle driver has the name <code>oci8</code>.
</p>
<h2><a name="environment_object"></a>Environment Objects</h2>
<p>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.
</p>
<pre class="example">
local driver = require"luasql.odbc"
local env = driver.odbc()
</pre>
<h4>Methods</h4>
<dl class="reference">
<dt><a name="env_close"></a><strong><code>env:close()</code></strong></dt>
<dd>Closes the environment <code>env</code>.
Only successful if all connections pertaining to it were closed first.<br/>
Returns: <code>true</code> in case of success and in case of failure it returns <code>false</code> with a <code>string message</code> stating the failure reason.</dd>
<dt><a name="env_connect"></a><strong><code>env:connect(sourcename[,username[,password]])</code></strong></dt>
<dd>Connects to a data source specified in <code>sourcename</code> using
<code>username</code> and <code>password</code> if they are supplied.<br/>
The <code>sourcename</code> 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: <a href="#postgres_extensions">PostgreSQL</a>,
and <a href="#mysql_extensions">MySQL</a> extensions.<br/>
Returns: a <a href="#connection_object">connection object</a>.</dd>
</dl>
<h2><a name="connection_object"></a>Connection Objects</h2>
<p>A connection object contains specific attributes and parameters of a
single data source connection.
A connection object is created by calling the
<code><a href="#env_connect">environment:connect</a></code>
method.</p>
<h4>Methods</h4>
<dl class="reference">
<dt><a name="conn_close"></a><strong><code>conn:close()</code></strong></dt>
<dd>Closes the connection <code>conn</code>.
Only successful if all cursors pertaining to it have been closed and the connection is still open.<br/>
Returns: <code>true</code> in case of success and in case of failure it returns <code>false</code> with a <code>string message</code> stating the failure reason.</dd>
<dt><a name="conn_commit"></a><strong><code>conn:commit()</code></strong></dt>
<dd>Commits the current transaction.
This feature might not work on database systems that do not implement
transactions.<br/>
Returns: <code>true</code> in case of success and <code>false</code> when
the operation could not be performed or when it is not implemented.</dd>
<dt><a name="conn_execute"></a><strong><code>conn:execute(statement)</code></strong></dt>
<dd>Executes the given SQL <code>statement</code>.<br/>
Returns: a <a href="#cursor_object">cursor object</a>
if there are results, or the number of rows affected by the command otherwise.</dd>
<dt><a name="conn_rollback"></a><strong><code>conn:rollback()</code></strong></dt>
<dd>Rolls back the current transaction.
This feature might not work on database systems that do not implement
transactions.<br/>
Returns: <code>true</code> in case of success and <code>false</code> when
the operation could not be performed or when it is not implemented.</dd>
<dt><a name="conn_setautocommit"></a><strong><code>conn:setautocommit(boolean)</code></strong></dt>
<dd>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.
<br/>
Returns: <code>true</code> in case of success and <code>false</code> when
the operation could not be performed or when it is not implemented.</dd>
</dl>
<h2><a name="cursor_object"></a>Cursor Objects</h2>
<p>A cursor object contains methods to retrieve data resulting from an
executed statement. A cursor object is created by using the
<code><a href="#conn_execute">connection:execute</a></code>
function.
See also <a href="#postgres_extensions">PostgreSQL</a>
and <a href="#oracle_extensions">Oracle</a> extensions.</p>
<h4>Methods</h4>
<dl class="reference">
<dt><a name="cur_close"></a><strong><code>cur:close()</code></strong></dt>
<dd>Closes this cursor.<br/>
Returns: <code>true</code> in case of success and in case of failure it returns <code>false</code> with a <code>string message</code> stating the failure reason.</dd>
<dt><a name="cur_fetch"></a><strong><code>cur:fetch([table[,modestring]])</code></strong></dt>
<dd>Retrieves the next row of results.<br/>
If <code>fetch</code> is called without parameters,
the results will be returned directly to the caller.
If <code>fetch</code> 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 <code>modestring</code> parameter can be used.
It is just a string indicating how the resulting table should be constructed.
The mode string can contain:
<dl>
<dt><strong>"n"</strong></dt><dd>the resulting table will have numerical indices (default)</dd>
<dt><strong>"a"</strong></dt><dd>the resulting table will have alphanumerical indices</dd>
</dl>
<br/>
The <em>numerical indices</em> are the positions of the fields in the SELECT
statement;
the <em>alphanumerical indices</em> are the names of the fields.<br/>
The optional <code>table</code> 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.<br/>
A call to <code>fetch</code> 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.<br/>
Returns: data, as above, or <code>nil</code> if there are no more rows.
Note that this method could return <code>nil</code> as a valid result.</dd>
<dt><a name="cur_colnames"></a><strong><code>cur:getcolnames()</code></strong></dt>
<dd>Returns: a list (table) of column names.</dd>
<dt><a name="cur_coltypes"></a><strong><code>cur:getcoltypes()</code></strong></dt>
<dd>Returns: a list (table) of column types.</dd>
</dl>
<p><a name="extensions"></a></p>
<h2><a name="postgres_extensions"></a>PostgreSQL Extensions</h2>
<p>Besides the basic functionality provided by all drivers,
the Postgres driver also offers these extra features:</p>
<dl class="reference">
<dt><strong><code>env:connect(sourcename[,username[,password[,hostname[,port]]]])</code></strong></dt>
<dd>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 <code>PQconnectdb</code> function
in the PostgreSQL manual
(e.g. <small><code>environment:connect("dbname=<<em>name</em>> user=<<em>username</em>>")</code></small>) <br/>
See also: <a href="#environment_object">environment objects</a><br/>
Returns: a <a href="#connection_object">connection object</a></dd>
<dt><strong><code>conn:escape(str)</code></strong></dt>
<dd>Escape especial characters in the given string according to the
connection's character set.<br/>
See also: Official documentation of function <a href="http://www.postgresql.org/docs/8.3/static/libpq-exec.html#LIBPQ-EXEC-ESCAPE-STRING">PQescapeStringConn</a><br/>
Returns: the escaped string.
</dd>
<dt><strong><code>cur:numrows()</code></strong></dt>
<dd>See also: <a href="#cursor_object">cursor objects</a><br/>
Returns: the number of rows in the query result.</dd>
</dl>
<h2><a name="mysql_extensions"></a>MySQL Extensions</h2>
<p>Besides the basic functionality provided by all drivers,
the MySQL driver also offers these extra features:</p>
<dl class="reference">
<dt><strong><code>env:connect(sourcename[,username[,password[,hostname[,port[,socket[,client_flag]]]]]])</code></strong></dt>
<dd>In the MySQL driver, this method adds two optional parameters
that indicate the hostname and port to connect.
It also accepts two optional parameters that indicate a unix socket and the
client flag.<br/>
See also: <a href="#environment_object">environment objects</a><br/>
Returns: a <a href="#connection_object">connection object</a></dd>
<dt><strong><code>conn:escape(str)</code></strong></dt>
<dd>Escape especial characters in the given string according to the
connection's character set.<br/>
See also: Official documentation of function <a href="http://dev.mysql.com/doc/refman/4.1/en/mysql-real-escape-string.html">mysql_real_escape_string</a><br/>
Returns: the escaped string.
</dd>
<a name="mysql_getlastautoid"></a>
<dt><strong><code>conn:getlastautoid()</code></strong></dt>
<dd>Obtains the value generated for an AUTO_INCREMENT column by the previous
INSERT or UPDATE statement.<br/>
See also: Official documentation of function <code>mysql_insert_id</code> for versions <a href="http://dev.mysql.com/doc/refman/4.1/en/mysql-insert-id.html">4.0</a>, <a href="http://dev.mysql.com/doc/refman/5.1/en/mysql-insert-id.html">5.1</a> and <a href="http://dev.mysql.com/doc/refman/6.0/en/mysql-insert-id.html">6.0</a><br/>
Returns: the number of the last value generated for an AUTO_INCREMENT column.
</dd>
<a name="mysql_ping"></a>
<dt><strong><code>conn:ping()</code></strong></dt>
<dd>Check whether the connection is closed (returns false) or opened (returns true).
It will return nil followed by an error message in case of error.<br/>
Returns: true, if the connection is opened, or false, if it is closed.
</dd>
<a name="mysql_numrows"></a>
<dt><strong><code>cur:numrows()</code></strong></dt>
<dd>See also: <a href="#cursor_object">cursor objects</a><br/>
Returns: the number of rows in the query result.</dd>
<a name="mysql_seek"></a>
<dt><strong><code>cur:seek(row_number)</code></strong></dt>
<dd>Seeks to an arbitrary row in a query result set. The number of the first row is 1.<br/>
See also: <a href="#cursor_object">cursor objects</a><br/>
Returns: nothing.</dd>
<a name="mysql_next_result"></a>
<dt><strong><code>cur:nextresult()</code></strong></dt>
<dd>Retrieves the next result set, if another result is available (e.g. multiple statements)<br/>
See also: <a href="#cursor_object">cursor objects</a><br/>
Returns: true, if next result was selected. false and -1 if no other result is available. false, errno and error message, if an error occured.<br/>
To use it you need to pass the <code>CLIENT_MULTI_STATEMENTS = 1<<16</code> flag on connection.
Be aware that you need to iterate over all results otherwise you will receive an "out of sync" error.
</dd>
<a name="mysql_has_next_result"></a>
<dt><strong><code>cur:hasnextresult()</code></strong></dt>
<dd>Checks if next result is available<br/>
See also: <a href="#cursor_object">cursor objects</a><br/>
Returns: true or false<br/>
To use it you need to pass the <code>CLIENT_MULTI_STATEMENTS = 1<<16</code> flag on connection.
Be aware that you need to iterate over all results otherwise you will receive an "out of sync" error.
</dd>
</dl>
<p>Notes:</p>
<p>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 <code>commit</code>, <code>rollback</code> and
<code>setautocommit</code> will not work.</p>
<p>If you are using LuaSQL 2.0, <strong><code>cur:numrows()</code></strong>
is available only in version 2.0.2 or later.</p>
<h2><a name="oracle_extensions"></a>Oracle Extensions</h2>
<p>Besides the basic functionality provided by all drivers,
the Oracle driver also offers this extra feature:</p>
<dl class="reference">
<dt><strong><code>cur:numrows()</code></strong></dt>
<dd>See also: <a href="#cursor_object">cursor objects</a><br/>
Returns: the number of rows in the query result.</dd>
</dl>
<h2><a name="sqlite3_extensions"></a>SQLite3 Extensions</h2>
<p>Besides the basic functionality provided by all drivers,
the SQLite3 driver also offers this extra feature:</p>
<dl class="reference">
<dt><strong><code>env:connect(sourcename[,locktimeout,readOnlyMode])</code></strong></dt>
<dd>In the SQLite3 driver, this method adds an optional parameter
that indicate the amount of milliseconds to wait for a write lock if one cannot be obtained immediately.
To connect in readOnlyMode, set readOnlyMode to true.<br/>
See also: <a href="#environment_object">environment objects</a><br/>
Returns: a <a href="#connection_object">connection object</a></dd>
<dt><strong><code>conn:escape(str)</code></strong></dt>
<dd>Escape especial characters in the given string according to the
connection's character set.<br/>
See also: Official documentation of function <a href="http://www.sqlite.org/c3ref/mprintf.html">sqlite3_mprintf</a><br/>
Returns: the escaped string.
</dd>
</dl>
</div> <!-- id="content" -->
</div> <!-- id="main" -->
<div id="about">
<p><a href="http://validator.w3.org/check?uri=referer">Valid XHTML 1.0!</a></p>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
</html>
|