Skip to content

Commit

Permalink
add {sqlString:} option to sqliteAdapter.unsafeExecute
Browse files Browse the repository at this point in the history
  • Loading branch information
radex committed Mar 9, 2022
1 parent f993c89 commit c854711
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 10 deletions.
6 changes: 3 additions & 3 deletions native/shared/Database.cpp
Expand Up @@ -20,9 +20,9 @@ Database::Database(jsi::Runtime *runtime, std::string path, bool usesExclusiveLo
#ifdef ANDROID
executeMultiple("pragma temp_store = memory;");
#endif

executeMultiple("pragma journal_mode = WAL;");

#ifdef ANDROID
// NOTE: This was added in an attempt to fix mysterious `database disk image is malformed` issue when using
// headless JS services
Expand Down Expand Up @@ -52,7 +52,7 @@ jsi::JSError Database::dbError(std::string description) {

void Database::destroy() {
const std::lock_guard<std::mutex> lock(mutex_);

if (isDestroyed_) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion native/shared/Database.h
Expand Up @@ -31,6 +31,7 @@ class Database : public jsi::HostObject {
jsi::Value unsafeLoadFromSync(int jsonId, jsi::Object &schema, std::string preamble, std::string postamble);
void unsafeResetDatabase(jsi::String &schema, int schemaVersion);
jsi::Value getLocal(jsi::String &key);
void executeMultiple(std::string sql);

private:
bool initialized_;
Expand All @@ -53,7 +54,6 @@ class Database : public jsi::HostObject {
void executeUpdate(std::string sql);
void getRow(sqlite3_stmt *stmt);
bool getNextRowOrTrue(sqlite3_stmt *stmt);
void executeMultiple(std::string sql);
jsi::Object resultDictionary(sqlite3_stmt *statement);
jsi::Array resultArray(sqlite3_stmt *statement);
jsi::Array resultColumns(sqlite3_stmt *statement);
Expand Down
6 changes: 6 additions & 0 deletions native/shared/DatabaseInstallation.cpp
Expand Up @@ -244,6 +244,12 @@ void Database::install(jsi::Runtime *runtime) {
auto postamble = args[3].getString(rt).utf8(rt);
return database->unsafeLoadFromSync(jsonId, schema, preamble, postamble);
});
createMethod(rt, adapter, "unsafeExecuteMultiple", 1, [database](jsi::Runtime &rt, const jsi::Value *args) {
assert(database->initialized_);
auto sqlString = args[0].getString(rt).utf8(rt);
database->executeMultiple(sqlString);
return jsi::Value::undefined();
});
createMethod(rt, adapter, "unsafeResetDatabase", 2, [database](jsi::Runtime &rt, const jsi::Value *args) {
assert(database->initialized_);
jsi::String schema = args[0].getString(rt);
Expand Down
14 changes: 9 additions & 5 deletions src/adapters/sqlite/index.js
Expand Up @@ -347,13 +347,17 @@ export default class SQLiteAdapter implements DatabaseAdapter {
operations &&
typeof operations === 'object' &&
Object.keys(operations).length === 1 &&
Array.isArray(operations.sqls),
'unsafeExecute expects an { sqls: [ [sql, [args..]], ... ] } object',
(Array.isArray(operations.sqls) || typeof operations.sqlString === 'string'),
"unsafeExecute expects an { sqls: [ [sql, [args..]], ... ] } or { sqlString: 'foo; bar' } object",
)
}
const queries: SQLiteQuery[] = (operations: any).sqls
const batchOperations = queries.map(([sql, args]) => [IGNORE_CACHE, null, sql, [args]])
this._dispatcher.call('batch', [batchOperations], callback)
if (operations.sqls) {
const queries: SQLiteQuery[] = (operations: any).sqls
const batchOperations = queries.map(([sql, args]) => [IGNORE_CACHE, null, sql, [args]])
this._dispatcher.call('batch', [batchOperations], callback)
} else if (operations.sqlString) {
this._dispatcher.call('unsafeExecuteMultiple', [operations.sqlString], callback)
}
}

getLocal(key: string, callback: ResultCallback<?string>): void {
Expand Down
1 change: 1 addition & 0 deletions src/adapters/sqlite/type.js
Expand Up @@ -72,6 +72,7 @@ export type SqliteDispatcherMethod =
| 'provideSyncJson'
| 'unsafeResetDatabase'
| 'getLocal'
| 'unsafeExecuteMultiple'

export interface SqliteDispatcher {
call(methodName: SqliteDispatcherMethod, args: any[], callback: ResultCallback<any>): void;
Expand Down
3 changes: 2 additions & 1 deletion src/adapters/type.js
Expand Up @@ -7,7 +7,7 @@ import type { RecordId } from '../Model'
import type { RawRecord } from '../RawRecord'
import type { ResultCallback } from '../utils/fp/Result'

import type { SQLiteQuery } from './sqlite/type'
import type { SQLiteQuery, SQL } from './sqlite/type'
import type { Loki } from './lokijs/type'

export type CachedFindResult = RecordId | ?RawRecord
Expand All @@ -21,6 +21,7 @@ export type BatchOperation =

export type UnsafeExecuteOperations =
| $Exact<{ sqls: SQLiteQuery[] }>
| $Exact<{ sqlString: SQL }> // JSI-only
| $Exact<{ loki: (Loki) => void }>

export interface DatabaseAdapter {
Expand Down

0 comments on commit c854711

Please sign in to comment.