Skip to content

Commit

Permalink
feat: add big number support for local connection (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
invisal authored Apr 6, 2024
1 parent 203105b commit 46fa92d
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/app/client/[[...driver]]/page-client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default function ClientPageBody() {
if (config.driver === "rqlite") {
return new RqliteDriver(config.url, config.username, config.password);
}
return new TursoDriver(config.url, config.token as string);
return new TursoDriver(config.url, config.token as string, true);
}, []);

const config = useMemo(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/app/client/s/[[...driver]]/page-client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function ClientPageBody() {
);
}

return new TursoDriver(conn.config.url, conn.config.token);
return new TursoDriver(conn.config.url, conn.config.token, true);
}, [conn]);

if (!driver || !conn) {
Expand Down
52 changes: 40 additions & 12 deletions src/components/query-result-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import {
} from "./ui/dropdown-menu";
import { triggerSelectFiles, uploadFile } from "@/lib/file-upload";
import { toast } from "sonner";
import { useDatabaseDriver } from "@/context/DatabaseDriverProvider";
import BigNumberCell from "./table-cell/BigNumberCell";

interface ResultTableProps {
data: OptimizeTableState;
Expand Down Expand Up @@ -91,6 +93,7 @@ export default function ResultTable({
}: ResultTableProps) {
const [stickyHeaderIndex, setStickHeaderIndex] = useState<number>();
const { openBlockEditor } = useBlockEditor();
const { databaseDriver } = useDatabaseDriver();

const renderHeader = useCallback(
(header: OptimizeTableHeaderWithIndexProps) => {
Expand All @@ -99,7 +102,7 @@ export default function ResultTable({
<DropdownMenuItem
onClick={() => {
setStickHeaderIndex(
header.index === stickyHeaderIndex ? undefined : header.index,
header.index === stickyHeaderIndex ? undefined : header.index
);
}}
>
Expand Down Expand Up @@ -132,7 +135,7 @@ export default function ResultTable({
</Header>
);
},
[stickyHeaderIndex, tableName, onSortColumnChange],
[stickyHeaderIndex, tableName, onSortColumnChange]
);

const renderCell = useCallback(
Expand Down Expand Up @@ -161,10 +164,7 @@ export default function ResultTable({
}}
/>
);
} else if (
header.dataType === TableColumnDataType.REAL ||
header.dataType === TableColumnDataType.INTEGER
) {
} else if (header.dataType === TableColumnDataType.REAL) {
return (
<NumberCell
state={state}
Expand All @@ -177,11 +177,39 @@ export default function ResultTable({
}}
/>
);
} else if (header.dataType === TableColumnDataType.INTEGER) {
if (databaseDriver.supportBigInt()) {
return (
<BigNumberCell
state={state}
editMode={editMode}
value={state.getValue(y, x) as DatabaseValue<bigint>}
focus={isFocus}
isChanged={state.hasCellChange(y, x)}
onChange={(newValue) => {
state.changeValue(y, x, newValue);
}}
/>
);
} else {
return (
<NumberCell
state={state}
editMode={editMode}
value={state.getValue(y, x) as DatabaseValue<number>}
focus={isFocus}
isChanged={state.hasCellChange(y, x)}
onChange={(newValue) => {
state.changeValue(y, x, newValue);
}}
/>
);
}
}

return <GenericCell value={state.getValue(y, x) as string} />;
},
[],
[databaseDriver]
);

const onHeaderContextMenu = useCallback((e: React.MouseEvent) => {
Expand Down Expand Up @@ -339,7 +367,7 @@ export default function ResultTable({
onClick: () => {
if (state.getSelectedRowCount() > 0) {
window.navigator.clipboard.writeText(
exportRowsToExcel(state.getSelectedRowsArray()),
exportRowsToExcel(state.getSelectedRowsArray())
);
}
},
Expand All @@ -356,8 +384,8 @@ export default function ResultTable({
exportRowsToSqlInsert(
tableName ?? "UnknownTable",
headers,
state.getSelectedRowsArray(),
),
state.getSelectedRowsArray()
)
);
}
},
Expand All @@ -381,7 +409,7 @@ export default function ResultTable({
},
])(event);
},
[data, tableName, copyCallback, pasteCallback, openBlockEditor],
[data, tableName, copyCallback, pasteCallback, openBlockEditor]
);

const onKeyDown = useCallback(
Expand Down Expand Up @@ -435,7 +463,7 @@ export default function ResultTable({

e.preventDefault();
},
[copyCallback, pasteCallback],
[copyCallback, pasteCallback]
);

return (
Expand Down
4 changes: 4 additions & 0 deletions src/drivers/base-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ export interface DatabaseTableOperationReslt {
}

export abstract class BaseDriver {
// Flags
abstract supportBigInt(): boolean;

// Methods
abstract close(): void;

abstract query(stmt: InStatement): Promise<DatabaseResultSet>;
Expand Down
4 changes: 4 additions & 0 deletions src/drivers/remote-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export default class RemoteDriver implements BaseDriver {
this.authToken = authToken;
}

supportBigInt(): boolean {
return false;
}

protected async request<T = unknown>(body: RequestOperationBody) {
const r = await fetch(`/api/ops/${this.id}`, {
method: "POST",
Expand Down
4 changes: 4 additions & 0 deletions src/drivers/rqlite-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ export default class RqliteDriver extends SqliteLikeBaseDriver {
return json.results.map(transformRawResult);
}

supportBigInt(): boolean {
return false;
}

async query(stmt: InStatement): Promise<DatabaseResultSet> {
return (await this.transaction([stmt]))[0];
}
Expand Down
9 changes: 8 additions & 1 deletion src/drivers/turso-driver.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,25 @@ export default class TursoDriver extends SqliteLikeBaseDriver {
protected client: Client;
protected endpoint: string = "";
protected authToken = "";
protected bigInt = false;

constructor(url: string, authToken: string) {
constructor(url: string, authToken: string, bigInt: boolean = false) {
super();
this.endpoint = url;
this.authToken = authToken;
this.bigInt = bigInt;

this.client = createClient({
url: this.endpoint,
authToken: this.authToken,
intMode: bigInt ? "bigint" : "number",
});
}

supportBigInt(): boolean {
return this.bigInt;
}

async query(stmt: InStatement) {
const stream = this.client;
const r = await stream.execute(stmt);
Expand Down

0 comments on commit 46fa92d

Please sign in to comment.