-
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathOPSQLiteModule.kt
More file actions
121 lines (102 loc) · 3.96 KB
/
OPSQLiteModule.kt
File metadata and controls
121 lines (102 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.op.sqlite
import android.util.Log
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReadableMap
import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import java.io.OutputStream
import com.facebook.react.util.RNLog;
internal class OPSQLiteModule(context: ReactApplicationContext?) : ReactContextBaseJavaModule(context) {
override fun getName(): String {
return NAME
}
fun getTypedExportedConstants(): MutableMap<String, Any?> {
val constants: MutableMap<String, Any?> = HashMap()
val context = reactApplicationContext
val dbPath =
context.getDatabasePath("defaultDatabase")
.absolutePath
.replace("defaultDatabase", "")
constants["ANDROID_DATABASE_PATH"] = dbPath
val filesPath = context.filesDir.absolutePath
constants["ANDROID_FILES_PATH"] = filesPath
val externalFilesDir = context.getExternalFilesDir(null)
constants["ANDROID_EXTERNAL_FILES_PATH"] = externalFilesDir?.absolutePath
constants["IOS_DOCUMENT_PATH"] = null
constants["IOS_LIBRARY_PATH"] = null
return constants
}
override fun getConstants(): MutableMap<String, Any?> {
return getTypedExportedConstants()
}
@ReactMethod(isBlockingSynchronousMethod = true)
fun install(): Boolean {
return try {
OPSQLiteBridge.instance.install(reactApplicationContext)
true
} catch (exception: Exception) {
Log.e(NAME, "Install exception: $exception")
false
}
}
@ReactMethod(isBlockingSynchronousMethod = true)
fun getDylibPath(bundleId: String, name: String): Boolean {
throw Exception("Do not call getDylibPath on Android")
}
@ReactMethod
fun moveAssetsDatabase(args: ReadableMap, promise: Promise) {
val filename = args.getString("filename")!!
val path = args.getString("path") ?: "custom"
val overwrite = if(args.hasKey("overwrite")) { args.getBoolean("overwrite") } else false
val context = reactApplicationContext
val assetsManager = context.assets
try {
val databasesFolder =
context.getDatabasePath("defaultDatabase")
.absolutePath
.replace("defaultDatabase", "")
val outputFile = File(databasesFolder, filename)
if (outputFile.exists()) {
if(overwrite) {
outputFile.delete()
} else {
promise.resolve(true)
return
}
}
val inputStream: InputStream = assetsManager.open("$path/$filename")
// 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()
promise.resolve(true)
} catch (exception: Exception) {
RNLog.e(this.reactApplicationContext, "Exception: $exception")
promise.resolve(false)
}
}
override fun invalidate() {
super.invalidate()
OPSQLiteBridge.instance.invalidate()
}
companion object {
init {
if (BuildConfig.USE_LIBSQL) {
System.loadLibrary("sql_experimental")
}
System.loadLibrary("op-sqlite")
}
const val NAME = "OPSQLite"
}
}