LuaSQLite3

Check-in [301059d037]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Update sqlite_open to sqlite_open_v2, with relevant flags defined; update wiki doc.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 301059d037a754e8f3ee2223d10f32ba5957edb1
User & Date: e 2016-11-05 19:12:01
References
2016-11-05
19:13 Ticket [db1f18d3ca] Additional parameters to the "open" function status still Open with 5 other changes artifact: 0b13f30a13 user: e
Context
2016-11-05
19:21
Remove older rockspec check-in: 3cf19187bf user: e tags: trunk
19:12
Update sqlite_open to sqlite_open_v2, with relevant flags defined; update wiki doc. check-in: 301059d037 user: e tags: trunk
2016-11-03
22:31
New rockspec for 0.9.4 with second module 'lsqlite3complete' that statically links sqlite.c check-in: 29da4fe833 user: e tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to doc/lsqlite3.wiki.

198
199
200
201
202
203
204












205
206
207
208
209
210
211
....
1080
1081
1082
1083
1084
1085
1086






1087
1088
1089
1090
1091
1092
1093
specific database, see <a href="#database_methods">Database methods</a>). Example:</p>
<pre>
        myDB=sqlite3.open('MyDatabase.sqlite3')  -- open
        -- do some database calls...
        myDB:close()  -- close</pre>
<p>In case of an error, the function returns nil, an error code and an
error message.</p>












<p>
</p>
<h2><a name="sqlite3_open_memory">sqlite3.open_memory</a></h2>
<pre>
        sqlite3.open_memory()</pre>
<p>Opens an SQLite database <strong>in memory</strong> and returns its handle as
userdata. In case of an error, the function returns nil, an error code
................................................................................
  CREATE_TEMP_TRIGGER: 5  CREATE_TEMP_VIEW: 6   CREATE_TRIGGER: 7     CREATE_VIEW: 8
  DELETE: 9               DROP_INDEX: 10        DROP_TABLE: 11        DROP_TEMP_INDEX: 12
  DROP_TEMP_TABLE: 13     DROP_TEMP_TRIGGER: 14 DROP_TEMP_VIEW: 15    DROP_TRIGGER: 16
  DROP_VIEW: 17           INSERT: 18            PRAGMA: 19            READ: 20
  SELECT: 21              TRANSACTION: 22       UPDATE: 23            ATTACH: 24
  DETACH: 25              ALTER_TABLE: 26       REINDEX: 27           ANALYZE: 28
  CREATE_VTABLE: 29       DROP_VTABLE: 30       FUNCTION: 31          SAVEPOINT: 32






</pre>
<p>For details about their exact meaning please see the <strong>SQLite3
documentation</strong> <a href="http://www.sqlite.org/">http://www.sqlite.org/</a>.</p>
<p>
</p>
<hr />
<h1><a name="version">VERSION</a></h1>







>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>







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
....
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
specific database, see <a href="#database_methods">Database methods</a>). Example:</p>
<pre>
        myDB=sqlite3.open('MyDatabase.sqlite3')  -- open
        -- do some database calls...
        myDB:close()  -- close</pre>
<p>In case of an error, the function returns nil, an error code and an
error message.</p>
<p>Since 0.9.4, there is a second optional flags argument to <code>sqlite3.open</code>.
<pre>
        sqlite3.open(filename, flags)</pre></p>
<p>
See <a href="https://www.sqlite.org/c3ref/open.html">sqlite3_open_v2</a> for an explanation of
these flags and options.</p>
<p>
Example:
<pre>
        local db = sqlite3.open('foo.db', sqlite3.OPEN_READWRITE + sqlite3.OPEN_CREATE + sqlite3.OPEN_SHAREDCACHE)
</pre>
The default value for flags is <code>sqlite3.OPEN_READWRITE + sqlite3.OPEN_CREATE</code>.</p>
<p>
</p>
<h2><a name="sqlite3_open_memory">sqlite3.open_memory</a></h2>
<pre>
        sqlite3.open_memory()</pre>
<p>Opens an SQLite database <strong>in memory</strong> and returns its handle as
userdata. In case of an error, the function returns nil, an error code
................................................................................
  CREATE_TEMP_TRIGGER: 5  CREATE_TEMP_VIEW: 6   CREATE_TRIGGER: 7     CREATE_VIEW: 8
  DELETE: 9               DROP_INDEX: 10        DROP_TABLE: 11        DROP_TEMP_INDEX: 12
  DROP_TEMP_TABLE: 13     DROP_TEMP_TRIGGER: 14 DROP_TEMP_VIEW: 15    DROP_TRIGGER: 16
  DROP_VIEW: 17           INSERT: 18            PRAGMA: 19            READ: 20
  SELECT: 21              TRANSACTION: 22       UPDATE: 23            ATTACH: 24
  DETACH: 25              ALTER_TABLE: 26       REINDEX: 27           ANALYZE: 28
  CREATE_VTABLE: 29       DROP_VTABLE: 30       FUNCTION: 31          SAVEPOINT: 32
</pre>
<p>and the Open Flags:</p>
<pre>
  OPEN_READONLY           OPEN_READWRITE        OPEN_CREATE           OPEN_URI
  OPEN_MEMORY             OPEN_NOMUTEX          OPEN_FULLMUTEX        OPEN_SHAREDCACHE
  OPEN_PRIVATECACHE
</pre>
<p>For details about their exact meaning please see the <strong>SQLite3
documentation</strong> <a href="http://www.sqlite.org/">http://www.sqlite.org/</a>.</p>
<p>
</p>
<hr />
<h1><a name="version">VERSION</a></h1>

Changes to lsqlite3.c.

59
60
61
62
63
64
65
66





67
68
69
70
71
72
73
....
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
....
2064
2065
2066
2067
2068
2069
2070

2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
....
2152
2153
2154
2155
2156
2157
2158











2159
2160
2161
2162
2163
2164
2165
/* compile time features */
#if !defined(SQLITE_OMIT_PROGRESS_CALLBACK)
    #define SQLITE_OMIT_PROGRESS_CALLBACK 0
#endif
#if !defined(LSQLITE_OMIT_UPDATE_HOOK)
    #define LSQLITE_OMIT_UPDATE_HOOK 0
#endif







typedef struct sdb sdb;
typedef struct sdb_vm sdb_vm;
typedef struct sdb_bu sdb_bu;
typedef struct sdb_func sdb_func;

/* to use as C user data so i know what function sqlite is calling */
................................................................................
        }
    }
    lua_pushstring(L, oldtemp);
    return 1;
}
#endif

static int lsqlite_do_open(lua_State *L, const char *filename) {
    sdb *db = newdb(L); /* create and leave in stack */

    if (sqlite3_open(filename, &db->db) == SQLITE_OK) {
        /* database handle already in the stack - return it */
        return 1;
    }

    /* failed to open database */
    lua_pushnil(L);                             /* push nil */
    lua_pushinteger(L, sqlite3_errcode(db->db));
................................................................................

    /* return */
    return 3;
}

static int lsqlite_open(lua_State *L) {
    const char *filename = luaL_checkstring(L, 1);

    return lsqlite_do_open(L, filename);
}

static int lsqlite_open_memory(lua_State *L) {
    return lsqlite_do_open(L, ":memory:");
}

static int lsqlite_newindex(lua_State *L) {
    lua_pushliteral(L, "attempt to change readonly table");
    lua_error(L);
    return 0;
}
................................................................................
    SC(REINDEX            )
    SC(ANALYZE            )
    SC(CREATE_VTABLE      )
    SC(DROP_VTABLE        )
    SC(FUNCTION           )
    SC(SAVEPOINT          )












    /* terminator */
    { NULL, 0 }
};

/* ======================================================= */

static const luaL_Reg dblib[] = {







<
>
>
>
>
>







 







|


|







 







>
|



|







 







>
>
>
>
>
>
>
>
>
>
>







59
60
61
62
63
64
65

66
67
68
69
70
71
72
73
74
75
76
77
....
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
....
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
....
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
/* compile time features */
#if !defined(SQLITE_OMIT_PROGRESS_CALLBACK)
    #define SQLITE_OMIT_PROGRESS_CALLBACK 0
#endif
#if !defined(LSQLITE_OMIT_UPDATE_HOOK)
    #define LSQLITE_OMIT_UPDATE_HOOK 0
#endif

#if defined(LSQLITE_OMIT_OPEN_V2)
    #define SQLITE3_OPEN(L,filename,flags) sqlite3_open(L,filename)
#else
    #define SQLITE3_OPEN(L,filename,flags) sqlite3_open_v2(L,filename,flags,NULL)
#endif

typedef struct sdb sdb;
typedef struct sdb_vm sdb_vm;
typedef struct sdb_bu sdb_bu;
typedef struct sdb_func sdb_func;

/* to use as C user data so i know what function sqlite is calling */
................................................................................
        }
    }
    lua_pushstring(L, oldtemp);
    return 1;
}
#endif

static int lsqlite_do_open(lua_State *L, const char *filename, int flags) {
    sdb *db = newdb(L); /* create and leave in stack */

    if (SQLITE3_OPEN(filename, &db->db, flags) == SQLITE_OK) {
        /* database handle already in the stack - return it */
        return 1;
    }

    /* failed to open database */
    lua_pushnil(L);                             /* push nil */
    lua_pushinteger(L, sqlite3_errcode(db->db));
................................................................................

    /* return */
    return 3;
}

static int lsqlite_open(lua_State *L) {
    const char *filename = luaL_checkstring(L, 1);
    int flags = luaL_optinteger(L, 2, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
    return lsqlite_do_open(L, filename, flags);
}

static int lsqlite_open_memory(lua_State *L) {
    return lsqlite_do_open(L, ":memory:", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
}

static int lsqlite_newindex(lua_State *L) {
    lua_pushliteral(L, "attempt to change readonly table");
    lua_error(L);
    return 0;
}
................................................................................
    SC(REINDEX            )
    SC(ANALYZE            )
    SC(CREATE_VTABLE      )
    SC(DROP_VTABLE        )
    SC(FUNCTION           )
    SC(SAVEPOINT          )

    /* file open flags */
    SC(OPEN_READONLY)
    SC(OPEN_READWRITE)
    SC(OPEN_CREATE)
    SC(OPEN_URI)
    SC(OPEN_MEMORY)
    SC(OPEN_NOMUTEX)
    SC(OPEN_FULLMUTEX)
    SC(OPEN_SHAREDCACHE)
    SC(OPEN_PRIVATECACHE)
    
    /* terminator */
    { NULL, 0 }
};

/* ======================================================= */

static const luaL_Reg dblib[] = {