diff --git a/.yarn/install-state.gz b/.yarn/install-state.gz index e7886808..24d1ca6c 100644 Binary files a/.yarn/install-state.gz and b/.yarn/install-state.gz differ diff --git a/android/src/main/java/com/op/sqlite/OPSQLiteModule.kt b/android/src/main/java/com/op/sqlite/OPSQLiteModule.kt index 30d31f0c..91ea3b03 100644 --- a/android/src/main/java/com/op/sqlite/OPSQLiteModule.kt +++ b/android/src/main/java/com/op/sqlite/OPSQLiteModule.kt @@ -3,6 +3,10 @@ package com.op.sqlite 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?) : @@ -38,6 +42,49 @@ internal class OPSQLiteModule(context: ReactApplicationContext?) : } } + @ReactMethod(isBlockingSynchronousMethod = true) + override fun moveAssetsDatabase(name: String, extension: String): Boolean { + val context = reactApplicationContext + val assetsManager = context.assets + + try { + +// val assets = assetsManager.list(""); + // 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 onCatalystInstanceDestroy() { OPSQLiteBridge.instance.clearState() } diff --git a/android/src/paper/java/com/op/sqlite/NativeOPSQLiteSpec.java b/android/src/paper/java/com/op/sqlite/NativeOPSQLiteSpec.java index b0dbf4bb..dc445573 100644 --- a/android/src/paper/java/com/op/sqlite/NativeOPSQLiteSpec.java +++ b/android/src/paper/java/com/op/sqlite/NativeOPSQLiteSpec.java @@ -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); } diff --git a/example/.yarn/install-state.gz b/example/.yarn/install-state.gz index 0fec8e9f..bda80cb0 100644 Binary files a/example/.yarn/install-state.gz and b/example/.yarn/install-state.gz differ diff --git a/example/android/app/src/main/assets/custom/sample.sqlite b/example/android/app/src/main/assets/custom/sample.sqlite new file mode 100644 index 00000000..8b9b0736 Binary files /dev/null and b/example/android/app/src/main/assets/custom/sample.sqlite differ diff --git a/example/android/link-assets-manifest.json b/example/android/link-assets-manifest.json new file mode 100644 index 00000000..9f1031f0 --- /dev/null +++ b/example/android/link-assets-manifest.json @@ -0,0 +1,9 @@ +{ + "migIndex": 1, + "data": [ + { + "path": "assets/sample.sqlite", + "sha1": "0f1675ac593b261b41a5144bc14f41163bd2a0c2" + } + ] +} diff --git a/example/assets/sample.sqlite b/example/assets/sample.sqlite new file mode 100644 index 00000000..8b9b0736 Binary files /dev/null and b/example/assets/sample.sqlite differ diff --git a/example/ios/OPSQLiteExample.xcodeproj/project.pbxproj b/example/ios/OPSQLiteExample.xcodeproj/project.pbxproj index 4074fae7..0fb65abe 100644 --- a/example/ios/OPSQLiteExample.xcodeproj/project.pbxproj +++ b/example/ios/OPSQLiteExample.xcodeproj/project.pbxproj @@ -12,6 +12,7 @@ 13B07FC11A68108700A75B9A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; }; 3B5A9E3FD2C56B364AF5A4F3 /* libPods-OPSQLiteExample.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 6EE3066EB7AAB7E17B1CAB50 /* libPods-OPSQLiteExample.a */; }; 81AB9BB82411601600AC10FF /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 81AB9BB72411601600AC10FF /* LaunchScreen.storyboard */; }; + 022AA638EE144393ADDCAEC0 /* sample.sqlite in Resources */ = {isa = PBXBuildFile; fileRef = 96FD9FD0FC4F4540AC7A9CE6 /* sample.sqlite */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -29,6 +30,7 @@ 81AB9BB72411601600AC10FF /* LaunchScreen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = LaunchScreen.storyboard; path = OPSQLiteExample/LaunchScreen.storyboard; sourceTree = ""; }; 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 = ""; }; ED297162215061F000B7C4FE /* JavaScriptCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = JavaScriptCore.framework; path = System/Library/Frameworks/JavaScriptCore.framework; sourceTree = SDKROOT; }; + 96FD9FD0FC4F4540AC7A9CE6 /* sample.sqlite */ = {isa = PBXFileReference; name = "sample.sqlite"; path = "../assets/sample.sqlite"; sourceTree = ""; fileEncoding = undefined; lastKnownFileType = unknown; explicitFileType = undefined; includeInIndex = 0; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -81,6 +83,7 @@ 83CBBA001A601CBA00E9B192 /* Products */, 2D16E6871FA4F8E400B85C8A /* Frameworks */, BBD78D7AC51CEA395F1C20DB /* Pods */, + A9BA4AF2CBC44B9BB28B8ABA /* Resources */, ); indentWidth = 2; sourceTree = ""; @@ -106,6 +109,15 @@ path = Pods; sourceTree = ""; }; + A9BA4AF2CBC44B9BB28B8ABA /* Resources */ = { + isa = "PBXGroup"; + children = ( + 96FD9FD0FC4F4540AC7A9CE6 /* sample.sqlite */, + ); + name = Resources; + sourceTree = ""; + path = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -169,6 +181,7 @@ files = ( 81AB9BB82411601600AC10FF /* LaunchScreen.storyboard in Resources */, 13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */, + 022AA638EE144393ADDCAEC0 /* sample.sqlite in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/example/ios/OPSQLiteExample/Info.plist b/example/ios/OPSQLiteExample/Info.plist index b3eb53c7..3e884935 100644 --- a/example/ios/OPSQLiteExample/Info.plist +++ b/example/ios/OPSQLiteExample/Info.plist @@ -32,7 +32,7 @@ NSLocationWhenInUseUsageDescription - + UILaunchStoryboardName LaunchScreen UIRequiredDeviceCapabilities diff --git a/example/ios/link-assets-manifest.json b/example/ios/link-assets-manifest.json new file mode 100644 index 00000000..9f1031f0 --- /dev/null +++ b/example/ios/link-assets-manifest.json @@ -0,0 +1,9 @@ +{ + "migIndex": 1, + "data": [ + { + "path": "assets/sample.sqlite", + "sha1": "0f1675ac593b261b41a5144bc14f41163bd2a0c2" + } + ] +} diff --git a/example/react-native.config.js b/example/react-native.config.js index a5166956..df1863e9 100644 --- a/example/react-native.config.js +++ b/example/react-native.config.js @@ -7,4 +7,5 @@ module.exports = { root: path.join(__dirname, '..'), }, }, + assets: ['./assets/'], }; diff --git a/example/src/App.tsx b/example/src/App.tsx index e7cbbb5a..1dad4035 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -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'; @@ -84,13 +84,23 @@ export default function App() { const copyDbPathToClipboad = async () => { const db = await open({name: 'dbPath.sqlite'}); - db.execute('CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT)'); + 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); @@ -105,6 +115,7 @@ export default function App() { title="Copy DB Path to clipboard" onPress={copyDbPathToClipboad} /> +