Small. Fast. Reliable.
Choose any three.
OBS: I was looking again in the load extension capabilities of the SQLite3 and probably will reimplement it as an extension module without modify the original sqlite3 source code !

I have a preview of something like that, http://dad-it.com:8080/sqlite3.dll is the 3.3.10 with minor modifications and with lsqlite3.5 (lua sqlite3 bindings), where every opened database has Lua 5.1.1 as procedural language, in Lua we have a global var 'db' that refers to the actual database, we can use anything that can be used with lsqlite3, as well http://dad-it.com:8080/sqlite3.exe is the sqlite3 shell with the same additions as the sqlite3.dll, a small example is at http://dad-it.com:8080/test-lua-sqlite3.sql.

  select install_lua_script('
  local function udf1_scalar(ctx, v)
    local ud = ctx:user_data()
    ud.r = (ud.r or '''') .. tostring(v)
    ctx:result_text(ud.r)
  end

  db:create_function(''udf1'', 1, udf1_scalar, { })');

  select 'Udf1 result :' || udf1(549);

  select install_lua_script('sequence_number = 0
  function sequencial_number(ctx)
    local ud = ctx:user_data()
    sequence_number = sequence_number + 1
    ud.r = sequence_number
    ctx:result_text(ud.r)
  end

  function set_sequencial_number(ctx,v)
    local ud = ctx:user_data()
    sequence_number = v
    ud.r = sequence_number
    ctx:result_text(ud.r)
  end

  db:create_function(''seqnr'', 0, sequencial_number, { })
  db:create_function(''set_seqnr'', 1, set_sequencial_number, { })')"

  select seqnr(),* from day_slots limit 10;

  in main.c:
  ---------
  #ifdef LUA_SQLITE_PROCEDURAL
  #include "lua_procedural.h"
  #endif

	/*
	** Close an existing SQLite database
	*/
	int sqlite3_close(sqlite3 *db){
	  HashElem *i;
	  int j;
	#ifdef LUA_SQLITE_PROCEDURAL
	  lua_State *L;
	#endif
	  if( !db ){
	    return SQLITE_OK;
	  }
	  if( sqlite3SafetyCheck(db) ){
	    return SQLITE_MISUSE;
	  }
	#ifdef LUA_SQLITE_PROCEDURAL
	  L = (lua_State*) sqlite3_get_db_user_data(db);
	#endif
	......
	#ifdef LUA_SQLITE_PROCEDURAL
	  if (L) lua_close(L);
	#endif
	  return SQLITE_OK;
	}
	.....
	/*
	** This routine does the work of opening a database on behalf of
	** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
	** is UTF-8 encoded.
	*/

	static int openDatabase(
	  const char *zFilename, /* Database filename UTF-8 encoded */
	  sqlite3 **ppDb         /* OUT: Returned database handle */
	){
	  sqlite3 *db;
	  int rc;
	  CollSeq *pColl;
	.......
	opendb_out:
	  if( SQLITE_NOMEM==(rc = sqlite3_errcode(db)) ){
	    sqlite3_close(db);
	    db = 0;
	  }
	  *ppDb = db;
	#ifdef LUA_SQLITE_PROCEDURAL
		install_lua_procedural(db);
	#endif
	  return sqlite3ApiExit(0, rc);
	}

	void sqlite3_set_db_user_data(sqlite3 *db, void *data){
		assert( db );
		db->pDBUserData = data;
	}

	void *sqlite3_get_db_user_data(sqlite3 *db){
		assert( db );
		return db->pDBUserData;
	}

	----------------------
	sqlit3Int.h:
	----------------------
	struct sqlite3 {
	  int nDb;                      /* Number of backends currently in use */
	.........
	  void *pDBUserData;
	};

	struct sqlite3_context {
	  FuncDef *pFunc;       /* Pointer to function information.  MUST BE FIRST */
	  VdbeFunc *pVdbeFunc;  /* Auxilary data, if created. */
	  Mem s;                /* The return value is stored here */
	  Mem *pMem;            /* Memory cell used to store aggregate context */
	  u8 isError;           /* Set to true for an error */
	  CollSeq *pColl;       /* Collating sequence */
	  void *pDBUserData;
	};
	------
	lua_procedural.h:
	-----------------
	#ifdef LUA_SQLITE_PROCEDURAL
	//lua includes
	#include "lua.h"
	#include "lauxlib.h"
	extern int luaopen_lsqlite3(lua_State *L);
	extern int lsqlite_register_db(lua_State *L, sqlite3 *db, const char *name);

	/*
	Install the main interpreter function
	*/
	static void install_lua_script(
	  sqlite3_context *context,
	  int argc,
	  sqlite3_value **argv
	){
	  assert( argc==1 );
	  lua_State *L = (lua_State*) sqlite3_db_user_data(context);
	  if(L && sqlite3_value_type(argv[0]) == SQLITE_TEXT){
		int res = luaL_dostring(L, sqlite3_value_text(argv[0]));
		sqlite3_result_int(context,res);
	  } else sqlite3_result_null(context);
	}

	static int install_lua_procedural(sqlite3 *db){
		lua_State *L = lua_open();
		if(L){
			luaL_openlibs(L); /* Load Lua libraries */
			sqlite3_set_db_user_data(db,(void*)L);
			lua_state_set_user_data(L,(void*)db);
			luaopen_lsqlite3(L);
			lsqlite_register_db(L, db, "db");
		    sqlite3_create_function(db, "install_lua_script", 1, SQLITE_UTF8, 0,
		        install_lua_script, 0, 0);
			return 0;
		} else {
	      //fprintf(stderr,"Unable to register lua scripting engine !\n");
		  return -1;
		}
	}
	#endif

Attachments:

  • lsqlite3.c 55387 bytes added by anonymous on 2007-Jan-11 21:42:25 UTC.
    This is the main file from lsqlite3.5 with some modifications.