Skip to content

Commit 8085e85

Browse files
committed
clean up
1 parent 13992c2 commit 8085e85

File tree

5 files changed

+23
-52
lines changed

5 files changed

+23
-52
lines changed

packages/admin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
"next-themes": "^0.3.0",
7979
"p-memoize": "^7.1.1",
8080
"pg-query-emscripten": "^5.1.0",
81-
"pgsql-ast-parser": "^12.0.1",
8281
"react": "^18.2.0",
8382
"react-dom": "^18.2.0",
8483
"react-hook-form": "^7.51.3",
@@ -100,6 +99,7 @@
10099
"autoprefixer": "^10.4.19",
101100
"eslint": "^8.57.0",
102101
"eslint-plugin-tailwindcss": "3.15.1",
102+
"pgsql-ast-parser": "^12.0.1",
103103
"postcss": "^8.4.38",
104104
"shadcn-ui": "0.8.0",
105105
"sql-autocomplete": "^1.1.1",

packages/admin/src/client/sql-codemirror.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export interface SqlCodeMirrorProps {
1414
code?: string
1515
onChange?: (query: string) => void
1616
onExecute?: (sql: string) => void
17-
errors?: Array<{position: number; message: string}>
17+
errors?: Array<{position: number; message: string | string[]}>
1818
height: string
1919
readonly?: boolean
2020
wrapText?: boolean
@@ -79,7 +79,7 @@ export const SqlCodeMirror = ({code, onChange, onExecute, errors, height, ...pro
7979
return {
8080
from,
8181
to,
82-
message: e.message.split('\n')[0] || unknownErrorMessage,
82+
message: [e.message].flat().join('\n').split('\n')[0] || unknownErrorMessage,
8383
severity: 'error',
8484
}
8585
})

packages/admin/src/client/views/Querier.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ export const Querier = () => {
2929
onSuccess: data => {
3030
const newErrors = data.results.flatMap(r => {
3131
if (r.error && typeof r.position === 'number') {
32-
return [{message: r.error.message, position: r.position + 1}]
32+
return [{message: r.error.message, position: r.position}]
3333
}
3434

3535
const parsed = PGErrorWrapper.safeParse(r.error?.cause)
3636
if (parsed.success) {
3737
const pgError = parsed.data.error
38-
return [{message: r.error?.message || pgError.code, position: Number(pgError.position) - 1}]
38+
return [{message: r.error?.message || pgError.code, position: Number(pgError.position)}]
3939
}
4040

4141
return []
@@ -76,7 +76,7 @@ export const Querier = () => {
7676
<div className="flex gap-1">
7777
<Button
7878
title="AI"
79-
disabled={aiMutation.isLoading}
79+
disabled={aiMutation.isPending}
8080
onClick={() => {
8181
const aiPrompt = prompt('Enter a prompt', aiMutation.variables?.prompt || '')
8282
if (!aiPrompt) return
@@ -94,7 +94,7 @@ export const Querier = () => {
9494
</Button>
9595
</div>
9696
</div>
97-
{aiMutation.isLoading && <FakeProgress value={aiMutation.isSuccess ? 100 : null} estimate={3000} />}
97+
{aiMutation.isPending && <FakeProgress value={aiMutation.isSuccess ? 100 : null} estimate={3000} />}
9898
<div className="flex flex-col gap-4 h-[90%] relative">
9999
<div ref={ref} className="h-1/2 border rounded-lg overflow-scroll relative bg-gray-800">
100100
<SqlCodeMirror

packages/admin/src/server/query.ts

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
import {FieldInfo, Queryable, nameQuery, sql} from '@pgkit/client'
2-
import PGQueryEmscripten, {PgQueryEmscriptenParseResult} from 'pg-query-emscripten'
2+
import PGQueryEmscripten from 'pg-query-emscripten'
33
import {type ServerContext} from './context.js'
44

5-
export type PgsqlParserParseResult = {
6-
RawStmt?: {
7-
stmt?: {}
8-
stmt_len?: number
9-
stmt_location?: number
10-
}
11-
}
12-
135
export const runQuery = async (query: string, {connection}: ServerContext): Promise<QueryResult[]> => {
14-
console.log('query', query)
156
if (query.startsWith('--split-semicolons\n')) {
167
const queries = query.split(/;\s*\n/)
178
const results = []
@@ -26,52 +17,32 @@ export const runQuery = async (query: string, {connection}: ServerContext): Prom
2617

2718
const results = [] as QueryResult[]
2819

29-
let nativeParsed: PgQueryEmscriptenParseResult
30-
try {
31-
const pgsqlParser = await new PGQueryEmscripten()
32-
nativeParsed = pgsqlParser.parse(query)
33-
if (nativeParsed.error) {
34-
const message = [
35-
nativeParsed.error.message,
36-
'',
37-
`If you think the query is actually valid, it's possible the parsing library has a bug.`,
38-
`Try adding --no-parse at the top of your query to disable statement-level query parsing and send it to the DB as-is.`,
39-
].join('\n')
40-
const err = new Error(message, {cause: nativeParsed.error})
41-
makeJsonable(err)
42-
// if (Math.random()) throw new Error(nativeParsed.error.message, {cause: nativeParsed.error})
43-
return [
44-
{
45-
query: nameQuery([query]),
46-
original: query,
47-
error: err,
48-
result: null,
49-
fields: null,
50-
position: nativeParsed.error.cursorpos - 1,
51-
},
52-
]
53-
}
54-
} catch (err) {
55-
makeJsonable(err)
56-
err.message = [
57-
err.message,
20+
const [parseError, nativeParsed] = await Promise.resolve()
21+
.then(() => new PGQueryEmscripten())
22+
.then(parser => [null, parser.parse(query)] as const)
23+
.catch((error: Error) => [error, null] as const)
24+
25+
if (parseError || nativeParsed.error) {
26+
const error = parseError || new Error(nativeParsed.error!.message, {cause: nativeParsed.error})
27+
error.message = [
28+
error.message,
5829
'',
5930
`If you think the query is actually valid, it's possible the parsing library has a bug.`,
6031
`Try adding --no-parse at the top of your query to disable statement-level query parsing and send it to the DB as-is.`,
6132
].join('\n')
33+
makeJsonable(error)
6234
return [
6335
{
6436
query: nameQuery([query]),
6537
original: query,
66-
error: err,
38+
error,
6739
result: null,
6840
fields: null,
69-
position: (err as {cursorPosition?: number})?.cursorPosition,
41+
position: nativeParsed?.error?.cursorpos,
7042
},
7143
]
7244
}
7345

74-
console.dir({nativeParsed}, {depth: null})
7546
const slices = nativeParsed.parse_tree.stmts.map(s => {
7647
// if (typeof s?.stmt_location !== 'number') return undefined
7748

pnpm-lock.yaml

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)