libluasqlite3.c

Go to the documentation of this file.
00001 /*
00002 
00003 lua-sqlite3 does not directly work for us. For one thing, we do not
00004 want the user to be able to choose what database to open. The database
00005 to access is our LogDb, and access to anything else is not to be
00006 allowed. (ConfigDb database is not to be accessed directly at all.)
00007 
00008 lua-sqlite3 ought to nonetheless be as good a starting point as any
00009 for implementing the access we require.
00010 
00011 LogDb access is also to be controlled quite carefully, and also, any
00012 requirements for mutex access are to be implemented. Unfortunately, to
00013 allow for interactive sessions, we cannot just go and lock the
00014 database for the duration of any user session. Luckily, on Symbian we
00015 presently have no need for MT, and Symbian's SQL Engine presumably
00016 supports locking at the database level. And a proper SQLite library on
00017 Linux should support locking. So there may be little that we have to
00018 do when it comes to protection against concurrent modification, but
00019 better keep this in mind.
00020 
00021 Note that most of the lua-sqlite3 functions like to just return an
00022 error code, and are meant to be used with a Lua wrapper, I suppose. We
00023 do not intend to use any wrapper, and hence shall be changing this for
00024 any functions that we are planning to use. We shall be creating Lua
00025 VMs left and right, and we want that to stay fast.
00026 
00027  */
00028 
00029 // This file is derived from lua-sqlite3-0.4.1, used under the following
00030 // license.
00031 //
00032 /*
00033  *  Author: Michael Roth <mroth@nessie.de>
00034  *
00035  *  Copyright (c) 2004, 2005, 2006 Michael Roth <mroth@nessie.de>
00036  *
00037  *  Permission is hereby granted, free of charge, to any person 
00038  *  obtaining a copy of this software and associated documentation
00039  *  files (the "Software"), to deal in the Software without restriction,
00040  *  including without limitation the rights to use, copy, modify, merge,
00041  *  publish, distribute, sublicense, and/or sell copies of the Software,
00042  *  and to permit persons to whom the Software is furnished to do so,
00043  *  subject to the following conditions:
00044  *
00045  *  The above copyright notice and this permission notice shall be 
00046  *  included in all copies or substantial portions of the Software.
00047  *
00048  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00049  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00050  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00051  *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
00052  *  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
00053  *  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
00054  *  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00055  *
00056  */
00057 
00058 
00059 
00060 #include "libluasqlite3.h"
00061 
00062 #include "ac_app_context.h"
00063 #include "ld_private.h"
00064 #include "sqlite_cl2.h"
00065 
00066 #include "lua.h"
00067 #include "lauxlib.h"
00068 
00069 #include <stdio.h>
00070 
00071 
00072 
00073 
00074 #define IS_INT(n)   ( ( (lua_Number) ((int)(n)) )  == (n) )
00075 
00076 #define CAST(type, arg)   ( (type)(arg) )
00077 
00078 #define FUNC(name)    static int name (lua_State * L)
00079 
00080 
00081 
00082 #define CB_DATA(ptr)    CAST(CB_Data *, (ptr))
00083 
00084 #define KEY(ptr, id)    CAST( void*, CAST(char*,(ptr))+(id) )
00085 
00086 #define KEY_KEY2VALUE_TABLE(p)  KEY((p), 1)
00087 #define KEY_FUNCTION_TABLE(p) KEY((p), 2)
00088 #define KEY_COLLATION_TABLE(p)  KEY((p), 3)
00089 #define KEY_COLLNEEDED_DATA(p)  KEY((p), 4)
00090 #define KEY_AUTHORIZER_DATA(p)  KEY((p), 5)
00091 #define KEY_PROGRESS_DATA(p)  KEY((p), 6)
00092 #define KEY_TRACE_DATA(p) KEY((p), 7)
00093 #define KEY_BUSY_DATA(p)  KEY((p), 8)
00094 #define KEY_COMMIT_DATA(p)  KEY((p), 9)
00095 
00096 #define KEY_XFUNC(p)    KEY((p), 1)
00097 #define KEY_XSTEP(p)    KEY((p), 2)
00098 #define KEY_XFINAL(p)   KEY((p), 3)
00099 #define KEY_XCOMPARE(p)   KEY((p), 1)
00100 #define KEY_XNEEDED(p)    KEY((p), 1)
00101 #define KEY_XAUTH(p)    KEY((p), 1)
00102 #define KEY_XPROGRESS(p)  KEY((p), 1)
00103 #define KEY_XTRACE(p)   KEY((p), 1)
00104 #define KEY_XBUSY(p)    KEY((p), 1)
00105 #define KEY_XCOMMIT(p)    KEY((p), 1)
00106 
00107 
00108 
00109 typedef struct
00110 {
00111   sqlite3 *   kr_db;
00112   lua_State *   L;
00113   int   key2value_pos;  /* Used by callback wrappers to find the key2value array on the lua stack */
00114 } DB;
00115 
00116 
00117 typedef struct
00118 {
00119   DB * db;
00120   sqlite3_stmt * stmt;
00121 } Stmt;
00122 
00123 
00124 typedef struct
00125 {
00126   DB * db;
00127 } CB_Data;
00128 
00129 
00130 
00131 
00132 
00133 
00134 static void push_private_table(lua_State * L, void * table_key);
00135 static void delete_private_value(lua_State * L, void * value_key);
00136 
00137 static CB_Data * new_cb_data(lua_State * L, DB * db);
00138 static CB_Data * get_cb_data(lua_State * L, DB * db, void * data_key);
00139 static CB_Data * get_named_cb_data(lua_State * L, DB * db, void * table_key, int name_pos);
00140 
00141 #define get_function_cb_data(L, db, name_pos) get_named_cb_data((L), (db), KEY_FUNCTION_TABLE(db), name_pos)
00142 #define get_collation_cb_data(L, db, name_pos)  get_named_cb_data((L), (db), KEY_COLLATION_TABLE(db), name_pos)
00143 #define get_collneeded_cb_data(L, db)   get_cb_data((L), (db), KEY_COLLNEEDED_DATA(db))
00144 #define get_authorizer_cb_data(L, db)   get_cb_data((L), (db), KEY_AUTHORIZER_DATA(db))
00145 #define get_progress_cb_data(L, db)   get_cb_data((L), (db), KEY_PROGRESS_DATA(db))
00146 #define get_trace_cb_data(L, db)    get_cb_data((L), (db), KEY_TRACE_DATA(db))
00147 #define get_busy_cb_data(L, db)     get_cb_data((L), (db), KEY_BUSY_DATA(db))
00148 #define get_commit_cb_data(L, db)   get_cb_data((L), (db), KEY_COMMIT_DATA(db))
00149 
00150 static void register_callback(lua_State * L, DB * db, void * cb_key, int callback_pos);
00151 static void init_callback_usage(lua_State * L, DB * db);
00152 static void push_callback(lua_State * L, DB * db, void * cb_key);
00153 
00154 static int pop_break_condition(lua_State * L);
00155 static void push_nil_or_string(lua_State * L, const char * str);
00156 
00157 
00158 
00159 
00160 static void push_private_table(lua_State * L, void * table_key)
00161 {
00162   lua_pushlightuserdata(L, table_key);
00163   lua_rawget(L, LUA_REGISTRYINDEX);
00164   if (lua_isnil(L, -1))
00165   {
00166     lua_pop(L, 1);
00167     lua_newtable(L);
00168     lua_pushlightuserdata(L, table_key);
00169     lua_pushvalue(L, -2);
00170     lua_rawset(L, LUA_REGISTRYINDEX);
00171   }
00172 }
00173 
00174 
00175 static void delete_private_value(lua_State * L, void * value_key)
00176 {
00177   lua_pushlightuserdata(L, value_key);
00178   lua_rawget(L, LUA_REGISTRYINDEX);
00179   if (!lua_isnil(L, -1))
00180   {
00181     lua_pushlightuserdata(L, value_key);
00182     lua_pushnil(L);
00183     lua_rawset(L, LUA_REGISTRYINDEX);
00184   }
00185   lua_pop(L, 1);
00186 }
00187 
00188 
00189 static CB_Data * new_cb_data(lua_State * L, DB * db)
00190 {
00191   CB_Data * cb_data = lua_newuserdata(L, sizeof(CB_Data));
00192   cb_data->db = db;
00193   return cb_data;
00194 }
00195 
00196 
00197 static CB_Data * get_cb_data(lua_State * L, DB * db, void * data_key)
00198 {
00199   CB_Data * cb_data;
00200   
00201   lua_pushlightuserdata(L, data_key);
00202   lua_rawget(L, LUA_REGISTRYINDEX);
00203   
00204   if (lua_isnil(L, -1))
00205   {
00206     lua_pushlightuserdata(L, data_key);
00207     cb_data = new_cb_data(L, db);
00208     lua_rawset(L, LUA_REGISTRYINDEX);
00209   }
00210   else
00211     cb_data = lua_touserdata(L, -1);
00212   
00213   lua_pop(L, 1);
00214   return cb_data;
00215 }
00216 
00217 
00218 static CB_Data * get_named_cb_data(lua_State * L, DB * db, void * table_key, int name_pos)
00219 {
00220   CB_Data * cb_data;
00221   
00222   push_private_table(L, table_key);
00223   lua_pushvalue(L, name_pos);
00224   lua_rawget(L, -2);
00225   
00226   if (lua_isnil(L, -1))
00227   {
00228     lua_pushvalue(L, name_pos);
00229     cb_data = new_cb_data(L, db);
00230     lua_rawset(L, LUA_REGISTRYINDEX);
00231   }
00232   else
00233     cb_data = lua_touserdata(L, -1);
00234   
00235   lua_pop(L, 2);
00236   return cb_data;
00237 }
00238 
00239 
00240 static void register_callback(lua_State * L, DB * db, void * cb_key, int callback_pos)
00241 {
00242   push_private_table(L, KEY_KEY2VALUE_TABLE(db));
00243   lua_pushlightuserdata(L, cb_key);
00244   lua_pushvalue(L, callback_pos);
00245   lua_rawset(L, -3);
00246   lua_pop(L, 1);
00247 }
00248 
00249 
00250 static void init_callback_usage(lua_State * L, DB * db)
00251 {
00252   db->L = L;
00253   db->key2value_pos = 0;  /* lazy initialized in push_callback() */
00254 }
00255 
00256 
00257 static void push_callback(lua_State * L, DB * db, void * cb_key)
00258 {
00259   if (db->key2value_pos == 0) /* lazy initializing of the key2value table */
00260   {
00261     push_private_table(L, KEY_KEY2VALUE_TABLE(db));
00262     db->key2value_pos = lua_gettop(L);
00263   }
00264   
00265   lua_pushlightuserdata(L, cb_key);
00266   lua_rawget(L, db->key2value_pos);
00267 }
00268 
00269 
00270 static int pop_break_condition(lua_State * L)
00271 {
00272   int result;
00273   
00274   if (lua_isnil(L, -1))
00275     result = 0;
00276   else if (lua_isboolean(L, -1))
00277     result = lua_toboolean(L, -1);
00278   else if (lua_isnumber(L, -1))
00279     result = lua_tonumber(L, -1);
00280   else
00281     result = 1;
00282   
00283   lua_pop(L, 1);
00284   return result;
00285 }
00286 
00287 
00288 static void push_nil_or_string(lua_State * L, const char * str)
00289 {
00290   if (str)
00291     lua_pushstring(L, str);
00292   else
00293     lua_pushnil(L);
00294 }
00295 
00296 
00297 
00298 
00299 /*
00300  *  Error Handling
00301  *  ==============
00302  *
00303  *  We try to work hard to be bullet proof. We even try to function 
00304  *  correctly in low memory situations.
00305  *  Usage errors of the API when detected raise an error also
00306  *  memory errors and such.
00307  *  Errors signaled from the sqlite library will result in the
00308  *  appropriate return codes.
00309  *
00310  */
00311 
00312 
00313 static void report_error(lua_State * L, const char * msg)
00314 {
00315   lua_settop(L, 0); /* Clear the stack to make sure, our error message will get a chance */
00316   lua_pushstring(L, msg);
00317   lua_error(L);
00318 }
00319 
00320 
00321 
00322 #define checkany(L, narg) ( luaL_checkany((L), (narg)) )
00323 #define checkstr(L, narg) ( luaL_checklstring((L), (narg), 0) )
00324 #define checknumber(L, narg)  ( luaL_checknumber((L), (narg)) )
00325 #define checkint(L, narg) ( (int)   checknumber((L), (narg)) )
00326 #define checkdouble(L, narg)  ( (double)  checknumber((L), (narg)) )
00327 
00328 static void * checkudata(lua_State * L, int narg)
00329 {
00330   if (!lua_isuserdata(L, narg))
00331     luaL_typerror(L, narg, "userdata");
00332   return lua_touserdata(L, narg);
00333 }
00334 
00335 #define checkcontext(L, narg) ( (sqlite3_context *) checkudata((L), (narg)) )
00336 #define checkvalues(L, narg)  ( (sqlite3_value **)  checkudata((L), (narg)) )
00337 #define checkstmt(L, narg)  ( (Stmt *)    checkudata((L), (narg)) )
00338 #define checkdb(L, narg)  ( (DB *)    checkudata((L), (narg)) )
00339 
00340 static sqlite3_stmt * checkstmt_stmt(lua_State * L, int narg)
00341 {
00342   return checkstmt(L, narg)->stmt;
00343 }
00344 
00345 static sqlite3 * checkdb_sqlite3(lua_State * L, int narg)
00346 {
00347   return checkdb(L, narg)->kr_db;
00348 }
00349 
00350 /*
00351 static pthread_mutex_t* checkdb_mutex(lua_State * L, int narg)
00352 {
00353   return &(checkdb(L, narg)->mutex);
00354 }
00355 */
00356 
00357 static int checknilornoneorfunc(lua_State * L, int narg)
00358 {
00359   if (lua_isnil(L, narg) || lua_isnone(L, narg))
00360     return 0;
00361   if (lua_isfunction(L, narg))
00362     return 1;
00363   luaL_typerror(L, narg, "nil, none or function");
00364   return 0; /* never reached, make compiler happy... */
00365 }
00366 
00367 
00368 
00369 
00370 
00371 
00372 FUNC( l_sqlite3_bind_null )
00373 {
00374   lua_pushnumber(L, sqlite3_bind_null(checkstmt_stmt(L, 1), checkint(L, 2)) );
00375   return 1;
00376 }
00377 
00378 
00379 FUNC( l_sqlite3_bind_text )
00380 {
00381   lua_pushnumber(L, sqlite3_bind_text(checkstmt_stmt(L, 1), checkint(L, 2), checkstr(L, 3), lua_strlen(L, 3), SQLITE_TRANSIENT) );
00382   return 1;
00383 }
00384 
00385 
00386 FUNC( l_sqlite3_bind_blob )
00387 {
00388   lua_pushnumber(L, sqlite3_bind_blob(checkstmt_stmt(L, 1), checkint(L, 2), checkstr(L, 3), lua_strlen(L, 3), SQLITE_TRANSIENT) );
00389   return 1;
00390 }
00391 
00392 
00393 FUNC( l_sqlite3_bind_int )
00394 {
00395   lua_pushnumber(L, sqlite3_bind_int(checkstmt_stmt(L, 1), checkint(L, 2), checkint(L, 3)) );
00396   return 1;
00397 }
00398 
00399 
00400 FUNC( l_sqlite3_bind_double )
00401 {
00402   lua_pushnumber(L, sqlite3_bind_double(checkstmt_stmt(L, 1), checkint(L, 2), checkdouble(L, 3)) );
00403   return 1;
00404 }
00405 
00406 
00407 FUNC( l_sqlite3_bind_number )
00408 {
00409   sqlite3_stmt * stmt   = checkstmt_stmt(L, 1);
00410   int index     = checkint(L, 2);
00411   lua_Number number   = checknumber(L, 3);
00412   
00413   if (IS_INT(number))
00414     lua_pushnumber(L, sqlite3_bind_int(stmt, index, (int)number) );
00415   else
00416     lua_pushnumber(L, sqlite3_bind_double(stmt, index, (double)number) );
00417   
00418   return 1;
00419 }
00420 
00421 
00422 FUNC( l_sqlite3_bind )
00423 {
00424   sqlite3_stmt * stmt = checkstmt_stmt(L, 1);
00425   int index   = checkint(L, 2);
00426   
00427   switch(lua_type(L, 3))
00428   {
00429     case LUA_TNONE:
00430     case LUA_TNIL:
00431       lua_pushnumber(L, sqlite3_bind_null(stmt, index) );
00432       break;
00433     
00434     case LUA_TNUMBER:
00435       {
00436         lua_Number number = lua_tonumber(L, 3);
00437         
00438         if (IS_INT(number))
00439           lua_pushnumber(L, sqlite3_bind_int(stmt, index, (int)number) );
00440         else
00441           lua_pushnumber(L, sqlite3_bind_double(stmt, index, (double)number) );
00442       }
00443       break;
00444     
00445     case LUA_TBOOLEAN:
00446       if (lua_toboolean(L, 3))
00447         lua_pushnumber(L, sqlite3_bind_int(stmt, index, 1) );
00448       else
00449         lua_pushnumber(L, sqlite3_bind_int(stmt, index, 0) );
00450       break;
00451     
00452     case LUA_TSTRING:
00453       lua_pushnumber(L, sqlite3_bind_text(stmt, index, lua_tostring(L, 3), lua_strlen(L, 3), SQLITE_TRANSIENT) );
00454       break;
00455     
00456     default:
00457       luaL_argerror(L, 3, "nil, boolean, number or string expected");
00458   }
00459   
00460   return 1;
00461 }
00462 
00463 
00464 FUNC( l_sqlite3_bind_parameter_count )
00465 {
00466   lua_pushnumber(L, sqlite3_bind_parameter_count(checkstmt_stmt(L, 1)) );
00467   return 1;
00468 }
00469 
00470 
00471 FUNC( l_sqlite3_bind_parameter_name )
00472 {
00473   const char * name = sqlite3_bind_parameter_name(checkstmt_stmt(L, 1), checkint(L, 2));
00474   if (name)
00475     lua_pushstring(L, name);
00476   else
00477     lua_pushnil(L);
00478   return 1;
00479 }
00480 
00481 
00482 FUNC( l_sqlite3_bind_parameter_name_x )
00483 {
00484   const char * name = sqlite3_bind_parameter_name(checkstmt_stmt(L, 1), checkint(L, 2));
00485   if (name && *name)
00486     lua_pushstring(L, name + 1);  /* Ignore leading '$' or ':' */
00487   else
00488     lua_pushnil(L);
00489   return 1;
00490 }
00491 
00492 
00493 FUNC( l_sqlite3_busy_timeout )
00494 {
00495   DB * db = checkdb(L, 1);
00496   int timeout = checkint(L, 2);
00497   
00498   delete_private_value(L, KEY_BUSY_DATA(db));
00499   
00500   lua_pushnumber(L, sqlite3_busy_timeout(db->kr_db, timeout) );
00501   return 1;
00502 }
00503 
00504 
00505 FUNC( l_sqlite3_changes )
00506 {
00507   lua_pushnumber(L, sqlite3_changes(checkdb_sqlite3(L, 1)) );
00508   return 1;
00509 }
00510 
00511 
00512 FUNC( l_sqlite3_close )
00513 {
00514   DB * db = checkdb(L, 1); // xxx the arg need not be popped, apparently
00515   
00516   delete_private_value(L, KEY_KEY2VALUE_TABLE(db));
00517   delete_private_value(L, KEY_FUNCTION_TABLE(db));
00518   delete_private_value(L, KEY_COLLATION_TABLE(db));
00519   delete_private_value(L, KEY_COLLNEEDED_DATA(db));
00520   delete_private_value(L, KEY_AUTHORIZER_DATA(db));
00521   delete_private_value(L, KEY_PROGRESS_DATA(db));
00522   delete_private_value(L, KEY_TRACE_DATA(db));
00523   delete_private_value(L, KEY_BUSY_DATA(db));
00524   delete_private_value(L, KEY_COMMIT_DATA(db));
00525 
00526   // I suppose Lua will GC the actual DB struct.
00527   return 0;
00528 }
00529 
00530 
00531 typedef const char * (*column_text_blob_t)(sqlite3_stmt *, int);
00532 
00533 static int l_sqlite3_column_text_or_blob(lua_State * L, column_text_blob_t column_text_blob)
00534 {
00535   sqlite3_stmt * stmt =  checkstmt_stmt(L, 1);
00536   int column    = checkint(L, 2);
00537   
00538   lua_pushlstring(L,  column_text_blob(stmt, column), sqlite3_column_bytes(stmt, column));
00539   return 1;
00540 }
00541 
00542 FUNC( l_sqlite3_column_blob )
00543 {
00544   return l_sqlite3_column_text_or_blob(L, (column_text_blob_t)sqlite3_column_blob);
00545 }
00546 
00547 FUNC( l_sqlite3_column_text )
00548 {
00549   return l_sqlite3_column_text_or_blob(L, (column_text_blob_t)sqlite3_column_text); /* FIXME: remove cast when API changes!!! */
00550 }
00551 
00552 
00553 FUNC( l_sqlite3_column_int )
00554 {
00555   lua_pushnumber(L, sqlite3_column_int(checkstmt_stmt(L, 1), checkint(L, 2)) );
00556   return 1;
00557 }
00558 
00559 
00560 FUNC( l_sqlite3_column_double )
00561 {
00562   lua_pushnumber(L, sqlite3_column_double(checkstmt_stmt(L, 1), checkint(L, 2)) );
00563   return 1;
00564 }
00565 
00566 
00567 FUNC( l_sqlite3_column_number )
00568 {
00569   sqlite3_stmt * stmt = checkstmt_stmt(L, 1);
00570   int column    = checkint(L, 2);
00571   
00572   if ( sqlite3_column_type(stmt, column) == SQLITE_INTEGER )
00573     lua_pushnumber(L, sqlite3_column_int(stmt, column) );
00574   else
00575     lua_pushnumber(L,sqlite3_column_double(stmt, column) );
00576   
00577   return 1;
00578 }
00579 
00580 
00581 
00582   
00583 static void push_column(lua_State * L, sqlite3_stmt * stmt, int column)
00584 {
00585   switch(sqlite3_column_type(stmt, column))
00586   {
00587     case SQLITE_NULL:
00588       lua_pushnil(L);
00589       break;
00590     
00591     case SQLITE_INTEGER:
00592       lua_pushnumber(L, sqlite3_column_int(stmt, column));
00593       break;
00594     
00595     case SQLITE_FLOAT:
00596       lua_pushnumber(L, sqlite3_column_double(stmt, column));
00597       break;
00598     
00599     case SQLITE_TEXT:
00600       lua_pushlstring(L, (const char*)sqlite3_column_text(stmt, column), sqlite3_column_bytes(stmt, column));
00601       break;
00602     
00603     case SQLITE_BLOB:
00604       lua_pushlstring(L, sqlite3_column_blob(stmt, column), sqlite3_column_bytes(stmt, column));
00605       break;
00606     
00607     default:
00608       lua_pushboolean(L, 0);
00609   }
00610 }
00611 
00612 
00613 FUNC( l_sqlite3_column )
00614 {
00615   push_column(L, checkstmt_stmt(L, 1), checkint(L, 2));
00616   return 1;
00617 }
00618 
00619 
00620 /*
00621  * mode: 0 = direct, 1 = integer, 2 = alphanumeric
00622  */
00623 static int l_sqlite3_row_mode(lua_State * L, int mode)
00624 {
00625   /* Old code / Just a reminder / To be removed: 
00626   ** checkargs(L, 1, 2, CHECK_PTR, CHECK_NILTABLE, 0);
00627   */
00628   
00629   sqlite3_stmt * stmt = checkstmt_stmt(L, 1);
00630   int num_columns = sqlite3_data_count(stmt); /* Maybe wrong state, so don't use sqlite3_column_count */
00631   int index;
00632   
00633   /* XXX Should really be cleaned up... Fixme! */
00634   
00635   if (mode == 0)
00636     lua_checkstack(L, num_columns);
00637   else
00638     if (!lua_istable(L, -1))
00639       lua_newtable(L);
00640   
00641   for (index=0; index<num_columns; index++)
00642     switch(mode)
00643     {
00644       case 0: /* direct mode */
00645         push_column(L, stmt, index);
00646         break;
00647       
00648       case 1: /* integer mode */
00649         push_column(L, stmt, index);
00650         lua_rawseti(L, -2, index+1);
00651         break;
00652       
00653       case 2: /* alphanumeric mode */
00654         lua_pushstring(L, sqlite3_column_name(stmt, index));
00655         push_column(L, stmt, index);
00656         lua_rawset(L, -3);
00657         break;
00658       
00659       default:
00660         report_error(L, "libluasqlite3: Internal error in sqlite3_row_mode");
00661     }
00662   
00663   if (mode)
00664     return 1;
00665   else
00666     return num_columns;
00667 }
00668 
00669 FUNC( l_sqlite3_drow )
00670 {
00671   return l_sqlite3_row_mode(L, 0);
00672 }
00673 
00674 FUNC( l_sqlite3_irow )
00675 {
00676   return l_sqlite3_row_mode(L, 1);
00677 }
00678 
00679 FUNC( l_sqlite3_arow )
00680 {
00681   return l_sqlite3_row_mode(L, 2);
00682 }
00683 
00684 
00685 FUNC( l_sqlite3_column_type )
00686 {
00687   lua_pushnumber(L, sqlite3_column_type(checkstmt_stmt(L, 1), checkint(L, 2)) );
00688   return 1;
00689 }
00690 
00691 
00692 FUNC( l_sqlite3_column_count )
00693 {
00694   lua_pushnumber(L, sqlite3_column_count(checkstmt_stmt(L, 1)) );
00695   return 1;
00696 }
00697 
00698 
00699 
00700 static int l_sqlite3_column_info(lua_State * L, const char * (*info_func)(sqlite3_stmt*,int) )
00701 {
00702   const char * info = info_func(checkstmt_stmt(L, 1), checkint(L, 2));
00703   
00704   if (info)
00705     lua_pushstring(L, info);
00706   else
00707     lua_pushstring(L, "");
00708   
00709   return 1;
00710 }
00711 
00712 FUNC( l_sqlite3_column_decltype )
00713 {
00714   return l_sqlite3_column_info(L, sqlite3_column_decltype);
00715 }
00716 
00717 FUNC( l_sqlite3_column_name )
00718 {
00719   return l_sqlite3_column_info(L, sqlite3_column_name);
00720 }
00721 
00722 
00723 FUNC( l_sqlite3_complete )
00724 {
00725   lua_pushboolean(L, sqlite3_complete(checkstr(L, 1)) );
00726   return 1;
00727 }
00728 
00729 
00730 FUNC( l_sqlite3_data_count )
00731 {
00732   lua_pushnumber(L, sqlite3_data_count(checkstmt_stmt(L, 1)) );
00733   return 1;
00734 }
00735 
00736 
00737 FUNC( l_sqlite3_errcode )
00738 {
00739   lua_pushnumber(L, sqlite3_errcode(checkdb_sqlite3(L, 1)) );
00740   return 1;
00741 }
00742 
00743 
00744 FUNC( l_sqlite3_errmsg )
00745 {
00746   lua_pushstring(L, sqlite3_errmsg(checkdb_sqlite3(L, 1)) );
00747   return 1;
00748 }
00749 
00750 
00751 FUNC( l_sqlite3_finalize )
00752 {
00753   lua_pushnumber(L, sqlite3_finalize(checkstmt_stmt(L, 1)) );
00754   return 1;
00755 }
00756 
00757 
00758 FUNC( l_sqlite3_interrupt )
00759 {
00760   sqlite3_interrupt(checkdb_sqlite3(L, 1));
00761   return 0;
00762 }
00763 
00764 
00765 FUNC( l_sqlite3_last_insert_rowid )
00766 {
00767   lua_pushnumber(L, sqlite3_last_insert_rowid(checkdb_sqlite3(L, 1)) );
00768   return 1;
00769 }
00770 
00771 
00772 FUNC( l_sqlite3_open )
00773 {
00774   sqlite3 * sqlite3   = ac_global_LogDb->db;
00775   DB * db = (DB *) lua_newuserdata(L, sizeof(DB));
00776   db->kr_db = sqlite3;
00777   return 1; /* database */
00778 }
00779 
00780 
00781 FUNC( l_sqlite3_prepare )
00782 {
00783   /* XXX This piece of code is not so nice. This piece should be redone... */
00784   
00785   DB * db     = checkdb(L, 1);
00786   const char * sql    = checkstr(L, 2);
00787   int sql_size      = lua_strlen(L, 2);
00788   const char * leftover   = 0;
00789   sqlite3_stmt * sqlite3_stmt   = 0;
00790   int error, leftover_size;
00791   Stmt * stmt;
00792   
00793   init_callback_usage(L, db);   /* Needed by trace handler... FIXME: maybe to be removed... */
00794   
00795   error = sqlite3_prepare(db->kr_db, sql, sql_size, &sqlite3_stmt, &leftover);
00796   
00797   leftover_size = leftover ? sql + sql_size - leftover : 0;
00798   
00799   lua_pushnumber(L, error);
00800   
00801   stmt = lua_newuserdata(L, sizeof(Stmt));
00802   stmt->db = checkdb(L, 1);
00803   stmt->stmt = sqlite3_stmt;
00804   
00805   if (leftover_size > 0)
00806     lua_pushlstring(L, leftover, leftover_size);
00807   else
00808     lua_pushnil(L);
00809   
00810   return 3; /* error code, statement, left over */
00811 }
00812 
00813 
00814 FUNC( l_sqlite3_reset )
00815 {
00816   lua_pushnumber(L, sqlite3_reset(checkstmt_stmt(L, 1)) );
00817   return 1;
00818 }
00819 
00820 
00821 FUNC( l_sqlite3_step )
00822 {
00823   Stmt * stmt = checkstmt(L, 1);
00824   init_callback_usage(L, stmt->db);
00825   lua_pushnumber(L, sqlite3_step(stmt->stmt) );
00826   return 1;
00827 }
00828 
00829 
00830 FUNC( l_sqlite3_total_changes )
00831 {
00832   lua_pushnumber(L, sqlite3_total_changes(checkdb_sqlite3(L, 1)) );
00833   return 1;
00834 }
00835 
00836 
00837 
00838 static int exec_callback_wrapper(void * cb_data, int num_columns, char ** values, char ** names)
00839 {
00840   int index;
00841   lua_State * L = (lua_State *) cb_data;
00842   
00843   lua_pushvalue(L, 3);  /* Callback function, resulting stack position 4 */
00844   lua_newtable(L);  /* Value array, resulting stack position 5 */
00845   lua_newtable(L);  /* Names array, resulting stack position 6 */
00846   
00847   for(index=0; index<num_columns; index++)
00848   {
00849     lua_pushstring(L, values[index]); /* Value */
00850     lua_rawseti(L, 5, index+1);   /* C-index are 0 based, Lua index are 1 based... */
00851     
00852     lua_pushstring(L, names[index]);  /* Name */
00853     lua_rawseti(L, 6, index+1);
00854   }
00855   
00856   if ( lua_pcall(L, 2, 1, 0) )    /* In: 2 arrays, Out: result code, On error: leave */
00857   {
00858     lua_pop(L, 1);      /* delete error message */
00859     return 1;
00860   }
00861   
00862   return pop_break_condition(L);
00863 }
00864 
00865 FUNC( l_sqlite3_exec )
00866 {
00867   DB * db   = checkdb(L, 1);
00868   sqlite3_callback cb;
00869   void * cb_data;
00870   
00871   if ( checknilornoneorfunc(L, 3) )
00872   {
00873     cb = exec_callback_wrapper;
00874     cb_data = L;
00875   }
00876   else
00877   {
00878     cb      = 0;
00879     cb_data = 0;
00880   }
00881   
00882   init_callback_usage(L, db);
00883   
00884   lua_pushnumber(L, sqlite3_exec(db->kr_db, checkstr(L, 2), cb, cb_data, 0) );
00885   return 1;
00886 }
00887 
00888 
00889 
00890 static void func_callback_wrapper(int which, sqlite3_context * ctx, int num_args, sqlite3_value ** values)
00891 {
00892   CB_Data * cb_data = sqlite3_user_data(ctx);
00893   DB *    db  = cb_data->db;
00894   lua_State *   L = db->L;
00895   
00896   switch(which)
00897   {
00898     case 0: push_callback(L, db, KEY_XFUNC(cb_data)); break;
00899     case 1: push_callback(L, db, KEY_XSTEP(cb_data)); break;
00900     case 2: push_callback(L, db, KEY_XFINAL(cb_data)); break;
00901   }
00902   
00903   if (lua_isnil(L, -1))
00904   {
00905     lua_pop(L, 1);
00906     fprintf(stderr, "libluasqlite3: func_callback_wrapper: Warning: function is null\n");
00907     return;
00908   }
00909   
00910   lua_pushlightuserdata(L, ctx);
00911   
00912   if (values)
00913   {
00914     lua_pushnumber(L, num_args);
00915     lua_pushlightuserdata(L, values);
00916   }
00917   
00918   if (lua_pcall(L, values ? 3 : 1, 0, 0))
00919   {
00920     fprintf(stderr, "libluasqlite3: func_callback_wrapper: Warning: user function error: %s\n", lua_tostring(L, -1));
00921     sqlite3_result_error(ctx, lua_tostring(L, -1), lua_strlen(L, -1));
00922     lua_pop(L, 1);
00923   }
00924 }
00925 
00926 static void xfunc_callback_wrapper(sqlite3_context * ctx, int num_args, sqlite3_value ** values)
00927 {
00928   func_callback_wrapper(0, ctx, num_args, values);
00929 }
00930 
00931 static void xstep_callback_wrapper(sqlite3_context * ctx, int num_args, sqlite3_value ** values)
00932 {
00933   func_callback_wrapper(1, ctx, num_args, values);
00934 }
00935 
00936 static void xfinal_callback_wrapper(sqlite3_context * ctx)
00937 {
00938   func_callback_wrapper(2, ctx, 0, 0);
00939 }
00940 
00941 FUNC( l_sqlite3_create_function )
00942 {
00943   DB * db   = checkdb(L, 1);
00944   CB_Data * cb_data = get_function_cb_data(L, db, 2);
00945   
00946   void (*xfunc)(sqlite3_context *, int, sqlite3_value **) = 0;
00947   void (*xstep)(sqlite3_context *, int, sqlite3_value **) = 0;
00948   void (*xfinal)(sqlite3_context *) = 0;
00949   
00950   if ( checknilornoneorfunc(L, 4) )
00951     xfunc = xfunc_callback_wrapper;
00952   else
00953     xfunc = 0;
00954   
00955   if ( checknilornoneorfunc(L, 5) )
00956     xstep = xstep_callback_wrapper;
00957   else
00958     xstep = 0;
00959   
00960   if ( checknilornoneorfunc(L, 6) )
00961     xfinal = xfinal_callback_wrapper;
00962   else
00963     xfinal = 0;
00964   
00965   register_callback(L, db, KEY_XFUNC(cb_data), 4);
00966   register_callback(L, db, KEY_XSTEP(cb_data), 5);
00967   register_callback(L, db, KEY_XFINAL(cb_data), 6);
00968   
00969   lua_pushnumber(L,
00970     sqlite3_create_function (
00971       db->kr_db,
00972       checkstr(L, 2),
00973       checkint(L, 3),
00974       SQLITE_UTF8,
00975       cb_data,
00976       xfunc,
00977       xstep,
00978       xfinal ));
00979   
00980   return 1;
00981 }
00982 
00983 
00984 static int xcompare_callback_wrapper(void * cb_data, int len_a, const void * str_a, int len_b, const void * str_b)
00985 {
00986   DB *    db = CB_DATA(cb_data)->db;
00987   lua_State * L  = db->L;
00988   int result;
00989   
00990   push_callback(L, db, KEY_XCOMPARE(cb_data));
00991   lua_pushlstring(L, str_a, len_a);
00992   lua_pushlstring(L, str_b, len_b);
00993   
00994   if ( lua_pcall(L, 2, 1, 0) )
00995     result = 0;     /* No way to signal errors to sqlite */
00996   else
00997     result = (int) lua_tonumber(L, -1);
00998   
00999   lua_pop(L, 1);
01000   return result;
01001 }
01002 
01003 FUNC( l_sqlite3_create_collation )
01004 {
01005   DB * db   = checkdb(L, 1);
01006   CB_Data * cb_data = get_collation_cb_data(L, db, 2);
01007   
01008   int (*xcompare)(void *, int, const void *, int, const void *);
01009   
01010   if ( checknilornoneorfunc(L, 3) )
01011     xcompare = xcompare_callback_wrapper;
01012   else
01013     xcompare = 0;
01014   
01015   register_callback(L, db, KEY_XCOMPARE(cb_data), 3);
01016   
01017   lua_pushnumber(L, sqlite3_create_collation(
01018                 db->kr_db, checkstr(L, 2), SQLITE_UTF8, cb_data, xcompare) );
01019   return 1;
01020 }
01021 
01022 
01023 static void xneeded_callback_wrapper(void * cb_data, sqlite3 * sqlite3, int eTextRep, const char * collation_name)
01024 {
01025   DB *    db = CB_DATA(cb_data)->db;
01026   lua_State *   L  = db->L;
01027 
01028   (void)sqlite3;
01029   (void)eTextRep;
01030   
01031   push_callback(L, db, KEY_XNEEDED(cb_data));
01032   lua_pushstring(L, collation_name);
01033   
01034   if (lua_pcall(L, 1, 0, 0))
01035     lua_pop(L, 1);
01036 }
01037 
01038 FUNC( l_sqlite3_collation_needed )
01039 {
01040   DB * db   = checkdb(L, 1);
01041   CB_Data * cb_data = get_collneeded_cb_data(L, db);
01042   
01043   void (*xneeded)(void *, sqlite3 *, int eTextRep, const char *);
01044   
01045   if ( checknilornoneorfunc(L, 2) )
01046     xneeded = xneeded_callback_wrapper;
01047   else
01048     xneeded = 0;
01049   
01050   register_callback(L, db, KEY_XNEEDED(cb_data), 2);
01051   
01052   lua_pushnumber(L, sqlite3_collation_needed(db->kr_db, cb_data, xneeded) );
01053   return 1;
01054 }
01055 
01056 
01057 
01058 
01059 static void xtrace_callback_wrapper(void * cb_data, const char * str)
01060 {
01061   DB *    db = CB_DATA(cb_data)->db;
01062   lua_State * L  = db->L;
01063   
01064   push_callback(L, db, KEY_XTRACE(cb_data));
01065   lua_pushstring(L, str);
01066   
01067   if ( lua_pcall(L, 1, 0, 0) )
01068     lua_pop(L, 1);    /* pop error message and delete it (errors are ignored) */
01069 }
01070 
01071 FUNC( l_sqlite3_trace )
01072 {
01073   DB * db   = checkdb(L, 1);
01074   CB_Data * cb_data = get_trace_cb_data(L, db);
01075   
01076   void (*xtrace)(void *, const char *);
01077   
01078   if ( checknilornoneorfunc(L, 2) )
01079     xtrace = xtrace_callback_wrapper;
01080   else
01081     xtrace = 0;
01082   
01083   register_callback(L, db, KEY_XTRACE(cb_data), 2);
01084   
01085   sqlite3_trace(db->kr_db, xtrace, cb_data);
01086   
01087   lua_pushnumber(L, SQLITE_OK);
01088   return 1;
01089 }
01090 
01091 
01092 
01093 
01094 FUNC( l_sqlite3_result_null )
01095 {
01096   sqlite3_result_null(checkcontext(L, 1));
01097   return 0;
01098 }
01099 
01100 
01101 FUNC( l_sqlite3_result_error )
01102 {
01103   sqlite3_result_error(checkcontext(L, 1), checkstr(L, 2), lua_strlen(L, 2));
01104   return 0;
01105 }
01106 
01107 
01108 FUNC( l_sqlite3_result_double )
01109 {
01110   sqlite3_result_double(checkcontext(L, 1), checkdouble(L, 2));
01111   return 0;
01112 }
01113 
01114 
01115 FUNC( l_sqlite3_result_int )
01116 {
01117   sqlite3_result_int(checkcontext(L, 1), checkint(L, 2));
01118   return 0;
01119 }
01120 
01121 
01122 FUNC( l_sqlite3_result_number )
01123 {
01124   lua_Number number = checknumber(L, 2);
01125   
01126   if (IS_INT(number))
01127     sqlite3_result_int(checkcontext(L, 1), (int)number);
01128   else
01129     sqlite3_result_double(checkcontext(L, 1), (double)number);
01130   
01131   return 0;
01132 }
01133 
01134 
01135 FUNC( l_sqlite3_result_blob )
01136 {
01137   sqlite3_result_blob(checkcontext(L, 1), checkstr(L, 2), lua_strlen(L, 2), SQLITE_TRANSIENT);
01138   return 0;
01139 }
01140 
01141 
01142 FUNC( l_sqlite3_result_text )
01143 {
01144   sqlite3_result_text(checkcontext(L, 1), checkstr(L, 2), lua_strlen(L, 2), SQLITE_TRANSIENT);
01145   return 0;
01146 }
01147 
01148 
01149 FUNC( l_sqlite3_result_value )
01150 {
01151   sqlite3_value ** values = checkvalues(L, 2);
01152   int index = checkint(L, 3);
01153   sqlite3_result_value(checkcontext(L, 1), values[index] );
01154   return 0;
01155 }
01156 
01157 FUNC( l_sqlite3_result )
01158 {
01159   sqlite3_context * context = checkcontext(L, 1);
01160   
01161   switch(lua_type(L, 2))
01162   {
01163     case LUA_TNONE:
01164     case LUA_TNIL:
01165       sqlite3_result_null(context);
01166       break;
01167     
01168     case LUA_TNUMBER:
01169       {
01170         lua_Number number = lua_tonumber(L, 2);
01171         
01172         if (IS_INT(number))
01173           sqlite3_result_int(context, (int)number);
01174         else
01175           sqlite3_result_double(context, (double)number);
01176       }
01177       break;
01178     
01179     case LUA_TBOOLEAN:
01180       if (lua_toboolean(L, 2))
01181         sqlite3_result_int(context, 1);
01182       else
01183         sqlite3_result_int(context, 0);
01184       break;
01185     
01186     case LUA_TSTRING:
01187       sqlite3_result_text(context, lua_tostring(L, 2), lua_strlen(L, 2), SQLITE_TRANSIENT);
01188       break;
01189     
01190     default:
01191       report_error(L, "libluasqlite3: Api usage error: Invalid argument to l_sqlite3_result:");
01192   }
01193   
01194   return 0;
01195 }
01196 
01197 
01198 FUNC( l_sqlite3_aggregate_count )
01199 {
01200   lua_pushnumber(L, sqlite3_aggregate_count(checkcontext(L, 1)) );
01201   return 1;
01202 }
01203 
01204 
01205 FUNC( l_sqlite3_aggregate_context )
01206 {
01207   lua_pushlightuserdata(L, sqlite3_aggregate_context(checkcontext(L, 1), 1));
01208   return 1;
01209 }
01210 
01211 
01212 FUNC( l_sqlite3_value_int )
01213 {
01214   sqlite3_value ** values = checkvalues(L, 1);
01215   int index = checkint(L, 2);
01216   lua_pushnumber(L, sqlite3_value_int(values[index]) );
01217   return 1;
01218 }
01219 
01220 
01221 FUNC( l_sqlite3_value_double )
01222 {
01223   sqlite3_value ** values = checkvalues(L, 1);
01224   int index = checkint(L, 2);
01225   lua_pushnumber(L, sqlite3_value_double(values[index]) );
01226   return 1;
01227 }
01228 
01229 
01230 FUNC( l_sqlite3_value_number )
01231 {
01232   sqlite3_value ** values = checkvalues(L, 1);
01233   int index = checkint(L, 2);
01234   sqlite3_value * value = values[index];
01235   if (sqlite3_value_type(value) == SQLITE_INTEGER)
01236     lua_pushnumber(L, sqlite3_value_int(value) );
01237   else
01238     lua_pushnumber(L, sqlite3_value_double(value) );
01239   return 1;
01240 }
01241 
01242 
01243 FUNC( l_sqlite3_value_blob )
01244 {
01245   sqlite3_value ** values = checkvalues(L, 1);
01246   int index = checkint(L, 2);
01247   lua_pushlstring(L, sqlite3_value_blob(values[index]), sqlite3_value_bytes(values[index]) );
01248   return 1;
01249 }
01250 
01251 
01252 FUNC( l_sqlite3_value_text )
01253 {
01254   sqlite3_value ** values = checkvalues(L, 1);
01255   int index = checkint(L, 2);
01256   lua_pushlstring(L, (const char*)sqlite3_value_text(values[index]), sqlite3_value_bytes(values[index]) );
01257   return 1;
01258 }
01259 
01260 
01261 FUNC( l_sqlite3_value )
01262 {
01263   sqlite3_value ** values = checkvalues(L, 1);
01264   int index     = checkint(L, 2);
01265   sqlite3_value * value   = values[index];
01266   
01267   switch(sqlite3_value_type(value))
01268   {
01269     case SQLITE_INTEGER:
01270       lua_pushnumber(L, sqlite3_value_int(value) );
01271       break;
01272     
01273     case SQLITE_FLOAT:
01274       lua_pushnumber(L, sqlite3_value_double(value) );
01275       break;
01276     
01277     case SQLITE_TEXT:
01278       lua_pushlstring(L, (const char*)sqlite3_value_text(value), sqlite3_value_bytes(value) );
01279       break;
01280     
01281     case SQLITE_BLOB:
01282       lua_pushlstring(L, sqlite3_value_blob(value), sqlite3_value_bytes(value) );
01283       break;
01284     
01285     case SQLITE_NULL:
01286       lua_pushnil(L);
01287       break;
01288     
01289     default:
01290       report_error(L, "libluasqlite3: Internal error: Unknonw SQLITE data type.");
01291   }
01292   return 1;
01293 }
01294 
01295 
01296 FUNC( l_sqlite3_value_type )
01297 {
01298   sqlite3_value ** values = checkvalues(L, 1);
01299   int index = checkint(L, 2);
01300   lua_pushnumber(L, sqlite3_value_type(values[index]) );
01301   return 1;
01302 }
01303 
01304 
01305 FUNC( l_sqlite3_libversion )
01306 {
01307   lua_pushstring(L, sqlite3_libversion() );
01308   return 1;
01309 }
01310 
01311 
01312 static int xcommit_callback_wrapper(void * cb_data)
01313 {
01314   DB *    db = CB_DATA(cb_data)->db;
01315   lua_State * L  = db->L;
01316   
01317   push_callback(L, db, KEY_XCOMMIT(cb_data));
01318   
01319   if ( lua_pcall(L, 0, 1, 0) )
01320   {
01321     lua_pop(L, 1);
01322     return 1;   /* on errors, rollback */
01323   }
01324   
01325   return pop_break_condition(L);
01326 }
01327 
01328 FUNC( l_sqlite3_commit_hook )
01329 {
01330   DB * db = checkdb(L, 1);
01331   CB_Data * cb_data = get_commit_cb_data(L, db);
01332   
01333   int (*xcommit)(void *);
01334   
01335   if ( checknilornoneorfunc(L, 1) )
01336     xcommit = xcommit_callback_wrapper;
01337   else
01338     xcommit = 0;
01339   
01340   register_callback(L, db, KEY_XCOMMIT(cb_data), 2);
01341   sqlite3_commit_hook(db->kr_db, xcommit, cb_data);
01342   
01343   lua_pushnumber(L, sqlite3_errcode(db->kr_db) );
01344   return 1;
01345 }
01346 
01347 
01348 static int xprogress_callback_wrapper(void * cb_data)
01349 {
01350   DB *    db = CB_DATA(cb_data)->db;
01351   lua_State * L  = db->L;
01352   
01353   push_callback(L, db, KEY_XPROGRESS(cb_data));
01354   
01355   if ( lua_pcall(L, 0, 1, 0) )
01356   {
01357     lua_pop(L, 1);
01358     return 1;   /* on errors, rollback */
01359   }
01360   
01361   return pop_break_condition(L);
01362   return 1;
01363 }
01364 
01365 FUNC( l_sqlite3_progress_handler )
01366 {
01367   DB * db   = checkdb(L, 1);
01368   CB_Data * cb_data = get_progress_cb_data(L, db);
01369   
01370   int (*xprogress)(void *);
01371   
01372   if ( checknilornoneorfunc(L, 1) )
01373     xprogress = xprogress_callback_wrapper;
01374   else
01375     xprogress = 0;
01376   
01377   register_callback(L, db, KEY_XPROGRESS(cb_data), 3);
01378   sqlite3_progress_handler(db->kr_db, checkint(L, 2), xprogress, cb_data);
01379   
01380   lua_pushnumber(L, sqlite3_errcode(db->kr_db) );
01381   return 1;
01382 }
01383 
01384 
01385 static int xbusy_callback_wrapper(void * cb_data, int num_called)
01386 {
01387   DB *    db = CB_DATA(cb_data)->db;
01388   lua_State * L  = db->L;
01389   
01390   push_callback(L, db, KEY_XBUSY(cb_data));
01391   lua_pushnumber(L, num_called);
01392   
01393   if ( lua_pcall(L, 1, 1, 0) )
01394   {
01395     lua_pop(L, 1);
01396     return 0;   /* On errors, sqlite should return SQLITE_BUSY */
01397   }
01398   
01399   return pop_break_condition(L);  /* WARNING: In reality, the semantic is inverted !!!*/
01400 }
01401 
01402 FUNC( l_sqlite3_busy_handler )
01403 {
01404   DB * db   = checkdb(L, 1);
01405   CB_Data * cb_data = get_busy_cb_data(L, db);
01406   
01407   int (*xbusy)(void *, int);
01408   
01409   if ( checknilornoneorfunc(L, 2) )
01410     xbusy = xbusy_callback_wrapper;
01411   else
01412     xbusy = 0;
01413   
01414   register_callback(L, db, KEY_XBUSY(cb_data), 2);
01415   
01416   lua_pushnumber(L, sqlite3_busy_handler(db->kr_db, xbusy, cb_data) );
01417   return 1;
01418 }
01419 
01420 
01421 
01422 static int xauth_callback_wrapper(void * cb_data, int auth_request, const char * name1, const char * name2, const char * db_name, const char * trigger_name)
01423 {
01424   DB *    db = CB_DATA(cb_data)->db;
01425   lua_State * L  = db->L;
01426   int result;
01427   
01428   push_callback(L, db, KEY_XAUTH(cb_data));
01429   lua_pushnumber(L, auth_request);
01430   push_nil_or_string(L, name1);
01431   push_nil_or_string(L, name2);
01432   push_nil_or_string(L, db_name);
01433   push_nil_or_string(L, trigger_name);
01434   
01435   if ( lua_pcall(L, 5, 1, 0) )
01436   {
01437     lua_pop(L, 1);
01438     return SQLITE_DENY;   /* On errors, sqlite should deny access */
01439   }
01440   
01441   if (lua_isnumber(L, -1))
01442     result = lua_tonumber(L, -1);
01443   else
01444     result = SQLITE_DENY; /* Wrong result values should deny access */
01445   
01446   lua_pop(L, 1);
01447   return result;
01448 }
01449 
01450 FUNC( l_sqlite3_set_authorizer )
01451 {
01452   DB * db   = checkdb(L, 1);
01453   CB_Data * cb_data = get_authorizer_cb_data(L, db);
01454   
01455   int (*xauth)(void *, int, const char *, const char *, const char *, const char *);
01456   
01457   if ( checknilornoneorfunc(L, 2) )
01458     xauth = xauth_callback_wrapper;
01459   else
01460     xauth = 0;
01461   
01462   register_callback(L, db, KEY_XAUTH(cb_data), 2);
01463 
01464   lua_pushnumber(L, sqlite3_set_authorizer(db->kr_db, xauth, cb_data) );
01465   return 1;
01466 }
01467 
01468 
01469 
01470 
01471 
01472 
01473 typedef struct { char * name; int (*func)(lua_State *); } f_entry;
01474 typedef struct { char * name; int value; } d_entry;
01475 
01476 
01477 
01478 static void f(lua_State * L, f_entry entries[])
01479 {
01480   int index;
01481   lua_newtable(L);
01482   for( index=0; entries[index].name; index++)
01483   {
01484     lua_pushstring(L, entries[index].name);
01485     lua_pushcfunction(L, entries[index].func);
01486     lua_rawset(L, -3);
01487   }
01488 }
01489 
01490 
01491 
01492 static void d(lua_State * L, d_entry entries[])
01493 {
01494   int index;
01495   lua_newtable(L);
01496   for( index=0; entries[index].name; index++)
01497   {
01498     lua_pushstring(L, entries[index].name);
01499     lua_pushnumber(L, entries[index].value);
01500     lua_rawset(L, -3);
01501   }
01502 }
01503 
01504 
01505 
01506 f_entry api_entries[] = {
01507   { "bind_null",    l_sqlite3_bind_null },
01508   { "bind_text",    l_sqlite3_bind_text },
01509   { "bind_blob",    l_sqlite3_bind_blob },
01510   { "bind_int",     l_sqlite3_bind_int },
01511   { "bind_double",    l_sqlite3_bind_double },
01512   { "bind_number",    l_sqlite3_bind_number },
01513   { "bind",     l_sqlite3_bind },
01514   { "bind_parameter_name",  l_sqlite3_bind_parameter_name },
01515   { "bind_parameter_name_x",  l_sqlite3_bind_parameter_name_x },
01516   { "bind_parameter_count", l_sqlite3_bind_parameter_count },
01517   { "busy_timeout",   l_sqlite3_busy_timeout },
01518   { "changes",      l_sqlite3_changes },
01519   { "close",      l_sqlite3_close },
01520   { "column_blob",    l_sqlite3_column_blob },
01521   { "column_text",    l_sqlite3_column_text },
01522   { "column_int",   l_sqlite3_column_int },
01523   { "column_double",    l_sqlite3_column_double },
01524   { "column_number",    l_sqlite3_column_number },
01525   { "column",     l_sqlite3_column },
01526   { "column_type",    l_sqlite3_column_type },
01527   { "column_count",   l_sqlite3_column_count },
01528   { "column_decltype",    l_sqlite3_column_decltype },
01529   { "column_name",    l_sqlite3_column_name },
01530   { "complete",     l_sqlite3_complete },
01531   { "data_count",   l_sqlite3_data_count },
01532   { "errcode",      l_sqlite3_errcode },
01533   { "errmsg",     l_sqlite3_errmsg },
01534   { "finalize",     l_sqlite3_finalize },
01535   { "interrupt",    l_sqlite3_interrupt },
01536   { "last_insert_rowid",  l_sqlite3_last_insert_rowid },
01537   { "open",     l_sqlite3_open },
01538   { "prepare",      l_sqlite3_prepare },
01539   { "reset",      l_sqlite3_reset },
01540   { "step",     l_sqlite3_step },
01541   { "total_changes",    l_sqlite3_total_changes },
01542   { "exec",     l_sqlite3_exec },
01543   { "create_function",    l_sqlite3_create_function },
01544   { "create_collation",   l_sqlite3_create_collation },
01545   { "trace",      l_sqlite3_trace },
01546   { "collation_needed",   l_sqlite3_collation_needed },
01547   { "result_null",    l_sqlite3_result_null },
01548   { "result_error",   l_sqlite3_result_error },
01549   { "result_double",    l_sqlite3_result_double },
01550   { "result_int",   l_sqlite3_result_int },
01551   { "result_number",    l_sqlite3_result_number },
01552   { "result_blob",    l_sqlite3_result_blob },
01553   { "result_text",    l_sqlite3_result_text },
01554   { "result_value",   l_sqlite3_result_value },
01555   { "result",     l_sqlite3_result },
01556   { "aggregate_count",    l_sqlite3_aggregate_count },
01557   { "aggregate_context",  l_sqlite3_aggregate_context },
01558   { "value_int",    l_sqlite3_value_int },
01559   { "value_double",   l_sqlite3_value_double },
01560   { "value_number",   l_sqlite3_value_number },
01561   { "value_blob",   l_sqlite3_value_blob },
01562   { "value_text",   l_sqlite3_value_text },
01563   { "value",      l_sqlite3_value },
01564   { "value_type",   l_sqlite3_value_type },
01565   { "libversion",   l_sqlite3_libversion },
01566   { "commit_hook",    l_sqlite3_commit_hook },
01567   { "progress_handler",   l_sqlite3_progress_handler },
01568   { "busy_handler",   l_sqlite3_busy_handler },
01569   { "set_authorizer",   l_sqlite3_set_authorizer },
01570   { "drow",     l_sqlite3_drow },
01571   { "irow",     l_sqlite3_irow },
01572   { "arow",     l_sqlite3_arow },
01573   { 0, 0 }
01574 };
01575 
01576 
01577 d_entry error_entries[] = {
01578   { "OK",     SQLITE_OK },
01579   { "ERROR",      SQLITE_ERROR },
01580   { "INTERNAL",     SQLITE_INTERNAL },
01581   { "PERM",     SQLITE_PERM },
01582   { "ABORT",      SQLITE_ABORT },
01583   { "BUSY",     SQLITE_BUSY },
01584   { "LOCKED",     SQLITE_LOCKED },
01585   { "NOMEM",      SQLITE_NOMEM },
01586   { "READONLY",     SQLITE_READONLY },
01587   { "INTERRUPT",    SQLITE_INTERRUPT },
01588   { "IOERR",      SQLITE_IOERR },
01589   { "CORRUPT",      SQLITE_CORRUPT },
01590   { "NOTFOUND",     SQLITE_NOTFOUND },
01591   { "FULL",     SQLITE_FULL },
01592   { "CANTOPEN",     SQLITE_CANTOPEN },
01593   { "PROTOCOL",     SQLITE_PROTOCOL },
01594   { "EMPTY",      SQLITE_EMPTY },
01595   { "SCHEMA",     SQLITE_SCHEMA },
01596   { "TOOBIG",     SQLITE_TOOBIG },
01597   { "CONSTRAINT",   SQLITE_CONSTRAINT },
01598   { "MISMATCH",     SQLITE_MISMATCH },
01599   { "MISUSE",     SQLITE_MISUSE },
01600   { "NOLFS",      SQLITE_NOLFS },
01601   { "AUTH",     SQLITE_AUTH },
01602   { "ROW",      SQLITE_ROW },
01603   { "DONE",     SQLITE_DONE },
01604   { "DENY",     SQLITE_DENY },
01605   { "IGNORE",     SQLITE_IGNORE },
01606   { 0, 0 }
01607 };
01608 
01609 
01610 d_entry type_entries[] = {
01611   { "INTEGER",      SQLITE_INTEGER },
01612   { "INT",      SQLITE_INTEGER },
01613   { "FLOAT",      SQLITE_FLOAT },
01614   { "DOUBLE",     SQLITE_FLOAT },
01615   { "TEXT",     SQLITE_TEXT },
01616   { "BLOB",     SQLITE_BLOB },
01617   { "NULL",     SQLITE_NULL },
01618   { 0, 0 }
01619 };
01620 
01621 
01622 d_entry auth_entries[] = {
01623   { "CREATE_INDEX",   SQLITE_CREATE_INDEX },
01624   { "CREATE_TABLE",   SQLITE_CREATE_TABLE },
01625   { "CREATE_TRIGGER",   SQLITE_CREATE_TRIGGER },
01626   { "CREATE_VIEW",    SQLITE_CREATE_VIEW },
01627   { "CREATE_TEMP_INDEX",  SQLITE_CREATE_TEMP_INDEX },
01628   { "CREATE_TEMP_TABLE",  SQLITE_CREATE_TEMP_TABLE },
01629   { "CREATE_TEMP_TRIGGER",  SQLITE_CREATE_TEMP_TRIGGER },
01630   { "CREATE_TEMP_VIEW", SQLITE_CREATE_TEMP_VIEW },
01631   { "DROP_INDEX",   SQLITE_DROP_INDEX },
01632   { "DROP_TABLE",   SQLITE_DROP_TABLE },
01633   { "DROP_TRIGGER",   SQLITE_DROP_TRIGGER },
01634   { "DROP_VIEW",    SQLITE_DROP_VIEW },
01635   { "DROP_TEMP_INDEX",    SQLITE_DROP_TEMP_INDEX },
01636   { "DROP_TEMP_TABLE",    SQLITE_DROP_TEMP_TABLE },
01637   { "DROP_TEMP_TRIGGER",  SQLITE_DROP_TEMP_TRIGGER },
01638   { "DROP_TEMP_VIEW",   SQLITE_DROP_TEMP_VIEW },
01639   { "INSERT",     SQLITE_INSERT },
01640   { "PRAGMA",     SQLITE_PRAGMA },
01641   { "READ",     SQLITE_READ },
01642   { "SELECT",     SQLITE_SELECT },
01643   { "TRANSACTION",    SQLITE_TRANSACTION },
01644   { "UPDATE",     SQLITE_UPDATE },
01645   { "ATTACH",     SQLITE_ATTACH },
01646   { "DETACH",     SQLITE_DETACH },
01647   { 0, 0 }
01648 };
01649 
01650 
01651 
01652 
01653 int luaopen_sqlite3(lua_State * L)
01654 {
01655   f(L, api_entries);
01656   d(L, error_entries);
01657   d(L, type_entries);
01658   d(L, auth_entries);
01659   
01660   return 4; /* api, error codes, type codes, auth requests */
01661 }
01662 
01663 
01664 

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:54 2011 by Doxygen 1.6.1