Skip to content

Commit

Permalink
Merge pull request #36 from JacobLinCool/row-counts
Browse files Browse the repository at this point in the history
Row counts and Import
  • Loading branch information
JacobLinCool authored Jan 27, 2024
2 parents 703989e + e314d6b commit 4337252
Show file tree
Hide file tree
Showing 14 changed files with 243 additions and 97 deletions.
5 changes: 3 additions & 2 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"d1-manager-manage-db": "Use D1 Manager to mange data in {db}",
"download": "Download",
"execute-sql-query-in-database": "Execute SQL query in database {db}",
"import": "Import",
"lang": {
"en": "English",
"es-ES": "Spanish from Spain",
Expand Down Expand Up @@ -46,7 +47,7 @@
},
"run-query": {
"export": "Export",
"n-ms-m-changes": "{n} ms, {m} changes",
"n-ms-m-changes": "{n} ms, {rr} rows read, {rw} rows written",
"name": "Run SQL Query",
"no-result": "No result",
"no-results": "No Results",
Expand All @@ -56,7 +57,7 @@
"semantic-query": {
"autorun-on-read-only-queries": "Readonly query will be run automatically.",
"export": "Export",
"n-ms-m-changes": "{n} ms, {m} changes",
"n-ms-m-changes": "{n} ms, {rr} rows read, {rw} rows written",
"name": "Semantic Query",
"no-result": "No result",
"no-results": "No Results",
Expand Down
5 changes: 3 additions & 2 deletions locales/es-ES.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"d1-manager-manage-db": "Usa D1 Manager para gestionar datos en {db}",
"download": "Descargar",
"execute-sql-query-in-database": "Ejecutar consulta SQL en la base de datos {db}",
"import": "Importar",
"lang": {
"en": "Inglés",
"es-ES": "Español de españa",
Expand Down Expand Up @@ -46,7 +47,7 @@
},
"run-query": {
"export": "Exportar",
"n-ms-m-changes": "{n} ms, {m} cambios",
"n-ms-m-changes": "{n} ms, {rr} filas leídas, {rw} filas escritas",
"name": "Ejecutar Consulta SQL",
"no-result": "Sin resultado",
"no-results": "Sin resultados",
Expand All @@ -56,7 +57,7 @@
"semantic-query": {
"autorun-on-read-only-queries": "La consulta de solo lectura se ejecutará automáticamente.",
"export": "Exportar",
"n-ms-m-changes": "{n} ms, {m} cambios",
"n-ms-m-changes": "{n} ms, {rr} filas leídas, {rw} filas escritas",
"name": "Consulta Semántica",
"no-result": "Sin resultado",
"no-results": "Sin resultados",
Expand Down
5 changes: 3 additions & 2 deletions locales/es-MX.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"d1-manager-manage-db": "Usa D1 Manager para gestionar datos en {db}",
"download": "Descargar",
"execute-sql-query-in-database": "Ejecutar consulta SQL en la base de datos {db}",
"import": "Importar",
"lang": {
"en": "Inglés",
"es-ES": "Español de españa",
Expand Down Expand Up @@ -46,7 +47,7 @@
},
"run-query": {
"export": "Exportar",
"n-ms-m-changes": "{n} ms, {m} cambios",
"n-ms-m-changes": "{n} ms, {rr} filas leídas, {rw} filas escritas",
"name": "Ejecutar consulta SQL",
"no-result": "Sin resultado",
"no-results": "Sin resultados",
Expand All @@ -56,7 +57,7 @@
"semantic-query": {
"autorun-on-read-only-queries": "La consulta de solo lectura se ejecutará automáticamente.",
"export": "Exportar",
"n-ms-m-changes": "{n} ms, {m} cambios",
"n-ms-m-changes": "{n} ms, {rr} filas leídas, {rw} filas escritas",
"name": "Consulta semántica",
"no-result": "Sin resultado",
"no-results": "Sin resultados",
Expand Down
5 changes: 3 additions & 2 deletions locales/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"d1-manager-manage-db": "D1 Manager を使用して {db} 内のデータを管理",
"download": "ダウンロード",
"execute-sql-query-in-database": "データベース {db} で SQL クエリを実行します",
"import": "インポート",
"lang": {
"en": "英語",
"es-ES": "スペイン語",
Expand Down Expand Up @@ -46,7 +47,7 @@
},
"run-query": {
"export": "エクスポート",
"n-ms-m-changes": "{n} ミリ秒で、{m} 件のデータ変更",
"n-ms-m-changes": "{n} ミリ秒、{rr} 行読み取り、{rw} 行書き込み",
"name": "SQL クエリの実行",
"no-result": "結果にデータがありません",
"no-results": "一致する結果はありません",
Expand All @@ -56,7 +57,7 @@
"semantic-query": {
"autorun-on-read-only-queries": "読み取り専用のクエリは自動的に実行されます。",
"export": "エクスポート",
"n-ms-m-changes": "{n} ミリ秒で、{m} 件のデータ変更",
"n-ms-m-changes": "{n} ミリ秒、{rr} 行読み取り、{rw} 行書き込み",
"name": "セマンティッククエリ",
"no-result": "結果にデータがありません",
"no-results": "一致する結果はありません",
Expand Down
5 changes: 3 additions & 2 deletions locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"d1-manager-manage-db": "使用 D1 Manager 管理 {db} 中的资料",
"download": "下载",
"execute-sql-query-in-database": "在数据库 {db} 中执行 SQL 查询",
"import": "汇入",
"lang": {
"en": "英语",
"es-ES": "西班牙语",
Expand Down Expand Up @@ -46,7 +47,7 @@
},
"run-query": {
"export": "导出",
"n-ms-m-changes": "花费 {n} 毫秒,{m} 笔资料更动",
"n-ms-m-changes": "{n} 毫秒,读取 {rr} 行,写入 {rw} 行",
"name": "运行 SQL 查询",
"no-result": "没有结果",
"no-results": "没有结果",
Expand All @@ -56,7 +57,7 @@
"semantic-query": {
"autorun-on-read-only-queries": "只读查询将自动运行。",
"export": "导出",
"n-ms-m-changes": "花费 {n} 毫秒,{m} 笔资料更动",
"n-ms-m-changes": "{n} 毫秒,读取 {rr} 行,写入 {rw} 行",
"name": "语义查询",
"no-result": "没有结果",
"no-results": "没有结果",
Expand Down
5 changes: 3 additions & 2 deletions locales/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"d1-manager-manage-db": "使用 D1 Manager 管理 {db} 中的資料",
"download": "下載",
"execute-sql-query-in-database": "在資料庫 {db} 中執行 SQL 查詢",
"import": "匯入",
"lang": {
"en": "英語",
"es-ES": "西班牙語",
Expand Down Expand Up @@ -46,7 +47,7 @@
},
"run-query": {
"export": "匯出",
"n-ms-m-changes": "花費 {n} 毫秒,{m} 筆資料更動",
"n-ms-m-changes": "{n} 毫秒,讀取 {rr} 列,寫入 {rw} 列",
"name": "執行 SQL 查詢",
"no-result": "回應中沒有資料",
"no-results": "沒有符合的結果",
Expand All @@ -56,7 +57,7 @@
"semantic-query": {
"autorun-on-read-only-queries": "只讀查詢將自動執行。",
"export": "匯出",
"n-ms-m-changes": "花費 {n} 毫秒,{m} 筆資料更動",
"n-ms-m-changes": "{n} 毫秒,讀取 {rr} 列,寫入 {rw} 列",
"name": "語意查詢",
"no-result": "回應中沒有資料",
"no-results": "沒有符合的結果",
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@sveltejs/vite-plugin-svelte": "^3.0.1",
"@tailwindcss/typography": "^0.5.10",
"@types/debug": "^4.1.12",
"@types/sql.js": "^1.4.9",
"@typescript-eslint/eslint-plugin": "^6.19.1",
"@typescript-eslint/parser": "^6.19.1",
"autoprefixer": "^10.4.17",
Expand All @@ -51,6 +52,7 @@
"prettier-plugin-organize-imports": "^3.2.4",
"prettier-plugin-svelte": "^3.1.2",
"prettier-plugin-tailwindcss": "^0.5.11",
"sql.js": "^1.10.2",
"svelte": "^4.2.9",
"svelte-check": "^3.6.3",
"svelte-i18n": "^4.0.0",
Expand Down
30 changes: 30 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ export const handle: Handle = async ({ event, resolve }) => {
};

const elog = extend("server-error");
elog.enabled = true;

export const handleError: HandleServerError = async ({ error }) => {
elog(error);

if (error instanceof Error && error.message.startsWith("D1_")) {
return {
code: 400,
message: error.message,
};
}

return {
code: 500,
message: "Internal Server Error",
Expand Down
55 changes: 17 additions & 38 deletions src/lib/plugin/RunQuery.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,8 @@
$: danger = is_dangerous(query);
let running = false;
let result:
| {
results: Record<string, unknown>[];
success: boolean;
meta: {
duration: number;
last_row_id: number;
changes: number;
served_by: string;
internal_stats: null;
};
}
| undefined;
let error:
| {
error: {
message: string;
cause?: string;
};
}
| undefined;
let result: D1Result<any> | undefined;
let error: string | undefined;
async function run() {
if (running) {
Expand All @@ -44,26 +25,23 @@
body: JSON.stringify({ query }),
});
const json = await res.json<typeof result | typeof error>();
const json = await res.json<D1Result | { message: string }>();
if (json) {
if ("error" in json) {
error = json;
result = undefined;
} else {
result = json;
error = undefined;
if ("message" in json) {
throw new Error(json.message);
}
result = json;
error = undefined;
} else {
throw new Error($t("plugin.run-query.no-result"));
}
} catch (err) {
error = {
error: {
message:
err instanceof Error ? err.message : $t("plugin.run-query.unknown-error"),
},
};
result = undefined;
if (err instanceof Error) {
error = err.message;
} else {
error = $t("plugin.run-query.unknown-error");
}
} finally {
running = false;
setTimeout(() => {
Expand Down Expand Up @@ -95,7 +73,7 @@
{#if result}
<div class="divider" />

{#if result.results.length}
{#if result?.results?.length}
<div class="max-h-[80vh] overflow-auto">
<table class="table-sm table w-full">
<thead>
Expand Down Expand Up @@ -129,11 +107,12 @@
{$t("plugin.run-query.n-ms-m-changes", {
values: {
n: result.meta.duration.toFixed(2),
m: result.meta.changes,
rr: result.meta.rows_read ?? "x",
rw: result.meta.rows_written ?? result.meta.changes,
},
})}
</p>
{#if result?.results.length}
{#if result?.results?.length}
<button
class="btn-primary btn-outline btn-sm btn"
on:click={() => (result ? export_csv(result.results, table) : undefined)}
Expand All @@ -148,7 +127,7 @@
<div class="divider" />

<div class="alert alert-error shadow-lg">
<div>{error.error.cause || error.error.message}</div>
<div>{error}</div>
</div>
{/if}

Expand Down
Loading

0 comments on commit 4337252

Please sign in to comment.