Skip to content

Commit

Permalink
Escape explorer table names (#510)
Browse files Browse the repository at this point in the history
  • Loading branch information
macjuul authored Oct 9, 2024
1 parent a683897 commit 224ab6c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 35 deletions.
25 changes: 6 additions & 19 deletions src/screens/database/views/explorer/ExplorerPane/hooks.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { keepPreviousData, useQuery } from "@tanstack/react-query";
import { useDatabaseSchema } from "~/hooks/schema";
import {
executeQueryFirst,
executeQuerySingle,
} from "~/screens/database/connection/connection";
import { executeQueryFirst, executeQuerySingle } from "~/screens/database/connection/connection";
import { escapeIdent } from "~/util/surrealql";

export type SortMode = [string, "asc" | "desc"] | null;

Expand All @@ -23,14 +21,7 @@ export function useRecordQuery(input: RecordQueryInput) {
queryKey: ["explorer", "records", input],
placeholderData: keepPreviousData,
queryFn: async () => {
const {
activeTable,
currentPage,
pageSize,
sortMode,
isFilterValid,
filter,
} = input;
const { activeTable, currentPage, pageSize, sortMode, isFilterValid, filter } = input;

try {
if (!activeTable || !isFilterValid) {
Expand All @@ -40,7 +31,7 @@ export function useRecordQuery(input: RecordQueryInput) {
const limitBy = pageSize;
const startAt = (currentPage - 1) * pageSize;

let fetchQuery = `SELECT * FROM ${activeTable}`;
let fetchQuery = `SELECT * FROM ${escapeIdent(activeTable)}`;

if (filter) {
fetchQuery += ` WHERE ${filter}`;
Expand All @@ -61,11 +52,7 @@ export function useRecordQuery(input: RecordQueryInput) {
const headers =
schema?.tables
?.find((t) => t.schema.name === activeTable)
?.fields?.filter(
(f) =>
!f.name.includes("[*]") &&
!f.name.includes("."),
)
?.fields?.filter((f) => !f.name.includes("[*]") && !f.name.includes("."))
?.map((f) => f.name) || [];

return {
Expand Down Expand Up @@ -96,7 +83,7 @@ export function usePaginationQuery(input: PaginationQueryInput) {
}

try {
let countQuery = `SELECT count() AS count FROM ${activeTable}`;
let countQuery = `SELECT count() AS count FROM ${escapeIdent(activeTable)}`;

if (filter) {
countQuery += ` WHERE ${filter}`;
Expand Down
38 changes: 22 additions & 16 deletions src/screens/database/views/explorer/ExplorerView/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import {
iconChevronRight,
iconDesigner,
iconDownload,
iconExplorer,
iconOpen,
iconPlus,
iconTable,
iconUpload,
} from "~/util/icons";

import { Box, Button, Group, Text } from "@mantine/core";
import { useDisclosure } from "@mantine/hooks";
import { memo, useState } from "react";
Expand All @@ -11,23 +22,12 @@ import { useConnection, useIsConnected } from "~/hooks/connection";
import { useEventSubscription } from "~/hooks/event";
import { usePanelMinSize } from "~/hooks/panels";
import { useStable } from "~/hooks/stable";
import { useIsLight } from "~/hooks/theme";
import { dispatchIntent, useIntent } from "~/hooks/url";
import { useViewEffect } from "~/hooks/view";
import { useDesigner } from "~/providers/Designer";
import { TablesPane } from "~/screens/database/components/TablesPane";
import { useInterfaceStore } from "~/stores/interface";
import { DisconnectedEvent } from "~/util/global-events";
import {
iconChevronRight,
iconDesigner,
iconDownload,
iconExplorer,
iconOpen,
iconPlus,
iconTable,
iconUpload,
} from "~/util/icons";
import { syncConnectionSchema } from "~/util/schema";
import { CreatorDrawer } from "../CreatorDrawer";
import { ExplorerPane } from "../ExplorerPane";
Expand Down Expand Up @@ -93,12 +93,19 @@ export function ExplorerView() {

return (
<>
<Box h="100%" ref={ref}>
<Box
h="100%"
ref={ref}
>
<PanelGroup
direction="horizontal"
style={{ opacity: minSize === 0 ? 0 : 1 }}
>
<Panel defaultSize={minSize} minSize={minSize} maxSize={35}>
<Panel
defaultSize={minSize}
minSize={minSize}
maxSize={35}
>
<TablesPaneLazy
icon={iconExplorer}
activeTable={activeTable}
Expand Down Expand Up @@ -151,9 +158,8 @@ export function ExplorerView() {
}}
>
<Text>
The explorer view provides an easy way to
browse your tables and records without
writing any queries.
The explorer view provides an easy way to browse your tables and
records without writing any queries.
</Text>
<Group>
<Button
Expand Down

0 comments on commit 224ab6c

Please sign in to comment.