дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-06 01:57:13 +02:00
fix(database): write binary keys and values as bytes when submitting edits (#8209)
The data grid shows a binary value as 0x... hex, and a submitted edit sent that text back as a quoted string: a row with a binary primary key could not be changed or deleted (SQLite and SQL Server matched no row or refused the varchar-to-varbinary conversion, MySQL silently changed nothing), and an edited binary value was stored as the bytes of its hex text. Submitting edits now looks up the table's binary columns when an edited key or value is 0x... hex, and writes those values as bytes (X'...' on SQLite and MySQL, 0x... on SQL Server); other text stays quoted, and the console's Output log shows the same literals. An edit that matches no row now fails on MySQL too (sqlx requests CLIENT_FOUND_ROWS, so a matched but unchanged row still counts), and a failed batch rolls its transaction back before the connection returns to the pool, discarding the connection if the rollback fails. Co-authored-by: ntbang0901 <[email protected]>
Цей коміт міститься в:
1 джерело
feb5262a16
коміт
8f080ee99e
11 files changed
+378
-49
No files matched your search
@@ -48,6 +48,7 @@ import {
|
||||
type EditBlockReason,
|
||||
} from "./resultEditing";
|
||||
import { formatElapsed, pageWindow } from "./resultView";
|
||||
import { literalKind } from "./sqlLiteral";
|
||||
import { resultTabMenu } from "./resultTabActions";
|
||||
import { nameMatcher } from "./sql/dialect";
|
||||
import {
|
||||
@@ -441,7 +442,9 @@ export function Result({ tab, onClose }: { tab: ResultTab; onClose?: () => void
|
||||
if (keyCells.length) await loadCells(tab, keyCells);
|
||||
const list = buildRowEdits(resultNames, rowsOf(r), pk, edits);
|
||||
// The Output log shows what the submit is about to change.
|
||||
for (const s of editStatements(driver, target.schema ?? "", target.table, list))
|
||||
const kindOf = (column: string) =>
|
||||
literalKind(driver, r.columns[resultNames.indexOf(column)]?.typeName ?? "", false);
|
||||
for (const s of editStatements(driver, target.schema ?? "", target.table, list, kindOf))
|
||||
log("statement", s, undefined, tab.path);
|
||||
const n = await withConnection(tab.sourceId, () =>
|
||||
call(commands.dbApplyEdits(tab.sourceId, target.schema ?? "", target.table, list)),
|
||||
|
||||
@@ -312,4 +312,32 @@ describe("editStatements", () => {
|
||||
]),
|
||||
).toEqual([`UPDATE PUBLIC.USERS SET "path" = 'C:\\\\it''s' WHERE ID = '1'`]);
|
||||
});
|
||||
|
||||
it.each([
|
||||
[
|
||||
"mysql",
|
||||
"UPDATE files SET data = X'CAFF', name = '0xCAFF' WHERE id = X'0102'",
|
||||
"DELETE FROM files WHERE id = X'0102'",
|
||||
],
|
||||
[
|
||||
"sqlServer",
|
||||
"UPDATE files SET data = 0xCAFF, name = '0xCAFF' WHERE id = 0x0102",
|
||||
"DELETE FROM files WHERE id = 0x0102",
|
||||
],
|
||||
] as const)("writes %s binary keys and values as the bytes the backend writes", (driver, update, del) => {
|
||||
const kindOf = (column: string) => (column === "name" ? "text" : "binary");
|
||||
const key = [{ column: "id", value: "0x0102" }];
|
||||
const edits: DbRowEdit[] = [
|
||||
{
|
||||
kind: "update",
|
||||
key,
|
||||
values: [
|
||||
{ column: "data", value: "0xCAFF" },
|
||||
{ column: "name", value: "0xCAFF" },
|
||||
],
|
||||
},
|
||||
{ kind: "delete", key },
|
||||
];
|
||||
expect(editStatements(driver, "", "files", edits, kindOf)).toEqual([update, del]);
|
||||
});
|
||||
});
|
||||
@@ -10,7 +10,7 @@ import type { DbCellValue, DbCompletion, DbDriver, DbRowEdit } from "@/platform/
|
||||
import { quoteIdent } from "./sql/complete";
|
||||
import { CLAUSE_WORDS, functions } from "./sql/dialect";
|
||||
import { identName, lex, type Token } from "./sql/lexer";
|
||||
import { sqlLiteral } from "./sqlLiteral";
|
||||
import { sqlLiteral, type LiteralKind } from "./sqlLiteral";
|
||||
|
||||
const sqlName = (token: Token, driver: DbDriver): string => {
|
||||
const name = identName(token);
|
||||
@@ -291,33 +291,43 @@ export function pkResultColumns(
|
||||
return out;
|
||||
}
|
||||
|
||||
const conditionOf = (driver: DbDriver, key: readonly DbCellValue[]): string =>
|
||||
type KindOf = (column: string) => LiteralKind;
|
||||
|
||||
const conditionOf = (driver: DbDriver, key: readonly DbCellValue[], kindOf: KindOf): string =>
|
||||
key
|
||||
.map((c) =>
|
||||
c.value === null
|
||||
? `${quoteIdent(c.column, driver)} IS NULL`
|
||||
: `${quoteIdent(c.column, driver)} = ${sqlLiteral(c.value, driver)}`,
|
||||
: `${quoteIdent(c.column, driver)} = ${sqlLiteral(c.value, driver, kindOf(c.column))}`,
|
||||
)
|
||||
.join(" AND ");
|
||||
|
||||
/**
|
||||
* The statements `dbApplyEdits` runs for `edits`, for the console's Output
|
||||
* log: the same `UPDATE` / `INSERT` / `DELETE` the backend generates.
|
||||
* log: the same `UPDATE` / `INSERT` / `DELETE` the backend generates, which
|
||||
* writes the `0x…` hex of a binary column (`kindOf`) as its bytes.
|
||||
*/
|
||||
export function editStatements(driver: DbDriver, schema: string, table: string, edits: readonly DbRowEdit[]): string[] {
|
||||
export function editStatements(
|
||||
driver: DbDriver,
|
||||
schema: string,
|
||||
table: string,
|
||||
edits: readonly DbRowEdit[],
|
||||
kindOf: KindOf = () => "text",
|
||||
): string[] {
|
||||
const quoted = (name: string) => quoteIdent(name, driver);
|
||||
const target = schema ? `${quoted(schema)}.${quoted(table)}` : quoted(table);
|
||||
const value = (c: DbCellValue) => sqlLiteral(c.value, driver, kindOf(c.column));
|
||||
return edits.map((e) => {
|
||||
const literal = (c: DbCellValue) => `${quoted(c.column)} = ${sqlLiteral(c.value, driver)}`;
|
||||
const literal = (c: DbCellValue) => `${quoted(c.column)} = ${value(c)}`;
|
||||
switch (e.kind) {
|
||||
case "update":
|
||||
return `UPDATE ${target} SET ${e.values.map(literal).join(", ")} WHERE ${conditionOf(driver, e.key)}`;
|
||||
return `UPDATE ${target} SET ${e.values.map(literal).join(", ")} WHERE ${conditionOf(driver, e.key, kindOf)}`;
|
||||
case "delete":
|
||||
return `DELETE FROM ${target} WHERE ${conditionOf(driver, e.key)}`;
|
||||
return `DELETE FROM ${target} WHERE ${conditionOf(driver, e.key, kindOf)}`;
|
||||
default:
|
||||
return e.values.length
|
||||
? `INSERT INTO ${target} (${e.values.map((c) => quoted(c.column)).join(", ")}) VALUES (${e.values
|
||||
.map((c) => sqlLiteral(c.value, driver))
|
||||
.map(value)
|
||||
.join(", ")})`
|
||||
: driver === "mysql"
|
||||
? `INSERT INTO ${target} () VALUES ()`
|
||||
|
||||
@@ -182,6 +182,27 @@ beforeEach(() => {
|
||||
});
|
||||
|
||||
describe("editable result grid", () => {
|
||||
it("logs a binary key and value as the bytes the submit writes", async () => {
|
||||
backend.driver = "mysql";
|
||||
const tab = show("SELECT * FROM users", [{ name: "id" }, { name: "name" }], [["0x0102", "0xCAFE"]]);
|
||||
tab.outcome.result!.columns = [
|
||||
{ name: "id", typeName: "VARBINARY" },
|
||||
{ name: "name", typeName: "BLOB" },
|
||||
];
|
||||
await act(async () => render(<ResultsView />));
|
||||
fireEvent.doubleClick(await screen.findByRole("gridcell", { name: "0xCAFE" }));
|
||||
const input = screen.getByRole("textbox", { name: "Edit name" });
|
||||
fireEvent.change(input, { target: { value: "0xCAFF" } });
|
||||
fireEvent.keyDown(input, { key: "Enter" });
|
||||
await act(async () => fireEvent.click(screen.getByRole("button", { name: "Submit" })));
|
||||
expect(backend.applyEdits.mock.calls[0]?.[3]).toEqual([
|
||||
{ kind: "update", key: [{ column: "id", value: "0x0102" }], values: [{ column: "name", value: "0xCAFF" }] },
|
||||
]);
|
||||
expect(useDatabase.getState().output.map((line) => line.text)).toContain(
|
||||
"UPDATE public.users SET name = X'CAFF' WHERE id = X'0102'",
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps every cell of a pasted row in the submitted edits", async () => {
|
||||
show("SELECT * FROM users", [{ name: "id" }, { name: "name" }], [["1", "Ann"]]);
|
||||
await act(async () => render(<ResultsView />));
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
import type { DbDriver } from "@/platform/ipc";
|
||||
import type { Cell } from "./model";
|
||||
|
||||
type LiteralKind = "text" | "number" | "binary";
|
||||
export type LiteralKind = "text" | "number" | "binary";
|
||||
|
||||
/** A decimal number every dialect reads, unlike JavaScript's `Number` (`""`, `0x10`). */
|
||||
const DECIMAL = /^[+-]?(\d+\.?\d*|\.\d+)(e[+-]?\d+)?$/i;
|
||||
|
||||
Reference in new issue
Заблокувати користувача