On Wed, Nov 16, 2005 at 10:22:42AM +0000, Robin Breathe wrote:
> Nathan Kurz wrote:
> > I'm using a computationally expensive user defined function called
> > 'match()'.  In case it makes a difference, match() is written in C,
> > and for testing, I'm loading it as a shared library into the sqlite3
> > shell application.  I want to return the value of match(), and also
> > order by it.  So my query looks something like this:
> 
> Nathan,
> 
> I'm intrigued to know how you loaded the additional shared library with
> your sqlite3(1) shell... is it simply a case of linking another library
> to it post installation, and if so how do you register the user
> functions that library contains?

Hi Robin ---

Unfortunately it's not a capability that comes built in.  I wrote a
simple patch to add 'pragma load_library(library)' that will
dynamically load a library and then call a register function in that
library.  It's not complicated, but I presume my implementation is
Linux specific.  But it wouldn't be hard to do for other platforms.

The patch is attached, in case it helps you or anyone else.  I think
this would be a useful feature to add to SQLite, but I can also see
how it might be considered feature creep.  One might also need a
greater level of platform specific handling than used in the rest.
But if you are using Linux and need the capability, try the patch!

--nate




--- src/pragma.c~       2005-09-20 11:42:23.000000000 -0600
+++ src/pragma.c        2005-11-12 10:07:10.000000000 -0700
@@ -918,6 +918,34 @@
   }else
 #endif
 
+#define SQLITE_PRAGMA_LOAD_LIBRARY
+#ifdef SQLITE_PRAGMA_LOAD_LIBRARY
+#include <dlfcn.h>  /* NOTE: also must add -ldl to LIBS in Makefile */
+  /*
+  **   PRAGMA load_library(<library>)
+  **
+  ** Dynamically load a shared library.  After loading, the function 
+  ** 'sqliteRegister(sqlite3 *db)' is searched for in the new library 
+  ** and called to allow that library to register user-defined functions.
+  */
+  if( sqlite3StrICmp(zLeft, "load_library")==0 && zRight ){ 
+    void *pHandle;
+    void (*pRegisterFunc)(sqlite3 *);
+    pHandle = dlopen(zRight, RTLD_LAZY);
+    if ( pHandle == 0 ) { 
+      sqlite3ErrorMsg(pParse, dlerror());
+      goto pragma_out;
+    }
+    pRegisterFunc = dlsym(pHandle, "sqlite3Register");
+    if ( pRegisterFunc == 0 )  {
+      sqlite3ErrorMsg(pParse, "Could not find function "
+                      "'sqlite3Register' in %s\n", zRight);
+      goto pragma_out;
+    }
+    pRegisterFunc(db);
+  }
+#endif
+
   {}
 
   if( v ){

Reply via email to