feat(grid): import BYTEA/BLOB cell from file with size gate

This commit is contained in:
zipg authored and GitHub committed 2026-08-09 20:13:18 +08:00
1 parent 31587bcbfe
commit 35362c8bf4
20 files changed
+326 -17

No files matched your search

+40
View File
@@ -1803,6 +1803,11 @@ pub fn format_grid_sql_literal(
return literal;
}
}
if is_postgres_binary_literal_column(database_type, column_info) {
if let Some(literal) = format_postgres_binary_literal_text(&text) {
return literal;
}
}
if is_oracle_raw_literal_column(database_type, column_info) {
if let Some(literal) = format_oracle_raw_literal_text(&text) {
return literal;
@@ -2141,6 +2146,24 @@ fn format_mysql_binary_literal_text(text: &str) -> Option<String> {
}
}
fn is_postgres_binary_literal_column(
database_type: Option<DatabaseType>,
column_info: Option<&DataGridColumnInfo>,
) -> bool {
database_type == Some(DatabaseType::Postgres)
&& column_info.is_some_and(|column| column.data_type.trim().eq_ignore_ascii_case("bytea"))
}
fn format_postgres_binary_literal_text(text: &str) -> Option<String> {
let trimmed = text.trim();
let hex = trimmed.strip_prefix("0x").or_else(|| trimmed.strip_prefix("0X"))?;
if hex.len() % 2 == 0 && hex.chars().all(|ch| ch.is_ascii_hexdigit()) {
Some(format!("decode('{hex}', 'hex')"))
} else {
None
}
}
fn is_oracle_raw_literal_column(database_type: Option<DatabaseType>, column_info: Option<&DataGridColumnInfo>) -> bool {
is_oracle_temporal_literal_database(database_type)
&& column_info.map(|column| is_oracle_raw_column_type(&column.data_type)).unwrap_or(false)
@@ -5403,6 +5426,23 @@ mod tests {
assert_eq!(format_grid_sql_literal(&json!("it's"), Some(DatabaseType::Postgres), None), "'it''s'");
}
#[test]
fn postgres_bytea_literals_decode_prefixed_hex_values() {
let bytea = column("payload", "bytea", true, None);
let text = column("label", "text", true, None);
assert_eq!(
format_grid_sql_literal(&json!("0x00aBff"), Some(DatabaseType::Postgres), Some(&bytea)),
"decode('00aBff', 'hex')"
);
assert_eq!(
format_grid_sql_literal(&json!("0x"), Some(DatabaseType::Postgres), Some(&bytea)),
"decode('', 'hex')"
);
assert_eq!(format_grid_sql_literal(&json!("0xabc"), Some(DatabaseType::Postgres), Some(&bytea)), "'0xabc'");
assert_eq!(format_grid_sql_literal(&json!("0x00ab"), Some(DatabaseType::Postgres), Some(&text)), "'0x00ab'");
}
#[test]
fn sqlite_family_literals_do_not_double_escape_backslashes() {
let value = json!(r#"{"json_raw":"{\"foo\":1,\"bar\":\"sometext\"}"}"#);