Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

mobile: register natural_key SQLite function in react-native-quick-sqlite

kashaf-ansari-dev73c2469294c1
diff --git a/apps/mobile/patches/react-native-quick-sqlite+8.2.7.patch b/apps/mobile/patches/react-native-quick-sqlite+8.2.7.patch
index 4a976d3c6..eb035e0ff 100644
--- a/apps/mobile/patches/react-native-quick-sqlite+8.2.7.patch
+++ b/apps/mobile/patches/react-native-quick-sqlite+8.2.7.patch
@@ -109746,25 +109746,52 @@ diff --git a/node_modules/react-native-quick-sqlite/cpp/sqliteBridge.cpp b/node_
index 9ffe010..4a6f18f 100644
--- a/node_modules/react-native-quick-sqlite/cpp/sqliteBridge.cpp
+++ b/node_modules/react-native-quick-sqlite/cpp/sqliteBridge.cpp
-@@ -20,6 +20,9 @@
+@@ -20,6 +20,38 @@
using namespace std;
using namespace facebook;
+extern "C" int sqlite3Fts5BetterTrigramInit(sqlite3 *db);
+extern "C" int sqlite3Fts5HtmlInit(sqlite3 *db);
++
++static void sqliteNaturalKeyFunc(sqlite3_context *context, int argc, sqlite3_value **argv) {
++ if (argc == 0 || sqlite3_value_type(argv[0]) == SQLITE_NULL) {
++ sqlite3_result_null(context);
++ return;
++ }
++ const unsigned char *text = sqlite3_value_text(argv[0]);
++ if (!text) {
++ sqlite3_result_text(context, "", 0, SQLITE_STATIC);
++ return;
++ }
++ string result;
++ const char *p = reinterpret_cast<const char*>(text);
++ while (*p) {
++ if (*p >= '0' && *p <= '9') {
++ const char *start = p;
++ while (*p >= '0' && *p <= '9') p++;
++ size_t len = p - start;
++ if (len < 10) result.append(10 - len, '0');
++ result.append(start, len);
++ } else {
++ result.push_back(*p);
++ p++;
++ }
++ }
++ sqlite3_result_text(context, result.c_str(), static_cast<int>(result.length()), SQLITE_TRANSIENT);
++}
+
map<string, sqlite3 *> dbMap = map<string, sqlite3 *>();
bool folder_exists(const std::string &foldername)
-@@ -90,12 +93,18 @@ SQLiteOPResult sqliteOpenDb(string const dbName, string const docPath)
+@@ -90,12 +122,18 @@ SQLiteOPResult sqliteOpenDb(string const dbName, string const docPath)
int exit = 0;
exit = sqlite3_open_v2(dbPath.c_str(), &db, sqlOpenFlags, nullptr);
+ if (exit == SQLITE_OK)
+ {
-+
+ sqlite3Fts5BetterTrigramInit(db);
+ sqlite3Fts5HtmlInit(db);
++ sqlite3_create_function(db, "natural_key", 1, SQLITE_UTF8, nullptr, sqliteNaturalKeyFunc, nullptr, nullptr);
+ }
+
if (exit != SQLITE_OK)