react-native-nitro-sqlite 8.2.1-nitro.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. package/RNNitroSQLite.podspec +59 -0
  2. package/android/.gradle/8.9/checksums/checksums.lock +0 -0
  3. package/android/.gradle/8.9/dependencies-accessors/gc.properties +0 -0
  4. package/android/.gradle/8.9/fileChanges/last-build.bin +0 -0
  5. package/android/.gradle/8.9/fileHashes/fileHashes.lock +0 -0
  6. package/android/.gradle/8.9/gc.properties +0 -0
  7. package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
  8. package/android/.gradle/buildOutputCleanup/cache.properties +2 -0
  9. package/android/.gradle/vcs-1/gc.properties +0 -0
  10. package/android/CMakeLists.txt +38 -0
  11. package/android/build.gradle +119 -0
  12. package/android/src/main/AndroidManifest.xml +4 -0
  13. package/android/src/main/cpp/cpp-adapter.cpp +21 -0
  14. package/android/src/main/java/com/margelo/rnnitrosqlite/RNNitroSQLiteOnLoadModule.java +57 -0
  15. package/android/src/main/java/com/margelo/rnnitrosqlite/RNNitroSQLitePackage.java +48 -0
  16. package/cpp/NitroSQLiteException.hpp +63 -0
  17. package/cpp/ThreadPool.cpp +91 -0
  18. package/cpp/ThreadPool.hpp +50 -0
  19. package/cpp/importSqlFile.cpp +48 -0
  20. package/cpp/importSqlFile.hpp +15 -0
  21. package/cpp/logs.hpp +38 -0
  22. package/cpp/macros.hpp +13 -0
  23. package/cpp/operations.cpp +296 -0
  24. package/cpp/operations.hpp +24 -0
  25. package/cpp/specs/HybridNativeQueryResult.cpp +20 -0
  26. package/cpp/specs/HybridNativeQueryResult.hpp +35 -0
  27. package/cpp/specs/HybridNitroSQLite.cpp +132 -0
  28. package/cpp/specs/HybridNitroSQLite.hpp +36 -0
  29. package/cpp/sqlite/sqlite3.c +237594 -0
  30. package/cpp/sqlite/sqlite3.h +12556 -0
  31. package/cpp/sqliteExecuteBatch.cpp +70 -0
  32. package/cpp/sqliteExecuteBatch.hpp +30 -0
  33. package/cpp/types.hpp +53 -0
  34. package/cpp/utils.hpp +61 -0
  35. package/ios/NitroSQLite.xcodeproj/project.pbxproj +447 -0
  36. package/ios/OnLoad.mm +41 -0
  37. package/ios/RNNitroSQLite-Swift.h +1 -0
  38. package/lib/commonjs/index.js +58 -0
  39. package/lib/commonjs/index.js.map +1 -0
  40. package/lib/commonjs/nitro.js +10 -0
  41. package/lib/commonjs/nitro.js.map +1 -0
  42. package/lib/commonjs/operations/execute.js +35 -0
  43. package/lib/commonjs/operations/execute.js.map +1 -0
  44. package/lib/commonjs/operations/session.js +39 -0
  45. package/lib/commonjs/operations/session.js.map +1 -0
  46. package/lib/commonjs/operations/transaction.js +95 -0
  47. package/lib/commonjs/operations/transaction.js.map +1 -0
  48. package/lib/commonjs/specs/NativeNitroSQLiteOnLoad.ios.js +11 -0
  49. package/lib/commonjs/specs/NativeNitroSQLiteOnLoad.ios.js.map +1 -0
  50. package/lib/commonjs/specs/NativeNitroSQLiteOnLoad.js +9 -0
  51. package/lib/commonjs/specs/NativeNitroSQLiteOnLoad.js.map +1 -0
  52. package/lib/commonjs/specs/NativeQueryResult.nitro.js +6 -0
  53. package/lib/commonjs/specs/NativeQueryResult.nitro.js.map +1 -0
  54. package/lib/commonjs/specs/NitroSQLite.nitro.js +6 -0
  55. package/lib/commonjs/specs/NitroSQLite.nitro.js.map +1 -0
  56. package/lib/commonjs/typeORM.js +62 -0
  57. package/lib/commonjs/typeORM.js.map +1 -0
  58. package/lib/commonjs/types.js +35 -0
  59. package/lib/commonjs/types.js.map +1 -0
  60. package/lib/module/index.js +23 -0
  61. package/lib/module/index.js.map +1 -0
  62. package/lib/module/nitro.js +4 -0
  63. package/lib/module/nitro.js.map +1 -0
  64. package/lib/module/operations/execute.js +28 -0
  65. package/lib/module/operations/execute.js.map +1 -0
  66. package/lib/module/operations/session.js +31 -0
  67. package/lib/module/operations/session.js.map +1 -0
  68. package/lib/module/operations/transaction.js +88 -0
  69. package/lib/module/operations/transaction.js.map +1 -0
  70. package/lib/module/specs/NativeNitroSQLiteOnLoad.ios.js +5 -0
  71. package/lib/module/specs/NativeNitroSQLiteOnLoad.ios.js.map +1 -0
  72. package/lib/module/specs/NativeNitroSQLiteOnLoad.js +3 -0
  73. package/lib/module/specs/NativeNitroSQLiteOnLoad.js.map +1 -0
  74. package/lib/module/specs/NativeQueryResult.nitro.js +2 -0
  75. package/lib/module/specs/NativeQueryResult.nitro.js.map +1 -0
  76. package/lib/module/specs/NitroSQLite.nitro.js +2 -0
  77. package/lib/module/specs/NitroSQLite.nitro.js.map +1 -0
  78. package/lib/module/typeORM.js +54 -0
  79. package/lib/module/typeORM.js.map +1 -0
  80. package/lib/module/types.js +33 -0
  81. package/lib/module/types.js.map +1 -0
  82. package/lib/tsconfig.tsbuildinfo +1 -0
  83. package/lib/typescript/index.d.ts +30 -0
  84. package/lib/typescript/nitro.d.ts +7 -0
  85. package/lib/typescript/operations/execute.d.ts +3 -0
  86. package/lib/typescript/operations/session.d.ts +4 -0
  87. package/lib/typescript/operations/transaction.d.ts +5 -0
  88. package/lib/typescript/specs/NativeNitroSQLiteOnLoad.d.ts +6 -0
  89. package/lib/typescript/specs/NativeNitroSQLiteOnLoad.ios.d.ts +3 -0
  90. package/lib/typescript/specs/NativeQueryResult.nitro.d.ts +37 -0
  91. package/lib/typescript/specs/NitroSQLite.nitro.d.ts +19 -0
  92. package/lib/typescript/typeORM.d.ts +19 -0
  93. package/lib/typescript/types.d.ts +79 -0
  94. package/nitrogen/generated/android/RNNitroSQLite+autolinking.cmake +48 -0
  95. package/nitrogen/generated/android/RNNitroSQLite+autolinking.gradle +25 -0
  96. package/nitrogen/generated/android/RNNitroSQLiteOnLoad.cpp +40 -0
  97. package/nitrogen/generated/android/RNNitroSQLiteOnLoad.hpp +25 -0
  98. package/nitrogen/generated/android/RNNitroSQLiteOnLoad.kt +1 -0
  99. package/nitrogen/generated/ios/RNNitroSQLite+autolinking.rb +56 -0
  100. package/nitrogen/generated/ios/RNNitroSQLite-Swift-Cxx-Bridge.cpp +8 -0
  101. package/nitrogen/generated/ios/RNNitroSQLite-Swift-Cxx-Bridge.hpp +303 -0
  102. package/nitrogen/generated/ios/RNNitroSQLite-Swift-Cxx-Umbrella.hpp +67 -0
  103. package/nitrogen/generated/ios/RNNitroSQLiteAutolinking.mm +35 -0
  104. package/nitrogen/generated/ios/RNNitroSQLiteAutolinking.swift +10 -0
  105. package/nitrogen/generated/shared/c++/BatchQueryCommand.hpp +77 -0
  106. package/nitrogen/generated/shared/c++/BatchQueryResult.hpp +68 -0
  107. package/nitrogen/generated/shared/c++/ColumnType.hpp +68 -0
  108. package/nitrogen/generated/shared/c++/FileLoadResult.hpp +72 -0
  109. package/nitrogen/generated/shared/c++/HybridNativeQueryResultSpec.cpp +24 -0
  110. package/nitrogen/generated/shared/c++/HybridNativeQueryResultSpec.hpp +74 -0
  111. package/nitrogen/generated/shared/c++/HybridNitroSQLiteSpec.cpp +31 -0
  112. package/nitrogen/generated/shared/c++/HybridNitroSQLiteSpec.hpp +91 -0
  113. package/nitrogen/generated/shared/c++/SQLiteQueryColumnMetadata.hpp +78 -0
  114. package/package.json +98 -0
  115. package/src/index.ts +26 -0
  116. package/src/nitro.ts +11 -0
  117. package/src/operations/execute.ts +45 -0
  118. package/src/operations/session.ts +58 -0
  119. package/src/operations/transaction.ts +137 -0
  120. package/src/specs/NativeNitroSQLiteOnLoad.ios.ts +7 -0
  121. package/src/specs/NativeNitroSQLiteOnLoad.ts +7 -0
  122. package/src/specs/NativeQueryResult.nitro.ts +44 -0
  123. package/src/specs/NitroSQLite.nitro.ts +39 -0
  124. package/src/typeORM.ts +101 -0
  125. package/src/types.ts +105 -0
@@ -0,0 +1,59 @@
1
+ require "json"
2
+
3
+ package = JSON.parse(File.read(File.join(__dir__, "package.json")))
4
+
5
+ # TODO: Should be customizable in package.json.
6
+ # Used to create comparable benchmark results
7
+ performance_mode = 1
8
+
9
+ Pod::Spec.new do |s|
10
+ s.name = "RNNitroSQLite"
11
+ s.version = package["version"]
12
+ s.summary = package["description"]
13
+ s.homepage = package["homepage"]
14
+ s.license = package["license"]
15
+ s.authors = package["author"]
16
+
17
+ s.platforms = { :ios => min_ios_version_supported, :visionos => "1.0" }
18
+ s.source = { :git => "https://github.com/margelo/react-native-nitro-sqlite.git", :tag => "#{s.version}" }
19
+
20
+ s.pod_target_xcconfig = {
21
+ :GCC_PREPROCESSOR_DEFINITIONS => "HAVE_FULLFSYNC=1",
22
+ :WARNING_CFLAGS => "-Wno-shorten-64-to-32 -Wno-comma -Wno-unreachable-code -Wno-conditional-uninitialized -Wno-deprecated-declarations",
23
+ :USE_HEADERMAP => "No",
24
+ 'CLANG_CXX_LANGUAGE_STANDARD' => 'c++20',
25
+ 'CLANG_CXX_LIBRARY' => 'libc++'
26
+ }
27
+
28
+ # s.header_mappings_dir = "cpp"
29
+ s.source_files = "ios/**/*.{h,hpp,m,mm}", "cpp/**/*.{h,hpp,c,cpp}"
30
+
31
+ load 'nitrogen/generated/ios/RNNitroSQLite+autolinking.rb'
32
+ add_nitrogen_files(s)
33
+
34
+ if defined?(install_modules_dependencies())
35
+ install_modules_dependencies(s)
36
+ else
37
+ s.dependency "React-callinvoker"
38
+ s.dependency "React"
39
+ s.dependency "React-Core"
40
+ end
41
+
42
+ optimizedCflags = '$(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'
43
+
44
+ if performance_mode == '1' then
45
+ log_message.call("Thread unsafe (1) performance mode enabled. Use only transactions! 🚀🚀")
46
+ xcconfig[:OTHER_CFLAGS] = optimizedCflags + ' -DSQLITE_THREADSAFE=0 '
47
+ end
48
+
49
+ if performance_mode == '2' then
50
+ log_message.call("Thread safe (2) performance mode enabled 🚀")
51
+ xcconfig[:OTHER_CFLAGS] = optimizedCflags + ' -DSQLITE_THREADSAFE=1 '
52
+ end
53
+
54
+ if ENV['NITRO_SQLITE_USE_PHONE_VERSION'] == '1' then
55
+ s.exclude_files = "cpp/sqlite3.c", "cpp/sqlite3.h"
56
+ s.library = "sqlite3"
57
+ end
58
+
59
+ end
File without changes
@@ -0,0 +1,2 @@
1
+ #Sun Oct 06 16:38:54 CEST 2024
2
+ gradle.version=8.9
File without changes
@@ -0,0 +1,38 @@
1
+ project(RNNitroSQLite)
2
+ cmake_minimum_required(VERSION 3.9.0)
3
+
4
+ set (PACKAGE_NAME RNNitroSQLite)
5
+ set (CMAKE_VERBOSE_MAKEFILE ON)
6
+ set (CMAKE_CXX_STANDARD 20)
7
+
8
+ add_definitions(
9
+ ${SQLITE_FLAGS}
10
+ )
11
+
12
+ file(GLOB_RECURSE cpp_files RELATIVE ${CMAKE_SOURCE_DIR}
13
+ "../cpp/**.c"
14
+ "../cpp/**.cpp"
15
+ "src/main/cpp/cpp-adapter.cpp"
16
+ )
17
+
18
+ # Create library "RNNitroSQLite" and add all C++ files to it
19
+ add_library(${PACKAGE_NAME} SHARED
20
+ ${cpp_files}
21
+ )
22
+
23
+ include(${CMAKE_SOURCE_DIR}/../nitrogen/generated/android/RNNitroSQLite+autolinking.cmake)
24
+
25
+ # Specifies a path to native header files.
26
+ include_directories(
27
+ ../cpp
28
+ ../cpp/specs
29
+ ../cpp/sqlite
30
+ )
31
+
32
+ find_library(LOG_LIB log)
33
+
34
+ target_link_libraries(
35
+ ${PACKAGE_NAME}
36
+ ${LOG_LIB}
37
+ android
38
+ )
@@ -0,0 +1,119 @@
1
+ buildscript {
2
+ repositories {
3
+ google()
4
+ mavenCentral()
5
+ }
6
+
7
+ dependencies {
8
+ classpath "com.android.tools.build:gradle:7.2.2"
9
+ }
10
+ }
11
+
12
+ def reactNativeArchitectures() {
13
+ def value = rootProject.getProperties().get("reactNativeArchitectures")
14
+ return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
15
+ }
16
+
17
+ def isNewArchitectureEnabled() {
18
+ return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
19
+ }
20
+
21
+ def SQLITE_FLAGS = rootProject.properties['nitroSqliteFlags']
22
+
23
+ apply plugin: "com.android.library"
24
+ apply plugin: 'org.jetbrains.kotlin.android'
25
+ apply from: '../nitrogen/generated/android/RNNitroSQLite+autolinking.gradle'
26
+
27
+ if (isNewArchitectureEnabled()) {
28
+ apply plugin: "com.facebook.react"
29
+ }
30
+
31
+ def getExtOrDefault(name) {
32
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["RNNitroSQLite_" + name]
33
+ }
34
+
35
+ def getExtOrIntegerDefault(name) {
36
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["RNNitroSQLite_" + name]).toInteger()
37
+ }
38
+
39
+ android {
40
+ namespace "com.margelo.rnnitrosqlite"
41
+
42
+ ndkVersion getExtOrDefault("ndkVersion")
43
+ compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
44
+
45
+ defaultConfig {
46
+ minSdkVersion getExtOrIntegerDefault("minSdkVersion")
47
+ targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
48
+ buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
49
+
50
+ externalNativeBuild {
51
+ cmake {
52
+ cppFlags "-O2", "-fexceptions", "-frtti", "-std=c++1y", "-DONANDROID", "-Wall", "-fstack-protector-all"
53
+ arguments "-DANDROID_STL=c++_shared", "-DSQLITE_FLAGS='${SQLITE_FLAGS ? SQLITE_FLAGS : ''}'"
54
+ abiFilters (*reactNativeArchitectures())
55
+ }
56
+ }
57
+ }
58
+
59
+ externalNativeBuild {
60
+ cmake {
61
+ path "CMakeLists.txt"
62
+ }
63
+ }
64
+
65
+ buildFeatures {
66
+ buildConfig true
67
+ prefab true
68
+ }
69
+
70
+ buildTypes {
71
+ release {
72
+ minifyEnabled false
73
+ }
74
+ }
75
+
76
+ lintOptions {
77
+ disable "GradleCompatible"
78
+ }
79
+
80
+ compileOptions {
81
+ sourceCompatibility JavaVersion.VERSION_1_8
82
+ targetCompatibility JavaVersion.VERSION_1_8
83
+ }
84
+
85
+ sourceSets {
86
+ main {
87
+ if (isNewArchitectureEnabled()) {
88
+ java.srcDirs += [
89
+ // React Codegen files
90
+ "${project.buildDir}/generated/source/codegen/java"
91
+ ]
92
+ }
93
+ }
94
+ }
95
+ }
96
+
97
+ repositories {
98
+ mavenCentral()
99
+ google()
100
+ }
101
+
102
+
103
+ dependencies {
104
+ // For < 0.71, this will be from the local maven repo
105
+ // For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
106
+ //noinspection GradleDynamicVersion
107
+ implementation "com.facebook.react:react-native:+"
108
+
109
+ // Add a dependency on NitroModules
110
+ implementation project(":react-native-nitro-modules")
111
+ }
112
+
113
+ if (isNewArchitectureEnabled()) {
114
+ react {
115
+ jsRootDir = file("../src/")
116
+ libraryName = "RNNitroSQLite"
117
+ codegenJavaPackageName = "com.margelo.rnnitrosqlite"
118
+ }
119
+ }
@@ -0,0 +1,4 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2
+ package="com.margelo.rnnitrosqlite">
3
+
4
+ </manifest>
@@ -0,0 +1,21 @@
1
+ #include <fbjni/fbjni.h>
2
+ #include <jni.h>
3
+ #include <jsi/jsi.h>
4
+ #include <typeinfo>
5
+ #include "HybridNitroSQLite.hpp"
6
+ #include "RNNitroSQLiteOnLoad.hpp"
7
+
8
+ using namespace margelo::nitro::rnnitrosqlite;
9
+
10
+ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {
11
+ return margelo::nitro::rnnitrosqlite::initialize(vm);
12
+ }
13
+
14
+ extern "C"
15
+ JNIEXPORT void JNICALL
16
+ Java_com_margelo_rnnitrosqlite_RNNitroSQLiteOnLoadModule_setDocPathInJNI(JNIEnv *env, jclass clazz,
17
+ jstring doc_path) {
18
+ const char *nativeString = env->GetStringUTFChars(doc_path, nullptr);
19
+ HybridNitroSQLite::docPath = std::string(nativeString);
20
+ env->ReleaseStringUTFChars(doc_path, nativeString);
21
+ }
@@ -0,0 +1,57 @@
1
+ package com.margelo.rnnitrosqlite;
2
+
3
+ import androidx.annotation.NonNull;
4
+
5
+ import com.facebook.react.bridge.Callback;
6
+ import com.facebook.react.bridge.NativeModule;
7
+ import com.facebook.react.bridge.Promise;
8
+ import com.facebook.react.bridge.ReactApplicationContext;
9
+ import com.facebook.react.bridge.ReactContext;
10
+ import com.facebook.react.bridge.ReactContextBaseJavaModule;
11
+ import com.facebook.react.bridge.ReactMethod;
12
+ import java.util.Map;
13
+ import java.util.HashMap;
14
+ import com.margelo.rnnitrosqlite.NativeNitroSQLiteOnLoadSpec;
15
+
16
+ public class RNNitroSQLiteOnLoadModule extends NativeNitroSQLiteOnLoadSpec {
17
+ private static ReactApplicationContext _reactContext;
18
+ private Callback reactApplicationContextReadyCallback;
19
+
20
+ public RNNitroSQLiteOnLoadModule(ReactApplicationContext reactContext) {
21
+ super(reactContext);
22
+
23
+ _reactContext = reactContext;
24
+ setDocPath(reactContext);
25
+
26
+ if (reactApplicationContextReadyCallback != null) {
27
+ reactApplicationContextReadyCallback.invoke();
28
+ }
29
+ }
30
+
31
+ public static void setDocPath(ReactApplicationContext context) {
32
+ final String path = context.getFilesDir().getAbsolutePath();
33
+ setDocPathInJNI(path);
34
+ }
35
+
36
+ @Override
37
+ @NonNull
38
+ public String getName() {
39
+ return NAME;
40
+ }
41
+
42
+ @Override
43
+ public void onReactApplicationContextReady(Callback callback) {
44
+ if (_reactContext != null) {
45
+ callback.invoke();
46
+ return;
47
+ }
48
+
49
+ reactApplicationContextReadyCallback = callback;
50
+ }
51
+
52
+ public static ReactApplicationContext getReactContext() {
53
+ return _reactContext;
54
+ }
55
+
56
+ private native static void setDocPathInJNI(String docPath);
57
+ }
@@ -0,0 +1,48 @@
1
+ package com.margelo.rnnitrosqlite;
2
+
3
+ import androidx.annotation.NonNull;
4
+ import androidx.annotation.Nullable;
5
+ import com.facebook.react.bridge.NativeModule;
6
+ import com.facebook.react.bridge.ReactApplicationContext;
7
+ import com.facebook.react.module.model.ReactModuleInfo;
8
+ import com.facebook.react.module.model.ReactModuleInfoProvider;
9
+ import com.facebook.react.TurboReactPackage;
10
+
11
+ import java.util.Collections;
12
+ import java.util.HashMap;
13
+ import java.util.List;
14
+ import java.util.Map;
15
+
16
+ public class RNNitroSQLitePackage extends TurboReactPackage {
17
+ static {
18
+ System.loadLibrary("RNNitroSQLite");
19
+ }
20
+
21
+ @Nullable
22
+ @Override
23
+ public NativeModule getModule(String name, ReactApplicationContext reactContext) {
24
+ if (name.equals(RNNitroSQLiteOnLoadModule.NAME)) {
25
+ return new RNNitroSQLiteOnLoadModule(reactContext);
26
+ } else {
27
+ return null;
28
+ }
29
+ }
30
+
31
+ @Override
32
+ public ReactModuleInfoProvider getReactModuleInfoProvider() {
33
+ return () -> {
34
+ final Map<String, ReactModuleInfo> moduleInfos = new HashMap<>();
35
+ moduleInfos.put(
36
+ RNNitroSQLiteOnLoadModule.NAME,
37
+ new ReactModuleInfo(
38
+ RNNitroSQLiteOnLoadModule.NAME,
39
+ RNNitroSQLiteOnLoadModule.NAME,
40
+ false,
41
+ true,
42
+ true,
43
+ false,
44
+ true));
45
+ return moduleInfos;
46
+ };
47
+ }
48
+ }
@@ -0,0 +1,63 @@
1
+ #pragma once
2
+
3
+ #include <string>
4
+ #include <iostream>
5
+ #include <exception>
6
+
7
+ enum NitroSQLiteExceptionType {
8
+ UnknownError,
9
+ DatabaseCannotBeOpened,
10
+ DatabaseNotOpen,
11
+ UnableToAttachToDatabase,
12
+ SqlExecutionError,
13
+ CouldNotLoadFile,
14
+ NoBatchCommandsProvided
15
+ };
16
+
17
+ inline std::unordered_map<NitroSQLiteExceptionType, std::string> exceptionTypeStrings = {
18
+ {UnknownError, "UnknownError"},
19
+ {DatabaseCannotBeOpened, "DatabaseCannotBeOpened"},
20
+ {DatabaseNotOpen, "DatabaseNotOpen"},
21
+ {UnableToAttachToDatabase, "UnableToAttachToDatabase"},
22
+ {SqlExecutionError, "SqlExecutionError"},
23
+ {CouldNotLoadFile, "CouldNotLoadFile"},
24
+ {NoBatchCommandsProvided, "NoBatchCommandsProvided"}
25
+ };
26
+
27
+ inline std::string typeToString(NitroSQLiteExceptionType type) {
28
+ return exceptionTypeStrings[type];
29
+ }
30
+
31
+ class NitroSQLiteException : public std::exception {
32
+ public:
33
+ explicit NitroSQLiteException(const char* message): NitroSQLiteException(NitroSQLiteExceptionType::UnknownError, message) {}
34
+ explicit NitroSQLiteException(const std::string& message): NitroSQLiteException(NitroSQLiteExceptionType::UnknownError, message) {}
35
+ NitroSQLiteException(const NitroSQLiteExceptionType& type, const char* message) : NitroSQLiteException(type, std::string(message)) {}
36
+ NitroSQLiteException(const NitroSQLiteExceptionType& type, const std::string& message)
37
+ : _exceptionString("[react-native-nitro-sqlite] " + typeToString(type) + ": " + message) {}
38
+
39
+ private:
40
+ const std::string _exceptionString;
41
+
42
+ public:
43
+ [[nodiscard]] const char* what() const noexcept override {
44
+ return this->_exceptionString.c_str();
45
+ }
46
+
47
+ static NitroSQLiteException DatabaseNotOpen(const std::string& dbName) {
48
+ return NitroSQLiteException(NitroSQLiteExceptionType::UnableToAttachToDatabase, dbName + " is not open");
49
+ }
50
+
51
+ static NitroSQLiteException SqlExecution(const std::string& errorMessage) {
52
+ return NitroSQLiteException(NitroSQLiteExceptionType::SqlExecutionError, errorMessage);
53
+ }
54
+
55
+ static NitroSQLiteException DatabaseFileNotFound(const std::string& dbFilePath) {
56
+ return NitroSQLiteException(NitroSQLiteExceptionType::SqlExecutionError, "Database file not found: " + dbFilePath);
57
+ }
58
+
59
+ static NitroSQLiteException CouldNotLoadFile(const std::string& fileLocation, std::optional<std::string> additionalLine = std::nullopt) {
60
+ const auto message = "Could not load file: " + fileLocation + (additionalLine ? (". " + *additionalLine) : "");
61
+ return NitroSQLiteException(NitroSQLiteExceptionType::CouldNotLoadFile, message);
62
+ }
63
+ };
@@ -0,0 +1,91 @@
1
+ //
2
+ // ThreadPool.cpp
3
+ // react-native-nitro-sqlite
4
+ //
5
+ // Created by Oscar on 13.03.22.
6
+ //
7
+
8
+ #include "ThreadPool.hpp"
9
+
10
+ namespace margelo::rnnitrosqlite {
11
+
12
+ ThreadPool::ThreadPool() : done(false) {
13
+ // This returns the number of threads supported by the system. If the
14
+ // function can't figure out this information, it returns 0. 0 is not good,
15
+ // so we create at least 1
16
+ auto numberOfThreads = std::thread::hardware_concurrency();
17
+ if (numberOfThreads == 0) {
18
+ numberOfThreads = 1;
19
+ }
20
+
21
+ for (unsigned i = 0; i < numberOfThreads; ++i) {
22
+ // The threads will execute the private member `doWork`. Note that we need
23
+ // to pass a reference to the function (namespaced with the class name) as
24
+ // the first argument, and the current object as second argument
25
+ threads.push_back(std::thread(&ThreadPool::doWork, this));
26
+ }
27
+ }
28
+
29
+ // The destructor joins all the threads so the program can exit gracefully.
30
+ // This will be executed if there is any exception (e.g. creating the threads)
31
+ ThreadPool::~ThreadPool() {
32
+ // So threads know it's time to shut down
33
+ done = true;
34
+
35
+ // Wake up all the threads, so they can finish and be joined
36
+ workQueueConditionVariable.notify_all();
37
+ for (auto& thread : threads) {
38
+ if (thread.joinable()) {
39
+ thread.join();
40
+ }
41
+ }
42
+ }
43
+
44
+ // This function will be called by the server every time there is a request
45
+ // that needs to be processed by the thread pool
46
+ void ThreadPool::queueWork(std::function<void(void)> task) {
47
+ // Grab the mutex
48
+ std::lock_guard<std::mutex> g(workQueueMutex);
49
+
50
+ // Push the request to the queue
51
+ workQueue.push(task);
52
+
53
+ // Notify one thread that there are requests to process
54
+ workQueueConditionVariable.notify_one();
55
+ }
56
+
57
+ // Function used by the threads to grab work from the queue
58
+ void ThreadPool::doWork() {
59
+ // Loop while the queue is not destructing
60
+ while (!done) {
61
+ std::function<void(void)> task;
62
+
63
+ // Create a scope, so we don't lock the queue for longer than necessary
64
+ {
65
+ std::unique_lock<std::mutex> g(workQueueMutex);
66
+ workQueueConditionVariable.wait(g, [&] {
67
+ // Only wake up if there are elements in the queue or the program is
68
+ // shutting down
69
+ return !workQueue.empty() || done;
70
+ });
71
+
72
+ // If we are shutting down exit witout trying to process more work
73
+ if (done) {
74
+ break;
75
+ }
76
+
77
+ task = workQueue.front();
78
+ workQueue.pop();
79
+ }
80
+ ++busy;
81
+ task();
82
+ --busy;
83
+ }
84
+ }
85
+
86
+ void ThreadPool::waitFinished() {
87
+ std::unique_lock<std::mutex> g(workQueueMutex);
88
+ workQueueConditionVariable.wait(g, [&] { return workQueue.empty() && (busy == 0); });
89
+ }
90
+
91
+ } // namespace margelo::rnnitrosqlite
@@ -0,0 +1,50 @@
1
+ //
2
+ // ThreadPool.hpp
3
+ // react-native-nitro-sqlite
4
+ //
5
+ // Created by Oscar on 13.03.22.
6
+ //
7
+
8
+ #pragma once
9
+
10
+ #include <condition_variable>
11
+ #include <exception>
12
+ #include <mutex>
13
+ #include <queue>
14
+ #include <stdio.h>
15
+ #include <thread>
16
+ #include <vector>
17
+
18
+ namespace margelo::rnnitrosqlite {
19
+
20
+ class ThreadPool {
21
+ public:
22
+ ThreadPool();
23
+ ~ThreadPool();
24
+ void queueWork(std::function<void(void)> task);
25
+ void waitFinished();
26
+
27
+ private:
28
+ unsigned int busy;
29
+ // This condition variable is used for the threads to wait until there is work
30
+ // to do
31
+ std::condition_variable_any workQueueConditionVariable;
32
+
33
+ // We store the threads in a vector, so we can later stop them gracefully
34
+ std::vector<std::thread> threads;
35
+
36
+ // Mutex to protect workQueue
37
+ std::mutex workQueueMutex;
38
+
39
+ // Queue of requests waiting to be processed
40
+ std::queue<std::function<void(void)>> workQueue;
41
+
42
+ // This will be set to true when the thread pool is shutting down. This tells
43
+ // the threads to stop looping and finish
44
+ bool done;
45
+
46
+ // Function used by the threads to grab work from the queue
47
+ void doWork();
48
+ };
49
+
50
+ } // namespace margelo::rnnitrosqlite
@@ -0,0 +1,48 @@
1
+ /**
2
+ * SQL File Loader implementation
3
+ */
4
+
5
+ #include "importSqlFile.hpp"
6
+ #include <fstream>
7
+ #include <iostream>
8
+ #include "NitroSQLiteException.hpp"
9
+ #include "operations.hpp"
10
+
11
+ namespace margelo::rnnitrosqlite {
12
+
13
+ SQLiteOperationResult importSqlFile(const std::string& dbName, const std::string& fileLocation) {
14
+ std::string line;
15
+ std::ifstream sqFile(fileLocation);
16
+ if (sqFile.is_open()) {
17
+ try {
18
+ int rowsAffected = 0;
19
+ int commands = 0;
20
+ sqliteExecuteLiteral(dbName, "BEGIN EXCLUSIVE TRANSACTION");
21
+ while (std::getline(sqFile, line, '\n')) {
22
+ if (!line.empty()) {
23
+ try {
24
+ SQLiteOperationResult result = sqliteExecuteLiteral(dbName, line);
25
+ rowsAffected += result.rowsAffected;
26
+ commands++;
27
+ } catch (NitroSQLiteException& e) {
28
+ sqliteExecuteLiteral(dbName, "ROLLBACK");
29
+ sqFile.close();
30
+ throw NitroSQLiteException::CouldNotLoadFile(fileLocation, "Transaction was rolled back");
31
+ }
32
+ }
33
+ }
34
+
35
+ sqFile.close();
36
+ sqliteExecuteLiteral(dbName, "COMMIT");
37
+ return {.rowsAffected = rowsAffected, .commands = commands};
38
+ } catch (...) {
39
+ sqFile.close();
40
+ sqliteExecuteLiteral(dbName, "ROLLBACK");
41
+ throw NitroSQLiteException(NitroSQLiteExceptionType::UnknownError, "Unexpected error. Transaction was rolled back");
42
+ }
43
+ } else {
44
+ throw NitroSQLiteException::CouldNotLoadFile(fileLocation);
45
+ }
46
+ }
47
+
48
+ } // namespace margelo::rnnitrosqlite
@@ -0,0 +1,15 @@
1
+ /**
2
+ * SQL File Loader
3
+ * Utilizes the regular sqlite bridge to load an SQLFile inside a transaction
4
+ *
5
+ */
6
+
7
+ #pragma once
8
+
9
+ #include "types.hpp"
10
+
11
+ namespace margelo::rnnitrosqlite {
12
+
13
+ SQLiteOperationResult importSqlFile(const std::string& dbName, const std::string& fileLocation);
14
+
15
+ }
package/cpp/logs.hpp ADDED
@@ -0,0 +1,38 @@
1
+ #ifdef ANDROID
2
+ // LOGS ANDROID
3
+ #include <android/log.h>
4
+ #define LOG_TAG "react-native-nitro-sqlite"
5
+ #define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)
6
+ #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
7
+ #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
8
+ #define LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
9
+ #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
10
+ #define LOGSIMPLE(...)
11
+ #else
12
+ // LOGS NO ANDROID
13
+ #include <stdio.h>
14
+ #define LOG_TAG "react-native-nitro-sqlite"
15
+ #define LOGV(...) \
16
+ printf(" "); \
17
+ printf(__VA_ARGS__); \
18
+ printf("\t - <%s> \n", LOG_TAG);
19
+ #define LOGD(...) \
20
+ printf(" "); \
21
+ printf(__VA_ARGS__); \
22
+ printf("\t - <%s> \n", LOG_TAG);
23
+ #define LOGI(...) \
24
+ printf(" "); \
25
+ printf(__VA_ARGS__); \
26
+ printf("\t - <%s> \n", LOG_TAG);
27
+ #define LOGW(...) \
28
+ printf(" * Warning: "); \
29
+ printf(__VA_ARGS__); \
30
+ printf("\t - <%s> \n", LOG_TAG);
31
+ #define LOGE(...) \
32
+ printf(" *** Error: "); \
33
+ printf(__VA_ARGS__); \
34
+ printf("\t - <%s> \n", LOG_TAG);
35
+ #define LOGSIMPLE(...) \
36
+ printf(" "); \
37
+ printf(__VA_ARGS__);
38
+ #endif // ANDROID