23#include "SqliteFunctions.h"
38# define UINT32_TYPE uint32_t
40# define UINT32_TYPE unsigned int
45# define UINT16_TYPE uint16_t
47# define UINT16_TYPE unsigned short int
52# define INT16_TYPE int16_t
54# define INT16_TYPE short int
59# define UINT8_TYPE uint8_t
61# define UINT8_TYPE unsigned char
66# define INT8_TYPE int8_t
68# define INT8_TYPE signed char
71#ifndef LONGDOUBLE_TYPE
72# define LONGDOUBLE_TYPE long double
74typedef sqlite_int64 i64;
75typedef sqlite_uint64 u64;
76typedef UINT32_TYPE u32;
77typedef UINT16_TYPE u16;
78typedef INT16_TYPE i16;
82static bool tryExec(sqlite3 *db,
const char *sql)
84 return SQLITE_OK == sqlite3_exec(db, sql,
nullptr ,
89#define sqlite3Toupper(x) toupper((unsigned char)(x))
90#define sqlite3Isalpha(x) isalpha((unsigned char)(x))
98static void soundexFunc(
99 sqlite3_context *context,
106 static const unsigned char iCode[] = {
107 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
108 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
109 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
110 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
111 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
112 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
113 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
114 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
117 zIn = (u8*)sqlite3_value_text(argv[0]);
118 if( zIn==0 ) zIn = (u8*)
"";
119 for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){}
121 u8 prevcode = iCode[zIn[i]&0x7f];
122 zResult[0] = sqlite3Toupper(zIn[i]);
123 for(j=1; j<4 && zIn[i]; i++){
124 int code = iCode[zIn[i]&0x7f];
126 if( code!=prevcode ){
128 zResult[j++] = code +
'0';
138 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
142 sqlite3_result_text(context,
"?000", 4, SQLITE_STATIC);
146bool createCustomSQLiteFunctions(sqlite3 *db)
148 int eTextRep = SQLITE_UTF8;
149#if SQLITE_VERSION_NUMBER >= 3008003
150 eTextRep |= SQLITE_DETERMINISTIC;
152 if (!tryExec(db,
"SELECT SOUNDEX()")) {
153 int res = sqlite3_create_function_v2(
164 if (res != SQLITE_OK) {