是否可以在sqlite中创建javascript用户定义函数 [英] Is it possible to create a javascript User-defined function in sqlite
问题描述
背景:
-
Firefox 3包括 SQLite 版本3.5.9.Firefox还允许使用javascript和可以调用嵌入式SQLite引擎.
Firefox 3 includes SQLite version 3.5.9. Firefox also allows extensions, which are written in javascript and can call the embedded SQLite engine.
按预期执行以下命令SQL语句'SELECT"TEXT" REGEXP"T *";'给出一个错误,因为有SQLite本身不包含REGEXP函数.
As expected, executing the following SQL statement 'SELECT "TEXT" REGEXP "T*";' gives an error, since there is no REGEXP function natively included in SQLite.
javascript包含一个内置的regexp函数.
javascript includes a built in regexp function.
SQLite允许通过SELECT load_extension('filename');
SQLite allows loadable extensions via SELECT load_extension('filename');
问题:是否可以在SQLite中加载用javascript编写的可以执行REGEXP的扩展程序?
推荐答案
是.可以调用javascript函数
Yes. It is possible to call javascript functions
//(thanks to Mirnal Kant, SQLManager)
//Version 2 -- Prevent Firefox crashing
// -- Suspect a problem with continual creation of Regex objects
var g_RegExpString = null;
var g_RegExp = null;
//functions to be created for the db
var smDbFunctions = {
// (0) = Regex Expression
// (1) = Column value to test
regexp: {
onFunctionCall: function(val) {
if (g_RegExp == null || val.getString(0) != g_RegExpString)
{
g_RegExpString = val.getString(0);
g_RegExp = new RegExp(g_RegExpString);
}
if (val.getString(1).match(g_RegExp)) return 1;
else return 0;
}
}
};
实例化一个SQLite实例后:
after instantiating a SQLite instance:
Database.createFunction("REGEXP", 2, smDbFunctions.regexp);
这篇关于是否可以在sqlite中创建javascript用户定义函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!