feat: isolate student SQL output and add result-row parsing
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
import { ref } from "vue"
|
||||
import { buildSetupSql, defaultSqlTableId, sqlTables } from "../data/sqlTables"
|
||||
import {
|
||||
buildSetupSql,
|
||||
defaultSqlTableId,
|
||||
sqlTables,
|
||||
type SqlColumn,
|
||||
} from "../data/sqlTables"
|
||||
|
||||
export const selectedTableId = ref(defaultSqlTableId)
|
||||
|
||||
@@ -12,5 +17,27 @@ export function buildSqlScript(studentSql: string) {
|
||||
sqlTables.find((item) => item.id === selectedTableId.value) ??
|
||||
sqlTables[0]
|
||||
const normalizedSql = studentSql.trim().replace(/;?\s*$/, ";")
|
||||
return `${buildSetupSql(table)}\n\n${normalizedSql}\n\nSELECT * FROM ${table.tableName};`
|
||||
return [
|
||||
buildSetupSql(table),
|
||||
".output /dev/null",
|
||||
normalizedSql,
|
||||
".output stdout",
|
||||
`SELECT * FROM ${table.tableName};`,
|
||||
].join("\n\n")
|
||||
}
|
||||
|
||||
export function parseResultRows(
|
||||
output: string,
|
||||
columns: SqlColumn[],
|
||||
): Record<string, string | number>[] {
|
||||
return output
|
||||
.split("\n")
|
||||
.map((line) => line.trim())
|
||||
.filter(Boolean)
|
||||
.map((line) => {
|
||||
const cells = line.split("|")
|
||||
return Object.fromEntries(
|
||||
columns.map((column, index) => [column.name, cells[index] ?? ""]),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import { computed, watch } from "vue"
|
||||
import CodeEditor from "../components/CodeEditor.vue"
|
||||
import { output } from "../composables/code"
|
||||
import { selectedTableId } from "../composables/sqlTable"
|
||||
import { sqlTables } from "../data/sqlTables"
|
||||
import { buildSetupSql, sqlTables } from "../data/sqlTables"
|
||||
import OutputSection from "./OutputSection.vue"
|
||||
|
||||
const selectedTable = computed(
|
||||
@@ -21,7 +21,7 @@ watch(selectedTableId, () => {
|
||||
<n-split direction="vertical" :default-size="1 / 3" :min="1 / 5" :max="3 / 5">
|
||||
<template #1>
|
||||
<CodeEditor
|
||||
:model-value="selectedTable.setupSql"
|
||||
:model-value="buildSetupSql(selectedTable)"
|
||||
language="sql"
|
||||
readonly
|
||||
label="表结构与初始数据"
|
||||
|
||||
Reference in New Issue
Block a user