mirror of
https://github.com/kunkunsh/kunkun.git
synced 2025-04-20 05:29:17 +00:00
fix: sqlite select command
This commit is contained in:
parent
3271507d0c
commit
382ceb120f
@ -1,4 +1,5 @@
|
||||
import * as schema from "@kksh/drizzle/schema"
|
||||
import * as dbCmd from "@kunkunapi/src/commands/db"
|
||||
import Database from "@tauri-apps/plugin-sql"
|
||||
import { drizzle } from "drizzle-orm/sqlite-proxy"
|
||||
|
||||
@ -14,28 +15,27 @@ export type SelectQueryResult = {
|
||||
*/
|
||||
// export const sqlite = await Database.load("sqlite:test.db");
|
||||
|
||||
export async function getDb() {
|
||||
return await Database.load("sqlite:test.db")
|
||||
}
|
||||
|
||||
/**
|
||||
* The drizzle database instance.
|
||||
*/
|
||||
export const db = drizzle<typeof schema>(
|
||||
async (sql, params, method) => {
|
||||
const sqlite = await getDb()
|
||||
let rows: any = []
|
||||
let results = []
|
||||
|
||||
console.log({
|
||||
sql,
|
||||
params,
|
||||
method
|
||||
})
|
||||
// If the query is a SELECT, use the select method
|
||||
if (isSelectQuery(sql)) {
|
||||
rows = await sqlite.select(sql, params).catch((e) => {
|
||||
rows = await dbCmd.select(sql, params).catch((e) => {
|
||||
console.error("SQL Error:", e)
|
||||
return []
|
||||
})
|
||||
} else {
|
||||
// Otherwise, use the execute method
|
||||
rows = await sqlite.execute(sql, params).catch((e) => {
|
||||
rows = await dbCmd.execute(sql, params).catch((e) => {
|
||||
console.error("SQL Error:", e)
|
||||
return []
|
||||
})
|
||||
@ -48,7 +48,6 @@ export const db = drizzle<typeof schema>(
|
||||
|
||||
// If the method is "all", return all rows
|
||||
results = method === "all" ? rows : rows[0]
|
||||
await sqlite.close()
|
||||
return { rows: results }
|
||||
},
|
||||
// Pass the schema to the drizzle instance
|
||||
|
@ -5,6 +5,7 @@
|
||||
import { systemCommands, systemCommandsFiltered } from "@/cmds/system"
|
||||
import AppsCmds from "@/components/main/AppsCmds.svelte"
|
||||
import { i18n } from "@/i18n"
|
||||
import { db } from "@/orm/database"
|
||||
import * as m from "@/paraglide/messages"
|
||||
import {
|
||||
appConfig,
|
||||
@ -31,7 +32,6 @@
|
||||
SystemCmds
|
||||
} from "@kksh/ui/main"
|
||||
import { cn } from "@kksh/ui/utils"
|
||||
import * as db from "@kunkunapi/src/commands/db"
|
||||
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow"
|
||||
import { getCurrentWindow, Window } from "@tauri-apps/api/window"
|
||||
import { platform } from "@tauri-apps/plugin-os"
|
||||
@ -114,15 +114,21 @@
|
||||
|
||||
<Button
|
||||
onclick={() => {
|
||||
db.select("SELECT * FROM extensions;", []).then((res) => {
|
||||
console.log(res)
|
||||
})
|
||||
db.execute(
|
||||
"INSERT INTO extension_data (ext_id, data_type, data, search_text, metadata) VALUES (?, ?, ?, ?, ?);",
|
||||
[1, "Test", "Hello, world!", "Hello, world!", "{'metadata': 'test'}"]
|
||||
).then((res) => {
|
||||
console.log(res)
|
||||
})
|
||||
db.query.extensions
|
||||
.findMany()
|
||||
.execute()
|
||||
.then((res) => {
|
||||
console.log(res)
|
||||
})
|
||||
// db.select("SELECT * FROM extensions;", []).then((res) => {
|
||||
// console.log(res)
|
||||
// })
|
||||
// db.execute(
|
||||
// "INSERT INTO extension_data (ext_id, data_type, data, search_text, metadata) VALUES (?, ?, ?, ?, ?);",
|
||||
// [1, "Test", "Hello, world!", "Hello, world!", "{'metadata': 'test'}"]
|
||||
// ).then((res) => {
|
||||
// console.log(res)
|
||||
// })
|
||||
}}
|
||||
>
|
||||
Select
|
||||
|
@ -124,6 +124,8 @@ impl JarvisDB {
|
||||
}
|
||||
|
||||
pub fn select(&self, query: String, values: Vec<JsonValue>) -> Result<Vec<JsonValue>> {
|
||||
println!("DB selecting: {}", query);
|
||||
println!("DB selecting values: {:?}", values);
|
||||
let mut stmt = self.conn.prepare(&query)?;
|
||||
|
||||
// Convert JsonValue parameters to appropriate types for rusqlite
|
||||
@ -153,8 +155,8 @@ impl JarvisDB {
|
||||
|
||||
// Execute the query with the converted parameters and map results
|
||||
let rows = stmt.query_map(params_from_iter(params.iter().map(|p| p.as_ref())), |row| {
|
||||
let mut result = serde_json::Map::new();
|
||||
for (i, name) in column_names.iter().enumerate() {
|
||||
let mut result = Vec::new();
|
||||
for i in 0..column_names.len() {
|
||||
let value: Value = match row.get_ref(i)? {
|
||||
rusqlite::types::ValueRef::Null => Value::Null,
|
||||
rusqlite::types::ValueRef::Integer(i) => Value::Number(i.into()),
|
||||
@ -168,9 +170,9 @@ impl JarvisDB {
|
||||
Value::String(String::from_utf8_lossy(b).into_owned())
|
||||
}
|
||||
};
|
||||
result.insert(name.clone(), value);
|
||||
result.push(value);
|
||||
}
|
||||
Ok(Value::Object(result))
|
||||
Ok(Value::Array(result))
|
||||
})?;
|
||||
|
||||
let mut results = Vec::new();
|
||||
|
Loading…
x
Reference in New Issue
Block a user