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