feat(grid): add conditional bulk updates for MySQL
This commit is contained in:
29 files changed
+1813
-85
No files matched your search
@@ -66,6 +66,44 @@ describe("coerceDataGridCellValue", () => {
|
||||
expect(coerceDataGridCellValue({ ...options, preserveEmptyString: true })).toBe("");
|
||||
});
|
||||
|
||||
it.each([null, false, true])("coerces MySQL TINYINT(1) from metadata when the sampled value is %p", (oldValue) => {
|
||||
expect(
|
||||
coerceDataGridCellValue({
|
||||
value: "true",
|
||||
oldValue,
|
||||
databaseType: "mysql",
|
||||
columnInfo: { data_type: "TINYINT(1)" },
|
||||
}),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["missing numeric metadata", 1, undefined, "2", 2],
|
||||
["empty numeric metadata", 1, { data_type: "" }, "2", 2],
|
||||
["missing boolean metadata", false, undefined, "true", true],
|
||||
["empty boolean metadata", true, { data_type: "" }, "0", false],
|
||||
])("falls back to the sampled value type for %s", (_name, oldValue, columnInfo, value, expected) => {
|
||||
expect(
|
||||
coerceDataGridCellValue({
|
||||
value,
|
||||
oldValue,
|
||||
databaseType: "mysql",
|
||||
columnInfo,
|
||||
}),
|
||||
).toBe(expected);
|
||||
});
|
||||
|
||||
it("prefers column metadata over the sampled value type", () => {
|
||||
expect(
|
||||
coerceDataGridCellValue({
|
||||
value: "2",
|
||||
oldValue: 1,
|
||||
databaseType: "mysql",
|
||||
columnInfo: { data_type: "varchar(255)" },
|
||||
}),
|
||||
).toBe("2");
|
||||
});
|
||||
|
||||
it.each([
|
||||
["numeric non-nullable", 42, "integer", false],
|
||||
["boolean nullable", true, "boolean", true],
|
||||
|
||||
@@ -197,6 +197,7 @@ export const exportDocsHtml = forward("exportDocsHtml");
|
||||
|
||||
// Query
|
||||
export const executeQuery = forward("executeQuery");
|
||||
export const executeConditionalUpdate = forward("executeConditionalUpdate");
|
||||
export const executeMulti = forward("executeMulti");
|
||||
export const executeMultiWithProgress = forward("executeMultiWithProgress");
|
||||
export const executeBatch = forward("executeBatch");
|
||||
@@ -208,6 +209,7 @@ export const executeInManualTransaction = forward("executeInManualTransaction");
|
||||
export const commitManualTransaction = forward("commitManualTransaction");
|
||||
export const rollbackManualTransaction = forward("rollbackManualTransaction");
|
||||
export const cancelQuery = forward("cancelQuery");
|
||||
export const cancelConditionalUpdate = forward("cancelConditionalUpdate");
|
||||
export const closeQuerySession = forward("closeQuerySession");
|
||||
export const closeClientConnectionSession = forward("closeClientConnectionSession");
|
||||
export const analyzeSqlReferences = forward("analyzeSqlReferences");
|
||||
@@ -258,6 +260,7 @@ export const buildDataGridColumnValueFilterCondition = forward("buildDataGridCol
|
||||
export const buildDataGridColumnValuesFilterCondition = forward("buildDataGridColumnValuesFilterCondition");
|
||||
export const buildDataGridColumnDistinctValuesSql = forward("buildDataGridColumnDistinctValuesSql");
|
||||
export const buildDataGridCountSql = forward("buildDataGridCountSql");
|
||||
export const buildDataGridConditionalUpdateSql = forward("buildDataGridConditionalUpdateSql");
|
||||
export const buildHiveTablePropertiesSql = forward("buildHiveTablePropertiesSql");
|
||||
export const buildExportInsertStatements = forward("buildExportInsertStatements");
|
||||
export const buildExportSqlInsert = forward("buildExportSqlInsert");
|
||||
|
||||
@@ -149,6 +149,7 @@ import type {
|
||||
DataGridColumnValueFilterConditionOptions,
|
||||
DataGridColumnValuesFilterConditionOptions,
|
||||
DataGridContextFilterConditionOptions,
|
||||
DataGridConditionalUpdateSqlOptions,
|
||||
DataGridCountSqlOptions,
|
||||
DataGridCopyInsertStatementOptions,
|
||||
DataGridCopyUpdateStatementOptions,
|
||||
@@ -1028,6 +1029,34 @@ export async function executeQuery(
|
||||
});
|
||||
}
|
||||
|
||||
export async function executeConditionalUpdate(
|
||||
connectionId: string,
|
||||
database: string,
|
||||
sql: string,
|
||||
schema?: string,
|
||||
executionId?: string,
|
||||
options?: {
|
||||
maxRows?: number;
|
||||
catalog?: string;
|
||||
fetchSize?: number;
|
||||
pageSize?: number;
|
||||
rowOffset?: number;
|
||||
resultSessionId?: string;
|
||||
clientSessionId?: string;
|
||||
timeoutSecs?: number;
|
||||
executionMode?: "simple" | "postgres_read_only_transaction";
|
||||
},
|
||||
): Promise<QueryResult> {
|
||||
return post("/api/query/execute-conditional-update", {
|
||||
connectionId,
|
||||
database,
|
||||
sql,
|
||||
schema,
|
||||
executionId,
|
||||
...options,
|
||||
});
|
||||
}
|
||||
|
||||
export async function executeMulti(
|
||||
connectionId: string,
|
||||
database: string,
|
||||
@@ -1200,6 +1229,15 @@ export async function cancelQuery(executionId: string): Promise<boolean> {
|
||||
return typeof result === "boolean" ? result : result.cancelled === true;
|
||||
}
|
||||
|
||||
export interface ConditionalUpdateCancellationResult {
|
||||
requested: boolean;
|
||||
terminal: boolean;
|
||||
}
|
||||
|
||||
export async function cancelConditionalUpdate(executionId: string): Promise<ConditionalUpdateCancellationResult> {
|
||||
return post("/api/query/cancel-conditional-update", { executionId });
|
||||
}
|
||||
|
||||
export async function analyzeSqlReferences(sql: string, dialect?: string): Promise<SqlReferenceAnalysis> {
|
||||
return post("/api/query/analyze-sql-references", { sql, dialect });
|
||||
}
|
||||
@@ -1431,6 +1469,11 @@ export async function buildDataGridCountSql(options: DataGridCountSqlOptions): P
|
||||
return post("/api/query/build-data-grid-count-sql", { options });
|
||||
}
|
||||
|
||||
export async function buildDataGridConditionalUpdateSql(options: DataGridConditionalUpdateSqlOptions): Promise<string | undefined> {
|
||||
const result = await post<string | null>("/api/query/build-data-grid-conditional-update-sql", { options });
|
||||
return result ?? undefined;
|
||||
}
|
||||
|
||||
export async function buildHiveTablePropertiesSql(options: HiveTablePropertiesSqlOptions): Promise<string> {
|
||||
return post("/api/query/build-hive-table-properties-sql", { options });
|
||||
}
|
||||
|
||||
@@ -73,6 +73,7 @@ import type {
|
||||
DataGridColumnValueFilterConditionOptions,
|
||||
DataGridColumnValuesFilterConditionOptions,
|
||||
DataGridContextFilterConditionOptions,
|
||||
DataGridConditionalUpdateSqlOptions,
|
||||
DataGridCountSqlOptions,
|
||||
DataGridCopyInsertStatementOptions,
|
||||
DataGridCopyUpdateStatementOptions,
|
||||
@@ -1173,6 +1174,38 @@ export async function executeQuery(
|
||||
}
|
||||
}
|
||||
|
||||
export async function executeConditionalUpdate(
|
||||
connectionId: string,
|
||||
database: string,
|
||||
sql: string,
|
||||
schema?: string,
|
||||
executionId?: string,
|
||||
options?: {
|
||||
maxRows?: number;
|
||||
catalog?: string;
|
||||
fetchSize?: number;
|
||||
pageSize?: number;
|
||||
rowOffset?: number;
|
||||
resultSessionId?: string;
|
||||
clientSessionId?: string;
|
||||
timeoutSecs?: number;
|
||||
executionMode?: "simple" | "postgres_read_only_transaction";
|
||||
},
|
||||
): Promise<QueryResult> {
|
||||
try {
|
||||
return await invoke("execute_conditional_update", {
|
||||
connectionId,
|
||||
database,
|
||||
sql,
|
||||
schema,
|
||||
executionId,
|
||||
...options,
|
||||
});
|
||||
} catch (error) {
|
||||
throw new BackendErrorException(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function executeMulti(
|
||||
connectionId: string,
|
||||
database: string,
|
||||
@@ -1285,6 +1318,15 @@ export async function cancelQuery(executionId: string): Promise<boolean> {
|
||||
return invoke("cancel_query", { executionId });
|
||||
}
|
||||
|
||||
export interface ConditionalUpdateCancellationResult {
|
||||
requested: boolean;
|
||||
terminal: boolean;
|
||||
}
|
||||
|
||||
export async function cancelConditionalUpdate(executionId: string): Promise<ConditionalUpdateCancellationResult> {
|
||||
return invoke("cancel_conditional_update", { executionId });
|
||||
}
|
||||
|
||||
export async function closeQuerySession(connectionId: string, database: string, sessionId: string, clientSessionId?: string, catalog?: string): Promise<boolean> {
|
||||
return invoke("close_query_session", {
|
||||
connectionId,
|
||||
@@ -1586,6 +1628,11 @@ export async function buildDataGridCountSql(options: DataGridCountSqlOptions): P
|
||||
return invoke("build_data_grid_count_sql", { options });
|
||||
}
|
||||
|
||||
export async function buildDataGridConditionalUpdateSql(options: DataGridConditionalUpdateSqlOptions): Promise<string | undefined> {
|
||||
const result = await invoke<string | null>("build_data_grid_conditional_update_sql", { options });
|
||||
return result ?? undefined;
|
||||
}
|
||||
|
||||
export async function buildHiveTablePropertiesSql(options: HiveTablePropertiesSqlOptions): Promise<string> {
|
||||
return invoke("build_hive_table_properties_sql", { options });
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { GridCellValue } from "@/lib/dataGrid/dataGridSql";
|
||||
import type { DatabaseType, ColumnInfo } from "@/types/database";
|
||||
import { isNumericColumnType } from "@/lib/dataGrid/dataGridColumnType";
|
||||
import { isBooleanColumnType } from "@/lib/dataGrid/dataGridBooleanColumn";
|
||||
|
||||
export interface CoerceDataGridCellValueOptions {
|
||||
value: string;
|
||||
@@ -22,25 +23,40 @@ export function coerceDataGridCellValue(options: CoerceDataGridCellValueOptions)
|
||||
// Number() return NaN and the literal fail to convert on the server. Strip
|
||||
// only unambiguous groupings and keep the normalized text for the precision
|
||||
// checks below, so exact values survive as text.
|
||||
const numericText = normalizeGroupedNumberText(value, options.columnInfo);
|
||||
if (typeof oldValue === "number") {
|
||||
const useSampledValueType = normalizeDataType(options.columnInfo?.data_type) === "";
|
||||
const numericInput = isNumericColumnType(options.columnInfo?.data_type) || (useSampledValueType && typeof oldValue === "number");
|
||||
const numericText = normalizeGroupedNumberText(value, options.columnInfo, oldValue);
|
||||
if (isBooleanInputColumn(options) || (useSampledValueType && typeof oldValue === "boolean")) {
|
||||
const booleanValue = parseBooleanInput(numericText);
|
||||
if (booleanValue !== undefined) return booleanValue;
|
||||
}
|
||||
if (numericInput) {
|
||||
const num = Number(numericText);
|
||||
if (!Number.isNaN(num)) {
|
||||
if (shouldPreserveNumericText(options, num, numericText)) {
|
||||
// Keep precision-sensitive numeric edits as text; JS Number rounds 64-bit integers.
|
||||
const text = numericText.trim();
|
||||
if (text === String(oldValue)) return oldValue;
|
||||
if (oldValue !== undefined && text === String(oldValue)) return oldValue;
|
||||
return text;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
}
|
||||
if (typeof oldValue === "boolean") {
|
||||
return numericText === "true" || numericText === "1";
|
||||
}
|
||||
return normalizeSmartQuotedJsonInput(numericText);
|
||||
}
|
||||
|
||||
function isBooleanInputColumn(options: CoerceDataGridCellValueOptions): boolean {
|
||||
if (isBooleanColumnType(options.columnInfo?.data_type, options.databaseType)) return true;
|
||||
return options.databaseType === "mysql" && options.columnInfo?.data_type.trim().toLowerCase() === "tinyint(1)";
|
||||
}
|
||||
|
||||
function parseBooleanInput(value: string): boolean | undefined {
|
||||
const normalized = value.trim().toLowerCase();
|
||||
if (normalized === "true" || normalized === "1") return true;
|
||||
if (normalized === "false" || normalized === "0") return false;
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function dataGridCellEditorText(options: { value: GridCellValue | undefined; databaseType: DatabaseType | undefined; columnInfo: Pick<ColumnInfo, "data_type"> | undefined }): string {
|
||||
const value = options.value ?? null;
|
||||
if (value === null) return "";
|
||||
@@ -106,8 +122,9 @@ function shouldPreserveNumericText(options: CoerceDataGridCellValueOptions, pars
|
||||
return shouldPreserveNumericTextForType(options.columnInfo?.data_type, text, parsedNumber);
|
||||
}
|
||||
|
||||
function normalizeGroupedNumberText(value: string, columnInfo: Pick<ColumnInfo, "data_type"> | undefined): string {
|
||||
if (!isNumericColumnType(columnInfo?.data_type)) return value;
|
||||
function normalizeGroupedNumberText(value: string, columnInfo: Pick<ColumnInfo, "data_type"> | undefined, oldValue: GridCellValue | undefined): string {
|
||||
const useSampledNumberType = normalizeDataType(columnInfo?.data_type) === "" && typeof oldValue === "number";
|
||||
if (!isNumericColumnType(columnInfo?.data_type) && !useSampledNumberType) return value;
|
||||
return stripUnambiguousThousandSeparators(value);
|
||||
}
|
||||
|
||||
|
||||
@@ -115,6 +115,15 @@ export interface DataGridCountSqlOptions {
|
||||
countHint?: string;
|
||||
}
|
||||
|
||||
export interface DataGridConditionalUpdateSqlOptions {
|
||||
databaseType?: DatabaseType;
|
||||
identifierQuote?: string;
|
||||
tableMeta: DataGridTableMeta;
|
||||
columnName: string;
|
||||
value: GridCellValue;
|
||||
whereInput: string;
|
||||
}
|
||||
|
||||
export interface HiveTablePropertiesSqlOptions {
|
||||
schema?: string;
|
||||
tableName: string;
|
||||
@@ -149,6 +158,10 @@ export function buildDataGridCountSql(options: DataGridCountSqlOptions): Promise
|
||||
return api.buildDataGridCountSql(options);
|
||||
}
|
||||
|
||||
export function buildDataGridConditionalUpdateSql(options: DataGridConditionalUpdateSqlOptions): Promise<string | undefined> {
|
||||
return api.buildDataGridConditionalUpdateSql(options);
|
||||
}
|
||||
|
||||
export function buildHiveTablePropertiesSql(options: HiveTablePropertiesSqlOptions): Promise<string> {
|
||||
return api.buildHiveTablePropertiesSql(options);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user