Skip to content

Commit

Permalink
deal error case
Browse files Browse the repository at this point in the history
  • Loading branch information
GreyRaphael committed Dec 15, 2023
1 parent 7b473a6 commit 03f5b57
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ edition = "2021"
tauri-build = { version = "1.5", features = [] }

[dependencies]
tauri = { version = "1.5", features = [ "dialog-open"] }
tauri = { version = "1.5", features = [ "dialog-open", "dialog-message"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
polars = { version = "0.35.4", features = ["parquet", "dtype-u8", "sql", "lazy", "ipc"] }
Expand Down
25 changes: 19 additions & 6 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,35 @@ fn generate_table(df: &DataFrame) -> String {
serde_json::to_string(&table).unwrap()
}

fn deal_error(e: PolarsError) -> String {
serde_json::json!({
"err_msg":e.to_string(),
})
.to_string()
}

#[tauri::command]
fn read_parquet_file(filename: &str, sql: &str) -> String {
let df = read_parquet(filename, sql).unwrap();
generate_table(&df)
match read_parquet(filename, sql) {
Ok(df) => generate_table(&df),
Err(e) => deal_error(e),
}
}

#[tauri::command]
fn read_ipc_file(filename: &str, sql: &str) -> String {
let df = read_ipc(filename, sql).unwrap();
generate_table(&df)
match read_ipc(filename, sql) {
Ok(df) => generate_table(&df),
Err(e) => deal_error(e),
}
}

#[tauri::command]
fn read_csv_file(filename: &str, sql: &str, sep: u8) -> String {
let df = read_csv(filename, sql, sep).unwrap();
generate_table(&df)
match read_csv(filename, sql, sep) {
Ok(df) => generate_table(&df),
Err(e) => deal_error(e),
}
}

fn main() {
Expand Down
5 changes: 3 additions & 2 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"tauri": {
"allowlist": {
"dialog": {
"open": true
"open": true,
"message": true
}
},
"bundle": {
Expand All @@ -37,4 +38,4 @@
}
]
}
}
}
44 changes: 25 additions & 19 deletions src/components/datatable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
</div>
<div id="query_container">
<n-button @click="execute_sql" :strong="true"> Execute </n-button>
<n-input v-model:value="sql" type="text" clearable style="width: 1000px;" />
<n-tooltip placement="bottom" trigger="hover">
<template #trigger>
<n-input v-model:value="sql" type="text" clearable style="width: 1000px;"
placeholder="select * from LAST where idx>10 order by idx desc offset 0 limit 100" />
</template>
<span>Not support field with "-"</span>
</n-tooltip>
</div>
</div>
<div id="table_container">
Expand All @@ -25,13 +31,13 @@
</template>

<script setup lang="ts">
import { NButton, NDataTable, NInput, NDropdown, NSelect, NModal } from 'naive-ui'
import { NButton, NDataTable, NInput, NDropdown, NSelect, NModal, NTooltip } from 'naive-ui'
import { invoke } from "@tauri-apps/api/tauri";
import { ref } from 'vue';
import { open } from '@tauri-apps/api/dialog';
import { open, message } from '@tauri-apps/api/dialog';
// parameters
const sql = ref("select * from LAST offset 0 limit 100");
const sql = ref("");
const last_filename = ref("");
// table headers & body
const tb_cols = ref(0);
Expand All @@ -54,30 +60,29 @@ const filetype_options = [
}
]
async function set_ui(j_str: string) {
let table = JSON.parse(j_str);
if (table.hasOwnProperty("err_msg")) {
message(table["err_msg"], { title: 'sql query error', type: 'error' });
} else {
tb_headers.value = table["headers"];
tb_body.value = table["body"];
tb_rows.value = table["row_count"];
tb_cols.value = table["col_count"];
}
}
async function read_parquet_file(filename: string, sql: string) {
let result: string = await invoke("read_parquet_file", { filename: filename, sql: sql });
let table = JSON.parse(result);
tb_headers.value = table["headers"];
tb_body.value = table["body"];
tb_rows.value = table["row_count"];
tb_cols.value = table["col_count"];
set_ui(result);
}
async function read_ipc_file(filename: string, sql: string) {
let result: string = await invoke("read_ipc_file", { filename: filename, sql: sql });
let table = JSON.parse(result);
tb_headers.value = table["headers"];
tb_body.value = table["body"];
tb_rows.value = table["row_count"];
tb_cols.value = table["col_count"];
set_ui(result);
}
async function read_csv_file(filename: string, sql: string, sep: number) {
let result: string = await invoke("read_csv_file", { filename: filename, sql: sql, sep: sep });
let table = JSON.parse(result);
tb_headers.value = table["headers"];
tb_body.value = table["body"];
tb_rows.value = table["row_count"];
tb_cols.value = table["col_count"];
set_ui(result);
}
async function execute_sql() {
Expand All @@ -93,6 +98,7 @@ async function execute_sql() {
}
async function choose_filetype(key: string) {
sql.value = "select * from LAST offset 0 limit 100";
const selected = await open({
multiple: false,
filters: [{
Expand Down

0 comments on commit 03f5b57

Please sign in to comment.