Skip to content

Commit

Permalink
[sqlite] more sqlite next api fix (expo#25637)
Browse files Browse the repository at this point in the history
# Why

fix some problems finding from dogfooding and expo#25618

# How

- fix the `:memory:` db being accidentally created as a file.
- fix utf8 issue on ios
- fix incorrect test case from expo#25623

# Test Plan

- check that `:memory:` file should not be created
- add a test case for utf8
  • Loading branch information
Kudo authored Nov 29, 2023
1 parent 146a040 commit 318d7f7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
20 changes: 18 additions & 2 deletions apps/test-suite/tests/SQLiteNext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ CREATE TABLE IF NOT EXISTS SomeTable (id INTEGER PRIMARY KEY NOT NULL, name VARC

await db.closeAsync();
});

it('should support utf-8', async () => {
const db = await SQLite.openDatabaseAsync(':memory:');
await db.execAsync(
'CREATE TABLE translations (id INTEGER PRIMARY KEY NOT NULL, key TEXT, value TEXT);'
);
const statement = await db.prepareAsync(
'INSERT INTO translations (key, value) VALUES (?, ?)'
);
await statement.runAsync('hello', '哈囉');
await statement.finalizeAsync();

const result = await db.getAsync<any>('SELECT * FROM translations');
expect(result.key).toBe('hello');
expect(result.value).toBe('哈囉');
});
});

describe('File system tests', () => {
Expand Down Expand Up @@ -280,15 +296,15 @@ DROP TABLE IF EXISTS Users;
CREATE TABLE IF NOT EXISTS Users (user_id INTEGER PRIMARY KEY NOT NULL, name VARCHAR(64));
`);

const statement = await db.prepareAsync('INSERT INTO Nulling (x, y) VALUES (?, ?)');
const statement = await db.prepareAsync('INSERT INTO Users (user_id, name) VALUES (?, ?)');
await statement.finalizeAsync();
let error = null;
try {
await statement.runAsync(null, null);
} catch (e) {
error = e;
}
expect(error).not.toBeNull();
expect(error.toString()).toMatch(/Access to closed resource/);
});
});

Expand Down
1 change: 1 addition & 0 deletions packages/expo-sqlite/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### 🐛 Bug fixes

- Fixed `expo-sqlite/next` crashes when access to finalized statements. ([#25623](https://github.com/expo/expo/pull/25623) by [@kudo](https://github.com/kudo))
- Fixed `expo-sqlite/next` UTF-8 text issue and `:memory:` database issue. ([#25637](https://github.com/expo/expo/pull/25637) by [@kudo](https://github.com/kudo))

### 💡 Others

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import expo.modules.kotlin.modules.ModuleDefinition
import java.io.File
import java.io.IOException

private const val MEMORY_DB_NAME = ":memory:"

@Suppress("unused")
class SQLiteModuleNext : Module() {
private val cachedDatabases: MutableList<NativeDatabase> = mutableListOf()
Expand Down Expand Up @@ -179,6 +181,9 @@ class SQLiteModuleNext : Module() {

@Throws(OpenDatabaseException::class)
private fun pathForDatabaseName(name: String): String {
if (name == MEMORY_DB_NAME) {
return name
}
try {
val directory = File("${context.filesDir}${File.separator}SQLite")
ensureDirExists(directory)
Expand Down Expand Up @@ -404,6 +409,9 @@ class SQLiteModuleNext : Module() {
throw DeleteDatabaseException(dbName)
}

if (dbName == MEMORY_DB_NAME) {
return
}
val dbFile = File(pathForDatabaseName(dbName))
if (!dbFile.exists()) {
throw DatabaseNotFoundException(dbName)
Expand Down
9 changes: 8 additions & 1 deletion packages/expo-sqlite/ios/SQLiteModuleNext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import sqlite3
private typealias ColumnNames = [String]
private typealias ColumnValues = [Any]
private let SQLITE_TRANSIENT = unsafeBitCast(OpaquePointer(bitPattern: -1), to: sqlite3_destructor_type.self)
private let MEMORY_DB_NAME = ":memory:"

public final class SQLiteModuleNext: Module {
// Store unmanaged (SQLiteModuleNext, Database) pairs for sqlite callbacks,
Expand Down Expand Up @@ -179,6 +180,9 @@ public final class SQLiteModuleNext: Module {
}

private func pathForDatabaseName(name: String) -> URL? {
if name == MEMORY_DB_NAME {
return URL(string: name)
}
guard let fileSystem = appContext?.fileSystem else {
return nil
}
Expand Down Expand Up @@ -391,6 +395,9 @@ public final class SQLiteModuleNext: Module {
throw DeleteDatabaseException(dbName)
}

if dbName == MEMORY_DB_NAME {
return
}
guard let path = pathForDatabaseName(name: dbName) else {
throw Exceptions.FileSystemModuleNotFound()
}
Expand Down Expand Up @@ -492,7 +499,7 @@ public final class SQLiteModuleNext: Module {
case let param as Double:
sqlite3_bind_double(instance, index, param)
case let param as String:
sqlite3_bind_text(instance, index, param, Int32(param.count), SQLITE_TRANSIENT)
sqlite3_bind_text(instance, index, param, -1, SQLITE_TRANSIENT)
case let param as Data:
_ = param.withUnsafeBytes {
sqlite3_bind_blob(instance, index, $0.baseAddress, Int32(param.count), SQLITE_TRANSIENT)
Expand Down

0 comments on commit 318d7f7

Please sign in to comment.