Skip to content

Commit

Permalink
Working android implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ospfranco committed Apr 20, 2024
1 parent f1c3da7 commit cf4585f
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 17 deletions.
7 changes: 6 additions & 1 deletion android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ else()
target_sources(${PACKAGE_NAME} PRIVATE ../cpp/sqlite3.h ../cpp/sqlite3.c)
endif()

if (OP_SQLITE_USE_CRSQLITE)
add_definitions(
-DOP_SQLITE_USE_CRSQLITE
)
endif()

set_target_properties(
${PACKAGE_NAME} PROPERTIES
CXX_STANDARD 20
Expand All @@ -64,7 +70,6 @@ find_package(ReactAndroid REQUIRED CONFIG)
find_package(fbjni REQUIRED CONFIG)
find_library(LOG_LIB log)


target_link_libraries(
${PACKAGE_NAME}
${LOG_LIB}
Expand Down
8 changes: 7 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,20 @@ android {

if(System.getenv("OP_SQLITE_USE_SQLCIPHER") == '1') {
println "OP-SQLITE using SQLCipher! 🔒"
cFlags += "-DOP_SQLITE_USE_SQLCIPHER=1"
cppFlags += "-DOP_SQLITE_USE_SQLCIPHER=1"
}

if(System.getenv("OP_SQLITE_USE_CRSQLITE") == '1') {
println "OP-SQLITE using CR-SQLite! 🤖"
cppFlags += "-DOP_SQLITE_USE_CRSQLITE=1"
}

cppFlags "-O2", "-fexceptions", "-frtti", "-std=c++1y", "-DONANDROID"
abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
arguments "-DANDROID_STL=c++_shared",
"-DSQLITE_FLAGS='${SQLITE_FLAGS ? SQLITE_FLAGS : ''}'"
"-DOP_SQLITE_USE_SQLCIPHER='${System.getenv("OP_SQLITE_USE_SQLCIPHER") == '1'? 1 : 0}'"
"-DOP_SQLITE_USE_CRSQLITE='${System.getenv("OP_SQLITE_USE_CRSQLITE") == '1'? 1 : 0}'"
abiFilters (*reactNativeArchitectures())
}
}
Expand Down
6 changes: 4 additions & 2 deletions android/cpp-adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
#include <jsi/jsi.h>
#include <typeinfo>

// This file is not using raw jni but rather fbjni, do not change how the native functions are registered
// This file is not using raw jni but rather fbjni, do not change how the native
// functions are registered
// https://github.com/facebookincubator/fbjni/blob/main/docs/quickref.md
struct OPSQLiteBridge : jni::JavaClass<OPSQLiteBridge> {
static constexpr auto kJavaDescriptor = "Lcom/op/sqlite/OPSQLiteBridge;";
Expand All @@ -27,7 +28,8 @@ struct OPSQLiteBridge : jni::JavaClass<OPSQLiteBridge> {
auto jsCallInvoker = jsCallInvokerHolder->cthis()->getCallInvoker();
std::string dbPathStr = dbPath->toStdString();

opsqlite::install(*jsiRuntime, jsCallInvoker, dbPathStr.c_str());
opsqlite::install(*jsiRuntime, jsCallInvoker, dbPathStr.c_str(),
"libcrsqlite");
}

static void clearStateNativeJsi(jni::alias_ref<jni::JObject> thiz) {
Expand Down
Binary file added android/src/main/jniLibs/arm64-v8a/libcrsqlite.so
Binary file not shown.
Binary file not shown.
Binary file added android/src/main/jniLibs/x86/libcrsqlite.so
Binary file not shown.
Binary file added android/src/main/jniLibs/x86_64/libcrsqlite.so
Binary file not shown.
9 changes: 6 additions & 3 deletions cpp/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ BridgeResult opsqlite_open(std::string const &dbName,
nullptr, nullptr);
#endif

#ifdef OP_SQLITE_CRSQLITE
#ifdef OP_SQLITE_USE_CRSQLITE
LOGI("Should load CRSQlite now");
char *errMsg;
const char *crsqliteEntryPoint = "sqlite3_crsqlite_init";

Expand All @@ -87,10 +88,12 @@ BridgeResult opsqlite_open(std::string const &dbName,

if (errMsg != nullptr) {
return {.type = SQLiteError, .message = errMsg};
} else {
LOGI("Loaded CRSQlite successfully");
}
#endif

return BridgeResult{.type = SQLiteOk, .affectedRows = 0};
return {.type = SQLiteOk, .affectedRows = 0};
}

BridgeResult opsqlite_close(std::string const &dbName) {
Expand All @@ -99,7 +102,7 @@ BridgeResult opsqlite_close(std::string const &dbName) {

sqlite3 *db = dbMap[dbName];

#ifdef OP_SQLITE_CRSQLITE
#ifdef OP_SQLITE_USE_CRSQLITE
opsqlite_execute(dbName, "select crsql_finalize();", nullptr, nullptr,
nullptr);
#endif
Expand Down
10 changes: 8 additions & 2 deletions example/ios/OPSQLiteExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,10 @@
);
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
OTHER_CFLAGS = "$(inherited) ";
OTHER_CFLAGS = (
"$(inherited)",
" ",
);
OTHER_CPLUSPLUSFLAGS = (
"$(OTHER_CFLAGS)",
"-DFOLLY_NO_CONFIG",
Expand Down Expand Up @@ -512,7 +515,10 @@
"\"$(inherited)\"",
);
MTL_ENABLE_DEBUG_INFO = NO;
OTHER_CFLAGS = "$(inherited) ";
OTHER_CFLAGS = (
"$(inherited)",
" ",
);
OTHER_CPLUSPLUSFLAGS = (
"$(OTHER_CFLAGS)",
"-DFOLLY_NO_CONFIG",
Expand Down
2 changes: 1 addition & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,7 @@ SPEC CHECKSUMS:
fmt: 4c2741a687cc09f0634a2e2c72a838b99f1ff120
glog: c5d68082e772fa1c511173d6b30a9de2c05a69a2
hermes-engine: 1a846a8f620a08c60f5f89aa65b8664f769f742f
op-sqlite: e3a7e3405530714f7b2c1ccb7897f3a95c0515ac
op-sqlite: 182bbbdd9a93ea385a81401a85b98683886f3f51
RCT-Folly: 045d6ecaa59d826c5736dfba0b2f4083ff8d79df
RCTDeprecation: 82b53c4f460b7a5b27c6be8310a71bc84df583f5
RCTRequired: d1a99a9f78fcc4acca99ab397822f4e58601b134
Expand Down
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios --simulator='iPhone 15' --scheme='debug'",
"ios": "react-native run-ios --scheme='debug'",
"start": "react-native start",
"pods": "cd ios && rm -rf Pods && rm -rf Podfile.lock && bundle exec pod install",
"build:android": "cd android && ./gradlew assembleDebug --no-daemon --console=plain -PreactNativeArchitectures=arm64-v8a",
Expand Down
8 changes: 4 additions & 4 deletions example/src/tests/queries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ export function queriesTests() {
});

describe('Queries tests', () => {
// it('Test crsqlite', async () => {
// const res = db.execute('select crsql_as_crr("User")');
// console.warn(res);
// });
it('Test crsqlite', async () => {
const res = db.execute('select crsql_as_crr("User")');
console.warn(res);
});

it('Insert', async () => {
const id = chance.integer();
Expand Down
4 changes: 2 additions & 2 deletions op-sqlite.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ Pod::Spec.new do |s|
xcconfig[:OTHER_CFLAGS] = optimizedCflags + ' -DSQLITE_THREADSAFE=1 '
end

if ENV['OP_SQLITE_CRSQLITE'] == '1' then
if ENV['OP_SQLITE_USE_CRSQLITE'] == '1' then
log_message.call("[OP-SQLITE] using CRQSQLite! 🤖")
xcconfig[:GCC_PREPROCESSOR_DEFINITIONS] += " OP_SQLITE_CRSQLITE=1"
xcconfig[:GCC_PREPROCESSOR_DEFINITIONS] += "-DOP_SQLITE_USE_CRSQLITE=1"
s.vendored_frameworks = "ios/crsqlite.xcframework"
end

Expand Down

0 comments on commit cf4585f

Please sign in to comment.