fix(core): preserve JSON literals in copied INSERT statements

This commit is contained in:
dienaso 2026-07-30 20:56:31 +08:00 committed by GitHub
parent b89e0079a5
commit 7f40aaccaf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 65 additions and 0 deletions

View File

@ -1724,6 +1724,20 @@ fn format_grid_copy_insert_sql_literal(
}
}
}
// JSON columns may expose a JSON array/object value (e.g. `[1,2,3]` or `{}`)
// instead of its string form. Keep it as a single JSON literal rather than
// letting format_grid_sql_literal serialize it as a PostgreSQL-style array
// (`{...}`). Serialize the value back to compact JSON text, format that as a
// string literal, then cast it for MySQL so it inserts as JSON.
if column_info.is_some_and(|column| {
let dt = column.data_type.trim();
dt.eq_ignore_ascii_case("json") || dt.eq_ignore_ascii_case("jsonb")
}) && (value.is_array() || value.is_object())
{
let json_text = value.to_string();
let string_literal = format_grid_sql_literal(&Value::String(json_text), database_type, column_info);
return mysql_json_predicate_literal(string_literal, database_type, column_info);
}
format_grid_sql_literal(value, database_type, column_info)
}
@ -2990,6 +3004,57 @@ mod tests {
);
}
#[test]
fn copy_insert_keeps_mysql_json_array_as_single_json_literal() {
let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
database_type: Some(DatabaseType::Mysql),
table_meta: None,
columns: vec!["data".to_string()],
column_types: Some(vec![Some("json".to_string())]),
source_columns: None,
rows: vec![vec![json!([1, 2, 3])]],
exclude_primary_keys: false,
include_computed_columns: false,
insert_mode: DataGridCopyInsertMode::Merged,
});
assert_eq!(statement.as_deref(), Some("INSERT INTO table_name (`data`) VALUES (CAST('[1,2,3]' AS JSON));"));
}
#[test]
fn copy_insert_keeps_mysql_json_empty_array_as_single_json_literal() {
let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
database_type: Some(DatabaseType::Mysql),
table_meta: None,
columns: vec!["data".to_string()],
column_types: Some(vec![Some("json".to_string())]),
source_columns: None,
rows: vec![vec![json!([])]],
exclude_primary_keys: false,
include_computed_columns: false,
insert_mode: DataGridCopyInsertMode::Merged,
});
assert_eq!(statement.as_deref(), Some("INSERT INTO table_name (`data`) VALUES (CAST('[]' AS JSON));"));
}
#[test]
fn copy_insert_keeps_mysql_json_object_as_single_json_literal() {
let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
database_type: Some(DatabaseType::Mysql),
table_meta: None,
columns: vec!["data".to_string()],
column_types: Some(vec![Some("json".to_string())]),
source_columns: None,
rows: vec![vec![json!({"a": 1})]],
exclude_primary_keys: false,
include_computed_columns: false,
insert_mode: DataGridCopyInsertMode::Merged,
});
assert_eq!(statement.as_deref(), Some("INSERT INTO table_name (`data`) VALUES (CAST('{\"a\":1}' AS JSON));"));
}
#[test]
fn builds_copy_insert_without_primary_keys_when_primary_keys_are_hidden() {
let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {