Skip to content

Commit 9c1ca41

Browse files
authored
fix(explorer): format default sql query (#3307)
1 parent facdaaf commit 9c1ca41

3 files changed

Lines changed: 380 additions & 1 deletion

File tree

.changeset/perfect-zebras-rest.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@latticexyz/explorer": patch
3+
---
4+
5+
When accessing a new table in Explore tab, the SQL editor now encloses column names that are also PostgreSQL keywords in double quotes in order to prevent invalid queries.

packages/explorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/explore/Explorer.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { indexerForChainId } from "../../../../utils/indexerForChainId";
1212
import { SQLEditor } from "./SQLEditor";
1313
import { TableSelector } from "./TableSelector";
1414
import { TablesViewer } from "./TablesViewer";
15+
import { postgresKeywords } from "./consts";
1516

1617
export function Explorer() {
1718
const { worldAddress } = useParams();
@@ -31,7 +32,10 @@ export function Explorer() {
3132
if (indexer.type === "sqlite") {
3233
setQuery(`SELECT * FROM "${tableName}"`);
3334
} else {
34-
setQuery(`SELECT ${Object.keys(table.schema).join(", ")} FROM ${tableName}`);
35+
const columns = Object.keys(table.schema).map((column) =>
36+
postgresKeywords.includes(column.toLowerCase()) ? `"${column}"` : column,
37+
);
38+
setQuery(`SELECT ${columns.join(", ")} FROM ${tableName}`);
3539
}
3640
}
3741
}, [chainId, setQuery, selectedTableId, table, worldAddress, prevSelectedTableId, query, indexer.type]);

packages/explorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/explore/consts.ts

Lines changed: 370 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,373 @@ export const monacoOptions: editor.IStandaloneEditorConstructionOptions = {
4141
automaticLayout: true,
4242
fixedOverflowWidgets: true,
4343
};
44+
45+
// Ref: https://www.postgresql.org/docs/8.1/sql-keywords-appendix.html
46+
export const postgresKeywords = [
47+
"a",
48+
"abort",
49+
"absolute",
50+
"access",
51+
"action",
52+
"add",
53+
"admin",
54+
"after",
55+
"aggregate",
56+
"all",
57+
"also",
58+
"alter",
59+
"always",
60+
"analyse",
61+
"analyze",
62+
"and",
63+
"any",
64+
"array",
65+
"as",
66+
"asc",
67+
"assertion",
68+
"assignment",
69+
"asymmetric",
70+
"at",
71+
"authorization",
72+
"backward",
73+
"before",
74+
"begin",
75+
"between",
76+
"bigint",
77+
"binary",
78+
"bit",
79+
"boolean",
80+
"both",
81+
"by",
82+
"cache",
83+
"called",
84+
"cascade",
85+
"cascaded",
86+
"case",
87+
"cast",
88+
"catalog",
89+
"chain",
90+
"char",
91+
"character",
92+
"characteristics",
93+
"check",
94+
"checkpoint",
95+
"class",
96+
"close",
97+
"cluster",
98+
"coalesce",
99+
"collate",
100+
"column",
101+
"comment",
102+
"commit",
103+
"committed",
104+
"configuration",
105+
"connection",
106+
"constraint",
107+
"constraints",
108+
"content",
109+
"continue",
110+
"conversion",
111+
"copy",
112+
"cost",
113+
"create",
114+
"cross",
115+
"csv",
116+
"current",
117+
"current_catalog",
118+
"current_date",
119+
"current_role",
120+
"current_time",
121+
"current_timestamp",
122+
"current_user",
123+
"cursor",
124+
"cycle",
125+
"database",
126+
"day",
127+
"deallocate",
128+
"dec",
129+
"decimal",
130+
"declare",
131+
"default",
132+
"defaults",
133+
"deferrable",
134+
"deferred",
135+
"delete",
136+
"delimiter",
137+
"delimiters",
138+
"desc",
139+
"dictionary",
140+
"disable",
141+
"discard",
142+
"distinct",
143+
"do",
144+
"document",
145+
"domain",
146+
"double",
147+
"drop",
148+
"each",
149+
"else",
150+
"enable",
151+
"encoding",
152+
"encrypted",
153+
"end",
154+
"escape",
155+
"except",
156+
"exclude",
157+
"excluding",
158+
"exclusive",
159+
"execute",
160+
"exists",
161+
"explain",
162+
"external",
163+
"extract",
164+
"false",
165+
"family",
166+
"fetch",
167+
"first",
168+
"float",
169+
"following",
170+
"for",
171+
"force",
172+
"foreign",
173+
"forward",
174+
"freeze",
175+
"from",
176+
"full",
177+
"function",
178+
"functions",
179+
"global",
180+
"grant",
181+
"granted",
182+
"greatest",
183+
"group",
184+
"handler",
185+
"having",
186+
"header",
187+
"hold",
188+
"hour",
189+
"identity",
190+
"if",
191+
"ilike",
192+
"immediate",
193+
"immutable",
194+
"implicit",
195+
"in",
196+
"including",
197+
"increment",
198+
"index",
199+
"indexes",
200+
"inherit",
201+
"inherits",
202+
"initially",
203+
"inner",
204+
"inout",
205+
"input",
206+
"insensitive",
207+
"insert",
208+
"instead",
209+
"int",
210+
"integer",
211+
"intersect",
212+
"interval",
213+
"into",
214+
"invoker",
215+
"is",
216+
"isnull",
217+
"isolation",
218+
"join",
219+
"key",
220+
"language",
221+
"large",
222+
"last",
223+
"lateral",
224+
"leading",
225+
"least",
226+
"left",
227+
"level",
228+
"like",
229+
"limit",
230+
"listen",
231+
"load",
232+
"local",
233+
"localtime",
234+
"localtimestamp",
235+
"location",
236+
"lock",
237+
"mapping",
238+
"match",
239+
"maxvalue",
240+
"minute",
241+
"minvalue",
242+
"mode",
243+
"month",
244+
"move",
245+
"name",
246+
"names",
247+
"national",
248+
"natural",
249+
"nchar",
250+
"new",
251+
"next",
252+
"no",
253+
"none",
254+
"not",
255+
"nothing",
256+
"notify",
257+
"notnull",
258+
"nowait",
259+
"null",
260+
"nullif",
261+
"numeric",
262+
"object",
263+
"of",
264+
"off",
265+
"offset",
266+
"oids",
267+
"old",
268+
"on",
269+
"only",
270+
"operator",
271+
"option",
272+
"options",
273+
"or",
274+
"order",
275+
"out",
276+
"outer",
277+
"over",
278+
"overlaps",
279+
"overlay",
280+
"owned",
281+
"owner",
282+
"parser",
283+
"partial",
284+
"partition",
285+
"passing",
286+
"password",
287+
"placing",
288+
"plans",
289+
"position",
290+
"preceding",
291+
"precision",
292+
"prepare",
293+
"prepared",
294+
"preserve",
295+
"primary",
296+
"prior",
297+
"privileges",
298+
"procedural",
299+
"procedure",
300+
"quote",
301+
"range",
302+
"read",
303+
"real",
304+
"reassign",
305+
"recheck",
306+
"recursive",
307+
"ref",
308+
"references",
309+
"reindex",
310+
"relative",
311+
"release",
312+
"rename",
313+
"repeatable",
314+
"replace",
315+
"replica",
316+
"reset",
317+
"restart",
318+
"restrict",
319+
"returning",
320+
"returns",
321+
"revoke",
322+
"right",
323+
"role",
324+
"rollback",
325+
"row",
326+
"rows",
327+
"rule",
328+
"savepoint",
329+
"schema",
330+
"scroll",
331+
"search",
332+
"second",
333+
"security",
334+
"select",
335+
"sequence",
336+
"sequences",
337+
"serializable",
338+
"server",
339+
"session",
340+
"session_user",
341+
"set",
342+
"setof",
343+
"share",
344+
"show",
345+
"similar",
346+
"simple",
347+
"smallint",
348+
"some",
349+
"stable",
350+
"standalone",
351+
"start",
352+
"statement",
353+
"statistics",
354+
"stdin",
355+
"stdout",
356+
"storage",
357+
"strict",
358+
"strip",
359+
"substring",
360+
"symmetric",
361+
"sysid",
362+
"system",
363+
"table",
364+
"tablespace",
365+
"temp",
366+
"template",
367+
"temporary",
368+
"text",
369+
"then",
370+
"time",
371+
"timestamp",
372+
"to",
373+
"trailing",
374+
"transaction",
375+
"treat",
376+
"trigger",
377+
"trim",
378+
"true",
379+
"truncate",
380+
"trusted",
381+
"type",
382+
"unbounded",
383+
"uncommitted",
384+
"unencrypted",
385+
"union",
386+
"unique",
387+
"unknown",
388+
"unlisten",
389+
"until",
390+
"update",
391+
"vacuum",
392+
"valid",
393+
"validator",
394+
"value",
395+
"values",
396+
"varchar",
397+
"variadic",
398+
"varying",
399+
"verbose",
400+
"version",
401+
"view",
402+
"volatile",
403+
"when",
404+
"where",
405+
"whitespace",
406+
"window",
407+
"with",
408+
"without",
409+
"work",
410+
"write",
411+
"year",
412+
"zone",
413+
];

0 commit comments

Comments
 (0)