fix some sqlite errors.

This commit is contained in:
Jordan
2025-02-13 07:48:32 -08:00
parent 52bb6f812c
commit 68cc052417
6 changed files with 23 additions and 60 deletions

View File

@ -7,12 +7,14 @@ describe('Settings', () => {
beforeEach(async () => {
// Initialize your Settings class here with a fresh database instance
settings = new Settings(await getDb());
const db = await getDb();
if (!db) throw new Error("Could not get db");
settings = new Settings(db);
});
afterEach(async () => {
// Clean up the database after each test
await settings.db.executeSql('DELETE FROM settings');
settings && await settings.db.executeSql('DELETE FROM settings');
});
describe('setHostLanguage', () => {
@ -57,7 +59,7 @@ describe('Settings', () => {
it('should return the LibreTranslate base URL from the database', async () => {
const value = 'https://another-example.com';
await settings.db.executeSql(
`INSERT INTO settings (libetransalte_base_url) VALUES (?)`,
`INSERT INTO settings (libretranslate_base_url) VALUES (?)`,
[value]
);

View File

@ -2,7 +2,7 @@ import * as SQLite from 'expo-sqlite';
export const MIGRATE_UP = {
1: [
`CREATE TABLE IF NOT EXIST settings (
`CREATE TABLE IF NOT EXISTS settings (
host_language TEXT,
libretranslate_base_url TEXT,
ui_direction INTEGER

View File

@ -19,7 +19,7 @@ export class Settings {
}
const query = `
SELECT ?
SELECT ${key}
FROM settings
LIMIT 1`
const result = await this.db.getFirstAsync(
@ -33,10 +33,10 @@ LIMIT 1`
if (!Settings.KEYS.includes(key)) {
throw new Error(`Invalid setting: '${key}'`)
}
const statement = `INSERT INTO settings (${key})
SELECT '?'
ON CONFLICT DO UPDATE SET ${key} = ?`
await this.db.runAsync(statement, [value, value]);
const statement = `REPLACE INTO settings ('${key}')
VALUES (?)`
const args = [value]
await this.db.runAsync(statement, args);
}
async setHostLanguage(value: string) {