Skip to content

Commit

Permalink
allow driver to disable mismatch indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
invisal committed Aug 22, 2024
1 parent a32f84b commit 13738b1
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 23 deletions.
6 changes: 5 additions & 1 deletion src/components/gui/query-result.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@ import ResultStats from "./result-stat";
import { useMemo } from "react";
import OptimizeTableState from "./table-optimized/OptimizeTableState";
import { QueryExplanation, isExplainQueryPlan } from "./query-explanation";
import { useDatabaseDriver } from "@/context/driver-provider";

export default function QueryResult({
result,
}: {
result: MultipleQueryResult;
}) {
const { databaseDriver } = useDatabaseDriver();

const data = useMemo(() => {
if (isExplainQueryPlan(result.sql)) {
return { _tag: "EXPLAIN", value: result.result } as const;
}

const state = OptimizeTableState.createFromResult(result.result);
state.setReadOnlyMode(true);
state.mismatchDetection = databaseDriver.getFlags().mismatchDetection;
return { _tag: "QUERY", value: state } as const;
}, [result]);
}, [result, databaseDriver]);

const stats = result.result.stat;

Expand Down
1 change: 1 addition & 0 deletions src/components/gui/table-cell/create-editable-cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export default function createEditableCell<T = unknown>({
focus={focus}
isChanged={isChanged}
align={align}
mismatchDetection={state.mismatchDetection}
onDoubleClick={() => {
if (
typeof editValue === "string" &&
Expand Down
45 changes: 25 additions & 20 deletions src/components/gui/table-cell/generic-cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ interface TableCellProps<T = unknown> {
onFocus?: () => void;
onDoubleClick?: () => void;
header: OptimizeTableHeaderWithIndexProps;
mismatchDetection?: boolean;
}

interface SneakpeakProps {
Expand Down Expand Up @@ -184,6 +185,7 @@ export default function GenericCell({
align,
onDoubleClick,
header,
mismatchDetection,
}: TableCellProps) {
const className = cn(
"libsql-cell font-mono flex",
Expand Down Expand Up @@ -278,26 +280,29 @@ export default function GenericCell({

return (
<div className="relative">
{valueType && header.dataType && valueType !== header.dataType && (
<Tooltip>
<TooltipTrigger asChild>
<div className="w-0 h-0 border-transparent border-r-8 border-b-8 border-r-red-400 dark:border-r-red-600 absolute right-0 top-0"></div>
</TooltipTrigger>
<TooltipContent>
<strong>Mismatched type:</strong>
<ul>
<li>
<strong>- Expected by column:</strong>{" "}
<code>{describeTableColumnType(header.dataType)}</code>
</li>
<li>
<strong>- But stored as:</strong>{" "}
<code>{describeTableColumnType(valueType)}</code>
</li>
</ul>
</TooltipContent>
</Tooltip>
)}
{mismatchDetection &&
valueType &&
header.dataType &&
valueType !== header.dataType && (
<Tooltip>
<TooltipTrigger asChild>
<div className="w-0 h-0 border-transparent border-r-8 border-b-8 border-r-red-400 dark:border-r-red-600 absolute right-0 top-0"></div>
</TooltipTrigger>
<TooltipContent>
<strong>Mismatched type:</strong>
<ul>
<li>
<strong>- Expected by column:</strong>{" "}
<code>{describeTableColumnType(header.dataType)}</code>
</li>
<li>
<strong>- But stored as:</strong>{" "}
<code>{describeTableColumnType(valueType)}</code>
</li>
</ul>
</TooltipContent>
</Tooltip>
)}

<div
className={className}
Expand Down
1 change: 1 addition & 0 deletions src/components/gui/table-optimized/OptimizeTableState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default class OptimizeTableState {
protected headerWidth: number[] = [];
protected editMode = false;
protected readOnlyMode = false;
public mismatchDetection = false;
protected container: HTMLDivElement | null = null;

protected changeCallback: TableChangeEventCallback[] = [];
Expand Down
9 changes: 8 additions & 1 deletion src/components/gui/tabs/table-data-tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,14 @@ export default function TableDataWindow({ tableName }: TableDataContentProps) {
orderBy: sortColumns,
});

setData(OptimizeTableState.createFromResult(dataResult, schemaResult));
const tableState = OptimizeTableState.createFromResult(
dataResult,
schemaResult
);
tableState.mismatchDetection =
databaseDriver.getFlags().mismatchDetection;
setData(tableState);

setStat(dataResult.stat);
setTableSchema(schemaResult);
updateTableSchema(tableName, schemaResult.columns);
Expand Down
1 change: 1 addition & 0 deletions src/drivers/base-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ export interface DriverFlags {
defaultSchema: string;
optionalSchema: boolean;
supportBigInt: boolean;
mismatchDetection: boolean;
}

export abstract class BaseDriver {
Expand Down
1 change: 1 addition & 0 deletions src/drivers/sqlite-base-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export abstract class SqliteLikeBaseDriver extends BaseDriver {
supportBigInt: false,
defaultSchema: "main",
optionalSchema: true,
mismatchDetection: false,
};
}

Expand Down
6 changes: 5 additions & 1 deletion src/drivers/turso-driver.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ export default class TursoDriver extends SqliteLikeBaseDriver {
}

override getFlags(): DriverFlags {
return { ...super.getFlags(), supportBigInt: this.bigInt };
return {
...super.getFlags(),
supportBigInt: this.bigInt,
mismatchDetection: this.bigInt,
};
}

async query(stmt: InStatement) {
Expand Down

0 comments on commit 13738b1

Please sign in to comment.