From f7a311e12eab86e24171fb22c32a6a62536d9e48 Mon Sep 17 00:00:00 2001 From: Oscar Franco Date: Wed, 3 Apr 2024 07:40:29 +0200 Subject: [PATCH] init --- cpp/OpenSqliteHo.cpp | 94 ++++++++++++++++++++++++++++++++++++++++++++ cpp/OpenSqliteHo.h | 31 +++++++++++++++ cpp/bindings.cpp | 59 ++------------------------- 3 files changed, 128 insertions(+), 56 deletions(-) create mode 100644 cpp/OpenSqliteHo.cpp create mode 100644 cpp/OpenSqliteHo.h diff --git a/cpp/OpenSqliteHo.cpp b/cpp/OpenSqliteHo.cpp new file mode 100644 index 00000000..e06025e2 --- /dev/null +++ b/cpp/OpenSqliteHo.cpp @@ -0,0 +1,94 @@ +// +// Created by jplc on 4/2/24. +// + +#include "OpenSqliteHo.h" +#include "bridge.h" +#include "macros.h" +#include "types.h" + +namespace jsi = facebook::jsi; +using std::string; + +namespace opsqlite { + +jsi::Function OpenSqliteHo::open(jsi::Runtime &rt, + const std::string &basePath) { + return HOSTFN("open", 3) { + string errMsg; + if (isParametersNumberNotOk(errMsg, count)) { + throw std::runtime_error(errMsg); + } + + jsi::Object options = args[0].asObject(rt); + std::string dbName = options.getProperty(rt, "name").asString(rt).utf8(rt); + std::string path = getPath(rt, options, basePath); + std::string encryptionKey = OpenSqliteHo::getEncryptionKey(rt, options); + +#ifdef OP_SQLITE_USE_SQLCIPHER + if (encryptionKey.empty()) { + throw std::runtime_error( + "[OP SQLite] using SQLCipher encryption key is required"); + } + // TODO(osp) find a way to display the yellow box from c++ + BridgeResult result = opsqlite_open(dbName, path, encryptionKey); +#else + // if (!encryptionKey.empty()) { + // // RCTLogWarn(@"Your message") + // throw std::runtime_error("[OP SQLite] SQLCipher is not enabled, " + // "encryption key is not allowed"); + // } + BridgeResult result = opsqlite_open(dbName, path); +#endif + + if (result.type == SQLiteError) { + throw std::runtime_error(result.message); + } + + return {}; + }); +} + +bool OpenSqliteHo::isParametersNumberNotOk(string &msg, int count) { + if (count == 0) { + msg = "[op-sqlite][open] database name is required"; + return true; + } + return false; +} + +string OpenSqliteHo::getPath(jsi::Runtime &rt, const jsi::Object &options, + const string &basePath) { + string path = basePath; + string location = getLocation(rt, options); + if (!location.empty()) { + if (location == ":memory:") { + path = ":memory:"; + } else if (location.rfind("/", 0) == 0) { + path = location; + } else { + path = path + "/" + location; + } + } + return path; +} + +string OpenSqliteHo::getLocation(jsi::Runtime &rt, const jsi::Object &options) { + string location = ""; + if (options.hasProperty(rt, "location")) { + location = options.getProperty(rt, "location").asString(rt).utf8(rt); + } + return location; +} + +string OpenSqliteHo::getEncryptionKey(jsi::Runtime &rt, + const jsi::Object &options) { + string encryptionKey = ""; + if (options.hasProperty(rt, "encryptionKey")) { + encryptionKey = + options.getProperty(rt, "encryptionKey").asString(rt).utf8(rt); + } + return encryptionKey; +} + +} // namespace opsqlite diff --git a/cpp/OpenSqliteHo.h b/cpp/OpenSqliteHo.h new file mode 100644 index 00000000..da483f85 --- /dev/null +++ b/cpp/OpenSqliteHo.h @@ -0,0 +1,31 @@ +// +// Created by jplc on 4/2/24. +// + +#ifndef OPSQLITEEXAMPLE_OPENSQLITEHO_H +#define OPSQLITEEXAMPLE_OPENSQLITEHO_H + +#include +#include + +namespace opsqlite { + +class JSI_EXPORT OpenSqliteHo : public facebook::jsi::HostObject { +public: + static facebook::jsi::Function open(facebook::jsi::Runtime &rt, + const std::string &basePath); + +private: + static bool isParametersNumberNotOk(std::string &msg, int count); + static std::string getPath(facebook::jsi::Runtime &rt, + const facebook::jsi::Object &options, + const std::string &basePath); + static std::string getLocation(facebook::jsi::Runtime &rt, + const facebook::jsi::Object &options); + static std::string getEncryptionKey(facebook::jsi::Runtime &rt, + const facebook::jsi::Object &options); +}; + +} // namespace opsqlite + +#endif // OPSQLITEEXAMPLE_OPENSQLITEHO_H diff --git a/cpp/bindings.cpp b/cpp/bindings.cpp index a884f32c..ddd43595 100644 --- a/cpp/bindings.cpp +++ b/cpp/bindings.cpp @@ -12,6 +12,8 @@ #include #include +#include "OpenSqliteHo.h" + namespace opsqlite { namespace jsi = facebook::jsi; @@ -49,62 +51,7 @@ void install(jsi::Runtime &rt, basePath = std::string(docPath); invoker = jsCallInvoker; - auto open = HOSTFN("open", 3) { - if (count == 0) { - throw std::runtime_error("[op-sqlite][open] database name is required"); - } - - jsi::Object options = args[0].asObject(rt); - std::string dbName = options.getProperty(rt, "name").asString(rt).utf8(rt); - std::string path = std::string(basePath); - std::string location; - std::string encryptionKey; - - if (options.hasProperty(rt, "location")) { - location = options.getProperty(rt, "location").asString(rt).utf8(rt); - } - - if (options.hasProperty(rt, "encryptionKey")) { - encryptionKey = - options.getProperty(rt, "encryptionKey").asString(rt).utf8(rt); - } - -#ifdef OP_SQLITE_USE_SQLCIPHER - if (encryptionKey.empty()) { - throw std::runtime_error( - "[OP SQLite] using SQLCipher encryption key is required"); - } -// TODO(osp) find a way to display the yellow box from c++ -#else - // if (!encryptionKey.empty()) { - // // RCTLogWarn(@"Your message") - // throw std::runtime_error("[OP SQLite] SQLCipher is not enabled, " - // "encryption key is not allowed"); - // } -#endif - - if (!location.empty()) { - if (location == ":memory:") { - path = ":memory:"; - } else if (location.rfind("/", 0) == 0) { - path = location; - } else { - path = path + "/" + location; - } - } - -#ifdef OP_SQLITE_USE_SQLCIPHER - BridgeResult result = opsqlite_open(dbName, path, encryptionKey); -#else - BridgeResult result = opsqlite_open(dbName, path); -#endif - - if (result.type == SQLiteError) { - throw std::runtime_error(result.message); - } - - return {}; - }); + auto open = OpenSqliteHo::open(rt, basePath); auto attach = HOSTFN("attach", 4) { if (count < 3) {