|
| 1 | +import indentString from 'indent-string' |
| 2 | +import { once } from 'lodash-es' |
| 3 | +import { action, makeAutoObservable, remove, runInAction } from 'mobx' |
| 4 | +import type { Column } from 'node-sql-parser' |
| 5 | +import nodeSqlParser from 'node-sql-parser' // https://vitejs.dev/guide/migration#ssr-externalized-modules-value-now-matches-production |
| 6 | +import type { ChangeEvent } from 'react' |
| 7 | + |
| 8 | +const getParser = once(() => { |
| 9 | + const { Parser } = nodeSqlParser |
| 10 | + return new Parser() |
| 11 | +}) |
| 12 | + |
| 13 | +export type Query = { |
| 14 | + name: string |
| 15 | + sql: string |
| 16 | +} |
| 17 | + |
| 18 | +export const DEFAULT_QUERY: Query = { |
| 19 | + name: 'my_query', |
| 20 | + sql: `select |
| 21 | + id, |
| 22 | + employees.name, |
| 23 | + ranking as my_rank |
| 24 | +from |
| 25 | + employees |
| 26 | +limit |
| 27 | + 2`, |
| 28 | +} |
| 29 | + |
| 30 | +export const state = makeAutoObservable( |
| 31 | + { |
| 32 | + queries: [DEFAULT_QUERY] as Query[], |
| 33 | + |
| 34 | + get parsed() { |
| 35 | + try { |
| 36 | + const jsonQuery = generateJsonQuery(this.queries) |
| 37 | + return jsonQuery |
| 38 | + } catch (err) { |
| 39 | + console.warn(err) |
| 40 | + return undefined |
| 41 | + } |
| 42 | + }, |
| 43 | + |
| 44 | + addQuery() { |
| 45 | + const newQuery = { ...DEFAULT_QUERY } |
| 46 | + newQuery.name += '_' + (this.queries.length + 1) |
| 47 | + this.queries.push(newQuery) |
| 48 | + }, |
| 49 | + }, |
| 50 | + {}, |
| 51 | + { |
| 52 | + autoBind: true, |
| 53 | + }, |
| 54 | +) |
| 55 | + |
| 56 | +// TODO: is there a way to define the action more |
| 57 | +// efficiently? |
| 58 | +export const formatQueryAction = (query: Query) => async () => { |
| 59 | + const { formatDialect, sqlite } = await import('sql-formatter') |
| 60 | + |
| 61 | + const sql = formatDialect(query.sql, { |
| 62 | + dialect: sqlite, |
| 63 | + tabWidth: 2, |
| 64 | + }) |
| 65 | + |
| 66 | + runInAction(() => { |
| 67 | + query.sql = sql |
| 68 | + }) |
| 69 | +} |
| 70 | + |
| 71 | +// TODO: is there a way to define the action more |
| 72 | +// efficiently? |
| 73 | +export const removeQueryAction = (index: number) => |
| 74 | + action(() => { |
| 75 | + remove(state.queries, index as any) |
| 76 | + }) |
| 77 | + |
| 78 | +export function bindMobxInput<T, K extends keyof T>(model: T, field: K) { |
| 79 | + return { |
| 80 | + value: model[field], |
| 81 | + onChange: action( |
| 82 | + (event: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => { |
| 83 | + ;(model as any)[field] = event.target.value |
| 84 | + }, |
| 85 | + ), |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +function getColumnNames(columns: Column[]) { |
| 90 | + const names: string[] = [] |
| 91 | + |
| 92 | + for (const column of columns) { |
| 93 | + if (column.as) { |
| 94 | + names.push(column.as) |
| 95 | + } else if (column.expr.type === 'column_ref') { |
| 96 | + names.push(column.expr.column) |
| 97 | + } else { |
| 98 | + console.warn('Cannot parse column def', column) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return names |
| 103 | +} |
| 104 | + |
| 105 | +export function parseOne(query: Query) { |
| 106 | + try { |
| 107 | + const parser = getParser() |
| 108 | + const ast = parser.astify(query.sql, { database: 'sqlite' }) |
| 109 | + console.log(ast) |
| 110 | + const first = Array.isArray(ast) ? ast[0] : ast |
| 111 | + if (!first) return undefined |
| 112 | + if (first.type !== 'select') return undefined |
| 113 | + const columns = first.columns |
| 114 | + if (!Array.isArray(columns)) return undefined |
| 115 | + const names = getColumnNames(columns) |
| 116 | + return names |
| 117 | + } catch (err) { |
| 118 | + console.warn(err) |
| 119 | + return undefined |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +function generateJsonQuery(queries: Query[]) { |
| 124 | + const parts: string[] = [] |
| 125 | + |
| 126 | + for (const query of queries) { |
| 127 | + const columns = parseOne(query) |
| 128 | + if (!columns || columns.length === 0) continue |
| 129 | + parts.push(` '${query.name}', |
| 130 | + ( |
| 131 | + select json_group_array( |
| 132 | + json_object(${columns.map((col) => `'${col}', ${col}`).join(', ')}) |
| 133 | + ) |
| 134 | + from ( |
| 135 | +${indentString(query.sql, 6)} |
| 136 | + ) |
| 137 | + )`) |
| 138 | + } |
| 139 | + |
| 140 | + const result = `select json_object( |
| 141 | +${parts.join(',\n')} |
| 142 | +) as json_result` |
| 143 | + |
| 144 | + return result |
| 145 | +} |
0 commit comments