Skip to content

Commit

Permalink
web,graphql: Remove default query
Browse files Browse the repository at this point in the history
  • Loading branch information
tbantle22 committed Nov 13, 2023
1 parent 7ea53c5 commit d4a071b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 234 deletions.
2 changes: 1 addition & 1 deletion graphql-server/src/branches/branch.queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SortBranchesBy } from "./branch.enum";
export const branchQuery = `SELECT * FROM dolt_branches WHERE name=?`;

export const getBranchesQuery = (sortBy?: SortBranchesBy) =>
`SELECT * FROM dolt_branches ${getOrderByForBranches(sortBy)}LIMIT 200`;
`SELECT * FROM dolt_branches ${getOrderByForBranches(sortBy)}`;

export const callNewBranch = `CALL DOLT_BRANCH(?, ?)`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,23 @@ import Btn from "@components/Btn";
import Loader from "@components/Loader";
import MobileSqlViewer from "@components/SqlEditor/MobileSqlViewer";
import { useSqlEditorContext } from "@contexts/sqleditor";
import {
ColumnForDataTableFragment,
useDataTableQuery,
} from "@gen/graphql-types";
import { OptionalRefParams, RefParams } from "@lib/params";
import { OptionalRefParams } from "@lib/params";
import { FaChevronDown } from "@react-icons/all-files/fa/FaChevronDown";
import { FaChevronUp } from "@react-icons/all-files/fa/FaChevronUp";
import { useEffect } from "react";
import Errors from "../Errors";
import { getEditorString, getSqlString } from "../utils";
import css from "./index.module.css";

type Params = OptionalRefParams & {
q?: string;
tableName?: string;
};
type RequireParams = RefParams & {
q?: string;
tableName: string;
};

type Props = {
params: Params;
params: OptionalRefParams & {
q?: string;
tableName?: string;
};
empty?: boolean;
};

type InnerProps = Props & {
cols?: ColumnForDataTableFragment[];
};

type QueryProps = Props & {
params: RequireParams;
};

function Inner(props: InnerProps) {
export default function DatabaseTableHeaderMobile(props: Props) {
const sqlString = getSqlString(
props.params.q,
props.params.tableName,
Expand All @@ -47,30 +29,20 @@ function Inner(props: InnerProps) {
useSqlEditorContext();

useEffect(() => {
const sqlRes = getEditorString(
const sqlQuery = getEditorString(
props.params.q,
props.params.tableName,
props.empty,
props.cols,
);
setEditorString(sqlRes.sqlQuery);
}, [
props.cols,
props.empty,
props.params.q,
props.params.tableName,
setEditorString,
]);
setEditorString(sqlQuery);
}, [props.empty, props.params.q, props.params.tableName]);

return props.empty ? (
<div className={css.empty}>Databases are read-only.</div>
) : (
return (
<div className={css.editorContainer}>
<Loader loaded={!loading} />
<div className={css.editorHeader}>
<Btn className={css.queryHeader} onClick={() => toggleSqlEditor()}>
<span>Query</span>

{showSqlEditor ? (
<FaChevronDown className={css.caret} />
) : (
Expand All @@ -89,27 +61,3 @@ function Inner(props: InnerProps) {
</div>
);
}

function WithQuery(props: QueryProps) {
const res = useDataTableQuery({
variables: props.params,
});
if (res.loading) return <Loader loaded={false} />;
return <Inner {...props} cols={res.data?.table.columns} />;
}

export default function DatabaseTableHeaderMobile(props: Props) {
if (props.params.tableName && props.params.refName) {
return (
<WithQuery
{...props}
params={{
...props.params,
refName: props.params.refName,
tableName: props.params.tableName,
}}
/>
);
}
return <Inner {...props} />;
}
69 changes: 11 additions & 58 deletions web/components/DatabaseTableHeader/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ import Btn from "@components/Btn";
import Loader from "@components/Loader";
import SqlEditor from "@components/SqlEditor";
import { useSqlEditorContext } from "@contexts/sqleditor";
import {
ColumnForDataTableFragment,
useDataTableQuery,
} from "@gen/graphql-types";
import { OptionalRefParams, RefParams } from "@lib/params";
import { OptionalRefParams } from "@lib/params";
import { BiPencil } from "@react-icons/all-files/bi/BiPencil";
import dynamic from "next/dynamic";
import { useEffect, useState } from "react";
import { useEffect } from "react";
import Buttons from "./Buttons";
import Errors from "./Errors";
import css from "./index.module.css";
Expand All @@ -19,32 +15,15 @@ const AceEditor = dynamic(async () => import("@components/AceEditor"), {
ssr: false,
});

type Params = OptionalRefParams & {
q?: string;
tableName?: string;
};

type RequireParams = RefParams & {
q?: string;
tableName: string;
};

type Props = {
params: Params;
params: OptionalRefParams & {
q?: string;
tableName?: string;
};
empty?: boolean;
};

type InnerProps = Props & {
cols?: ColumnForDataTableFragment[];
};

type QueryProps = Props & {
params: RequireParams;
};

function Inner(props: InnerProps) {
const [showDefaultQueryInfo, setShowDefaultQueryInfo] = useState(false);

export default function DatabaseTableHeader(props: Props) {
const sqlString = getSqlString(
props.params.q,
props.params.tableName,
Expand All @@ -60,15 +39,13 @@ function Inner(props: InnerProps) {
} = useSqlEditorContext();

useEffect(() => {
const sqlRes = getEditorString(
const sqlQuery = getEditorString(
props.params.q,
props.params.tableName,
props.empty,
props.cols,
);
setEditorString(sqlRes.sqlQuery);
setShowDefaultQueryInfo(sqlRes.showDefaultQueryInfo);
}, [props.cols]);
setEditorString(sqlQuery);
}, [props.params.q, props.params.tableName, props.empty]);

return (
<div className={css.editorContainer}>
Expand All @@ -86,7 +63,7 @@ function Inner(props: InnerProps) {
<Buttons sqlString={editorString} params={props.params} />
</div>
{showSqlEditor ? (
<SqlEditor {...props} showDefaultQueryInfo={showDefaultQueryInfo} />
<SqlEditor {...props} />
) : (
<Btn
data-cy="sql-editor-collapsed"
Expand Down Expand Up @@ -120,27 +97,3 @@ function getQueryTitle(sqlString: string, empty?: boolean): string {
if (empty) return "Sample Query";
return "Query";
}

function WithQuery(props: QueryProps) {
const res = useDataTableQuery({
variables: props.params,
});
if (res.loading) return <Loader loaded={false} />;
return <Inner {...props} cols={res.data?.table.columns} />;
}

export default function DatabaseTableHeader(props: Props) {
if (props.params.tableName && props.params.refName) {
return (
<WithQuery
{...props}
params={{
...props.params,
refName: props.params.refName,
tableName: props.params.tableName,
}}
/>
);
}
return <Inner {...props} />;
}
45 changes: 5 additions & 40 deletions web/components/DatabaseTableHeader/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,41 @@
import { ColumnForDataTableFragment } from "@gen/graphql-types";
import { sampleQuery } from "./utils";

const idPKColumn: ColumnForDataTableFragment = {
name: "id",
isPrimaryKey: true,
type: "INT",
};

const pkPKColumn: ColumnForDataTableFragment = {
name: "pk2",
isPrimaryKey: true,
type: "INT",
};

const nameColumn: ColumnForDataTableFragment = {
name: "name",
isPrimaryKey: false,
type: "VARCHAR(16383)",
};

describe("tests sampleQuery", () => {
const tests: Array<{
desc: string;
cols: ColumnForDataTableFragment[];
expectedQuery: string;
expectedShowLink: boolean;
tableName?: string;
}> = [
{
desc: "Keyless table",
cols: [nameColumn],
expectedQuery: `SELECT *\nFROM \`Keyless table\`\nLIMIT 200;\n`,
expectedShowLink: false,
expectedQuery: `SELECT * FROM \`Keyless table\``,
tableName: "Keyless table",
},
{
desc: "One key and data",
cols: [idPKColumn, nameColumn],
expectedQuery: `SELECT *\nFROM \`One key and data table\`\nORDER BY \`id\` ASC\nLIMIT 200;\n`,
expectedShowLink: true,
expectedQuery: `SELECT * FROM \`One key and data table\``,
tableName: "One key and data table",
},
{
desc: "Two Key",
cols: [idPKColumn, pkPKColumn],
expectedQuery: `SELECT *\nFROM \`Two Keys table\`\nORDER BY \`id\` ASC, \`pk2\` ASC\nLIMIT 200;\n`,
expectedShowLink: true,
expectedQuery: `SELECT * FROM \`Two Keys table\``,
tableName: "Two Keys table",
},
{
desc: "Two Keys and data",
cols: [idPKColumn, pkPKColumn, nameColumn],
expectedQuery: `SELECT *\nFROM \`Two Keys and data table\`\nORDER BY \`id\` ASC, \`pk2\` ASC\nLIMIT 200;\n`,
expectedShowLink: true,
expectedQuery: `SELECT * FROM \`Two Keys and data table\``,
tableName: "Two Keys and data table",
},
{
desc: "no table Name",
cols: [idPKColumn, pkPKColumn, nameColumn],
expectedShowLink: false,
expectedQuery: `SHOW TABLES;\n`,
},
];

tests.forEach(test => {
it(`tests ${test.desc} table default query`, () => {
const { sqlQuery, showDefaultQueryInfo } = sampleQuery(
test.tableName,
test.cols,
);
const sqlQuery = sampleQuery(test.tableName);
expect(sqlQuery).toContain(test.expectedQuery);
expect(showDefaultQueryInfo).toBe(test.expectedShowLink);
});
});
});
60 changes: 12 additions & 48 deletions web/components/DatabaseTableHeader/utils.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { ColumnForDataTableFragment } from "@gen/graphql-types";
import { isDoltSystemTable } from "@lib/doltSystemTables";

const sqlSelectRowLimit = 200;

export const exampleCreateTable = `CREATE TABLE tablename (pk INT, col1 VARCHAR(255), PRIMARY KEY (pk));`;

type EditorStringResult = {
sqlQuery: string;
showDefaultQueryInfo: boolean;
};
const exampleCreateTable = `CREATE TABLE tablename (pk INT, col1 VARCHAR(255), PRIMARY KEY (pk));`;

export function getSqlString(
query?: string,
Expand All @@ -28,53 +20,25 @@ export function getEditorString(
query?: string,
tableName?: string,
empty?: boolean,
cols?: ColumnForDataTableFragment[],
): EditorStringResult {
): string {
if (empty) {
return {
sqlQuery: sampleCreateQueryForEmpty(),
showDefaultQueryInfo: false,
};
return sampleCreateQueryForEmpty();
}
return query
? { sqlQuery: query, showDefaultQueryInfo: false }
: sampleQuery(tableName ?? "", cols);
return query ?? sampleQuery(tableName ?? "");
}

function getOrderByClause(cols?: ColumnForDataTableFragment[]) {
if (!cols || cols.length === 0) {
return "";
}
const colCauses = cols
.filter(col => col.isPrimaryKey)
.map(c => `\`${c.name}\` ASC`)
.join(", ");
return colCauses !== "" ? `ORDER BY ${colCauses}` : "";
function addEmptyLines(firstLine: string): string {
const lines = [firstLine];
// eslint-disable-next-line no-empty
while (lines.push("") < 5) {}
return lines.join("\n");
}

export function sampleQuery(
tableName?: string,
cols?: ColumnForDataTableFragment[],
): EditorStringResult {
let lines: string[];
const orderByClause = getOrderByClause(cols);
export function sampleQuery(tableName?: string): string {
if (!tableName || isDoltSystemTable(tableName)) {
lines = ["SHOW TABLES;"];
} else {
lines = [
"SELECT *",
`FROM \`${tableName}\``,
orderByClause,
`LIMIT ${sqlSelectRowLimit};`,
];
return addEmptyLines("SHOW TABLES;");
}
lines = lines.filter(line => line !== "");
// eslint-disable-next-line no-empty
while (lines.push("") < 5) {}
return {
sqlQuery: lines.join("\n"),
showDefaultQueryInfo: orderByClause !== "" && lines[0] !== "SHOW TABLES;",
};
return addEmptyLines(`SELECT * FROM \`${tableName}\`;`);
}

export function sampleCreateQueryForEmpty(): string {
Expand Down
Loading

0 comments on commit d4a071b

Please sign in to comment.