Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

542 KB
Raw Blame
use chrono::{Local, NaiveDateTime};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashSet;

#[path = "data_grid_neo4j_sql.rs"]
mod data_grid_neo4j_sql;
use data_grid_neo4j_sql::{build_neo4j_data_grid_rollback_statements, build_neo4j_data_grid_save_statements};

#[path = "data_grid_iotdb_sql.rs"]
mod data_grid_iotdb_sql;
use data_grid_iotdb_sql::{
    build_iotdb_data_grid_rollback_statements, build_iotdb_data_grid_save_statements, uses_iotdb_table_model_save,
    validate_iotdb_existing_rows,
};

#[path = "data_grid_tdengine_sql.rs"]
mod data_grid_tdengine_sql;
use data_grid_tdengine_sql::{
    build_tdengine_data_grid_rollback_statements, build_tdengine_data_grid_save_statements,
    validate_tdengine_existing_rows, validate_tdengine_inserted_rows,
};

#[path = "data_grid_salesforce_sql.rs"]
mod data_grid_salesforce_sql;
use data_grid_salesforce_sql::{
    build_salesforce_data_grid_rollback_statements, build_salesforce_data_grid_save_statements,
    validate_salesforce_id_column,
};

use crate::models::connection::DatabaseType;
use crate::sql_dialect::{
    firebird_rows_clause, quote_table_identifier, table_pagination_strategy, uses_oracle_row_id, uses_postgres_ctid,
    uses_single_row_insert_statements, uses_synthetic_row_id, uses_xugu_row_id, TablePaginationStrategy,
};
use crate::value_literals::{format_ch_array_sql_literal, format_pg_array_sql_literal};
use dbx_types::types::is_opaque_aggregate_state_type;

const DBX_ROWID_COLUMN: &str = "__DBX_ROWID";
pub const DBX_NEO4J_ELEMENT_ID_COLUMN: &str = "__DBX_ELEMENT_ID";
pub const DBX_TDENGINE_TBNAME_COLUMN: &str = "tbname";
const DATA_GRID_COLUMN_DISTINCT_VALUES_DEFAULT_LIMIT: usize = 1000;
const DATA_GRID_COLUMN_DISTINCT_VALUES_MAX_LIMIT: usize = 1000;
/// Alias for the single cell a keyless guard query returns.
const KEYLESS_GUARD_COUNT_ALIAS: &str = "dbx_keyless_row_matches";
const KEYLESS_AMBIGUOUS_ROW_ERROR: &str = "Cannot safely update or delete this row: the table has no primary key, so the row is identified by matching every column value, and more than one row in the table matches that condition. Add a primary key or unique index, or make the rows distinguishable, before editing.";
const KEYLESS_UNIDENTIFIABLE_ROW_ERROR: &str = "Cannot safely update or delete this row: the table has no primary key and none of the result columns map to a table column, so there is no condition that can target a single row. Add a primary key, or edit the table directly, before saving.";
const POSTGRES_CTID_UNIDENTIFIABLE_ROW_ERROR: &str = "Cannot safely update or delete this PostgreSQL row because its hidden row identifier is missing or invalid. Refresh the table data before saving.";
const POSTGRES_CTID_STALE_ROW_ERROR: &str = "Cannot safely update or delete this PostgreSQL row: it no longer matches the physical row that was loaded. Another session may have changed or deleted it. Refresh the table data and try again.";
/// Dameng rejects every comparison against its binary LOB types with
/// `Data type mismatch` (SQLSTATE 22000, code -6105), and spatial columns are
/// BLOB-backed and behave the same, so a keyless row predicate — which matches
/// every result column value — cannot carry one of them.
const DAMENG_KEYLESS_BINARY_LOB_ERROR: &str = "Cannot safely update or delete this row: the table has no primary key, and Dameng cannot compare binary LOB values, so Dbx cannot build a condition that targets it. Add a primary key or unique index, or exclude the binary LOB column from the query, before editing.";

/// Row budget for one statement. It binds only the predicate shapes that cannot
/// merge into an `IN` run (a compound key stays one OR-ed part per row) and the
/// deleted-rows rollback INSERT value tuples; merged `IN` runs are bounded by
/// `MYSQL_DATA_GRID_BATCH_MAX_VALUES` and by the text budget instead, so the
/// three budgets each cover a different shape.
const MYSQL_DATA_GRID_BATCH_MAX_ROWS: usize = 500;
const MYSQL_DATA_GRID_BATCH_TARGET_SQL_BYTES: usize = 256 * 1024;
/// Per-statement ceiling on the literal values a batched MySQL UPDATE/DELETE may
/// carry. MySQL charges the range optimizer ≈230 bytes per `IN` literal (every
/// `IN` value counts as a predicate combined with `OR`), so 10k values ≈ 2.3 MiB
/// of the 8 MiB default `range_optimizer_max_mem_size` — about 3.6× headroom.
/// The 256 KiB text budget alone is not enough: short integer keys are ≈7 bytes
/// of text each, so it would let a statement reach ≈8.6 MiB and silently abandon
/// the range scan (warning 3170, or a hard error under `sql_safe_updates`).
/// Keeping 500 values per flush would instead re-join the runs into the same
/// oversized statement, which is why the cap counts values rather than rows.
const MYSQL_DATA_GRID_BATCH_MAX_VALUES: usize = 10_000;
const ORACLE_SQL_LITERAL_MAX_BYTES: usize = 4000;
const ORACLE_LOB_LITERAL_CHUNK_BYTES: usize = 3900;
/// SQL Server types an `N'..' + NCHAR(n) + N'..'` chain of non-`max` operands as
/// `nvarchar(4000)`; a longer concatenation result is truncated without an error.
const SQLSERVER_NVARCHAR_LITERAL_MAX_CHARS: usize = 4000;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct DataGridTableMeta {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub catalog: Option<String>,
    /// Doris / StarRocks multi-catalog: the database under the external
    /// catalog, used as the middle segment of the 3-part qualified name.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub database: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub schema: Option<String>,
    pub table_name: String,
    #[serde(default)]
    pub primary_keys: Vec<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub columns: Option<Vec<DataGridColumnInfo>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
pub struct DataGridColumnInfo {
    pub name: String,
    #[serde(default)]
    pub data_type: String,
    #[serde(default)]
    pub is_nullable: bool,
    #[serde(default)]
    pub is_primary_key: bool,
    #[serde(default)]
    pub column_default: Option<String>,
    #[serde(default)]
    pub extra: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DataGridSaveStatementOptions {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub database_type: Option<DatabaseType>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub identifier_quote: Option<String>,
    /// Server version reported by the connection (for example `Neo4j/4.4.44`). Neo4j renamed the
    /// node identity function in 5.0, so the saved statements must address rows the same way the
    /// grid read them.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub server_version: Option<String>,
    pub table_meta: DataGridTableMeta,
    pub columns: Vec<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub source_columns: Option<Vec<Option<String>>>,
    #[serde(default)]
    pub rows: Vec<Vec<Value>>,
    #[serde(default)]
    pub dirty_rows: Vec<(usize, Vec<(usize, Value)>)>,
    #[serde(default)]
    pub deleted_rows: Vec<usize>,
    #[serde(default)]
    pub new_rows: Vec<Vec<Value>>,
    /// `生成 SQL 时包含数据库名`: qualify the saved table with its database on
    /// engines that address tables as `database.table` (MySQL family,
    /// ClickHouse). Off by default so existing statements are unchanged.
    #[serde(default)]
    pub include_database_name: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DataGridCopyUpdateStatementOptions {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub database_type: Option<DatabaseType>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub identifier_quote: Option<String>,
    pub table_meta: DataGridTableMeta,
    pub columns: Vec<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub source_columns: Option<Vec<Option<String>>>,
    #[serde(default)]
    pub rows: Vec<Vec<Value>>,
    /// `生成 SQL 时包含数据库名`: qualify the copied table the same way the
    /// save statements and the copy-as-INSERT statements do. Defaults to true so
    /// callers that never strip the table metadata keep the historical shape.
    #[serde(default = "default_include_database_name")]
    pub include_database_name: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DataGridCopyInsertStatementOptions {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub database_type: Option<DatabaseType>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub identifier_quote: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub table_meta: Option<DataGridTableMeta>,
    pub columns: Vec<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub column_types: Option<Vec<Option<String>>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub source_columns: Option<Vec<Option<String>>>,
    #[serde(default)]
    pub rows: Vec<Vec<Value>>,
    #[serde(default)]
    pub primary_key_mode: DataGridPrimaryKeyMode,
    #[serde(default)]
    pub include_computed_columns: bool,
    #[serde(default = "default_include_database_name")]
    pub include_database_name: bool,
    #[serde(default)]
    pub insert_mode: DataGridCopyInsertMode,
}

fn default_include_database_name() -> bool {
    true
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "kebab-case")]
pub enum DataGridCopyInsertMode {
    #[default]
    Merged,
    RowByRow,
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "kebab-case")]
pub enum DataGridPrimaryKeyMode {
    #[default]
    Include,
    ExcludeGenerated,
    ExcludeAll,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum DataGridContextFilterMode {
    Equals,
    NotEquals,
    IsNull,
    IsNotNull,
    IsBlank,
    IsNotBlank,
    Like,
    NotLike,
    BeginsWith,
    EndsWith,
    LessThan,
    LessThanOrEqual,
    GreaterThan,
    GreaterThanOrEqual,
    In,
    NotIn,
    Between,
    NotBetween,
}

fn supports_data_grid_context_filter_mode(
    database_type: Option<DatabaseType>,
    mode: DataGridContextFilterMode,
) -> bool {
    if database_type == Some(DatabaseType::VictoriaMetrics) {
        return false;
    }
    !matches!(
        (database_type, mode),
        (
            Some(DatabaseType::InfluxDb | DatabaseType::Cassandra | DatabaseType::Jdbc),
            DataGridContextFilterMode::In
                | DataGridContextFilterMode::NotIn
                | DataGridContextFilterMode::Between
                | DataGridContextFilterMode::NotBetween
        )
    )
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DataGridContextFilterConditionOptions {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub database_type: Option<DatabaseType>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub identifier_quote: Option<String>,
    pub column_name: String,
    pub mode: DataGridContextFilterMode,
    pub value: Value,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub values: Vec<Value>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub end_value: Option<Value>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub column_info: Option<DataGridColumnInfo>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DataGridColumnValueFilterConditionOptions {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub database_type: Option<DatabaseType>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub identifier_quote: Option<String>,
    pub column_name: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub column_info: Option<DataGridColumnInfo>,
    pub raw_value: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DataGridColumnValuesFilterConditionOptions {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub database_type: Option<DatabaseType>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub identifier_quote: Option<String>,
    pub column_name: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub column_info: Option<DataGridColumnInfo>,
    #[serde(default)]
    pub values: Vec<Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DataGridColumnDistinctValuesSqlOptions {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub database_type: Option<DatabaseType>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub driver_profile: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub identifier_quote: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub catalog: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub database: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub schema: Option<String>,
    pub table_name: String,
    pub column_name: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub column_info: Option<DataGridColumnInfo>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub where_input: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub search_value: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub limit: Option<usize>,
    #[serde(default)]
    pub include_counts: bool,
    #[serde(default)]
    pub exclude_nulls: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DataGridCountSqlOptions {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub database_type: Option<DatabaseType>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub identifier_quote: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub catalog: Option<String>,
    /// Doris / StarRocks multi-catalog: the database under the external
    /// catalog, used as the middle segment of the 3-part qualified name when
    /// `schema` is absent.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub database: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub schema: Option<String>,
    pub table_name: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub where_input: Option<String>,
    /// Optional optimizer hint injected between SELECT and the select list.
    /// Example: "/*+ set(query_dop 32) */" for GaussDB parallel COUNT(*).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub count_hint: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DataGridConditionalUpdateSqlOptions {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub database_type: Option<DatabaseType>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub identifier_quote: Option<String>,
    pub table_meta: DataGridTableMeta,
    pub column_name: String,
    pub value: Value,
    pub where_input: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HiveTablePropertiesSqlOptions {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub database_type: Option<DatabaseType>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub schema: Option<String>,
    pub table_name: String,
    pub property_name: String,
}

/// A server-side check that must pass before a keyless save may run.
///
/// Without a primary key a row is identified by matching every column value,
/// so the same predicate can match physical rows the loaded page never saw.
/// `sql` counts, on the server, how many rows one of the predicates this save
/// actually sends to the database matches. The save must be refused with
/// `message` unless the returned count is at most `max_matched_rows`, and also
/// refused when the count cannot be obtained at all — an unverified keyless
/// write is exactly the ambiguous write this guard exists to prevent.
///
/// A predicate built from a physical row locator (PostgreSQL `tableoid`/`ctid`)
/// also sets `min_matched_rows`: that locator only stays valid while the row is
/// unchanged, so a count of zero means the row moved and must be reported with
/// `min_message` instead of silently updating nothing.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DataGridSaveGuard {
    pub sql: String,
    pub max_matched_rows: u32,
    #[serde(default)]
    pub min_matched_rows: u32,
    pub message: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub min_message: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DataGridSavePreparation {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub validation_error: Option<String>,
    pub statements: Vec<String>,
    pub rollback_statements: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub execution_schema: Option<String>,
    /// Checks the caller must run — and pass — before executing `statements`.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub keyless_guards: Vec<DataGridSaveGuard>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub bound_statements: Vec<dbx_types::types::BlobBoundStatement>,
}

pub fn prepare_data_grid_save(options: DataGridSaveStatementOptions) -> DataGridSavePreparation {
    prepare_data_grid_save_for_driver_profile(options, None)
}

pub fn prepare_data_grid_save_for_driver_profile(
    options: DataGridSaveStatementOptions,
    driver_profile: Option<&str>,
) -> DataGridSavePreparation {
    let validation_error = validate_data_grid_save(&options);
    if validation_error.is_some() {
        return DataGridSavePreparation {
            validation_error,
            statements: Vec::new(),
            rollback_statements: Vec::new(),
            execution_schema: data_grid_save_execution_schema(
                options.database_type,
                driver_profile,
                &options.table_meta,
            ),
            keyless_guards: Vec::new(),
            bound_statements: Vec::new(),
        };
    }

    let mut keyless_guards = Vec::new();
    if options.database_type == Some(DatabaseType::Neo4j) {
        let generated = build_neo4j_data_grid_save_statements(&options).and_then(|statements| {
            build_neo4j_data_grid_rollback_statements(&options).map(|rollback| (statements, rollback))
        });
        let (validation_error, statements, rollback_statements) = match generated {
            Ok((statements, rollback)) => (None, statements, rollback),
            Err(error) => (Some(error), Vec::new(), Vec::new()),
        };
        return DataGridSavePreparation {
            validation_error,
            statements,
            rollback_statements,
            execution_schema: None,
            keyless_guards,
            bound_statements: Vec::new(),
        };
    }
    let mut bound_statements = Vec::new();
    let statements =
        build_data_grid_save_statements(&options, driver_profile, &mut keyless_guards, &mut bound_statements);
    if !bound_statements.iter().any(|statement| !statement.blob_parameters.is_empty()) {
        bound_statements.clear();
    }
    DataGridSavePreparation {
        validation_error: None,
        statements,
        rollback_statements: build_data_grid_rollback_statements(&options, driver_profile),
        execution_schema: data_grid_save_execution_schema(options.database_type, driver_profile, &options.table_meta),
        keyless_guards,
        bound_statements,
    }
}

/// Relational SQL UPDATE/WHERE predicates are not meaningful for graph,
/// document, or time-series stores that don't speak relational SQL.
pub fn supports_relational_copy_predicates(database_type: Option<DatabaseType>) -> bool {
    !matches!(database_type, Some(DatabaseType::Neo4j | DatabaseType::Tdengine | DatabaseType::MongoDb))
}

pub fn build_data_grid_copy_update_statements(options: DataGridCopyUpdateStatementOptions) -> Vec<String> {
    if !supports_relational_copy_predicates(options.database_type) {
        return Vec::new();
    }
    let primary_keys = &options.table_meta.primary_keys;
    if primary_keys.is_empty() {
        return Vec::new();
    }

    let save_columns = effective_copy_columns(options.source_columns.as_deref(), &options.columns);
    let column_info = options.table_meta.columns.as_deref().unwrap_or(&[]);
    let primary_key_indexes: Vec<Option<usize>> = primary_keys
        .iter()
        .map(|primary_key| find_column_index(options.database_type, &save_columns, primary_key))
        .collect();
    if primary_key_indexes.iter().any(Option::is_none) {
        return Vec::new();
    }
    let primary_key_indexes: Vec<usize> = primary_key_indexes.into_iter().flatten().collect();
    let primary_key_set: Vec<String> =
        primary_keys.iter().map(|primary_key| normalize_column_name(primary_key)).collect();
    let writable_indexes: Vec<(&str, usize, Option<&DataGridColumnInfo>)> = save_columns
        .iter()
        .enumerate()
        .filter_map(|(index, column)| Some((column.as_deref()?, index)))
        .filter(|(column, _)| !primary_key_set.contains(&normalize_column_name(column)))
        .filter(|(column, _)| !is_synthetic_row_id(options.database_type, Some(column)))
        .map(|(column, index)| (column, index, column_info_for(column_info, column)))
        .collect();
    let primary_key_info =
        primary_keys.iter().map(|primary_key| column_info_for(column_info, primary_key)).collect::<Vec<_>>();

    if writable_indexes
        .iter()
        .any(|(_, _, info)| info.is_some_and(|info| is_opaque_aggregate_state_type(&info.data_type)))
        || primary_key_info.iter().any(|info| info.is_some_and(|info| is_opaque_aggregate_state_type(&info.data_type)))
    {
        return Vec::new();
    }

    if writable_indexes.is_empty() {
        return Vec::new();
    }
    if options.rows.iter().any(|row| {
        writable_indexes.iter().any(|(_, index, info)| {
            is_oceanbase_blob_column(options.database_type, *info)
                && row.get(*index).is_some_and(|value| !value.is_null() && oceanbase_blob_hex(value).is_none())
        })
    }) {
        return Vec::new();
    }

    let table = data_grid_generated_table_name(
        options.database_type,
        options.table_meta.catalog.as_deref(),
        options.table_meta.schema.as_deref(),
        options.table_meta.database.as_deref(),
        &options.table_meta.table_name,
        options.identifier_quote.as_deref(),
        options.include_database_name,
    );
    let mut statements = Vec::new();
    for row in &options.rows {
        if primary_key_indexes.iter().any(|index| row.get(*index).unwrap_or(&Value::Null).is_null()) {
            continue;
        }
        let sets = writable_indexes
            .iter()
            .map(|(column, index, info)| {
                format!(
                    "{} = {}",
                    data_grid_identifier(options.database_type, column, options.identifier_quote.as_deref()),
                    format_grid_assignment_sql_literal(
                        row.get(*index).unwrap_or(&Value::Null),
                        options.database_type,
                        *info,
                        options.identifier_quote.as_deref(),
                    )
                )
            })
            .collect::<Vec<_>>()
            .join(", ");
        if sets.is_empty() {
            continue;
        }
        let where_clause = primary_keys
            .iter()
            .enumerate()
            .map(|(index, primary_key)| {
                build_column_predicate(
                    options.database_type,
                    primary_key,
                    row.get(primary_key_indexes[index]).unwrap_or(&Value::Null),
                    primary_key_info[index],
                    false,
                    options.identifier_quote.as_deref(),
                )
            })
            .collect::<Vec<_>>()
            .join(" AND ");
        statements.push(data_grid_statement(
            options.database_type,
            data_grid_update_sql(options.database_type, &table, &sets, &where_clause),
        ));
    }
    statements
}

pub fn build_data_grid_copy_insert_statement(options: DataGridCopyInsertStatementOptions) -> Option<String> {
    build_data_grid_copy_insert_statement_with_formatters(
        options,
        |reference| reference,
        format_grid_copy_insert_sql_literal,
    )
}

pub(crate) fn build_data_grid_copy_insert_statement_with_formatters(
    options: DataGridCopyInsertStatementOptions,
    format_reference: impl Fn(String) -> String,
    format_literal: impl Fn(&Value, Option<DatabaseType>, Option<&DataGridColumnInfo>, Option<&str>) -> String,
) -> Option<String> {
    build_data_grid_copy_insert_statements_with_formatters(options, format_reference, format_literal)
        .map(|statements| statements.join("\n"))
}

pub(crate) fn build_data_grid_copy_insert_statements_with_formatters(
    options: DataGridCopyInsertStatementOptions,
    format_reference: impl Fn(String) -> String,
    format_literal: impl Fn(&Value, Option<DatabaseType>, Option<&DataGridColumnInfo>, Option<&str>) -> String,
) -> Option<Vec<String>> {
    let save_columns = effective_copy_columns(options.source_columns.as_deref(), &options.columns);
    let column_info = options.table_meta.as_ref().and_then(|meta| meta.columns.as_deref()).unwrap_or(&[]);
    let primary_keys = options.table_meta.as_ref().map(|meta| meta.primary_keys.as_slice()).unwrap_or_default();
    let known_columns: Vec<String> = save_columns.iter().flatten().cloned().collect();
    let insertable_columns: Vec<(&str, usize, Option<DataGridColumnInfo>)> = save_columns
        .iter()
        .enumerate()
        .filter_map(|(index, column)| Some((column.as_deref()?, index)))
        .map(|(column, index)| {
            let fallback_type =
                options.column_types.as_deref().and_then(|types| types.get(index)).and_then(|value| value.as_deref());
            (column, index, copy_column_info(options.database_type, column_info, column, fallback_type))
        })
        .filter(|(column, _, info)| {
            !is_grid_insert_omitted_column(
                options.database_type,
                info.as_ref(),
                Some(column),
                options.include_computed_columns,
            )
        })
        .collect();
    let insert_columns: Vec<(&str, usize, Option<DataGridColumnInfo>)> = insertable_columns
        .iter()
        .filter(|(column, _, info)| {
            let is_primary_key =
                is_primary_key_column(options.database_type, primary_keys, column, column_info, &known_columns);
            match options.primary_key_mode {
                DataGridPrimaryKeyMode::Include => true,
                DataGridPrimaryKeyMode::ExcludeGenerated => {
                    !is_primary_key || !info.as_ref().is_some_and(is_auto_generated_column)
                }
                DataGridPrimaryKeyMode::ExcludeAll => !is_primary_key,
            }
        })
        .cloned()
        .collect();

    if insert_columns
        .iter()
        .any(|(_, _, info)| info.as_ref().is_some_and(|info| is_opaque_aggregate_state_type(&info.data_type)))
    {
        return None;
    }

    if insert_columns.is_empty() || options.rows.is_empty() {
        return None;
    }
    if options.rows.iter().any(|row| {
        insert_columns.iter().any(|(_, index, info)| {
            is_oceanbase_blob_column(options.database_type, info.as_ref())
                && row.get(*index).is_some_and(|value| !value.is_null() && oceanbase_blob_hex(value).is_none())
        })
    }) {
        return None;
    }

    let table = options.table_meta.as_ref().map_or_else(
        || "table_name".to_string(),
        |meta| {
            // This copy option controls every namespace prefix, including
            // SQLite's main schema and external catalogs.
            if !options.include_database_name {
                return data_grid_identifier(
                    options.database_type,
                    &meta.table_name,
                    options.identifier_quote.as_deref(),
                );
            }
            // MySQL-compatible engines can store the database in either field.
            let use_database_fallback = match options.database_type {
                Some(DatabaseType::Mysql | DatabaseType::Goldendb | DatabaseType::ClickHouse) => true,
                Some(DatabaseType::Doris | DatabaseType::StarRocks) => meta
                    .catalog
                    .as_deref()
                    .is_none_or(|catalog| catalog.trim().is_empty() || catalog.trim() == "internal"),
                _ => false,
            };
            // MySQL table tabs store the namespace in `database` without a
            // schema. Explicit schemas still take priority for cross-database
            // sources; schema-based engines must not use this fallback.
            let schema = if use_database_fallback {
                meta.schema
                    .as_deref()
                    .filter(|schema| !schema.trim().is_empty())
                    .or_else(|| meta.database.as_deref().filter(|database| !database.trim().is_empty()))
            } else {
                meta.schema.as_deref()
            };
            // ClickHouse (`database.table`) and SQL Server
            // (`database.schema.table`) address tables across databases on the
            // same connection, so the setting resolves the full name for them.
            if let Some(qualified) = crate::sql_dialect::database_qualified_table_name(
                options.database_type,
                meta.catalog.as_deref(),
                schema,
                meta.database.as_deref(),
                &meta.table_name,
            ) {
                return qualified;
            }
            data_grid_qualified_table_name(
                options.database_type,
                meta.catalog.as_deref(),
                schema,
                meta.database.as_deref(),
                &meta.table_name,
                options.identifier_quote.as_deref(),
            )
        },
    );
    let table = format_reference(table);
    let columns = insert_columns
        .iter()
        .map(|(_, index, _)| {
            format_reference(data_grid_identifier(
                options.database_type,
                &options.columns[*index],
                options.identifier_quote.as_deref(),
            ))
        })
        .collect::<Vec<_>>()
        .join(", ");
    let value_rows = options
        .rows
        .iter()
        .map(|row| {
            format!(
                "({})",
                insert_columns
                    .iter()
                    .map(|(_, index, info)| {
                        format_literal(
                            row.get(*index).unwrap_or(&Value::Null),
                            options.database_type,
                            info.as_ref(),
                            options.identifier_quote.as_deref(),
                        )
                    })
                    .collect::<Vec<_>>()
                    .join(", ")
            )
        })
        .collect::<Vec<_>>();
    let statements = if options.insert_mode == DataGridCopyInsertMode::RowByRow
        || options.database_type.is_some_and(uses_single_row_insert_statements)
    {
        value_rows
            .iter()
            .map(|values| {
                let sql = format!("INSERT INTO {table} ({columns}) VALUES {values}");
                if options.database_type == Some(DatabaseType::OceanbaseOracle) {
                    data_grid_statement(options.database_type, sql)
                } else {
                    format!("{sql};")
                }
            })
            .collect::<Vec<_>>()
    } else {
        vec![format!(
            "INSERT INTO {table} ({columns}) VALUES{}{};",
            if value_rows.len() == 1 { " " } else { "\n" },
            value_rows.join(",\n")
        )]
    };
    // SQL Server and Dameng reject explicit values for identity columns unless the
    // statement runs between `SET IDENTITY_INSERT <table> ON` and `OFF` (SQL Server
    // error 544), so a copied INSERT that carries the identity column must ship the
    // wrapper. Dameng AUTO_INCREMENT columns are not IDENTITY columns (DM error -2717)
    // and must not be wrapped. Each statement is wrapped on its own so row-by-row
    // copies stay individually executable, matching the SQL export path.
    let needs_identity_insert_wrapper = match options.database_type {
        Some(DatabaseType::SqlServer) => insert_columns.iter().any(|(_, _, info)| {
            info.as_ref().is_some_and(|col| {
                col.extra.as_deref().is_some_and(|extra| extra.to_ascii_lowercase().contains("identity"))
                    || is_auto_generated_column(col)
            })
        }),
        Some(DatabaseType::Dameng) => insert_columns.iter().any(|(_, _, info)| {
            info.as_ref().is_some_and(|col| {
                col.extra.as_deref().is_some_and(|extra| {
                    let normalized = extra.to_ascii_lowercase();
                    normalized.contains("identity")
                        && !normalized.contains("auto_increment")
                        && !normalized.contains("autoincrement")
                })
            })
        }),
        _ => false,
    };
    if needs_identity_insert_wrapper {
        return Some(
            statements
                .into_iter()
                .map(|statement| {
                    format!("SET IDENTITY_INSERT {table} ON;\n{statement}\nSET IDENTITY_INSERT {table} OFF;")
                })
                .collect(),
        );
    }
    Some(statements)
}

pub fn build_data_grid_context_filter_condition(options: DataGridContextFilterConditionOptions) -> Option<String> {
    if !supports_data_grid_context_filter_mode(options.database_type, options.mode) {
        return None;
    }

    let column = column_filter_ref(options.database_type, &options.column_name, options.identifier_quote.as_deref());
    let like_column = column_like_filter_ref(
        options.database_type,
        &options.column_name,
        options.column_info.as_ref(),
        options.identifier_quote.as_deref(),
    );
    let value = &options.value;
    match options.mode {
        DataGridContextFilterMode::IsNull => Some(format!("{column} IS NULL")),
        DataGridContextFilterMode::IsNotNull => Some(format!("{column} IS NOT NULL")),
        DataGridContextFilterMode::IsBlank
            if matches!(options.database_type, Some(DatabaseType::Oracle | DatabaseType::OceanbaseOracle)) =>
        {
            Some(format!("{column} IS NULL"))
        }
        DataGridContextFilterMode::IsNotBlank
            if matches!(options.database_type, Some(DatabaseType::Oracle | DatabaseType::OceanbaseOracle)) =>
        {
            Some(format!("{column} IS NOT NULL"))
        }
        DataGridContextFilterMode::IsBlank => Some(format!("({column} IS NULL OR {column} = '')")),
        DataGridContextFilterMode::IsNotBlank => Some(format!("({column} IS NOT NULL AND {column} <> '')")),
        DataGridContextFilterMode::Equals if value.is_null() => Some(format!("{column} IS NULL")),
        DataGridContextFilterMode::NotEquals if value.is_null() => Some(format!("{column} IS NOT NULL")),
        DataGridContextFilterMode::Like => Some(format!(
            "{like_column} LIKE {}",
            format_grid_sql_literal(
                &Value::String(format!("%{}%", value_to_filter_text(value))),
                options.database_type,
                None
            )
        )),
        DataGridContextFilterMode::NotLike => Some(format!(
            "{like_column} NOT LIKE {}",
            format_grid_sql_literal(
                &Value::String(format!("%{}%", value_to_filter_text(value))),
                options.database_type,
                None
            )
        )),
        DataGridContextFilterMode::BeginsWith => Some(format!(
            "{like_column} LIKE {}",
            format_grid_sql_literal(
                &Value::String(format!("{}%", value_to_filter_text(value))),
                options.database_type,
                None
            )
        )),
        DataGridContextFilterMode::EndsWith => Some(format!(
            "{like_column} LIKE {}",
            format_grid_sql_literal(
                &Value::String(format!("%{}", value_to_filter_text(value))),
                options.database_type,
                None
            )
        )),
        DataGridContextFilterMode::LessThan => Some(format!(
            "{column} < {}",
            format_data_grid_context_filter_literal(
                value,
                options.database_type,
                &options.column_name,
                options.column_info.as_ref(),
                options.identifier_quote.as_deref(),
            )
        )),
        DataGridContextFilterMode::LessThanOrEqual => Some(format!(
            "{column} <= {}",
            format_data_grid_context_filter_literal(
                value,
                options.database_type,
                &options.column_name,
                options.column_info.as_ref(),
                options.identifier_quote.as_deref(),
            )
        )),
        DataGridContextFilterMode::GreaterThan => Some(format!(
            "{column} > {}",
            format_data_grid_context_filter_literal(
                value,
                options.database_type,
                &options.column_name,
                options.column_info.as_ref(),
                options.identifier_quote.as_deref(),
            )
        )),
        DataGridContextFilterMode::GreaterThanOrEqual => Some(format!(
            "{column} >= {}",
            format_data_grid_context_filter_literal(
                value,
                options.database_type,
                &options.column_name,
                options.column_info.as_ref(),
                options.identifier_quote.as_deref(),
            )
        )),
        DataGridContextFilterMode::In => build_data_grid_context_membership_filter_condition(
            &column,
            &options.values,
            options.database_type,
            &options.column_name,
            options.column_info.as_ref(),
            options.identifier_quote.as_deref(),
            false,
        ),
        DataGridContextFilterMode::NotIn => build_data_grid_context_membership_filter_condition(
            &column,
            &options.values,
            options.database_type,
            &options.column_name,
            options.column_info.as_ref(),
            options.identifier_quote.as_deref(),
            true,
        ),
        DataGridContextFilterMode::Between => build_data_grid_context_range_filter_condition(
            &column,
            value,
            options.end_value.as_ref(),
            options.database_type,
            &options.column_name,
            options.column_info.as_ref(),
            options.identifier_quote.as_deref(),
            false,
        ),
        DataGridContextFilterMode::NotBetween => build_data_grid_context_range_filter_condition(
            &column,
            value,
            options.end_value.as_ref(),
            options.database_type,
            &options.column_name,
            options.column_info.as_ref(),
            options.identifier_quote.as_deref(),
            true,
        ),
        DataGridContextFilterMode::Equals => Some(format!(
            "{column} = {}",
            format_data_grid_context_filter_literal(
                value,
                options.database_type,
                &options.column_name,
                options.column_info.as_ref(),
                options.identifier_quote.as_deref(),
            )
        )),
        DataGridContextFilterMode::NotEquals => Some(format!(
            "{column} <> {}",
            format_data_grid_context_filter_literal(
                value,
                options.database_type,
                &options.column_name,
                options.column_info.as_ref(),
                options.identifier_quote.as_deref(),
            )
        )),
    }
}

fn format_data_grid_context_filter_literal(
    value: &Value,
    database_type: Option<DatabaseType>,
    column_name: &str,
    column_info: Option<&DataGridColumnInfo>,
    identifier_quote: Option<&str>,
) -> String {
    if database_type == Some(DatabaseType::Iotdb) && column_info.is_none() && column_name.eq_ignore_ascii_case("time") {
        if let Some(value) = value.as_str().filter(|value| is_decimal_i64(value)) {
            return value.to_string();
        }
    }
    format_grid_sql_literal_with_identifier_quote(value, database_type, column_info, identifier_quote)
}

fn is_decimal_i64(value: &str) -> bool {
    let digits = value.strip_prefix('-').unwrap_or(value);
    !digits.is_empty() && digits.bytes().all(|byte| byte.is_ascii_digit()) && value.parse::<i64>().is_ok()
}

fn build_data_grid_context_membership_filter_condition(
    column: &str,
    values: &[Value],
    database_type: Option<DatabaseType>,
    column_name: &str,
    column_info: Option<&DataGridColumnInfo>,
    identifier_quote: Option<&str>,
    negated: bool,
) -> Option<String> {
    if values.is_empty() {
        return None;
    }

    let mut has_null = false;
    let mut literals = Vec::new();
    let mut seen_literals = HashSet::new();
    for value in values {
        if value.is_null() {
            has_null = true;
            continue;
        }
        let literal =
            format_data_grid_context_filter_literal(value, database_type, column_name, column_info, identifier_quote);
        if seen_literals.insert(literal.clone()) {
            literals.push(literal);
        }
    }

    let membership = build_membership_predicate(column, &literals, database_type, negated);

    if negated {
        return match membership {
            Some(membership) => Some(format!("({column} IS NOT NULL AND {membership})")),
            None if has_null => Some(format!("{column} IS NOT NULL")),
            None => None,
        };
    }

    match membership {
        Some(membership) if has_null => Some(format!("({column} IS NULL OR {membership})")),
        Some(membership) => Some(membership),
        None if has_null => Some(format!("{column} IS NULL")),
        None => None,
    }
}

fn build_membership_predicate(
    column: &str,
    literals: &[String],
    database_type: Option<DatabaseType>,
    negated: bool,
) -> Option<String> {
    if literals.is_empty() {
        return None;
    }
    if database_type == Some(DatabaseType::Neo4j) {
        let predicate = format!("{column} IN [{}]", literals.join(", "));
        return Some(if negated { format!("NOT ({predicate})") } else { predicate });
    }

    let operator = if negated { "NOT IN" } else { "IN" };
    if database_type != Some(DatabaseType::Oracle) || literals.len() <= 1000 {
        return Some(format!("{column} {operator} ({})", literals.join(", ")));
    }

    // Oracle limits each IN expression to 1000 values; preserve NOT IN semantics
    // by joining its chunks with AND instead of OR.
    let joiner = if negated { " AND " } else { " OR " };
    let chunks =
        literals.chunks(1000).map(|chunk| format!("{column} {operator} ({})", chunk.join(", "))).collect::<Vec<_>>();
    Some(format!("({})", chunks.join(joiner)))
}

fn build_data_grid_context_range_filter_condition(
    column: &str,
    start_value: &Value,
    end_value: Option<&Value>,
    database_type: Option<DatabaseType>,
    column_name: &str,
    column_info: Option<&DataGridColumnInfo>,
    identifier_quote: Option<&str>,
    negated: bool,
) -> Option<String> {
    let end_value = end_value?;
    if start_value.is_null() || end_value.is_null() {
        return None;
    }
    let start =
        format_data_grid_context_filter_literal(start_value, database_type, column_name, column_info, identifier_quote);
    let end =
        format_data_grid_context_filter_literal(end_value, database_type, column_name, column_info, identifier_quote);
    if database_type == Some(DatabaseType::Neo4j) {
        return if negated {
            Some(format!("({column} < {start} OR {column} > {end})"))
        } else {
            Some(format!("({column} >= {start} AND {column} <= {end})"))
        };
    }
    let operator = if negated { "NOT BETWEEN" } else { "BETWEEN" };
    Some(format!("{column} {operator} {start} AND {end}"))
}

pub fn build_data_grid_column_value_filter_condition(
    options: DataGridColumnValueFilterConditionOptions,
) -> Option<String> {
    let text = options.raw_value.trim();
    if text.is_empty() {
        return None;
    }
    let column = column_filter_ref(options.database_type, &options.column_name, options.identifier_quote.as_deref());
    if text.eq_ignore_ascii_case("null") {
        return Some(format!("{column} IS NULL"));
    }
    let value = parse_typed_filter_value(text, options.database_type, options.column_info.as_ref());
    Some(format!("{column} = {}", format_grid_sql_literal(&value, options.database_type, options.column_info.as_ref())))
}

pub fn build_data_grid_column_values_filter_condition(
    options: DataGridColumnValuesFilterConditionOptions,
) -> Option<String> {
    if options.values.is_empty() {
        return None;
    }

    let column = column_filter_ref(options.database_type, &options.column_name, options.identifier_quote.as_deref());
    let mut has_null = false;
    let mut literals = Vec::new();
    let mut seen_literals = HashSet::new();
    for value in &options.values {
        if value.is_null() {
            has_null = true;
            continue;
        }
        let literal = format_grid_sql_literal(value, options.database_type, options.column_info.as_ref());
        if seen_literals.insert(literal.clone()) {
            literals.push(literal);
        }
    }

    let mut predicates = Vec::new();
    if has_null {
        predicates.push(format!("{column} IS NULL"));
    }
    if literals.len() == 1 {
        predicates.push(format!("{column} = {}", literals[0]));
    } else if let Some(membership) = build_membership_predicate(&column, &literals, options.database_type, false) {
        predicates.push(membership);
    }

    match predicates.len() {
        0 => None,
        1 => predicates.into_iter().next(),
        _ => Some(format!("({})", predicates.join(" OR "))),
    }
}

pub fn build_data_grid_column_distinct_values_sql(options: DataGridColumnDistinctValuesSqlOptions) -> String {
    if options.database_type == Some(DatabaseType::Neo4j) {
        return build_neo4j_data_grid_column_distinct_values_sql(&options);
    }

    let limit = data_grid_column_distinct_values_limit(options.limit);
    let table = data_grid_qualified_table_name(
        options.database_type,
        options.catalog.as_deref(),
        options.schema.as_deref(),
        options.database.as_deref(),
        &options.table_name,
        options.identifier_quote.as_deref(),
    );
    let column = column_filter_ref(options.database_type, &options.column_name, options.identifier_quote.as_deref());
    let mut predicates = Vec::new();
    let predicate = crate::sql_dialect::normalize_where_input(options.where_input.as_deref());
    if !predicate.is_empty() {
        predicates.push(format!("({predicate})"));
    }
    if options.exclude_nulls {
        predicates.push(format!("{column} IS NOT NULL"));
    }
    if let Some(search_predicate) = data_grid_column_distinct_values_search_predicate(&options) {
        predicates.push(search_predicate);
    }
    let where_clause =
        if predicates.is_empty() { String::new() } else { format!(" WHERE {}", predicates.join(" AND ")) };
    let select_list = if options.include_counts {
        format!("{column} AS dbx_value, COUNT(*) AS dbx_count")
    } else {
        format!("{column} AS dbx_value")
    };
    let group_by = format!(" GROUP BY {column}");
    let order_by = if options.include_counts { " ORDER BY dbx_count DESC, dbx_value" } else { " ORDER BY dbx_value" };
    let from_clause = format!(" FROM {table}{where_clause}{group_by}{order_by}");

    match table_pagination_strategy(options.database_type) {
        TablePaginationStrategy::SqlServerTop if is_sqlserver_legacy_profile(options.driver_profile.as_deref()) => {
            format!("SELECT {select_list}{from_clause}")
        }
        TablePaginationStrategy::SqlServerTop => format!("SELECT TOP ({limit}) {select_list}{from_clause}"),
        TablePaginationStrategy::IrisTop => format!("SELECT TOP {limit} {select_list}{from_clause}"),
        TablePaginationStrategy::InformixFirst => format!("SELECT FIRST {limit} {select_list}{from_clause}"),
        TablePaginationStrategy::FirebirdRows => {
            let rows = firebird_rows_clause(limit, 0);
            format!("SELECT {select_list}{from_clause} {rows}")
        }
        TablePaginationStrategy::Db2FetchFirst | TablePaginationStrategy::FetchFirst => {
            format!("SELECT {select_list}{from_clause} FETCH FIRST {limit} ROWS ONLY")
        }
        TablePaginationStrategy::Rownum => {
            let inner = format!("SELECT {select_list}{from_clause}");
            format!("SELECT * FROM ({inner}) WHERE ROWNUM <= {limit}")
        }
        TablePaginationStrategy::AgentMaxRows | TablePaginationStrategy::Unbounded => {
            format!("SELECT {select_list}{from_clause}")
        }
        TablePaginationStrategy::QuestDbLimit | TablePaginationStrategy::LimitOffset => {
            format!("SELECT {select_list}{from_clause} LIMIT {limit}")
        }
    }
}

fn is_sqlserver_legacy_profile(driver_profile: Option<&str>) -> bool {
    driver_profile.is_some_and(|profile| profile.trim().eq_ignore_ascii_case("sqlserver-legacy"))
}

pub fn build_data_grid_count_sql(options: DataGridCountSqlOptions) -> String {
    if options.database_type == Some(DatabaseType::Neo4j) {
        let label = quote_ident(Some(DatabaseType::Neo4j), &options.table_name);
        let predicate = crate::sql_dialect::normalize_where_input(options.where_input.as_deref());
        let where_clause = if predicate.is_empty() { String::new() } else { format!(" WHERE ({predicate})") };
        return format!("MATCH (n:{label}){where_clause} RETURN count(n) AS cnt");
    }
    // Keep the reference identical to the one the grid's SELECT uses: Caché/IRIS
    // reject quoted ordinary names when delimited identifiers are disabled, so
    // the count must not be the only statement that quotes them (#8929).
    let table = data_grid_qualified_table_name(
        options.database_type,
        options.catalog.as_deref(),
        options.schema.as_deref(),
        options.database.as_deref(),
        &options.table_name,
        options.identifier_quote.as_deref(),
    );
    let predicate = crate::sql_dialect::normalize_where_input(options.where_input.as_deref());
    let where_clause = if predicate.is_empty() { String::new() } else { format!(" WHERE ({predicate})") };
    let hint = options.count_hint.as_deref().unwrap_or("");
    let hint_part = if hint.is_empty() { String::new() } else { format!(" {hint}") };
    format!("SELECT{hint_part} COUNT(*) AS cnt FROM {table}{where_clause}")
}

pub fn build_data_grid_conditional_update_sql(options: DataGridConditionalUpdateSqlOptions) -> Option<String> {
    if options.database_type != Some(DatabaseType::Mysql) {
        return None;
    }
    let predicate = crate::sql_dialect::normalize_where_input(Some(&options.where_input));
    if predicate.is_empty() {
        return None;
    }

    let columns = options.table_meta.columns.as_deref()?;
    let column_info = column_info_for(columns, &options.column_name)?;
    let primary_key_set: Vec<String> =
        options.table_meta.primary_keys.iter().map(|primary_key| normalize_column_name(primary_key)).collect();
    if primary_key_set.contains(&normalize_column_name(&column_info.name))
        || column_info.is_primary_key
        || is_grid_update_omitted_column(
            options.database_type,
            Some(column_info),
            Some(&column_info.name),
            &primary_key_set,
        )
    {
        return None;
    }

    let table = data_grid_qualified_table_name(
        options.database_type,
        options.table_meta.catalog.as_deref(),
        options.table_meta.schema.as_deref(),
        options.table_meta.database.as_deref(),
        &options.table_meta.table_name,
        options.identifier_quote.as_deref(),
    );
    let sets = format!(
        "{} = {}",
        data_grid_identifier(options.database_type, &column_info.name, options.identifier_quote.as_deref()),
        format_grid_save_sql_literal(
            &options.value,
            options.database_type,
            Some(column_info),
            options.identifier_quote.as_deref(),
        )
    );
    Some(data_grid_statement(
        options.database_type,
        data_grid_update_sql(options.database_type, &table, &sets, &format!("({predicate})")),
    ))
}

pub fn build_hive_table_properties_sql(options: HiveTablePropertiesSqlOptions) -> String {
    if options.database_type == Some(DatabaseType::Transwarp)
        && options.property_name.eq_ignore_ascii_case("transactional")
    {
        let database = options.schema.as_deref().unwrap_or("default").replace('\'', "''");
        let table = options.table_name.replace('\'', "''");
        return format!(
            "SELECT transactional FROM system.tables_v WHERE database_name = '{database}' AND table_name = '{table}'"
        );
    }
    let table = qualified_table_name(Some(DatabaseType::Hive), options.schema.as_deref(), &options.table_name);
    let property = options.property_name.replace('\'', "''");
    format!("SHOW TBLPROPERTIES {table} ('{property}')")
}

fn data_grid_column_distinct_values_limit(limit: Option<usize>) -> usize {
    limit.unwrap_or(DATA_GRID_COLUMN_DISTINCT_VALUES_DEFAULT_LIMIT).clamp(1, DATA_GRID_COLUMN_DISTINCT_VALUES_MAX_LIMIT)
}

fn data_grid_column_distinct_values_search_predicate(
    options: &DataGridColumnDistinctValuesSqlOptions,
) -> Option<String> {
    let search = options.search_value.as_deref()?.trim();
    if search.is_empty() {
        return None;
    }
    if !options.column_info.as_ref().map(|column| is_textual_column_type(&column.data_type)).unwrap_or(true)
        && !is_postgres_like_pattern_database(options.database_type)
    {
        let column =
            column_filter_ref(options.database_type, &options.column_name, options.identifier_quote.as_deref());
        let value = parse_typed_filter_value(search, options.database_type, options.column_info.as_ref());
        return Some(format!(
            "{column} = {}",
            format_grid_sql_literal(&value, options.database_type, options.column_info.as_ref())
        ));
    }
    let column = column_like_filter_ref(
        options.database_type,
        &options.column_name,
        options.column_info.as_ref(),
        options.identifier_quote.as_deref(),
    );
    let pattern = Value::String(format!("%{search}%"));
    Some(format!("{column} LIKE {}", format_grid_sql_literal(&pattern, options.database_type, None)))
}

fn build_neo4j_data_grid_column_distinct_values_sql(options: &DataGridColumnDistinctValuesSqlOptions) -> String {
    let limit = data_grid_column_distinct_values_limit(options.limit);
    let label = quote_ident(Some(DatabaseType::Neo4j), &options.table_name);
    let column = column_filter_ref(Some(DatabaseType::Neo4j), &options.column_name, None);
    let mut predicates = Vec::new();
    let predicate = crate::sql_dialect::normalize_where_input(options.where_input.as_deref());
    if !predicate.is_empty() {
        predicates.push(predicate);
    }
    if options.exclude_nulls {
        predicates.push(format!("{column} IS NOT NULL"));
    }
    if let Some(search) = options.search_value.as_deref().map(str::trim).filter(|value| !value.is_empty()) {
        predicates.push(format!(
            "toString({column}) CONTAINS {}",
            format_grid_sql_literal(&Value::String(search.to_string()), Some(DatabaseType::Neo4j), None)
        ));
    }
    let where_clause =
        if predicates.is_empty() { String::new() } else { format!(" WHERE {}", predicates.join(" AND ")) };
    if options.include_counts {
        format!(
            "MATCH (n:{label}){where_clause} RETURN {column} AS dbx_value, count(*) AS dbx_count ORDER BY dbx_count DESC, dbx_value LIMIT {limit}"
        )
    } else {
        format!(
            "MATCH (n:{label}){where_clause} RETURN DISTINCT {column} AS dbx_value ORDER BY dbx_value LIMIT {limit}"
        )
    }
}

fn validate_data_grid_save(options: &DataGridSaveStatementOptions) -> Option<String> {
    if let Some(error) = validate_oceanbase_blob_save(options) {
        return Some(error);
    }
    if let Some(error) = validate_opaque_aggregate_state_write(options) {
        return Some(error);
    }
    if let Some(error) = validate_salesforce_id_column(options) {
        return Some(error);
    }
    if let Some(error) = validate_iotdb_existing_rows(options) {
        return Some(error);
    }
    if let Some(error) = validate_tdengine_inserted_rows(options) {
        return Some(error);
    }
    if let Some(error) = validate_inserted_primary_keys(options) {
        return Some(error);
    }
    if let Some(error) = validate_tdengine_existing_rows(options) {
        return Some(error);
    }
    if let Some(error) = validate_existing_row_primary_keys(options) {
        return Some(error);
    }
    if let Some(error) = validate_postgres_ctid_row_identifier(options) {
        return Some(error);
    }
    if let Some(error) = validate_oracle_keyless_lob_predicate(options) {
        return Some(error);
    }
    if let Some(error) = validate_dameng_keyless_binary_lob_predicate(options) {
        return Some(error);
    }
    if let Some(error) = validate_keyless_row_predicate(options) {
        return Some(error);
    }

    let save_columns = effective_columns(options);
    let not_null_columns: Vec<String> = options
        .table_meta
        .columns
        .as_deref()
        .unwrap_or(&[])
        .iter()
        .filter(|column| {
            !column.is_nullable
                && column.column_default.is_none()
                && !is_auto_generated_column(column)
                && !is_non_identity_generated_column(Some(column))
                && !is_synthetic_row_id(options.database_type, Some(&column.name))
        })
        .map(|column| normalize_column_name(&column.name))
        .collect();
    if let Some(error) = validate_clickhouse_mutable_updates(options) {
        return Some(error);
    }

    if not_null_columns.is_empty() {
        return None;
    }

    for (_, changes) in &options.dirty_rows {
        for (column_index, value) in changes {
            let source_column = save_columns.get(*column_index).and_then(|column| column.as_deref());
            if is_null_write_to_not_null_column(options.database_type, &not_null_columns, source_column, value) {
                return Some(null_write_error(source_column.unwrap_or_default()));
            }
        }
    }

    // MySQL BEFORE INSERT triggers can populate omitted NOT NULL columns. New-row NULL values are
    // omitted from the generated INSERT, so let MySQL apply triggers or report missing required fields.
    if options.database_type != Some(DatabaseType::Mysql) {
        for row in &options.new_rows {
            for column_index in 0..options.columns.len() {
                let source_column = save_columns.get(column_index).and_then(|column| column.as_deref());
                if is_null_write_to_not_null_column(
                    options.database_type,
                    &not_null_columns,
                    source_column,
                    row.get(column_index).unwrap_or(&Value::Null),
                ) {
                    return Some(null_write_error(source_column.unwrap_or_default()));
                }
            }
        }
    }

    None
}

fn validate_opaque_aggregate_state_write(options: &DataGridSaveStatementOptions) -> Option<String> {
    let save_columns = effective_columns(options);
    let column_info = options.table_meta.columns.as_deref().unwrap_or(&[]);
    let opaque_indexes = save_columns
        .iter()
        .enumerate()
        .filter_map(|(index, column)| {
            let column = column.as_deref()?;
            column_info_for(column_info, column)
                .is_some_and(|info| is_opaque_aggregate_state_type(&info.data_type))
                .then_some(index)
        })
        .collect::<HashSet<_>>();
    if opaque_indexes.is_empty() {
        return None;
    }
    if options.dirty_rows.iter().any(|(_, changes)| changes.iter().any(|(index, _)| opaque_indexes.contains(index)))
        || options
            .new_rows
            .iter()
            .any(|row| opaque_indexes.iter().any(|index| row.get(*index).is_some_and(|value| !value.is_null())))
    {
        return Some("Doris aggregate-state columns are opaque and cannot be written automatically; use an explicit Doris state function instead.".to_string());
    }
    if options.table_meta.primary_keys.is_empty()
        && (!options.dirty_rows.is_empty() || !options.deleted_rows.is_empty())
        && options.dirty_rows.iter().map(|(index, _)| *index).chain(options.deleted_rows.iter().copied()).any(
            |row_index| {
                options.rows.get(row_index).is_some_and(|row| {
                    opaque_indexes.iter().any(|index| row.get(*index).is_some_and(|value| !value.is_null()))
                })
            },
        )
    {
        return Some("Cannot safely update or delete a keyless row whose predicate would contain an opaque Doris aggregate-state value.".to_string());
    }
    None
}

fn validate_existing_row_primary_keys(options: &DataGridSaveStatementOptions) -> Option<String> {
    let primary_keys = &options.table_meta.primary_keys;
    if primary_keys.is_empty() || (options.dirty_rows.is_empty() && options.deleted_rows.is_empty()) {
        return None;
    }

    let save_columns = effective_columns(options);
    let primary_key_indexes: Vec<Option<usize>> = primary_keys
        .iter()
        .map(|primary_key| find_column_index(options.database_type, &save_columns, primary_key))
        .collect();
    let missing_primary_keys = primary_keys
        .iter()
        .zip(&primary_key_indexes)
        .filter_map(|(primary_key, index)| index.is_none().then_some(primary_key.as_str()))
        .collect::<Vec<_>>();
    if !missing_primary_keys.is_empty() {
        return Some(format!(
            "Cannot safely update or delete rows because the query result does not include every primary key column (missing: {}). Refresh or rerun the query before saving.",
            missing_primary_keys.join(", ")
        ));
    }

    let primary_key_indexes = primary_key_indexes.into_iter().flatten().collect::<Vec<_>>();
    for row_index in
        options.dirty_rows.iter().map(|(row_index, _)| *row_index).chain(options.deleted_rows.iter().copied())
    {
        let Some(row) = options.rows.get(row_index) else {
            continue;
        };
        if let Some((primary_key, _)) =
            primary_keys.iter().zip(&primary_key_indexes).find(|(_, index)| row.get(**index).is_none_or(Value::is_null))
        {
            return Some(format!(
                "Cannot safely update or delete rows because primary key column \"{primary_key}\" has no value in the query result. Refresh or rerun the query before saving."
            ));
        }
    }

    None
}

fn validate_postgres_ctid_row_identifier(options: &DataGridSaveStatementOptions) -> Option<String> {
    if !uses_postgres_ctid(options.database_type)
        || !options.table_meta.primary_keys.is_empty()
        || (options.dirty_rows.is_empty() && options.deleted_rows.is_empty())
    {
        return None;
    }

    let save_columns = effective_columns(options);
    // Without the hidden locator in the projection there is nothing to check;
    // the save falls back to its previous keyless behaviour.
    let row_id_index = find_column_index(options.database_type, &save_columns, DBX_ROWID_COLUMN)?;
    for row_index in
        options.dirty_rows.iter().map(|(row_index, _)| *row_index).chain(options.deleted_rows.iter().copied())
    {
        let Some(row) = options.rows.get(row_index) else {
            continue;
        };
        if row.get(row_id_index).and_then(parse_postgres_ctid_row_id).is_none() {
            return Some(POSTGRES_CTID_UNIDENTIFIABLE_ROW_ERROR.to_string());
        }
    }
    None
}

fn validate_oracle_keyless_lob_predicate(options: &DataGridSaveStatementOptions) -> Option<String> {
    if !uses_oracle_row_id(options.database_type)
        || !options.table_meta.primary_keys.is_empty()
        || (options.dirty_rows.is_empty() && options.deleted_rows.is_empty())
    {
        return None;
    }
    let has_lob_column =
        options.table_meta.columns.as_deref().unwrap_or(&[]).iter().any(|column| is_oracle_lob_type(&column.data_type));
    if !has_lob_column {
        return None;
    }

    // LOB equality is unsupported in Oracle-compatible SQL. Refuse unsafe
    // keyless writes instead of dropping LOB predicates and risking extra rows.
    Some("Cannot safely update or delete this Oracle-compatible row because the table has LOB columns but no primary key or ROWID identifier.".to_string())
}

/// Dameng refuses `=` and `LIKE` against binary LOB columns with a bare
/// `Data type mismatch`, so a non-NULL binary LOB value can never appear in the
/// keyless row predicate built from every result column. Spatial columns
/// (`SYSGEO2.ST_GEOMETRY` and friends) are BLOB-backed and fail the same way.
/// Refuse the write with an actionable message instead of sending SQL the
/// server rejects. NULL values stay editable because they only need `IS NULL`,
/// and textual LOBs (`CLOB`, `TEXT`) compare fine and are left alone.
fn validate_dameng_keyless_binary_lob_predicate(options: &DataGridSaveStatementOptions) -> Option<String> {
    if options.database_type != Some(DatabaseType::Dameng)
        || !options.table_meta.primary_keys.is_empty()
        || (options.dirty_rows.is_empty() && options.deleted_rows.is_empty())
    {
        return None;
    }
    let save_columns = effective_columns(options);
    let column_info = options.table_meta.columns.as_deref().unwrap_or(&[]);
    let touched_row_indexes =
        options.dirty_rows.iter().map(|(row_index, _)| *row_index).chain(options.deleted_rows.iter().copied());
    for row_index in touched_row_indexes {
        let Some(row) = options.rows.get(row_index) else {
            continue;
        };
        let compares_binary_lob = save_columns.iter().enumerate().any(|(index, column)| {
            let Some(column) = column.as_deref() else {
                return false;
            };
            if is_synthetic_row_id(options.database_type, Some(column)) || row.get(index).is_none_or(Value::is_null) {
                return false;
            }
            column_info_for(column_info, column).is_some_and(|info| is_dameng_uncomparable_lob_type(&info.data_type))
        });
        if compares_binary_lob {
            return Some(DAMENG_KEYLESS_BINARY_LOB_ERROR.to_string());
        }
    }
    None
}

/// Without a primary key a row can only be addressed by matching every column
/// value, and `build_row_where` drops columns that have no source column of
/// their own. When nothing is left, the generated predicate is empty and the
/// UPDATE/DELETE would target the whole table, so there is neither a reliable
/// row identifier nor anything a server-side check could count: refuse.
///
/// Whether a non-empty predicate really addresses a single physical row cannot
/// be decided here — the loaded page is not the table. That decision belongs to
/// the `keyless_guards` this preparation emits, which count the matches of the
/// exact predicate on the server.
fn validate_keyless_row_predicate(options: &DataGridSaveStatementOptions) -> Option<String> {
    if !options.table_meta.primary_keys.is_empty() || !uses_keyless_row_predicate(options.database_type) {
        return None;
    }
    let save_columns = effective_columns(options);
    let column_info = options.table_meta.columns.as_deref().unwrap_or(&[]);
    let touched_row_indexes =
        options.dirty_rows.iter().map(|(row_index, _)| *row_index).chain(options.deleted_rows.iter().copied());
    for row_index in touched_row_indexes {
        let Some(row) = options.rows.get(row_index) else {
            continue;
        };
        let predicate = build_row_where(
            options.database_type,
            &save_columns,
            row,
            column_info,
            options.identifier_quote.as_deref(),
        );
        if predicate.trim().is_empty() {
            return Some(KEYLESS_UNIDENTIFIABLE_ROW_ERROR.to_string());
        }
    }
    None
}

fn validate_clickhouse_mutable_updates(options: &DataGridSaveStatementOptions) -> Option<String> {
    if options.database_type != Some(DatabaseType::ClickHouse) || options.dirty_rows.is_empty() {
        return None;
    }
    let save_columns = effective_columns(options);
    let column_info = options.table_meta.columns.as_deref().unwrap_or(&[]);
    let primary_key_set: Vec<String> =
        options.table_meta.primary_keys.iter().map(|primary_key| normalize_column_name(primary_key)).collect();
    let has_clickhouse_key_metadata = !primary_key_set.is_empty()
        || column_info.iter().any(|column| is_clickhouse_partition_key_column(options.database_type, Some(column)));
    if !has_clickhouse_key_metadata {
        return None;
    }

    for (_, changes) in &options.dirty_rows {
        if changes.is_empty() {
            continue;
        }
        let has_mutable_column = changes.iter().any(|(column_index, _)| {
            let Some(column) = save_columns.get(*column_index).and_then(|column| column.as_deref()) else {
                return false;
            };
            !is_grid_update_omitted_column(
                options.database_type,
                column_info_for(column_info, column),
                Some(column),
                &primary_key_set,
            )
        });
        if !has_mutable_column {
            return Some(clickhouse_no_mutable_columns_error());
        }
    }
    None
}

fn validate_inserted_primary_keys(options: &DataGridSaveStatementOptions) -> Option<String> {
    let primary_keys = &options.table_meta.primary_keys;
    if primary_keys.is_empty() || options.new_rows.is_empty() {
        return None;
    }

    let save_columns = effective_columns(options);
    let primary_key_indexes: Vec<Option<usize>> = primary_keys
        .iter()
        .map(|primary_key| find_column_index(options.database_type, &save_columns, primary_key))
        .collect();
    if primary_key_indexes.iter().any(Option::is_none) {
        return None;
    }
    let primary_key_indexes: Vec<usize> = primary_key_indexes.into_iter().flatten().collect();

    let mut existing_keys: Vec<String> = Vec::new();
    for row in &options.rows {
        if let Some(key) = primary_key_value_key(&primary_key_indexes, row) {
            existing_keys.push(key);
        }
    }

    let mut new_keys: Vec<String> = Vec::new();
    for row in &options.new_rows {
        let Some(key) = primary_key_value_key(&primary_key_indexes, row) else {
            continue;
        };
        if existing_keys.contains(&key) || new_keys.contains(&key) {
            return Some(duplicate_primary_key_error(
                primary_keys,
                &primary_key_indexes,
                row,
                existing_keys.contains(&key),
            ));
        }
        new_keys.push(key);
    }

    None
}

/// One predicate this save sends, plus whether it addresses a physical row
/// locator that must still match a row rather than merely "at most one".
struct GuardedPredicate {
    sql: String,
    requires_match: bool,
}

/// Builds the statements the save executes, and alongside them the server-side
/// guards for every keyless predicate those statements actually send. The guard
/// is derived from the same `where_clause` value the UPDATE/DELETE carries, so
/// the safety decision can never be made against a different set of columns or
/// values than the mutation itself uses.
fn build_data_grid_save_statements(
    options: &DataGridSaveStatementOptions,
    driver_profile: Option<&str>,
    keyless_guards: &mut Vec<DataGridSaveGuard>,
    bound_statements: &mut Vec<dbx_types::types::BlobBoundStatement>,
) -> Vec<String> {
    if options.database_type == Some(DatabaseType::Tdengine) {
        return build_tdengine_data_grid_save_statements(options);
    }
    if options.database_type == Some(DatabaseType::Salesforce) {
        return build_salesforce_data_grid_save_statements(options);
    }
    if uses_iotdb_table_model_save(options) {
        return build_iotdb_data_grid_save_statements(options);
    }

    let starrocks = options.database_type == Some(DatabaseType::StarRocks)
        || (options.database_type == Some(DatabaseType::Mysql)
            && driver_profile.is_some_and(|profile| profile.eq_ignore_ascii_case("starrocks")));
    let save_columns = effective_columns(options);
    let column_info = options.table_meta.columns.as_deref().unwrap_or(&[]);
    let schema = crate::sql_dialect::table_data_schema(
        options.database_type,
        driver_profile,
        options.table_meta.schema.as_deref(),
    );
    let table = data_grid_save_table_name(options, schema);
    let mut statements = Vec::new();
    let primary_key_set: Vec<String> =
        options.table_meta.primary_keys.iter().map(|primary_key| normalize_column_name(primary_key)).collect();

    let guards_keyless_predicates = primary_key_set.is_empty() && uses_keyless_row_predicate(options.database_type);
    // With the hidden locator present every generated predicate addresses one
    // physical row, so its guard must match exactly one row rather than "at most one".
    let postgres_ctid_locator = guards_keyless_predicates
        && uses_postgres_ctid(options.database_type)
        && find_column_index(options.database_type, &save_columns, DBX_ROWID_COLUMN).is_some();
    let mut guarded_predicates: Vec<GuardedPredicate> = Vec::new();
    let guard_predicate = |predicate: &str, guarded: &mut Vec<GuardedPredicate>| {
        if !guards_keyless_predicates || predicate.trim().is_empty() {
            return;
        }
        if !guarded.iter().any(|existing| existing.sql == predicate) {
            guarded.push(GuardedPredicate { sql: predicate.to_string(), requires_match: postgres_ctid_locator });
        }
    };

    let batch_mysql_writes = supports_mysql_data_grid_batch(options);
    let mut update_sets: Option<String> = None;
    let mut update_predicates = Vec::new();
    for (row_index, changes) in &options.dirty_rows {
        let Some(row) = options.rows.get(*row_index) else {
            continue;
        };
        let sets = changes
            .iter()
            .filter_map(|(column_index, value)| {
                let column = save_columns.get(*column_index)?.as_deref()?;
                if is_grid_update_omitted_column(
                    options.database_type,
                    column_info_for(column_info, column),
                    Some(column),
                    &primary_key_set,
                ) {
                    return None;
                }
                Some(format!(
                    "{} = {}",
                    data_grid_identifier(options.database_type, column, options.identifier_quote.as_deref()),
                    format_grid_save_sql_literal(
                        value,
                        options.database_type,
                        column_info_for(column_info, column),
                        options.identifier_quote.as_deref(),
                    )
                ))
            })
            .collect::<Vec<_>>()
            .join(", ");
        if sets.is_empty() {
            continue;
        }
        let mut predicate = build_primary_key_predicate(
            options.database_type,
            &options.table_meta.primary_keys,
            &save_columns,
            row,
            column_info,
            options.identifier_quote.as_deref(),
        );
        let guarded_lob = append_oceanbase_lob_save_guards(
            options,
            &save_columns,
            row,
            changes.iter().map(|(index, _)| *index),
            &mut predicate.sql,
        );
        if guarded_lob {
            // An appended `AND ...` guard makes the clause more than the plain
            // equality it started as; never merge a guarded predicate.
            predicate.plain = None;
        }
        guard_predicate(&predicate.sql, &mut guarded_predicates);
        if batch_mysql_writes {
            if update_sets.as_deref().is_some_and(|current| current != sets) {
                let current_sets = update_sets.take().unwrap_or_default();
                push_mysql_key_predicate_batches(
                    &mut statements,
                    &format!("UPDATE {table} SET {current_sets} WHERE "),
                    std::mem::take(&mut update_predicates),
                );
            }
            update_sets = Some(sets);
            update_predicates.push(predicate);
        } else {
            let statement = data_grid_update_sql(options.database_type, &table, &sets, &predicate.sql);
            statements.push(data_grid_save_statement(options.database_type, statement, guarded_lob, bound_statements));
        }
    }
    if let Some(sets) = update_sets {
        push_mysql_key_predicate_batches(
            &mut statements,
            &format!("UPDATE {table} SET {sets} WHERE "),
            update_predicates,
        );
    }

    let mut delete_predicates = Vec::new();
    for row_index in &options.deleted_rows {
        let Some(row) = options.rows.get(*row_index) else {
            continue;
        };
        let mut predicate = build_primary_key_predicate(
            options.database_type,
            &options.table_meta.primary_keys,
            &save_columns,
            row,
            column_info,
            options.identifier_quote.as_deref(),
        );
        let guarded_lob =
            append_oceanbase_lob_save_guards(options, &save_columns, row, 0..save_columns.len(), &mut predicate.sql);
        if guarded_lob {
            predicate.plain = None;
        }
        guard_predicate(&predicate.sql, &mut guarded_predicates);
        if batch_mysql_writes {
            delete_predicates.push(predicate);
        } else {
            let statement = data_grid_delete_sql(options.database_type, &table, &predicate.sql);
            statements.push(data_grid_save_statement(options.database_type, statement, guarded_lob, bound_statements));
        }
    }
    if !delete_predicates.is_empty() {
        push_mysql_key_predicate_batches(&mut statements, &format!("DELETE FROM {table} WHERE "), delete_predicates);
    }

    for row in &options.new_rows {
        if options.database_type == Some(DatabaseType::Hive) {
            if let Some(statement) = build_hive_values_insert(options, &table, &save_columns, row, true, true) {
                statements.push(data_grid_statement(options.database_type, statement));
            }
            continue;
        }
        let insert_pairs: Vec<(&str, &Value)> = save_columns
            .iter()
            .enumerate()
            .filter_map(|(index, column)| Some((column.as_deref()?, row.get(index).unwrap_or(&Value::Null))))
            .filter(|(column, value)| {
                let column_info = column_info_for(column_info, column);
                // Empty generated values must be omitted so the database can apply AUTO_INCREMENT/IDENTITY semantics.
                !column_info.is_some_and(is_auto_generated_column) || !grid_value_is_empty(value)
            })
            .filter(|(column, _)| {
                !is_grid_insert_omitted_column(
                    options.database_type,
                    column_info_for(column_info, column),
                    Some(column),
                    false,
                )
            })
            .filter(|(_, value)| starrocks || !value.is_null())
            .collect();
        if insert_pairs.is_empty() {
            if options.database_type == Some(DatabaseType::Mysql) {
                statements
                    .push(data_grid_statement(options.database_type, format!("INSERT INTO {table} () VALUES ()")));
            }
            continue;
        }
        let columns = insert_pairs
            .iter()
            .map(|(column, _)| data_grid_identifier(options.database_type, column, options.identifier_quote.as_deref()))
            .collect::<Vec<_>>()
            .join(", ");
        let values = insert_pairs
            .iter()
            .map(|(column, value)| {
                // A new-row NULL means unset. Keep StarRocks defaulted keys in the
                // target list, and still emit an INSERT when every value is unset.
                if starrocks && value.is_null() {
                    let info = column_info_for(column_info, column);
                    let default = info.and_then(|info| info.column_default.as_deref());
                    if let Some(default) = default {
                        // StarRocks can reject UUID defaults through VALUES(DEFAULT).
                        // Only emit known functions, never arbitrary metadata as SQL.
                        let normalized = default
                            .chars()
                            .filter(|ch| !ch.is_ascii_whitespace())
                            .collect::<String>()
                            .to_ascii_lowercase();
                        let mut expression = normalized.as_str();
                        while let Some(inner) = expression.strip_prefix('(').and_then(|value| value.strip_suffix(')')) {
                            expression = inner;
                        }
                        match expression {
                            "uuid()" => return "uuid()".to_string(),
                            "uuid_numeric()" => return "uuid_numeric()".to_string(),
                            _ => {}
                        }
                    } else if info.is_some_and(|info| info.is_nullable) {
                        return "NULL".to_string();
                    }
                    return "DEFAULT".to_string();
                }
                format_grid_save_sql_literal(
                    value,
                    options.database_type,
                    column_info_for(column_info, column),
                    options.identifier_quote.as_deref(),
                )
            })
            .collect::<Vec<_>>()
            .join(", ");
        statements.push(data_grid_save_statement(
            options.database_type,
            if options.database_type == Some(DatabaseType::Transwarp) {
                format!("INSERT INTO {table} ({columns}) SELECT {values}")
            } else {
                format!("INSERT INTO {table} ({columns}) VALUES ({values})")
            },
            false,
            bound_statements,
        ));
    }

    keyless_guards.extend(guarded_predicates.into_iter().map(|predicate| DataGridSaveGuard {
        sql: format!("SELECT COUNT(*) AS {KEYLESS_GUARD_COUNT_ALIAS} FROM {table} WHERE ({})", predicate.sql),
        max_matched_rows: 1,
        min_matched_rows: u32::from(predicate.requires_match),
        message: KEYLESS_AMBIGUOUS_ROW_ERROR.to_string(),
        min_message: predicate.requires_match.then(|| POSTGRES_CTID_STALE_ROW_ERROR.to_string()),
    }));

    statements
}

fn build_data_grid_rollback_statements(
    options: &DataGridSaveStatementOptions,
    driver_profile: Option<&str>,
) -> Vec<String> {
    if options.database_type == Some(DatabaseType::Tdengine) {
        return build_tdengine_data_grid_rollback_statements(options);
    }
    if options.database_type == Some(DatabaseType::Salesforce) {
        return build_salesforce_data_grid_rollback_statements(options);
    }
    if uses_iotdb_table_model_save(options) {
        return build_iotdb_data_grid_rollback_statements(options);
    }
    if options.database_type == Some(DatabaseType::ClickHouse) {
        return Vec::new();
    }

    let save_columns = effective_columns(options);
    let column_info = options.table_meta.columns.as_deref().unwrap_or(&[]);
    let schema = crate::sql_dialect::table_data_schema(
        options.database_type,
        driver_profile,
        options.table_meta.schema.as_deref(),
    );
    let table = data_grid_save_table_name(options, schema);
    let mut statements = Vec::new();
    let postgres_ctid_identity = uses_postgres_ctid(options.database_type)
        && options.table_meta.primary_keys.is_empty()
        && find_column_index(options.database_type, &save_columns, DBX_ROWID_COLUMN).is_some();

    for row in &options.new_rows {
        // Server-generated keys are unknown; never invent an undo predicate from
        // the unset draft values. Reuse the existing key-based rollback guard.
        let where_clause = if matches!(options.database_type, Some(DatabaseType::Mysql | DatabaseType::StarRocks)) {
            build_mysql_insert_rollback_where(options, &save_columns, row, column_info)
        } else {
            let where_clause = build_save_row_where(
                options.database_type,
                &save_columns,
                row,
                column_info,
                options.identifier_quote.as_deref(),
            );
            (!where_clause.is_empty()).then_some(where_clause)
        };
        if let Some(where_clause) = where_clause {
            statements
                .push(data_grid_statement(options.database_type, format!("DELETE FROM {table} WHERE {where_clause}")));
        }
    }

    let batch_mysql_writes = supports_mysql_data_grid_batch(options);
    let mut deleted_insert_columns: Option<String> = None;
    let mut deleted_insert_values = Vec::new();
    for row_index in &options.deleted_rows {
        let Some(row) = options.rows.get(*row_index) else {
            continue;
        };
        if options.database_type == Some(DatabaseType::Hive) {
            if let Some(statement) = build_hive_values_insert(options, &table, &save_columns, row, false, false) {
                statements.push(data_grid_statement(options.database_type, statement));
            }
            continue;
        }
        let insert_pairs: Vec<(&str, &Value)> = save_columns
            .iter()
            .enumerate()
            .filter_map(|(index, column)| Some((column.as_deref()?, row.get(index).unwrap_or(&Value::Null))))
            .filter(|(column, _)| {
                !is_grid_insert_omitted_column(
                    options.database_type,
                    column_info_for(column_info, column),
                    Some(column),
                    false,
                )
            })
            .collect();
        let columns = insert_pairs
            .iter()
            .map(|(column, _)| data_grid_identifier(options.database_type, column, options.identifier_quote.as_deref()))
            .collect::<Vec<_>>()
            .join(", ");
        let values = insert_pairs
            .iter()
            .map(|(column, value)| {
                format_grid_assignment_sql_literal(
                    value,
                    options.database_type,
                    column_info_for(column_info, column),
                    options.identifier_quote.as_deref(),
                )
            })
            .collect::<Vec<_>>()
            .join(", ");
        if batch_mysql_writes {
            if deleted_insert_columns.as_deref().is_some_and(|current| current != columns) {
                let current_columns = deleted_insert_columns.take().unwrap_or_default();
                push_mysql_values_insert_batches(
                    &mut statements,
                    &table,
                    &current_columns,
                    std::mem::take(&mut deleted_insert_values),
                );
            }
            deleted_insert_columns = Some(columns);
            deleted_insert_values.push(format!("({values})"));
        } else {
            statements.push(data_grid_statement(
                options.database_type,
                if options.database_type == Some(DatabaseType::Transwarp) {
                    format!("INSERT INTO {table} ({columns}) SELECT {values}")
                } else {
                    format!("INSERT INTO {table} ({columns}) VALUES ({values})")
                },
            ));
        }
    }
    if let Some(columns) = deleted_insert_columns {
        push_mysql_values_insert_batches(&mut statements, &table, &columns, deleted_insert_values);
    }

    for (row_index, changes) in &options.dirty_rows {
        let Some(row) = options.rows.get(*row_index) else {
            continue;
        };
        let mut after_row = row.clone();
        for (column_index, value) in changes {
            if *column_index < after_row.len() {
                after_row[*column_index] = value.clone();
            }
        }
        let writable_changes: Vec<(&(usize, Value), &str)> = changes
            .iter()
            .filter_map(|change @ (column_index, _)| {
                let column = save_columns.get(*column_index)?.as_deref()?;
                if is_grid_update_omitted_column(
                    options.database_type,
                    column_info_for(column_info, column),
                    Some(column),
                    &[],
                ) {
                    return None;
                }
                Some((change, column))
            })
            .collect();
        let sets = writable_changes
            .iter()
            .map(|((column_index, _), column)| {
                format!(
                    "{} = {}",
                    data_grid_identifier(options.database_type, column, options.identifier_quote.as_deref()),
                    format_grid_assignment_sql_literal(
                        row.get(*column_index).unwrap_or(&Value::Null),
                        options.database_type,
                        column_info_for(column_info, column),
                        options.identifier_quote.as_deref(),
                    )
                )
            })
            .collect::<Vec<_>>()
            .join(", ");
        if sets.is_empty() {
            continue;
        }
        let rollback_identity = if postgres_ctid_identity {
            let row_predicate = build_save_row_where(
                options.database_type,
                &save_columns,
                &after_row,
                column_info,
                options.identifier_quote.as_deref(),
            );
            if row_predicate.is_empty() {
                continue;
            }
            format!("({row_predicate}) AND (SELECT COUNT(*) FROM {table} WHERE ({row_predicate})) = 1")
        } else {
            build_primary_key_where(
                options.database_type,
                &options.table_meta.primary_keys,
                &save_columns,
                &after_row,
                column_info,
                options.identifier_quote.as_deref(),
            )
        };
        let mut predicates = vec![rollback_identity];
        if !postgres_ctid_identity {
            predicates.extend(writable_changes.iter().map(|((_, value), column)| {
                build_save_column_predicate(
                    options.database_type,
                    column,
                    value,
                    column_info_for(column_info, column),
                    true,
                    options.identifier_quote.as_deref(),
                )
            }));
        }
        statements.push(data_grid_statement(
            options.database_type,
            format!(
                "UPDATE {table} SET {sets} WHERE {}",
                predicates.into_iter().filter(|part| !part.is_empty()).collect::<Vec<_>>().join(" AND ")
            ),
        ));
    }

    statements
}

fn supports_mysql_data_grid_batch(options: &DataGridSaveStatementOptions) -> bool {
    // Keyless rows use full-row predicates, which are too wide and ambiguous to combine safely.
    options.database_type == Some(DatabaseType::Mysql) && !options.table_meta.primary_keys.is_empty()
}

/// One row key predicate of a batched MySQL UPDATE/DELETE.
struct MysqlKeyPredicate {
    /// Exactly the text the statement carries when this predicate cannot be
    /// merged — the same OR-form clause an unbatched statement would use.
    sql: String,
    /// Present only when `sql` is provably one plain non-NULL equality on a
    /// single column, so it may be merged into an `IN` list.
    plain: Option<MysqlPlainEquality>,
}

/// A provably plain `` `column` = literal `` key equality. Both pieces are
/// already rendered — the dialect-quoted identifier and the formatted literal —
/// and are accepted only after the clause they came from was checked to be
/// exactly `"{column} = {literal}"` (`plain_equality_from_clause`).
#[derive(Clone)]
struct MysqlPlainEquality {
    column: String,
    literal: String,
}

/// A rendered part of a batched MySQL statement, with the weight the
/// per-statement value budget has to account for it.
struct MysqlBatchPart {
    sql: String,
    /// Literal values this part contributes to the statement's value budget. An
    /// `IN` run contributes one per literal, a standalone predicate one; an
    /// INSERT value tuple contributes nothing because that budget exists only to
    /// bound range-optimizer memory on key predicates.
    values: usize,
}

/// Render the batched `WHERE` parts of a MySQL save. Runs of rows that differ
/// only in a single primary-key value collapse into `` `id` IN (...) `` instead
/// of a chain of ORs (#8857), which is the form a DBA is willing to run.
/// Anything that is not provably one plain non-NULL `column = literal` equality
/// — compound keys, `IS NULL`, BINARY/JSON comparisons, BLOB-guarded keys —
/// falls back to its own text, which stays correct for every predicate shape.
///
/// The merge is streaming: a non-mergeable predicate flushes the open run in
/// place, so the order the caller built the predicates in is preserved rather
/// than regrouped (`SET A → SET B → SET A` stays three statements).
///
/// Whether a predicate is "plain" is decided by the code that rendered it
/// (`build_primary_key_predicate`), and only after that rendering was checked to
/// reproduce the clause character for character. That check is why a future
/// change in `build_column_predicate` cannot silently widen a batch: a new
/// wrapper or comparison stops matching and the predicate keeps its OR form.
fn push_mysql_key_predicate_batches(statements: &mut Vec<String>, prefix: &str, predicates: Vec<MysqlKeyPredicate>) {
    const IN_JOINER: &str = ", ";

    struct InRun {
        column: String,
        values: Vec<String>,
        bytes: usize,
    }

    fn flush_run(run: &mut InRun, parts: &mut Vec<MysqlBatchPart>) {
        if run.values.len() == 1 {
            parts.push(MysqlBatchPart { sql: format!("{} = {}", run.column, run.values[0]), values: 1 });
        } else {
            parts.push(MysqlBatchPart {
                sql: format!("{} IN ({})", run.column, run.values.join(IN_JOINER)),
                values: run.values.len(),
            });
        }
        run.values.clear();
        run.bytes = 0;
    }

    let mut parts: Vec<MysqlBatchPart> = Vec::new();
    let mut run: Option<InRun> = None;
    for predicate in predicates {
        let Some(plain) = predicate.plain else {
            if let Some(open) = run.as_mut() {
                flush_run(open, &mut parts);
            }
            run = None;
            parts.push(MysqlBatchPart { sql: predicate.sql, values: 1 });
            continue;
        };
        let literal_bytes = plain.literal.len() + IN_JOINER.len();
        let starts_new_run = run.as_ref().is_some_and(|open| {
            open.column != plain.column
                || open.bytes + literal_bytes > MYSQL_DATA_GRID_BATCH_TARGET_SQL_BYTES
                || open.values.len() >= MYSQL_DATA_GRID_BATCH_MAX_VALUES
        });
        if starts_new_run {
            if let Some(open) = run.as_mut() {
                flush_run(open, &mut parts);
            }
            run = None;
        }
        let open = run.get_or_insert_with(|| InRun { column: plain.column, values: Vec::new(), bytes: 0 });
        open.bytes += literal_bytes;
        open.values.push(plain.literal);
    }
    if let Some(open) = run.as_mut() {
        flush_run(open, &mut parts);
    }

    push_mysql_joined_batches(statements, prefix, parts, " OR ", true, Some(MYSQL_DATA_GRID_BATCH_MAX_VALUES));
}

fn push_mysql_values_insert_batches(
    statements: &mut Vec<String>,
    table: &str,
    columns: &str,
    value_tuples: Vec<String>,
) {
    push_mysql_joined_batches(
        statements,
        &format!("INSERT INTO {table} ({columns}) VALUES "),
        value_tuples.into_iter().map(|tuple| MysqlBatchPart { sql: tuple, values: 0 }).collect(),
        ", ",
        false,
        // Row values are not key predicates; the value budget does not apply.
        None,
    );
}

/// Joins the parts of a MySQL statement, splitting whenever the open batch
/// reaches the row budget, the SQL byte budget, or — when `max_values` is set —
/// the per-statement value budget (see `MYSQL_DATA_GRID_BATCH_MAX_VALUES`).
fn push_mysql_joined_batches(
    statements: &mut Vec<String>,
    prefix: &str,
    parts: Vec<MysqlBatchPart>,
    separator: &str,
    wrap_multiple_parts: bool,
    max_values: Option<usize>,
) {
    let mut batch: Vec<MysqlBatchPart> = Vec::new();
    let mut batch_values = 0usize;
    for part in parts {
        let next_len = joined_batch_sql_len(prefix, &batch, &part, separator, wrap_multiple_parts);
        let over_values = max_values.is_some_and(|max| batch_values + part.values > max);
        if !batch.is_empty()
            && (batch.len() >= MYSQL_DATA_GRID_BATCH_MAX_ROWS
                || next_len > MYSQL_DATA_GRID_BATCH_TARGET_SQL_BYTES
                || over_values)
        {
            push_mysql_joined_batch_statement(
                statements,
                prefix,
                std::mem::take(&mut batch),
                separator,
                wrap_multiple_parts,
            );
            batch_values = 0;
        }
        batch_values += part.values;
        batch.push(part);
    }
    if !batch.is_empty() {
        push_mysql_joined_batch_statement(statements, prefix, batch, separator, wrap_multiple_parts);
    }
}

fn joined_batch_sql_len(
    prefix: &str,
    current: &[MysqlBatchPart],
    next: &MysqlBatchPart,
    separator: &str,
    wrap_multiple_parts: bool,
) -> usize {
    let part_bytes = |part: &str| part.len() + usize::from(wrap_multiple_parts) * 2;
    if current.is_empty() {
        return prefix.len() + next.sql.len() + 1;
    }
    let current_bytes = if current.len() == 1 && wrap_multiple_parts {
        current[0].sql.len()
    } else {
        current.iter().map(|part| part_bytes(&part.sql)).sum::<usize>()
            + separator.len() * current.len().saturating_sub(1)
    };
    let existing_adjustment = if current.len() == 1 && wrap_multiple_parts { 2 } else { 0 };
    prefix.len() + current_bytes + existing_adjustment + separator.len() + part_bytes(&next.sql) + 1
}

fn push_mysql_joined_batch_statement(
    statements: &mut Vec<String>,
    prefix: &str,
    parts: Vec<MysqlBatchPart>,
    separator: &str,
    wrap_multiple_parts: bool,
) {
    let body = if parts.len() == 1 || !wrap_multiple_parts {
        parts.into_iter().map(|part| part.sql).collect::<Vec<_>>().join(separator)
    } else {
        parts.into_iter().map(|part| format!("({})", part.sql)).collect::<Vec<_>>().join(separator)
    };
    statements.push(format!("{prefix}{body};"));
}

fn build_mysql_insert_rollback_where(
    options: &DataGridSaveStatementOptions,
    columns: &[Option<String>],
    row: &[Value],
    column_info: &[DataGridColumnInfo],
) -> Option<String> {
    if options.table_meta.primary_keys.is_empty() {
        return None;
    }

    for primary_key in &options.table_meta.primary_keys {
        let index = columns.iter().position(|column| column.as_deref() == Some(primary_key.as_str()))?;
        let value = row.get(index).unwrap_or(&Value::Null);
        let info = column_info_for(column_info, primary_key);
        if value.is_null()
            || empty_string_saves_as_null(value, info)
            || info.is_some_and(is_auto_generated_column)
            || info.is_some_and(|column| is_non_identity_generated_column(Some(column)))
        {
            // Generated or trigger-populated keys are unknown until after INSERT.
            // Do not emit a rollback predicate that cannot match the inserted row.
            return None;
        }
    }

    Some(build_primary_key_where(
        options.database_type,
        &options.table_meta.primary_keys,
        columns,
        row,
        column_info,
        options.identifier_quote.as_deref(),
    ))
}

pub fn effective_columns(options: &DataGridSaveStatementOptions) -> Vec<Option<String>> {
    let columns = match &options.source_columns {
        Some(source_columns) if source_columns.len() == options.columns.len() => source_columns.clone(),
        _ => options.columns.iter().map(|column| Some(column.clone())).collect(),
    };
    if options.database_type != Some(DatabaseType::Hive) {
        return columns;
    }
    columns
        .into_iter()
        .map(|column| column.map(|column| resolve_hive_target_column(&options.table_meta, &column)))
        .collect()
}

fn resolve_hive_target_column(table_meta: &DataGridTableMeta, result_column: &str) -> String {
    let Some(columns) = table_meta.columns.as_deref() else {
        return result_column.to_string();
    };
    if let Some(column) = unique_column_info_match(Some(DatabaseType::Hive), columns, result_column) {
        return column.name.clone();
    }
    let Some(unqualified) = last_qualified_identifier_component(result_column) else {
        return result_column.to_string();
    };
    // Hive JDBC may expose SELECT * labels as `table.column`. Resolve them through
    // target metadata, while exact matching above preserves real dotted column names.
    unique_column_info_match(Some(DatabaseType::Hive), columns, &unqualified)
        .map_or_else(|| result_column.to_string(), |column| column.name.clone())
}

pub(crate) fn unique_column_info_match<'a>(
    database_type: Option<DatabaseType>,
    columns: &'a [DataGridColumnInfo],
    name: &str,
) -> Option<&'a DataGridColumnInfo> {
    if let Some(column) = columns.iter().find(|column| column.name == name) {
        return Some(column);
    }
    if matches!(database_type, Some(DatabaseType::Postgres | DatabaseType::Redshift)) {
        return None;
    }
    let normalized = normalize_column_name(name);
    let mut matches = columns.iter().filter(|column| normalize_column_name(&column.name) == normalized);
    let first = matches.next()?;
    matches.next().is_none().then_some(first)
}

fn last_qualified_identifier_component(name: &str) -> Option<String> {
    let mut quote = None;
    let mut component_start = 0;
    let mut last_component = None;
    let chars = name.char_indices().collect::<Vec<_>>();
    let mut index = 0;
    while index < chars.len() {
        let (byte_index, ch) = chars[index];
        if let Some(end_quote) = quote {
            if ch == end_quote {
                if chars.get(index + 1).is_some_and(|(_, next)| *next == end_quote) {
                    index += 2;
                    continue;
                }
                quote = None;
            }
        } else {
            match ch {
                '`' | '"' => quote = Some(ch),
                '[' => quote = Some(']'),
                '.' => {
                    let component = name[component_start..byte_index].trim();
                    if component.is_empty() {
                        return None;
                    }
                    last_component = Some(component);
                    component_start = byte_index + ch.len_utf8();
                }
                _ => {}
            }
        }
        index += 1;
    }
    if quote.is_some() || last_component.is_none() {
        return None;
    }
    unquote_identifier_component(name[component_start..].trim())
}

fn unquote_identifier_component(component: &str) -> Option<String> {
    if component.is_empty() {
        return None;
    }
    for (open, close) in [('`', '`'), ('"', '"'), ('[', ']')] {
        if component.starts_with(open) || component.ends_with(close) {
            let inner = component.strip_prefix(open)?.strip_suffix(close)?;
            let escaped = format!("{close}{close}");
            return Some(inner.replace(&escaped, &close.to_string()));
        }
    }
    Some(component.to_string())
}

fn build_hive_values_insert(
    options: &DataGridSaveStatementOptions,
    table: &str,
    save_columns: &[Option<String>],
    row: &[Value],
    save_literals: bool,
    skip_all_null: bool,
) -> Option<String> {
    let metadata_columns = options.table_meta.columns.as_deref().unwrap_or(&[]);
    let target_columns = if metadata_columns.is_empty() {
        save_columns.iter().filter_map(|column| column.as_deref()).collect::<Vec<_>>()
    } else {
        metadata_columns.iter().map(|column| column.name.as_str()).collect::<Vec<_>>()
    };
    if target_columns.is_empty() {
        return None;
    }
    let values = target_columns
        .iter()
        .map(|column| {
            let value = find_column_index(Some(DatabaseType::Hive), save_columns, column)
                .and_then(|index| row.get(index))
                .unwrap_or(&Value::Null);
            (column, value)
        })
        .collect::<Vec<_>>();
    if skip_all_null && values.iter().all(|(_, value)| value.is_null()) {
        return None;
    }
    let values = values
        .into_iter()
        .map(|(column, value)| {
            let info = column_info_for(metadata_columns, column);
            if save_literals {
                format_grid_save_sql_literal(value, options.database_type, info, options.identifier_quote.as_deref())
            } else {
                format_grid_assignment_sql_literal(
                    value,
                    options.database_type,
                    info,
                    options.identifier_quote.as_deref(),
                )
            }
        })
        .collect::<Vec<_>>()
        .join(", ");
    Some(format!("INSERT INTO TABLE {table} VALUES ({values})"))
}

fn effective_copy_columns(source_columns: Option<&[Option<String>]>, columns: &[String]) -> Vec<Option<String>> {
    match source_columns {
        Some(source_columns) if source_columns.len() == columns.len() => source_columns.to_vec(),
        _ => columns.iter().map(|column| Some(column.clone())).collect(),
    }
}

fn copy_column_info(
    database_type: Option<DatabaseType>,
    column_info: &[DataGridColumnInfo],
    column: &str,
    fallback_type: Option<&str>,
) -> Option<DataGridColumnInfo> {
    if let Some(info) = unique_column_info_match(database_type, column_info, column) {
        return Some(info.clone());
    }
    fallback_type.map(|data_type| DataGridColumnInfo {
        name: column.to_string(),
        data_type: data_type.to_string(),
        is_nullable: true,
        is_primary_key: false,
        column_default: None,
        extra: None,
    })
}

fn data_grid_save_execution_schema(
    database_type: Option<DatabaseType>,
    driver_profile: Option<&str>,
    table_meta: &DataGridTableMeta,
) -> Option<String> {
    if matches!(database_type, Some(DatabaseType::Neo4j | DatabaseType::Oracle)) {
        return None;
    }
    crate::sql_dialect::table_data_schema(database_type, driver_profile, table_meta.schema.as_deref())
        .map(str::to_string)
}

pub fn normalize_data_grid_save_error(database_type: Option<DatabaseType>, error: &str) -> String {
    if database_type == Some(DatabaseType::Hive)
        && (error.contains("Attempt to do update or delete") || error.contains("Error 10294"))
    {
        return "Hive UPDATE/DELETE are not enabled for this table or server. Add rows with INSERT, or enable ACID transactional tables in Hive before editing/deleting existing rows.".to_string();
    }
    error.to_string()
}

pub(crate) fn format_grid_copy_insert_sql_literal(
    value: &Value,
    database_type: Option<DatabaseType>,
    column_info: Option<&DataGridColumnInfo>,
    identifier_quote: Option<&str>,
) -> String {
    if is_oracle_temporal_literal_database(database_type) {
        if let Some(text) = value.as_str() {
            if let Some(literal) =
                format_oracle_temporal_literal(text, column_info.map(|column| column.data_type.as_str()))
            {
                return literal;
            }
        }
    }
    format_grid_assignment_sql_literal(value, database_type, column_info, identifier_quote)
}

fn format_grid_assignment_sql_literal(
    value: &Value,
    database_type: Option<DatabaseType>,
    column_info: Option<&DataGridColumnInfo>,
    identifier_quote: Option<&str>,
) -> String {
    if is_oceanbase_blob_column(database_type, column_info) {
        if value.is_null() {
            return "NULL".to_string();
        }
        if let Some(hex) = oceanbase_blob_hex(value) {
            return if hex.is_empty() { "EMPTY_BLOB()".to_string() } else { format!("TO_BLOB(HEXTORAW('{hex}'))") };
        }
        // Invalid input must fail in SQL rather than storing the display text as bytes.
        return "TO_BLOB(HEXTORAW('invalid BLOB hex'))".to_string();
    }
    if matches!(database_type, Some(DatabaseType::Oracle | DatabaseType::OceanbaseOracle)) {
        if let Some(constructor) = column_info.and_then(|column| oracle_character_lob_constructor(&column.data_type)) {
            if let Some(text) = value.as_str() {
                if database_type == Some(DatabaseType::OceanbaseOracle) && text.is_empty() && constructor == "TO_CLOB" {
                    return "EMPTY_CLOB()".to_string();
                }
                return format_oracle_lob_assignment_literal(text, constructor);
            }
        }
    }
    if (value.is_array() || value.is_object()) && is_json_document_column(column_info) {
        return format_grid_sql_literal_with_identifier_quote(
            &Value::String(value.to_string()),
            database_type,
            column_info,
            identifier_quote,
        );
    }
    format_grid_sql_literal_with_identifier_quote(value, database_type, column_info, identifier_quote)
}

fn format_oracle_lob_assignment_literal(text: &str, constructor: &str) -> String {
    // Keep each Oracle SQL literal below the parser limit while preserving a CLOB result.
    let escaped_len = text.chars().map(oracle_sql_literal_char_len).sum::<usize>();
    if escaped_len <= ORACLE_SQL_LITERAL_MAX_BYTES {
        let mut escaped_text = String::with_capacity(escaped_len);
        append_oracle_sql_literal_characters(&mut escaped_text, text);
        return format!("'{escaped_text}'");
    }

    let mut chunks = Vec::new();
    let mut current = String::with_capacity(ORACLE_LOB_LITERAL_CHUNK_BYTES);
    let mut current_len = 0;
    for ch in text.chars() {
        let char_len = oracle_sql_literal_char_len(ch);
        if current_len > 0 && current_len + char_len > ORACLE_LOB_LITERAL_CHUNK_BYTES {
            chunks.push(format!("{constructor}('{current}')"));
            current = String::with_capacity(ORACLE_LOB_LITERAL_CHUNK_BYTES);
            current_len = 0;
        }
        append_oracle_sql_literal_char(&mut current, ch);
        current_len += char_len;
    }
    if current_len > 0 {
        chunks.push(format!("{constructor}('{current}')"));
    }
    chunks.join(" || ")
}

fn oracle_sql_literal_char_len(ch: char) -> usize {
    match ch {
        '\'' => 2,
        _ => ch.len_utf8(),
    }
}

fn append_oracle_sql_literal_characters(output: &mut String, text: &str) {
    for ch in text.chars() {
        append_oracle_sql_literal_char(output, ch);
    }
}

fn append_oracle_sql_literal_char(output: &mut String, ch: char) {
    match ch {
        '\'' => output.push_str("''"),
        _ => output.push(ch),
    }
}

fn is_json_document_column(column_info: Option<&DataGridColumnInfo>) -> bool {
    column_info.is_some_and(|column| {
        let data_type = column.data_type.trim();
        data_type.eq_ignore_ascii_case("json") || data_type.eq_ignore_ascii_case("jsonb")
    })
}

pub fn format_grid_sql_literal(
    value: &Value,
    database_type: Option<DatabaseType>,
    column_info: Option<&DataGridColumnInfo>,
) -> String {
    format_grid_sql_literal_with_identifier_quote(value, database_type, column_info, None)
}

pub fn format_grid_sql_literal_with_identifier_quote(
    value: &Value,
    database_type: Option<DatabaseType>,
    column_info: Option<&DataGridColumnInfo>,
    identifier_quote: Option<&str>,
) -> String {
    if value.is_null() {
        return "NULL".to_string();
    }
    // Boolean values on BIT columns use database-native boolean/bit literals.
    // This covers MySQL, SQL Server, and any other database where BIT
    // is a numeric/boolean type rather than a bit-string type like
    // PostgreSQL's bit(n).
    if let Some(value) = value.as_bool() {
        if is_kingbase_bit_literal_column(database_type, column_info) {
            return format!("b'{}'", if value { '1' } else { '0' });
        }
        // SQL Server has no TRUE/FALSE literals (its boolean type is BIT, which
        // is_bit_literal_column already covers); any other column there still
        // needs numeric 1/0 instead of a literal.
        if is_bit_literal_column(database_type, column_info) || database_type == Some(DatabaseType::SqlServer) {
            return if value { "1" } else { "0" }.to_string();
        }
        return if value { "TRUE" } else { "FALSE" }.to_string();
    }
    if is_kingbase_bit_literal_column(database_type, column_info) {
        if let Some(number) = value.as_number() {
            if let Some(literal) = format_kingbase_bit_literal_text(&number.to_string()) {
                return literal;
            }
        }
        if let Some(text) = value.as_str() {
            if let Some(literal) = format_kingbase_bit_literal_text(text) {
                return literal;
            }
        }
    }
    if is_mysql_bit_literal_column(database_type, column_info, identifier_quote) {
        if let Some(number) = value.as_number() {
            return number.to_string();
        }
        if let Some(text) = value.as_str().and_then(format_mysql_bit_literal_text) {
            return text;
        }
    }
    if let Some(number) = value.as_number() {
        return number.to_string();
    }
    if let Some(arr) = value.as_array() {
        if let Some(element_type) = postgres_json_array_element_type(database_type, column_info) {
            return format_postgres_json_array_sql_literal(arr, element_type);
        }
        if matches!(database_type, Some(DatabaseType::ClickHouse) | Some(DatabaseType::Databend)) {
            return format_ch_array_sql_literal(arr);
        }
        return format_pg_array_sql_literal(arr);
    }
    let text = value.as_str().map_or_else(|| value.to_string(), ToString::to_string);
    if database_type == Some(DatabaseType::Iotdb)
        && column_info.is_some_and(|column| {
            column.data_type.trim().split(['(', ' ']).next().is_some_and(|base| base.eq_ignore_ascii_case("timestamp"))
        })
    {
        if let Ok(timestamp) = text.parse::<i64>() {
            // IoTDB can use nanosecond epoch values that exceed JavaScript's
            // safe integer range, so its Agent transports TIMESTAMP cells as
            // decimal strings. Keep those strings as numeric SQL literals.
            return timestamp.to_string();
        }
    }
    if column_info.map(|column| is_uuid_column_type(&column.data_type)).unwrap_or(false) {
        if let Some(literal) = format_hex_uuid_literal(&text) {
            return literal;
        }
    }
    if is_mysql_binary_literal_column(database_type, column_info) {
        if let Some(literal) = format_mysql_binary_literal_text(&text) {
            // DBX result values expose binary columns as prefixed hex; keep them
            // as MySQL hex literals so copied INSERT/UPDATE SQL round-trips bytes.
            return literal;
        }
    }
    if is_sqlserver_binary_literal_column(database_type, column_info) {
        if let Some(literal) = format_sqlserver_binary_literal_text(&text) {
            // SQL Server timestamp/rowversion values are binary counters, not
            // Unicode strings. Keyless row predicates must compare their bytes.
            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;
        }
    }
    if column_info.map(|column| is_numeric_type(&column.data_type)).unwrap_or(false) && is_numeric_literal(&text) {
        // BigDecimal/BigInteger cells cross JSON-RPC as strings so browsers cannot round them.
        return text;
    }
    if database_type == Some(DatabaseType::ManticoreSearch) {
        if let Some(typed_value) = manticore_typed_attribute_value(&text, column_info) {
            return format_grid_sql_literal_with_identifier_quote(
                &typed_value,
                database_type,
                column_info,
                identifier_quote,
            );
        }
    }
    if text.is_empty() {
        return if database_type == Some(DatabaseType::SqlServer) { "N''" } else { "''" }.to_string();
    }
    // MySQL geometry columns: wrap WKT text with ST_GeomFromText()
    if is_mysql_geometry_literal_database(database_type)
        && column_info.map(|column| is_geometry_column_type(&column.data_type)).unwrap_or(false)
    {
        let escaped = text.replace('\\', "\\\\").replace('\'', "''");
        return format!("ST_GeomFromText('{}')", escaped);
    }
    if is_oracle_temporal_literal_database(database_type) {
        if let Some(literal) =
            format_oracle_temporal_literal(&text, column_info.map(|column| column.data_type.as_str()))
        {
            return literal;
        }
    }
    let literal_text = if database_type == Some(DatabaseType::Tdengine) {
        format_tdengine_timestamp_literal_text(&text)
    } else if database_type == Some(DatabaseType::SqlServer) {
        crate::sqlserver_temporal::normalize_sqlserver_temporal_literal(
            &text,
            column_info.map(|column| column.data_type.as_str()),
        )
        .unwrap_or(text)
    } else if is_mysql_datetime_literal_database(database_type)
        && column_info.map(|column| is_temporal_column_type(&column.data_type)).unwrap_or(true)
    {
        format_mysql_temporal_literal_text(&text, column_info.map(|column| column.data_type.as_str()))
    } else {
        text
    };
    if database_type == Some(DatabaseType::Postgres) && (literal_text.contains('\\') || literal_text.contains('\u{1a}'))
    {
        // Escape strings have stable backslash semantics regardless of the
        // session's standard_conforming_strings setting. The SUB byte is spelled
        // `\x1A` so the copied script does not carry a raw 0x1A control byte.
        let escaped_text = literal_text.replace('\\', "\\\\").replace('\'', "''").replace('\u{1a}', "\\x1A");
        return format!("E'{escaped_text}'");
    }
    if database_type == Some(DatabaseType::SqlServer) {
        return format_sqlserver_unicode_literal(&literal_text);
    }
    let escaped_text = if database_type == Some(DatabaseType::Neo4j) {
        literal_text.replace('\\', "\\\\").replace('\'', "\\'")
    } else if is_sqlite_literal_database(database_type) || keeps_literal_backslashes(database_type) {
        // These engines keep backslashes literal in ordinary string literals,
        // so only the quote delimiter needs escaping.
        literal_text.replace('\'', "''")
    } else {
        let escaped_text = literal_text.replace('\\', "\\\\").replace('\'', "''");
        match grid_sub_control_escape(database_type) {
            Some(escape) => escape_grid_sub_control(&escaped_text, escape),
            None => escaped_text,
        }
    };
    let escaped = format!("'{escaped_text}'");
    escaped
}

/// How a backslash-escaping dialect spells the 0x1A (SUB / Ctrl-Z) byte,
/// mirroring the SQL export path (`dbx-core`'s `SubControlEscape`).
///
/// `\Z` is a MySQL-family escape. ClickHouse has no `\Z` in its escape table and
/// reads the sequence as a literal backslash followed by `Z`, and Snowflake drops
/// the backslash, so both need `\xhh` instead. The MySQL manual is explicit about
/// why these bytes must not stay raw in a script: ASCII 26 stands for
/// end-of-file on Windows (`mysql db_name < file.sql` stops there), and the
/// `mysql` client truncates quoted strings containing NUL characters.
#[derive(Clone, Copy, PartialEq, Eq)]
enum GridSubControlEscape {
    /// The MySQL-family `\Z`.
    BackslashZ,
    /// `\x1a`, for dialects whose escape table has no `\Z`.
    Hex,
}

fn grid_sub_control_escape(database_type: Option<DatabaseType>) -> Option<GridSubControlEscape> {
    match database_type {
        Some(DatabaseType::Mysql | DatabaseType::Doris | DatabaseType::StarRocks | DatabaseType::Goldendb) => {
            Some(GridSubControlEscape::BackslashZ)
        }
        Some(DatabaseType::ClickHouse | DatabaseType::Snowflake) => Some(GridSubControlEscape::Hex),
        _ => None,
    }
}

fn escape_grid_sub_control(text: &str, escape: GridSubControlEscape) -> String {
    if !text.contains(['\u{1a}', '\0']) {
        return text.to_string();
    }
    let sub_control = match escape {
        GridSubControlEscape::BackslashZ => "\\Z",
        GridSubControlEscape::Hex => "\\x1a",
    };
    text.replace('\0', "\\0").replace('\u{1a}', sub_control)
}

fn postgres_json_array_element_type(
    database_type: Option<DatabaseType>,
    column_info: Option<&DataGridColumnInfo>,
) -> Option<&'static str> {
    if database_type != Some(DatabaseType::Postgres) {
        return None;
    }
    match column_info?.data_type.trim().to_ascii_lowercase().as_str() {
        "json[]" | "_json" => Some("json"),
        "jsonb[]" | "_jsonb" => Some("jsonb"),
        _ => None,
    }
}

fn format_postgres_json_array_sql_literal(arr: &[Value], element_type: &str) -> String {
    if arr.is_empty() {
        return format!("ARRAY[]::{element_type}[]");
    }
    let elements = arr
        .iter()
        .map(|value| {
            if value.is_null() {
                return "NULL".to_string();
            }
            let json = match value {
                Value::String(text) => serde_json::from_str::<Value>(text)
                    .map(|value| value.to_string())
                    .unwrap_or_else(|_| Value::String(text.clone()).to_string()),
                _ => value.to_string(),
            };
            let escaped = json.replace('\\', "\\\\").replace('\'', "''");
            format!("E'{escaped}'::{element_type}")
        })
        .collect::<Vec<_>>()
        .join(", ");
    format!("ARRAY[{elements}]")
}

fn format_sqlserver_unicode_literal(text: &str) -> String {
    let mut parts = Vec::new();
    let mut segment = String::new();

    for ch in text.chars() {
        let line_break = match ch {
            '\r' => Some(13),
            '\n' => Some(10),
            _ => None,
        };
        if let Some(codepoint) = line_break {
            if !segment.is_empty() {
                parts.push(format!("N'{}'", segment.replace('\'', "''")));
                segment.clear();
            }
            // Keep physical newlines out of generated SQL so a preceding
            // backslash cannot be consumed as a line-continuation marker.
            parts.push(format!("NCHAR({codepoint})"));
        } else {
            segment.push(ch);
        }
    }

    if !segment.is_empty() {
        parts.push(format!("N'{}'", segment.replace('\'', "''")));
    }
    if parts.is_empty() {
        return "N''".to_string();
    }
    // SQL Server types a `+` chain of ordinary literals and `NCHAR(n)` as
    // `nvarchar(4000)`, and silently truncates a longer result instead of
    // failing, so a long value needs an `nvarchar(max)` operand to survive
    // (issue #12202). A single literal needs no anchor: SQL Server already types
    // a literal longer than 4000 characters as `nvarchar(max)`. Each emitted
    // character of the chain is one UTF-16 code unit of `text`, so the value's
    // own length is the length of the concatenation.
    if parts.len() > 1 && text.encode_utf16().count() > SQLSERVER_NVARCHAR_LITERAL_MAX_CHARS {
        parts.insert(0, "CAST(N'' AS NVARCHAR(MAX))".to_string());
    }
    parts.join(" + ")
}

fn is_sqlite_literal_database(database_type: Option<DatabaseType>) -> bool {
    matches!(
        database_type,
        Some(DatabaseType::Sqlite | DatabaseType::Rqlite | DatabaseType::Turso | DatabaseType::CloudflareD1)
    )
}

/// Engines whose ordinary string literals keep a backslash literal: Oracle and
/// the engines that inherit its lexer for this purpose, plus the PostgreSQL
/// family, whose `standard_conforming_strings` default makes `'dir\'` a complete
/// string. Doubling the backslash there would copy `C:\\tmp` for a stored
/// `C:\tmp`. Mirrors the SQL export path, which only doubles backslashes for the
/// dialects whose escape table has one, and `keeps_backslash_literal` in
/// `dbx-core`'s transfer path.
fn keeps_literal_backslashes(database_type: Option<DatabaseType>) -> bool {
    matches!(
        database_type,
        Some(
            DatabaseType::Oracle
                | DatabaseType::OceanbaseOracle
                | DatabaseType::Dameng
                | DatabaseType::Yashandb
                | DatabaseType::Oscar
                | DatabaseType::Xugu
                | DatabaseType::Gaussdb
                | DatabaseType::OpenGauss
                | DatabaseType::Kingbase
                | DatabaseType::Highgo
                | DatabaseType::Uxdb
                | DatabaseType::Vastbase
                | DatabaseType::Gbase8c
                | DatabaseType::Kwdb
        )
    )
}

fn format_grid_save_sql_literal(
    value: &Value,
    database_type: Option<DatabaseType>,
    column_info: Option<&DataGridColumnInfo>,
    identifier_quote: Option<&str>,
) -> String {
    if empty_string_saves_as_null(value, column_info) {
        "NULL".to_string()
    } else {
        format_grid_assignment_sql_literal(value, database_type, column_info, identifier_quote)
    }
}

fn empty_string_saves_as_null(value: &Value, column_info: Option<&DataGridColumnInfo>) -> bool {
    value.as_str() == Some("")
        && column_info.is_some_and(|column| column.is_nullable && !is_textual_column_type(&column.data_type))
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum OracleTemporalKind {
    Date,
    Timestamp,
    TimestampWithTimeZone,
}

fn is_oracle_temporal_literal_database(database_type: Option<DatabaseType>) -> bool {
    matches!(database_type, Some(DatabaseType::Oracle | DatabaseType::OceanbaseOracle))
}

pub fn format_oracle_temporal_literal(text: &str, data_type: Option<&str>) -> Option<String> {
    let kind = oracle_temporal_column_kind(data_type?)?;
    let parts = regex_like_oracle_temporal(text)?;
    let fraction = parts.fraction.as_deref().unwrap_or_default();
    let datetime = format!("{} {}{}", parts.date, parts.time, fraction);
    match kind {
        OracleTemporalKind::Date if oracle_temporal_parts_are_midnight(&parts) => {
            Some(format!("DATE '{}'", parts.date))
        }
        OracleTemporalKind::Date => Some(format!("TO_DATE('{} {}', 'YYYY-MM-DD HH24:MI:SS')", parts.date, parts.time)),
        OracleTemporalKind::Timestamp => {
            let mask = oracle_timestamp_format_mask(datetime.contains('.'));
            Some(format!("TO_TIMESTAMP('{datetime}', '{mask}')"))
        }
        OracleTemporalKind::TimestampWithTimeZone => {
            if parts.zone.is_empty() {
                let mask = oracle_timestamp_format_mask(datetime.contains('.'));
                return Some(format!("TO_TIMESTAMP('{datetime}', '{mask}')"));
            }
            let zone = oracle_timezone_suffix(&parts.zone);
            let mask = oracle_timestamp_format_mask(datetime.contains('.'));
            Some(format!("TO_TIMESTAMP_TZ('{datetime} {zone}', '{mask} TZH:TZM')"))
        }
    }
}

fn oracle_temporal_parts_are_midnight(parts: &Rfc3339Parts) -> bool {
    parts.time == "00:00:00"
        && parts
            .fraction
            .as_deref()
            .map(|fraction| fraction.trim_start_matches('.').chars().all(|ch| ch == '0'))
            .unwrap_or(true)
}

fn oracle_temporal_column_kind(data_type: &str) -> Option<OracleTemporalKind> {
    let lower = data_type.trim().to_ascii_lowercase();
    let base = lower.split(['(', ' ']).next().unwrap_or("");
    match base {
        "date" => Some(OracleTemporalKind::Date),
        "timestamp" if lower.contains("with time zone") || lower.contains("with local time zone") => {
            Some(OracleTemporalKind::TimestampWithTimeZone)
        }
        "timestamp" => Some(OracleTemporalKind::Timestamp),
        _ => None,
    }
}

fn oracle_timestamp_format_mask(has_fraction: bool) -> &'static str {
    if has_fraction {
        "YYYY-MM-DD HH24:MI:SS.FF"
    } else {
        "YYYY-MM-DD HH24:MI:SS"
    }
}

fn oracle_timezone_suffix(zone: &str) -> String {
    if zone.eq_ignore_ascii_case("z") {
        "+00:00".to_string()
    } else {
        zone.to_string()
    }
}

fn regex_like_oracle_temporal(text: &str) -> Option<Rfc3339Parts> {
    if let Some(parts) = regex_like_rfc3339(text) {
        return Some(parts);
    }
    if let Some((datetime, zone)) = text.rsplit_once(' ') {
        let bytes = zone.as_bytes();
        let normalized_zone = if is_timezone_offset(zone) {
            Some(zone.to_string())
        } else if bytes.len() == 5
            && matches!(bytes[0], b'+' | b'-')
            && bytes[1].is_ascii_digit()
            && bytes[2] == b':'
            && bytes[3].is_ascii_digit()
            && bytes[4].is_ascii_digit()
        {
            Some(format!("{}0{}", &zone[..1], &zone[1..]))
        } else {
            None
        };
        if let Some(zone) = normalized_zone {
            let mut parts = regex_like_local_datetime(datetime)?;
            parts.zone = zone;
            return Some(parts);
        }
    }
    regex_like_local_datetime(text)
}

fn regex_like_local_datetime(text: &str) -> Option<Rfc3339Parts> {
    let bytes = text.as_bytes();
    if bytes.len() < 10 || bytes.get(4) != Some(&b'-') || bytes.get(7) != Some(&b'-') {
        return None;
    }
    let date = text.get(0..10)?;
    if bytes.len() == 10 {
        return Some(Rfc3339Parts {
            date: date.to_string(),
            time: "00:00:00".to_string(),
            fraction: None,
            zone: String::new(),
        });
    }
    let separator = *bytes.get(10)?;
    if separator != b'T' && separator != b' ' {
        return None;
    }
    if bytes.len() < 19 || bytes.get(13) != Some(&b':') || bytes.get(16) != Some(&b':') {
        return None;
    }
    let time = text.get(11..19)?;
    let rest = text.get(19..)?;
    let fraction = if let Some(rest) = rest.strip_prefix('.') {
        let digit_count = rest.chars().take_while(|ch| ch.is_ascii_digit()).count();
        if digit_count == 0 || digit_count > 9 || digit_count != rest.len() {
            return None;
        }
        Some(format!(".{}", &rest[..digit_count]))
    } else if rest.is_empty() {
        None
    } else {
        return None;
    };
    Some(Rfc3339Parts { date: date.to_string(), time: time.to_string(), fraction, zone: String::new() })
}

fn is_mysql_bit_literal_column(
    database_type: Option<DatabaseType>,
    column_info: Option<&DataGridColumnInfo>,
    identifier_quote: Option<&str>,
) -> bool {
    (is_mysql_datetime_literal_database(database_type)
        || (database_type == Some(DatabaseType::Kingbase) && identifier_quote == Some("`")))
        && column_info.map(|column| is_bit_column_type(&column.data_type)).unwrap_or(false)
}

fn is_kingbase_bit_literal_column(
    database_type: Option<DatabaseType>,
    column_info: Option<&DataGridColumnInfo>,
) -> bool {
    database_type == Some(DatabaseType::Kingbase)
        && column_info.map(|column| is_bit_column_type(&column.data_type)).unwrap_or(false)
}

fn is_bit_literal_column(database_type: Option<DatabaseType>, column_info: Option<&DataGridColumnInfo>) -> bool {
    database_type != Some(DatabaseType::Postgres)
        && column_info.map(|column| is_bit_column_type(&column.data_type)).unwrap_or(false)
}

fn is_bit_column_type(data_type: &str) -> bool {
    let lower = data_type.to_ascii_lowercase();
    lower.split(|ch: char| !ch.is_ascii_alphanumeric()).any(|token| {
        // SQL Server/tiberius reports nullable BIT result columns as `bitn`.
        // They still need numeric 0/1 literals in generated UPDATE SQL.
        matches!(token, "bit" | "bitn")
    })
}

fn is_mysql_geometry_literal_database(database_type: Option<DatabaseType>) -> bool {
    matches!(
        database_type,
        Some(
            DatabaseType::Mysql
                | DatabaseType::Doris
                | DatabaseType::StarRocks
                | DatabaseType::Goldendb
                | DatabaseType::Sundb
        )
    )
}

fn is_mysql_binary_literal_column(
    database_type: Option<DatabaseType>,
    column_info: Option<&DataGridColumnInfo>,
) -> bool {
    database_type == Some(DatabaseType::Mysql)
        && column_info.map(|column| is_mysql_binary_column_type(&column.data_type)).unwrap_or(false)
}

fn is_mysql_binary_column_type(data_type: &str) -> bool {
    let lower = data_type.trim().to_ascii_lowercase();
    let base = lower.split(['(', ':', ' ']).next().unwrap_or("").trim();
    matches!(base, "binary" | "varbinary" | "blob" | "tinyblob" | "mediumblob" | "longblob")
}

fn is_uuid_column_type(data_type: &str) -> bool {
    let lower = data_type.trim().to_ascii_lowercase();
    let base = lower.split(['(', ':', ' ']).next().unwrap_or("").trim();
    matches!(base, "uuid" | "uniqueidentifier")
}

fn format_hex_uuid_literal(text: &str) -> Option<String> {
    let trimmed = text.trim();
    let hex = trimmed.strip_prefix("0x").or_else(|| trimmed.strip_prefix("0X"))?;
    if !hex.is_ascii() {
        return None;
    }
    let hex_bytes = hex.as_bytes();
    if hex_bytes.len() == 72 && hex_bytes.iter().all(|b| b.is_ascii_hexdigit()) {
        let mut bytes = Vec::with_capacity(36);
        for chunk in hex_bytes.chunks_exact(2) {
            let s = std::str::from_utf8(chunk).ok()?;
            let byte = u8::from_str_radix(s, 16).ok()?;
            bytes.push(byte);
        }
        if bytes.len() == 36
            && bytes.iter().enumerate().all(|(idx, &b)| {
                if idx == 8 || idx == 13 || idx == 18 || idx == 23 {
                    b == b'-'
                } else {
                    b.is_ascii_hexdigit()
                }
            })
        {
            let uuid_str = std::str::from_utf8(&bytes).ok()?;
            return Some(format!("'{uuid_str}'"));
        }
    }
    if hex_bytes.len() == 32 && hex_bytes.iter().all(|b| b.is_ascii_hexdigit()) {
        let lower = hex.to_ascii_lowercase();
        return Some(format!(
            "'{}-{}-{}-{}-{}'",
            &lower[0..8],
            &lower[8..12],
            &lower[12..16],
            &lower[16..20],
            &lower[20..32]
        ));
    }
    None
}

fn format_mysql_binary_literal_text(text: &str) -> Option<String> {
    let trimmed = text.trim();
    let hex = trimmed.strip_prefix("0x")?;
    if hex.chars().all(|ch| ch.is_ascii_hexdigit()) {
        Some(if hex.is_empty() { "X''".to_string() } else { trimmed.to_string() })
    } else {
        None
    }
}

fn is_sqlserver_binary_literal_column(
    database_type: Option<DatabaseType>,
    column_info: Option<&DataGridColumnInfo>,
) -> bool {
    database_type == Some(DatabaseType::SqlServer)
        && column_info.is_some_and(|column| {
            let normalized = column.data_type.trim().trim_matches('"').to_ascii_lowercase();
            let base = normalized.split(['(', ' ', '\t', '\n']).next().unwrap_or("");
            matches!(base, "binary" | "varbinary" | "timestamp" | "rowversion")
        })
}

fn format_sqlserver_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.bytes().all(|byte| byte.is_ascii_hexdigit()) {
        Some(format!("0x{hex}"))
    } else {
        None
    }
}

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| {
            let data_type = column.data_type.trim();
            // accept an optional single schema qualifier (pg_catalog.bytea) so the write-back
            // literal path stays aligned with the frontend BYTEA_TYPE_RE edit gate
            let bytea_name = data_type.split_once('.').map_or(data_type, |(_, name)| name);
            bytea_name.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)
}

fn is_oracle_raw_column_type(data_type: &str) -> bool {
    data_type.trim().split(['(', ':', ' ']).next().is_some_and(|base| base.eq_ignore_ascii_case("raw"))
}

fn format_oracle_raw_literal_text(text: &str) -> Option<String> {
    let hex = text.strip_prefix("0x")?;
    if hex.len() % 2 == 0 && hex.chars().all(|ch| ch.is_ascii_hexdigit()) {
        Some(format!("HEXTORAW('{hex}')"))
    } else {
        None
    }
}

fn is_geometry_column_type(data_type: &str) -> bool {
    let lower = data_type.to_ascii_lowercase();
    let base = lower.split('(').next().unwrap_or(&lower).trim();
    matches!(
        base,
        "geometry"
            | "point"
            | "linestring"
            | "polygon"
            | "multipoint"
            | "multilinestring"
            | "multipolygon"
            | "geometrycollection"
    )
}

fn manticore_typed_attribute_value(text: &str, column_info: Option<&DataGridColumnInfo>) -> Option<Value> {
    let data_type = column_info?.data_type.to_ascii_lowercase();
    if is_boolean_type(&data_type, None) && text.eq_ignore_ascii_case("true") {
        return Some(Value::Bool(true));
    }
    if is_boolean_type(&data_type, None) && text.eq_ignore_ascii_case("false") {
        return Some(Value::Bool(false));
    }
    if is_numeric_type(&data_type) && is_numeric_literal(text) {
        return text.parse::<serde_json::Number>().ok().map(Value::Number);
    }
    None
}

fn format_mysql_bit_literal_text(text: &str) -> Option<String> {
    let trimmed = text.trim();
    if trimmed.eq_ignore_ascii_case("true") {
        return Some("1".to_string());
    }
    if trimmed.eq_ignore_ascii_case("false") {
        return Some("0".to_string());
    }
    if trimmed.chars().all(|ch| ch.is_ascii_digit()) && !trimmed.is_empty() {
        return Some(if trimmed.len() == 1 {
            trimmed.to_string()
        } else if trimmed.chars().all(|ch| matches!(ch, '0' | '1')) {
            format!("b'{trimmed}'")
        } else {
            trimmed.to_string()
        });
    }
    let lower = trimmed.to_ascii_lowercase();
    if lower.starts_with("b'") && trimmed.ends_with('\'') {
        let bits = &trimmed[2..trimmed.len() - 1];
        if !bits.is_empty() && bits.chars().all(|ch| matches!(ch, '0' | '1')) {
            return Some(format!("b'{bits}'"));
        }
    }
    None
}

fn format_kingbase_bit_literal_text(text: &str) -> Option<String> {
    let trimmed = text.trim();
    if trimmed.eq_ignore_ascii_case("true") {
        return Some("b'1'".to_string());
    }
    if trimmed.eq_ignore_ascii_case("false") {
        return Some("b'0'".to_string());
    }
    if !trimmed.is_empty() && trimmed.chars().all(|ch| matches!(ch, '0' | '1')) {
        return Some(format!("b'{trimmed}'"));
    }
    let lower = trimmed.to_ascii_lowercase();
    if lower.starts_with("b'") && trimmed.ends_with('\'') {
        let bits = &trimmed[2..trimmed.len() - 1];
        if !bits.is_empty() && bits.chars().all(|ch| matches!(ch, '0' | '1')) {
            return Some(format!("b'{bits}'"));
        }
    }
    None
}

fn is_mysql_datetime_literal_database(database_type: Option<DatabaseType>) -> bool {
    matches!(
        database_type,
        Some(
            DatabaseType::Mysql
                | DatabaseType::Doris
                | DatabaseType::StarRocks
                | DatabaseType::Goldendb
                | DatabaseType::Sundb
        )
    )
}

fn format_mysql_temporal_literal_text(text: &str, data_type: Option<&str>) -> String {
    let Some(captures) = regex_like_rfc3339(text) else {
        return text.to_string();
    };
    match temporal_column_kind(data_type) {
        Some("date") => captures.date,
        Some("time") => {
            format!("{}{}", captures.time, normalize_mysql_fractional_seconds(captures.fraction.as_deref()))
        }
        _ => format!(
            "{} {}{}",
            captures.date,
            captures.time,
            normalize_mysql_fractional_seconds(captures.fraction.as_deref())
        ),
    }
}

struct Rfc3339Parts {
    date: String,
    time: String,
    fraction: Option<String>,
    zone: String,
}

fn regex_like_rfc3339(text: &str) -> Option<Rfc3339Parts> {
    let bytes = text.as_bytes();
    if bytes.len() < 20 || bytes.get(4) != Some(&b'-') || bytes.get(7) != Some(&b'-') {
        return None;
    }
    let separator = *bytes.get(10)?;
    if separator != b'T' && separator != b' ' {
        return None;
    }
    if bytes.get(13) != Some(&b':') || bytes.get(16) != Some(&b':') {
        return None;
    }
    let date = &text[0..10];
    let time = text.get(11..19)?;
    let rest = text.get(19..)?;
    let (fraction, zone) = if let Some(rest) = rest.strip_prefix('.') {
        let digit_count = rest.chars().take_while(|ch| ch.is_ascii_digit()).count();
        if digit_count == 0 || digit_count > 9 {
            return None;
        }
        (Some(format!(".{}", &rest[..digit_count])), &rest[digit_count..])
    } else {
        (None, rest)
    };
    if zone == "Z" || zone == "z" || is_timezone_offset(zone) {
        Some(Rfc3339Parts { date: date.to_string(), time: time.to_string(), fraction, zone: zone.to_string() })
    } else {
        None
    }
}

fn is_timezone_offset(value: &str) -> bool {
    let bytes = value.as_bytes();
    bytes.len() == 6
        && matches!(bytes[0], b'+' | b'-')
        && bytes[3] == b':'
        && bytes[1].is_ascii_digit()
        && bytes[2].is_ascii_digit()
        && bytes[4].is_ascii_digit()
        && bytes[5].is_ascii_digit()
}

fn normalize_mysql_fractional_seconds(fraction: Option<&str>) -> String {
    match fraction {
        Some(fraction) if fraction.len() > 7 => fraction[..7].to_string(),
        Some(fraction) => fraction.to_string(),
        None => String::new(),
    }
}

fn is_temporal_column_type(data_type: &str) -> bool {
    temporal_column_kind(Some(data_type)).is_some()
}

fn temporal_column_kind(data_type: Option<&str>) -> Option<&'static str> {
    let base =
        data_type.unwrap_or("").trim().to_ascii_lowercase().split(['(', ':', ' ']).next().unwrap_or("").to_string();
    match base.as_str() {
        "date" => Some("date"),
        "time" => Some("time"),
        "datetime" | "timestamp" => Some("datetime"),
        _ => None,
    }
}

fn format_tdengine_timestamp_literal_text(text: &str) -> String {
    let Some((date, time, fraction)) = parse_tdengine_timestamp(text) else {
        return text.to_string();
    };
    format!(
        "{date}T{time}{}{suffix}",
        normalize_fractional_seconds(fraction.as_deref()),
        suffix = local_timezone_offset_suffix(text)
    )
}

fn parse_tdengine_timestamp(text: &str) -> Option<(String, String, Option<String>)> {
    if text.len() < 19 {
        return None;
    }
    let bytes = text.as_bytes();
    if bytes.get(4) != Some(&b'-') || bytes.get(7) != Some(&b'-') || bytes.get(10) != Some(&b' ') {
        return None;
    }
    if bytes.get(13) != Some(&b':') || bytes.get(16) != Some(&b':') {
        return None;
    }
    let date = text[0..10].to_string();
    let time = text.get(11..19)?.to_string();
    let rest = text.get(19..)?;
    if rest.is_empty() {
        return Some((date, time, None));
    }
    let fraction = rest.strip_prefix('.')?;
    if fraction.is_empty() || fraction.len() > 9 || !fraction.chars().all(|ch| ch.is_ascii_digit()) {
        return None;
    }
    Some((date, time, Some(format!(".{fraction}"))))
}

fn normalize_fractional_seconds(fraction: Option<&str>) -> String {
    match fraction {
        Some(fraction) if fraction.len() >= 4 => fraction[..4].to_string(),
        Some(fraction) => format!("{fraction:0<4}"),
        None => ".000".to_string(),
    }
}

fn local_timezone_offset_suffix(text: &str) -> String {
    let naive = NaiveDateTime::parse_from_str(&text.replace(' ', "T"), "%Y-%m-%dT%H:%M:%S%.f").ok();
    let offset_minutes = naive
        .and_then(|dt| {
            let local = dt.and_local_timezone(Local).earliest()?;
            Some(local.offset().local_minus_utc() / -60)
        })
        .unwrap_or_else(|| Local::now().offset().local_minus_utc() / -60);
    let sign = if offset_minutes <= 0 { "+" } else { "-" };
    let abs = offset_minutes.abs();
    format!("{sign}{:02}:{:02}", abs / 60, abs % 60)
}

fn build_primary_key_where(
    database_type: Option<DatabaseType>,
    primary_keys: &[String],
    columns: &[Option<String>],
    row: &[Value],
    column_info: &[DataGridColumnInfo],
    identifier_quote: Option<&str>,
) -> String {
    build_primary_key_predicate(database_type, primary_keys, columns, row, column_info, identifier_quote).sql
}

/// Builds a row's key `WHERE` clause and, for a single-column key, the structured
/// equality the MySQL batching may merge into an `IN` list. Both come out of the
/// same rendering call, so the merge decision can never be made against a clause
/// that differs from the one the statement carries.
fn build_primary_key_predicate(
    database_type: Option<DatabaseType>,
    primary_keys: &[String],
    columns: &[Option<String>],
    row: &[Value],
    column_info: &[DataGridColumnInfo],
    identifier_quote: Option<&str>,
) -> MysqlKeyPredicate {
    if primary_keys.is_empty() && uses_keyless_row_predicate(database_type) {
        if uses_postgres_ctid(database_type) {
            if let Some(row_id_index) = find_column_index(database_type, columns, DBX_ROWID_COLUMN) {
                // `validate_postgres_ctid_row_identifier` already refused every
                // touched row whose locator does not parse, so this fallback only
                // keeps a malformed caller from generating an unguarded write.
                return MysqlKeyPredicate {
                    sql: build_postgres_ctid_predicate(row.get(row_id_index).unwrap_or(&Value::Null))
                        .unwrap_or_else(|| "FALSE".to_string()),
                    // A physical row locator is a single equality set, not a
                    // mergeable plain column equality, and PostgreSQL never
                    // batches grid writes anyway.
                    plain: None,
                };
            }
        }
        // A keyless row predicate matches every column value: never one plain
        // equality, and never batched (`supports_mysql_data_grid_batch`).
        return MysqlKeyPredicate {
            sql: build_row_where(database_type, columns, row, column_info, identifier_quote),
            plain: None,
        };
    }

    let single_key = primary_keys.len() == 1;
    let mut plain = None;
    let mut parts = Vec::with_capacity(primary_keys.len());
    for primary_key in primary_keys {
        let value = row
            .get(find_column_index(database_type, columns, primary_key).unwrap_or(usize::MAX))
            .unwrap_or(&Value::Null);
        let (part, part_plain) = build_column_predicate_with_plain(
            database_type,
            primary_key,
            value,
            column_info_for(column_info, primary_key),
            false,
            identifier_quote,
        );
        parts.push(part);
        if single_key {
            plain = part_plain;
        }
    }
    let sql = parts.join(" AND ");
    let plain = plain.and_then(|plain| plain_equality_from_clause(&sql, plain));
    MysqlKeyPredicate { sql, plain }
}

/// Accepts the structured equality only when re-rendering it as
/// `{column} = {literal}` reproduces the clause about to be sent, modulo the
/// outer parentheses a batched part may add. `build_column_predicate` stays the
/// single source of truth for that text: if a future branch there renders another
/// form (a new wrapper, a different comparison), this check fails and the
/// predicate keeps the OR form instead of silently merging into an `IN` list that
/// targets rows the clause alone would not.
fn plain_equality_from_clause(clause: &str, plain: MysqlPlainEquality) -> Option<MysqlPlainEquality> {
    // Keep the retired text parser's explicit rejection of a bare `NULL` literal.
    // A NULL key value already leaves through the `IS NULL` branch, so this only
    // guards a formatter change that would emit the keyword for a real value:
    // `col = NULL` and `col IN (NULL)` both match nothing, but an `IN (NULL, ...)`
    // list would still read as a bug in the SQL a DBA has to approve.
    if plain.literal.eq_ignore_ascii_case("null") {
        return None;
    }
    let expected = format!("{} = {}", plain.column, plain.literal);
    (strip_outer_parentheses(clause) == expected).then_some(plain)
}

fn strip_outer_parentheses(text: &str) -> &str {
    let mut trimmed = text.trim();
    while let Some(inner) = trimmed.strip_prefix('(').and_then(|inner| inner.strip_suffix(')')) {
        trimmed = inner;
    }
    trimmed
}

fn build_row_where(
    database_type: Option<DatabaseType>,
    columns: &[Option<String>],
    row: &[Value],
    column_info: &[DataGridColumnInfo],
    identifier_quote: Option<&str>,
) -> String {
    columns
        .iter()
        .enumerate()
        .filter_map(|(index, column)| {
            let column = column.as_deref()?;
            if is_synthetic_row_id(database_type, Some(column)) {
                return None;
            }
            Some(build_column_predicate(
                database_type,
                column,
                row.get(index).unwrap_or(&Value::Null),
                column_info_for(column_info, column),
                true,
                identifier_quote,
            ))
        })
        .collect::<Vec<_>>()
        .join(" AND ")
}

fn build_save_row_where(
    database_type: Option<DatabaseType>,
    columns: &[Option<String>],
    row: &[Value],
    column_info: &[DataGridColumnInfo],
    identifier_quote: Option<&str>,
) -> String {
    columns
        .iter()
        .enumerate()
        .filter_map(|(index, column)| {
            let column = column.as_deref()?;
            if is_synthetic_row_id(database_type, Some(column)) {
                return None;
            }
            Some(build_save_column_predicate(
                database_type,
                column,
                row.get(index).unwrap_or(&Value::Null),
                column_info_for(column_info, column),
                true,
                identifier_quote,
            ))
        })
        .collect::<Vec<_>>()
        .join(" AND ")
}

pub fn build_column_predicate(
    database_type: Option<DatabaseType>,
    column: &str,
    value: &Value,
    column_info: Option<&DataGridColumnInfo>,
    use_binary_text_comparison: bool,
    identifier_quote: Option<&str>,
) -> String {
    build_column_predicate_with_plain(
        database_type,
        column,
        value,
        column_info,
        use_binary_text_comparison,
        identifier_quote,
    )
    .0
}

/// Renders one key predicate and, alongside it, the structured
/// `` (identifier, literal) `` pair when the rendering is the plain
/// `column = literal` equality. The pair is a *claim* about the text returned
/// here, not an independent derivation: callers must confirm it still reproduces
/// that text (`plain_equality_from_clause`) before merging anything.
fn build_column_predicate_with_plain(
    database_type: Option<DatabaseType>,
    column: &str,
    value: &Value,
    column_info: Option<&DataGridColumnInfo>,
    use_binary_text_comparison: bool,
    identifier_quote: Option<&str>,
) -> (String, Option<MysqlPlainEquality>) {
    let ident = predicate_ident(database_type, column, identifier_quote);
    if value.is_null() {
        return (format!("{ident} IS NULL"), None);
    }
    if let Some(predicate) = oceanbase_lob_predicate(database_type, &ident, value, column_info) {
        return (predicate, None);
    }
    if use_binary_text_comparison && uses_mysql_binary_text_predicate(database_type, value, column_info) {
        return (
            format!(
                "BINARY {ident} = {}",
                format_grid_assignment_sql_literal(value, database_type, column_info, identifier_quote)
            ),
            None,
        );
    }
    let literal = format_grid_assignment_sql_literal(value, database_type, column_info, identifier_quote);
    if use_binary_text_comparison {
        if let Some(predicate) = postgres_keyless_json_predicate(database_type, &ident, &literal, column_info) {
            return (predicate, None);
        }
    }
    let sql = format!("{ident} = {}", mysql_json_predicate_literal(literal.clone(), database_type, column_info));
    (sql, Some(MysqlPlainEquality { column: ident, literal }))
}

fn build_save_column_predicate(
    database_type: Option<DatabaseType>,
    column: &str,
    value: &Value,
    column_info: Option<&DataGridColumnInfo>,
    use_binary_text_comparison: bool,
    identifier_quote: Option<&str>,
) -> String {
    let ident = predicate_ident(database_type, column, identifier_quote);
    if value.is_null() || empty_string_saves_as_null(value, column_info) {
        format!("{ident} IS NULL")
    } else if let Some(predicate) = oceanbase_lob_predicate(database_type, &ident, value, column_info) {
        predicate
    } else if use_binary_text_comparison && uses_mysql_binary_text_predicate(database_type, value, column_info) {
        format!(
            "BINARY {ident} = {}",
            format_grid_save_sql_literal(value, database_type, column_info, identifier_quote)
        )
    } else {
        let literal = format_grid_save_sql_literal(value, database_type, column_info, identifier_quote);
        if use_binary_text_comparison {
            if let Some(predicate) = postgres_keyless_json_predicate(database_type, &ident, &literal, column_info) {
                return predicate;
            }
        }
        format!("{ident} = {}", mysql_json_predicate_literal(literal, database_type, column_info))
    }
}

fn oceanbase_lob_predicate(
    database_type: Option<DatabaseType>,
    ident: &str,
    value: &Value,
    column_info: Option<&DataGridColumnInfo>,
) -> Option<String> {
    if is_oceanbase_blob_column(database_type, column_info) {
        if value.is_null() {
            return Some(format!("{ident} IS NULL"));
        }
        let Some(hex) = oceanbase_blob_hex(value) else {
            return Some("1 = 0".to_string());
        };
        if hex.len() > 4000 {
            // Reuse the temporary BLOB bind in oceanbase_blob_statement instead of
            // compiling one RAW predicate per 1000 bytes of the original value.
            return Some(format!(
                "(DBMS_LOB.GETLENGTH({ident}) = {} AND DBMS_LOB.COMPARE({ident}, TO_BLOB(HEXTORAW('{hex}'))) = 0)",
                hex.len() / 2
            ));
        }
        let mut predicates = vec![format!("DBMS_LOB.GETLENGTH({ident}) = {}", hex.len() / 2)];
        for (index, chunk) in hex.as_bytes().chunks(2000).enumerate() {
            let chunk = std::str::from_utf8(chunk).expect("validated ASCII hex");
            predicates.push(format!(
                "DBMS_LOB.SUBSTR({ident}, {}, {}) = HEXTORAW('{chunk}')",
                chunk.len() / 2,
                index * 1000 + 1
            ));
        }
        return Some(format!("({})", predicates.join(" AND ")));
    }
    if database_type != Some(DatabaseType::OceanbaseOracle)
        || column_info.and_then(|column| oracle_character_lob_constructor(&column.data_type)) != Some("TO_CLOB")
    {
        return None;
    }
    if value.is_null() {
        return Some(format!("{ident} IS NULL"));
    }
    let text = value.as_str()?;
    let mut predicates = vec![format!("DBMS_LOB.GETLENGTH({ident}) = {}", text.chars().count())];
    let mut offset = 1;
    let mut chunk = String::new();
    let mut count = 0;
    for ch in text.chars() {
        if chunk.len() + oracle_sql_literal_char_len(ch) > 2000 {
            predicates.push(format!(
                "UTL_RAW.CAST_TO_RAW(DBMS_LOB.SUBSTR({ident}, {count}, {offset})) = UTL_RAW.CAST_TO_RAW('{chunk}')"
            ));
            offset += count;
            chunk.clear();
            count = 0;
        }
        append_oracle_sql_literal_char(&mut chunk, ch);
        count += 1;
    }
    if count > 0 {
        predicates.push(format!(
            "UTL_RAW.CAST_TO_RAW(DBMS_LOB.SUBSTR({ident}, {count}, {offset})) = UTL_RAW.CAST_TO_RAW('{chunk}')"
        ));
    }
    Some(format!("({})", predicates.join(" AND ")))
}

fn append_oceanbase_lob_save_guards(
    options: &DataGridSaveStatementOptions,
    columns: &[Option<String>],
    row: &[Value],
    indexes: impl Iterator<Item = usize>,
    where_clause: &mut String,
) -> bool {
    let mut guarded = false;
    let column_info = options.table_meta.columns.as_deref().unwrap_or(&[]);
    for index in indexes {
        let Some(column) = columns.get(index).and_then(|column| column.as_deref()) else {
            continue;
        };
        let ident = predicate_ident(options.database_type, column, options.identifier_quote.as_deref());
        if let Some(predicate) = oceanbase_lob_predicate(
            options.database_type,
            &ident,
            row.get(index).unwrap_or(&Value::Null),
            column_info_for(column_info, column),
        ) {
            if !where_clause.is_empty() {
                where_clause.push_str(" AND ");
            }
            where_clause.push_str(&predicate);
            guarded = true;
        }
    }
    guarded
}

fn data_grid_save_statement(
    database_type: Option<DatabaseType>,
    sql: String,
    guard_row_count: bool,
    bound: &mut Vec<dbx_types::types::BlobBoundStatement>,
) -> String {
    let preview = if guard_row_count {
        oceanbase_guarded_lob_statement(sql.clone())
    } else {
        data_grid_statement(database_type, sql.clone())
    };
    if database_type == Some(DatabaseType::OceanbaseOracle) {
        let (rewritten, blobs) = oceanbase_blob_literals(&sql, true);
        let execution_sql = if blobs.is_empty() {
            preview.clone()
        } else if guard_row_count {
            format!("BEGIN\n{rewritten};\nIF SQL%ROWCOUNT <> 1 THEN RAISE_APPLICATION_ERROR(-20001, 'LOB target changed or missing; reload before saving'); END IF;\nEND;")
        } else {
            rewritten
        };
        bound.push(dbx_types::types::BlobBoundStatement {
            preview_sql: preview.clone(),
            sql: execution_sql,
            blob_parameters: blobs.into_iter().map(str::to_string).collect(),
        });
    }
    preview
}

fn oceanbase_guarded_lob_statement(sql: String) -> String {
    if let Some(statement) = oceanbase_blob_statement(&sql, true) {
        return statement;
    }
    format!("BEGIN\n{sql};\nIF SQL%ROWCOUNT <> 1 THEN\nRAISE_APPLICATION_ERROR(-20001, 'LOB target changed or missing; reload before saving');\nEND IF;\nEND;")
}

fn postgres_keyless_json_predicate(
    database_type: Option<DatabaseType>,
    ident: &str,
    literal: &str,
    column_info: Option<&DataGridColumnInfo>,
) -> Option<String> {
    if !is_postgres_like_pattern_database(database_type) {
        return None;
    }
    let normalized = column_info?.data_type.trim().to_ascii_lowercase();
    let data_type = normalized.rsplit('.').next().unwrap_or(&normalized).trim_matches('"');
    match data_type {
        "json" => Some(format!("{ident}::text = {literal}::text")),
        "jsonb" => Some(format!("{ident} = {literal}::jsonb")),
        _ => None,
    }
}

fn mysql_json_predicate_literal(
    literal: String,
    database_type: Option<DatabaseType>,
    column_info: Option<&DataGridColumnInfo>,
) -> String {
    if database_type == Some(DatabaseType::Mysql)
        && column_info.is_some_and(|column| column.data_type.trim().eq_ignore_ascii_case("json"))
    {
        format!("CAST({literal} AS JSON)")
    } else {
        literal
    }
}

pub(crate) fn data_grid_statement(database_type: Option<DatabaseType>, sql: String) -> String {
    if database_type == Some(DatabaseType::OceanbaseOracle) {
        if let Some(statement) = oceanbase_blob_statement(&sql, false) {
            return statement;
        }
    }
    if database_type == Some(DatabaseType::ManticoreSearch) {
        sql
    } else {
        format!("{sql};")
    }
}

fn is_oceanbase_blob_column(database_type: Option<DatabaseType>, column_info: Option<&DataGridColumnInfo>) -> bool {
    database_type == Some(DatabaseType::OceanbaseOracle)
        && column_info.is_some_and(|column| column.data_type.trim().eq_ignore_ascii_case("BLOB"))
}

fn oceanbase_blob_hex(value: &Value) -> Option<&str> {
    let hex = value.as_str()?.strip_prefix("0x")?;
    (hex.len() % 2 == 0 && hex.bytes().all(|byte| byte.is_ascii_hexdigit())).then_some(hex)
}

/// Reuse grid LOB constructors without applying save-time transaction or row guards.
pub fn format_oracle_lob_export_literal(
    value: &Value,
    database_type: Option<DatabaseType>,
    data_type: Option<&str>,
) -> Result<Option<String>, String> {
    if !is_oracle_temporal_literal_database(database_type) {
        return Ok(None);
    }
    let Some(data_type) = data_type else { return Ok(None) };
    let blob = database_type == Some(DatabaseType::OceanbaseOracle) && data_type.trim().eq_ignore_ascii_case("BLOB");
    if !blob && oracle_character_lob_constructor(data_type).is_none() {
        return Ok(None);
    }
    if !blob && value.as_str() == Some("") {
        return Ok(Some("EMPTY_CLOB()".to_string()));
    }
    if blob && !value.is_null() && oceanbase_blob_hex(value).is_none() {
        return Err(
            "OceanBase BLOB export requires NULL or 0x followed by complete hexadecimal byte pairs.".to_string()
        );
    }
    let column = DataGridColumnInfo {
        name: String::new(),
        data_type: data_type.to_string(),
        is_nullable: true,
        is_primary_key: false,
        column_default: None,
        extra: None,
    };
    Ok(Some(format_grid_assignment_sql_literal(value, database_type, Some(&column), None)))
}

fn validate_oceanbase_blob_save(options: &DataGridSaveStatementOptions) -> Option<String> {
    if options.database_type != Some(DatabaseType::OceanbaseOracle) {
        return None;
    }
    let columns = effective_columns(options);
    let info = options.table_meta.columns.as_deref().unwrap_or(&[]);
    let valid = |index: usize, value: &Value| {
        !is_oceanbase_blob_column(
            options.database_type,
            columns.get(index).and_then(|column| column.as_deref()).and_then(|column| column_info_for(info, column)),
        ) || value.is_null()
            || oceanbase_blob_hex(value).is_some()
    };
    for (row_index, changes) in &options.dirty_rows {
        for (index, value) in changes {
            if !valid(*index, value)
                || options
                    .rows
                    .get(*row_index)
                    .is_some_and(|row| !valid(*index, row.get(*index).unwrap_or(&Value::Null)))
            {
                return Some("OceanBase BLOB values must be NULL or 0x followed by complete hexadecimal byte pairs; reload the original value before saving.".to_string());
            }
        }
    }
    for row in options.deleted_rows.iter().filter_map(|index| options.rows.get(*index)).chain(options.new_rows.iter()) {
        if row.iter().enumerate().any(|(index, value)| !valid(index, value)) {
            return Some("OceanBase BLOB values must be NULL or 0x followed by complete hexadecimal byte pairs; reload the original value before saving.".to_string());
        }
    }
    None
}

// Only rewrite our generated BLOB literal outside quoted strings/identifiers. The SQL text
// uses binds so temporary PL variables cannot resolve to an identically named table column.
pub fn oceanbase_blob_statement(sql: &str, guard_row_count: bool) -> Option<String> {
    let (rewritten, blobs) = oceanbase_blob_literals(sql, false);
    if blobs.is_empty() {
        return None;
    }
    oceanbase_temporary_blob_statement(&rewritten, &blobs, guard_row_count)
}

fn oceanbase_blob_literals(sql: &str, positional: bool) -> (String, Vec<&str>) {
    const PREFIX: &str = "TO_BLOB(HEXTORAW('";
    let sql = sql.trim_end_matches(';');
    let mut rewritten = String::new();
    let mut blobs = Vec::new();
    let mut index = 0;
    while index < sql.len() {
        let rest = &sql[index..];
        if let Some(hex_start) = rest.strip_prefix(PREFIX) {
            if let Some(end) = hex_start.find("'))") {
                let hex = &hex_start[..end];
                if hex.len() > 4000 && hex.len() % 2 == 0 && hex.bytes().all(|byte| byte.is_ascii_hexdigit()) {
                    rewritten.push_str(&if positional { "?".to_string() } else { format!(":dbx_lob_{}", blobs.len()) });
                    blobs.push(hex);
                    index += PREFIX.len() + end + 3;
                    continue;
                }
            }
        }
        let ch = rest.chars().next().expect("nonempty SQL remainder");
        rewritten.push(ch);
        index += ch.len_utf8();
        if ch == '\'' || ch == '"' {
            while index < sql.len() {
                let quoted = sql[index..].chars().next().expect("nonempty quoted remainder");
                rewritten.push(quoted);
                index += quoted.len_utf8();
                if quoted == ch {
                    if sql[index..].starts_with(ch) {
                        rewritten.push(ch);
                        index += ch.len_utf8();
                    } else {
                        break;
                    }
                }
            }
        }
    }
    (rewritten, blobs)
}

fn oceanbase_temporary_blob_statement(rewritten: &str, blobs: &[&str], guard_row_count: bool) -> Option<String> {
    let mut statement = "DECLARE\ndbx_sql CLOB;\n".to_string();
    let mut cleanup = String::new();
    for index in 0..blobs.len() {
        statement.push_str(&format!("dbx_lob_{index} BLOB;\n"));
        cleanup.push_str(&format!(
            "IF DBMS_LOB.ISTEMPORARY(dbx_lob_{index}) = 1 THEN DBMS_LOB.FREETEMPORARY(dbx_lob_{index}); END IF;\n"
        ));
    }
    statement.push_str("BEGIN\ndbx_sql := ");
    statement.push_str(&format_oracle_lob_assignment_literal(rewritten, "TO_CLOB"));
    statement.push_str(";\n");
    for (index, hex) in blobs.iter().enumerate() {
        statement.push_str(&format!("DBMS_LOB.CREATETEMPORARY(dbx_lob_{index}, TRUE, DBMS_LOB.CALL);\n"));
        for chunk in hex.as_bytes().chunks(2000) {
            let chunk = std::str::from_utf8(chunk).expect("validated ASCII hex");
            statement.push_str(&format!(
                "DBMS_LOB.WRITEAPPEND(dbx_lob_{index}, {}, HEXTORAW('{chunk}'));\n",
                chunk.len() / 2
            ));
        }
    }
    statement.push_str(&format!(
        "EXECUTE IMMEDIATE dbx_sql USING {};\n",
        (0..blobs.len()).map(|index| format!("dbx_lob_{index}")).collect::<Vec<_>>().join(", ")
    ));
    if guard_row_count {
        statement.push_str("IF SQL%ROWCOUNT <> 1 THEN RAISE_APPLICATION_ERROR(-20001, 'LOB target changed or missing; reload before saving'); END IF;\n");
    }
    cleanup.push_str("IF DBMS_LOB.ISTEMPORARY(dbx_sql) = 1 THEN DBMS_LOB.FREETEMPORARY(dbx_sql); END IF;\n");
    statement.push_str(&cleanup);
    statement.push_str("EXCEPTION WHEN OTHERS THEN\n");
    statement.push_str(&cleanup);
    statement.push_str("RAISE;\nEND;");
    Some(statement)
}

fn data_grid_update_sql(database_type: Option<DatabaseType>, table: &str, sets: &str, where_clause: &str) -> String {
    if database_type == Some(DatabaseType::ClickHouse) {
        format!("ALTER TABLE {table} UPDATE {sets} WHERE {where_clause}")
    } else {
        format!("UPDATE {table} SET {sets} WHERE {where_clause}")
    }
}

pub(crate) fn data_grid_delete_sql(database_type: Option<DatabaseType>, table: &str, where_clause: &str) -> String {
    if database_type == Some(DatabaseType::ClickHouse) {
        format!("ALTER TABLE {table} DELETE WHERE {where_clause}")
    } else {
        format!("DELETE FROM {table} WHERE {where_clause}")
    }
}

fn uses_mysql_binary_text_predicate(
    database_type: Option<DatabaseType>,
    value: &Value,
    column_info: Option<&DataGridColumnInfo>,
) -> bool {
    database_type == Some(DatabaseType::Mysql)
        && value.is_string()
        && column_info.map(|column| is_textual_column_type(&column.data_type)).unwrap_or(false)
}

fn is_textual_column_type(data_type: &str) -> bool {
    let lower = data_type.trim().to_ascii_lowercase();
    let base = lower.split(['(', ':', ' ']).next().unwrap_or("").trim();
    matches!(
        base,
        "char"
            | "character"
            | "varchar"
            | "varchar2"
            | "nvarchar"
            | "nvarchar2"
            | "nchar"
            | "string"
            | "text"
            | "tinytext"
            | "mediumtext"
            | "longtext"
            | "ntext"
            | "clob"
            | "nclob"
            | "enum"
            | "set"
    ) || lower.starts_with("character varying")
        || lower.starts_with("national character varying")
}

fn is_oracle_lob_type(data_type: &str) -> bool {
    let lower = data_type.trim().trim_matches('"').to_ascii_lowercase();
    let base = lower.split(['(', ':', ' ']).next().unwrap_or("");
    matches!(base, "blob" | "clob" | "nclob" | "bfile" | "lob")
        || lower.starts_with("binary large object")
        || lower.starts_with("character large object")
}

/// Dameng binary LOB types have no comparison operator: `=`, `LIKE` and even
/// `col = col` all raise `Data type mismatch`, so they cannot be part of a
/// keyless row predicate. Spatial types are BLOB-backed and behave the same.
fn is_dameng_uncomparable_lob_type(data_type: &str) -> bool {
    let lower = data_type.trim().trim_matches('"').to_ascii_lowercase();
    if lower.starts_with("binary large object") {
        return true;
    }
    let head = lower.split(['(', ':', ' ']).next().unwrap_or("");
    let base = head.rsplit('.').next().unwrap_or(head).trim();
    matches!(base, "blob" | "image" | "longvarbinary" | "bfile" | "geometry") || base.ends_with("_geometry")
}

fn oracle_character_lob_constructor(data_type: &str) -> Option<&'static str> {
    let lower = data_type.trim().trim_matches('"').to_ascii_lowercase();
    let base = lower.split(['(', ':', ' ']).next().unwrap_or("");
    match base {
        "clob" => Some("TO_CLOB"),
        "nclob" => Some("TO_NCLOB"),
        _ if lower.starts_with("character large object") => Some("TO_CLOB"),
        _ => None,
    }
}

fn is_synthetic_row_id(database_type: Option<DatabaseType>, name: Option<&str>) -> bool {
    uses_synthetic_row_id(database_type) && name.is_some_and(|name| name.eq_ignore_ascii_case(DBX_ROWID_COLUMN))
}

fn parse_postgres_ctid_row_id(value: &Value) -> Option<(u32, String, u32)> {
    let value = value.as_str()?;
    let (table_oid, rest) = value.split_once(':')?;
    if table_oid.is_empty() || !table_oid.bytes().all(|byte| byte.is_ascii_digit()) {
        return None;
    }
    let table_oid = table_oid.parse::<u32>().ok()?;
    let (ctid, xmin) = rest.split_once(':')?;
    let ctid = ctid.strip_prefix('(')?.strip_suffix(')')?;
    let (block, offset) = ctid.split_once(',')?;
    if block.is_empty()
        || offset.is_empty()
        || !block.bytes().all(|byte| byte.is_ascii_digit())
        || !offset.bytes().all(|byte| byte.is_ascii_digit())
    {
        return None;
    }
    let block = block.parse::<u32>().ok()?;
    let offset = offset.parse::<u16>().ok()?;
    if xmin.is_empty() || !xmin.bytes().all(|byte| byte.is_ascii_digit()) {
        return None;
    }
    let xmin = xmin.parse::<u32>().ok()?;
    Some((table_oid, format!("({block},{offset})"), xmin))
}

fn build_postgres_ctid_predicate(value: &Value) -> Option<String> {
    let (table_oid, ctid, xmin) = parse_postgres_ctid_row_id(value)?;
    Some(format!("tableoid = '{table_oid}'::oid AND ctid = '{ctid}'::tid AND xmin = '{xmin}'::xid"))
}

pub fn is_neo4j_element_id(database_type: Option<DatabaseType>, name: Option<&str>) -> bool {
    database_type == Some(DatabaseType::Neo4j) && name == Some(DBX_NEO4J_ELEMENT_ID_COLUMN)
}

pub fn extra_is_auto_generated(extra: &str) -> bool {
    extra.to_ascii_lowercase().split(|ch: char| !ch.is_ascii_alphanumeric() && ch != '_').any(|part| {
        matches!(part, "auto_increment" | "autoincrement" | "identity" | "smallserial" | "serial" | "bigserial")
    })
}

pub fn is_auto_generated_column(column: &DataGridColumnInfo) -> bool {
    extra_is_auto_generated(column.extra.as_deref().unwrap_or(""))
        || column.column_default.as_deref().is_some_and(|default| default.to_ascii_lowercase().contains("nextval("))
}

fn grid_value_is_empty(value: &Value) -> bool {
    value.is_null() || value.as_str().is_some_and(str::is_empty)
}

pub fn is_grid_insert_omitted_column(
    database_type: Option<DatabaseType>,
    column_info: Option<&DataGridColumnInfo>,
    name: Option<&str>,
    include_computed_columns: bool,
) -> bool {
    is_synthetic_row_id(database_type, name)
        || is_postgres_tsvector_column(database_type, column_info)
        || is_sqlserver_rowversion_column(database_type, column_info)
        || (!include_computed_columns
            && (is_non_identity_generated_column(column_info)
                || is_sqlserver_computed_column(database_type, column_info)))
}

fn is_grid_update_omitted_column(
    database_type: Option<DatabaseType>,
    column_info: Option<&DataGridColumnInfo>,
    name: Option<&str>,
    primary_key_set: &[String],
) -> bool {
    is_synthetic_row_id(database_type, name)
        || is_clickhouse_key_column(database_type, column_info, name, primary_key_set)
        || is_non_identity_generated_column(column_info)
}

fn is_clickhouse_key_column(
    database_type: Option<DatabaseType>,
    column_info: Option<&DataGridColumnInfo>,
    name: Option<&str>,
    primary_key_set: &[String],
) -> bool {
    if database_type != Some(DatabaseType::ClickHouse) {
        return false;
    }
    column_info.is_some_and(|column| column.is_primary_key)
        || is_clickhouse_partition_key_column(database_type, column_info)
        || name.is_some_and(|name| primary_key_set.contains(&normalize_column_name(name)))
}

fn is_clickhouse_partition_key_column(
    database_type: Option<DatabaseType>,
    column_info: Option<&DataGridColumnInfo>,
) -> bool {
    database_type == Some(DatabaseType::ClickHouse)
        && column_info.and_then(|column| column.extra.as_deref()).is_some_and(|extra| {
            extra.split(|ch: char| !ch.is_ascii_alphanumeric() && ch != '_').any(|part| part == "partition_key")
        })
}

fn is_postgres_tsvector_column(database_type: Option<DatabaseType>, column_info: Option<&DataGridColumnInfo>) -> bool {
    database_type == Some(DatabaseType::Postgres)
        && column_info.map(|column| is_postgres_tsvector_type(&column.data_type)).unwrap_or(false)
}

fn is_postgres_tsvector_type(data_type: &str) -> bool {
    let normalized = data_type.trim().trim_matches('"').to_ascii_lowercase();
    normalized == "tsvector" || normalized.ends_with(".tsvector")
}

/// SQL Server `timestamp`/`rowversion` columns are server-generated counters:
/// like PostgreSQL `tsvector` they can never take an explicit INSERT value
/// ("Cannot insert an explicit value into a timestamp column"), so they are
/// omitted unconditionally. The grid copy path carries table metadata
/// (sys.columns) types here, where the column surfaces as `timestamp`;
/// result-set types (`varbinary` on TDS) would not identify it.
fn is_sqlserver_rowversion_column(
    database_type: Option<DatabaseType>,
    column_info: Option<&DataGridColumnInfo>,
) -> bool {
    database_type == Some(DatabaseType::SqlServer)
        && column_info.map(|column| is_sqlserver_rowversion_type(&column.data_type)).unwrap_or(false)
}

fn is_sqlserver_rowversion_type(data_type: &str) -> bool {
    let normalized = data_type.trim().trim_matches('"').to_ascii_lowercase();
    let base = normalized.split(['(', ' ', '\t', '\n']).next().unwrap_or("").trim();
    matches!(base, "timestamp" | "rowversion")
}

/// SQL Server computed columns mark themselves with the bare `computed` string
/// in their metadata EXTRA (sys.columns), which the dialect-neutral generated
/// keywords ("generated always as"/"virtual generated"/"stored generated") do
/// not match; without this branch a copy INSERT would still write a value into
/// a computed column ("Cannot insert a value into the computed column").
fn is_sqlserver_computed_column(database_type: Option<DatabaseType>, column_info: Option<&DataGridColumnInfo>) -> bool {
    database_type == Some(DatabaseType::SqlServer)
        && column_info
            .and_then(|column| column.extra.as_deref())
            .is_some_and(|extra| extra.trim().eq_ignore_ascii_case("computed"))
}

pub fn is_non_identity_generated_column(column_info: Option<&DataGridColumnInfo>) -> bool {
    let extra = column_info.and_then(|column| column.extra.as_deref()).unwrap_or("").to_ascii_lowercase();
    (extra.contains("generated always as") || extra.contains("virtual generated") || extra.contains("stored generated"))
        && !extra.contains("identity")
}

fn is_null_write_to_not_null_column(
    database_type: Option<DatabaseType>,
    not_null_columns: &[String],
    column: Option<&str>,
    value: &Value,
) -> bool {
    let Some(column) = column else {
        return false;
    };
    if is_synthetic_row_id(database_type, Some(column)) || is_neo4j_element_id(database_type, Some(column)) {
        return false;
    }
    value.is_null() && not_null_columns.iter().any(|not_null| not_null == &normalize_column_name(column))
}

fn find_column_index(database_type: Option<DatabaseType>, columns: &[Option<String>], target: &str) -> Option<usize> {
    if let Some(index) = columns.iter().position(|column| column.as_deref() == Some(target)) {
        return Some(index);
    }
    // PostgreSQL can have distinct `id` and quoted `"ID"` columns. Only
    // dialects whose result metadata is known to drift in case may fall back,
    // and even then a case-only match must be unique. Vastbase reports result
    // labels upper-cased (`ID`) while primary-key metadata keeps the stored
    // spelling (`id`), so the grid's primary-key badge and the save path have
    // to agree on the same column (#8797).
    if !matches!(
        database_type,
        Some(
            DatabaseType::Goldendb
                | DatabaseType::Kingbase
                | DatabaseType::Tdengine
                | DatabaseType::Hive
                | DatabaseType::Vastbase
        )
    ) {
        return None;
    }
    let normalized_target = normalize_column_name(target);
    let mut matches = columns.iter().enumerate().filter_map(|(index, column)| {
        (column.as_deref().map(normalize_column_name).unwrap_or_default() == normalized_target).then_some(index)
    });
    let first = matches.next()?;
    matches.next().is_none().then_some(first)
}

fn primary_key_value_key(primary_key_indexes: &[usize], row: &[Value]) -> Option<String> {
    let values: Vec<Value> =
        primary_key_indexes.iter().map(|index| row.get(*index).cloned().unwrap_or(Value::Null)).collect();
    if values.iter().any(Value::is_null) {
        return None;
    }
    serde_json::to_string(&values).ok()
}

fn duplicate_primary_key_error(
    primary_keys: &[String],
    primary_key_indexes: &[usize],
    row: &[Value],
    matches_existing_row: bool,
) -> String {
    let key_summary = primary_keys
        .iter()
        .enumerate()
        .map(|(index, primary_key)| {
            format!(
                "{} = {}",
                primary_key,
                format_key_value_for_message(row.get(primary_key_indexes[index]).unwrap_or(&Value::Null))
            )
        })
        .collect::<Vec<_>>()
        .join(", ");
    let source = if matches_existing_row { "the existing primary key" } else { "another new row's primary key" };
    format!("New row duplicates {source} ({key_summary}). Change the key before saving.")
}

fn format_key_value_for_message(value: &Value) -> String {
    if value.is_null() {
        return "NULL".to_string();
    }
    if let Some(value) = value.as_str() {
        return format!("\"{}\"", value.replace('"', "\\\""));
    }
    value.to_string()
}

fn normalize_column_name(name: &str) -> String {
    name.to_ascii_uppercase()
}

pub(crate) fn is_primary_key_column(
    database_type: Option<DatabaseType>,
    primary_keys: &[String],
    column_name: &str,
    column_info: &[DataGridColumnInfo],
    known_columns: &[String],
) -> bool {
    if primary_keys.iter().any(|primary_key| primary_key == column_name) {
        return true;
    }

    let normalized = normalize_column_name(column_name);
    let matching_metadata: Vec<&DataGridColumnInfo> =
        column_info.iter().filter(|column| normalize_column_name(&column.name) == normalized).collect();
    let exact_metadata = matching_metadata.iter().find(|column| column.name == column_name);
    if exact_metadata.is_some_and(|metadata| metadata.is_primary_key) {
        return true;
    }
    if matches!(database_type, Some(DatabaseType::Postgres | DatabaseType::Redshift)) {
        return false;
    }
    if matching_metadata.len() == 1 {
        let metadata = matching_metadata[0];
        return metadata.is_primary_key
            || primary_keys.iter().filter(|primary_key| normalize_column_name(primary_key) == normalized).count() == 1;
    }
    if matching_metadata.len() > 1 {
        return false;
    }

    let visible_spellings: std::collections::HashSet<&str> =
        known_columns.iter().filter(|column| normalize_column_name(column) == normalized).map(String::as_str).collect();
    if visible_spellings.len() > 1 {
        return false;
    }
    primary_keys.iter().filter(|primary_key| normalize_column_name(primary_key) == normalized).count() == 1
}

fn null_write_error(column: &str) -> String {
    format!("Column \"{column}\" does not allow NULL.")
}

fn clickhouse_no_mutable_columns_error() -> String {
    "ClickHouse primary or partition key columns cannot be updated. Change a non-key column before saving.".to_string()
}

fn predicate_ident(database_type: Option<DatabaseType>, name: &str, identifier_quote: Option<&str>) -> String {
    if is_synthetic_row_id(database_type, Some(name)) {
        if uses_postgres_ctid(database_type) {
            return "(tableoid::text || ':' || ctid::text || ':' || xmin::text)".to_string();
        }
        if uses_xugu_row_id(database_type) {
            return "ROWID".to_string();
        }
        "ROWIDTOCHAR(ROWID)".to_string()
    } else {
        data_grid_identifier(database_type, name, identifier_quote)
    }
}

pub fn quote_ident(database_type: Option<DatabaseType>, name: &str) -> String {
    quote_table_identifier(database_type, name)
}

pub fn qualified_table_name(database_type: Option<DatabaseType>, schema: Option<&str>, table_name: &str) -> String {
    crate::sql_dialect::qualified_table_name(database_type, schema, table_name)
}

fn data_grid_identifier(database_type: Option<DatabaseType>, name: &str, identifier_quote: Option<&str>) -> String {
    if database_type == Some(DatabaseType::Iris) {
        return crate::sql_dialect::quote_iris_identifier(name, identifier_quote);
    }
    crate::sql_dialect::quote_table_data_identifier(database_type, name, identifier_quote)
}

/// Table reference for every generated data-grid SQL surface that honors the
/// `生成 SQL 时包含数据库名` setting: save / rollback / keyless-guard statements
/// and the copy-as-INSERT/UPDATE/SELECT statements.
///
/// With the setting on, engines whose active database is normally omitted get a
/// fully qualified name (`database.table`, or `database.schema.table` for SQL
/// Server, whose tables stay addressable across databases). Every other engine —
/// and the setting off — keeps the historical shape, so no existing SQL changes
/// shape unless the user opted in.
#[allow(clippy::too_many_arguments)]
pub fn data_grid_generated_table_name(
    database_type: Option<DatabaseType>,
    catalog: Option<&str>,
    schema: Option<&str>,
    database: Option<&str>,
    table_name: &str,
    identifier_quote: Option<&str>,
    include_database_name: bool,
) -> String {
    if include_database_name {
        if let Some(qualified) =
            crate::sql_dialect::database_qualified_table_name(database_type, catalog, schema, database, table_name)
        {
            return qualified;
        }
    }
    data_grid_qualified_table_name(database_type, catalog, schema, database, table_name, identifier_quote)
}

/// Table reference for the grid's save / rollback / keyless-guard statements.
///
/// Mirrors the data-table SELECT label and the copy-as-INSERT statements: when
/// `include_database_name` is on, engines addressed via `database.table`
/// (MySQL family, ClickHouse) get the database prefix — taken from
/// `table_meta.schema` when the table lives outside the connection's default
/// database, otherwise from `table_meta.database` — and SQL Server gets the
/// three-part `database.schema.table` form. Every other engine keeps the
/// historical shape.
fn data_grid_save_table_name(options: &DataGridSaveStatementOptions, schema: Option<&str>) -> String {
    data_grid_generated_table_name(
        options.database_type,
        options.table_meta.catalog.as_deref(),
        schema,
        options.table_meta.database.as_deref(),
        &options.table_meta.table_name,
        options.identifier_quote.as_deref(),
        options.include_database_name,
    )
}

pub fn data_grid_qualified_table_name(
    database_type: Option<DatabaseType>,
    catalog: Option<&str>,
    schema: Option<&str>,
    database: Option<&str>,
    table_name: &str,
    identifier_quote: Option<&str>,
) -> String {
    if database_type == Some(DatabaseType::Iris) {
        let table = crate::sql_dialect::quote_iris_identifier(table_name, identifier_quote);
        return schema
            .map(str::trim)
            .filter(|schema| !schema.is_empty())
            .map(|schema| format!("{}.{table}", crate::sql_dialect::quote_iris_identifier(schema, identifier_quote)))
            .unwrap_or(table);
    }
    if crate::sql_dialect::uses_connection_identifier_quote(database_type, identifier_quote) {
        crate::sql_dialect::table_data_qualified_table_name(database_type, schema, table_name, identifier_quote)
    } else {
        crate::sql_dialect::qualified_table_name_with_catalog(database_type, catalog, schema, database, table_name)
    }
}

fn column_filter_ref(database_type: Option<DatabaseType>, column_name: &str, identifier_quote: Option<&str>) -> String {
    let quoted = predicate_ident(database_type, column_name, identifier_quote);
    if database_type == Some(DatabaseType::Neo4j) {
        format!("n.{quoted}")
    } else {
        quoted
    }
}

fn column_like_filter_ref(
    database_type: Option<DatabaseType>,
    column_name: &str,
    column_info: Option<&DataGridColumnInfo>,
    identifier_quote: Option<&str>,
) -> String {
    let column = column_filter_ref(database_type, column_name, identifier_quote);
    if is_postgres_like_pattern_database(database_type)
        && column_info.map(|column_info| !is_textual_column_type(&column_info.data_type)).unwrap_or(true)
    {
        format!("{column}::text")
    } else {
        column
    }
}

fn is_postgres_like_pattern_database(database_type: Option<DatabaseType>) -> bool {
    matches!(
        database_type,
        Some(
            DatabaseType::Postgres
                | DatabaseType::Redshift
                | DatabaseType::Gaussdb
                | DatabaseType::Kwdb
                | DatabaseType::Kingbase
                | DatabaseType::Highgo
                | DatabaseType::Uxdb
                | DatabaseType::Vastbase
                | DatabaseType::Gbase8c
                | DatabaseType::OpenGauss
        )
    )
}

fn value_to_filter_text(value: &Value) -> String {
    if let Some(value) = value.as_str() {
        value.to_string()
    } else if value.is_null() {
        String::new()
    } else {
        value.to_string()
    }
}

fn parse_typed_filter_value(
    text: &str,
    database_type: Option<DatabaseType>,
    column_info: Option<&DataGridColumnInfo>,
) -> Value {
    let unquoted = unwrap_matching_quotes(text);
    let data_type = column_info.map(|column| column.data_type.to_ascii_lowercase()).unwrap_or_default();
    if is_boolean_type(&data_type, database_type) && unquoted.eq_ignore_ascii_case("true") {
        return Value::Bool(true);
    }
    if is_boolean_type(&data_type, database_type) && unquoted.eq_ignore_ascii_case("false") {
        return Value::Bool(false);
    }
    if (is_numeric_type(&data_type) || data_type.is_empty()) && is_numeric_literal(&unquoted) {
        if let Ok(number) = unquoted.parse::<serde_json::Number>() {
            return Value::Number(number);
        }
    }
    Value::String(unquoted)
}

fn unwrap_matching_quotes(text: &str) -> String {
    let mut chars = text.chars();
    let Some(first) = chars.next() else {
        return String::new();
    };
    let Some(last) = text.chars().last() else {
        return String::new();
    };
    if text.len() >= 2 && ((first == '\'' && last == '\'') || (first == '"' && last == '"')) {
        text[1..text.len() - 1].to_string()
    } else {
        text.to_string()
    }
}

fn is_numeric_type(data_type: &str) -> bool {
    let lower = data_type.to_ascii_lowercase();
    const NUMERIC_TOKENS: &[&str] = &[
        "int",
        "integer",
        "bigint",
        "smallint",
        "tinyint",
        "mediumint",
        "serial",
        "number",
        "numeric",
        "decimal",
        "float",
        "double",
        "real",
        "money",
    ];
    lower
        .split(|ch: char| !ch.is_ascii_alphanumeric())
        .any(|token| NUMERIC_TOKENS.contains(&token) || is_sized_numeric_token(token))
}

// Databases like Cloud Spanner (GoogleSQL) and ClickHouse spell their numeric
// types with an explicit bit width, e.g. INT64/FLOAT64/FLOAT32 or
// Int8/UInt64/Float64. Recognizing `int`/`uint`/`float` followed by digits keeps
// these as numeric SQL literals so filter values are not quoted as strings,
// which strongly typed engines reject (INT64 = STRING has no matching operator).
// `interval` and similar names stay excluded because they are not `int`+digits.
fn is_sized_numeric_token(token: &str) -> bool {
    for prefix in ["int", "uint", "float"] {
        if let Some(width) = token.strip_prefix(prefix) {
            if !width.is_empty() && width.bytes().all(|b| b.is_ascii_digit()) {
                return true;
            }
        }
    }
    false
}

fn is_boolean_type(data_type: &str, database_type: Option<DatabaseType>) -> bool {
    let lower = data_type.to_ascii_lowercase();
    lower.split(|ch: char| !ch.is_ascii_alphanumeric()).any(|token| {
        matches!(token, "bool" | "boolean")
            || (matches!(token, "bit" | "bitn") && database_type != Some(DatabaseType::Postgres))
    })
}

fn is_numeric_literal(text: &str) -> bool {
    if text.trim() != text || text.is_empty() {
        return false;
    }
    text.parse::<f64>().is_ok_and(f64::is_finite)
        && text.chars().all(|ch| ch.is_ascii_digit() || matches!(ch, '+' | '-' | '.' | 'e' | 'E'))
        && text.chars().any(|ch| ch.is_ascii_digit())
}

fn uses_keyless_row_predicate(database_type: Option<DatabaseType>) -> bool {
    matches!(
        database_type,
        Some(
            DatabaseType::Mysql
                | DatabaseType::ManticoreSearch
                | DatabaseType::Postgres
                | DatabaseType::Sqlite
                | DatabaseType::Rqlite
                | DatabaseType::Turso
                | DatabaseType::CloudflareD1
                | DatabaseType::DuckDb
                | DatabaseType::SqlServer
                | DatabaseType::Oracle
                | DatabaseType::Doris
                | DatabaseType::StarRocks
                | DatabaseType::Redshift
                | DatabaseType::Dameng
                | DatabaseType::Gaussdb
                | DatabaseType::Kwdb
                | DatabaseType::Kingbase
                | DatabaseType::Highgo
                | DatabaseType::Uxdb
                | DatabaseType::Gbase8c
                | DatabaseType::Vastbase
                | DatabaseType::Goldendb
                | DatabaseType::Yashandb
                | DatabaseType::Oscar
                | DatabaseType::Databricks
                | DatabaseType::SapHana
                | DatabaseType::Teradata
                | DatabaseType::Vertica
                | DatabaseType::Firebird
                | DatabaseType::Exasol
                | DatabaseType::OpenGauss
                | DatabaseType::Questdb
                | DatabaseType::OceanbaseOracle
                | DatabaseType::Gbase
                | DatabaseType::Gbase8s
                | DatabaseType::Access
                | DatabaseType::H2
                | DatabaseType::Snowflake
                | DatabaseType::Db2
                | DatabaseType::Informix
                | DatabaseType::Bigquery
                | DatabaseType::Sundb
                | DatabaseType::Databend
                | DatabaseType::Hive
                | DatabaseType::Iris
        )
    )
}

pub fn column_info_for<'a>(columns: &'a [DataGridColumnInfo], name: &str) -> Option<&'a DataGridColumnInfo> {
    let normalized = normalize_column_name(name);
    columns.iter().find(|column| normalize_column_name(&column.name) == normalized)
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::*;

    fn column(name: &str, data_type: &str, nullable: bool, extra: Option<&str>) -> DataGridColumnInfo {
        DataGridColumnInfo {
            name: name.to_string(),
            data_type: data_type.to_string(),
            is_nullable: nullable,
            is_primary_key: false,
            column_default: None,
            extra: extra.map(ToString::to_string),
        }
    }

    fn sundb_people_save_options() -> DataGridSaveStatementOptions {
        let mut options = mysql_people_save_options(1);
        options.database_type = Some(DatabaseType::Sundb);
        // SUNDB resolves unquoted names case-insensitively (folding to uppercase), so the
        // grid must not quote them: a quoted `"rs_salest_budget"` preserved the case and
        // matched nothing (#11986).
        options.table_meta.table_name = "rs_salest_budget".to_string();
        options.table_meta.schema = None;
        options.dirty_rows = vec![(0, vec![(1, json!("blocked"))])];
        options
    }

    #[test]
    fn sundb_grid_save_leaves_simple_identifiers_unquoted() {
        let statements =
            build_data_grid_save_statements(&sundb_people_save_options(), None, &mut Vec::new(), &mut Vec::new());

        assert_eq!(statements.len(), 1);
        assert!(statements[0].starts_with("UPDATE rs_salest_budget SET "), "{}", statements[0]);
        assert!(statements[0].contains("WHERE id = 1"), "{}", statements[0]);
        assert!(!statements[0].contains("\"rs_salest_budget\""), "{}", statements[0]);
    }

    fn mysql_people_save_options(row_count: usize) -> DataGridSaveStatementOptions {
        DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("app".to_string()),
                table_name: "people".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![column("id", "bigint", false, None), column("status", "varchar(32)", true, None)]),
            },
            columns: vec!["id".to_string(), "status".to_string()],
            source_columns: None,
            rows: (1..=row_count).map(|id| vec![json!(id), json!("active")]).collect(),
            dirty_rows: vec![],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        }
    }

    #[test]
    fn starrocks_insert_uses_server_defaults_for_unset_uuid_columns() {
        for (database_type, profile) in [(DatabaseType::StarRocks, None), (DatabaseType::Mysql, Some("starrocks"))] {
            for default in ["uuid()", "(uuid())", " (( UUID ( ) )) ", "uuid_numeric()", "(UUID_NUMERIC())"] {
                let mut options = mysql_people_save_options(0);
                options.database_type = Some(database_type);
                let id = &mut options.table_meta.columns.as_mut().unwrap()[0];
                id.data_type =
                    if default.to_ascii_lowercase().contains("numeric") { "largeint" } else { "varchar(36)" }.into();
                id.column_default = Some(default.into());
                options.new_rows = vec![vec![Value::Null, json!("new")], vec![Value::Null, Value::Null]];
                let function =
                    if default.to_ascii_lowercase().contains("numeric") { "uuid_numeric()" } else { "uuid()" };
                let result = prepare_data_grid_save_for_driver_profile(options, profile);
                assert_eq!(result.validation_error, None);
                assert_eq!(result.statements.len(), 2);
                assert!(result.rollback_statements.is_empty());
                assert!(
                    result.statements[0].contains(&format!("(`id`, `status`) VALUES ({function}, 'new')")),
                    "{:?}",
                    result.statements
                );
                assert!(
                    result.statements[1].contains(&format!("(`id`, `status`) VALUES ({function}, NULL)")),
                    "{:?}",
                    result.statements
                );
            }
        }
    }

    #[test]
    fn starrocks_insert_uuid_key_with_timestamp_default() {
        let mut options = mysql_people_save_options(0);
        options.database_type = Some(DatabaseType::StarRocks);
        options.table_meta.schema = None;
        options.table_meta.table_name = "test2".into();
        options.columns = vec!["id".into(), "name".into(), "create_at".into()];
        let mut id = column("id", "varchar(255)", false, None);
        id.column_default = Some("(uuid())".into());
        let mut timestamp = column("create_at", "datetime", true, None);
        timestamp.column_default = Some("CURRENT_TIMESTAMP".into());
        options.table_meta.columns = Some(vec![id, column("name", "varchar(255)", true, None), timestamp]);
        options.new_rows = vec![vec![Value::Null, json!("11"), Value::Null]];
        let result = prepare_data_grid_save(options.clone());
        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec!["INSERT INTO `test2` (`id`, `name`, `create_at`) VALUES (uuid(), '11', DEFAULT);"]
        );
        assert!(result.rollback_statements.is_empty());

        for default in ["'uuid()'", "'fixed-id'", "uuid(); SELECT 1"] {
            options.table_meta.columns.as_mut().unwrap()[0].column_default = Some(default.into());
            let result = prepare_data_grid_save(options.clone());
            assert_eq!(result.validation_error, None);
            assert!(result.statements[0].contains("VALUES (DEFAULT, '11', DEFAULT)"));
        }
    }

    #[test]
    fn starrocks_insert_preserves_explicit_uuid_values_and_mysql_omission() {
        let mut options = mysql_people_save_options(0);
        options.database_type = Some(DatabaseType::StarRocks);
        options.table_meta.columns.as_mut().unwrap()[0].column_default = Some("uuid()".into());
        options.new_rows = vec![vec![json!("manual-id"), json!("new")]];
        let result = prepare_data_grid_save(options.clone());
        assert_eq!(result.validation_error, None);
        assert!(result.statements[0].contains("VALUES ('manual-id', 'new')"));
        options.database_type = Some(DatabaseType::Mysql);
        options.new_rows = vec![vec![Value::Null, json!("new")]];
        let result = prepare_data_grid_save(options);
        assert!(result.statements[0].contains("(`status`) VALUES ('new')"));
    }

    /// A MySQL grid tab keeps its namespace in `table_meta.database` (not
    /// `schema`), so the save statements are the one generated-SQL surface that
    /// never picked up `生成 SQL 时包含数据库名`. It must match the data-table
    /// SELECT label and the copy-as-INSERT statements.
    #[test]
    fn grid_sql_spells_the_sub_byte_per_dialect() {
        let value = json!("before\u{1a}after");

        // MySQL-family scripts need \Z: a raw 0x1A makes the `mysql` client treat
        // the rest of a batch script as end-of-file.
        assert_eq!(format_grid_sql_literal(&value, Some(DatabaseType::Mysql), None), "'before\\Zafter'");
        assert_eq!(format_grid_sql_literal(&value, Some(DatabaseType::StarRocks), None), "'before\\Zafter'");

        // ClickHouse and Snowflake have no \Z in their escape table, so the byte is
        // spelled as a hexadecimal escape instead.
        assert_eq!(format_grid_sql_literal(&value, Some(DatabaseType::ClickHouse), None), "'before\\x1aafter'");
        assert_eq!(format_grid_sql_literal(&value, Some(DatabaseType::Snowflake), None), "'before\\x1aafter'");

        // PostgreSQL keeps the escape-string form the SQL export uses.
        assert_eq!(format_grid_sql_literal(&value, Some(DatabaseType::Postgres), None), "E'before\\x1Aafter'");

        // Engines with no escape for the byte keep it raw instead of inventing one.
        assert_eq!(format_grid_sql_literal(&value, Some(DatabaseType::Oracle), None), "'before\u{1a}after'");
        assert_eq!(format_grid_sql_literal(&value, Some(DatabaseType::Sqlite), None), "'before\u{1a}after'");
    }

    #[test]
    fn grid_sql_escapes_the_nul_byte_for_escaping_dialects() {
        let value = json!("before\u{0}after");

        // The mysql client truncates quoted strings containing a raw NUL.
        assert_eq!(format_grid_sql_literal(&value, Some(DatabaseType::Mysql), None), "'before\\0after'");
        assert_eq!(format_grid_sql_literal(&value, Some(DatabaseType::ClickHouse), None), "'before\\0after'");
    }

    #[test]
    fn grid_sql_keeps_backslashes_literal_for_pg_family_and_oracle_like_targets() {
        for database_type in [
            DatabaseType::Gaussdb,
            DatabaseType::OpenGauss,
            DatabaseType::Kingbase,
            DatabaseType::Highgo,
            DatabaseType::Uxdb,
            DatabaseType::Vastbase,
            DatabaseType::Kwdb,
            DatabaseType::Yashandb,
            DatabaseType::Oscar,
            DatabaseType::Xugu,
        ] {
            assert_eq!(
                format_grid_sql_literal(&json!(r"C:\tmp"), Some(database_type), None),
                r"'C:\tmp'",
                "{database_type:?}"
            );
        }

        // Dialects whose escape table has a backslash escape still double it.
        assert_eq!(format_grid_sql_literal(&json!(r"C:\tmp"), Some(DatabaseType::Mysql), None), r"'C:\\tmp'");
    }

    #[test]
    fn mysql_data_grid_save_honors_include_database_name() {
        let mut options = mysql_people_save_options(1);
        options.table_meta.database = Some("appdb".to_string());
        options.table_meta.schema = None;
        options.dirty_rows = vec![(0, vec![(1, json!("blocked"))])];

        options.include_database_name = true;
        let qualified = prepare_data_grid_save(options.clone());
        assert_eq!(qualified.validation_error, None);
        assert_eq!(qualified.statements, vec!["UPDATE `appdb`.`people` SET `status` = 'blocked' WHERE `id` = 1;"]);
        assert_eq!(
            qualified.rollback_statements,
            vec!["UPDATE `appdb`.`people` SET `status` = 'active' WHERE `id` = 1 AND BINARY `status` = 'blocked';"]
        );

        options.include_database_name = false;
        let bare = prepare_data_grid_save(options);
        assert_eq!(bare.statements, vec!["UPDATE `people` SET `status` = 'blocked' WHERE `id` = 1;"]);
        assert_eq!(
            bare.rollback_statements,
            vec!["UPDATE `people` SET `status` = 'active' WHERE `id` = 1 AND BINARY `status` = 'blocked';"]
        );
    }

    #[test]
    fn mysql_data_grid_save_qualifies_insert_delete_and_keyless_guard() {
        let mut options = mysql_people_save_options(0);
        options.table_meta.database = Some("appdb".to_string());
        options.table_meta.schema = None;
        options.include_database_name = true;
        options.new_rows = vec![vec![json!(7), json!("created")]];
        options.deleted_rows = vec![];

        let inserted = prepare_data_grid_save(options.clone());
        assert_eq!(inserted.statements, vec!["INSERT INTO `appdb`.`people` (`id`, `status`) VALUES (7, 'created');"]);

        let mut keyless = options;
        keyless.new_rows = vec![];
        keyless.table_meta.primary_keys = vec![];
        keyless.deleted_rows = vec![0];
        let deleted = prepare_data_grid_save(keyless);
        assert!(
            deleted.statements.iter().all(|statement| statement.contains("`appdb`.`people`")),
            "every statement must carry the database qualifier: {:?}",
            deleted.statements
        );
        assert!(
            deleted.keyless_guards.iter().all(|guard| guard.sql.contains("`appdb`.`people`")),
            "the keyless guard counts rows in the same table reference: {:?}",
            deleted.keyless_guards
        );
    }

    /// Engines that already address tables through `schema.table` keep their
    /// shape — the flag only adds the namespace those dialects cannot express.
    #[test]
    fn data_grid_save_leaves_schema_qualified_engines_unchanged() {
        let mut options = mysql_people_save_options(1);
        options.database_type = Some(DatabaseType::Postgres);
        options.identifier_quote = None;
        options.dirty_rows = vec![(0, vec![(1, json!("blocked"))])];
        options.include_database_name = true;

        let result = prepare_data_grid_save(options);
        assert_eq!(result.statements, vec!["UPDATE \"app\".\"people\" SET \"status\" = 'blocked' WHERE \"id\" = 1;"]);
    }

    /// A cross-database editable result (`SELECT * FROM db_9.users`) keeps its own
    /// namespace in `schema` while `database` still holds the connection's default
    /// database — the qualifier must follow the table, not the connection.
    #[test]
    fn mysql_data_grid_save_prefers_cross_database_schema() {
        let mut options = mysql_people_save_options(1);
        options.table_meta.schema = Some("db_9".to_string());
        options.table_meta.database = Some("appdb".to_string());
        options.dirty_rows = vec![(0, vec![(1, json!("blocked"))])];
        options.include_database_name = true;

        let result = prepare_data_grid_save(options);
        assert_eq!(result.statements, vec!["UPDATE `db_9`.`people` SET `status` = 'blocked' WHERE `id` = 1;"]);
        assert_eq!(result.execution_schema.as_deref(), Some("db_9"));
    }

    /// issue #9262: SQL Server tables are addressable as
    /// `database.schema.table`, so the setting must reach the three-part form on
    /// the save / rollback / keyless-guard statements too.
    #[test]
    fn sqlserver_data_grid_save_honors_include_database_name() {
        let mut options = mysql_people_save_options(1);
        options.database_type = Some(DatabaseType::SqlServer);
        options.table_meta.schema = Some("dbo".to_string());
        options.table_meta.database = Some("dbx".to_string());
        options.dirty_rows = vec![(0, vec![(1, json!("blocked"))])];

        options.include_database_name = true;
        let qualified = prepare_data_grid_save(options.clone());
        assert_eq!(qualified.validation_error, None);
        assert_eq!(qualified.statements, vec!["UPDATE [dbx].[dbo].[people] SET [status] = N'blocked' WHERE [id] = 1;"]);
        assert_eq!(
            qualified.rollback_statements,
            vec!["UPDATE [dbx].[dbo].[people] SET [status] = N'active' WHERE [id] = 1 AND [status] = N'blocked';"]
        );

        options.include_database_name = false;
        let bare = prepare_data_grid_save(options);
        assert_eq!(bare.statements, vec!["UPDATE [dbo].[people] SET [status] = N'blocked' WHERE [id] = 1;"]);
    }

    #[test]
    fn sqlserver_data_grid_save_qualifies_insert_and_keyless_guard() {
        let mut options = mysql_people_save_options(0);
        options.database_type = Some(DatabaseType::SqlServer);
        options.table_meta.schema = Some("dbo".to_string());
        options.table_meta.database = Some("dbx".to_string());
        options.include_database_name = true;
        options.new_rows = vec![vec![json!(7), json!("created")]];
        let inserted = prepare_data_grid_save(options.clone());
        assert_eq!(
            inserted.statements,
            vec!["INSERT INTO [dbx].[dbo].[people] ([id], [status]) VALUES (7, N'created');"]
        );

        let mut keyless = options;
        keyless.new_rows = vec![];
        keyless.table_meta.primary_keys = vec![];
        keyless.deleted_rows = vec![0];
        let deleted = prepare_data_grid_save(keyless);
        assert!(
            deleted.statements.iter().all(|statement| statement.contains("[dbx].[dbo].[people]")),
            "every statement must carry the three-part name: {:?}",
            deleted.statements
        );
        assert!(
            deleted.keyless_guards.iter().all(|guard| guard.sql.contains("[dbx].[dbo].[people]")),
            "the keyless guard counts rows in the same table reference: {:?}",
            deleted.keyless_guards
        );
    }

    #[test]
    fn iris_data_grid_count_queries_the_table_without_wrapping_top_sql() {
        // Caché 2016 runs with delimited identifiers disabled, where a quoted
        // ordinary name is not a table reference — the count must use the same
        // unquoted spelling as the grid SELECT (#8929). Delimited names keep
        // their quotes.
        assert_eq!(
            build_data_grid_count_sql(DataGridCountSqlOptions {
                database_type: Some(DatabaseType::Iris),
                identifier_quote: Some("\"".to_string()),
                catalog: None,
                database: None,
                schema: Some("SS".to_string()),
                table_name: "SS_User".to_string(),
                where_input: Some("SSUSR_IsActive = 'Y'".to_string()),
                count_hint: None,
            }),
            "SELECT COUNT(*) AS cnt FROM SS.SS_User WHERE (SSUSR_IsActive = 'Y')"
        );
        assert_eq!(
            build_data_grid_count_sql(DataGridCountSqlOptions {
                database_type: Some(DatabaseType::Iris),
                identifier_quote: None,
                catalog: None,
                database: None,
                schema: Some("App Schema".to_string()),
                table_name: "Patient Record".to_string(),
                where_input: None,
                count_hint: None,
            }),
            "SELECT COUNT(*) AS cnt FROM \"App Schema\".\"Patient Record\""
        );
    }

    #[test]
    fn mysql_conditional_update_uses_typed_value_and_requires_a_writable_column_and_where() {
        let options = DataGridConditionalUpdateSqlOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("app".to_string()),
                table_name: "people".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![column("id", "bigint", false, None), column("status", "varchar(32)", true, None)]),
            },
            column_name: "status".to_string(),
            value: json!("O'Reilly"),
            where_input: "WHERE tenant_id = 7;".to_string(),
        };

        assert_eq!(
            build_data_grid_conditional_update_sql(options.clone()),
            Some("UPDATE `app`.`people` SET `status` = 'O''Reilly' WHERE (tenant_id = 7);".to_string())
        );

        let mut condition_only = options.clone();
        condition_only.where_input = "tenant_id = 7".to_string();
        assert_eq!(
            build_data_grid_conditional_update_sql(condition_only),
            Some("UPDATE `app`.`people` SET `status` = 'O''Reilly' WHERE (tenant_id = 7);".to_string())
        );

        let mut empty_where = options.clone();
        empty_where.where_input = " ".to_string();
        assert_eq!(build_data_grid_conditional_update_sql(empty_where), None);

        let mut primary_key = options;
        primary_key.column_name = "id".to_string();
        assert_eq!(build_data_grid_conditional_update_sql(primary_key), None);
    }

    #[test]
    fn neo4j_data_grid_save_addresses_nodes_with_the_identity_function_of_the_server() {
        let options = |server_version: Option<&str>, rows: Vec<Vec<Value>>| DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Neo4j),
            identifier_quote: None,
            server_version: server_version.map(str::to_string),
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "Employee".to_string(),
                primary_keys: vec![],
                columns: Some(vec![
                    column(DBX_NEO4J_ELEMENT_ID_COLUMN, "string", false, None),
                    column("name", "string", true, None),
                ]),
            },
            columns: vec![DBX_NEO4J_ELEMENT_ID_COLUMN.to_string(), "name".to_string()],
            source_columns: None,
            rows,
            dirty_rows: vec![(0, vec![(1, json!("after"))])],
            deleted_rows: vec![1],
            new_rows: vec![],
            include_database_name: false,
        };
        let legacy_rows = || vec![vec![json!(23), json!("before")], vec![json!(24), json!("gone")]];

        // Neo4j 4.x only knows `id()`; `elementId()` would be an unknown function there (#10503).
        // That `id()` is an Integer while grids carry text, so the predicate compares numbers —
        // with a quoted literal the edit would silently match no row.
        for rows in [legacy_rows(), vec![vec![json!("23"), json!("before")], vec![json!("24"), json!("gone")]]] {
            let on_neo4j_4 = prepare_data_grid_save(options(Some("Neo4j/4.4.44"), rows));
            assert_eq!(
                on_neo4j_4.statements,
                vec![
                    "MATCH (n:`Employee`) WHERE id(n) = 23 SET n.`name` = 'after';".to_string(),
                    "MATCH (n:`Employee`) WHERE id(n) = 24 DETACH DELETE n;".to_string(),
                ]
            );
        }

        // Neo4j 5.0+ renamed the identity function, and `elementId()` returns a string.
        let on_neo4j_5 = prepare_data_grid_save(options(
            Some("Neo4j/5.26.0"),
            vec![vec![json!("4:0db5d0e9:1:6"), json!("before")], vec![json!("4:0db5d0e9:1:7"), json!("gone")]],
        ));
        assert_eq!(
            on_neo4j_5.statements,
            vec![
                "MATCH (n:`Employee`) WHERE elementId(n) = '4:0db5d0e9:1:6' SET n.`name` = 'after';".to_string(),
                "MATCH (n:`Employee`) WHERE elementId(n) = '4:0db5d0e9:1:7' DETACH DELETE n;".to_string(),
            ]
        );

        // A non-numeric identity never becomes a bare Cypher number, even on the legacy function.
        let legacy_text_id = prepare_data_grid_save(options(
            Some("Neo4j/4.4.44"),
            vec![vec![json!("4:0db5d0e9:1:6"), json!("before")], vec![json!("4:0db5d0e9:1:7"), json!("gone")]],
        ));
        assert_eq!(
            legacy_text_id.statements,
            vec![
                "MATCH (n:`Employee`) WHERE id(n) = '4:0db5d0e9:1:6' SET n.`name` = 'after';".to_string(),
                "MATCH (n:`Employee`) WHERE id(n) = '4:0db5d0e9:1:7' DETACH DELETE n;".to_string(),
            ]
        );

        // Callers without connection metadata keep the historical `elementId()` spelling.
        let without_version = prepare_data_grid_save(options(
            None,
            vec![vec![json!("4:0db5d0e9:1:6"), json!("before")], vec![json!("4:0db5d0e9:1:7"), json!("gone")]],
        ));
        assert_eq!(without_version.statements, on_neo4j_5.statements);
    }

    #[test]
    fn neo4j_property_writes_preserve_types_and_exact_integers() {
        for (kind, value, expected) in [
            ("Long", json!("9223372036854775807"), "toInteger('9223372036854775807')"),
            ("Integer", json!("-9223372036854775808"), "toInteger('-9223372036854775808')"),
            ("Double", json!(2), "toFloat('2')"),
            ("Boolean", json!("false"), "false"),
            ("Date", json!("2026-09-30"), "date('2026-09-30')"),
            ("Duration", json!("P1D"), "duration('P1D')"),
            ("StringArray", json!("[\"A\",\"B\"]"), "['A', 'B']"),
            ("LongArray", json!("[9007199254740997]"), "[toInteger('9007199254740997')]"),
            ("BooleanArray", json!([true, false]), "[true, false]"),
            ("String", json!("a'b\\c"), "'a\\'b\\\\c'"),
        ] {
            let prepared = prepare_data_grid_save(neo4j_property_save_options(kind, value));
            assert_eq!(prepared.validation_error, None, "{kind}");
            assert_eq!(
                prepared.statements,
                vec![format!("MATCH (n:`Person`) WHERE elementId(n) = 'sample-id' SET n.`value` = {expected};")]
            );
            assert_eq!(prepared.rollback_statements, prepared.statements);
        }
    }

    #[test]
    fn neo4j_property_writes_reject_invalid_and_ambiguous_types_before_execution() {
        for (kind, value) in [
            ("Long", json!("9223372036854775808")),
            ("Long", json!("1.5")),
            ("Long", json!("1); MATCH (n) DELETE n")),
            ("Double", json!("NaN")),
            ("Double", json!("Infinity")),
            ("Boolean", json!("not-a-boolean")),
            ("LongArray", json!("[null]")),
            ("StringArray", json!("[1]")),
            ("LongArray", json!("[1.5]")),
            ("StringArray", json!("invalid JSON")),
            ("Long | String", json!("12")),
            ("Unknown", json!("12")),
            ("Point", json!("{x:1,y:2}")),
        ] {
            let prepared = prepare_data_grid_save(neo4j_property_save_options(kind, value));
            assert!(prepared.validation_error.is_some(), "{kind}");
            assert!(prepared.statements.is_empty(), "{kind}");
            assert!(prepared.rollback_statements.is_empty(), "{kind}");
        }
    }

    #[test]
    fn neo4j_insert_and_delete_do_not_generate_unsafe_history_reversals() {
        let mut options = neo4j_property_save_options("Long", json!("9007199254740997"));
        options.dirty_rows.clear();
        options.new_rows = vec![vec![Value::Null, json!("9007199254740997")]];
        let prepared = prepare_data_grid_save(options.clone());
        assert_eq!(prepared.validation_error, None);
        assert_eq!(prepared.statements, vec!["CREATE (n:`Person` {`value`: toInteger('9007199254740997')});"]);
        assert!(prepared.rollback_statements.is_empty());
        options.new_rows.clear();
        options.deleted_rows = vec![0];
        let prepared = prepare_data_grid_save(options);
        assert_eq!(prepared.statements, vec!["MATCH (n:`Person`) WHERE elementId(n) = 'sample-id' DETACH DELETE n;"]);
        assert!(prepared.rollback_statements.is_empty());
    }

    fn neo4j_property_save_options(kind: &str, value: Value) -> DataGridSaveStatementOptions {
        DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Neo4j),
            identifier_quote: None,
            server_version: Some("Neo4j/5.26.0".to_string()),
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "Person".to_string(),
                primary_keys: vec![DBX_NEO4J_ELEMENT_ID_COLUMN.to_string()],
                columns: Some(vec![column("value", kind, true, None)]),
            },
            columns: vec![DBX_NEO4J_ELEMENT_ID_COLUMN.to_string(), "value".to_string()],
            source_columns: None,
            rows: vec![vec![json!("sample-id"), value.clone()]],
            dirty_rows: vec![(0, vec![(1, value)])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        }
    }

    #[test]
    fn neo4j_counts_use_cypher_and_keep_the_grid_predicate() {
        for (where_input, where_clause) in [(None, ""), (Some("WHERE n.`age` > 10"), " WHERE (n.`age` > 10)")] {
            let options = DataGridCountSqlOptions {
                database_type: Some(DatabaseType::Neo4j),
                identifier_quote: None,
                catalog: None,
                database: Some("neo4j".to_string()),
                schema: Some("neo4j".to_string()),
                table_name: "P`erson".to_string(),
                where_input: where_input.map(str::to_string),
                count_hint: None,
            };
            assert_eq!(
                build_data_grid_count_sql(options),
                format!("MATCH (n:`P``erson`){where_clause} RETURN count(n) AS cnt")
            );
        }
    }

    #[test]
    fn iris_cache_data_grid_save_uses_unquoted_ordinary_identifiers() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Iris),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("SQLUser".to_string()),
                table_name: "PA_PatMas".to_string(),
                primary_keys: vec!["PAPMI_RowId".to_string()],
                columns: Some(vec![
                    column("PAPMI_RowId", "integer", false, None),
                    column("PAPMI_ID", "varchar(32)", true, None),
                ]),
            },
            columns: vec!["PAPMI_RowId".to_string(), "PAPMI_ID".to_string()],
            source_columns: None,
            rows: vec![vec![json!(51), json!("before")]],
            dirty_rows: vec![(0, vec![(1, json!("210101202201016555"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(
            result.statements,
            vec!["UPDATE SQLUser.PA_PatMas SET PAPMI_ID = '210101202201016555' WHERE PAPMI_RowId = 51;"]
        );
        assert_eq!(
            result.rollback_statements,
            vec![
                "UPDATE SQLUser.PA_PatMas SET PAPMI_ID = 'before' WHERE PAPMI_RowId = 51 AND PAPMI_ID = '210101202201016555';"
            ]
        );
    }

    #[test]
    fn iris_keyless_save_uses_full_row_predicate() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Iris),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("SQLUser".to_string()),
                table_name: "Unkeyed".to_string(),
                primary_keys: vec![],
                columns: Some(vec![
                    column("DocId", "integer", false, None),
                    column("Title", "varchar(64)", true, None),
                ]),
            },
            columns: vec!["DocId".to_string(), "Title".to_string()],
            source_columns: None,
            rows: vec![vec![json!(7), json!("draft")]],
            dirty_rows: vec![(0, vec![(1, json!("final"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec!["UPDATE SQLUser.Unkeyed SET Title = 'final' WHERE DocId = 7 AND Title = 'draft';"]
        );
    }

    #[test]
    fn iris_keyless_save_rejects_unidentifiable_rows() {
        // Mirrors the computed-column case: when no result column maps to a
        // table column the row predicate is empty and the save must stay
        // disabled instead of emitting a dangling WHERE.
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Iris),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("SQLUser".to_string()),
                table_name: "Unkeyed".to_string(),
                primary_keys: vec![],
                columns: Some(vec![column("DocId", "integer", true, None), column("Title", "varchar(64)", true, None)]),
            },
            columns: vec!["expr".to_string(), "display".to_string()],
            source_columns: Some(vec![None, None]),
            rows: vec![vec![json!("x"), json!("draft")]],
            dirty_rows: vec![(0, vec![(1, json!("final"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert!(result.validation_error.is_some(), "{:?}", result.validation_error);
        assert!(result.statements.is_empty());
    }

    #[test]
    fn iris_identity_columns_are_omitted_from_new_row_inserts() {
        let row_id = column("PAPMI_RowId", "integer", false, Some("identity"));
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Iris),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("SQLUser".to_string()),
                table_name: "PA_PatMas".to_string(),
                primary_keys: vec![],
                columns: Some(vec![row_id, column("PAPMI_ID", "varchar(32)", false, None)]),
            },
            columns: vec!["PAPMI_RowId".to_string(), "PAPMI_ID".to_string()],
            source_columns: None,
            rows: vec![],
            dirty_rows: vec![],
            deleted_rows: vec![],
            new_rows: vec![vec![Value::Null, json!("new-id")]],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements, vec!["INSERT INTO SQLUser.PA_PatMas (PAPMI_ID) VALUES ('new-id');"]);
    }

    #[test]
    fn iris_data_grid_save_quotes_only_delimited_identifiers_across_mutations() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Iris),
            identifier_quote: Some("\"".to_string()),
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("App Schema".to_string()),
                table_name: "Patient Record".to_string(),
                primary_keys: vec!["Row ID".to_string()],
                columns: Some(vec![
                    column("Row ID", "integer", false, None),
                    column("PAPMI_ID", "varchar(32)", true, None),
                ]),
            },
            columns: vec!["Row ID".to_string(), "PAPMI_ID".to_string()],
            source_columns: None,
            rows: vec![vec![json!(51), json!("deleted")]],
            dirty_rows: vec![],
            deleted_rows: vec![0],
            new_rows: vec![vec![json!(52), json!("inserted")]],
            include_database_name: false,
        });

        assert_eq!(
            result.statements,
            vec![
                "DELETE FROM \"App Schema\".\"Patient Record\" WHERE \"Row ID\" = 51;",
                "INSERT INTO \"App Schema\".\"Patient Record\" (\"Row ID\", PAPMI_ID) VALUES (52, 'inserted');",
            ]
        );
        assert_eq!(
            result.rollback_statements,
            vec![
                "DELETE FROM \"App Schema\".\"Patient Record\" WHERE \"Row ID\" = 52 AND PAPMI_ID = 'inserted';",
                "INSERT INTO \"App Schema\".\"Patient Record\" (\"Row ID\", PAPMI_ID) VALUES (51, 'deleted');",
            ]
        );
    }

    #[test]
    fn postgres_keyless_update_preserves_jsonb_array_elements() {
        let endpoints =
            json!([r#"{"port":10031,"type":"admin_web"}"#, r#""quoted""#, r#"[1,true,{"nested":null}]"#, null]);
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Postgres),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("public".to_string()),
                table_name: "services".to_string(),
                primary_keys: vec![],
                columns: Some(vec![
                    column("id", "integer", false, None),
                    column("name", "text", false, None),
                    column("endpoints", "jsonb[]", true, None),
                ]),
            },
            columns: vec!["id".to_string(), "name".to_string(), "endpoints".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("before"), endpoints]],
            dirty_rows: vec![(0, vec![(1, json!("after"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements.len(), 1);
        let statement = &result.statements[0];
        assert!(statement.starts_with("UPDATE \"public\".\"services\" SET \"name\" = 'after' WHERE "));
        assert!(statement.contains("\"endpoints\" = ARRAY["));
        assert!(statement.contains("admin_web"));
        assert!(statement.contains(r#"E'"quoted"'::jsonb"#), "{statement}");
        assert!(statement.contains("NULL]"));
        assert!(!statement.contains('\u{1}'));
    }

    #[test]
    fn postgres_keyless_update_casts_json_predicates() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Postgres),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("public".to_string()),
                table_name: "profiles".to_string(),
                primary_keys: vec![],
                columns: Some(vec![
                    column("user_id", "uuid", false, None),
                    column("status", "text", false, None),
                    column("photo", "json", true, None),
                    column("extend_list", "jsonb", true, None),
                ]),
            },
            columns: vec!["user_id".to_string(), "status".to_string(), "photo".to_string(), "extend_list".to_string()],
            source_columns: None,
            rows: vec![vec![
                json!("d7910cef-4188-4309-99ce-8e7b64d64869"),
                json!("0"),
                json!("[]"),
                json!(r#"[{"type":"combobox","key":"gender","value":"C017MALE"}]"#),
            ]],
            dirty_rows: vec![(0, vec![(1, json!("1"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements.len(), 1);
        let statement = &result.statements[0];
        assert!(statement.contains(r#""photo"::text = '[]'::text"#), "{statement}");
        assert!(
            statement.contains(r#""extend_list" = '[{"type":"combobox","key":"gender","value":"C017MALE"}]'::jsonb"#),
            "{statement}"
        );
    }

    #[test]
    fn postgres_json_array_literals_preserve_json_documents_and_nulls() {
        let value = json!([r#"{"object":true}"#, r#""text""#, "[1,2]", "plain text", "null", null]);
        let json_column = column("payload", "json[]", true, None);
        let jsonb_column = column("payload", "jsonb[]", true, None);
        let text_column = column("payload", "text[]", true, None);
        let integer_column = column("payload", "integer[]", true, None);

        assert_eq!(
            format_grid_sql_literal(&value, Some(DatabaseType::Postgres), Some(&json_column)),
            r#"ARRAY[E'{"object":true}'::json, E'"text"'::json, E'[1,2]'::json, E'"plain text"'::json, E'null'::json, NULL]"#
        );
        assert_eq!(
            format_grid_sql_literal(&value, Some(DatabaseType::Postgres), Some(&jsonb_column)),
            r#"ARRAY[E'{"object":true}'::jsonb, E'"text"'::jsonb, E'[1,2]'::jsonb, E'"plain text"'::jsonb, E'null'::jsonb, NULL]"#
        );
        assert_eq!(
            format_grid_sql_literal(&json!([]), Some(DatabaseType::Postgres), Some(&jsonb_column)),
            "ARRAY[]::jsonb[]"
        );
        assert_eq!(
            format_grid_sql_literal(
                &json!(["first", null, "second"]),
                Some(DatabaseType::Postgres),
                Some(&text_column)
            ),
            r#"'{"first",NULL,"second"}'"#
        );
        assert_eq!(
            format_grid_sql_literal(&json!([1, null, 2]), Some(DatabaseType::Postgres), Some(&integer_column)),
            "'{1,NULL,2}'"
        );
    }

    #[test]
    fn builds_copy_update_statements() {
        let statements = build_data_grid_copy_update_statements(DataGridCopyUpdateStatementOptions {
            database_type: Some(DatabaseType::Postgres),
            identifier_quote: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("public".to_string()),
                table_name: "users".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: None,
            },
            columns: vec!["id".to_string(), "name".to_string(), "status".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("Ada"), json!("active")]],
            include_database_name: false,
        });
        assert_eq!(
            statements,
            vec!["UPDATE \"public\".\"users\" SET \"name\" = 'Ada', \"status\" = 'active' WHERE \"id\" = 1;"]
        );
    }

    /// The right-click `复制 → SQL UPDATE 语句` path must honor
    /// `生成 SQL 时包含数据库名` exactly like the copy-as-INSERT statements do
    /// (issue #9262).
    #[test]
    fn copy_update_honors_include_database_name() {
        let options = DataGridCopyUpdateStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: Some("appdb".to_string()),
                schema: None,
                table_name: "people".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: None,
            },
            columns: vec!["id".to_string(), "status".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("blocked")]],
            include_database_name: true,
        };
        assert_eq!(
            build_data_grid_copy_update_statements(options.clone()),
            vec!["UPDATE `appdb`.`people` SET `status` = 'blocked' WHERE `id` = 1;"]
        );
        // The frontend strips the metadata when the setting is off, but the
        // option itself must also fall back to the historical bare shape.
        assert_eq!(
            build_data_grid_copy_update_statements(DataGridCopyUpdateStatementOptions {
                include_database_name: false,
                ..options
            }),
            vec!["UPDATE `people` SET `status` = 'blocked' WHERE `id` = 1;"]
        );
    }

    #[test]
    fn sqlserver_copy_update_uses_three_part_name() {
        let options = DataGridCopyUpdateStatementOptions {
            database_type: Some(DatabaseType::SqlServer),
            identifier_quote: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: Some("dbx".to_string()),
                schema: Some("dbo".to_string()),
                table_name: "player states".to_string(),
                primary_keys: vec!["role id".to_string()],
                columns: None,
            },
            columns: vec!["role id".to_string(), "state".to_string()],
            source_columns: None,
            rows: vec![vec![json!(42), json!("ready")]],
            include_database_name: true,
        };
        assert_eq!(
            build_data_grid_copy_update_statements(options),
            vec!["UPDATE [dbx].[dbo].[player states] SET [state] = N'ready' WHERE [role id] = 42;"]
        );
    }

    #[test]
    fn builds_copy_insert_statement_without_primary_keys() {
        let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            table_meta: Some(DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "users".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![
                    DataGridColumnInfo {
                        name: "id".to_string(),
                        data_type: "bigint".to_string(),
                        is_nullable: false,
                        is_primary_key: true,
                        column_default: None,
                        extra: Some("auto_increment".to_string()),
                    },
                    DataGridColumnInfo {
                        name: "login_name".to_string(),
                        data_type: "varchar(64)".to_string(),
                        is_nullable: false,
                        is_primary_key: false,
                        column_default: None,
                        extra: None,
                    },
                    DataGridColumnInfo {
                        name: "display_name".to_string(),
                        data_type: "varchar(64)".to_string(),
                        is_nullable: true,
                        is_primary_key: false,
                        column_default: None,
                        extra: None,
                    },
                ]),
            }),
            columns: vec!["id".to_string(), "login_name".to_string(), "display_name".to_string()],
            column_types: None,
            source_columns: None,
            rows: vec![vec![json!(1), json!("ada"), json!("Ada")], vec![json!(2), json!("linus"), json!("Linus")]],
            primary_key_mode: DataGridPrimaryKeyMode::ExcludeGenerated,
            include_computed_columns: false,
            include_database_name: true,
            insert_mode: DataGridCopyInsertMode::Merged,
        });
        assert_eq!(
            statement.as_deref(),
            Some("INSERT INTO `users` (`login_name`, `display_name`) VALUES\n('ada', 'Ada'),\n('linus', 'Linus');")
        );
    }

    #[test]
    fn recognizes_postgres_serial_extras_as_auto_generated() {
        for extra in ["serial", "smallserial", "bigserial"] {
            assert!(extra_is_auto_generated(extra), "expected {extra} to be auto-generated");
        }
    }

    #[test]
    fn builds_postgres_copy_insert_without_serial_primary_key() {
        let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
            database_type: Some(DatabaseType::Postgres),
            identifier_quote: None,
            table_meta: Some(DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("public".to_string()),
                table_name: "users".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![
                    DataGridColumnInfo {
                        name: "id".to_string(),
                        data_type: "integer".to_string(),
                        is_nullable: false,
                        is_primary_key: true,
                        column_default: Some("nextval('public.users_id_seq'::regclass)".to_string()),
                        extra: None,
                    },
                    DataGridColumnInfo {
                        name: "name".to_string(),
                        data_type: "text".to_string(),
                        is_nullable: false,
                        is_primary_key: false,
                        column_default: None,
                        extra: None,
                    },
                ]),
            }),
            columns: vec!["id".to_string(), "name".to_string()],
            column_types: None,
            source_columns: None,
            rows: vec![vec![json!(1), json!("Ada")]],
            primary_key_mode: DataGridPrimaryKeyMode::ExcludeGenerated,
            include_computed_columns: false,
            include_database_name: true,
            insert_mode: DataGridCopyInsertMode::Merged,
        });

        assert_eq!(statement.as_deref(), Some("INSERT INTO \"public\".\"users\" (\"name\") VALUES ('Ada');"));
    }

    #[test]
    fn copy_insert_uses_display_columns_but_source_columns_for_metadata() {
        let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            table_meta: Some(DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "psn_basic_info".to_string(),
                primary_keys: vec![],
                columns: Some(vec![
                    DataGridColumnInfo {
                        name: "PSN_NO".to_string(),
                        data_type: "varchar(32)".to_string(),
                        is_nullable: false,
                        is_primary_key: false,
                        column_default: None,
                        extra: None,
                    },
                    DataGridColumnInfo {
                        name: "NAME".to_string(),
                        data_type: "varchar(32)".to_string(),
                        is_nullable: true,
                        is_primary_key: false,
                        column_default: None,
                        extra: None,
                    },
                ]),
            }),
            columns: vec!["psn".to_string(), "psn_no".to_string(), "name".to_string()],
            column_types: None,
            source_columns: Some(vec![
                Some("PSN_NO".to_string()),
                Some("PSN_NO".to_string()),
                Some("NAME".to_string()),
            ]),
            rows: vec![vec![json!("A-1"), json!("A-1"), json!("Ada")]],
            primary_key_mode: DataGridPrimaryKeyMode::Include,
            include_computed_columns: false,
            include_database_name: true,
            insert_mode: DataGridCopyInsertMode::Merged,
        });
        assert_eq!(
            statement.as_deref(),
            Some("INSERT INTO `psn_basic_info` (`psn`, `psn_no`, `name`) VALUES ('A-1', 'A-1', 'Ada');")
        );
    }

    #[test]
    fn copy_insert_uses_source_columns_for_generated_column_exclusions() {
        let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            table_meta: Some(DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "users".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![
                    DataGridColumnInfo {
                        name: "id".to_string(),
                        data_type: "bigint".to_string(),
                        is_nullable: false,
                        is_primary_key: true,
                        column_default: None,
                        extra: Some("auto_increment".to_string()),
                    },
                    DataGridColumnInfo {
                        name: "display_name".to_string(),
                        data_type: "varchar(64)".to_string(),
                        is_nullable: true,
                        is_primary_key: false,
                        column_default: None,
                        extra: None,
                    },
                    DataGridColumnInfo {
                        name: "display_name_upper".to_string(),
                        data_type: "varchar(64)".to_string(),
                        is_nullable: true,
                        is_primary_key: false,
                        column_default: None,
                        extra: Some("virtual generated".to_string()),
                    },
                ]),
            }),
            columns: vec!["identifier".to_string(), "label".to_string(), "label_upper".to_string()],
            column_types: None,
            source_columns: Some(vec![
                Some("id".to_string()),
                Some("display_name".to_string()),
                Some("display_name_upper".to_string()),
            ]),
            rows: vec![vec![json!(1), json!("Ada"), json!("ADA")]],
            primary_key_mode: DataGridPrimaryKeyMode::ExcludeGenerated,
            include_computed_columns: false,
            include_database_name: true,
            insert_mode: DataGridCopyInsertMode::Merged,
        });
        assert_eq!(statement.as_deref(), Some("INSERT INTO `users` (`label`) VALUES ('Ada');"));
    }

    #[test]
    fn copy_update_keeps_source_columns_for_writeback() {
        let statements = build_data_grid_copy_update_statements(DataGridCopyUpdateStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "users".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: None,
            },
            columns: vec!["identifier".to_string(), "label".to_string()],
            source_columns: Some(vec![Some("id".to_string()), Some("display_name".to_string())]),
            rows: vec![vec![json!(1), json!("Ada")]],
            include_database_name: false,
        });
        assert_eq!(statements, vec!["UPDATE `users` SET `display_name` = 'Ada' WHERE `id` = 1;"]);
    }

    #[test]
    fn copy_insert_generated_primary_key_exclusion_keeps_manual_composite_key_members() {
        let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            table_meta: Some(DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "move_statistic_product_daily".to_string(),
                primary_keys: vec!["id".to_string(), "stat_date".to_string()],
                columns: Some(vec![
                    DataGridColumnInfo {
                        name: "id".to_string(),
                        data_type: "bigint unsigned".to_string(),
                        is_nullable: false,
                        is_primary_key: true,
                        column_default: None,
                        extra: Some("auto_increment".to_string()),
                    },
                    DataGridColumnInfo {
                        name: "stat_date".to_string(),
                        data_type: "date".to_string(),
                        is_nullable: false,
                        is_primary_key: true,
                        column_default: None,
                        extra: None,
                    },
                    DataGridColumnInfo {
                        name: "product_name".to_string(),
                        data_type: "varchar(255)".to_string(),
                        is_nullable: false,
                        is_primary_key: false,
                        column_default: Some("".to_string()),
                        extra: None,
                    },
                ]),
            }),
            columns: vec!["id".to_string(), "stat_date".to_string(), "product_name".to_string()],
            column_types: None,
            source_columns: None,
            rows: vec![vec![json!(1), json!("2026-08-18"), json!("sweater")]],
            primary_key_mode: DataGridPrimaryKeyMode::ExcludeGenerated,
            include_computed_columns: false,
            include_database_name: true,
            insert_mode: DataGridCopyInsertMode::Merged,
        });
        assert_eq!(
            statement.as_deref(),
            Some("INSERT INTO `move_statistic_product_daily` (`stat_date`, `product_name`) VALUES ('2026-08-18', 'sweater');")
        );
    }

    #[test]
    fn copy_insert_primary_key_exclusion_removes_manual_primary_keys() {
        let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
            database_type: Some(DatabaseType::Postgres),
            identifier_quote: None,
            table_meta: Some(DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("public".to_string()),
                table_name: "countries".to_string(),
                primary_keys: vec!["code".to_string()],
                columns: Some(vec![
                    DataGridColumnInfo {
                        name: "code".to_string(),
                        data_type: "varchar(2)".to_string(),
                        is_nullable: false,
                        is_primary_key: true,
                        column_default: None,
                        extra: None,
                    },
                    DataGridColumnInfo {
                        name: "label".to_string(),
                        data_type: "text".to_string(),
                        is_nullable: false,
                        is_primary_key: false,
                        column_default: None,
                        extra: None,
                    },
                ]),
            }),
            columns: vec!["code".to_string(), "label".to_string()],
            column_types: None,
            source_columns: None,
            rows: vec![vec![json!("AD"), json!("Andorra")]],
            primary_key_mode: DataGridPrimaryKeyMode::ExcludeAll,
            include_computed_columns: false,
            include_database_name: true,
            insert_mode: DataGridCopyInsertMode::Merged,
        });
        assert_eq!(statement.as_deref(), Some("INSERT INTO \"public\".\"countries\" (\"label\") VALUES ('Andorra');"));
    }

    #[test]
    fn copy_insert_primary_key_exclusion_matches_unique_case_insensitive_mysql_primary_key_names() {
        let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            table_meta: Some(DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "users".to_string(),
                primary_keys: vec!["ID".to_string()],
                columns: Some(vec![DataGridColumnInfo {
                    name: "id".to_string(),
                    data_type: "int".to_string(),
                    is_nullable: false,
                    is_primary_key: false,
                    column_default: None,
                    extra: None,
                }]),
            }),
            columns: vec!["id".to_string()],
            column_types: None,
            source_columns: None,
            rows: vec![vec![json!(1)]],
            primary_key_mode: DataGridPrimaryKeyMode::ExcludeAll,
            include_computed_columns: false,
            include_database_name: true,
            insert_mode: DataGridCopyInsertMode::Merged,
        });

        assert!(statement.is_none());
    }

    #[test]
    fn copy_insert_primary_key_exclusion_preserves_case_distinct_postgres_columns() {
        let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
            database_type: Some(DatabaseType::Postgres),
            identifier_quote: None,
            table_meta: Some(DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("public".to_string()),
                table_name: "quoted_ids".to_string(),
                primary_keys: vec!["ID".to_string()],
                columns: Some(vec![
                    DataGridColumnInfo {
                        name: "ID".to_string(),
                        data_type: "text".to_string(),
                        is_nullable: false,
                        is_primary_key: true,
                        column_default: None,
                        extra: None,
                    },
                    DataGridColumnInfo {
                        name: "id".to_string(),
                        data_type: "text".to_string(),
                        is_nullable: false,
                        is_primary_key: false,
                        column_default: None,
                        extra: None,
                    },
                    DataGridColumnInfo {
                        name: "name".to_string(),
                        data_type: "text".to_string(),
                        is_nullable: false,
                        is_primary_key: false,
                        column_default: None,
                        extra: None,
                    },
                ]),
            }),
            columns: vec!["ID".to_string(), "id".to_string(), "name".to_string()],
            column_types: None,
            source_columns: None,
            rows: vec![vec![json!("key"), json!("lowercase"), json!("Ada")]],
            primary_key_mode: DataGridPrimaryKeyMode::ExcludeAll,
            include_computed_columns: false,
            include_database_name: true,
            insert_mode: DataGridCopyInsertMode::Merged,
        });

        assert_eq!(
            statement.as_deref(),
            Some("INSERT INTO \"public\".\"quoted_ids\" (\"id\", \"name\") VALUES ('lowercase', 'Ada');")
        );
    }

    #[test]
    fn copy_insert_primary_key_exclusion_uses_exact_postgres_names_without_column_metadata() {
        let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
            database_type: Some(DatabaseType::Postgres),
            identifier_quote: None,
            table_meta: Some(DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "quoted_ids".to_string(),
                primary_keys: vec!["ID".to_string()],
                columns: None,
            }),
            columns: vec!["id".to_string()],
            column_types: None,
            source_columns: None,
            rows: vec![vec![json!("lowercase")]],
            primary_key_mode: DataGridPrimaryKeyMode::ExcludeAll,
            include_computed_columns: false,
            include_database_name: true,
            insert_mode: DataGridCopyInsertMode::Merged,
        });

        assert_eq!(statement.as_deref(), Some("INSERT INTO \"quoted_ids\" (\"id\") VALUES ('lowercase');"));
    }

    #[test]
    fn copy_insert_uses_exact_postgres_column_metadata_for_case_distinct_columns() {
        let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
            database_type: Some(DatabaseType::Postgres),
            identifier_quote: None,
            table_meta: Some(DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "quoted_ids".to_string(),
                primary_keys: vec!["ID".to_string()],
                columns: Some(vec![DataGridColumnInfo {
                    name: "ID".to_string(),
                    data_type: "jsonb".to_string(),
                    is_nullable: false,
                    is_primary_key: true,
                    column_default: None,
                    extra: Some("identity".to_string()),
                }]),
            }),
            columns: vec!["id".to_string()],
            column_types: Some(vec![Some("text".to_string())]),
            source_columns: None,
            rows: vec![vec![json!({"value": "lowercase"})]],
            primary_key_mode: DataGridPrimaryKeyMode::ExcludeGenerated,
            include_computed_columns: false,
            include_database_name: true,
            insert_mode: DataGridCopyInsertMode::Merged,
        });

        assert_eq!(
            statement.as_deref(),
            Some("INSERT INTO \"quoted_ids\" (\"id\") VALUES ('{\"value\":\"lowercase\"}');")
        );
    }

    #[test]
    fn copy_insert_primary_key_exclusion_removes_keys_without_column_metadata() {
        let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            table_meta: Some(DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "users".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: None,
            }),
            columns: vec!["id".to_string(), "login_name".to_string()],
            column_types: None,
            source_columns: None,
            rows: vec![vec![json!(1), json!("ada")]],
            primary_key_mode: DataGridPrimaryKeyMode::ExcludeAll,
            include_computed_columns: false,
            include_database_name: true,
            insert_mode: DataGridCopyInsertMode::Merged,
        });
        assert_eq!(statement.as_deref(), Some("INSERT INTO `users` (`login_name`) VALUES ('ada');"));
    }

    #[test]
    fn copy_insert_keeps_json_cells_as_single_json_literals() {
        let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
            database_type: Some(DatabaseType::Elasticsearch),
            identifier_quote: None,
            table_meta: None,
            columns: vec!["id".to_string(), "active".to_string(), "profile".to_string()],
            column_types: Some(vec![Some("number".to_string()), Some("boolean".to_string()), Some("json".to_string())]),
            source_columns: None,
            rows: vec![vec![json!(7), json!(true), json!(r#"{"name":"Ada","roles":["admin"]}"#)]],
            primary_key_mode: DataGridPrimaryKeyMode::Include,
            include_computed_columns: false,
            include_database_name: true,
            insert_mode: DataGridCopyInsertMode::Merged,
        });

        assert_eq!(
            statement.as_deref(),
            Some("INSERT INTO table_name (\"id\", \"active\", \"profile\") VALUES (7, TRUE, '{\"name\":\"Ada\",\"roles\":[\"admin\"]}');")
        );
    }

    #[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),
            identifier_quote: None,
            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])]],
            primary_key_mode: DataGridPrimaryKeyMode::Include,
            include_computed_columns: false,
            include_database_name: true,
            insert_mode: DataGridCopyInsertMode::Merged,
        });

        assert_eq!(statement.as_deref(), Some("INSERT INTO table_name (`data`) VALUES ('[1,2,3]');"));
    }

    #[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),
            identifier_quote: None,
            table_meta: None,
            columns: vec!["data".to_string()],
            column_types: Some(vec![Some("json".to_string())]),
            source_columns: None,
            rows: vec![vec![json!([])]],
            primary_key_mode: DataGridPrimaryKeyMode::Include,
            include_computed_columns: false,
            include_database_name: true,
            insert_mode: DataGridCopyInsertMode::Merged,
        });

        assert_eq!(statement.as_deref(), Some("INSERT INTO table_name (`data`) VALUES ('[]');"));
    }

    #[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),
            identifier_quote: None,
            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})]],
            primary_key_mode: DataGridPrimaryKeyMode::Include,
            include_computed_columns: false,
            include_database_name: true,
            insert_mode: DataGridCopyInsertMode::Merged,
        });

        assert_eq!(statement.as_deref(), Some("INSERT INTO table_name (`data`) VALUES ('{\"a\":1}');"));
    }

    #[test]
    fn copy_update_formats_mysql_json_documents_as_compact_strings() {
        let statements = build_data_grid_copy_update_statements(DataGridCopyUpdateStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "documents".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![column("id", "int", false, None), column("payload", "JSON", true, None)]),
            },
            columns: vec!["id".to_string(), "payload".to_string()],
            source_columns: None,
            rows: vec![
                vec![json!(1), json!([111, 222, 333])],
                vec![json!(2), json!({"nested": [1, 2]})],
                vec![json!(3), json!([])],
            ],
            include_database_name: false,
        });

        assert_eq!(
            statements,
            vec![
                "UPDATE `documents` SET `payload` = '[111,222,333]' WHERE `id` = 1;",
                "UPDATE `documents` SET `payload` = '{\"nested\":[1,2]}' WHERE `id` = 2;",
                "UPDATE `documents` SET `payload` = '[]' WHERE `id` = 3;",
            ]
        );
    }

    #[test]
    fn copy_update_preserves_json_scalars_and_generic_arrays() {
        let json_statements = build_data_grid_copy_update_statements(DataGridCopyUpdateStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "documents".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![column("id", "int", false, None), column("payload", "json", true, None)]),
            },
            columns: vec!["id".to_string(), "payload".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!(r#"[1,2]"#)], vec![json!(2), json!(true)], vec![json!(3), Value::Null]],
            include_database_name: false,
        });
        assert_eq!(
            json_statements,
            vec![
                "UPDATE `documents` SET `payload` = '[1,2]' WHERE `id` = 1;",
                "UPDATE `documents` SET `payload` = TRUE WHERE `id` = 2;",
                "UPDATE `documents` SET `payload` = NULL WHERE `id` = 3;",
            ]
        );

        let generic_statements = build_data_grid_copy_update_statements(DataGridCopyUpdateStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "arrays".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: None,
            },
            columns: vec!["id".to_string(), "payload".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!([1, 2])]],
            include_database_name: false,
        });
        assert_eq!(generic_statements, vec!["UPDATE `arrays` SET `payload` = '{1,2}' WHERE `id` = 1;"]);

        assert_eq!(
            format_grid_sql_literal(
                &json!([1, 2]),
                Some(DatabaseType::Postgres),
                Some(&column("items", "integer[]", true, None)),
            ),
            "'{1,2}'"
        );
        for database_type in [DatabaseType::ClickHouse, DatabaseType::Databend] {
            assert_eq!(format_grid_sql_literal(&json!([1, 2]), Some(database_type), None), "[1,2]");
        }
    }

    #[test]
    fn builds_copy_insert_without_primary_keys_when_primary_keys_are_hidden() {
        let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            table_meta: Some(DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "users".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: None,
            }),
            columns: vec!["login_name".to_string(), "display_name".to_string()],
            column_types: None,
            source_columns: None,
            rows: vec![vec![json!("ada"), json!("Ada")]],
            primary_key_mode: DataGridPrimaryKeyMode::ExcludeGenerated,
            include_computed_columns: false,
            include_database_name: true,
            insert_mode: DataGridCopyInsertMode::Merged,
        });

        assert_eq!(
            statement.as_deref(),
            Some("INSERT INTO `users` (`login_name`, `display_name`) VALUES ('ada', 'Ada');")
        );
    }

    #[test]
    fn builds_copy_insert_statement_row_by_row() {
        let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            table_meta: Some(DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "users".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: None,
            }),
            columns: vec!["id".to_string(), "login_name".to_string(), "display_name".to_string()],
            column_types: None,
            source_columns: None,
            rows: vec![vec![json!(1), json!("ada"), json!("Ada")], vec![json!(2), json!("linus"), json!("Linus")]],
            primary_key_mode: DataGridPrimaryKeyMode::Include,
            include_computed_columns: false,
            include_database_name: true,
            insert_mode: DataGridCopyInsertMode::RowByRow,
        });
        assert_eq!(
            statement.as_deref(),
            Some(
                "INSERT INTO `users` (`id`, `login_name`, `display_name`) VALUES (1, 'ada', 'Ada');\nINSERT INTO `users` (`id`, `login_name`, `display_name`) VALUES (2, 'linus', 'Linus');"
            )
        );
    }

    #[test]
    fn oracle_copy_insert_statement_uses_one_statement_per_row() {
        let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
            database_type: Some(DatabaseType::Oracle),
            identifier_quote: None,
            table_meta: Some(DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("APP".to_string()),
                table_name: "USERS".to_string(),
                primary_keys: vec!["ID".to_string()],
                columns: None,
            }),
            columns: vec!["ID".to_string(), "NAME".to_string()],
            column_types: None,
            source_columns: None,
            rows: vec![vec![json!(1), json!("Ada")], vec![json!(2), json!("Linus")]],
            primary_key_mode: DataGridPrimaryKeyMode::Include,
            include_computed_columns: false,
            include_database_name: true,
            insert_mode: DataGridCopyInsertMode::Merged,
        });

        assert_eq!(
            statement.as_deref(),
            Some("INSERT INTO \"APP\".\"USERS\" (\"ID\", \"NAME\") VALUES (1, 'Ada');\nINSERT INTO \"APP\".\"USERS\" (\"ID\", \"NAME\") VALUES (2, 'Linus');")
        );
    }

    fn sqlserver_identity_copy_insert_options(
        rows: Vec<Vec<Value>>,
        insert_mode: DataGridCopyInsertMode,
    ) -> DataGridCopyInsertStatementOptions {
        DataGridCopyInsertStatementOptions {
            database_type: Some(DatabaseType::SqlServer),
            identifier_quote: None,
            table_meta: Some(DataGridTableMeta {
                catalog: None,
                database: Some("dbx_test".to_string()),
                schema: Some("dbo".to_string()),
                table_name: "gen_table".to_string(),
                primary_keys: vec!["table_id".to_string()],
                columns: Some(vec![
                    column("table_id", "int", false, Some("identity")),
                    column("table_name", "nvarchar(200)", false, None),
                ]),
            }),
            columns: vec!["table_id".to_string(), "table_name".to_string()],
            column_types: None,
            source_columns: None,
            rows,
            primary_key_mode: DataGridPrimaryKeyMode::Include,
            include_computed_columns: false,
            include_database_name: true,
            insert_mode,
        }
    }

    #[test]
    fn sqlserver_copy_insert_wraps_identity_columns_with_identity_insert() {
        let statement = build_data_grid_copy_insert_statement(sqlserver_identity_copy_insert_options(
            vec![vec![json!(1), json!("t_destype")]],
            DataGridCopyInsertMode::Merged,
        ));
        assert_eq!(
            statement.as_deref(),
            Some(
                "SET IDENTITY_INSERT [dbx_test].[dbo].[gen_table] ON;\nINSERT INTO [dbx_test].[dbo].[gen_table] ([table_id], [table_name]) VALUES (1, N't_destype');\nSET IDENTITY_INSERT [dbx_test].[dbo].[gen_table] OFF;"
            )
        );
    }

    #[test]
    fn sqlserver_row_by_row_copy_insert_wraps_every_statement() {
        let statement = build_data_grid_copy_insert_statement(sqlserver_identity_copy_insert_options(
            vec![vec![json!(1), json!("t_destype")], vec![json!(2), json!("t_user")]],
            DataGridCopyInsertMode::RowByRow,
        ));
        assert_eq!(
            statement.as_deref(),
            Some(
                "SET IDENTITY_INSERT [dbx_test].[dbo].[gen_table] ON;\nINSERT INTO [dbx_test].[dbo].[gen_table] ([table_id], [table_name]) VALUES (1, N't_destype');\nSET IDENTITY_INSERT [dbx_test].[dbo].[gen_table] OFF;\nSET IDENTITY_INSERT [dbx_test].[dbo].[gen_table] ON;\nINSERT INTO [dbx_test].[dbo].[gen_table] ([table_id], [table_name]) VALUES (2, N't_user');\nSET IDENTITY_INSERT [dbx_test].[dbo].[gen_table] OFF;"
            )
        );
    }

    #[test]
    fn sqlserver_copy_insert_omits_identity_wrapper_without_identity_columns() {
        let mut options = sqlserver_identity_copy_insert_options(
            vec![vec![json!(1), json!("t_destype")]],
            DataGridCopyInsertMode::Merged,
        );
        options.table_meta.as_mut().expect("table meta").columns =
            Some(vec![column("table_id", "int", false, None), column("table_name", "nvarchar(200)", false, None)]);
        let statement = build_data_grid_copy_insert_statement(options);
        assert_eq!(
            statement.as_deref(),
            Some("INSERT INTO [dbx_test].[dbo].[gen_table] ([table_id], [table_name]) VALUES (1, N't_destype');")
        );
    }

    #[test]
    fn dameng_copy_insert_wraps_identity_columns_with_identity_insert() {
        let mut options = sqlserver_identity_copy_insert_options(
            vec![vec![json!(1), json!("t_destype")]],
            DataGridCopyInsertMode::Merged,
        );
        options.database_type = Some(DatabaseType::Dameng);
        let statement = build_data_grid_copy_insert_statement(options);
        assert_eq!(
            statement.as_deref(),
            Some(
                "SET IDENTITY_INSERT \"dbo\".\"gen_table\" ON;\nINSERT INTO \"dbo\".\"gen_table\" (\"table_id\", \"table_name\") VALUES (1, 't_destype');\nSET IDENTITY_INSERT \"dbo\".\"gen_table\" OFF;"
            )
        );
    }

    #[test]
    fn dameng_copy_insert_omits_identity_wrapper_for_auto_increment_columns() {
        let mut options = sqlserver_identity_copy_insert_options(
            vec![vec![json!(1), json!("t_destype")]],
            DataGridCopyInsertMode::Merged,
        );
        options.database_type = Some(DatabaseType::Dameng);
        options.table_meta.as_mut().unwrap().columns = Some(vec![
            column("table_id", "bigint", false, Some("auto_increment")),
            column("table_name", "varchar", true, None),
        ]);
        let statement = build_data_grid_copy_insert_statement(options);
        assert_eq!(
            statement.as_deref(),
            Some("INSERT INTO \"dbo\".\"gen_table\" (\"table_id\", \"table_name\") VALUES (1, 't_destype');")
        );
    }

    #[test]
    fn mysql_copy_statements_preserve_blob_hex_literals() {
        let table_meta = DataGridTableMeta {
            catalog: None,
            database: None,
            schema: None,
            table_name: "reports".to_string(),
            primary_keys: vec!["id".to_string()],
            columns: Some(vec![column("id", "int", false, None), column("payload", "MEDIUMBLOB", true, None)]),
        };
        let columns = vec!["id".to_string(), "payload".to_string()];
        let rows = vec![vec![json!(1), json!("0x0001abff")], vec![json!(2), json!("0x")]];

        let insert = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            table_meta: Some(table_meta.clone()),
            columns: columns.clone(),
            column_types: None,
            source_columns: None,
            rows: rows.clone(),
            primary_key_mode: DataGridPrimaryKeyMode::Include,
            include_computed_columns: false,
            include_database_name: true,
            insert_mode: DataGridCopyInsertMode::Merged,
        });
        assert_eq!(
            insert.as_deref(),
            Some("INSERT INTO `reports` (`id`, `payload`) VALUES\n(1, 0x0001abff),\n(2, X'');")
        );

        let updates = build_data_grid_copy_update_statements(DataGridCopyUpdateStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            table_meta,
            columns,
            source_columns: None,
            rows,
            include_database_name: false,
        });
        assert_eq!(
            updates,
            vec![
                "UPDATE `reports` SET `payload` = 0x0001abff WHERE `id` = 1;",
                "UPDATE `reports` SET `payload` = X'' WHERE `id` = 2;"
            ]
        );
    }

    #[test]
    fn mysql_text_columns_keep_prefixed_hex_strings_quoted() {
        assert_eq!(
            format_grid_sql_literal(
                &json!("0x0001abff"),
                Some(DatabaseType::Mysql),
                Some(&column("note", "varchar(64)", true, None)),
            ),
            "'0x0001abff'"
        );
        assert_eq!(
            format_grid_sql_literal(
                &json!("0xnothex"),
                Some(DatabaseType::Mysql),
                Some(&column("payload", "blob", true, None)),
            ),
            "'0xnothex'"
        );
    }

    #[test]
    fn builds_copy_insert_statement_omits_postgres_tsvector_columns() {
        let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
            database_type: Some(DatabaseType::Postgres),
            identifier_quote: None,
            table_meta: Some(DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("public".to_string()),
                table_name: "articles".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![
                    column("id", "integer", false, None),
                    column("title", "text", false, None),
                    column("search_vector", "tsvector", true, None),
                ]),
            }),
            columns: vec!["id".to_string(), "title".to_string(), "search_vector".to_string()],
            column_types: None,
            source_columns: None,
            rows: vec![vec![json!(1), json!("Hello"), json!("'hello':1A")]],
            primary_key_mode: DataGridPrimaryKeyMode::Include,
            include_computed_columns: false,
            include_database_name: true,
            insert_mode: DataGridCopyInsertMode::Merged,
        });

        assert_eq!(
            statement.as_deref(),
            Some("INSERT INTO \"public\".\"articles\" (\"id\", \"title\") VALUES (1, 'Hello');")
        );
    }

    #[test]
    fn builds_copy_insert_statement_omits_sqlserver_rowversion_and_computed_columns() {
        // The copy path carries table metadata (sys.columns): `rowversion` shows
        // up as data_type "timestamp" and computed columns as extra "computed".
        // Both reject explicit INSERT values, so the copy INSERT must omit them
        // (issue #10764); computed returns only under the explicit option.
        let options = |include_computed_columns: bool| DataGridCopyInsertStatementOptions {
            database_type: Some(DatabaseType::SqlServer),
            identifier_quote: None,
            table_meta: Some(DataGridTableMeta {
                catalog: None,
                database: Some("dbx_test".to_string()),
                schema: Some("dbo".to_string()),
                table_name: "sync_state".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![
                    column("id", "int", false, None),
                    column("note", "nvarchar(50)", true, None),
                    column("row_version", "timestamp", false, None),
                    column("total", "int", true, Some("computed")),
                ]),
            }),
            columns: vec!["id".to_string(), "note".to_string(), "row_version".to_string(), "total".to_string()],
            column_types: None,
            source_columns: None,
            rows: vec![vec![json!(7), json!("ok"), json!("0x00000000000007D1"), json!(42)]],
            primary_key_mode: DataGridPrimaryKeyMode::Include,
            include_computed_columns,
            include_database_name: true,
            insert_mode: DataGridCopyInsertMode::Merged,
        };

        assert_eq!(
            build_data_grid_copy_insert_statement(options(false)).as_deref(),
            Some("INSERT INTO [dbx_test].[dbo].[sync_state] ([id], [note]) VALUES (7, N'ok');")
        );
        assert_eq!(
            build_data_grid_copy_insert_statement(options(true)).as_deref(),
            Some("INSERT INTO [dbx_test].[dbo].[sync_state] ([id], [note], [total]) VALUES (7, N'ok', 42);")
        );
    }

    #[test]
    fn sqlserver_rowversion_and_computed_rules_do_not_leak_into_other_dialects() {
        let rowversion = column("row_version", "timestamp", false, None);
        let computed = column("total", "int", true, Some("computed"));
        assert!(is_grid_insert_omitted_column(
            Some(DatabaseType::SqlServer),
            Some(&rowversion),
            Some("row_version"),
            false
        ));
        assert!(is_grid_insert_omitted_column(Some(DatabaseType::SqlServer), Some(&computed), Some("total"), false));
        // The computed-column option only governs the computed family; the SQL
        // Server rowversion rule stays unconditional.
        assert!(is_grid_insert_omitted_column(
            Some(DatabaseType::SqlServer),
            Some(&rowversion),
            Some("row_version"),
            true
        ));
        assert!(!is_grid_insert_omitted_column(Some(DatabaseType::SqlServer), Some(&computed), Some("total"), true));
        // MySQL `timestamp` is an ordinary datetime column and must stay insertable.
        let mysql_timestamp = column("created_at", "timestamp", false, None);
        assert!(!is_grid_insert_omitted_column(
            Some(DatabaseType::Mysql),
            Some(&mysql_timestamp),
            Some("created_at"),
            false
        ));
        assert!(!is_grid_insert_omitted_column(
            Some(DatabaseType::Postgres),
            Some(&mysql_timestamp),
            Some("created_at"),
            false
        ));
    }

    #[test]
    fn oracle_copy_insert_uses_result_column_types_for_date_literals() {
        let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
            database_type: Some(DatabaseType::Oracle),
            identifier_quote: None,
            table_meta: None,
            columns: vec!["ID".to_string(), "CREATED_ON".to_string(), "RAW_TEXT".to_string()],
            column_types: Some(vec![
                Some("NUMBER".to_string()),
                Some("DATE".to_string()),
                Some("VARCHAR2".to_string()),
            ]),
            source_columns: None,
            rows: vec![vec![json!(1), json!("2022-08-25T09:58:43Z"), json!("2022-08-25T09:58:43Z")]],
            primary_key_mode: DataGridPrimaryKeyMode::Include,
            include_computed_columns: false,
            include_database_name: true,
            insert_mode: DataGridCopyInsertMode::Merged,
        });

        assert_eq!(
            statement.as_deref(),
            Some("INSERT INTO table_name (\"ID\", \"CREATED_ON\", \"RAW_TEXT\") VALUES (1, TO_DATE('2022-08-25 09:58:43', 'YYYY-MM-DD HH24:MI:SS'), '2022-08-25T09:58:43Z');")
        );
    }

    #[test]
    fn builds_filter_conditions() {
        for (database_type, identifier_quote, column_name, expected) in [
            (DatabaseType::Gaussdb, Some("\""), "column_01", "column_01 = 1"),
            (DatabaseType::Gaussdb, Some("\""), "MixedCase", "\"MixedCase\" = 1"),
            (DatabaseType::Gaussdb, Some("\""), "order", "\"order\" = 1"),
            (DatabaseType::Gaussdb, Some("\""), "order detail", "\"order detail\" = 1"),
            (DatabaseType::Gaussdb, Some("\""), "\"AlreadyQuoted\"", "\"AlreadyQuoted\" = 1"),
            (DatabaseType::Gaussdb, Some("`"), "MixedCase", "`MixedCase` = 1"),
            (DatabaseType::Postgres, Some("`"), "order", "`order` = 1"),
            (DatabaseType::OpenGauss, Some("`"), "order detail", "`order detail` = 1"),
            (DatabaseType::Gaussdb, None, "column_01", "\"column_01\" = 1"),
            (DatabaseType::OpenGauss, None, "column_01", "\"column_01\" = 1"),
        ] {
            assert_eq!(
                build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                    database_type: Some(database_type),
                    identifier_quote: identifier_quote.map(str::to_string),
                    column_name: column_name.to_string(),
                    mode: DataGridContextFilterMode::Equals,
                    value: json!(1),
                    values: Vec::new(),
                    end_value: None,
                    column_info: Some(column(column_name, "integer", false, None)),
                })
                .as_deref(),
                Some(expected)
            );
        }
        assert_eq!(
            build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                database_type: Some(DatabaseType::Kingbase),
                identifier_quote: Some("`".to_string()),
                column_name: "file_name".to_string(),
                mode: DataGridContextFilterMode::Equals,
                value: json!("34-B-0048"),
                values: Vec::new(),
                end_value: None,
                column_info: Some(column("file_name", "varchar", false, None)),
            })
            .as_deref(),
            Some("`file_name` = '34-B-0048'")
        );
        for (mode, expected) in [
            (DataGridContextFilterMode::GreaterThanOrEqual, "`score` >= 80"),
            (DataGridContextFilterMode::LessThanOrEqual, "`score` <= 80"),
        ] {
            assert_eq!(
                build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                    database_type: Some(DatabaseType::Mysql),
                    identifier_quote: None,
                    column_name: "score".to_string(),
                    mode,
                    value: json!(80),
                    values: Vec::new(),
                    end_value: None,
                    column_info: Some(column("score", "int", false, None)),
                })
                .as_deref(),
                Some(expected)
            );
        }
        assert_eq!(
            build_data_grid_column_value_filter_condition(DataGridColumnValueFilterConditionOptions {
                database_type: Some(DatabaseType::Kingbase),
                identifier_quote: Some("`".to_string()),
                column_name: "file_name".to_string(),
                column_info: Some(column("file_name", "varchar", false, None)),
                raw_value: "34-B-0048".to_string(),
            })
            .as_deref(),
            Some("`file_name` = '34-B-0048'")
        );
        assert_eq!(
            build_data_grid_column_value_filter_condition(DataGridColumnValueFilterConditionOptions {
                database_type: Some(DatabaseType::Mysql),
                identifier_quote: None,
                column_name: "id".to_string(),
                column_info: Some(column("id", "int", false, None)),
                raw_value: "49436".to_string(),
            })
            .as_deref(),
            Some("`id` = 49436")
        );
        // Cloud Spanner (GoogleSQL) spells integers as INT64; the value must stay
        // an unquoted numeric literal or `INT64 = STRING` fails to compile.
        assert_eq!(
            build_data_grid_column_value_filter_condition(DataGridColumnValueFilterConditionOptions {
                database_type: Some(DatabaseType::Spanner),
                identifier_quote: Some("`".to_string()),
                column_name: "slipnumber".to_string(),
                column_info: Some(column("slipnumber", "INT64", false, None)),
                raw_value: "1005".to_string(),
            })
            .as_deref(),
            Some("`slipnumber` = 1005")
        );
        assert_eq!(
            build_data_grid_column_value_filter_condition(DataGridColumnValueFilterConditionOptions {
                database_type: Some(DatabaseType::Spanner),
                identifier_quote: Some("`".to_string()),
                column_name: "ratio".to_string(),
                column_info: Some(column("ratio", "FLOAT64", false, None)),
                raw_value: "1.5".to_string(),
            })
            .as_deref(),
            Some("`ratio` = 1.5")
        );
        assert_eq!(
            build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                database_type: Some(DatabaseType::Postgres),
                identifier_quote: None,
                column_name: "status".to_string(),
                mode: DataGridContextFilterMode::Like,
                value: json!("active"),
                values: Vec::new(),
                end_value: None,
                column_info: Some(column("status", "varchar", true, None)),
            })
            .as_deref(),
            Some("\"status\" LIKE '%active%'")
        );
        assert_eq!(
            build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                database_type: Some(DatabaseType::Postgres),
                identifier_quote: None,
                column_name: "update_date".to_string(),
                mode: DataGridContextFilterMode::Like,
                value: json!("128"),
                values: Vec::new(),
                end_value: None,
                column_info: Some(column("update_date", "bigint", false, None)),
            })
            .as_deref(),
            Some("\"update_date\"::text LIKE '%128%'")
        );
        assert_eq!(
            build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                database_type: Some(DatabaseType::Postgres),
                identifier_quote: None,
                column_name: "created_at".to_string(),
                mode: DataGridContextFilterMode::NotLike,
                value: json!("2026"),
                values: Vec::new(),
                end_value: None,
                column_info: Some(column("created_at", "timestamp without time zone", false, None)),
            })
            .as_deref(),
            Some("\"created_at\"::text NOT LIKE '%2026%'")
        );
        assert_eq!(
            build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                database_type: Some(DatabaseType::Mysql),
                identifier_quote: None,
                column_name: "file_name".to_string(),
                mode: DataGridContextFilterMode::BeginsWith,
                value: json!("FN"),
                values: Vec::new(),
                end_value: None,
                column_info: Some(column("file_name", "varchar", false, None)),
            })
            .as_deref(),
            Some("`file_name` LIKE 'FN%'")
        );
        assert_eq!(
            build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                database_type: Some(DatabaseType::Mysql),
                identifier_quote: None,
                column_name: "file_name".to_string(),
                mode: DataGridContextFilterMode::EndsWith,
                value: json!(".sql"),
                values: Vec::new(),
                end_value: None,
                column_info: Some(column("file_name", "varchar", false, None)),
            })
            .as_deref(),
            Some("`file_name` LIKE '%.sql'")
        );
        assert_eq!(
            build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                database_type: Some(DatabaseType::Postgres),
                identifier_quote: None,
                column_name: "update_date".to_string(),
                mode: DataGridContextFilterMode::BeginsWith,
                value: json!("128"),
                values: Vec::new(),
                end_value: None,
                column_info: Some(column("update_date", "bigint", false, None)),
            })
            .as_deref(),
            Some("\"update_date\"::text LIKE '128%'")
        );
        assert_eq!(
            build_data_grid_column_value_filter_condition(DataGridColumnValueFilterConditionOptions {
                database_type: Some(DatabaseType::SqlServer),
                identifier_quote: None,
                column_name: "active".to_string(),
                column_info: Some(column("active", "bitn", false, None)),
                raw_value: "false".to_string(),
            })
            .as_deref(),
            Some("[active] = 0")
        );
    }

    #[test]
    fn builds_blank_and_nonblank_context_filter_conditions() {
        let build = |database_type: DatabaseType,
                     mode: DataGridContextFilterMode,
                     identifier_quote: Option<&str>,
                     column_name: &str| {
            build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                database_type: Some(database_type),
                identifier_quote: identifier_quote.map(str::to_string),
                column_name: column_name.to_string(),
                mode,
                value: Value::Null,
                values: Vec::new(),
                end_value: None,
                column_info: Some(column(column_name, "varchar", true, None)),
            })
        };

        assert_eq!(
            build(DatabaseType::Mysql, DataGridContextFilterMode::IsBlank, None, "status"),
            Some("(`status` IS NULL OR `status` = '')".to_string())
        );
        assert_eq!(
            build(DatabaseType::Mysql, DataGridContextFilterMode::IsNotBlank, None, "status"),
            Some("(`status` IS NOT NULL AND `status` <> '')".to_string())
        );
        assert_eq!(
            build(DatabaseType::Mysql, DataGridContextFilterMode::IsNull, None, "status"),
            Some("`status` IS NULL".to_string())
        );
        assert_eq!(
            build(DatabaseType::Mysql, DataGridContextFilterMode::IsNotNull, None, "status"),
            Some("`status` IS NOT NULL".to_string())
        );
        assert_eq!(
            build(DatabaseType::Kingbase, DataGridContextFilterMode::IsBlank, Some("`"), "order detail"),
            Some("(`order detail` IS NULL OR `order detail` = '')".to_string())
        );

        for database_type in [DatabaseType::Oracle, DatabaseType::OceanbaseOracle] {
            assert_eq!(
                build(database_type, DataGridContextFilterMode::IsBlank, None, "STATUS"),
                Some("\"STATUS\" IS NULL".to_string())
            );
            assert_eq!(
                build(database_type, DataGridContextFilterMode::IsNotBlank, None, "STATUS"),
                Some("\"STATUS\" IS NOT NULL".to_string())
            );
        }
    }

    #[test]
    fn keeps_context_filter_mode_serialization_stable() {
        assert_eq!(serde_json::to_string(&DataGridContextFilterMode::IsNull).unwrap(), "\"is-null\"");
        assert_eq!(serde_json::to_string(&DataGridContextFilterMode::IsNotNull).unwrap(), "\"is-not-null\"");
        assert!(matches!(
            serde_json::from_str::<DataGridContextFilterMode>("\"is-null\"").unwrap(),
            DataGridContextFilterMode::IsNull
        ));
        assert!(matches!(
            serde_json::from_str::<DataGridContextFilterMode>("\"is-not-null\"").unwrap(),
            DataGridContextFilterMode::IsNotNull
        ));
        assert_eq!(serde_json::to_string(&DataGridContextFilterMode::IsBlank).unwrap(), "\"is-blank\"");
        assert_eq!(serde_json::to_string(&DataGridContextFilterMode::IsNotBlank).unwrap(), "\"is-not-blank\"");
    }

    #[test]
    fn builds_oracle_synthetic_rowid_context_filter_conditions() {
        let equals = |database_type, column_name: &str, value| {
            build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                database_type,
                identifier_quote: None,
                column_name: column_name.to_string(),
                mode: DataGridContextFilterMode::Equals,
                value,
                values: Vec::new(),
                end_value: None,
                column_info: None,
            })
        };

        for database_type in [DatabaseType::Oracle, DatabaseType::OceanbaseOracle] {
            assert_eq!(
                equals(Some(database_type), DBX_ROWID_COLUMN, json!("AAAFd1AAFAAAABSAA/")),
                Some("ROWIDTOCHAR(ROWID) = 'AAAFd1AAFAAAABSAA/'".to_string())
            );
        }
        assert_eq!(equals(Some(DatabaseType::Oracle), "REPORT_ID", json!(7)), Some("\"REPORT_ID\" = 7".to_string()));
        assert_eq!(equals(Some(DatabaseType::Neo4j), "score", json!(7)), Some("n.`score` = 7".to_string()));
        assert_eq!(equals(None, DBX_ROWID_COLUMN, json!("row-id")), Some("\"__DBX_ROWID\" = 'row-id'".to_string()));
        assert_eq!(equals(Some(DatabaseType::VictoriaMetrics), DBX_ROWID_COLUMN, json!("row-id")), None);
    }

    #[test]
    fn builds_postgres_ctid_context_filter_conditions() {
        let condition = build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
            database_type: Some(DatabaseType::Postgres),
            identifier_quote: None,
            column_name: DBX_ROWID_COLUMN.to_string(),
            mode: DataGridContextFilterMode::Equals,
            value: json!("16384:(0,1):742"),
            values: Vec::new(),
            end_value: None,
            column_info: None,
        });

        assert_eq!(
            condition.as_deref(),
            Some("(tableoid::text || ':' || ctid::text || ':' || xmin::text) = '16384:(0,1):742'")
        );
    }

    #[test]
    fn keeps_iotdb_tree_time_epoch_filters_unquoted_without_column_metadata() {
        let condition = |mode, value, values, end_value| {
            build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                database_type: Some(DatabaseType::Iotdb),
                identifier_quote: None,
                column_name: "Time".to_string(),
                mode,
                value,
                values,
                end_value,
                column_info: None,
            })
        };

        assert_eq!(
            condition(DataGridContextFilterMode::Equals, json!("1786954706123"), Vec::new(), None),
            Some("Time = 1786954706123".to_string())
        );
        assert_eq!(
            condition(
                DataGridContextFilterMode::Between,
                json!("1786954706123"),
                Vec::new(),
                Some(json!("1786958307123"))
            ),
            Some("Time BETWEEN 1786954706123 AND 1786958307123".to_string())
        );
        assert_eq!(
            condition(DataGridContextFilterMode::Equals, json!("not-an-epoch"), Vec::new(), None),
            Some("Time = 'not-an-epoch'".to_string())
        );
    }

    #[test]
    fn builds_membership_and_range_context_filter_conditions() {
        assert_eq!(
            build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                database_type: Some(DatabaseType::Mysql),
                identifier_quote: None,
                column_name: "id".to_string(),
                mode: DataGridContextFilterMode::In,
                value: Value::Null,
                values: vec![json!(42), json!(99)],
                end_value: None,
                column_info: Some(column("id", "int", false, None)),
            })
            .as_deref(),
            Some("`id` IN (42, 99)")
        );
        assert_eq!(
            build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                database_type: Some(DatabaseType::Postgres),
                identifier_quote: None,
                column_name: "status".to_string(),
                mode: DataGridContextFilterMode::In,
                value: Value::Null,
                values: vec![Value::Null, json!("active"), json!("pending"), json!("active")],
                end_value: None,
                column_info: Some(column("status", "varchar", true, None)),
            })
            .as_deref(),
            Some("(\"status\" IS NULL OR \"status\" IN ('active', 'pending'))")
        );
        assert_eq!(
            build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                database_type: Some(DatabaseType::Postgres),
                identifier_quote: None,
                column_name: "status".to_string(),
                mode: DataGridContextFilterMode::NotIn,
                value: Value::Null,
                values: vec![Value::Null, json!("active"), json!("pending")],
                end_value: None,
                column_info: Some(column("status", "varchar", true, None)),
            })
            .as_deref(),
            Some("(\"status\" IS NOT NULL AND \"status\" NOT IN ('active', 'pending'))")
        );
        assert_eq!(
            build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                database_type: Some(DatabaseType::Neo4j),
                identifier_quote: None,
                column_name: "name".to_string(),
                mode: DataGridContextFilterMode::In,
                value: Value::Null,
                values: vec![json!("O'Reilly"), json!(r"C:\temp")],
                end_value: None,
                column_info: Some(column("name", "string", false, None)),
            })
            .as_deref(),
            Some(r#"n.`name` IN ['O\'Reilly', 'C:\\temp']"#)
        );
        assert_eq!(
            build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                database_type: Some(DatabaseType::SqlServer),
                identifier_quote: None,
                column_name: "score".to_string(),
                mode: DataGridContextFilterMode::Between,
                value: json!(10),
                values: Vec::new(),
                end_value: Some(json!(20)),
                column_info: Some(column("score", "int", false, None)),
            })
            .as_deref(),
            Some("[score] BETWEEN 10 AND 20")
        );
        assert_eq!(
            build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                database_type: Some(DatabaseType::SqlServer),
                identifier_quote: None,
                column_name: "score".to_string(),
                mode: DataGridContextFilterMode::NotBetween,
                value: json!(10),
                values: Vec::new(),
                end_value: Some(json!(20)),
                column_info: Some(column("score", "int", false, None)),
            })
            .as_deref(),
            Some("[score] NOT BETWEEN 10 AND 20")
        );
    }

    #[test]
    fn builds_neo4j_membership_and_range_context_filter_conditions() {
        assert_eq!(
            build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                database_type: Some(DatabaseType::Neo4j),
                identifier_quote: None,
                column_name: "score".to_string(),
                mode: DataGridContextFilterMode::In,
                value: Value::Null,
                values: vec![json!(10), json!(20)],
                end_value: None,
                column_info: Some(column("score", "integer", false, None)),
            })
            .as_deref(),
            Some("n.`score` IN [10, 20]")
        );
        assert_eq!(
            build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                database_type: Some(DatabaseType::Neo4j),
                identifier_quote: None,
                column_name: "score".to_string(),
                mode: DataGridContextFilterMode::NotIn,
                value: Value::Null,
                values: vec![json!(10), json!(20)],
                end_value: None,
                column_info: Some(column("score", "integer", false, None)),
            })
            .as_deref(),
            Some("(n.`score` IS NOT NULL AND NOT (n.`score` IN [10, 20]))")
        );
        assert_eq!(
            build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                database_type: Some(DatabaseType::Neo4j),
                identifier_quote: None,
                column_name: "score".to_string(),
                mode: DataGridContextFilterMode::Between,
                value: json!(10),
                values: Vec::new(),
                end_value: Some(json!(20)),
                column_info: Some(column("score", "integer", false, None)),
            })
            .as_deref(),
            Some("(n.`score` >= 10 AND n.`score` <= 20)")
        );
        assert_eq!(
            build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                database_type: Some(DatabaseType::Neo4j),
                identifier_quote: None,
                column_name: "score".to_string(),
                mode: DataGridContextFilterMode::NotBetween,
                value: json!(10),
                values: Vec::new(),
                end_value: Some(json!(20)),
                column_info: Some(column("score", "integer", false, None)),
            })
            .as_deref(),
            Some("(n.`score` < 10 OR n.`score` > 20)")
        );
    }

    #[test]
    fn does_not_emit_membership_or_range_filters_for_unsupported_dialects() {
        for database_type in [DatabaseType::InfluxDb, DatabaseType::Cassandra, DatabaseType::Jdbc] {
            for mode in [
                DataGridContextFilterMode::In,
                DataGridContextFilterMode::NotIn,
                DataGridContextFilterMode::Between,
                DataGridContextFilterMode::NotBetween,
            ] {
                let (value, values, end_value) = match mode {
                    DataGridContextFilterMode::In | DataGridContextFilterMode::NotIn => {
                        (Value::Null, vec![json!(10), json!(20)], None)
                    }
                    DataGridContextFilterMode::Between | DataGridContextFilterMode::NotBetween => {
                        (json!(10), Vec::new(), Some(json!(20)))
                    }
                    _ => unreachable!("only membership and range modes are tested"),
                };

                assert_eq!(
                    build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                        database_type: Some(database_type),
                        identifier_quote: None,
                        column_name: "score".to_string(),
                        mode,
                        value,
                        values,
                        end_value,
                        column_info: Some(column("score", "int", false, None)),
                    }),
                    None,
                    "{database_type:?} must not emit {mode:?}"
                );
            }
        }
    }

    #[test]
    fn does_not_emit_sql_filters_for_victoriametrics() {
        for mode in [
            DataGridContextFilterMode::Equals,
            DataGridContextFilterMode::Like,
            DataGridContextFilterMode::GreaterThan,
            DataGridContextFilterMode::IsNull,
            DataGridContextFilterMode::IsBlank,
            DataGridContextFilterMode::IsNotBlank,
            DataGridContextFilterMode::In,
            DataGridContextFilterMode::Between,
        ] {
            assert_eq!(
                build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                    database_type: Some(DatabaseType::VictoriaMetrics),
                    identifier_quote: None,
                    column_name: "value".to_string(),
                    mode,
                    value: json!(10),
                    values: vec![json!(10), json!(20)],
                    end_value: Some(json!(20)),
                    column_info: Some(column("value", "double", false, None)),
                }),
                None,
                "VictoriaMetrics must not emit SQL filter mode {mode:?}"
            );
        }
    }

    #[test]
    fn context_membership_and_range_filters_require_complete_values() {
        let column_info = Some(column("score", "int", false, None));
        assert_eq!(
            build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                database_type: Some(DatabaseType::Mysql),
                identifier_quote: None,
                column_name: "score".to_string(),
                mode: DataGridContextFilterMode::In,
                value: Value::Null,
                values: Vec::new(),
                end_value: None,
                column_info: column_info.clone(),
            }),
            None
        );
        assert_eq!(
            build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                database_type: Some(DatabaseType::Mysql),
                identifier_quote: None,
                column_name: "score".to_string(),
                mode: DataGridContextFilterMode::Between,
                value: json!(10),
                values: Vec::new(),
                end_value: None,
                column_info: column_info.clone(),
            }),
            None
        );
        assert_eq!(
            build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                database_type: Some(DatabaseType::Mysql),
                identifier_quote: None,
                column_name: "score".to_string(),
                mode: DataGridContextFilterMode::Between,
                value: Value::Null,
                values: Vec::new(),
                end_value: Some(json!(20)),
                column_info: column_info.clone(),
            }),
            None
        );
        assert_eq!(
            build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
                database_type: Some(DatabaseType::Mysql),
                identifier_quote: None,
                column_name: "score".to_string(),
                mode: DataGridContextFilterMode::NotBetween,
                value: json!(10),
                values: Vec::new(),
                end_value: Some(Value::Null),
                column_info,
            }),
            None
        );
    }

    #[test]
    fn context_filter_options_default_new_fields_when_deserializing_old_requests() {
        let options: DataGridContextFilterConditionOptions = serde_json::from_value(json!({
            "databaseType": "mysql",
            "columnName": "id",
            "mode": "equals",
            "value": 42,
        }))
        .unwrap();

        assert!(options.values.is_empty());
        assert_eq!(options.end_value, None);
    }

    #[test]
    fn builds_multi_value_filter_conditions() {
        assert_eq!(
            build_data_grid_column_values_filter_condition(DataGridColumnValuesFilterConditionOptions {
                database_type: Some(DatabaseType::Postgres),
                identifier_quote: None,
                column_name: "status".to_string(),
                column_info: Some(column("status", "varchar", true, None)),
                values: vec![json!("active"), json!("pending"), Value::Null, json!("active")],
            })
            .as_deref(),
            Some("(\"status\" IS NULL OR \"status\" IN ('active', 'pending'))")
        );
        assert_eq!(
            build_data_grid_column_values_filter_condition(DataGridColumnValuesFilterConditionOptions {
                database_type: Some(DatabaseType::Mysql),
                identifier_quote: None,
                column_name: "id".to_string(),
                column_info: Some(column("id", "int", false, None)),
                values: vec![json!(42)],
            })
            .as_deref(),
            Some("`id` = 42")
        );
    }

    #[test]
    fn chunks_large_oracle_membership_filters_with_correct_boolean_operator() {
        let values = (0..=1000).map(|value| json!(value)).collect::<Vec<_>>();
        let in_condition = build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
            database_type: Some(DatabaseType::Oracle),
            identifier_quote: None,
            column_name: "id".to_string(),
            mode: DataGridContextFilterMode::In,
            value: Value::Null,
            values: values.clone(),
            end_value: None,
            column_info: Some(column("id", "number", false, None)),
        })
        .unwrap();
        assert_eq!(in_condition.matches("\"id\" IN (").count(), 2);
        assert!(in_condition.contains(") OR \"id\" IN ("));

        let not_in_condition = build_data_grid_context_filter_condition(DataGridContextFilterConditionOptions {
            database_type: Some(DatabaseType::Oracle),
            identifier_quote: None,
            column_name: "id".to_string(),
            mode: DataGridContextFilterMode::NotIn,
            value: Value::Null,
            values,
            end_value: None,
            column_info: Some(column("id", "number", false, None)),
        })
        .unwrap();
        assert_eq!(not_in_condition.matches("\"id\" NOT IN (").count(), 2);
        assert!(not_in_condition.contains(") AND \"id\" NOT IN ("));
    }

    #[test]
    fn keeps_postgres_bit_strings_out_of_boolean_literal_handling() {
        let bit = column("flags", "bit(3)", false, None);
        let varying = column("flags", "bit varying", false, None);

        assert_eq!(parse_typed_filter_value("true", Some(DatabaseType::Postgres), Some(&bit)), json!("true"));
        assert_eq!(parse_typed_filter_value("false", Some(DatabaseType::Postgres), Some(&varying)), json!("false"));
        assert_eq!(
            build_data_grid_column_value_filter_condition(DataGridColumnValueFilterConditionOptions {
                database_type: Some(DatabaseType::Postgres),
                identifier_quote: None,
                column_name: "flags".to_string(),
                column_info: Some(bit),
                raw_value: "true".to_string(),
            })
            .as_deref(),
            Some("\"flags\" = 'true'")
        );
        assert_eq!(
            format_grid_sql_literal(
                &json!(true),
                Some(DatabaseType::SqlServer),
                Some(&column("flag", "bit", false, None))
            ),
            "1"
        );
    }

    #[test]
    fn builds_column_distinct_values_sql() {
        assert_eq!(
            build_data_grid_column_distinct_values_sql(DataGridColumnDistinctValuesSqlOptions {
                database_type: Some(DatabaseType::Postgres),
                driver_profile: None,
                identifier_quote: None,
                catalog: None,
                database: None,
                schema: Some("public".to_string()),
                table_name: "users".to_string(),
                column_name: "status".to_string(),
                column_info: Some(column("status", "varchar", true, None)),
                where_input: Some("WHERE deleted_at IS NULL;".to_string()),
                search_value: Some("act".to_string()),
                limit: None,
                include_counts: true,
                exclude_nulls: true,
            }),
            "SELECT \"status\" AS dbx_value, COUNT(*) AS dbx_count FROM \"public\".\"users\" WHERE (deleted_at IS NULL) AND \"status\" IS NOT NULL AND \"status\" LIKE '%act%' GROUP BY \"status\" ORDER BY dbx_count DESC, dbx_value LIMIT 1000"
        );
        assert_eq!(
            build_data_grid_column_distinct_values_sql(DataGridColumnDistinctValuesSqlOptions {
                database_type: Some(DatabaseType::SqlServer),
                driver_profile: None,
                identifier_quote: None,
                catalog: None,
                database: None,
                schema: None,
                table_name: "users".to_string(),
                column_name: "status".to_string(),
                column_info: Some(column("status", "nvarchar", true, None)),
                where_input: None,
                search_value: None,
                limit: Some(25),
                include_counts: false,
                exclude_nulls: true,
            }),
            "SELECT TOP (25) [status] AS dbx_value FROM [users] WHERE [status] IS NOT NULL GROUP BY [status] ORDER BY dbx_value"
        );
        assert_eq!(
            build_data_grid_column_distinct_values_sql(DataGridColumnDistinctValuesSqlOptions {
                database_type: Some(DatabaseType::SqlServer),
                driver_profile: None,
                identifier_quote: None,
                catalog: None,
                database: None,
                schema: None,
                table_name: "users".to_string(),
                column_name: "id".to_string(),
                column_info: Some(column("id", "int", false, None)),
                where_input: None,
                search_value: Some("42".to_string()),
                limit: Some(25),
                include_counts: true,
                exclude_nulls: false,
            }),
            "SELECT TOP (25) [id] AS dbx_value, COUNT(*) AS dbx_count FROM [users] WHERE [id] = 42 GROUP BY [id] ORDER BY dbx_count DESC, dbx_value"
        );
        assert_eq!(
            build_data_grid_column_distinct_values_sql(DataGridColumnDistinctValuesSqlOptions {
                database_type: Some(DatabaseType::SqlServer),
                driver_profile: Some(" SQLSERVER-LEGACY ".to_string()),
                identifier_quote: None,
                catalog: None,
                database: None,
                schema: None,
                table_name: "users".to_string(),
                column_name: "status".to_string(),
                column_info: Some(column("status", "nvarchar", true, None)),
                where_input: None,
                search_value: None,
                limit: Some(25),
                include_counts: true,
                exclude_nulls: false,
            }),
            "SELECT [status] AS dbx_value, COUNT(*) AS dbx_count FROM [users] GROUP BY [status] ORDER BY dbx_count DESC, dbx_value"
        );
        assert_eq!(
            build_data_grid_column_distinct_values_sql(DataGridColumnDistinctValuesSqlOptions {
                database_type: Some(DatabaseType::Oracle),
                driver_profile: None,
                identifier_quote: None,
                catalog: None,
                database: None,
                schema: Some("APP".to_string()),
                table_name: "EVENTS".to_string(),
                column_name: "KIND".to_string(),
                column_info: Some(column("KIND", "VARCHAR2", true, None)),
                where_input: None,
                search_value: None,
                limit: Some(10),
                include_counts: true,
                exclude_nulls: false,
            }),
            "SELECT * FROM (SELECT \"KIND\" AS dbx_value, COUNT(*) AS dbx_count FROM \"APP\".\"EVENTS\" GROUP BY \"KIND\" ORDER BY dbx_count DESC, dbx_value) WHERE ROWNUM <= 10"
        );
        assert_eq!(
            build_data_grid_column_distinct_values_sql(DataGridColumnDistinctValuesSqlOptions {
                database_type: Some(DatabaseType::Firebird),
                driver_profile: None,
                identifier_quote: None,
                catalog: None,
                database: None,
                schema: None,
                table_name: "USERS".to_string(),
                column_name: "STATUS".to_string(),
                column_info: Some(column("STATUS", "varchar(32)", true, None)),
                where_input: Some("WHERE DELETED_AT IS NULL".to_string()),
                search_value: None,
                limit: Some(25),
                include_counts: false,
                exclude_nulls: false,
            }),
            "SELECT \"STATUS\" AS dbx_value FROM \"USERS\" WHERE (DELETED_AT IS NULL) GROUP BY \"STATUS\" ORDER BY dbx_value ROWS 25"
        );
        // Doris / StarRocks external-catalog tables are addressed with a 3-part name.
        assert_eq!(
            build_data_grid_column_distinct_values_sql(DataGridColumnDistinctValuesSqlOptions {
                database_type: Some(DatabaseType::Doris),
                driver_profile: None,
                identifier_quote: None,
                catalog: Some("iceberg_catalog".to_string()),
                database: None,
                schema: Some("sales".to_string()),
                table_name: "orders".to_string(),
                column_name: "status".to_string(),
                column_info: Some(column("status", "varchar", true, None)),
                where_input: None,
                search_value: None,
                limit: Some(10),
                include_counts: false,
                exclude_nulls: false,
            }),
            "SELECT `status` AS dbx_value FROM `iceberg_catalog`.`sales`.`orders` GROUP BY `status` ORDER BY dbx_value LIMIT 10"
        );
        assert_eq!(
            build_data_grid_column_distinct_values_sql(DataGridColumnDistinctValuesSqlOptions {
                database_type: Some(DatabaseType::StarRocks),
                driver_profile: None,
                identifier_quote: None,
                catalog: Some("hive_catalog".to_string()),
                database: None,
                schema: None,
                table_name: "orders".to_string(),
                column_name: "status".to_string(),
                column_info: Some(column("status", "varchar", true, None)),
                where_input: None,
                search_value: None,
                limit: Some(10),
                include_counts: true,
                exclude_nulls: false,
            }),
            "SELECT `status` AS dbx_value, COUNT(*) AS dbx_count FROM `hive_catalog`.`orders` GROUP BY `status` ORDER BY dbx_count DESC, dbx_value LIMIT 10"
        );
        // The built-in `internal` catalog is never prefixed.
        assert_eq!(
            build_data_grid_column_distinct_values_sql(DataGridColumnDistinctValuesSqlOptions {
                database_type: Some(DatabaseType::Doris),
                driver_profile: None,
                identifier_quote: None,
                catalog: Some("internal".to_string()),
                database: None,
                schema: None,
                table_name: "orders".to_string(),
                column_name: "status".to_string(),
                column_info: Some(column("status", "varchar", true, None)),
                where_input: None,
                search_value: None,
                limit: Some(10),
                include_counts: false,
                exclude_nulls: false,
            }),
            "SELECT `status` AS dbx_value FROM `orders` GROUP BY `status` ORDER BY dbx_value LIMIT 10"
        );
        assert_eq!(
            build_data_grid_column_distinct_values_sql(DataGridColumnDistinctValuesSqlOptions {
                database_type: Some(DatabaseType::Neo4j),
                driver_profile: None,
                identifier_quote: None,
                catalog: None,
                database: None,
                schema: None,
                table_name: "User".to_string(),
                column_name: "status".to_string(),
                column_info: Some(column("status", "string", true, None)),
                where_input: Some("n.active = true".to_string()),
                search_value: None,
                limit: Some(10),
                include_counts: true,
                exclude_nulls: true,
            }),
            "MATCH (n:`User`) WHERE n.active = true AND n.`status` IS NOT NULL RETURN n.`status` AS dbx_value, count(*) AS dbx_count ORDER BY dbx_count DESC, dbx_value LIMIT 10"
        );
    }

    #[test]
    fn builds_grid_count_sql() {
        assert_eq!(
            build_data_grid_count_sql(DataGridCountSqlOptions {
                database_type: Some(DatabaseType::Postgres),
                identifier_quote: None,
                catalog: None,
                database: None,
                schema: Some("public".to_string()),
                table_name: "users".to_string(),
                where_input: Some("WHERE active = true;".to_string()),
                count_hint: None,
            }),
            "SELECT COUNT(*) AS cnt FROM \"public\".\"users\" WHERE (active = true)"
        );
        assert_eq!(
            build_data_grid_count_sql(DataGridCountSqlOptions {
                database_type: Some(DatabaseType::Jdbc),
                identifier_quote: None,
                catalog: None,
                database: None,
                schema: Some("DEMO".to_string()),
                table_name: "STUDENT".to_string(),
                where_input: None,
                count_hint: None,
            }),
            "SELECT COUNT(*) AS cnt FROM DEMO.STUDENT"
        );
        assert_eq!(
            build_data_grid_count_sql(DataGridCountSqlOptions {
                database_type: Some(DatabaseType::Doris),
                identifier_quote: None,
                catalog: Some("iceberg_catalog".to_string()),
                database: None,
                schema: Some("sales".to_string()),
                table_name: "orders".to_string(),
                where_input: Some("WHERE active = true;".to_string()),
                count_hint: None,
            }),
            "SELECT COUNT(*) AS cnt FROM `iceberg_catalog`.`sales`.`orders` WHERE (active = true)"
        );
        // catalog + database (schema absent) → 3-part `catalog.database.table`
        assert_eq!(
            build_data_grid_count_sql(DataGridCountSqlOptions {
                database_type: Some(DatabaseType::Doris),
                identifier_quote: None,
                catalog: Some("iceberg_catalog".to_string()),
                database: Some("sales".to_string()),
                schema: None,
                table_name: "orders".to_string(),
                where_input: Some("WHERE active = true;".to_string()),
                count_hint: None,
            }),
            "SELECT COUNT(*) AS cnt FROM `iceberg_catalog`.`sales`.`orders` WHERE (active = true)"
        );
        assert_eq!(
            build_data_grid_count_sql(DataGridCountSqlOptions {
                database_type: Some(DatabaseType::StarRocks),
                identifier_quote: None,
                catalog: Some("hive_catalog".to_string()),
                database: None,
                schema: None,
                table_name: "orders".to_string(),
                where_input: None,
                count_hint: None,
            }),
            "SELECT COUNT(*) AS cnt FROM `hive_catalog`.`orders`"
        );
        assert_eq!(
            build_data_grid_count_sql(DataGridCountSqlOptions {
                database_type: Some(DatabaseType::Kingbase),
                identifier_quote: Some("`".to_string()),
                catalog: None,
                database: None,
                schema: Some("cqbq_ls".to_string()),
                table_name: "ANALYZE".to_string(),
                where_input: None,
                count_hint: None,
            }),
            "SELECT COUNT(*) AS cnt FROM `cqbq_ls`.`ANALYZE`"
        );
        assert_eq!(
            build_data_grid_count_sql(DataGridCountSqlOptions {
                database_type: Some(DatabaseType::Gaussdb),
                identifier_quote: Some("\"".to_string()),
                catalog: None,
                database: None,
                schema: Some("schema_01".to_string()),
                table_name: "table_01".to_string(),
                where_input: None,
                count_hint: None,
            }),
            "SELECT COUNT(*) AS cnt FROM schema_01.table_01"
        );
        assert_eq!(
            build_data_grid_count_sql(DataGridCountSqlOptions {
                database_type: Some(DatabaseType::Postgres),
                identifier_quote: Some("`".to_string()),
                catalog: None,
                database: None,
                schema: Some("App Schema".to_string()),
                table_name: "order".to_string(),
                where_input: None,
                count_hint: None,
            }),
            "SELECT COUNT(*) AS cnt FROM `App Schema`.`order`"
        );
    }

    #[test]
    fn builds_grid_count_sql_with_optimizer_hint() {
        // GaussDB with optimizer hint
        assert_eq!(
            build_data_grid_count_sql(DataGridCountSqlOptions {
                database_type: Some(DatabaseType::Gaussdb),
                identifier_quote: Some("\"".to_string()),
                catalog: None,
                database: None,
                schema: Some("schema_01".to_string()),
                table_name: "table_01".to_string(),
                where_input: None,
                count_hint: Some("/*+ set(query_dop 32) */".to_string()),
            }),
            "SELECT /*+ set(query_dop 32) */ COUNT(*) AS cnt FROM schema_01.table_01"
        );
        // GaussDB with hint and WHERE clause
        assert_eq!(
            build_data_grid_count_sql(DataGridCountSqlOptions {
                database_type: Some(DatabaseType::Gaussdb),
                identifier_quote: None,
                catalog: None,
                database: None,
                schema: Some("sec_acct".to_string()),
                table_name: "main_sec_acct_info".to_string(),
                where_input: Some("status = 'active'".to_string()),
                count_hint: Some("/*+ set(query_dop 32) */".to_string()),
            }),
            "SELECT /*+ set(query_dop 32) */ COUNT(*) AS cnt FROM \"sec_acct\".\"main_sec_acct_info\" WHERE (status = 'active')"
        );
        // Non-GaussDB database with hint (should still work)
        assert_eq!(
            build_data_grid_count_sql(DataGridCountSqlOptions {
                database_type: Some(DatabaseType::Postgres),
                identifier_quote: None,
                catalog: None,
                database: None,
                schema: Some("public".to_string()),
                table_name: "users".to_string(),
                where_input: None,
                count_hint: Some("/*+ set(query_dop 32) */".to_string()),
            }),
            "SELECT /*+ set(query_dop 32) */ COUNT(*) AS cnt FROM \"public\".\"users\""
        );
        // No hint (default) — unchanged behavior
        assert_eq!(
            build_data_grid_count_sql(DataGridCountSqlOptions {
                database_type: Some(DatabaseType::Gaussdb),
                identifier_quote: None,
                catalog: None,
                database: None,
                schema: Some("schema_01".to_string()),
                table_name: "table_01".to_string(),
                where_input: None,
                count_hint: None,
            }),
            "SELECT COUNT(*) AS cnt FROM \"schema_01\".\"table_01\""
        );
    }

    #[test]
    fn builds_hive_table_properties_sql() {
        assert_eq!(
            build_hive_table_properties_sql(HiveTablePropertiesSqlOptions {
                database_type: None,
                schema: Some("default".to_string()),
                table_name: "events".to_string(),
                property_name: "transactional".to_string(),
            }),
            "SHOW TBLPROPERTIES `default`.`events` ('transactional')"
        );
    }

    #[test]
    fn prepares_hive_insert_from_qualified_result_labels() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Hive),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("ai_test".to_string()),
                table_name: "t1".to_string(),
                primary_keys: vec![],
                columns: Some(vec![
                    column("id", "int", false, None),
                    column("name", "string", true, None),
                    column("amount", "double", true, None),
                    column("create_time", "string", true, None),
                ]),
            },
            columns: vec![
                "t1.id".to_string(),
                "t1.name".to_string(),
                "t1.amount".to_string(),
                "t1.create_time".to_string(),
            ],
            source_columns: None,
            rows: vec![],
            dirty_rows: vec![],
            deleted_rows: vec![],
            new_rows: vec![vec![json!(4), Value::Null, Value::Null, Value::Null]],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements, vec!["INSERT INTO TABLE `ai_test`.`t1` VALUES (4, NULL, NULL, NULL);"]);
        assert_eq!(
            result.rollback_statements,
            vec!["DELETE FROM `ai_test`.`t1` WHERE `id` = 4 AND `name` IS NULL AND `amount` IS NULL AND `create_time` IS NULL;"]
        );
    }

    #[test]
    fn resolves_quoted_hive_source_columns_for_updates_and_primary_keys() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Hive),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("default".to_string()),
                table_name: "users".to_string(),
                primary_keys: vec!["ID".to_string()],
                columns: Some(vec![column("id", "int", false, None), column("name", "string", true, None)]),
            },
            columns: vec!["identifier".to_string(), "display_name".to_string()],
            source_columns: Some(vec![Some("`u`.`id`".to_string()), Some("`u`.`name`".to_string())]),
            rows: vec![vec![json!(1), json!("Ada")], vec![json!(2), json!("Grace")]],
            dirty_rows: vec![(0, vec![(1, json!("Ada Lovelace"))])],
            deleted_rows: vec![1],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec![
                "UPDATE `default`.`users` SET `name` = 'Ada Lovelace' WHERE `ID` = 1;",
                "DELETE FROM `default`.`users` WHERE `ID` = 2;",
            ]
        );
        assert_eq!(
            result.rollback_statements,
            vec![
                "INSERT INTO TABLE `default`.`users` VALUES (2, 'Grace');",
                "UPDATE `default`.`users` SET `name` = 'Ada' WHERE `ID` = 1 AND `name` = 'Ada Lovelace';",
            ]
        );
    }

    #[test]
    fn preserves_real_dotted_hive_columns_and_other_dialects() {
        let hive_options = DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Hive),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("default".to_string()),
                table_name: "events".to_string(),
                primary_keys: vec![],
                columns: Some(vec![column("payload.id", "int", true, None), column("name", "string", true, None)]),
            },
            columns: vec!["payload.id".to_string(), "events.name".to_string()],
            source_columns: None,
            rows: vec![],
            dirty_rows: vec![],
            deleted_rows: vec![],
            new_rows: vec![vec![json!(7), json!("created")]],
            include_database_name: false,
        };
        assert_eq!(effective_columns(&hive_options), vec![Some("payload.id".to_string()), Some("name".to_string())]);
        assert!(prepare_data_grid_save(hive_options).rollback_statements[0].contains("`payload.id` = 7"));

        let postgres_options = DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Postgres),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("public".to_string()),
                table_name: "events".to_string(),
                primary_keys: vec![],
                columns: Some(vec![column("id", "integer", true, None)]),
            },
            columns: vec!["events.id".to_string()],
            source_columns: None,
            rows: vec![],
            dirty_rows: vec![],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        };
        assert_eq!(effective_columns(&postgres_options), vec![Some("events.id".to_string())]);
    }

    #[test]
    fn transwarp_grid_insert_and_restore_use_select_syntax() {
        let options = DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Transwarp),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("default".to_string()),
                table_name: "events".to_string(),
                primary_keys: vec![],
                columns: Some(vec![column("id", "int", true, None), column("name", "string", true, None)]),
            },
            columns: vec!["id".to_string(), "name".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("before")]],
            dirty_rows: vec![],
            deleted_rows: vec![0],
            new_rows: vec![vec![json!(2), json!("after")]],
            include_database_name: false,
        };
        let result = prepare_data_grid_save(options);
        assert!(result
            .statements
            .iter()
            .any(|sql| sql.contains("INSERT INTO `default`.`events` (`id`, `name`) SELECT 2, 'after'")));
        assert!(result
            .rollback_statements
            .iter()
            .any(|sql| sql.contains("INSERT INTO `default`.`events` (`id`, `name`) SELECT 1, 'before'")));
    }

    #[test]
    fn transwarp_transactional_property_uses_system_table() {
        let sql = build_hive_table_properties_sql(HiveTablePropertiesSqlOptions {
            database_type: Some(DatabaseType::Transwarp),
            schema: Some("analytics".to_string()),
            table_name: "events".to_string(),
            property_name: "transactional".to_string(),
        });
        assert_eq!(
            sql,
            "SELECT transactional FROM system.tables_v WHERE database_name = 'analytics' AND table_name = 'events'"
        );
    }

    #[test]
    fn formats_temporal_copy_literals() {
        assert_eq!(
            format_grid_sql_literal(&json!("2026-05-12T00:00:00+00:00"), Some(DatabaseType::Mysql), None),
            "'2026-05-12 00:00:00'"
        );
        assert_eq!(
            format_grid_sql_literal(&json!("2026-05-12T00:00:00.123456Z"), Some(DatabaseType::Mysql), None),
            "'2026-05-12 00:00:00.123456'"
        );
        assert_eq!(
            format_grid_sql_literal(&json!("2026-05-12 00:00:00.123456"), Some(DatabaseType::Mysql), None),
            "'2026-05-12 00:00:00.123456'"
        );
    }

    #[test]
    fn keeps_temporal_like_text_with_multibyte_seconds_verbatim() {
        let date = column("event_day", "DATE", true, None);
        let text = "2026-05-12 00:00:05";
        for (database_type, column_info) in
            [(DatabaseType::Mysql, None), (DatabaseType::Tdengine, None), (DatabaseType::Oracle, Some(&date))]
        {
            assert_eq!(
                format_grid_sql_literal(&json!(text), Some(database_type), column_info),
                format!("'{text}'"),
                "{database_type:?}"
            );
        }
    }

    #[test]
    fn keeps_temporal_like_text_with_multibyte_date_field_verbatim() {
        let date = column("event_day", "DATE", true, None);
        let text = "2026-05-12 00:00:00";
        for database_type in [DatabaseType::Oracle, DatabaseType::OceanbaseOracle] {
            assert_eq!(
                format_grid_sql_literal(&json!(text), Some(database_type), Some(&date)),
                format!("'{text}'"),
                "{database_type:?}"
            );
        }
    }

    #[test]
    fn formats_sqlserver_datetime_copy_literals_with_supported_precision() {
        let datetime = column("date1", "datetime", true, None);
        let datetime2 = column("date2", "datetime2(7)", true, None);
        let raw_text = column("note", "nvarchar(64)", true, None);

        assert_eq!(
            format_grid_sql_literal(
                &json!("2026-06-29 10:11:12.896666666"),
                Some(DatabaseType::SqlServer),
                Some(&datetime)
            ),
            "N'2026-06-29 10:11:12.897'"
        );
        assert_eq!(
            format_grid_sql_literal(
                &json!("2026-06-29 10:11:12.8966666"),
                Some(DatabaseType::SqlServer),
                Some(&datetime2)
            ),
            "N'2026-06-29 10:11:12.8966666'"
        );
        assert_eq!(
            format_grid_sql_literal(
                &json!("2026-06-29 10:11:12.123456"),
                Some(DatabaseType::SqlServer),
                Some(&datetime2)
            ),
            "N'2026-06-29 10:11:12.1234560'"
        );
        assert_eq!(
            format_grid_sql_literal(
                &json!("2026-06-29 10:11:12.896666666"),
                Some(DatabaseType::SqlServer),
                Some(&raw_text)
            ),
            "N'2026-06-29 10:11:12.896666666'"
        );
    }

    #[test]
    fn formats_oracle_temporal_literals_without_nls_parsing() {
        let timestamp = column("created_at", "TIMESTAMP(6)", true, None);
        let timestamp_tz = column("recorded_at", "TIMESTAMP(6) WITH TIME ZONE", true, None);
        let timestamp_ltz = column("local_recorded_at", "TIMESTAMP(6) WITH LOCAL TIME ZONE", true, None);
        let date = column("event_day", "DATE", true, None);
        let text = column("raw_text", "VARCHAR2(64)", true, None);

        assert_eq!(
            format_grid_sql_literal(&json!("2022-08-25T09:58:43Z"), Some(DatabaseType::Oracle), Some(&timestamp)),
            "TO_TIMESTAMP('2022-08-25 09:58:43', 'YYYY-MM-DD HH24:MI:SS')"
        );
        assert_eq!(
            format_grid_sql_literal(
                &json!("2022-08-25T09:58:43.123456+08:00"),
                Some(DatabaseType::Oracle),
                Some(&timestamp_tz)
            ),
            "TO_TIMESTAMP_TZ('2022-08-25 09:58:43.123456 +08:00', 'YYYY-MM-DD HH24:MI:SS.FF TZH:TZM')"
        );
        assert_eq!(
            format_grid_sql_literal(&json!("2022-08-25T09:58:43Z"), Some(DatabaseType::Oracle), Some(&timestamp_ltz)),
            "TO_TIMESTAMP_TZ('2022-08-25 09:58:43 +00:00', 'YYYY-MM-DD HH24:MI:SS TZH:TZM')"
        );
        assert_eq!(
            format_grid_sql_literal(&json!("2022-08-25T09:58:43Z"), Some(DatabaseType::Oracle), Some(&date)),
            "TO_DATE('2022-08-25 09:58:43', 'YYYY-MM-DD HH24:MI:SS')"
        );
        assert_eq!(
            format_grid_sql_literal(&json!("2022-08-25T09:58:43Z"), Some(DatabaseType::Oracle), Some(&text)),
            "'2022-08-25T09:58:43Z'"
        );
    }

    #[test]
    fn copy_insert_preserves_oracle_agent_timezone_values_and_nulls() {
        for database_type in [DatabaseType::Oracle, DatabaseType::OceanbaseOracle] {
            for (data_type, value, literal) in [
                (
                    "TIMESTAMP WITH TIME ZONE",
                    "2026-10-09 13:14:15 +8:00",
                    "TO_TIMESTAMP_TZ('2026-10-09 13:14:15 +08:00', 'YYYY-MM-DD HH24:MI:SS TZH:TZM')",
                ),
                (
                    "TIMESTAMP(6) WITH TIME ZONE",
                    "2026-10-09 13:14:15.123456 +8:00",
                    "TO_TIMESTAMP_TZ('2026-10-09 13:14:15.123456 +08:00', 'YYYY-MM-DD HH24:MI:SS.FF TZH:TZM')",
                ),
                (
                    "timestamp(9) with time zone",
                    "2026-10-09 13:14:15.123456789 -5:30",
                    "TO_TIMESTAMP_TZ('2026-10-09 13:14:15.123456789 -05:30', 'YYYY-MM-DD HH24:MI:SS.FF TZH:TZM')",
                ),
                (
                    "TIMESTAMP(3) WITH TIME ZONE",
                    "2026-10-09 13:14:15.123 +00:00",
                    "TO_TIMESTAMP_TZ('2026-10-09 13:14:15.123 +00:00', 'YYYY-MM-DD HH24:MI:SS.FF TZH:TZM')",
                ),
            ] {
                for has_table_columns in [true, false] {
                    let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
                        database_type: Some(database_type),
                        identifier_quote: None,
                        table_meta: Some(DataGridTableMeta {
                            catalog: None,
                            database: None,
                            schema: None,
                            table_name: "tz_roundtrip".to_string(),
                            primary_keys: vec![],
                            columns: has_table_columns.then(|| vec![column("Z", data_type, true, None)]),
                        }),
                        columns: vec!["Z".to_string()],
                        column_types: Some(vec![Some(data_type.to_string())]),
                        source_columns: None,
                        rows: vec![vec![json!(value)], vec![Value::Null]],
                        primary_key_mode: DataGridPrimaryKeyMode::Include,
                        include_computed_columns: false,
                        include_database_name: false,
                        insert_mode: DataGridCopyInsertMode::RowByRow,
                    })
                    .unwrap();
                    assert!(statement.contains(literal), "{database_type:?}: {statement}");
                    assert!(statement.contains("VALUES (NULL)"), "{statement}");
                }
            }
        }
    }

    #[test]
    fn formats_oracle_temporal_literals_from_editor_values_without_nls_parsing() {
        let timestamp = column("created_at", "TIMESTAMP(6)", true, None);
        let date = column("event_day", "DATE", true, None);

        assert_eq!(
            format_grid_sql_literal(&json!("2022-08-25 09:58:43"), Some(DatabaseType::Oracle), Some(&timestamp)),
            "TO_TIMESTAMP('2022-08-25 09:58:43', 'YYYY-MM-DD HH24:MI:SS')"
        );
        assert_eq!(
            format_grid_sql_literal(&json!("2022-08-25 09:58:43.123456"), Some(DatabaseType::Oracle), Some(&timestamp)),
            "TO_TIMESTAMP('2022-08-25 09:58:43.123456', 'YYYY-MM-DD HH24:MI:SS.FF')"
        );
        assert_eq!(
            format_grid_sql_literal(&json!("2022-08-25 09:58:43.654321"), Some(DatabaseType::Oracle), Some(&timestamp)),
            "TO_TIMESTAMP('2022-08-25 09:58:43.654321', 'YYYY-MM-DD HH24:MI:SS.FF')"
        );
        assert_eq!(
            format_grid_sql_literal(&json!("2022-08-25 09:58:43"), Some(DatabaseType::Oracle), Some(&date)),
            "TO_DATE('2022-08-25 09:58:43', 'YYYY-MM-DD HH24:MI:SS')"
        );
        assert_eq!(
            format_grid_sql_literal(&json!("2022-08-25"), Some(DatabaseType::Oracle), Some(&date)),
            "DATE '2022-08-25'"
        );
        assert_eq!(
            format_grid_sql_literal(&json!("2022-08-25T00:00:00Z"), Some(DatabaseType::Oracle), Some(&date)),
            "DATE '2022-08-25'"
        );
    }

    #[test]
    fn prepares_oceanbase_blob_bytes_and_snapshot_guards() {
        let blob = column("CONTENT", "BLOB", true, None);
        let database_type = Some(DatabaseType::OceanbaseOracle);
        assert_eq!(format_grid_assignment_sql_literal(&json!("0x"), database_type, Some(&blob), None), "EMPTY_BLOB()");
        assert_eq!(format_grid_assignment_sql_literal(&Value::Null, database_type, Some(&blob), None), "NULL");
        assert_eq!(
            format_grid_assignment_sql_literal(&json!("0x00ff"), database_type, Some(&blob), None),
            "TO_BLOB(HEXTORAW('00ff'))"
        );
        assert_eq!(
            build_column_predicate(database_type, "CONTENT", &json!("0x"), Some(&blob), true, None),
            "(DBMS_LOB.GETLENGTH(\"CONTENT\") = 0)"
        );
        let original = format!("0x{}ff", "00".repeat(1000));
        let new_value = format!("0x{}80", "00ff".repeat(1500));
        let mut options = DataGridSaveStatementOptions {
            database_type,
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("APP".into()),
                table_name: "FILES".into(),
                primary_keys: vec!["ID".into()],
                columns: Some(vec![column("ID", "NUMBER", false, None), blob]),
            },
            columns: vec!["ID".into(), "CONTENT".into()],
            source_columns: None,
            rows: vec![vec![json!(1), json!(original)]],
            dirty_rows: vec![(0, vec![(1, json!(new_value))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        };
        let prepared = prepare_data_grid_save(options.clone());
        assert_eq!(prepared.validation_error, None);
        let sql = &prepared.statements[0];
        assert!(sql.starts_with("DECLARE\n"));
        assert!(sql.contains("CREATETEMPORARY(dbx_lob_0"));
        assert_eq!(sql.matches("WRITEAPPEND(dbx_lob_0").count(), 4);
        assert!(sql.contains("WRITEAPPEND(dbx_lob_0, 1, HEXTORAW('80'))"));
        assert!(sql.contains("EXECUTE IMMEDIATE dbx_sql USING dbx_lob_0"));
        assert!(sql.contains("DBMS_LOB.SUBSTR(\"CONTENT\", 1, 1001) = HEXTORAW(''ff'')"));
        assert!(sql.contains("SQL%ROWCOUNT <> 1"));
        assert_eq!(sql.matches("FREETEMPORARY(dbx_lob_0)").count(), 2);
        assert!(prepared.rollback_statements[0].contains("DBMS_LOB.GETLENGTH(\"CONTENT\") = 3001"));
        options.dirty_rows[0].1[0].1 = json!("0x123");
        let invalid = prepare_data_grid_save(options);
        assert!(invalid.validation_error.is_some());
        assert!(invalid.statements.is_empty());
    }

    #[test]
    fn oceanbase_large_blob_baseline_uses_one_exact_bound_comparison() {
        let original = format!("0x{}80", "00ff".repeat(524291)); // 1 MiB + 7 bytes
        let prepared = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::OceanbaseOracle),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("APP".into()),
                table_name: "FILES".into(),
                primary_keys: vec!["ID".into()],
                columns: Some(vec![column("ID", "NUMBER", false, None), column("CONTENT", "BLOB", true, None)]),
            },
            columns: vec!["ID".into(), "CONTENT".into()],
            source_columns: None,
            rows: vec![vec![json!(6), json!(original)]],
            dirty_rows: vec![(0, vec![(1, json!("0x00ff80"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });
        assert_eq!(prepared.validation_error, None);
        let sql = &prepared.statements[0];
        assert_eq!(sql.matches("DBMS_LOB.COMPARE").count(), 1);
        assert!(!sql.contains("DBMS_LOB.SUBSTR"));
        assert!(sql.contains("DBMS_LOB.COMPARE(\"CONTENT\", :dbx_lob_0) = 0"));
        assert!(sql.contains("DBMS_LOB.GETLENGTH(\"CONTENT\") = 1048583"));
        assert!(sql.contains("EXECUTE IMMEDIATE dbx_sql USING dbx_lob_0"));
        assert!(sql.contains("SQL%ROWCOUNT <> 1"));
        assert_eq!(sql.matches("FREETEMPORARY(dbx_lob_0)").count(), 2);
        let bound = &prepared.bound_statements[0];
        assert_eq!(bound.preview_sql, *sql);
        assert!(bound.sql.len() < 512);
        assert!(!bound.sql.contains("WRITEAPPEND"));
        assert!(bound.sql.contains("DBMS_LOB.COMPARE(\"CONTENT\", ?) = 0"));
        assert_eq!(bound.blob_parameters.len(), 1);
        assert_eq!(bound.blob_parameters[0].len(), 1048583 * 2);
    }

    #[test]
    fn oceanbase_blob_statement_keeps_literal_text_and_identifiers_opaque() {
        let hex = "abcd".repeat(1001);
        let sql = format!("UPDATE \"TO_BLOB(HEXTORAW('fake'))\" SET \"B\" = TO_BLOB(HEXTORAW('{hex}')), \"T\" = 'TO_BLOB(HEXTORAW(''{hex}''))' WHERE \"ID\" = 1");
        let statement = oceanbase_blob_statement(&sql, false).unwrap();
        assert_eq!(statement.matches("CREATETEMPORARY(").count(), 1);
        assert!(statement.contains("USING dbx_lob_0"));
        assert!(!statement.contains("dbx_lob_1"));
        assert!(!statement.contains("SQL%ROWCOUNT <> 1"));
        assert_eq!(oceanbase_blob_statement("UPDATE T SET B = TO_BLOB(HEXTORAW('00ff'))", false), None);
        assert_eq!(
            oceanbase_blob_statement(&format!("UPDATE T SET TXT = 'TO_BLOB(HEXTORAW(''{hex}''))'"), false),
            None
        );
    }

    #[test]
    fn oceanbase_bound_blobs_keep_new_and_original_values_in_sql_order() {
        let old_a = "11".repeat(2049);
        let old_b = "22".repeat(2050);
        let new_a = "33".repeat(2051);
        let new_b = "44".repeat(2052);
        let mut options = DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::OceanbaseOracle),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("APP".into()),
                table_name: "FILES".into(),
                primary_keys: vec!["ID".into()],
                columns: Some(vec![
                    column("ID", "NUMBER", false, None),
                    column("A", "BLOB", true, None),
                    column("B", "BLOB", true, None),
                ]),
            },
            columns: vec!["ID".into(), "A".into(), "B".into()],
            source_columns: None,
            rows: vec![vec![json!(1), json!(format!("0x{old_a}")), json!(format!("0x{old_b}"))]],
            dirty_rows: vec![(0, vec![(1, json!(format!("0x{new_a}"))), (2, json!(format!("0x{new_b}")))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        };
        let prepared = prepare_data_grid_save(options.clone());
        let bound = &prepared.bound_statements[0];
        assert_eq!(bound.blob_parameters, vec![new_a, new_b, old_a.clone(), old_b.clone()]);
        assert_eq!(bound.sql.matches('?').count(), 4);
        assert!(bound.sql.contains("SQL%ROWCOUNT <> 1"));
        for (value, literal) in [(Value::Null, "NULL"), (json!("0x"), "EMPTY_BLOB()")] {
            options.dirty_rows = vec![(0, vec![(1, value)])];
            let prepared = prepare_data_grid_save(options.clone());
            assert_eq!(prepared.bound_statements[0].blob_parameters, vec![old_a.clone()]);
            assert!(prepared.bound_statements[0].sql.contains(&format!("\"A\" = {literal}")));
        }
    }

    #[test]
    fn prepares_oceanbase_clob_guards_and_preserves_empty_lob() {
        let clob = column("BODY", "CLOB", true, None);
        let database_type = Some(DatabaseType::OceanbaseOracle);
        assert_eq!(format_grid_assignment_sql_literal(&json!(""), database_type, Some(&clob), None), "EMPTY_CLOB()");
        assert_eq!(
            build_column_predicate(database_type, "BODY", &json!(""), Some(&clob), true, None),
            "(DBMS_LOB.GETLENGTH(\"BODY\") = 0)"
        );
        assert_eq!(
            build_column_predicate(database_type, "BODY", &Value::Null, Some(&clob), true, None),
            "\"BODY\" IS NULL"
        );
        let text = format!("{}'尾", "😀".repeat(501));
        let predicate = build_column_predicate(database_type, "BODY", &json!(text), Some(&clob), true, None);
        assert!(predicate.contains("GETLENGTH(\"BODY\") = 503"));
        assert!(predicate.contains("SUBSTR(\"BODY\", 500, 1)"));
        assert!(predicate.contains("SUBSTR(\"BODY\", 3, 501)"));
        assert!(predicate.contains("😀''尾"));
        assert!(!predicate.contains("DBMS_LOB.COMPARE"));

        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type,
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("APP".into()),
                table_name: "DOCS".into(),
                primary_keys: vec!["ID".into()],
                columns: Some(vec![column("ID", "NUMBER", false, None), clob]),
            },
            columns: vec!["ID".into(), "BODY".into()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("original")]],
            dirty_rows: vec![(0, vec![(1, json!("edited"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });
        assert_eq!(result.validation_error, None);
        assert!(result.statements[0].contains("\"BODY\" = 'edited'"));
        assert!(result.statements[0].contains("UTL_RAW.CAST_TO_RAW('original')"));
        assert!(result.statements[0].contains("SQL%ROWCOUNT <> 1"));
        assert!(result.rollback_statements[0].contains("UTL_RAW.CAST_TO_RAW('edited')"));
    }

    #[test]
    fn prepares_oracle_clob_update_without_oversized_string_literals() {
        let clob = column("body", "CLOB", true, None);
        let large_value = "x".repeat(4205);
        let literal =
            format_grid_assignment_sql_literal(&json!(large_value), Some(DatabaseType::Oracle), Some(&clob), None);
        let chunks = literal
            .split("TO_CLOB('")
            .skip(1)
            .map(|chunk| chunk.split("')").next().unwrap_or_default())
            .collect::<Vec<_>>();

        assert_eq!(chunks.len(), 2);
        assert!(chunks.iter().all(|chunk| chunk.len() <= ORACLE_LOB_LITERAL_CHUNK_BYTES));
        assert_eq!(chunks[0].len(), ORACLE_LOB_LITERAL_CHUNK_BYTES);
        assert_eq!(chunks[1].len(), 4205 - ORACLE_LOB_LITERAL_CHUNK_BYTES);
        assert_eq!(
            format_grid_assignment_sql_literal(&json!("short"), Some(DatabaseType::Oracle), Some(&clob), None),
            "'short'"
        );
        let nclob = column("body", "NCLOB", true, None);
        assert!(format_grid_assignment_sql_literal(
            &json!(large_value),
            Some(DatabaseType::Oracle),
            Some(&nclob),
            None
        )
        .starts_with("TO_NCLOB('"));
        let special_value = format!("{}'\\", "x".repeat(3998));
        let special_literal =
            format_grid_assignment_sql_literal(&json!(special_value), Some(DatabaseType::Oracle), Some(&clob), None);
        assert!(special_literal.starts_with("TO_CLOB('"));
        assert!(special_literal.contains("''"));
        assert!(!special_literal.contains("\\\\"));
        let varchar = column("body", "VARCHAR2(5000)", true, None);
        let varchar_literal =
            format_grid_assignment_sql_literal(&json!(large_value), Some(DatabaseType::Oracle), Some(&varchar), None);
        assert!(varchar_literal.starts_with("'"));
        assert!(!varchar_literal.contains("TO_CLOB("));

        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Oracle),
            identifier_quote: Some("\"".to_string()),
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("APP".to_string()),
                table_name: "DOCUMENTS".to_string(),
                primary_keys: vec!["ID".to_string()],
                columns: Some(vec![column("ID", "NUMBER", false, None), clob]),
            },
            columns: vec!["ID".to_string(), "BODY".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("old")]],
            dirty_rows: vec![(0, vec![(1, json!(large_value))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements.len(), 1);
        assert!(result.statements[0].contains("\"BODY\" = TO_CLOB('"));
        assert!(result.statements[0].contains("WHERE \"ID\" = 1;"));
    }

    #[test]
    fn formats_numeric_string_literals_for_numeric_columns_without_quotes() {
        let number = column("amount", "NUMBER(20,0)", true, None);
        let text = column("code", "VARCHAR2(32)", true, None);

        assert_eq!(
            format_grid_sql_literal(&json!("12345678901234567890"), Some(DatabaseType::Oracle), Some(&number)),
            "12345678901234567890"
        );
        assert_eq!(
            format_grid_sql_literal(&json!("12345678901234567890"), Some(DatabaseType::Oracle), Some(&text)),
            "'12345678901234567890'"
        );
        assert_eq!(
            format_grid_sql_literal(&json!("123-not-a-number"), Some(DatabaseType::Oracle), Some(&number)),
            "'123-not-a-number'"
        );
    }

    #[test]
    fn prepares_oracle_number28_updates_from_serialized_query_results() {
        let identifiers = ["2026081810175800100000000000", "2026081810175800100000000001"];
        let rows: Vec<Vec<Value>> = identifiers
            .iter()
            .map(|identifier| vec![serde_json::from_str(identifier).unwrap(), json!("old")])
            .collect();
        let query_result: crate::types::QueryResult = serde_json::from_value(json!({
            "columns": ["ID", "NAME"],
            "column_types": ["NUMBER(28)", "VARCHAR2(20)"],
            "rows": rows,
            "affected_rows": 0,
            "execution_time_ms": 0,
        }))
        .unwrap();
        let wire = serde_json::to_string(&query_result).unwrap();
        let deserialized: crate::types::QueryResult = serde_json::from_str(&wire).unwrap();

        for (row, identifier) in deserialized.rows.iter().zip(identifiers) {
            assert_eq!(row[0], json!(identifier));
        }

        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Oracle),
            identifier_quote: Some("\"".to_string()),
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("APP".to_string()),
                table_name: "ITEMS".to_string(),
                primary_keys: vec!["ID".to_string()],
                columns: Some(vec![
                    column("ID", "NUMBER(28)", false, None),
                    column("NAME", "VARCHAR2(20)", true, None),
                ]),
            },
            columns: deserialized.columns,
            source_columns: None,
            rows: deserialized.rows,
            dirty_rows: vec![(0, vec![(1, json!("new"))]), (1, vec![(1, json!("new"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            identifiers
                .iter()
                .map(|identifier| format!("UPDATE \"APP\".\"ITEMS\" SET \"NAME\" = 'new' WHERE \"ID\" = {identifier};"))
                .collect::<Vec<_>>()
        );
    }

    #[test]
    fn prepares_sqlserver_bigint_update_from_numeric_string() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::SqlServer),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("dbo".to_string()),
                table_name: "users".to_string(),
                primary_keys: vec!["Id".to_string()],
                columns: Some(vec![column("Id", "int", false, None), column("UserId", "bigint", true, None)]),
            },
            columns: vec!["Id".to_string(), "UserId".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!(142189065666650_i64)]],
            dirty_rows: vec![(0, vec![(1, json!("144847503924137986"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements, vec!["UPDATE [dbo].[users] SET [UserId] = 144847503924137986 WHERE [Id] = 1;"]);
    }

    #[test]
    fn prepares_sqlserver_cross_database_update() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::SqlServer),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: Some("BarDB".to_string()),
                database: Some("BarDB".to_string()),
                schema: Some("dbo".to_string()),
                table_name: "TUser".to_string(),
                primary_keys: vec!["ID".to_string()],
                columns: Some(vec![column("ID", "int", false, None), column("UserId", "bigint", false, None)]),
            },
            columns: vec!["ID".to_string(), "UserId".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!(10279)]],
            dirty_rows: vec![(0, vec![(1, json!(10280))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements, vec!["UPDATE [BarDB].[dbo].[TUser] SET [UserId] = 10280 WHERE [ID] = 1;"]);
        assert_eq!(
            result.rollback_statements,
            vec!["UPDATE [BarDB].[dbo].[TUser] SET [UserId] = 10279 WHERE [ID] = 1 AND [UserId] = 10280;"]
        );
    }

    #[test]
    fn prepares_kingbase_update_when_source_primary_key_case_differs() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Kingbase),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("ltcins_qd_db".to_string()),
                table_name: "KG07".to_string(),
                primary_keys: vec!["CKG023".to_string()],
                columns: Some(vec![
                    column("CKG023", "varchar", false, None),
                    column("CKG096", "character", true, None),
                ]),
            },
            columns: vec!["ckg023".to_string(), "CKG096".to_string()],
            source_columns: Some(vec![Some("ckg023".to_string()), Some("CKG096".to_string())]),
            rows: vec![vec![json!("2026071511071859"), json!("03")]],
            dirty_rows: vec![(0, vec![(1, json!("02"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec![r#"UPDATE "ltcins_qd_db"."KG07" SET "CKG096" = '02' WHERE "CKG023" = '2026071511071859';"#]
        );
        assert_eq!(
            result.rollback_statements,
            vec![
                r#"UPDATE "ltcins_qd_db"."KG07" SET "CKG096" = '03' WHERE "CKG023" = '2026071511071859' AND "CKG096" = '02';"#
            ]
        );
    }

    #[test]
    fn rejects_existing_row_save_when_primary_key_is_missing() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Kingbase),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("ltcins_qd_db".to_string()),
                table_name: "KG07".to_string(),
                primary_keys: vec!["CKG023".to_string()],
                columns: Some(vec![
                    column("CKG023", "varchar", false, None),
                    column("CKG096", "character", true, None),
                ]),
            },
            columns: vec!["CKG096".to_string()],
            source_columns: Some(vec![Some("CKG096".to_string())]),
            rows: vec![vec![json!("03")]],
            dirty_rows: vec![(0, vec![(0, json!("02"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(
            result.validation_error.as_deref(),
            Some(
                "Cannot safely update or delete rows because the query result does not include every primary key column (missing: CKG023). Refresh or rerun the query before saving."
            )
        );
        assert!(result.statements.is_empty());
        assert!(result.rollback_statements.is_empty());
    }

    #[test]
    fn rejects_existing_row_save_when_primary_key_value_is_null() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Kingbase),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("ltcins_qd_db".to_string()),
                table_name: "KG07".to_string(),
                primary_keys: vec!["CKG023".to_string()],
                columns: Some(vec![
                    column("CKG023", "varchar", false, None),
                    column("CKG096", "character", true, None),
                ]),
            },
            columns: vec!["CKG023".to_string(), "CKG096".to_string()],
            source_columns: None,
            rows: vec![vec![Value::Null, json!("03")]],
            dirty_rows: vec![(0, vec![(1, json!("02"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(
            result.validation_error.as_deref(),
            Some(
                "Cannot safely update or delete rows because primary key column \"CKG023\" has no value in the query result. Refresh or rerun the query before saving."
            )
        );
        assert!(result.statements.is_empty());
        assert!(result.rollback_statements.is_empty());
    }

    #[test]
    fn mysql_grouped_result_update_uses_physical_columns_and_primary_key() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("app".to_string()),
                table_name: "users".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![column("id", "bigint", false, None), column("department", "varchar", false, None)]),
            },
            columns: vec!["用户ID".to_string(), "部门".to_string(), "订单数".to_string()],
            source_columns: Some(vec![Some("id".to_string()), Some("department".to_string()), None]),
            rows: vec![vec![json!(7), json!("sales"), json!(3)]],
            dirty_rows: vec![(0, vec![(1, json!("support"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements, vec!["UPDATE `app`.`users` SET `department` = 'support' WHERE `id` = 7;"]);
        assert_eq!(
            result.rollback_statements,
            vec!["UPDATE `app`.`users` SET `department` = 'sales' WHERE `id` = 7 AND BINARY `department` = 'support';"]
        );
    }

    #[test]
    fn mysql_update_omits_virtual_and_stored_generated_columns() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("app".to_string()),
                table_name: "users".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![
                    column("id", "bigint", false, None),
                    column("virtual_label", "varchar", true, Some("VIRTUAL GENERATED")),
                    column("stored_label", "varchar", true, Some("STORED GENERATED")),
                    column("department", "varchar", false, None),
                ]),
            },
            columns: vec![
                "id".to_string(),
                "virtual_label".to_string(),
                "stored_label".to_string(),
                "department".to_string(),
            ],
            source_columns: Some(vec![
                Some("id".to_string()),
                Some("virtual_label".to_string()),
                Some("stored_label".to_string()),
                Some("department".to_string()),
            ]),
            rows: vec![vec![json!(7), json!("sales-7"), json!("SALES"), json!("sales")]],
            dirty_rows: vec![(0, vec![(1, json!("support-7")), (2, json!("SUPPORT")), (3, json!("support"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements, vec!["UPDATE `app`.`users` SET `department` = 'support' WHERE `id` = 7;"]);
        assert_eq!(
            result.rollback_statements,
            vec!["UPDATE `app`.`users` SET `department` = 'sales' WHERE `id` = 7 AND BINARY `department` = 'support';"]
        );
    }

    #[test]
    fn kingbase_column_index_prefers_exact_case_and_rejects_ambiguous_fallback() {
        let columns = vec![Some("ckg023".to_string()), Some("CKG023".to_string())];

        assert_eq!(find_column_index(Some(DatabaseType::Kingbase), &columns, "CKG023"), Some(1));
        assert_eq!(find_column_index(Some(DatabaseType::Kingbase), &columns[..1], "CKG023"), Some(0));
        assert_eq!(
            find_column_index(
                Some(DatabaseType::Kingbase),
                &[Some("ckg023".to_string()), Some("Ckg023".to_string())],
                "CKG023"
            ),
            None
        );
    }

    #[test]
    fn vastbase_column_index_prefers_exact_case_and_rejects_ambiguous_fallback() {
        let columns = vec![Some("id".to_string()), Some("ID".to_string())];

        assert_eq!(find_column_index(Some(DatabaseType::Vastbase), &columns, "ID"), Some(1));
        assert_eq!(find_column_index(Some(DatabaseType::Vastbase), &columns[..1], "ID"), Some(0));
        assert_eq!(
            find_column_index(Some(DatabaseType::Vastbase), &[Some("id".to_string()), Some("Id".to_string())], "ID"),
            None
        );
    }

    /// Vastbase reports `SELECT *` labels upper-cased while the primary-key
    /// metadata keeps the stored lower-case spelling (#8797). The grid badges
    /// the column as the primary key, so the backend has to resolve it through
    /// the same unique case-insensitive fallback instead of refusing the edit.
    #[test]
    fn vastbase_save_uses_unique_case_insensitive_primary_key_from_uppercased_labels() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Vastbase),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("app_support".to_string()),
                table_name: "auth_mobile_user".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![column("id", "varchar", false, None), column("third_id", "varchar", true, None)]),
            },
            columns: vec!["ID".to_string(), "THIRD_ID".to_string()],
            source_columns: Some(vec![Some("ID".to_string()), Some("THIRD_ID".to_string())]),
            rows: vec![vec![json!("207515959510335490"), json!("88271")]],
            dirty_rows: vec![(0, vec![(1, json!("88272"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec![
                "UPDATE \"app_support\".\"auth_mobile_user\" SET \"THIRD_ID\" = '88272' WHERE \"id\" = '207515959510335490';"
            ]
        );
    }

    #[test]
    fn rejects_vastbase_save_when_case_insensitive_primary_key_match_is_ambiguous() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Vastbase),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("app_support".to_string()),
                table_name: "auth_mobile_user".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![
                    column("id", "varchar", false, None),
                    column("ID", "varchar", false, None),
                    column("third_id", "varchar", true, None),
                ]),
            },
            columns: vec!["Id".to_string(), "iD".to_string(), "THIRD_ID".to_string()],
            source_columns: Some(vec![Some("Id".to_string()), Some("iD".to_string()), Some("THIRD_ID".to_string())]),
            rows: vec![vec![json!("1"), json!("2"), json!("88271")]],
            dirty_rows: vec![(0, vec![(2, json!("88272"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert!(result.validation_error.as_deref().is_some_and(|error| error.contains("missing: id")));
        assert!(result.statements.is_empty());
        assert!(result.rollback_statements.is_empty());
    }

    #[test]
    fn goldendb_column_index_prefers_exact_case_and_rejects_ambiguous_fallback() {
        let columns = vec![Some("id".to_string()), Some("ID".to_string())];

        assert_eq!(find_column_index(Some(DatabaseType::Goldendb), &columns, "ID"), Some(1));
        assert_eq!(find_column_index(Some(DatabaseType::Goldendb), &columns[..1], "ID"), Some(0));
        assert_eq!(
            find_column_index(Some(DatabaseType::Goldendb), &[Some("id".to_string()), Some("Id".to_string())], "ID"),
            None
        );
    }

    #[test]
    fn goldendb_save_uses_unique_case_insensitive_primary_key_for_update_and_delete() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Goldendb),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("app".to_string()),
                table_name: "people".to_string(),
                primary_keys: vec!["ID".to_string()],
                columns: Some(vec![column("ID", "bigint", false, None), column("name", "varchar", true, None)]),
            },
            columns: vec!["id".to_string(), "name".to_string()],
            source_columns: Some(vec![Some("id".to_string()), Some("name".to_string())]),
            rows: vec![vec![json!(1), json!("Ada")], vec![json!(2), json!("Grace")]],
            dirty_rows: vec![(0, vec![(1, json!("Ada Lovelace"))])],
            deleted_rows: vec![1],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec![
                "UPDATE `app`.`people` SET `name` = 'Ada Lovelace' WHERE `ID` = 1;",
                "DELETE FROM `app`.`people` WHERE `ID` = 2;",
            ]
        );
    }

    #[test]
    fn goldendb_save_supports_composite_primary_keys_with_unique_case_insensitive_matches() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Goldendb),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("app".to_string()),
                table_name: "members".to_string(),
                primary_keys: vec!["TENANT_ID".to_string(), "USER_ID".to_string()],
                columns: Some(vec![
                    column("TENANT_ID", "bigint", false, None),
                    column("USER_ID", "bigint", false, None),
                    column("name", "varchar", true, None),
                ]),
            },
            columns: vec!["tenant_id".to_string(), "user_id".to_string(), "name".to_string()],
            source_columns: Some(vec![
                Some("tenant_id".to_string()),
                Some("user_id".to_string()),
                Some("name".to_string()),
            ]),
            rows: vec![vec![json!(10), json!(1), json!("Ada")], vec![json!(10), json!(2), json!("Grace")]],
            dirty_rows: vec![(0, vec![(2, json!("Ada Lovelace"))])],
            deleted_rows: vec![1],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec![
                "UPDATE `app`.`members` SET `name` = 'Ada Lovelace' WHERE `TENANT_ID` = 10 AND `USER_ID` = 1;",
                "DELETE FROM `app`.`members` WHERE `TENANT_ID` = 10 AND `USER_ID` = 2;",
            ]
        );
    }

    #[test]
    fn rejects_goldendb_save_when_case_insensitive_primary_key_match_is_ambiguous() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Goldendb),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("app".to_string()),
                table_name: "people".to_string(),
                primary_keys: vec!["ID".to_string()],
                columns: Some(vec![column("ID", "bigint", false, None), column("name", "varchar", true, None)]),
            },
            columns: vec!["id".to_string(), "Id".to_string(), "name".to_string()],
            source_columns: Some(vec![Some("id".to_string()), Some("Id".to_string()), Some("name".to_string())]),
            rows: vec![vec![json!(1), json!(2), json!("Ada")]],
            dirty_rows: vec![(0, vec![(2, json!("Grace"))])],
            deleted_rows: vec![0],
            new_rows: vec![],
            include_database_name: false,
        });

        assert!(result.validation_error.as_deref().is_some_and(|error| error.contains("missing: ID")));
        assert!(result.statements.is_empty());
        assert!(result.rollback_statements.is_empty());
    }

    #[test]
    fn rejects_goldendb_save_when_primary_key_column_is_truly_missing() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Goldendb),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("app".to_string()),
                table_name: "people".to_string(),
                primary_keys: vec!["ID".to_string()],
                columns: Some(vec![column("ID", "bigint", false, None), column("name", "varchar", true, None)]),
            },
            columns: vec!["tenant_id".to_string(), "name".to_string()],
            source_columns: Some(vec![Some("tenant_id".to_string()), Some("name".to_string())]),
            rows: vec![vec![json!(10), json!("Ada")]],
            dirty_rows: vec![(0, vec![(1, json!("Grace"))])],
            deleted_rows: vec![0],
            new_rows: vec![],
            include_database_name: false,
        });

        assert!(result.validation_error.as_deref().is_some_and(|error| error.contains("missing: ID")));
        assert!(result.statements.is_empty());
        assert!(result.rollback_statements.is_empty());
    }

    #[test]
    fn rejects_postgres_save_when_only_case_different_column_is_returned() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Postgres),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("public".to_string()),
                table_name: "case_keys".to_string(),
                primary_keys: vec!["ID".to_string()],
                columns: Some(vec![
                    column("id", "integer", false, None),
                    column("ID", "integer", false, None),
                    column("name", "text", true, None),
                ]),
            },
            columns: vec!["id".to_string(), "name".to_string()],
            source_columns: Some(vec![Some("id".to_string()), Some("name".to_string())]),
            rows: vec![vec![json!(1), json!("Ada")]],
            dirty_rows: vec![(0, vec![(1, json!("Grace"))])],
            deleted_rows: vec![0],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(
            result.validation_error.as_deref(),
            Some(
                "Cannot safely update or delete rows because the query result does not include every primary key column (missing: ID). Refresh or rerun the query before saving."
            )
        );
        assert!(result.statements.is_empty());
        assert!(result.rollback_statements.is_empty());
    }

    #[test]
    fn rejects_kingbase_save_when_case_only_primary_key_match_is_ambiguous() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Kingbase),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("public".to_string()),
                table_name: "case_keys".to_string(),
                primary_keys: vec!["CKG023".to_string()],
                columns: Some(vec![column("CKG023", "varchar", false, None), column("name", "varchar", true, None)]),
            },
            columns: vec!["ckg023".to_string(), "Ckg023".to_string(), "name".to_string()],
            source_columns: Some(vec![
                Some("ckg023".to_string()),
                Some("Ckg023".to_string()),
                Some("name".to_string()),
            ]),
            rows: vec![vec![json!("first"), json!("second"), json!("Ada")]],
            dirty_rows: vec![(0, vec![(2, json!("Grace"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert!(result.validation_error.as_deref().is_some_and(|error| error.contains("missing: CKG023")));
        assert!(result.statements.is_empty());
        assert!(result.rollback_statements.is_empty());
    }

    #[test]
    fn kingbase_mysql_compat_save_uses_connection_identifier_quote() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Kingbase),
            identifier_quote: Some("`".to_string()),
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("gc".to_string()),
                table_name: "docfileinfo".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![column("id", "integer", false, None), column("file_name", "varchar", false, None)]),
            },
            columns: vec!["id".to_string(), "file_name".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("old")]],
            dirty_rows: vec![(0, vec![(1, json!("34-B-0048"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements, vec!["UPDATE `gc`.`docfileinfo` SET `file_name` = '34-B-0048' WHERE `id` = 1;"]);
        assert!(result
            .rollback_statements
            .iter()
            .all(|statement| statement.contains("`gc`.`docfileinfo`") && !statement.contains('"')));
    }

    /// Spanner's dialect is fixed at database creation time and only the connected agent
    /// knows it: GoogleSQL quotes with backticks (and has an empty default schema),
    /// the PostgreSQL dialect quotes with double quotes and defaults to `public`.
    #[test]
    fn spanner_save_uses_connection_identifier_quote_per_dialect() {
        let googlesql = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Spanner),
            identifier_quote: Some("`".to_string()),
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some(String::new()),
                table_name: "singers".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![column("id", "INT64", false, None), column("name", "STRING(100)", false, None)]),
            },
            columns: vec!["id".to_string(), "name".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("old")]],
            dirty_rows: vec![(0, vec![(1, json!("Ada"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(googlesql.validation_error, None);
        // Single-segment table name: `` ``.`singers` `` would be `Invalid empty identifier`.
        assert_eq!(googlesql.statements, vec!["UPDATE `singers` SET `name` = 'Ada' WHERE `id` = 1;"]);

        let postgres_dialect = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Spanner),
            identifier_quote: Some("\"".to_string()),
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("public".to_string()),
                table_name: "singers".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![
                    column("id", "bigint", false, None),
                    column("name", "character varying", false, None),
                ]),
            },
            columns: vec!["id".to_string(), "name".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("old")]],
            dirty_rows: vec![(0, vec![(1, json!("Ada"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(postgres_dialect.validation_error, None);
        assert_eq!(
            postgres_dialect.statements,
            vec!["UPDATE \"public\".\"singers\" SET \"name\" = 'Ada' WHERE \"id\" = 1;"]
        );

        // No quote reported: fall back to the GoogleSQL default (backticks), never to the
        // ANSI double quote, which GoogleSQL parses as a string literal.
        let no_reported_quote = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Spanner),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some(String::new()),
                table_name: "singers".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![column("id", "INT64", false, None), column("name", "STRING(100)", false, None)]),
            },
            columns: vec!["id".to_string(), "name".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("old")]],
            dirty_rows: vec![(0, vec![(1, json!("Ada"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(no_reported_quote.validation_error, None);
        assert_eq!(no_reported_quote.statements, vec!["UPDATE `singers` SET `name` = 'Ada' WHERE `id` = 1;"]);
    }

    #[test]
    fn kingbase_mysql_compat_copy_insert_uses_backtick_identifiers() {
        let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
            database_type: Some(DatabaseType::Kingbase),
            identifier_quote: Some("`".to_string()),
            table_meta: Some(DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("audit-schema".to_string()),
                table_name: "events".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: None,
            }),
            columns: vec!["id".to_string(), "event_type".to_string()],
            column_types: None,
            source_columns: None,
            rows: vec![vec![json!(1), json!("login")]],
            primary_key_mode: DataGridPrimaryKeyMode::Include,
            include_computed_columns: false,
            include_database_name: true,
            insert_mode: DataGridCopyInsertMode::Merged,
        });

        assert_eq!(
            statement.as_deref(),
            Some("INSERT INTO `audit-schema`.`events` (`id`, `event_type`) VALUES (1, 'login');")
        );
        assert!(!statement.as_deref().unwrap_or_default().contains('"'));
    }

    #[test]
    fn kingbase_mysql_compat_copy_update_uses_backtick_identifiers() {
        let statements = build_data_grid_copy_update_statements(DataGridCopyUpdateStatementOptions {
            database_type: Some(DatabaseType::Kingbase),
            identifier_quote: Some("`".to_string()),
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("audit-schema".to_string()),
                table_name: "events".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: None,
            },
            columns: vec!["id".to_string(), "event_type".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("logout")]],
            include_database_name: false,
        });

        assert_eq!(statements, vec!["UPDATE `audit-schema`.`events` SET `event_type` = 'logout' WHERE `id` = 1;"]);
    }

    #[test]
    fn vastbase_query_result_writes_use_resolved_search_path_schema() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Vastbase),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: Some("smes_dev".to_string()),
                schema: Some("tenant_b".to_string()),
                table_name: "TBLCUSPOSTMATERIALLOG".to_string(),
                primary_keys: vec![],
                columns: Some(vec![column("MONO", "varchar", true, None), column("ID", "bigint", false, None)]),
            },
            columns: vec!["MONO".to_string(), "ID".to_string()],
            source_columns: None,
            rows: vec![vec![json!("mono"), json!(461936049002042_i64)]],
            dirty_rows: vec![(0, vec![(0, json!("LY-SC01-260800002"))])],
            deleted_rows: vec![],
            new_rows: vec![vec![json!("dbx-insert-check"), json!(461936049002043_i64)]],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.execution_schema.as_deref(), Some("tenant_b"));
        assert_eq!(
            result.statements,
            vec![
                "UPDATE \"tenant_b\".\"TBLCUSPOSTMATERIALLOG\" SET \"MONO\" = 'LY-SC01-260800002' WHERE \"MONO\" = 'mono' AND \"ID\" = 461936049002042;",
                "INSERT INTO \"tenant_b\".\"TBLCUSPOSTMATERIALLOG\" (\"MONO\", \"ID\") VALUES ('dbx-insert-check', 461936049002043);",
            ]
        );
    }

    #[test]
    fn gbase8s_save_omits_owner_for_insert_update_delete_and_rollback() {
        let result = prepare_data_grid_save_for_driver_profile(
            DataGridSaveStatementOptions {
                database_type: Some(DatabaseType::Informix),
                identifier_quote: Some(String::new()),
                server_version: None,
                table_meta: DataGridTableMeta {
                    catalog: None,
                    database: Some("webcenter".to_string()),
                    schema: Some("gbasedbt".to_string()),
                    table_name: "user_device".to_string(),
                    primary_keys: vec!["id".to_string()],
                    columns: Some(vec![column("id", "integer", false, None), column("dev_id", "varchar", false, None)]),
                },
                columns: vec!["id".to_string(), "dev_id".to_string()],
                source_columns: None,
                rows: vec![vec![json!(1), json!("1")], vec![json!(2), json!("deleted")]],
                dirty_rows: vec![(0, vec![(1, json!("2"))])],
                deleted_rows: vec![1],
                new_rows: vec![vec![json!(3), json!("new")]],
                include_database_name: false,
            },
            Some("GBASE8S"),
        );

        assert_eq!(result.validation_error, None);
        assert_eq!(result.execution_schema, None);
        assert_eq!(
            result.statements,
            vec![
                "UPDATE user_device SET dev_id = '2' WHERE id = 1;",
                "DELETE FROM user_device WHERE id = 2;",
                "INSERT INTO user_device (id, dev_id) VALUES (3, 'new');",
            ]
        );
        assert_eq!(
            result.rollback_statements,
            vec![
                "DELETE FROM user_device WHERE id = 3 AND dev_id = 'new';",
                "INSERT INTO user_device (id, dev_id) VALUES (2, 'deleted');",
                "UPDATE user_device SET dev_id = '1' WHERE id = 1 AND dev_id = '2';",
            ]
        );
    }

    #[test]
    fn standard_informix_save_preserves_owner_qualification_without_gbase8s_profile() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Informix),
            identifier_quote: Some(String::new()),
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: Some("dbx_test".to_string()),
                schema: Some("gbasedbt".to_string()),
                table_name: "connection_smoke".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![column("id", "integer", false, None), column("product", "varchar", false, None)]),
            },
            columns: vec!["id".to_string(), "product".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("GBase 8s")]],
            dirty_rows: vec![(0, vec![(1, json!("GBase 8s updated"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec!["UPDATE gbasedbt.connection_smoke SET product = 'GBase 8s updated' WHERE id = 1;"]
        );
        assert_eq!(
            result.rollback_statements,
            vec![
                "UPDATE gbasedbt.connection_smoke SET product = 'GBase 8s' WHERE id = 1 AND product = 'GBase 8s updated';"
            ]
        );
    }

    #[test]
    fn gaussdb_jdbc_save_selectively_quotes_identifiers() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Gaussdb),
            identifier_quote: Some("\"".to_string()),
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("schema_01".to_string()),
                table_name: "table_01".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![column("id", "integer", false, None), column("name", "varchar", false, None)]),
            },
            columns: vec!["id".to_string(), "name".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("old")]],
            dirty_rows: vec![(0, vec![(1, json!("new"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements, vec!["UPDATE schema_01.table_01 SET name = 'new' WHERE id = 1;"]);
    }

    #[test]
    fn postgres_driver_to_gaussdb_m_mode_save_uses_backticks() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Postgres),
            identifier_quote: Some("`".to_string()),
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("App Schema".to_string()),
                table_name: "order".to_string(),
                primary_keys: vec!["ID".to_string()],
                columns: Some(vec![
                    column("ID", "integer", false, None),
                    column("display name", "varchar", false, None),
                ]),
            },
            columns: vec!["ID".to_string(), "display name".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("old")]],
            dirty_rows: vec![(0, vec![(1, json!("new"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements, vec!["UPDATE `App Schema`.`order` SET `display name` = 'new' WHERE `ID` = 1;"]);
    }

    #[test]
    fn postgres_save_uses_exact_quoted_primary_key_for_update_delete_and_rollback() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Postgres),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("public".to_string()),
                table_name: "case_keys".to_string(),
                primary_keys: vec!["ID".to_string()],
                columns: Some(vec![
                    column("id", "integer", false, None),
                    column("ID", "integer", false, None),
                    column("name", "text", true, None),
                ]),
            },
            columns: vec!["id".to_string(), "ID".to_string(), "name".to_string()],
            source_columns: Some(vec![Some("id".to_string()), Some("ID".to_string()), Some("name".to_string())]),
            rows: vec![vec![json!(1), json!(101), json!("Ada")], vec![json!(2), json!(202), json!("Grace")]],
            dirty_rows: vec![(0, vec![(2, json!("Ada Lovelace"))])],
            deleted_rows: vec![1],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec![
                r#"UPDATE "public"."case_keys" SET "name" = 'Ada Lovelace' WHERE "ID" = 101;"#,
                r#"DELETE FROM "public"."case_keys" WHERE "ID" = 202;"#,
            ]
        );
        assert!(result.rollback_statements.iter().all(|statement| !statement.contains(r#"WHERE "ID" = 1 AND"#)));
        assert!(result.rollback_statements.iter().any(|statement| statement.contains(r#"WHERE "ID" = 101"#)));
    }

    #[test]
    fn mysql_join_result_delete_targets_only_the_resolved_source_primary_key() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("lims".to_string()),
                table_name: "lims_batchs_simple".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![
                    column("id", "bigint", false, None),
                    column("sno", "integer", true, None),
                    column("batchs_id", "bigint", false, None),
                    column("simple_id", "bigint", false, None),
                ]),
            },
            columns: vec!["id".to_string(), "sno".to_string(), "batchs_id".to_string(), "simple_id".to_string()],
            source_columns: Some(vec![
                Some("id".to_string()),
                Some("sno".to_string()),
                Some("batchs_id".to_string()),
                Some("simple_id".to_string()),
            ]),
            rows: vec![vec![json!(2658055), json!(4), json!(57485), json!(492045)]],
            dirty_rows: vec![],
            deleted_rows: vec![0],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements, vec!["DELETE FROM `lims`.`lims_batchs_simple` WHERE `id` = 2658055;"]);
    }

    #[test]
    fn prepares_oracle_timestamp_insert_from_iso_grid_value() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Oracle),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("APP".to_string()),
                table_name: "EVENTS".to_string(),
                primary_keys: vec!["ID".to_string()],
                columns: Some(vec![
                    column("ID", "NUMBER", false, None),
                    column("CREATED_AT", "TIMESTAMP(6)", true, None),
                ]),
            },
            columns: vec!["ID".to_string(), "CREATED_AT".to_string()],
            source_columns: None,
            rows: vec![],
            dirty_rows: vec![],
            deleted_rows: vec![],
            new_rows: vec![vec![json!(1), json!("2022-08-25T09:58:43Z")]],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec![
                "INSERT INTO \"APP\".\"EVENTS\" (\"ID\", \"CREATED_AT\") VALUES (1, TO_TIMESTAMP('2022-08-25 09:58:43', 'YYYY-MM-DD HH24:MI:SS'));"
            ]
        );
    }

    #[test]
    fn prepares_oracle_raw_update_with_hex_literals() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Oracle),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("APP".to_string()),
                table_name: "RAW_VALUES".to_string(),
                primary_keys: vec!["ID".to_string()],
                columns: Some(vec![column("ID", "RAW(16)", false, None), column("PAYLOAD", "RAW(16)", true, None)]),
            },
            columns: vec!["ID".to_string(), "PAYLOAD".to_string()],
            source_columns: None,
            rows: vec![vec![json!("0x00112233445566778899aabbccddeeff"), json!("0xaabb")]],
            dirty_rows: vec![(0, vec![(1, json!("0xccdd"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec![
                "UPDATE \"APP\".\"RAW_VALUES\" SET \"PAYLOAD\" = HEXTORAW('ccdd') WHERE \"ID\" = HEXTORAW('00112233445566778899aabbccddeeff');"
            ]
        );
    }

    #[test]
    fn prepares_oracle_update_without_schema_when_the_frontend_resolved_the_current_schema() {
        // A JDBC Oracle edit resolves the login user's schema on the frontend and
        // sends the folded table name with an empty schema. The generated UPDATE
        // must stay unqualified instead of reintroducing the service name.
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Oracle),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "IMP_T".to_string(),
                primary_keys: vec!["ID".to_string()],
                columns: Some(vec![column("ID", "NUMBER", false, None), column("NAME", "VARCHAR2(100)", true, None)]),
            },
            columns: vec!["ID".to_string(), "NAME".to_string()],
            source_columns: None,
            rows: vec![vec![json!(7), json!("old")]],
            dirty_rows: vec![(0, vec![(1, json!("new"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements, vec!["UPDATE \"IMP_T\" SET \"NAME\" = 'new' WHERE \"ID\" = 7;"]);
    }

    #[test]
    fn oracle_raw_literals_require_valid_even_length_hex() {
        let raw = column("ID", "RAW(16)", false, None);
        let text = column("ID", "VARCHAR2(64)", false, None);
        let blob = column("ID", "BLOB", false, None);

        for database_type in [DatabaseType::Oracle, DatabaseType::OceanbaseOracle] {
            assert_eq!(format_grid_sql_literal(&json!("0x00aB"), Some(database_type), Some(&raw)), "HEXTORAW('00aB')");
            assert_eq!(format_grid_sql_literal(&json!("0xabc"), Some(database_type), Some(&raw)), "'0xabc'");
            assert_eq!(format_grid_sql_literal(&json!("0x00gg"), Some(database_type), Some(&raw)), "'0x00gg'");
            assert_eq!(format_grid_sql_literal(&json!("0x00ab"), Some(database_type), Some(&text)), "'0x00ab'");
            assert_eq!(format_grid_sql_literal(&json!("0x00ab"), Some(database_type), Some(&blob)), "'0x00ab'");
        }
    }

    #[test]
    fn prepares_oceanbase_oracle_lob_deletes_with_synthetic_rowid() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::OceanbaseOracle),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("APP".to_string()),
                table_name: "DATA_REPORT_SUB_TASK".to_string(),
                primary_keys: vec![DBX_ROWID_COLUMN.to_string()],
                columns: Some(vec![
                    column(DBX_ROWID_COLUMN, "VARCHAR2", false, None),
                    column("ID", "VARCHAR2(100)", false, None),
                    column("SMC_RESPONSE", "CLOB", true, None),
                    column("RAW_PAYLOAD", "BLOB", true, None),
                    column("ARCHIVE_VALUE", "LOB", true, None),
                ]),
            },
            columns: vec![
                DBX_ROWID_COLUMN.to_string(),
                "ID".to_string(),
                "SMC_RESPONSE".to_string(),
                "RAW_PAYLOAD".to_string(),
                "ARCHIVE_VALUE".to_string(),
            ],
            source_columns: None,
            rows: vec![
                vec![
                    json!("*AAABk1AAEAAAAAgAAA"),
                    json!("task-1"),
                    json!("response"),
                    json!("0x0011"),
                    json!("archive"),
                ],
                vec![json!("*AAABk1AAEAAAAAgAAB"), json!("task-2"), Value::Null, Value::Null, Value::Null],
            ],
            dirty_rows: vec![],
            deleted_rows: vec![0, 1],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements.len(), 2);
        assert!(result.statements[0].contains("ROWIDTOCHAR(ROWID) = '*AAABk1AAEAAAAAgAAA'"));
        assert!(result.statements[0].contains("DBMS_LOB.GETLENGTH(\"SMC_RESPONSE\") = 8"));
        assert!(result.statements[0].contains("UTL_RAW.CAST_TO_RAW('response')"));
        assert!(result.statements[1].contains("ROWIDTOCHAR(ROWID) = '*AAABk1AAEAAAAAgAAB'"));
        assert!(result.statements[1].contains("\"SMC_RESPONSE\" IS NULL"));
        assert!(result.statements.iter().all(|statement| statement.contains("SQL%ROWCOUNT <> 1")));
    }

    #[test]
    fn prepares_oceanbase_oracle_lob_delete_with_declared_primary_key() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::OceanbaseOracle),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("APP".to_string()),
                table_name: "DOCUMENTS".to_string(),
                primary_keys: vec!["ID".to_string()],
                columns: Some(vec![
                    column("ID", "NUMBER", false, None),
                    column("TITLE", "VARCHAR2(100)", false, None),
                    column("BODY", "CLOB", true, None),
                    column("CONTENT", "BLOB", true, None),
                ]),
            },
            columns: vec!["ID".to_string(), "TITLE".to_string(), "BODY".to_string(), "CONTENT".to_string()],
            source_columns: None,
            rows: vec![vec![json!(42), json!("report"), json!("body"), Value::Null]],
            dirty_rows: vec![],
            deleted_rows: vec![0],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements.len(), 1);
        assert!(result.statements[0].contains("WHERE \"ID\" = 42 AND (DBMS_LOB.GETLENGTH(\"BODY\") = 4"));
        assert!(result.statements[0].contains("SQL%ROWCOUNT <> 1"));
    }

    #[test]
    fn rejects_oceanbase_oracle_keyless_lob_writes_without_rowid() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::OceanbaseOracle),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("APP".to_string()),
                table_name: "DOCUMENTS".to_string(),
                primary_keys: vec![],
                columns: Some(vec![column("TITLE", "VARCHAR2(100)", false, None), column("BODY", "CLOB", true, None)]),
            },
            columns: vec!["TITLE".to_string(), "BODY".to_string()],
            source_columns: None,
            rows: vec![vec![json!("duplicate title"), json!("unique body")]],
            dirty_rows: vec![],
            deleted_rows: vec![0],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(
            result.validation_error.as_deref(),
            Some("Cannot safely update or delete this Oracle-compatible row because the table has LOB columns but no primary key or ROWID identifier.")
        );
        assert!(result.statements.is_empty());
        assert!(result.rollback_statements.is_empty());
    }

    fn dameng_keyless_options(
        table_columns: Vec<DataGridColumnInfo>,
        columns: Vec<String>,
        row: Vec<Value>,
    ) -> DataGridSaveStatementOptions {
        DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Dameng),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("SYSDBA".to_string()),
                table_name: "T8819".to_string(),
                primary_keys: vec![],
                columns: Some(table_columns),
            },
            columns,
            source_columns: None,
            rows: vec![row],
            dirty_rows: vec![(0, vec![(1, json!("b"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        }
    }

    #[test]
    fn rejects_dameng_keyless_binary_lob_writes() {
        // Regression test for https://github.com/t8y2/dbx/issues/8819: Dameng
        // rejects `"B" = '0x0102'` with `Data type mismatch` (code -6105), so a
        // keyless row predicate cannot carry a non-NULL binary LOB column.
        let result = prepare_data_grid_save(dameng_keyless_options(
            vec![
                column("GID", "INT", true, None),
                column("NAME", "VARCHAR(10)", true, None),
                column("B", "BLOB", true, None),
            ],
            vec!["GID".to_string(), "NAME".to_string(), "B".to_string()],
            vec![json!(1), json!("a"), json!("0x0102")],
        ));

        assert_eq!(result.validation_error.as_deref(), Some(DAMENG_KEYLESS_BINARY_LOB_ERROR));
        assert!(result.statements.is_empty());
        assert!(result.rollback_statements.is_empty());
        assert!(result.keyless_guards.is_empty());
    }

    #[test]
    fn rejects_dameng_keyless_binary_lob_deletes() {
        let mut options = dameng_keyless_options(
            vec![column("GID", "INT", true, None), column("B", "BLOB", true, None)],
            vec!["GID".to_string(), "B".to_string()],
            vec![json!(1), json!("0x0102")],
        );
        options.dirty_rows = vec![];
        options.deleted_rows = vec![0];

        let result = prepare_data_grid_save(options);

        assert_eq!(result.validation_error.as_deref(), Some(DAMENG_KEYLESS_BINARY_LOB_ERROR));
        assert!(result.statements.is_empty());
        assert!(result.rollback_statements.is_empty());
    }

    #[test]
    fn rejects_dameng_keyless_spatial_lob_writes() {
        // The issue's column is a DMGEO2 spatial type, which Dbx receives as
        // `SYSGEO2.ST_GEOMETRY(...)` text and which the server also refuses to
        // compare. Spatial columns are BLOB-backed, so they are refused too.
        let result = prepare_data_grid_save(dameng_keyless_options(
            vec![
                column("GID", "INT", true, None),
                column("NAME", "VARCHAR(10)", true, None),
                column("GEOM", "SYSGEO2.ST_GEOMETRY", true, None),
            ],
            vec!["GID".to_string(), "NAME".to_string(), "GEOM".to_string()],
            vec![json!(1), json!("a"), json!("SYSGEO2.ST_GEOMETRY(dm.jdbc.driver.DmdbBlob@5f0f259e)")],
        ));

        assert_eq!(result.validation_error.as_deref(), Some(DAMENG_KEYLESS_BINARY_LOB_ERROR));
        assert!(result.statements.is_empty());
    }

    #[test]
    fn allows_dameng_keyless_writes_when_comparable_values_remain() {
        // Textual LOBs and NULL binary LOBs are comparable (`= 'text'`,
        // `IS NULL`), so keyless editing keeps working and only the columns
        // that really cannot be compared are refused.
        let result = prepare_data_grid_save(dameng_keyless_options(
            vec![
                column("GID", "INT", true, None),
                column("NAME", "VARCHAR(10)", true, None),
                column("BODY", "CLOB", true, None),
                column("B", "BLOB", true, None),
            ],
            vec!["GID".to_string(), "NAME".to_string(), "BODY".to_string(), "B".to_string()],
            vec![json!(1), json!("a"), json!("body"), Value::Null],
        ));

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec![
                r#"UPDATE "SYSDBA"."T8819" SET "NAME" = 'b' WHERE "GID" = 1 AND "NAME" = 'a' AND "BODY" = 'body' AND "B" IS NULL;"#
            ]
        );
        assert_guards_cover_statement_predicates(&result);
    }

    #[test]
    fn allows_dameng_binary_lob_writes_with_a_primary_key() {
        let mut options = dameng_keyless_options(
            vec![
                column("GID", "INT", false, None),
                column("NAME", "VARCHAR(10)", true, None),
                column("B", "BLOB", true, None),
            ],
            vec!["GID".to_string(), "NAME".to_string(), "B".to_string()],
            vec![json!(1), json!("a"), json!("0x0102")],
        );
        options.table_meta.primary_keys = vec!["GID".to_string()];

        assert_eq!(prepare_data_grid_save(options).validation_error, None);
    }

    #[test]
    fn allows_dameng_keyless_writes_that_do_not_load_the_binary_lob_column() {
        // A binary LOB column outside the result set never reaches the row
        // predicate, so it must not block the save.
        let mut options = dameng_keyless_options(
            vec![
                column("GID", "INT", true, None),
                column("NAME", "VARCHAR(10)", true, None),
                column("B", "BLOB", true, None),
            ],
            vec!["GID".to_string(), "NAME".to_string()],
            vec![json!(1), json!("a")],
        );
        options.dirty_rows = vec![(0, vec![(1, json!("b"))])];

        assert_eq!(prepare_data_grid_save(options).validation_error, None);
    }

    fn daily_stats_keyless_options() -> DataGridSaveStatementOptions {
        DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Sqlite),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "daily_stats".to_string(),
                primary_keys: vec![],
                columns: Some(vec![column("stat_date", "TEXT", false, None), column("period", "TEXT", true, None)]),
            },
            columns: vec!["stat_date".to_string(), "period".to_string()],
            source_columns: None,
            rows: vec![vec![json!("2026-09-07"), Value::Null], vec![json!("2026-09-07"), Value::Null]],
            dirty_rows: vec![(0, vec![(1, json!("早上"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        }
    }

    fn postgres_ctid_options() -> DataGridSaveStatementOptions {
        DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Postgres),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("public".to_string()),
                table_name: "duplicate_rows".to_string(),
                primary_keys: vec![],
                columns: Some(vec![column("id", "integer", false, None), column("value", "text", true, None)]),
            },
            columns: vec![DBX_ROWID_COLUMN.to_string(), "id".to_string(), "value".to_string()],
            source_columns: None,
            rows: vec![
                vec![json!("16384:(0,1):742"), json!(1), json!("same")],
                vec![json!("16384:(0,2):743"), json!(1), json!("same")],
            ],
            dirty_rows: vec![(0, vec![(2, json!("updated"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        }
    }

    /// The predicate a guard counts must be byte-identical to the one its
    /// statement carries, otherwise the safety decision is made against a
    /// different set of columns or values than the mutation actually uses.
    fn assert_guards_cover_statement_predicates(preparation: &DataGridSavePreparation) {
        for guard in &preparation.keyless_guards {
            assert_eq!(guard.max_matched_rows, 1);
            let predicate = guard
                .sql
                .split_once(" WHERE (")
                .and_then(|(_, rest)| rest.strip_suffix(')'))
                .unwrap_or_else(|| panic!("guard is not a counting query: {}", guard.sql));
            assert!(
                preparation.statements.iter().any(|statement| statement.contains(&format!("WHERE {predicate}"))),
                "no statement carries the guarded predicate {predicate:?}"
            );
        }
        for statement in &preparation.statements {
            let Some((_, predicate)) = statement.split_once(" WHERE ") else {
                continue;
            };
            let predicate = predicate.trim_end_matches(';');
            assert!(
                preparation.keyless_guards.iter().any(|guard| guard.sql.ends_with(&format!("WHERE ({predicate})"))),
                "predicate {predicate:?} is sent to the database without a guard"
            );
        }
    }

    #[test]
    fn guards_sqlite_keyless_update_with_a_server_side_row_count() {
        // Regression test for https://github.com/t8y2/dbx/issues/8321: a SQLite
        // table with no primary key, two rows inserted with only `stat_date`
        // filled in. Editing row 0's `period` must not silently also rewrite
        // row 1, which has identical values in every column. Whether a second
        // matching row exists cannot be answered from the loaded page, so the
        // save carries a guard that counts the matches on the server.
        let result = prepare_data_grid_save(daily_stats_keyless_options());

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec![
                r#"UPDATE "daily_stats" SET "period" = '早上' WHERE "stat_date" = '2026-09-07' AND "period" IS NULL;"#
            ]
        );
        assert_eq!(
            result.keyless_guards,
            vec![DataGridSaveGuard {
                sql: r#"SELECT COUNT(*) AS dbx_keyless_row_matches FROM "daily_stats" WHERE ("stat_date" = '2026-09-07' AND "period" IS NULL)"#
                    .to_string(),
                max_matched_rows: 1,
                min_matched_rows: 0,
                message: KEYLESS_AMBIGUOUS_ROW_ERROR.to_string(),
                min_message: None,
            }]
        );
        assert_guards_cover_statement_predicates(&result);
    }

    #[test]
    fn uses_postgres_tableoid_and_ctid_to_edit_one_duplicate_row() {
        let update = prepare_data_grid_save(postgres_ctid_options());

        assert_eq!(update.validation_error, None);
        assert_eq!(
            update.statements,
            vec![
                r#"UPDATE "public"."duplicate_rows" SET "value" = 'updated' WHERE tableoid = '16384'::oid AND ctid = '(0,1)'::tid AND xmin = '742'::xid;"#
            ]
        );
        assert_eq!(update.keyless_guards.len(), 1);
        assert_eq!(
            update.keyless_guards[0].sql,
            r#"SELECT COUNT(*) AS dbx_keyless_row_matches FROM "public"."duplicate_rows" WHERE (tableoid = '16384'::oid AND ctid = '(0,1)'::tid AND xmin = '742'::xid)"#
        );
        // A physical locator must still address one row: zero matches means the
        // row moved, which the frontend reports instead of "deleted successfully".
        assert_eq!(update.keyless_guards[0].min_matched_rows, 1);
        assert_eq!(update.keyless_guards[0].min_message.as_deref(), Some(POSTGRES_CTID_STALE_ROW_ERROR));
        assert_guards_cover_statement_predicates(&update);

        // CTID changes after UPDATE, so history rollback uses the new full-row
        // values and only proceeds if that predicate still identifies one row.
        assert_eq!(
            update.rollback_statements,
            vec![
                r#"UPDATE "public"."duplicate_rows" SET "value" = 'same' WHERE ("id" = 1 AND "value" = 'updated') AND (SELECT COUNT(*) FROM "public"."duplicate_rows" WHERE ("id" = 1 AND "value" = 'updated')) = 1;"#
            ]
        );

        let mut delete_options = postgres_ctid_options();
        delete_options.dirty_rows.clear();
        delete_options.deleted_rows = vec![0];
        let delete = prepare_data_grid_save(delete_options);
        assert_eq!(
            delete.statements,
            vec![
                r#"DELETE FROM "public"."duplicate_rows" WHERE tableoid = '16384'::oid AND ctid = '(0,1)'::tid AND xmin = '742'::xid;"#
            ]
        );
        assert_eq!(delete.keyless_guards.len(), 1);
        assert_eq!(delete.keyless_guards[0].min_matched_rows, 1);
        assert_eq!(delete.keyless_guards[0].min_message.as_deref(), Some(POSTGRES_CTID_STALE_ROW_ERROR));
        assert_guards_cover_statement_predicates(&delete);
    }

    #[test]
    fn rejects_invalid_postgres_ctid_before_building_mutations() {
        let mut options = postgres_ctid_options();
        options.rows[0][0] = json!("not-a-ctid");

        let result = prepare_data_grid_save(options);

        assert_eq!(result.validation_error.as_deref(), Some(POSTGRES_CTID_UNIDENTIFIABLE_ROW_ERROR));
        assert!(result.statements.is_empty());
        assert!(result.keyless_guards.is_empty());
    }

    #[test]
    fn guards_sqlite_keyless_update_even_when_the_loaded_page_looks_unique() {
        // The duplicate row may live outside the loaded/filtered page, so a
        // page that shows only distinguishable rows proves nothing and must
        // still be verified on the server.
        let mut options = daily_stats_keyless_options();
        options.rows = vec![vec![json!("2026-09-07"), json!("早上")], vec![json!("2026-09-07"), json!("中午")]];
        options.dirty_rows = vec![(0, vec![(1, json!("上午"))])];
        let result = prepare_data_grid_save(options);

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec![
                r#"UPDATE "daily_stats" SET "period" = '上午' WHERE "stat_date" = '2026-09-07' AND "period" = '早上';"#
            ]
        );
        assert_eq!(result.keyless_guards.len(), 1);
        assert_guards_cover_statement_predicates(&result);
    }

    #[test]
    fn guards_keyless_predicate_without_the_columns_the_predicate_omits() {
        // `build_row_where` drops result columns that have no source column, so
        // a guard built from the visible values would decide uniqueness using
        // `total`, a value the mutation predicate never mentions.
        let mut options = daily_stats_keyless_options();
        options.table_meta.columns =
            Some(vec![column("stat_date", "TEXT", false, None), column("period", "TEXT", true, None)]);
        options.columns = vec!["stat_date".to_string(), "period".to_string(), "total".to_string()];
        options.source_columns = Some(vec![Some("stat_date".to_string()), Some("period".to_string()), None]);
        options.rows =
            vec![vec![json!("2026-09-07"), Value::Null, json!(1)], vec![json!("2026-09-07"), Value::Null, json!(2)]];
        let result = prepare_data_grid_save(options);

        assert_eq!(result.validation_error, None);
        assert_eq!(result.keyless_guards.len(), 1);
        assert!(!result.keyless_guards[0].sql.contains("total"));
        assert_guards_cover_statement_predicates(&result);
    }

    #[test]
    fn guards_keyless_delete_and_deduplicates_identical_predicates() {
        let mut options = daily_stats_keyless_options();
        options.dirty_rows = vec![];
        options.deleted_rows = vec![0, 1];
        let result = prepare_data_grid_save(options);

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements.len(), 2);
        // Both rows produce the same predicate; one guard covers both.
        assert_eq!(result.keyless_guards.len(), 1);
        assert_guards_cover_statement_predicates(&result);
    }

    #[test]
    fn omits_keyless_guards_when_the_table_has_a_primary_key() {
        let mut options = daily_stats_keyless_options();
        options.table_meta.primary_keys = vec!["stat_date".to_string()];
        let result = prepare_data_grid_save(options);

        assert_eq!(result.validation_error, None);
        assert!(result.keyless_guards.is_empty());
    }

    #[test]
    fn rejects_keyless_edit_when_no_column_can_address_the_row() {
        // Every result column is computed, so `build_row_where` produces an
        // empty predicate: there is neither a row identifier nor anything a
        // server-side count could check, and the write must stay disabled.
        let mut options = daily_stats_keyless_options();
        options.source_columns = Some(vec![None, None]);
        let result = prepare_data_grid_save(options);

        assert_eq!(result.validation_error.as_deref(), Some(KEYLESS_UNIDENTIFIABLE_ROW_ERROR));
        assert!(result.statements.is_empty());
        assert!(result.keyless_guards.is_empty());
    }

    #[test]
    fn preserves_oceanbase_oracle_keyless_predicates_for_comparable_columns() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::OceanbaseOracle),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("APP".to_string()),
                table_name: "TASK_STATUS".to_string(),
                primary_keys: vec![],
                columns: Some(vec![
                    column("TASK_NAME", "VARCHAR2(100)", false, None),
                    column("STATUS", "VARCHAR2(16)", true, None),
                ]),
            },
            columns: vec!["TASK_NAME".to_string(), "STATUS".to_string()],
            source_columns: None,
            rows: vec![vec![json!("task-1"), json!("RUNNING")], vec![json!("task-2"), Value::Null]],
            dirty_rows: vec![],
            deleted_rows: vec![0, 1],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec![
                "DELETE FROM \"APP\".\"TASK_STATUS\" WHERE \"TASK_NAME\" = 'task-1' AND \"STATUS\" = 'RUNNING';",
                "DELETE FROM \"APP\".\"TASK_STATUS\" WHERE \"TASK_NAME\" = 'task-2' AND \"STATUS\" IS NULL;",
            ]
        );
    }

    /// A predicate the save already proved plain: the same `(column, literal)`
    /// pair `build_primary_key_predicate` produces, for direct batcher tests.
    fn mysql_plain_predicate(sql: &str, column: &str, literal: &str) -> MysqlKeyPredicate {
        MysqlKeyPredicate {
            sql: sql.to_string(),
            plain: Some(MysqlPlainEquality { column: column.to_string(), literal: literal.to_string() }),
        }
    }

    #[test]
    fn batches_mysql_updates_with_identical_values() {
        let mut options = mysql_people_save_options(3);
        options.dirty_rows =
            vec![(0, vec![(1, Value::Null)]), (1, vec![(1, Value::Null)]), (2, vec![(1, Value::Null)])];

        let result = prepare_data_grid_save(options);

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements, vec!["UPDATE `app`.`people` SET `status` = NULL WHERE `id` IN (1, 2, 3);"]);
        assert_eq!(result.rollback_statements.len(), 3);
    }

    #[test]
    fn keeps_single_dirty_mysql_row_as_plain_equality() {
        let mut options = mysql_people_save_options(1);
        options.dirty_rows = vec![(0, vec![(1, Value::Null)])];

        let result = prepare_data_grid_save(options);

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements, vec!["UPDATE `app`.`people` SET `status` = NULL WHERE `id` = 1;"]);
    }

    #[test]
    fn preserves_mysql_update_order_when_mergeable_and_unmergeable_predicates_mix() {
        // Renders past the save path: a NULL key row is rejected by
        // `validate_existing_row_primary_keys`, but the batcher itself must still
        // flush an open run in place instead of regrouping, so the order the save
        // built (IN-able, unmergeable, IN-able) survives.
        let mut statements = Vec::new();
        push_mysql_key_predicate_batches(
            &mut statements,
            "UPDATE `app`.`people` SET `id` = 10 WHERE ",
            vec![
                mysql_plain_predicate("`id` = 1", "`id`", "1"),
                MysqlKeyPredicate { sql: "`id` IS NULL".to_string(), plain: None },
                mysql_plain_predicate("`id` = 3", "`id`", "3"),
            ],
        );

        assert_eq!(
            statements,
            vec!["UPDATE `app`.`people` SET `id` = 10 WHERE (`id` = 1) OR (`id` IS NULL) OR (`id` = 3);"]
        );
    }

    #[test]
    fn keeps_null_mysql_key_predicates_out_of_the_in_run() {
        // A NULL key cannot be merged (`col IN (NULL)` never matches, unlike
        // `col = NULL`), so it stays its own part and never joins a neighbour run.
        let mut statements = Vec::new();
        push_mysql_key_predicate_batches(
            &mut statements,
            "UPDATE `app`.`people` SET `id` = 1 WHERE ",
            vec![
                mysql_plain_predicate("`id` = 1", "`id`", "1"),
                mysql_plain_predicate("`id` = 2", "`id`", "2"),
                MysqlKeyPredicate { sql: "`id` IS NULL".to_string(), plain: None },
                mysql_plain_predicate("`id` = 3", "`id`", "3"),
                mysql_plain_predicate("`id` = 4", "`id`", "4"),
            ],
        );

        assert_eq!(
            statements,
            vec!["UPDATE `app`.`people` SET `id` = 1 WHERE (`id` IN (1, 2)) OR (`id` IS NULL) OR (`id` IN (3, 4));"]
        );
    }

    #[test]
    fn merges_mysql_string_keys_containing_sql_keywords() {
        // The retired text parser rejected any literal containing " AND " / " OR "
        // / starting with "IS "; the structured check proves these are plain
        // equalities, so both paths merge them now.
        let mut options = mysql_people_save_options(0);
        options.table_meta.columns = Some(vec![column("id", "varchar(32)", false, None)]);
        options.columns = vec!["id".to_string()];
        options.table_meta.primary_keys = vec!["id".to_string()];
        options.rows = vec![vec![json!("a AND b")], vec![json!("c OR d")], vec![json!("IS x")]];
        options.dirty_rows =
            vec![(0, vec![(0, json!("new"))]), (1, vec![(0, json!("new"))]), (2, vec![(0, json!("new"))])];
        options.deleted_rows = vec![];

        let updates = prepare_data_grid_save(options.clone());

        assert_eq!(updates.validation_error, None);
        assert_eq!(
            updates.statements,
            vec!["UPDATE `app`.`people` SET `id` = 'new' WHERE `id` IN ('a AND b', 'c OR d', 'IS x');"]
        );

        let deletes = prepare_data_grid_save(DataGridSaveStatementOptions {
            dirty_rows: vec![],
            deleted_rows: vec![0, 1, 2],
            ..options
        });
        assert_eq!(deletes.validation_error, None);
        assert_eq!(deletes.statements, vec!["DELETE FROM `app`.`people` WHERE `id` IN ('a AND b', 'c OR d', 'IS x');"]);
    }

    #[test]
    fn merges_mysql_string_keys_with_quotes_and_backslashes() {
        let mut options = mysql_people_save_options(0);
        options.table_meta.columns = Some(vec![column("id", "varchar(32)", false, None)]);
        options.columns = vec!["id".to_string()];
        options.table_meta.primary_keys = vec!["id".to_string()];
        options.rows = vec![vec![json!("O'Reilly")], vec![json!("back\\slash")]];
        options.dirty_rows = vec![(0, vec![(0, json!("new"))]), (1, vec![(0, json!("new"))])];

        let result = prepare_data_grid_save(options);

        // The literals keep the formatter's escaping; only the predicate shape changed.
        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec!["UPDATE `app`.`people` SET `id` = 'new' WHERE `id` IN ('O''Reilly', 'back\\\\slash');"]
        );
    }

    #[test]
    fn keeps_null_mysql_key_predicates_out_of_the_in_run_after_a_rejected_save() {
        // A NULL key value never reaches the batcher: the save refuses the write
        // outright, which is also why `col IN (NULL)` cannot silently over-match.
        let mut options = mysql_people_save_options(0);
        options.table_meta.columns = Some(vec![column("id", "bigint", true, None)]);
        options.columns = vec!["id".to_string()];
        options.table_meta.primary_keys = vec!["id".to_string()];
        options.rows = vec![vec![json!(1)], vec![Value::Null], vec![json!(3)]];
        options.dirty_rows = vec![(0, vec![(0, json!(10))]), (1, vec![(0, json!(11))]), (2, vec![(0, json!(12))])];

        let result = prepare_data_grid_save(options);

        assert!(result.statements.is_empty());
        assert!(
            result.validation_error.unwrap_or_default().contains("has no value in the query result"),
            "a NULL key must be refused instead of merging into an IN list"
        );
    }

    #[test]
    fn keeps_guarded_mysql_predicates_out_of_the_in_run() {
        // A clause that carries an extra `AND` guard is not a plain equality even
        // when its first half looks like one; merging it would widen the batch.
        let mut statements = Vec::new();
        push_mysql_key_predicate_batches(
            &mut statements,
            "DELETE FROM `app`.`people` WHERE ",
            vec![
                mysql_plain_predicate("`id` = 1", "`id`", "1"),
                MysqlKeyPredicate { sql: "`id` = 2 AND `payload` = 'x'".to_string(), plain: None },
                mysql_plain_predicate("`id` = 3", "`id`", "3"),
            ],
        );

        assert_eq!(
            statements,
            vec!["DELETE FROM `app`.`people` WHERE (`id` = 1) OR (`id` = 2 AND `payload` = 'x') OR (`id` = 3);"]
        );
    }

    #[test]
    fn keeps_compound_key_mysql_updates_in_or_form() {
        let mut options = mysql_people_save_options(2);
        options.table_meta.primary_keys = vec!["id".to_string(), "status".to_string()];
        options.dirty_rows = vec![(0, vec![(1, json!("x"))]), (1, vec![(1, json!("x"))])];

        let result = prepare_data_grid_save(options);

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec![
                "UPDATE `app`.`people` SET `status` = 'x' WHERE (`id` = 1 AND `status` = 'active') OR (`id` = 2 AND `status` = 'active');"
            ]
        );
    }

    #[test]
    fn rejects_plain_report_when_the_rendered_clause_differs_from_the_pair() {
        let mysql = Some(DatabaseType::Mysql);
        // Sanity: the ordinary MySQL key is plain.
        let predicate = build_primary_key_predicate(
            mysql,
            &["id".to_string()],
            &[Some("id".to_string())],
            &[json!(1)],
            &[column("id", "int", false, None)],
            None,
        );
        assert_eq!(predicate.sql, "`id` = 1");
        assert!(predicate.plain.is_some());

        // MySQL JSON columns already wrap the literal (`CAST(... AS JSON)`). Any
        // future branch that renders another form must stop being mergeable,
        // because the pair no longer reproduces the clause.
        let json_predicate = build_primary_key_predicate(
            mysql,
            &["payload".to_string()],
            &[Some("payload".to_string())],
            &[json!(r#"{"a":1}"#)],
            &[column("payload", "json", false, None)],
            None,
        );
        assert!(json_predicate.sql.contains("CAST("), "{}", json_predicate.sql);
        assert!(json_predicate.plain.is_none(), "a wrapped literal must stay in the OR form");
    }

    #[test]
    fn plain_equality_check_ignores_outer_parentheses_only() {
        let plain = |column: &str, literal: &str| MysqlPlainEquality {
            column: column.to_string(),
            literal: literal.to_string(),
        };

        assert!(plain_equality_from_clause("`id` = 1", plain("`id`", "1")).is_some());
        assert!(plain_equality_from_clause("(`id` = 1)", plain("`id`", "1")).is_some());
        // An appended guard, a compound key or a different comparison must not pass.
        assert!(plain_equality_from_clause("`id` = 1 AND `status` = 'x'", plain("`id`", "1")).is_none());
        assert!(plain_equality_from_clause("BINARY `id` = 1", plain("`id`", "1")).is_none());
        assert!(plain_equality_from_clause("`id` IS NULL", plain("`id`", "1")).is_none());
        assert!(plain_equality_from_clause("`id` = CAST('1' AS JSON)", plain("`id`", "1")).is_none());
        // A bare NULL keyword is never merged, matching the retired parser.
        assert!(plain_equality_from_clause("`id` = NULL", plain("`id`", "NULL")).is_none());
        assert!(plain_equality_from_clause("`id` = null", plain("`id`", "null")).is_none());
    }

    #[test]
    fn splits_mysql_batches_above_the_value_budget() {
        let row_count = MYSQL_DATA_GRID_BATCH_MAX_VALUES + 1;
        let mut options = mysql_people_save_options(row_count);
        options.dirty_rows = (0..row_count).map(|index| (index, vec![(1, json!("archived"))])).collect();

        let updates = prepare_data_grid_save(options.clone());

        assert_eq!(updates.validation_error, None);
        assert_eq!(updates.statements.len(), 2);
        // The first statement holds exactly the value budget, so the last value
        // it carries is the budget itself and the leftover opens the next one.
        assert!(updates.statements[0].contains("`id` IN ("));
        assert!(updates.statements[0].ends_with(&format!("{});", MYSQL_DATA_GRID_BATCH_MAX_VALUES)));
        assert!(!updates.statements[0].contains(&format!(", {row_count})")));
        assert!(updates.statements[1].ends_with(&format!("WHERE `id` = {row_count};")));

        // The same budget applies to DELETE, which already emits IN lists today.
        let deletes = prepare_data_grid_save(DataGridSaveStatementOptions {
            dirty_rows: vec![],
            deleted_rows: (0..row_count).collect(),
            ..options
        });

        assert_eq!(deletes.validation_error, None);
        assert_eq!(deletes.statements.len(), 2);
        assert!(deletes.statements[0].ends_with(&format!("{});", MYSQL_DATA_GRID_BATCH_MAX_VALUES)));
        assert!(deletes.statements[1].ends_with(&format!("WHERE `id` = {row_count};")));
    }

    #[test]
    fn mysql_data_grid_update_respects_target_sql_size() {
        // Keys long enough that two of them fit the text budget but not three, so
        // the merged run still has to split even though all rows share one SET.
        let key_length = 101_000;
        let mut options = mysql_people_save_options(3);
        options.rows[0][0] = json!("a".repeat(key_length));
        options.rows[1][0] = json!("b".repeat(key_length));
        options.rows[2][0] = json!("c".repeat(key_length));
        options.dirty_rows = vec![
            (0, vec![(1, json!("archived"))]),
            (1, vec![(1, json!("archived"))]),
            (2, vec![(1, json!("archived"))]),
        ];

        let result = prepare_data_grid_save(options);

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements.len(), 2);
        assert!(result.statements[0].contains("`id` IN ("), "{}", &result.statements[0][..80]);
        assert!(result.statements.iter().all(|statement| statement.contains("'archived'")));
        assert!(result.statements.iter().all(|statement| statement.len() <= MYSQL_DATA_GRID_BATCH_TARGET_SQL_BYTES));
    }

    #[test]
    fn splits_mysql_in_runs_when_the_plain_column_changes() {
        // The save path never mixes key columns inside one SET group, so this is
        // only reachable from a direct caller — but merging across columns would
        // rewrite `id = 1 OR code = 'a'` into a list that matches the wrong rows,
        // so the column change must break the run.
        let mut statements = Vec::new();
        push_mysql_key_predicate_batches(
            &mut statements,
            "UPDATE `app`.`people` SET `status` = 'x' WHERE ",
            vec![
                mysql_plain_predicate("`id` = 1", "`id`", "1"),
                mysql_plain_predicate("`code` = 'a'", "`code`", "'a'"),
                mysql_plain_predicate("`id` = 2", "`id`", "2"),
            ],
        );

        assert_eq!(
            statements,
            vec!["UPDATE `app`.`people` SET `status` = 'x' WHERE (`id` = 1) OR (`code` = 'a') OR (`id` = 2);"]
        );
    }

    #[test]
    fn keeps_non_mysql_batched_updates_in_or_form() {
        for (database_type, quote) in [(DatabaseType::Postgres, '"'), (DatabaseType::StarRocks, '`')] {
            let mut options = mysql_people_save_options(2);
            options.database_type = Some(database_type);
            options.table_meta.primary_keys = vec!["id".to_string()];
            options.dirty_rows = vec![(0, vec![(1, Value::Null)]), (1, vec![(1, Value::Null)])];

            let result = prepare_data_grid_save_for_driver_profile(options, None);

            assert_eq!(result.validation_error, None);
            assert_eq!(
                result.statements,
                vec![
                    format!("UPDATE {quote}app{quote}.{quote}people{quote} SET {quote}status{quote} = NULL WHERE {quote}id{quote} = 1;"),
                    format!("UPDATE {quote}app{quote}.{quote}people{quote} SET {quote}status{quote} = NULL WHERE {quote}id{quote} = 2;"),
                ],
                "{database_type:?} must keep one statement per row"
            );
        }
    }

    #[test]
    fn keeps_mysql_batching_for_starrocks_driver_profiles() {
        // The batch gate stays `DatabaseType::Mysql`; a MySQL connection whose
        // driver profile is "starrocks" must keep the same batching (and the same
        // IN form the DELETE path already emits there).
        let mut options = mysql_people_save_options(2);
        options.dirty_rows = vec![(0, vec![(1, Value::Null)]), (1, vec![(1, Value::Null)])];

        let result = prepare_data_grid_save_for_driver_profile(options, Some("starrocks"));

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements, vec!["UPDATE `app`.`people` SET `status` = NULL WHERE `id` IN (1, 2);"]);
    }

    #[test]
    fn preserves_mysql_update_order_across_different_values() {
        let mut options = mysql_people_save_options(3);
        options.dirty_rows =
            vec![(0, vec![(1, Value::Null)]), (1, vec![(1, json!("archived"))]), (2, vec![(1, Value::Null)])];

        let result = prepare_data_grid_save(options);

        assert_eq!(
            result.statements,
            vec![
                "UPDATE `app`.`people` SET `status` = NULL WHERE `id` = 1;",
                "UPDATE `app`.`people` SET `status` = 'archived' WHERE `id` = 2;",
                "UPDATE `app`.`people` SET `status` = NULL WHERE `id` = 3;",
            ]
        );
    }

    #[test]
    fn batches_mysql_deletes_and_deleted_row_rollbacks() {
        let mut options = mysql_people_save_options(3);
        options.deleted_rows = vec![0, 1, 2];

        let result = prepare_data_grid_save(options);
        for statement in &result.statements {
            println!("statement: {statement}");
        }

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements, vec!["DELETE FROM `app`.`people` WHERE `id` IN (1, 2, 3);"]);
        assert_eq!(
            result.rollback_statements,
            vec!["INSERT INTO `app`.`people` (`id`, `status`) VALUES (1, 'active'), (2, 'active'), (3, 'active');"]
        );
    }

    #[test]
    fn batches_mysql_compound_key_deletes_keep_or_form() {
        let mut options = mysql_people_save_options(3);
        options.table_meta.primary_keys = vec!["id".to_string(), "status".to_string()];
        options.deleted_rows = vec![0, 1];

        let result = prepare_data_grid_save(options);

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec![
                "DELETE FROM `app`.`people` WHERE (`id` = 1 AND `status` = 'active') OR (`id` = 2 AND `status` = 'active');"
            ]
        );
    }

    #[test]
    fn chunks_large_mysql_data_grid_batches() {
        let mut options = mysql_people_save_options(MYSQL_DATA_GRID_BATCH_MAX_ROWS + 1);
        options.deleted_rows = (0..options.rows.len()).collect();

        let result = prepare_data_grid_save(options);

        // IN lists are far more compact than OR chains, so the batch fits in a
        // single statement up to the row limit.
        assert_eq!(result.statements.len(), 1);
        assert!(result.statements[0].contains("`id` IN ("));
        assert!(result.statements[0].contains("501"));
        assert_eq!(result.rollback_statements.len(), 2);
        assert!(result.rollback_statements[0].contains("(500, 'active')"));
        assert_eq!(
            result.rollback_statements[1],
            "INSERT INTO `app`.`people` (`id`, `status`) VALUES (501, 'active');"
        );
    }

    #[test]
    fn mysql_data_grid_batch_respects_target_sql_size() {
        let mut options = mysql_people_save_options(2);
        options.rows[0][0] = json!("a".repeat(MYSQL_DATA_GRID_BATCH_TARGET_SQL_BYTES / 2));
        options.rows[1][0] = json!("b".repeat(MYSQL_DATA_GRID_BATCH_TARGET_SQL_BYTES / 2));
        options.deleted_rows = vec![0, 1];

        let result = prepare_data_grid_save(options);

        // The IN list stays within the SQL byte budget, splitting into a
        // second statement once the accumulated literals would exceed it.
        assert_eq!(result.statements.len(), 2);
        assert!(result.statements.iter().all(|statement| statement.len() <= MYSQL_DATA_GRID_BATCH_TARGET_SQL_BYTES));
    }

    #[test]
    fn keeps_keyless_mysql_writes_row_by_row() {
        let mut options = mysql_people_save_options(2);
        options.table_meta.primary_keys.clear();
        options.deleted_rows = vec![0, 1];

        let result = prepare_data_grid_save(options);

        assert_eq!(result.statements.len(), 2);
        assert_eq!(result.rollback_statements.len(), 2);
        assert!(result.statements.iter().all(|statement| !statement.contains(" OR ")));
    }

    #[test]
    fn casts_keyless_mysql_json_row_predicates() {
        let options = DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("app".to_string()),
                table_name: "documents".to_string(),
                primary_keys: vec![],
                columns: Some(vec![column("payload", "JSON", false, None), column("note", "varchar(32)", true, None)]),
            },
            columns: vec!["payload".to_string(), "note".to_string()],
            source_columns: None,
            rows: vec![vec![json!(r#"{"name":"before"}"#), json!("row-a")]],
            dirty_rows: vec![(0, vec![(0, json!(r#"{"name":"after"}"#))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        };

        let update = prepare_data_grid_save(options.clone());
        assert_eq!(update.validation_error, None);
        assert_eq!(
            update.statements,
            vec![
                r#"UPDATE `app`.`documents` SET `payload` = '{"name":"after"}' WHERE `payload` = CAST('{"name":"before"}' AS JSON) AND BINARY `note` = 'row-a';"#
            ]
        );
        assert_eq!(
            update.rollback_statements,
            vec![
                r#"UPDATE `app`.`documents` SET `payload` = '{"name":"before"}' WHERE `payload` = CAST('{"name":"after"}' AS JSON) AND BINARY `note` = 'row-a' AND `payload` = CAST('{"name":"after"}' AS JSON);"#
            ]
        );

        let delete = prepare_data_grid_save(DataGridSaveStatementOptions {
            dirty_rows: vec![],
            deleted_rows: vec![0],
            ..options
        });
        assert_eq!(delete.validation_error, None);
        assert_eq!(
            delete.statements,
            vec![
                r#"DELETE FROM `app`.`documents` WHERE `payload` = CAST('{"name":"before"}' AS JSON) AND BINARY `note` = 'row-a';"#
            ]
        );
        assert_eq!(
            delete.rollback_statements,
            vec![r#"INSERT INTO `app`.`documents` (`payload`, `note`) VALUES ('{"name":"before"}', 'row-a');"#]
        );
    }

    #[test]
    fn mysql_json_save_assignments_are_compact_and_predicates_stay_cast() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("app".to_string()),
                table_name: "documents".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![column("id", "int", false, None), column("payload", "JSON", true, None)]),
            },
            columns: vec!["id".to_string(), "payload".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!({"before": true})]],
            dirty_rows: vec![(0, vec![(1, json!([]))])],
            deleted_rows: vec![],
            new_rows: vec![vec![json!(2), json!({"nested": [1, 2]})]],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec![
                "UPDATE `app`.`documents` SET `payload` = '[]' WHERE `id` = 1;",
                "INSERT INTO `app`.`documents` (`id`, `payload`) VALUES (2, '{\"nested\":[1,2]}');",
            ]
        );
        assert_eq!(
            result.rollback_statements,
            vec![
                "DELETE FROM `app`.`documents` WHERE `id` = 2;",
                "UPDATE `app`.`documents` SET `payload` = '{\"before\":true}' WHERE `id` = 1 AND `payload` = CAST('[]' AS JSON);",
            ]
        );
    }

    #[test]
    fn formats_mysql_bit_literals_without_string_quotes() {
        let bit = column("flag", "bit(1)", true, None);
        let bit_string = column("flags", "bit(8)", true, None);

        assert_eq!(format_grid_sql_literal(&json!("0"), Some(DatabaseType::Mysql), Some(&bit)), "0");
        assert_eq!(format_grid_sql_literal(&json!("1"), Some(DatabaseType::Mysql), Some(&bit)), "1");
        assert_eq!(format_grid_sql_literal(&json!(true), Some(DatabaseType::Mysql), Some(&bit)), "1");
        assert_eq!(
            format_grid_sql_literal(&json!("10101010"), Some(DatabaseType::Mysql), Some(&bit_string)),
            "b'10101010'"
        );
        assert_eq!(format_grid_sql_literal(&json!("0"), Some(DatabaseType::Postgres), Some(&bit)), "'0'");
    }

    #[test]
    fn formats_kingbase_bit_literals_for_both_compatibility_modes() {
        let bit = column("flag", "pg_catalog.bit(1)", true, None);

        assert_eq!(
            format_grid_sql_literal_with_identifier_quote(
                &json!("0"),
                Some(DatabaseType::Kingbase),
                Some(&bit),
                Some("`"),
            ),
            "b'0'"
        );
        assert_eq!(
            format_grid_sql_literal_with_identifier_quote(
                &json!("10101010"),
                Some(DatabaseType::Kingbase),
                Some(&column("flags", "pg_catalog.bit(8)", true, None)),
                Some("`"),
            ),
            "b'10101010'"
        );
        assert_eq!(
            format_grid_sql_literal_with_identifier_quote(
                &json!("0"),
                Some(DatabaseType::Kingbase),
                Some(&bit),
                Some("\""),
            ),
            "b'0'"
        );
        assert_eq!(
            format_grid_sql_literal_with_identifier_quote(
                &json!(1),
                Some(DatabaseType::Kingbase),
                Some(&bit),
                Some("\""),
            ),
            "b'1'"
        );
    }

    #[test]
    fn kingbase_bit_save_uses_bit_string_literals() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Kingbase),
            identifier_quote: Some("`".to_string()),
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("public".to_string()),
                table_name: "flags".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![column("id", "int", false, None), column("flag", "pg_catalog.bit(1)", true, None)]),
            },
            columns: vec!["id".to_string(), "flag".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("1")]],
            dirty_rows: vec![(0, vec![(1, json!("0"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.statements, vec!["UPDATE `public`.`flags` SET `flag` = b'0' WHERE `id` = 1;"],);
        assert_eq!(
            result.rollback_statements,
            vec!["UPDATE `public`.`flags` SET `flag` = b'1' WHERE `id` = 1 AND `flag` = b'0';"],
        );
    }

    #[test]
    fn postgres_literals_use_escape_strings_for_stable_backslash_semantics() {
        let value = json!(r#"{"json_raw":"{\"foo\":1,\"bar\":\"sometext\"}"}"#);
        assert_eq!(
            format_grid_sql_literal(&value, Some(DatabaseType::Postgres), None),
            r#"E'{"json_raw":"{\\"foo\\":1,\\"bar\\":\\"sometext\\"}"}'"#
        );
        assert_eq!(format_grid_sql_literal(&json!("it's"), Some(DatabaseType::Postgres), None), "'it''s'");
    }

    #[test]
    fn postgres_temporal_literals_preserve_session_formatted_values_for_grid_writes() {
        let timestamp = column("created_at", "timestamp without time zone", false, None);
        let timestamptz = column("created_at", "timestamp with time zone", false, None);

        assert_eq!(
            format_grid_sql_literal(&json!("2026-09-13 01:00:00"), Some(DatabaseType::Postgres), Some(&timestamp),),
            "'2026-09-13 01:00:00'"
        );
        assert_eq!(
            format_grid_sql_literal(
                &json!("2026-09-13T13:00:00+12:00"),
                Some(DatabaseType::Postgres),
                Some(&timestamptz),
            ),
            "'2026-09-13T13:00:00+12:00'"
        );
    }

    #[test]
    fn uuid_column_literals_decode_prefixed_hex_values() {
        let uuid_col = column("id", "uuid", false, None);
        assert_eq!(
            format_grid_sql_literal(
                &json!("0x30316130353762662d326262642d373061362d623363332d353665623430396130663935"),
                Some(DatabaseType::Mysql),
                Some(&uuid_col)
            ),
            "'01a057bf-2bbd-70a6-b3c3-56eb409a0f95'"
        );
        assert_eq!(
            format_grid_sql_literal(
                &json!("0x01a057bf2bbd70a6b3c356eb409a0f95"),
                Some(DatabaseType::Mysql),
                Some(&uuid_col)
            ),
            "'01a057bf-2bbd-70a6-b3c3-56eb409a0f95'"
        );
        assert_eq!(
            format_grid_sql_literal(
                &json!("01a057bf-2bbd-70a6-b3c3-56eb409a0f95"),
                Some(DatabaseType::Mysql),
                Some(&uuid_col)
            ),
            "'01a057bf-2bbd-70a6-b3c3-56eb409a0f95'"
        );
    }

    #[test]
    fn uuid_column_literals_handle_malformed_unicode_safely() {
        let uuid_col = column("id", "uuid", false, None);

        // 3-byte UTF-8 char '你' + 69 ASCII chars = 72 bytes. Slicing at byte index 2 must not panic.
        let malformed_72_bytes = format!("0x你{}", "0".repeat(69));
        assert_eq!(malformed_72_bytes.len(), 2 + 3 + 69); // 74 bytes with prefix '0x'
        assert_eq!(format_hex_uuid_literal(&malformed_72_bytes), None);
        // format_grid_sql_literal falls back safely to quoted string without panicking
        assert_eq!(
            format_grid_sql_literal(&json!(malformed_72_bytes), Some(DatabaseType::Mysql), Some(&uuid_col)),
            format!("'{}'", malformed_72_bytes.replace('\\', "\\\\").replace('\'', "\\'"))
        );

        // 10 3-byte UTF-8 chars (30 bytes) + 2 ASCII chars = 32 bytes
        let malformed_32_bytes = format!("0x{}00", "你".repeat(10));
        assert_eq!(format_hex_uuid_literal(&malformed_32_bytes), None);

        // Non-hex ASCII chars
        let non_hex_72 = format!("0x{}", "g".repeat(72));
        assert_eq!(format_hex_uuid_literal(&non_hex_72), None);
        let non_hex_32 = format!("0x{}", "z".repeat(32));
        assert_eq!(format_hex_uuid_literal(&non_hex_32), None);
    }

    #[test]
    fn binary_columns_preserve_canonical_hex_for_ascii_uuid() {
        let binary_col = column("payload", "binary(36)", false, None);
        let varbinary_col = column("payload", "varbinary(36)", false, None);
        let hex = "0x30316130353762662d326262642d373061362d623363332d353665623430396130663935";

        // MySQL binary columns emit the canonical 0x<hex> literal unchanged;
        // the important part of the fix is that content-shape UUID guessing
        // no longer turns this into a quoted '01a057bf-...' UUID string.
        assert_eq!(format_grid_sql_literal(&json!(hex), Some(DatabaseType::Mysql), Some(&binary_col)), hex);
        assert_eq!(format_grid_sql_literal(&json!(hex), Some(DatabaseType::Mysql), Some(&varbinary_col)), hex);
    }

    #[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 postgres_bytea_literal_gate_accepts_schema_qualified_and_rejects_arrays() {
        let qualified = column("payload", "pg_catalog.bytea", true, None);
        let array = column("payload", "bytea[]", true, None);

        assert_eq!(
            format_grid_sql_literal(&json!("0x00aBff"), Some(DatabaseType::Postgres), Some(&qualified)),
            "decode('00aBff', 'hex')"
        );
        assert_eq!(
            format_grid_sql_literal(&json!("0x00aBff"), Some(DatabaseType::Postgres), Some(&array)),
            "'0x00aBff'"
        );
        assert_eq!(
            format_grid_sql_literal(&json!("0x00aBff"), Some(DatabaseType::Gaussdb), Some(&qualified)),
            "'0x00aBff'"
        );
    }

    #[test]
    fn sqlite_family_literals_do_not_double_escape_backslashes() {
        let value = json!(r#"{"json_raw":"{\"foo\":1,\"bar\":\"sometext\"}"}"#);
        for database_type in
            [DatabaseType::Sqlite, DatabaseType::Rqlite, DatabaseType::Turso, DatabaseType::CloudflareD1]
        {
            assert_eq!(
                format_grid_sql_literal(&value, Some(database_type), None),
                r#"'{"json_raw":"{\"foo\":1,\"bar\":\"sometext\"}"}'"#
            );
            assert_eq!(format_grid_sql_literal(&json!("it's"), Some(database_type), None), "'it''s'");
        }
    }

    #[test]
    fn dameng_data_grid_writes_do_not_double_escape_backslashes() {
        assert_eq!(format_grid_sql_literal(&json!(r"\n"), Some(DatabaseType::Dameng), None), r"'\n'");
        assert_eq!(format_grid_sql_literal(&json!(r"line\n's"), Some(DatabaseType::Dameng), None), r"'line\n''s'");

        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Dameng),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("DBX_TEST".to_string()),
                table_name: "DBX_NEWLINE_REPRO".to_string(),
                primary_keys: vec!["ID".to_string()],
                columns: Some(vec![column("ID", "INT", false, None), column("VAL", "VARCHAR(100)", true, None)]),
            },
            columns: vec!["ID".to_string(), "VAL".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("old")]],
            dirty_rows: vec![(0, vec![(1, json!(r"line\n's"))])],
            deleted_rows: vec![],
            new_rows: vec![vec![json!(2), json!(r"\n")]],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec![
                r#"UPDATE "DBX_TEST"."DBX_NEWLINE_REPRO" SET "VAL" = 'line\n''s' WHERE "ID" = 1;"#,
                r#"INSERT INTO "DBX_TEST"."DBX_NEWLINE_REPRO" ("ID", "VAL") VALUES (2, '\n');"#,
            ]
        );
    }

    #[test]
    fn oracle_data_grid_writes_do_not_double_escape_backslashes() {
        assert_eq!(format_grid_sql_literal(&json!(r"\n"), Some(DatabaseType::Oracle), None), r"'\n'");
        assert_eq!(format_grid_sql_literal(&json!(r"line\n's"), Some(DatabaseType::Oracle), None), r"'line\n''s'");

        let nested_json = r#"{"ext":"{\"v1\":\"123\",\"v3\":\"{\\\"vl1\\\":\\\"x\\\"}\"}"}"#;
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Oracle),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("DBX_TEST".to_string()),
                table_name: "DBX9708_JSON".to_string(),
                primary_keys: vec!["ID".to_string()],
                columns: Some(vec![column("ID", "NUMBER", false, None), column("VAL", "VARCHAR2(400)", true, None)]),
            },
            columns: vec!["ID".to_string(), "VAL".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("old")]],
            dirty_rows: vec![(0, vec![(1, json!(nested_json))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec![format!(r#"UPDATE "DBX_TEST"."DBX9708_JSON" SET "VAL" = '{nested_json}' WHERE "ID" = 1;"#)]
        );
    }

    #[test]
    fn sqlserver_binary_literals_preserve_prefixed_hex_values() {
        for data_type in ["timestamp", "ROWVERSION", "binary(8)", "VARBINARY(MAX)"] {
            let binary = column("version", data_type, true, None);
            for (value, expected) in [
                ("0x0000000000639f1c", "0x0000000000639f1c"),
                ("0x00aBff", "0x00aBff"),
                ("0X00aBff", "0x00aBff"),
                ("0x", "0x"),
            ] {
                assert_eq!(
                    format_grid_sql_literal(&json!(value), Some(DatabaseType::SqlServer), Some(&binary)),
                    expected,
                    "{data_type}: {value}"
                );
            }
            assert_eq!(format_grid_sql_literal(&Value::Null, Some(DatabaseType::SqlServer), Some(&binary)), "NULL");
        }
    }

    #[test]
    fn sqlserver_binary_literals_keep_invalid_hex_and_text_columns_quoted() {
        let binary = column("version", "timestamp", true, None);
        for (value, expected) in [
            ("0x0g", "N'0x0g'"),
            ("0x123", "N'0x123'"),
            ("0x你", "N'0x你'"),
            ("0x00; DELETE FROM users", "N'0x00; DELETE FROM users'"),
            ("0x00' OR 1=1--", "N'0x00'' OR 1=1--'"),
        ] {
            assert_eq!(format_grid_sql_literal(&json!(value), Some(DatabaseType::SqlServer), Some(&binary)), expected);
        }
        for data_type in ["nvarchar(50)", "datetime2", "timestamped", "notbinary"] {
            let text = column("value", data_type, true, None);
            assert_eq!(
                format_grid_sql_literal(&json!("0x00ab"), Some(DatabaseType::SqlServer), Some(&text)),
                "N'0x00ab'"
            );
        }
        assert_eq!(format_grid_sql_literal(&json!("0x00ab"), Some(DatabaseType::SqlServer), None), "N'0x00ab'");
        for database_type in [DatabaseType::Postgres, DatabaseType::Mysql] {
            assert_eq!(format_grid_sql_literal(&json!("0x00ab"), Some(database_type), Some(&binary)), "'0x00ab'");
        }
    }

    #[test]
    fn sqlserver_keyless_save_uses_binary_rowversion_predicates() {
        // Table metadata calls this timestamp/rowversion; TDS result metadata
        // may instead report binary/varbinary. Both must preserve the bytes.
        for data_type in ["timestamp", "rowversion", "binary(8)", "varbinary(8)"] {
            let mut options = mysql_people_save_options(2);
            options.database_type = Some(DatabaseType::SqlServer);
            options.table_meta.schema = Some("dbo".into());
            options.table_meta.primary_keys.clear();
            options.table_meta.columns.as_mut().unwrap().push(column("version", data_type, false, None));
            options.columns.push("version".into());
            options.rows[0].push(json!("0x0000000000639f1c"));
            options.rows[1].push(json!("0x0000000000639f1d"));
            options.dirty_rows = vec![(0, vec![(1, json!("blocked"))])];
            options.deleted_rows = vec![1];

            let result = prepare_data_grid_save(options);
            assert_eq!(result.validation_error, None);
            assert_eq!(
                result.statements,
                vec![
                    "UPDATE [dbo].[people] SET [status] = N'blocked' WHERE [id] = 1 AND [status] = N'active' AND [version] = 0x0000000000639f1c;",
                    "DELETE FROM [dbo].[people] WHERE [id] = 2 AND [status] = N'active' AND [version] = 0x0000000000639f1d;",
                ],
                "{data_type}"
            );
            assert_eq!(result.keyless_guards.len(), 2);
            assert_guards_cover_statement_predicates(&result);
        }
    }

    #[test]
    fn sqlserver_literals_do_not_double_escape_backslashes() {
        assert_eq!(format_grid_sql_literal(&json!(r".\SQL2016"), Some(DatabaseType::SqlServer), None), r"N'.\SQL2016'");
        assert_eq!(
            format_grid_sql_literal(&json!(r".\SQL2016's"), Some(DatabaseType::SqlServer), None),
            r"N'.\SQL2016''s'"
        );
    }

    #[test]
    fn sqlserver_literals_preserve_backslashes_before_line_breaks() {
        assert_eq!(
            format_grid_sql_literal(&json!("line1\\\nline2"), Some(DatabaseType::SqlServer), None),
            r"N'line1\' + NCHAR(10) + N'line2'"
        );
        assert_eq!(
            format_grid_sql_literal(&json!("line1\\\r\nline2"), Some(DatabaseType::SqlServer), None),
            r"N'line1\' + NCHAR(13) + NCHAR(10) + N'line2'"
        );
    }

    #[test]
    fn sqlserver_long_multiline_literals_anchor_nvarchar_max() {
        // A `+` chain of non-`max` operands is typed nvarchar(4000) by SQL Server, which
        // silently truncates a longer result, so a long value must be anchored on
        // nvarchar(max) or the copied statement loses everything past 4000 characters
        // (issue #12202).
        let long_value = format!("{}\n{}", "a".repeat(3000), "b".repeat(3000));
        let literal = format_grid_sql_literal(&json!(long_value), Some(DatabaseType::SqlServer), None);
        assert!(literal.starts_with("CAST(N'' AS NVARCHAR(MAX)) + N'"), "{literal}");
        assert!(literal.contains(" + NCHAR(10) + N'"), "{literal}");

        // A value that fits in nvarchar(4000) keeps the plain chain.
        let short_value = format_grid_sql_literal(&json!("line1\nline2"), Some(DatabaseType::SqlServer), None);
        assert_eq!(short_value, r"N'line1' + NCHAR(10) + N'line2'");

        // A single long literal is already nvarchar(max) by SQL Server's own rules.
        let single_line = format_grid_sql_literal(&json!("a".repeat(5000)), Some(DatabaseType::SqlServer), None);
        assert_eq!(single_line, format!("N'{}'", "a".repeat(5000)));
    }

    #[test]
    fn prepares_sqlserver_updates_without_doubling_backslashes() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::SqlServer),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("dbo".to_string()),
                table_name: "dbx_issue_4181".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![column("id", "int", false, None), column("value", "nvarchar(100)", true, None)]),
            },
            columns: vec!["id".to_string(), "value".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("initial")]],
            dirty_rows: vec![(0, vec![(1, json!(r".\SQL2016"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec![r"UPDATE [dbo].[dbx_issue_4181] SET [value] = N'.\SQL2016' WHERE [id] = 1;"]
        );
    }

    #[test]
    fn mysql_and_neo4j_literals_keep_doubling_backslashes() {
        // MySQL (default sql_mode, without NO_BACKSLASH_ESCAPES) and Neo4j do treat
        // backslash as an escape character, so this behavior must be preserved.
        assert_eq!(format_grid_sql_literal(&json!(r"a\b"), Some(DatabaseType::Mysql), None), r"'a\\b'");
        assert_eq!(format_grid_sql_literal(&json!(r"a\b"), Some(DatabaseType::Neo4j), None), r"'a\\b'");
    }

    #[test]
    fn oceanbase_oracle_literals_do_not_double_escape_backslashes() {
        assert_eq!(format_grid_sql_literal(&json!(r"a\b"), Some(DatabaseType::OceanbaseOracle), None), r"'a\b'");
        assert_eq!(
            format_grid_sql_literal(&json!(r"line\n's"), Some(DatabaseType::OceanbaseOracle), None),
            r"'line\n''s'"
        );
    }

    #[test]
    fn oracle_clob_literals_keep_backslashes_single() {
        let clob = column("body", "CLOB", true, None);
        assert_eq!(
            format_grid_assignment_sql_literal(&json!(r"a\b's"), Some(DatabaseType::Oracle), Some(&clob), None),
            r"'a\b''s'"
        );
        // Backslashes count as one byte toward the chunk budget, so a value at the
        // chunk boundary splits at the same offset it would without backslashes.
        let value = format!("{}\\", "x".repeat(ORACLE_SQL_LITERAL_MAX_BYTES));
        let literal = format_grid_assignment_sql_literal(&json!(value), Some(DatabaseType::Oracle), Some(&clob), None);
        let chunks = literal
            .split("TO_CLOB('")
            .skip(1)
            .map(|chunk| chunk.split("')").next().unwrap_or_default())
            .collect::<Vec<_>>();
        assert_eq!(chunks.len(), 2);
        assert_eq!(chunks[0].len(), ORACLE_LOB_LITERAL_CHUNK_BYTES);
        assert!(chunks[1].ends_with('\\'));
        assert!(!chunks.iter().any(|chunk| chunk.contains("\\\\")));
    }

    #[test]
    fn prepares_sqlserver_bitn_updates_with_numeric_literals() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::SqlServer),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("dbo".to_string()),
                table_name: "flags".to_string(),
                primary_keys: vec![],
                columns: Some(vec![column("id", "int", false, None), column("active", "bitn", false, None)]),
            },
            columns: vec!["id".to_string(), "active".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!(false)]],
            dirty_rows: vec![(0, vec![(1, json!(true))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements, vec!["UPDATE [dbo].[flags] SET [active] = 1 WHERE [id] = 1 AND [active] = 0;"]);
        assert_eq!(
            result.rollback_statements,
            vec!["UPDATE [dbo].[flags] SET [active] = 0 WHERE [id] = 1 AND [active] = 1 AND [active] = 1;"]
        );
        for sql in result.statements.iter().chain(result.rollback_statements.iter()) {
            assert!(!sql.contains("TRUE"));
            assert!(!sql.contains("FALSE"));
        }
    }

    #[test]
    fn saves_empty_nullable_mysql_numeric_cell_as_null() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "employees".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![column("id", "int(11)", false, None), column("age", "int(11)", true, None)]),
            },
            columns: vec!["id".to_string(), "age".to_string()],
            source_columns: None,
            rows: vec![vec![json!(2), json!(36)]],
            dirty_rows: vec![(0, vec![(1, json!(""))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements, vec!["UPDATE `employees` SET `age` = NULL WHERE `id` = 2;"]);
        assert_eq!(
            result.rollback_statements,
            vec!["UPDATE `employees` SET `age` = 36 WHERE `id` = 2 AND `age` IS NULL;"]
        );
    }

    #[test]
    fn saves_json_null_bigint_cell_as_unquoted_sql_null() {
        // Regression for #9970: bulk-editing a bigint column to NULL must emit
        // `= NULL`, never `= 'NULL'` / `= ''` (ERROR 1366 on MySQL/OceanBase).
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "orders".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![column("id", "bigint", false, None), column("detail_id", "bigint", true, None)]),
            },
            columns: vec!["id".to_string(), "detail_id".to_string()],
            source_columns: None,
            rows: vec![vec![json!(7), json!(42)]],
            dirty_rows: vec![(0, vec![(1, Value::Null)])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements, vec!["UPDATE `orders` SET `detail_id` = NULL WHERE `id` = 7;"]);
    }

    #[test]
    fn mysql_conditional_update_null_bigint_is_unquoted_sql_null() {
        let statement = build_data_grid_conditional_update_sql(DataGridConditionalUpdateSqlOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "orders".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![column("id", "bigint", false, None), column("detail_id", "bigint", true, None)]),
            },
            column_name: "detail_id".to_string(),
            value: Value::Null,
            where_input: "tenant_id = 1".to_string(),
        });

        assert_eq!(statement, Some("UPDATE `orders` SET `detail_id` = NULL WHERE (tenant_id = 1);".to_string()));
    }

    #[test]
    fn keeps_empty_nullable_mysql_text_cell_as_empty_string() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "employees".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![column("id", "int(11)", false, None), column("name", "varchar(50)", true, None)]),
            },
            columns: vec!["id".to_string(), "name".to_string()],
            source_columns: None,
            rows: vec![vec![json!(2), json!("Ada")]],
            dirty_rows: vec![(0, vec![(1, json!(""))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements, vec!["UPDATE `employees` SET `name` = '' WHERE `id` = 2;"]);
        assert_eq!(
            result.rollback_statements,
            vec!["UPDATE `employees` SET `name` = 'Ada' WHERE `id` = 2 AND BINARY `name` = '';"]
        );
    }

    #[test]
    fn preserves_mysql_text_cell_line_breaks() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "employees".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![column("id", "int(11)", false, None), column("name", "varchar(50)", true, None)]),
            },
            columns: vec!["id".to_string(), "name".to_string()],
            source_columns: None,
            rows: vec![vec![json!(2), json!("Ada")]],
            dirty_rows: vec![(0, vec![(1, json!("111\n222"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements, vec!["UPDATE `employees` SET `name` = '111\n222' WHERE `id` = 2;"]);
        assert_eq!(
            result.rollback_statements,
            vec!["UPDATE `employees` SET `name` = 'Ada' WHERE `id` = 2 AND BINARY `name` = '111\n222';"]
        );
    }

    #[test]
    fn prepares_sqlserver_save_statements() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::SqlServer),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("game".to_string()),
                table_name: "player states".to_string(),
                primary_keys: vec!["role id".to_string()],
                columns: None,
            },
            columns: vec!["role id".to_string(), "state".to_string(), "updated at".to_string()],
            source_columns: None,
            rows: vec![vec![json!(42), json!("old"), json!("2026-05-03")]],
            dirty_rows: vec![(0, vec![(1, json!("ready")), (2, json!("2026-05-04"))])],
            deleted_rows: vec![0],
            new_rows: vec![vec![json!(43), json!("new"), json!("2026-05-05")]],
            include_database_name: false,
        });

        assert_eq!(
            result.statements,
            vec![
                "UPDATE [game].[player states] SET [state] = N'ready', [updated at] = N'2026-05-04' WHERE [role id] = 42;",
                "DELETE FROM [game].[player states] WHERE [role id] = 42;",
                "INSERT INTO [game].[player states] ([role id], [state], [updated at]) VALUES (43, N'new', N'2026-05-05');",
            ]
        );
    }

    #[test]
    fn prepares_tdengine_child_table_delete_from_stable_row() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Tdengine),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("dbx_tdengine_demo".to_string()),
                table_name: "meters".to_string(),
                primary_keys: vec![DBX_TDENGINE_TBNAME_COLUMN.to_string(), "ts".to_string()],
                columns: Some(vec![column("ts", "TIMESTAMP", false, None), column("voltage", "FLOAT", true, None)]),
            },
            columns: vec![DBX_TDENGINE_TBNAME_COLUMN.to_string(), "ts".to_string(), "voltage".to_string()],
            source_columns: None,
            rows: vec![vec![json!("codex_delete_verify"), json!("2026-07-10T13:59:00.456+08:00"), json!(221.5)]],
            dirty_rows: vec![],
            deleted_rows: vec![0],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec!["DELETE FROM `dbx_tdengine_demo`.`codex_delete_verify` WHERE `ts` = '2026-07-10T13:59:00.456+08:00';"]
        );
        assert_eq!(
            result.rollback_statements,
            vec!["INSERT INTO `dbx_tdengine_demo`.`meters` (`tbname`, `ts`, `voltage`) VALUES ('codex_delete_verify', '2026-07-10T13:59:00.456+08:00', 221.5);"]
        );
    }

    #[test]
    fn rejects_tdengine_composite_key_delete_from_same_timestamp_stable_rows() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Tdengine),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("dbx_tdengine_demo".to_string()),
                table_name: "meters".to_string(),
                primary_keys: vec![DBX_TDENGINE_TBNAME_COLUMN.to_string(), "ts".to_string(), "seq".to_string()],
                columns: Some(vec![
                    column("ts", "TIMESTAMP", false, None),
                    column("seq", "INT", false, Some("COMPOSITE KEY")),
                    column("voltage", "FLOAT", true, None),
                ]),
            },
            columns: vec![
                DBX_TDENGINE_TBNAME_COLUMN.to_string(),
                "ts".to_string(),
                "seq".to_string(),
                "voltage".to_string(),
            ],
            source_columns: None,
            rows: vec![
                vec![json!("device_a"), json!("2026-07-10T13:59:00.456+08:00"), json!(1), json!(221.5)],
                vec![json!("device_a"), json!("2026-07-10T13:59:00.456+08:00"), json!(2), json!(222.5)],
            ],
            dirty_rows: vec![],
            deleted_rows: vec![1],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(
            result.validation_error,
            Some("TDengine tables with composite keys do not support row deletion.".to_string())
        );
        assert!(result.statements.is_empty());
        assert!(result.rollback_statements.is_empty());
    }

    #[test]
    fn prepares_tdengine_delete_from_direct_child_table_row() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Tdengine),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("dbx_tdengine_demo".to_string()),
                table_name: "codex_grid_accept_20260710".to_string(),
                primary_keys: vec!["ts".to_string()],
                columns: Some(vec![column("ts", "TIMESTAMP", false, None), column("voltage", "FLOAT", true, None)]),
            },
            columns: vec!["ts".to_string(), "voltage".to_string()],
            source_columns: None,
            rows: vec![vec![json!("2026-07-10T16:00:00.111+08:00"), json!(220.1)]],
            dirty_rows: vec![],
            deleted_rows: vec![0],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec!["DELETE FROM `dbx_tdengine_demo`.`codex_grid_accept_20260710` WHERE `ts` = '2026-07-10T16:00:00.111+08:00';"]
        );
        assert_eq!(
            result.rollback_statements,
            vec!["INSERT INTO `dbx_tdengine_demo`.`codex_grid_accept_20260710` (`ts`, `voltage`) VALUES ('2026-07-10T16:00:00.111+08:00', 220.1);"]
        );
    }

    #[test]
    fn prepares_tdengine_overwrite_for_direct_child_table_row() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Tdengine),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("dbx_tdengine_demo".to_string()),
                table_name: "codex_grid_update_verify_20260710".to_string(),
                primary_keys: vec!["ts".to_string()],
                columns: Some(vec![
                    column("ts", "TIMESTAMP", false, None),
                    column("voltage", "FLOAT", true, None),
                    column("current", "FLOAT", true, None),
                ]),
            },
            columns: vec!["ts".to_string(), "voltage".to_string(), "current".to_string()],
            source_columns: None,
            rows: vec![vec![json!("2026-07-10T16:30:00.444+08:00"), json!(220.0), json!(1.0)]],
            dirty_rows: vec![(0, vec![(1, json!(229.9))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec!["INSERT INTO `dbx_tdengine_demo`.`codex_grid_update_verify_20260710` (`ts`, `voltage`, `current`) VALUES ('2026-07-10T16:30:00.444+08:00', 229.9, 1.0);"]
        );
        assert_eq!(
            result.rollback_statements,
            vec!["INSERT INTO `dbx_tdengine_demo`.`codex_grid_update_verify_20260710` (`ts`, `voltage`, `current`) VALUES ('2026-07-10T16:30:00.444+08:00', 220.0, 1.0);"]
        );
    }

    #[test]
    fn prepares_tdengine_composite_key_overwrite_and_rollback_for_same_timestamp_child_rows() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Tdengine),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("dbx_tdengine_demo".to_string()),
                table_name: "device_a".to_string(),
                primary_keys: vec!["ts".to_string(), "seq".to_string()],
                columns: Some(vec![
                    column("ts", "TIMESTAMP", false, None),
                    column("seq", "INT", false, Some("COMPOSITE KEY")),
                    column("voltage", "FLOAT", true, None),
                ]),
            },
            columns: vec!["ts".to_string(), "seq".to_string(), "voltage".to_string()],
            source_columns: None,
            rows: vec![
                vec![json!("2026-07-10T16:30:00.444+08:00"), json!(1), json!(220.0)],
                vec![json!("2026-07-10T16:30:00.444+08:00"), json!(2), json!(221.0)],
            ],
            dirty_rows: vec![(1, vec![(2, json!(229.9))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec!["INSERT INTO `dbx_tdengine_demo`.`device_a` (`ts`, `seq`, `voltage`) VALUES ('2026-07-10T16:30:00.444+08:00', 2, 229.9);"]
        );
        assert_eq!(
            result.rollback_statements,
            vec!["INSERT INTO `dbx_tdengine_demo`.`device_a` (`ts`, `seq`, `voltage`) VALUES ('2026-07-10T16:30:00.444+08:00', 2, 221.0);"]
        );
    }

    #[test]
    fn prepares_tdengine_stable_insert_with_child_table_identity() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Tdengine),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("dbx_tdengine_demo".to_string()),
                table_name: "issue_3121_devices".to_string(),
                primary_keys: vec![DBX_TDENGINE_TBNAME_COLUMN.to_string(), "ts".to_string()],
                columns: Some(vec![
                    column("ts", "TIMESTAMP", false, None),
                    column("reading", "FLOAT", true, None),
                    column("site", "VARCHAR", true, Some("TAG")),
                ]),
            },
            columns: vec![
                DBX_TDENGINE_TBNAME_COLUMN.to_string(),
                "ts".to_string(),
                "reading".to_string(),
                "site".to_string(),
            ],
            source_columns: None,
            rows: vec![],
            dirty_rows: vec![],
            deleted_rows: vec![],
            new_rows: vec![vec![
                json!("codex_issue3121_insert_verify"),
                json!("2026-07-10T17:48:51.000+08:00"),
                json!(1.0),
                json!("codex-lab"),
            ]],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec!["INSERT INTO `dbx_tdengine_demo`.`issue_3121_devices` (`tbname`, `ts`, `reading`, `site`) VALUES ('codex_issue3121_insert_verify', '2026-07-10T17:48:51.000+08:00', 1.0, 'codex-lab');"]
        );
        assert_eq!(
            result.rollback_statements,
            vec!["DELETE FROM `dbx_tdengine_demo`.`codex_issue3121_insert_verify` WHERE `ts` = '2026-07-10T17:48:51.000+08:00';"]
        );
    }

    #[test]
    fn skips_tdengine_composite_key_insert_rollback_for_same_timestamp_rows() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Tdengine),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("dbx_tdengine_demo".to_string()),
                table_name: "meters".to_string(),
                primary_keys: vec![DBX_TDENGINE_TBNAME_COLUMN.to_string(), "ts".to_string(), "seq".to_string()],
                columns: Some(vec![
                    column("ts", "TIMESTAMP", false, None),
                    column("seq", "INT", false, Some("COMPOSITE KEY")),
                    column("voltage", "FLOAT", true, None),
                ]),
            },
            columns: vec![
                DBX_TDENGINE_TBNAME_COLUMN.to_string(),
                "ts".to_string(),
                "seq".to_string(),
                "voltage".to_string(),
            ],
            source_columns: None,
            rows: vec![],
            dirty_rows: vec![],
            deleted_rows: vec![],
            new_rows: vec![
                vec![json!("device_a"), json!("2026-07-10T17:48:51.000+08:00"), json!(1), json!(221.0)],
                vec![json!("device_a"), json!("2026-07-10T17:48:51.000+08:00"), json!(2), json!(222.0)],
            ],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec![
                "INSERT INTO `dbx_tdengine_demo`.`meters` (`tbname`, `ts`, `seq`, `voltage`) VALUES ('device_a', '2026-07-10T17:48:51.000+08:00', 1, 221.0);",
                "INSERT INTO `dbx_tdengine_demo`.`meters` (`tbname`, `ts`, `seq`, `voltage`) VALUES ('device_a', '2026-07-10T17:48:51.000+08:00', 2, 222.0);",
            ]
        );
        assert!(result.rollback_statements.is_empty());
    }

    #[test]
    fn rejects_tdengine_stable_insert_without_child_table_identity() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Tdengine),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("dbx_tdengine_demo".to_string()),
                table_name: "issue_3121_devices".to_string(),
                primary_keys: vec![DBX_TDENGINE_TBNAME_COLUMN.to_string(), "ts".to_string()],
                columns: Some(vec![column("ts", "TIMESTAMP", false, None), column("reading", "FLOAT", true, None)]),
            },
            columns: vec![DBX_TDENGINE_TBNAME_COLUMN.to_string(), "ts".to_string(), "reading".to_string()],
            source_columns: None,
            rows: vec![],
            dirty_rows: vec![],
            deleted_rows: vec![],
            new_rows: vec![vec![Value::Null, json!("2026-07-10T17:48:51.000+08:00"), json!(1.0)]],
            include_database_name: false,
        });

        assert_eq!(
            result.validation_error,
            Some("TDengine STABLE inserts require a child table name (tbname).".to_string())
        );
        assert!(result.statements.is_empty());
        assert!(result.rollback_statements.is_empty());
    }

    #[test]
    fn rejects_tdengine_delete_without_child_table_identity() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Tdengine),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("dbx_tdengine_demo".to_string()),
                table_name: "meters".to_string(),
                primary_keys: vec![DBX_TDENGINE_TBNAME_COLUMN.to_string(), "ts".to_string()],
                columns: Some(vec![column("ts", "TIMESTAMP", false, None)]),
            },
            columns: vec!["ts".to_string()],
            source_columns: None,
            rows: vec![vec![json!("2026-07-10T13:59:00.456+08:00")]],
            dirty_rows: vec![],
            deleted_rows: vec![0],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(
            result.validation_error,
            Some("TDengine row editing requires all row identifier columns in the result.".to_string())
        );
        assert!(result.statements.is_empty());
    }

    #[test]
    fn rejects_tdengine_existing_row_edit_when_composite_key_is_missing() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Tdengine),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("dbx_tdengine_demo".to_string()),
                table_name: "device_a".to_string(),
                primary_keys: vec!["ts".to_string(), "seq".to_string()],
                columns: Some(vec![
                    column("ts", "TIMESTAMP", false, None),
                    column("seq", "INT", false, Some("COMPOSITE KEY")),
                    column("voltage", "FLOAT", true, None),
                ]),
            },
            columns: vec!["ts".to_string(), "voltage".to_string()],
            source_columns: None,
            rows: vec![vec![json!("2026-07-10T16:30:00.444+08:00"), json!(220.0)]],
            dirty_rows: vec![(0, vec![(1, json!(229.9))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(
            result.validation_error,
            Some("TDengine row editing requires all row identifier columns in the result.".to_string())
        );
        assert!(result.statements.is_empty());
        assert!(result.rollback_statements.is_empty());
    }

    #[test]
    fn rejects_tdengine_existing_row_identity_changes() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Tdengine),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("dbx_tdengine_demo".to_string()),
                table_name: "device_a".to_string(),
                primary_keys: vec!["ts".to_string(), "seq".to_string()],
                columns: Some(vec![
                    column("ts", "TIMESTAMP", false, None),
                    column("seq", "INT", false, Some("COMPOSITE KEY")),
                    column("voltage", "FLOAT", true, None),
                ]),
            },
            columns: vec!["ts".to_string(), "seq".to_string(), "voltage".to_string()],
            source_columns: None,
            rows: vec![vec![json!("2026-07-10T16:30:00.444+08:00"), json!(2), json!(220.0)]],
            dirty_rows: vec![(0, vec![(1, json!(3))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, Some("TDengine row identifier columns cannot be edited.".to_string()));
        assert!(result.statements.is_empty());
        assert!(result.rollback_statements.is_empty());
    }

    #[test]
    fn prepares_databend_save_statements() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Databend),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("default".to_string()),
                table_name: "people".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![column("id", "int", false, None), column("name", "string", true, None)]),
            },
            columns: vec!["id".to_string(), "name".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("Ada")]],
            dirty_rows: vec![(0, vec![(1, json!("Linus"))])],
            deleted_rows: vec![0],
            new_rows: vec![vec![json!(2), json!("Grace")]],
            include_database_name: false,
        });

        assert_eq!(
            result.statements,
            vec![
                "UPDATE `default`.`people` SET `name` = 'Linus' WHERE `id` = 1;",
                "DELETE FROM `default`.`people` WHERE `id` = 1;",
                "INSERT INTO `default`.`people` (`id`, `name`) VALUES (2, 'Grace');",
            ]
        );
    }

    #[test]
    fn prepares_clickhouse_mutation_save_statements() {
        let mut id_column = column("id", "UInt64", false, None);
        id_column.is_primary_key = true;
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::ClickHouse),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("default".to_string()),
                table_name: "people".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![id_column, column("name", "String", true, None)]),
            },
            columns: vec!["id".to_string(), "name".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("Ada")]],
            dirty_rows: vec![(0, vec![(1, json!("Linus"))])],
            deleted_rows: vec![0],
            new_rows: vec![vec![json!(2), json!("Grace")]],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec![
                "ALTER TABLE `people` UPDATE `name` = 'Linus' WHERE `id` = 1;",
                "ALTER TABLE `people` DELETE WHERE `id` = 1;",
                "INSERT INTO `people` (`id`, `name`) VALUES (2, 'Grace');",
            ]
        );
        assert!(result.rollback_statements.is_empty());
    }

    #[test]
    fn rejects_clickhouse_key_only_update() {
        let mut id_column = column("id", "UInt64", false, None);
        id_column.is_primary_key = true;
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::ClickHouse),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("default".to_string()),
                table_name: "people".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![id_column, column("name", "String", true, None)]),
            },
            columns: vec!["id".to_string(), "name".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("Ada")]],
            dirty_rows: vec![(0, vec![(0, json!(2))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(
            result.validation_error,
            Some(
                "ClickHouse primary or partition key columns cannot be updated. Change a non-key column before saving."
                    .to_string()
            )
        );
        assert!(result.statements.is_empty());
    }

    #[test]
    fn omits_clickhouse_partition_key_update_assignments() {
        let mut event_date_column = column("event_date", "Date", false, Some("partition_key"));
        event_date_column.is_primary_key = false;
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::ClickHouse),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("default".to_string()),
                table_name: "events".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![
                    column("id", "UInt64", false, None),
                    event_date_column,
                    column("name", "String", true, None),
                ]),
            },
            columns: vec!["id".to_string(), "event_date".to_string(), "name".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("2026-06-24"), json!("Ada")]],
            dirty_rows: vec![(0, vec![(1, json!("2026-06-25")), (2, json!("Linus"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements, vec!["ALTER TABLE `events` UPDATE `name` = 'Linus' WHERE `id` = 1;"]);
    }

    #[test]
    fn rejects_clickhouse_partition_key_only_update() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::ClickHouse),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("default".to_string()),
                table_name: "events".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![
                    column("id", "UInt64", false, None),
                    column("event_date", "Date", false, Some("partition_key")),
                    column("name", "String", true, None),
                ]),
            },
            columns: vec!["id".to_string(), "event_date".to_string(), "name".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("2026-06-24"), json!("Ada")]],
            dirty_rows: vec![(0, vec![(1, json!("2026-06-25"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(
            result.validation_error,
            Some(
                "ClickHouse primary or partition key columns cannot be updated. Change a non-key column before saving."
                    .to_string()
            )
        );
        assert!(result.statements.is_empty());
    }

    #[test]
    fn builds_clickhouse_copy_update_statements() {
        let statements = build_data_grid_copy_update_statements(DataGridCopyUpdateStatementOptions {
            database_type: Some(DatabaseType::ClickHouse),
            identifier_quote: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("default".to_string()),
                table_name: "people".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![column("id", "UInt64", false, None), column("name", "String", true, None)]),
            },
            columns: vec!["id".to_string(), "name".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("Ada")]],
            include_database_name: false,
        });

        assert_eq!(statements, vec!["ALTER TABLE `people` UPDATE `name` = 'Ada' WHERE `id` = 1;"]);
    }

    #[test]
    fn doris_external_catalog_save_and_copy_statements_use_catalog_scope() {
        let table_meta = DataGridTableMeta {
            catalog: Some("iceberg_catalog".to_string()),
            database: None,
            schema: Some("sales".to_string()),
            table_name: "orders".to_string(),
            primary_keys: vec!["id".to_string()],
            columns: Some(vec![column("id", "bigint", false, None), column("status", "varchar", true, None)]),
        };

        let copy_updates = build_data_grid_copy_update_statements(DataGridCopyUpdateStatementOptions {
            database_type: Some(DatabaseType::Doris),
            identifier_quote: None,
            table_meta: table_meta.clone(),
            columns: vec!["id".to_string(), "status".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("paid")]],
            include_database_name: false,
        });
        assert_eq!(
            copy_updates,
            vec!["UPDATE `iceberg_catalog`.`sales`.`orders` SET `status` = 'paid' WHERE `id` = 1;"]
        );

        let copy_insert = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
            database_type: Some(DatabaseType::Doris),
            identifier_quote: None,
            table_meta: Some(table_meta.clone()),
            columns: vec!["id".to_string(), "status".to_string()],
            column_types: None,
            source_columns: None,
            rows: vec![vec![json!(2), json!("new")]],
            primary_key_mode: DataGridPrimaryKeyMode::Include,
            include_computed_columns: false,
            include_database_name: true,
            insert_mode: DataGridCopyInsertMode::Merged,
        });
        assert_eq!(
            copy_insert.as_deref(),
            Some("INSERT INTO `iceberg_catalog`.`sales`.`orders` (`id`, `status`) VALUES (2, 'new');")
        );

        let save = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Doris),
            identifier_quote: None,
            server_version: None,
            table_meta,
            columns: vec!["id".to_string(), "status".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("pending")], vec![json!(3), json!("cancelled")]],
            dirty_rows: vec![(0, vec![(1, json!("paid"))])],
            deleted_rows: vec![1],
            new_rows: vec![vec![json!(4), json!("new")]],
            include_database_name: false,
        });
        assert_eq!(
            save.statements,
            vec![
                "UPDATE `iceberg_catalog`.`sales`.`orders` SET `status` = 'paid' WHERE `id` = 1;",
                "DELETE FROM `iceberg_catalog`.`sales`.`orders` WHERE `id` = 3;",
                "INSERT INTO `iceberg_catalog`.`sales`.`orders` (`id`, `status`) VALUES (4, 'new');",
            ]
        );
        assert!(save.validation_error.is_none());
    }

    #[test]
    fn prepares_databend_keyless_save_statements_with_row_predicate() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Databend),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("default".to_string()),
                table_name: "people".to_string(),
                primary_keys: vec![],
                columns: Some(vec![column("id", "int", true, None), column("name", "string", true, None)]),
            },
            columns: vec!["id".to_string(), "name".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("Ada")]],
            dirty_rows: vec![(0, vec![(1, json!("Linus"))])],
            deleted_rows: vec![0],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(
            result.statements,
            vec![
                "UPDATE `default`.`people` SET `name` = 'Linus' WHERE `id` = 1 AND `name` = 'Ada';",
                "DELETE FROM `default`.`people` WHERE `id` = 1 AND `name` = 'Ada';",
            ]
        );
    }

    #[test]
    fn prepares_oscar_keyless_save_statements_with_schema_qualified_row_predicate() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Oscar),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("SYSDBA".to_string()),
                table_name: "PEOPLE".to_string(),
                primary_keys: vec![],
                columns: Some(vec![column("ID", "INTEGER", true, None), column("NAME", "VARCHAR", true, None)]),
            },
            columns: vec!["ID".to_string(), "NAME".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("Ada")]],
            dirty_rows: vec![(0, vec![(1, json!("Linus"))])],
            deleted_rows: vec![0],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(
            result.statements,
            vec![
                "UPDATE \"SYSDBA\".\"PEOPLE\" SET \"NAME\" = 'Linus' WHERE \"ID\" = 1 AND \"NAME\" = 'Ada';",
                "DELETE FROM \"SYSDBA\".\"PEOPLE\" WHERE \"ID\" = 1 AND \"NAME\" = 'Ada';",
            ]
        );
    }

    #[test]
    fn skips_expression_only_source_columns() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Postgres),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("public".to_string()),
                table_name: "ihli_data".to_string(),
                primary_keys: vec!["iso3".to_string(), "year".to_string()],
                columns: None,
            },
            columns: vec!["iso3".to_string(), "year".to_string(), "country_name".to_string(), "score".to_string()],
            source_columns: Some(vec![
                Some("iso3".to_string()),
                Some("year".to_string()),
                Some("country_name".to_string()),
                None,
            ]),
            rows: vec![vec![json!("LUX"), json!(2007), json!("Luxembourg"), json!(50242.1)]],
            dirty_rows: vec![(0, vec![(2, json!("Luxembourg City")), (3, json!(999))])],
            deleted_rows: vec![],
            new_rows: vec![vec![json!("USA"), json!(2008), json!("United States"), json!(43000)]],
            include_database_name: false,
        });

        assert_eq!(
            result.statements,
            vec![
                r#"UPDATE "public"."ihli_data" SET "country_name" = 'Luxembourg City' WHERE "iso3" = 'LUX' AND "year" = 2007;"#,
                r#"INSERT INTO "public"."ihli_data" ("iso3", "year", "country_name") VALUES ('USA', 2008, 'United States');"#,
            ]
        );
    }

    #[test]
    fn formats_mysql_temporal_columns_by_target_type() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "policies".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![
                    column("id", "int", false, None),
                    column("insurance_start_time", "datetime", true, None),
                    column("raw_text", "varchar(64)", true, None),
                    column("coverage_day", "date", true, None),
                    column("start_clock", "time", true, None),
                ]),
            },
            columns: vec![
                "id".to_string(),
                "insurance_start_time".to_string(),
                "raw_text".to_string(),
                "coverage_day".to_string(),
                "start_clock".to_string(),
            ],
            source_columns: None,
            rows: vec![vec![
                json!(1),
                json!("2026-05-12T00:00:00+00:00"),
                json!("old"),
                json!("2026-05-12T00:00:00+00:00"),
                json!("2026-05-12T09:30:45+00:00"),
            ]],
            dirty_rows: vec![(
                0,
                vec![
                    (1, json!("2026-05-12T00:00:00+00:00")),
                    (2, json!("2026-05-12T00:00:00+00:00")),
                    (3, json!("2026-05-12T00:00:00+00:00")),
                    (4, json!("2026-05-12T09:30:45+00:00")),
                ],
            )],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(
            result.statements,
            vec!["UPDATE `policies` SET `insurance_start_time` = '2026-05-12 00:00:00', `raw_text` = '2026-05-12T00:00:00+00:00', `coverage_day` = '2026-05-12', `start_clock` = '09:30:45' WHERE `id` = 1;"]
        );
    }

    #[test]
    fn mysql_primary_key_text_predicates_do_not_use_binary_comparison() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "school".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![column("id", "varchar(32)", true, None), column("age", "varchar(8)", false, None)]),
            },
            columns: vec!["id".to_string(), "age".to_string()],
            source_columns: None,
            rows: vec![vec![json!("0001492305e412e88086bd582d2678e0"), json!("17")]],
            dirty_rows: vec![(0, vec![(1, json!("18"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(
            result.statements,
            vec!["UPDATE `school` SET `age` = '18' WHERE `id` = '0001492305e412e88086bd582d2678e0';"]
        );
    }

    #[test]
    fn mysql_row_text_predicates_use_binary_comparison_for_width_sensitive_edits() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "parts".to_string(),
                primary_keys: vec![],
                columns: Some(vec![column("code", "varchar(32)", true, None)]),
            },
            columns: vec!["code".to_string()],
            source_columns: None,
            rows: vec![vec![json!("S471355(0)")]],
            dirty_rows: vec![(0, vec![(0, json!("S471355(0)"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(
            result.statements,
            vec!["UPDATE `parts` SET `code` = 'S471355(0)' WHERE BINARY `code` = 'S471355(0)';"]
        );
        assert_eq!(
            result.rollback_statements,
            vec![
                "UPDATE `parts` SET `code` = 'S471355(0)' WHERE BINARY `code` = 'S471355(0)' AND BINARY `code` = 'S471355(0)';"
            ]
        );
    }

    #[test]
    fn prepares_manticore_save_statements_without_trailing_semicolons() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::ManticoreSearch),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "rt_products".to_string(),
                primary_keys: vec![],
                columns: Some(vec![column("id", "bigint", false, None), column("title", "text", true, None)]),
            },
            columns: vec!["id".to_string(), "title".to_string()],
            source_columns: None,
            rows: vec![vec![json!("1"), json!("old")], vec![json!("2"), json!("deleted")]],
            dirty_rows: vec![(0, vec![(1, json!("new"))])],
            deleted_rows: vec![1],
            new_rows: vec![vec![json!("3"), json!("inserted")]],
            include_database_name: false,
        });

        assert_eq!(
            result.statements,
            vec![
                "UPDATE `rt_products` SET `title` = 'new' WHERE `id` = 1 AND `title` = 'old'",
                "DELETE FROM `rt_products` WHERE `id` = 2 AND `title` = 'deleted'",
                "INSERT INTO `rt_products` (`id`, `title`) VALUES (3, 'inserted')",
            ]
        );
        assert!(result.rollback_statements.iter().all(|statement| !statement.ends_with(';')));
    }

    #[test]
    fn validates_duplicate_inserted_primary_keys() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Postgres),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "education_data".to_string(),
                primary_keys: vec!["country_code".to_string(), "year".to_string()],
                columns: None,
            },
            columns: vec!["country_code".to_string(), "year".to_string(), "value".to_string()],
            source_columns: None,
            rows: vec![vec![json!("ALB"), json!(2021), json!(0.812)]],
            dirty_rows: vec![],
            deleted_rows: vec![],
            new_rows: vec![vec![json!("ALB"), json!(2021), json!(0.913)]],
            include_database_name: false,
        });

        assert_eq!(
            result.validation_error,
            Some(r#"New row duplicates the existing primary key (country_code = "ALB", year = 2021). Change the key before saving."#.to_string())
        );
        assert!(result.statements.is_empty());
    }

    fn pk_column(name: &str, data_type: &str, nullable: bool, extra: Option<&str>) -> DataGridColumnInfo {
        DataGridColumnInfo {
            name: name.to_string(),
            data_type: data_type.to_string(),
            is_nullable: nullable,
            is_primary_key: true,
            column_default: None,
            extra: extra.map(ToString::to_string),
        }
    }

    #[test]
    fn prepare_data_grid_save_omits_hidden_oracle_rowid_from_cloned_row_insert() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Oracle),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("APP".to_string()),
                table_name: "TT_PLATFORM_CARS".to_string(),
                primary_keys: vec![DBX_ROWID_COLUMN.to_string()],
                columns: Some(vec![
                    column("ID", "NUMBER", false, None),
                    column("PLATFORM", "VARCHAR2(100)", true, None),
                ]),
            },
            columns: vec!["ID".to_string(), "PLATFORM".to_string(), "__DBX_PK_0".to_string()],
            source_columns: Some(vec![
                Some("ID".to_string()),
                Some("PLATFORM".to_string()),
                Some(DBX_ROWID_COLUMN.to_string()),
            ]),
            rows: vec![],
            dirty_rows: vec![],
            deleted_rows: vec![],
            new_rows: vec![vec![json!(72), json!("轻卡"), Value::Null]],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec![r#"INSERT INTO "APP"."TT_PLATFORM_CARS" ("ID", "PLATFORM") VALUES (72, '轻卡');"#]
        );
    }

    #[test]
    fn prepares_xugu_rowid_updates_deletes_and_inserts_without_writing_synthetic_key() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Xugu),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("APP".to_string()),
                table_name: "ROWID_TABLE".to_string(),
                primary_keys: vec![DBX_ROWID_COLUMN.to_string()],
                columns: Some(vec![
                    column(DBX_ROWID_COLUMN, "ROWID", false, None),
                    column("ID", "INTEGER", false, None),
                    column("VALUE", "VARCHAR(40)", true, None),
                ]),
            },
            columns: vec![DBX_ROWID_COLUMN.to_string(), "ID".to_string(), "VALUE".to_string()],
            source_columns: None,
            rows: vec![vec![json!("AA-1"), json!(1), json!("old")], vec![json!("AA-2"), json!(2), json!("remove")]],
            dirty_rows: vec![(0, vec![(2, json!("new"))])],
            deleted_rows: vec![1],
            new_rows: vec![vec![Value::Null, json!(3), json!("inserted")]],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec![
                "UPDATE \"APP\".\"ROWID_TABLE\" SET \"VALUE\" = 'new' WHERE ROWID = 'AA-1';",
                "DELETE FROM \"APP\".\"ROWID_TABLE\" WHERE ROWID = 'AA-2';",
                "INSERT INTO \"APP\".\"ROWID_TABLE\" (\"ID\", \"VALUE\") VALUES (3, 'inserted');",
            ]
        );
    }

    #[test]
    fn prepare_data_grid_save_skips_sqlite_autoincrement_pk_validation() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Sqlite),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "OnlineLogs".to_string(),
                primary_keys: vec!["OnlineLogId".to_string()],
                columns: Some(vec![
                    pk_column("OnlineLogId", "INTEGER", false, Some("autoincrement")),
                    column("LogTime", "TEXT", false, None),
                ]),
            },
            columns: vec!["OnlineLogId".to_string(), "LogTime".to_string()],
            source_columns: None,
            rows: vec![],
            dirty_rows: vec![],
            deleted_rows: vec![],
            new_rows: vec![vec![Value::Null, json!("2026-06-12T00:00:00Z")]],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements, vec![r#"INSERT INTO "OnlineLogs" ("LogTime") VALUES ('2026-06-12T00:00:00Z');"#]);
    }

    #[test]
    fn prepare_data_grid_save_includes_explicit_sqlite_pk_value() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Sqlite),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "OnlineLogs".to_string(),
                primary_keys: vec!["OnlineLogId".to_string()],
                columns: Some(vec![
                    pk_column("OnlineLogId", "INTEGER", false, Some("autoincrement")),
                    column("LogTime", "TEXT", false, None),
                ]),
            },
            columns: vec!["OnlineLogId".to_string(), "LogTime".to_string()],
            source_columns: None,
            rows: vec![],
            dirty_rows: vec![],
            deleted_rows: vec![],
            new_rows: vec![vec![json!(42), json!("2026-06-12T00:00:00Z")]],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(
            result.statements,
            vec![r#"INSERT INTO "OnlineLogs" ("OnlineLogId", "LogTime") VALUES (42, '2026-06-12T00:00:00Z');"#]
        );
    }

    #[test]
    fn prepare_data_grid_save_omits_empty_mysql_auto_increment_value() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("app".to_string()),
                table_name: "users".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![
                    pk_column("id", "BIGINT", false, Some("auto_increment")),
                    column("name", "VARCHAR", false, None),
                ]),
            },
            columns: vec!["id".to_string(), "name".to_string()],
            source_columns: None,
            rows: vec![],
            dirty_rows: vec![],
            deleted_rows: vec![],
            new_rows: vec![vec![json!(""), json!("Ada")]],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements, vec!["INSERT INTO `app`.`users` (`name`) VALUES ('Ada');"]);
    }

    #[test]
    fn prepare_data_grid_save_omits_mysql_not_null_column_for_before_insert_trigger() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("app".to_string()),
                table_name: "events".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![
                    pk_column("id", "BIGINT", false, Some("auto_increment")),
                    column("trigger_value", "VARCHAR(64)", false, None),
                    column("payload", "VARCHAR(64)", false, None),
                ]),
            },
            columns: vec!["id".to_string(), "trigger_value".to_string(), "payload".to_string()],
            source_columns: None,
            rows: vec![],
            dirty_rows: vec![],
            deleted_rows: vec![],
            new_rows: vec![vec![Value::Null, Value::Null, json!("created")]],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements, vec!["INSERT INTO `app`.`events` (`payload`) VALUES ('created');"]);
        assert!(result.rollback_statements.is_empty());
    }

    #[test]
    fn prepare_data_grid_save_uses_mysql_default_row_insert_for_trigger_only_rows() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("app".to_string()),
                table_name: "trigger_only".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![
                    pk_column("id", "BIGINT", false, Some("auto_increment")),
                    column("required_value", "VARCHAR(64)", false, None),
                ]),
            },
            columns: vec!["id".to_string(), "required_value".to_string()],
            source_columns: None,
            rows: vec![],
            dirty_rows: vec![],
            deleted_rows: vec![],
            new_rows: vec![vec![Value::Null, Value::Null]],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements, vec!["INSERT INTO `app`.`trigger_only` () VALUES ();"]);
        assert!(result.rollback_statements.is_empty());
    }

    #[test]
    fn prepare_data_grid_save_uses_known_mysql_primary_key_for_trigger_rollback() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("app".to_string()),
                table_name: "events".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![
                    pk_column("id", "BIGINT", false, None),
                    column("trigger_value", "VARCHAR(64)", false, None),
                    column("payload", "VARCHAR(64)", false, None),
                ]),
            },
            columns: vec!["id".to_string(), "trigger_value".to_string(), "payload".to_string()],
            source_columns: None,
            rows: vec![],
            dirty_rows: vec![],
            deleted_rows: vec![],
            new_rows: vec![vec![json!(7), Value::Null, json!("created")]],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements, vec!["INSERT INTO `app`.`events` (`id`, `payload`) VALUES (7, 'created');"]);
        assert_eq!(result.rollback_statements, vec!["DELETE FROM `app`.`events` WHERE `id` = 7;"]);
    }

    #[test]
    fn prepare_data_grid_save_still_rejects_mysql_null_update() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Mysql),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("app".to_string()),
                table_name: "events".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![
                    pk_column("id", "BIGINT", false, Some("auto_increment")),
                    column("trigger_value", "VARCHAR(64)", false, None),
                ]),
            },
            columns: vec!["id".to_string(), "trigger_value".to_string()],
            source_columns: None,
            rows: vec![vec![json!(1), json!("existing")]],
            dirty_rows: vec![(0, vec![(1, Value::Null)])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, Some(r#"Column "trigger_value" does not allow NULL."#.to_string()));
        assert!(result.statements.is_empty());
    }

    #[test]
    fn prepare_data_grid_save_omits_empty_kingbase_sqlserver_identity_value() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Kingbase),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: Some("dbo".to_string()),
                table_name: "orders".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: Some(vec![
                    pk_column("id", "int", false, Some("identity(1,1)")),
                    column("name", "varchar", false, None),
                ]),
            },
            columns: vec!["id".to_string(), "name".to_string()],
            source_columns: None,
            rows: vec![],
            dirty_rows: vec![],
            deleted_rows: vec![],
            new_rows: vec![vec![Value::Null, json!("Ada")]],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, None);
        assert_eq!(result.statements, vec![r#"INSERT INTO "dbo"."orders" ("name") VALUES ('Ada');"#]);
    }

    #[test]
    fn prepare_data_grid_save_still_validates_other_not_null_columns_in_sqlite() {
        let result = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Sqlite),
            identifier_quote: None,
            server_version: None,
            table_meta: DataGridTableMeta {
                catalog: None,
                database: None,
                schema: None,
                table_name: "OnlineLogs".to_string(),
                primary_keys: vec!["OnlineLogId".to_string()],
                columns: Some(vec![
                    pk_column("OnlineLogId", "INTEGER", false, Some("autoincrement")),
                    column("LogTime", "TEXT", false, None),
                ]),
            },
            columns: vec!["OnlineLogId".to_string(), "LogTime".to_string()],
            source_columns: None,
            rows: vec![],
            dirty_rows: vec![],
            deleted_rows: vec![],
            new_rows: vec![vec![Value::Null, Value::Null]],
            include_database_name: false,
        });

        assert_eq!(result.validation_error, Some(r#"Column "LogTime" does not allow NULL."#.to_string()));
        assert!(result.statements.is_empty());
    }

    #[test]
    fn opaque_aggregate_state_blocks_keyless_predicates_and_copy_sql() {
        let table_meta = DataGridTableMeta {
            catalog: None,
            database: Some("analytics".to_string()),
            schema: None,
            table_name: "states".to_string(),
            primary_keys: vec![],
            columns: Some(vec![
                column("name", "varchar", false, None),
                column("v2", "agg_state<sum(int)>", false, None),
            ]),
        };
        let save = prepare_data_grid_save(DataGridSaveStatementOptions {
            database_type: Some(DatabaseType::Doris),
            identifier_quote: None,
            server_version: None,
            table_meta: table_meta.clone(),
            columns: vec!["name".to_string(), "v2".to_string()],
            source_columns: None,
            rows: vec![vec![json!("before"), json!("0x00ff")]],
            dirty_rows: vec![(0, vec![(0, json!("after"))])],
            deleted_rows: vec![],
            new_rows: vec![],
            include_database_name: false,
        });
        assert!(save.validation_error.as_deref().is_some_and(|error| error.contains("keyless row")));
        assert!(save.statements.is_empty());

        assert!(build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
            database_type: Some(DatabaseType::Doris),
            identifier_quote: None,
            table_meta: Some(table_meta.clone()),
            columns: vec!["name".to_string(), "v2".to_string()],
            column_types: None,
            source_columns: None,
            rows: vec![vec![json!("before"), json!("0x00ff")]],
            primary_key_mode: DataGridPrimaryKeyMode::Include,
            include_computed_columns: false,
            include_database_name: true,
            insert_mode: DataGridCopyInsertMode::Merged,
        })
        .is_none());

        let mut keyed = table_meta;
        keyed.primary_keys = vec!["name".to_string()];
        assert!(build_data_grid_copy_update_statements(DataGridCopyUpdateStatementOptions {
            database_type: Some(DatabaseType::Doris),
            identifier_quote: None,
            table_meta: keyed,
            columns: vec!["name".to_string(), "v2".to_string()],
            source_columns: None,
            rows: vec![vec![json!("before"), json!("0x00ff")]],
            include_database_name: true,
        })
        .is_empty());
    }
}