KDb

SqliteFunctions.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2015 JarosÅ‚aw Staniek <[email protected]>
3 
4  Contains portions of sqlite3.c licensed under public domain. The author disclaims
5  copyright to this source code.
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this program; see the file COPYING. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21 */
22 
23 #include "SqliteFunctions.h"
24 
25 #include <QDebug>
26 
27 #include <cctype>
28 
29 static bool tryExec(sqlite3 *db, const char *sql)
30 {
31  return SQLITE_OK == sqlite3_exec(db, sql, nullptr /*callback*/,
32  nullptr /* 1st argument to callback */, nullptr /*err*/);
33 }
34 
35 // BEGIN from sqlite3.c
36 #define sqlite3Toupper(x) toupper((unsigned char)(x))
37 #define sqlite3Isalpha(x) isalpha((unsigned char)(x))
38 
39 /*
40 ** Compute the soundex encoding of a word.
41 **
42 ** IMP: R-59782-00072 The soundex(X) function returns a string that is the
43 ** soundex encoding of the string X.
44 */
45 static void soundexFunc(
46  sqlite3_context *context,
47  int argc,
48  sqlite3_value **argv
49 ){
50  char zResult[8];
51  const uchar *zIn;
52  int i, j;
53  static const uchar iCode[] = {
54  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
55  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
56  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
57  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
58  0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
59  1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
60  0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
61  1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
62  };
63  Q_ASSERT(argc==1);
64  zIn = (uchar*)sqlite3_value_text(argv[0]);
65  if( zIn==nullptr ) zIn = (uchar*)"";
66  for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){}
67  if( zIn[i] ){
68  uchar prevcode = iCode[zIn[i]&0x7f];
69  zResult[0] = sqlite3Toupper(zIn[i]);
70  for(j=1; j<4 && zIn[i]; i++){
71  int code = iCode[zIn[i]&0x7f];
72  if( code>0 ){
73  if( code!=prevcode ){
74  prevcode = code;
75  zResult[j++] = code + '0';
76  }
77  }else{
78  prevcode = 0;
79  }
80  }
81  while( j<4 ){
82  zResult[j++] = '0';
83  }
84  zResult[j] = 0;
85  sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
86  }else{
87  /* IMP: R-64894-50321 The string "?000" is returned if the argument
88  ** is NULL or contains no ASCII alphabetic characters. */
89  sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
90  }
91 }
92 
93 bool createCustomSQLiteFunctions(sqlite3 *db)
94 {
95  int eTextRep = SQLITE_UTF8;
96 #if SQLITE_VERSION_NUMBER >= 3008003
97  eTextRep |= SQLITE_DETERMINISTIC;
98 #endif
99  if (!tryExec(db, "SELECT SOUNDEX()")) {
100  int res = sqlite3_create_function_v2(
101  db,
102  "SOUNDEX",
103  1, //nArg
104  eTextRep,
105  nullptr, // pApp
106  soundexFunc,
107  nullptr, // xStep
108  nullptr, // xFinal
109  nullptr // xDestroy
110  );
111  if (res != SQLITE_OK) {
112  return false;
113  }
114  }
115  return true;
116 }
117 
118 // END from sqlite3.c
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jun 25 2022 06:21:34 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.