feat: structure candidate SQL table data as columns/rows

- Replace literal setupSql strings with structured columns/rows model
- Add SqlColumn interface and updated SqlTablePreset with columns/rows arrays
- Implement toSqlLiteral() for value escaping and buildSetupSql() for SQL generation
- Update sqlTable.ts to call buildSetupSql() instead of accessing setupSql field
- Maintains data parity with existing tables (students, employees, products)

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-05 00:34:39 -06:00
parent 4abd5761a0
commit 9a01d0e972
2 changed files with 73 additions and 57 deletions

View File

@@ -1,5 +1,5 @@
import { ref } from "vue" import { ref } from "vue"
import { defaultSqlTableId, sqlTables } from "../data/sqlTables" import { buildSetupSql, defaultSqlTableId, sqlTables } from "../data/sqlTables"
export const selectedTableId = ref(defaultSqlTableId) export const selectedTableId = ref(defaultSqlTableId)
@@ -12,5 +12,5 @@ export function buildSqlScript(studentSql: string) {
sqlTables.find((item) => item.id === selectedTableId.value) ?? sqlTables.find((item) => item.id === selectedTableId.value) ??
sqlTables[0] sqlTables[0]
const normalizedSql = studentSql.trim().replace(/;?\s*$/, ";") const normalizedSql = studentSql.trim().replace(/;?\s*$/, ";")
return `${table.setupSql}\n\n${normalizedSql}\n\nSELECT * FROM ${table.tableName};` return `${buildSetupSql(table)}\n\n${normalizedSql}\n\nSELECT * FROM ${table.tableName};`
} }

View File

@@ -1,81 +1,97 @@
export interface SqlColumn {
name: string
type: string
}
export interface SqlTablePreset { export interface SqlTablePreset {
id: string id: string
label: string label: string
tableName: string tableName: string
setupSql: string columns: SqlColumn[]
rows: (string | number)[][]
} }
const studentsSetupSql = `CREATE TABLE students ( export function toSqlLiteral(value: string | number): string {
id INTEGER PRIMARY KEY, if (typeof value === "number") return String(value)
name TEXT NOT NULL, return `'${value.replace(/'/g, "''")}'`
class TEXT NOT NULL, }
score INTEGER NOT NULL
);
INSERT INTO students (id, name, class, score) VALUES export function buildSetupSql(table: SqlTablePreset): string {
(1, '张伟', '一班', 92), const columnDefs = table.columns
(2, '王芳', '一班', 58), .map((column) => `${column.name} ${column.type}`)
(3, '李娜', '二班', 76), .join(",\n ")
(4, '刘洋', '二班', 45), const columnNames = table.columns.map((column) => column.name).join(", ")
(5, '陈静', '一班', 88), const valuesList = table.rows
(6, '杨帆', '三班', 63), .map((row) => `(${row.map(toSqlLiteral).join(", ")})`)
(7, '赵敏', '二班', 39), .join(",\n ")
(8, '孙涛', '三班', 81);` return `CREATE TABLE ${table.tableName} (\n ${columnDefs}\n);\n\nINSERT INTO ${table.tableName} (${columnNames}) VALUES\n ${valuesList};`
}
const employeesSetupSql = `CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
department TEXT NOT NULL,
salary INTEGER NOT NULL,
hire_date TEXT NOT NULL
);
INSERT INTO employees (id, name, department, salary, hire_date) VALUES
(1, '周明', '技术部', 12000, '2019-03-01'),
(2, '吴倩', '市场部', 8000, '2021-07-15'),
(3, '郑凯', '技术部', 15500, '2017-11-20'),
(4, '钱多多', '财务部', 9200, '2020-01-10'),
(5, '孙丽', '市场部', 7600, '2022-05-30'),
(6, '李强', '技术部', 10800, '2023-09-01'),
(7, '林小雨', '财务部', 8900, '2018-06-12'),
(8, '黄河', '市场部', 6800, '2024-02-18');`
const productsSetupSql = `CREATE TABLE products (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
category TEXT NOT NULL,
price REAL NOT NULL,
stock INTEGER NOT NULL
);
INSERT INTO products (id, name, category, price, stock) VALUES
(1, '无线鼠标', '电子产品', 59.9, 120),
(2, '机械键盘', '电子产品', 299.0, 45),
(3, '保温杯', '生活用品', 39.5, 0),
(4, '笔记本', '文具', 12.0, 300),
(5, '蓝牙耳机', '电子产品', 199.0, 0),
(6, '台灯', '生活用品', 89.0, 60),
(7, '钢笔', '文具', 25.0, 150),
(8, '充电宝', '电子产品', 129.0, 8);`
export const sqlTables: SqlTablePreset[] = [ export const sqlTables: SqlTablePreset[] = [
{ {
id: "students", id: "students",
label: "学生成绩表", label: "学生成绩表",
tableName: "students", tableName: "students",
setupSql: studentsSetupSql, columns: [
{ name: "id", type: "INTEGER PRIMARY KEY" },
{ name: "name", type: "TEXT NOT NULL" },
{ name: "class", type: "TEXT NOT NULL" },
{ name: "score", type: "INTEGER NOT NULL" },
],
rows: [
[1, "张伟", "一班", 92],
[2, "王芳", "一班", 58],
[3, "李娜", "二班", 76],
[4, "刘洋", "二班", 45],
[5, "陈静", "一班", 88],
[6, "杨帆", "三班", 63],
[7, "赵敏", "二班", 39],
[8, "孙涛", "三班", 81],
],
}, },
{ {
id: "employees", id: "employees",
label: "员工工资表", label: "员工工资表",
tableName: "employees", tableName: "employees",
setupSql: employeesSetupSql, columns: [
{ name: "id", type: "INTEGER PRIMARY KEY" },
{ name: "name", type: "TEXT NOT NULL" },
{ name: "department", type: "TEXT NOT NULL" },
{ name: "salary", type: "INTEGER NOT NULL" },
{ name: "hire_date", type: "TEXT NOT NULL" },
],
rows: [
[1, "周明", "技术部", 12000, "2019-03-01"],
[2, "吴倩", "市场部", 8000, "2021-07-15"],
[3, "郑凯", "技术部", 15500, "2017-11-20"],
[4, "钱多多", "财务部", 9200, "2020-01-10"],
[5, "孙丽", "市场部", 7600, "2022-05-30"],
[6, "李强", "技术部", 10800, "2023-09-01"],
[7, "林小雨", "财务部", 8900, "2018-06-12"],
[8, "黄河", "市场部", 6800, "2024-02-18"],
],
}, },
{ {
id: "products", id: "products",
label: "商品库存表", label: "商品库存表",
tableName: "products", tableName: "products",
setupSql: productsSetupSql, columns: [
{ name: "id", type: "INTEGER PRIMARY KEY" },
{ name: "name", type: "TEXT NOT NULL" },
{ name: "category", type: "TEXT NOT NULL" },
{ name: "price", type: "REAL NOT NULL" },
{ name: "stock", type: "INTEGER NOT NULL" },
],
rows: [
[1, "无线鼠标", "电子产品", 59.9, 120],
[2, "机械键盘", "电子产品", 299.0, 45],
[3, "保温杯", "生活用品", 39.5, 0],
[4, "笔记本", "文具", 12.0, 300],
[5, "蓝牙耳机", "电子产品", 199.0, 0],
[6, "台灯", "生活用品", 89.0, 60],
[7, "钢笔", "文具", 25.0, 150],
[8, "充电宝", "电子产品", 129.0, 8],
],
}, },
] ]