libluasqlite3.c

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

Generated on Sat May 19 05:40:22 2012 for cpp by  doxygen 1.5.6