Skip to content

Commit

Permalink
Merge branch 'main' into bridgeless
Browse files Browse the repository at this point in the history
# Conflicts:
#	.yarn/install-state.gz
#	android/src/main/java/com/op/sqlite/OPSQLiteModule.kt
#	example/.yarn/install-state.gz
#	example/ios/Podfile.lock
#	src/index.ts
  • Loading branch information
ospfranco committed Mar 27, 2024
2 parents 255481d + b083c02 commit 9bb229c
Show file tree
Hide file tree
Showing 18 changed files with 144 additions and 345 deletions.
Binary file added .yarn/install-state.gz
Binary file not shown.
47 changes: 46 additions & 1 deletion android/src/main/java/com/op/sqlite/OPSQLiteModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import android.util.Log
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.module.annotations.ReactModule;
import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import java.io.OutputStream

@ReactModule(name = OPSQLiteModule.NAME)
internal class OPSQLiteModule(context: ReactApplicationContext?) :
Expand Down Expand Up @@ -40,7 +44,48 @@ internal class OPSQLiteModule(context: ReactApplicationContext?) :
}
}

override fun invalidate() {
@ReactMethod(isBlockingSynchronousMethod = true)
override fun moveAssetsDatabase(name: String, extension: String): Boolean {
val context = reactApplicationContext
val assetsManager = context.assets

try {
// Open the input stream for the asset file
val inputStream: InputStream = assetsManager.open("custom/$name.$extension")

// Create the output file in the documents directory
val databasesFolder = context
.getDatabasePath("defaultDatabase")
.absolutePath
.replace("defaultDatabase", "")

val outputFile = File(databasesFolder, "$name.$extension")

if (outputFile.exists()) {
return true
}

// Open the output stream for the output file
val outputStream: OutputStream = FileOutputStream(outputFile)

// Copy the contents from the input stream to the output stream
val buffer = ByteArray(1024)
var length: Int
while (inputStream.read(buffer).also { length = it } > 0) {
outputStream.write(buffer, 0, length)
}

// Close the streams
inputStream.close()
outputStream.close()

return true
} catch (exception: Exception) {
return false
}
}

override fun invalidate() {
super.invalidate()
OPSQLiteBridge.instance.clearState()
}
Expand Down
4 changes: 4 additions & 0 deletions android/src/paper/java/com/op/sqlite/NativeOPSQLiteSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,8 @@ public NativeOPSQLiteSpec(ReactApplicationContext reactContext) {
@ReactMethod(isBlockingSynchronousMethod = true)
@DoNotStrip
public abstract boolean install();

@ReactMethod(isBlockingSynchronousMethod = true)
@DoNotStrip
public abstract boolean moveAssetsDatabase(String name, String extension);
}
Binary file not shown.
9 changes: 9 additions & 0 deletions example/android/link-assets-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"migIndex": 1,
"data": [
{
"path": "assets/sample.sqlite",
"sha1": "0f1675ac593b261b41a5144bc14f41163bd2a0c2"
}
]
}
Binary file added example/assets/sample.sqlite
Binary file not shown.
13 changes: 13 additions & 0 deletions example/ios/OPSQLiteExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
022AA638EE144393ADDCAEC0 /* sample.sqlite in Resources */ = {isa = PBXBuildFile; fileRef = 96FD9FD0FC4F4540AC7A9CE6 /* sample.sqlite */; };
13B07FBC1A68108700A75B9A /* AppDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB01A68108700A75B9A /* AppDelegate.mm */; };
13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 13B07FB51A68108700A75B9A /* Images.xcassets */; };
13B07FC11A68108700A75B9A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; };
Expand All @@ -28,6 +29,7 @@
6EE3066EB7AAB7E17B1CAB50 /* libPods-OPSQLiteExample.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-OPSQLiteExample.a"; sourceTree = BUILT_PRODUCTS_DIR; };
81AB9BB72411601600AC10FF /* LaunchScreen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = LaunchScreen.storyboard; path = OPSQLiteExample/LaunchScreen.storyboard; sourceTree = "<group>"; };
89C6BE57DB24E9ADA2F236DE /* Pods-OPSQLiteExample-OPSQLiteExampleTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-OPSQLiteExample-OPSQLiteExampleTests.release.xcconfig"; path = "Target Support Files/Pods-OPSQLiteExample-OPSQLiteExampleTests/Pods-OPSQLiteExample-OPSQLiteExampleTests.release.xcconfig"; sourceTree = "<group>"; };
96FD9FD0FC4F4540AC7A9CE6 /* sample.sqlite */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = undefined; includeInIndex = 0; lastKnownFileType = unknown; name = sample.sqlite; path = ../assets/sample.sqlite; sourceTree = "<group>"; };
ED297162215061F000B7C4FE /* JavaScriptCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = JavaScriptCore.framework; path = System/Library/Frameworks/JavaScriptCore.framework; sourceTree = SDKROOT; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -81,6 +83,7 @@
83CBBA001A601CBA00E9B192 /* Products */,
2D16E6871FA4F8E400B85C8A /* Frameworks */,
BBD78D7AC51CEA395F1C20DB /* Pods */,
A9BA4AF2CBC44B9BB28B8ABA /* Resources */,
);
indentWidth = 2;
sourceTree = "<group>";
Expand All @@ -95,6 +98,15 @@
name = Products;
sourceTree = "<group>";
};
A9BA4AF2CBC44B9BB28B8ABA /* Resources */ = {
isa = PBXGroup;
children = (
96FD9FD0FC4F4540AC7A9CE6 /* sample.sqlite */,
);
name = Resources;
path = "";
sourceTree = "<group>";
};
BBD78D7AC51CEA395F1C20DB /* Pods */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -169,6 +181,7 @@
files = (
81AB9BB82411601600AC10FF /* LaunchScreen.storyboard in Resources */,
13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */,
022AA638EE144393ADDCAEC0 /* sample.sqlite in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

This file was deleted.

8 changes: 4 additions & 4 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ PODS:
- hermes-engine (0.74.0-rc.5):
- hermes-engine/Pre-built (= 0.74.0-rc.5)
- hermes-engine/Pre-built (0.74.0-rc.5)
- op-sqlite (3.0.1):
- op-sqlite (3.0.7):
- React
- React-callinvoker
- React-Core
Expand Down Expand Up @@ -1352,13 +1352,13 @@ EXTERNAL SOURCES:
:path: "../node_modules/react-native/ReactCommon/yoga"

SPEC CHECKSUMS:
boost: 26fad476bfa736552bbfa698a06cc530475c1505
DoubleConversion: fea03f2699887d960129cc54bba7e52542b6f953
boost: d3f49c53809116a5d38da093a8aa78bf551aed09
DoubleConversion: 76ab83afb40bddeeee456813d9c04f67f78771b5
FBLazyVector: 5b76d5fff75c5fb146a50a6edee4db626f9914d5
fmt: 4c2741a687cc09f0634a2e2c72a838b99f1ff120
glog: c5d68082e772fa1c511173d6b30a9de2c05a69a2
hermes-engine: 28f3a005a29e856b1b5cd0e20b3fa7bbf862cdf3
op-sqlite: 24443c22e059972abf4109b4e781d3f03336a761
op-sqlite: af2411e57364db634729dbd4d4b24d0a9d973ab7
RCT-Folly: 045d6ecaa59d826c5736dfba0b2f4083ff8d79df
RCTDeprecation: ef45994c9e22748cfeb6553422514437d0ea15f3
RCTRequired: ab444d2da42365430b9e8e95bd2a0fb18620fdc6
Expand Down
9 changes: 9 additions & 0 deletions example/ios/link-assets-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"migIndex": 1,
"data": [
{
"path": "assets/sample.sqlite",
"sha1": "0f1675ac593b261b41a5144bc14f41163bd2a0c2"
}
]
}
1 change: 1 addition & 0 deletions example/react-native.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ module.exports = {
root: path.join(__dirname, '..'),
},
},
assets: ['./assets/'],
};
17 changes: 14 additions & 3 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import {dbSetupTests, queriesTests, runTests, blobTests} from './tests/index';
import {styled} from 'nativewind';
import {registerHooksTests} from './tests/hooks.spec';
import {open} from '@op-engineering/op-sqlite';
import {moveAssetsDatabase, open} from '@op-engineering/op-sqlite';
import clsx from 'clsx';
import {preparedStatementsTests} from './tests/preparedStatements.spec';
import {constantsTests} from './tests/constants.spec';
Expand Down Expand Up @@ -82,14 +82,24 @@ export default function App() {
};

const copyDbPathToClipboad = async () => {
const db = await open({name: 'dbPath.sqlite'});
db.execute('CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT)');
const db = await open({name: 'dbPath.sqlite', encryptionKey: 'test'});
db.execute(
'CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY AUTOINCREMENT)',
);
const path = db.getDbPath();
await db.close();
console.warn(path);
Clipboard.setString(path);
};

const openAssetsDb = async () => {
const moved = moveAssetsDatabase('sample', 'sqlite');
console.log('moved', moved);
const db = open({name: 'sample.sqlite'});
const users = db.execute('SELECT * FROM User');
console.log('users', users.rows?._array);
};

const allTestsPassed = results.reduce((acc: boolean, r: any) => {
return acc && r.type !== 'incorrect';
}, true);
Expand All @@ -104,6 +114,7 @@ export default function App() {
title="Copy DB Path to clipboard"
onPress={copyDbPathToClipboad}
/>
<Button title="Open assets db" onPress={openAssetsDb} />
<Button title="Create 300k Record DB" onPress={createLargeDb} />
<Button title="Query 300k Records" onPress={queryLargeDb} />
{isLoading && <ActivityIndicator color={'white'} size="large" />}
Expand Down
17 changes: 17 additions & 0 deletions ios/OPSQLite.mm
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ - (NSDictionary *)getConstants {
return @true;
}

RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(moveAssetsDatabase:(NSString *)name extension:(NSString *)extension) {
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, true) objectAtIndex:0];
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:name ofType:extension];
NSString *destinationPath = [documentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", name, extension]];

NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:destinationPath]) {
return @true;
}

NSError *error;
[fileManager copyItemAtPath:sourcePath toPath:destinationPath error:&error];
if (error) {
return @false;
}
return @true;
}

#if RCT_NEW_ARCH_ENABLED
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
Expand Down
19 changes: 11 additions & 8 deletions op-sqlite.podspec
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
require "json"

log_message = lambda do |message|
puts "\e[34m#{message}\e[0m"
end

package = JSON.parse(File.read(File.join(__dir__, "package.json")))
folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'
fabric_enabled = ENV['RCT_NEW_ARCH_ENABLED'] == '1'
Expand All @@ -26,11 +30,12 @@ Pod::Spec.new do |s|
}

if ENV['OP_SQLITE_USE_SQLCIPHER'] == '1' then
puts "OP-SQLITE using SQLCipher! 🔒\n"
log_message.call("[OP-SQLITE] using SQLCipher! 🔒")
s.exclude_files = "cpp/sqlite3.c", "cpp/sqlite3.h"
xcconfig[:GCC_PREPROCESSOR_DEFINITIONS] += " OP_SQLITE_USE_SQLCIPHER=1"
xcconfig[:GCC_PREPROCESSOR_DEFINITIONS] += " OP_SQLITE_USE_SQLCIPHER=1 HAVE_FULLFSYNC=1 SQLITE_HAS_CODEC SQLITE_TEMP_STORE=2"
s.dependency "OpenSSL-Universal"
else
puts "OP-SQLITE using SQLite! 📦\n"
log_message.call("[OP-SQLITE] using vanilla SQLite! 📦")
s.exclude_files = "cpp/sqlcipher/sqlite3.c", "cpp/sqlcipher/sqlite3.h"
end

Expand All @@ -46,22 +51,20 @@ Pod::Spec.new do |s|

optimizedCflags = other_cflags + '$(inherited) -DSQLITE_DQS=0 -DSQLITE_DEFAULT_MEMSTATUS=0 -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 -DSQLITE_LIKE_DOESNT_MATCH_BLOBS=1 -DSQLITE_MAX_EXPR_DEPTH=0 -DSQLITE_OMIT_DEPRECATED=1 -DSQLITE_OMIT_PROGRESS_CALLBACK=1 -DSQLITE_OMIT_SHARED_CACHE=1 -DSQLITE_USE_ALLOCA=1'



if ENV['OP_SQLITE_USE_PHONE_VERSION'] == '1' then
puts "OP-SQLITE using iOS embedded SQLite! 📱\n"
log_message.call("[OP-SQLITE] using iOS embedded SQLite! 📱")
xcconfig[:GCC_PREPROCESSOR_DEFINITIONS] += " OP_SQLITE_USE_PHONE_VERSION=1"
s.exclude_files = "cpp/sqlite3.c", "cpp/sqlite3.h"
s.library = "sqlite3"
end

if ENV['OP_SQLITE_PERF'] == '1' then
puts "OP-SQLITE performance mode enabled! 🚀\n"
log_message.call("[OP-SQLITE] performance mode enabled! 🚀")
xcconfig[:OTHER_CFLAGS] = optimizedCflags + ' -DSQLITE_THREADSAFE=0 '
end

if ENV['OP_SQLITE_PERF'] == '2' then
puts "OP-SQLITE (thread safe) performance mode enabled! 🚀\n"
log_message.call("[OP-SQLITE] (thread safe) performance mode enabled! 🚀")
xcconfig[:OTHER_CFLAGS] = optimizedCflags + ' -DSQLITE_THREADSAFE=1 '
end

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@op-engineering/op-sqlite",
"version": "3.0.1",
"version": "3.0.7",
"description": "Next generation SQLite for React Native",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down Expand Up @@ -28,7 +28,7 @@
"example": "yarn --cwd example",
"pods": "cd example && yarn pods",
"bootstrap": "yarn example && yarn && yarn pods",
"bump": "./bump-version.sh"
"release": "./bump-version.sh && git add . && git commit -m 'Release v$(node -p \"require('./package.json').version\")' && git tag v$(node -p \"require('./package.json').version\") && git push origin main --tags && npm publish"
},
"keywords": [
"react-native",
Expand Down
Loading

0 comments on commit 9bb229c

Please sign in to comment.