Merge pull request #123 from abidbodal/add-readonly-flag-sqlite3
Add an option to env:connect in sqlite3
This commit is contained in:
commit
14762f35c3
@ -382,9 +382,10 @@ o driver Oracle ainda oferece uma funcionalidade adicional:</p>
|
|||||||
<p>Além das funcionalidades básicas oferecidas por todos os drivers,
|
<p>Além das funcionalidades básicas oferecidas por todos os drivers,
|
||||||
o driver para SQLite3 ainda oferece uma funcionalidade adicional:</p>
|
o driver para SQLite3 ainda oferece uma funcionalidade adicional:</p>
|
||||||
|
|
||||||
<dt><strong><code>env:connect(sourcename[,locktimeout])</code></strong></dt>
|
<dt><strong><code>env:connect(sourcename[,locktimeout,readOnlyMode])</code></strong></dt>
|
||||||
<dd>No driver para SQLite3, este método tem mais um parâmetro opcional que indica
|
<dd>No driver para SQLite3, este método tem mais um parâmetro opcional que indica
|
||||||
a quantidade de milissegundos para esperar por uma trava de escrita no banco de dados, se ela não for obtida de imediato.<br/>
|
a quantidade de milissegundos para esperar por uma trava de escrita no banco de dados, se ela não for obtida de imediato.
|
||||||
|
TODO: translate me : To connect in readOnlyMode, set readOnlyMode to true<br/>
|
||||||
Veja também: <a href="#environment_object">ambiente</a><br/>
|
Veja também: <a href="#environment_object">ambiente</a><br/>
|
||||||
Retorna: uma <a href="#connection_object">conexão</a></dd>
|
Retorna: uma <a href="#connection_object">conexão</a></dd>
|
||||||
|
|
||||||
|
@ -404,9 +404,10 @@ the Oracle driver also offers this extra feature:</p>
|
|||||||
<p>Besides the basic functionality provided by all drivers,
|
<p>Besides the basic functionality provided by all drivers,
|
||||||
the SQLite3 driver also offers this extra feature:</p>
|
the SQLite3 driver also offers this extra feature:</p>
|
||||||
|
|
||||||
<dt><strong><code>env:connect(sourcename[,locktimeout])</code></strong></dt>
|
<dt><strong><code>env:connect(sourcename[,locktimeout, readOnlyMode])</code></strong></dt>
|
||||||
<dd>In the SQLite3 driver, this method adds an optional parameter
|
<dd>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.<br/>
|
that indicate the amount of milisseconds 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/>
|
See also: <a href="#environment_object">environment objects</a><br/>
|
||||||
Returns: a <a href="#connection_object">connection object</a></dd>
|
Returns: a <a href="#connection_object">connection object</a></dd>
|
||||||
|
|
||||||
|
@ -413,9 +413,10 @@ the Oracle driver also offers this extra feature:</p>
|
|||||||
<p>Besides the basic functionality provided by all drivers,
|
<p>Besides the basic functionality provided by all drivers,
|
||||||
the SQLite3 driver also offers this extra feature:</p>
|
the SQLite3 driver also offers this extra feature:</p>
|
||||||
|
|
||||||
<dt><strong><code>env:connect(sourcename[,locktimeout])</code></strong></dt>
|
<dt><strong><code>env:connect(sourcename[,locktimeout,readOnlyMode])</code></strong></dt>
|
||||||
<dd>In the SQLite3 driver, this method adds an optional parameter
|
<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.<br/>
|
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/>
|
See also: <a href="#environment_object">environment objects</a><br/>
|
||||||
Returns: a <a href="#connection_object">connection object</a></dd>
|
Returns: a <a href="#connection_object">connection object</a></dd>
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
@ -549,19 +550,36 @@ static int env_connect(lua_State *L)
|
|||||||
sqlite3 *conn;
|
sqlite3 *conn;
|
||||||
const char *errmsg;
|
const char *errmsg;
|
||||||
int res;
|
int res;
|
||||||
|
bool readOnlyMode = false;
|
||||||
|
int mode;
|
||||||
|
|
||||||
getenvironment(L); /* validate environment */
|
getenvironment(L); /* validate environment */
|
||||||
|
|
||||||
sourcename = luaL_checkstring(L, 2);
|
if (lua_isboolean(L, 4)) {
|
||||||
|
if (lua_toboolean(L, 4)) {
|
||||||
|
readOnlyMode = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sourcename = luaL_checkstring(L, 2);
|
||||||
#if SQLITE_VERSION_NUMBER > 3006013
|
#if SQLITE_VERSION_NUMBER > 3006013
|
||||||
if (strstr(sourcename, ":memory:")) /* TODO: rework this and get/add param 'flag' for sqlite3_open_v2 - see TODO below */
|
if (strstr(sourcename, ":memory:")) /* TODO: rework this and get/add param 'flag' for sqlite3_open_v2 - see TODO below */
|
||||||
{
|
{
|
||||||
res = sqlite3_open_v2(sourcename, &conn, SQLITE_OPEN_READWRITE | SQLITE_OPEN_MEMORY, NULL);
|
if (readOnlyMode) {
|
||||||
|
mode = SQLITE_OPEN_READONLY | SQLITE_OPEN_MEMORY;
|
||||||
|
} else {
|
||||||
|
mode = SQLITE_OPEN_READWRITE | SQLITE_OPEN_MEMORY;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
res = sqlite3_open_v2(sourcename, &conn, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
|
if (readOnlyMode) {
|
||||||
|
mode = SQLITE_OPEN_READONLY;
|
||||||
|
} else {
|
||||||
|
mode = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
res = sqlite3_open_v2(sourcename, &conn, mode, NULL);
|
||||||
#else
|
#else
|
||||||
res = sqlite3_open(sourcename, &conn);
|
res = sqlite3_open(sourcename, &conn);
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user