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。

602 KB
Raw Blame
pub mod batch_progress;
pub use dbx_drivers::query_messages;
pub mod document_ops;
pub mod hbase_ops;
pub mod mongo_ops;
pub mod mysql_data_charset_policy;
#[cfg(test)]
mod mysql_data_charset_tests;
pub mod object_cache;
pub mod pl_debug;
pub mod plugin_data;
pub mod plugin_plan;
pub mod query_cancel;
pub mod redis_ops;
pub mod snapshot_export;
mod sqlserver_agent_batch;
pub mod sqlserver_manual_transaction;
pub mod two_phase_commit;

pub use dbx_drivers::execution::{
    await_stream_with_progress_timeout, canceled_error, is_canceled, lock_shared_client_with_wait,
    query_timeout_duration, resolve_query_timeout, timeout_error, timeout_error_for, wait_for_query,
    wait_for_query_opt, wait_for_query_with_timeout, wait_for_result_opt, wait_for_result_with_timeout,
    wait_for_value_opt, DbOperationBudget, StreamProgressClock, MAX_ROWS, QUERY_CANCELED, QUERY_TIMEOUT,
};

use futures::StreamExt;
use mysql_async::prelude::Queryable;
use serde::{Deserialize, Serialize};
use sqlparser::ast::{
    visit_relations_mut, Ident, ObjectName, ObjectNamePart, ObjectType, Statement, TableFactor, VisitMut, VisitorMut,
};
use sqlparser::dialect::{GenericDialect, MsSqlDialect, PostgreSqlDialect};
use sqlparser::parser::Parser;
use std::collections::{HashMap, HashSet};
use std::ops::ControlFlow;
use std::sync::{Arc, Condvar, Mutex};
use std::time::Duration;
use tokio_util::sync::CancellationToken;

use crate::agent_recovery::{RecoveryDecision, RecoveryPolicy, RecoveryScope};
use crate::connection::{AppState, PoolKind, TransactionSession, TxnConnection};
use crate::database_capabilities;
use crate::db;
use crate::db::agent_driver::{AgentCallError, AgentErrorStage, AgentOperationOutcome};
use crate::models::connection::{ConnectionConfig, DatabaseType};
use crate::query_execution_sql::{is_oracle_proven_read_only_statement, is_write_sql, strip_sql_comments_and_literals};
use crate::sql::{split_sql_batches, split_sql_statements, starts_with_executable_sql_keyword_for_database};
use crate::sql_dialect::{quote_iris_identifier, resolve_for_db, CAP_TRANSACTIONAL_DDL};
use crate::sql_risk::{classify_sql_risk_for_database, SqlRisk};

pub const AGENT_PROTOCOL_MAX_ROWS: usize = i32::MAX as usize;
pub const METADATA_POOL_BUSY_ERROR: &str = "DBX metadata pool is busy; please retry";
pub const MANUAL_TRANSACTION_IDLE_TIMEOUT_SECS: u64 = 300;
pub const MANUAL_TRANSACTION_SESSION_NOT_FOUND_ERROR: &str =
    "Transaction session not found or expired; it may have been auto-rolled back due to inactivity";
pub const MANUAL_TRANSACTION_IDLE_ROLLBACK_ERROR: &str = "Transaction was auto-rolled back due to inactivity";

pub fn is_manual_transaction_session_expired_error(error: &str) -> bool {
    error.starts_with(MANUAL_TRANSACTION_SESSION_NOT_FOUND_ERROR) || error == MANUAL_TRANSACTION_IDLE_ROLLBACK_ERROR
}

/// Returns true when a metadata request failed because all pool/client slots
/// were temporarily occupied. This is deliberately separate from
/// `is_connection_error`: a saturated pool is healthy and must not trigger a
/// shared-pool reconnect.
pub fn is_pool_saturation_error(err: &str) -> bool {
    let lower = err.to_lowercase();
    lower.contains("connection pool checkout timed out [stage=wait") || lower.contains("dbx metadata pool is busy")
}
/// Fallback when a Mongo connection hits the generic SQL executor instead of the shell path.
/// Wording must match packages/mongo-shell `MONGO_SHELL_COMMAND_HINT`
/// (desktop/CLI diagnose first; this is only the Rust SQL-executor backstop).
const MONGO_SHELL_COMMAND_HINT: &str = "Use MongoDB shell-style commands, for example: db.collection.find({}).limit(100), db.collection.aggregate([]), db.collection.aggregate([], { explain: true }), db.version(), db.collection.countDocuments({}), db.collection.distinct(\"field\"), db.collection.getIndexes(), db.collection.createIndex({...}), db.createUser({...}), or db.collection.insertOne({...}).";
const SQL_OMITTED_ERROR_CONTEXT: &str =
    "SQL text omitted from user-facing error; enable debug SQL diagnostics to inspect the original statement.";

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PoolErrorAction {
    Keep,
    Discard,
    ReconnectAndRetry,
}

#[derive(Debug, Clone)]
pub enum QueryExecutionError {
    Agent(AgentCallError),
    DuckDb {
        code: String,
        message: String,
    },
    Canceled {
        stage: AgentErrorStage,
        operation_outcome: AgentOperationOutcome,
    },
    Timeout(String),
    Sql(String),
    /// Native PostgreSQL SQL failure whose driver-reported cursor position was
    /// resolved against the executed statement text. Kept distinct from
    /// [`Self::Sql`] so the position survives `classify_query_error` and reaches
    /// [`Self::into_backend_error`] as a typed field instead of being parsed back
    /// out of a string.
    SqlWithPosition {
        message: String,
        position: crate::sql_error_position::SqlErrorPosition,
    },
    Legacy(String),
}

impl QueryExecutionError {
    pub fn into_legacy_string(self) -> String {
        let mut message = match self {
            Self::Agent(error) => error.into_legacy_string(),
            Self::DuckDb { message, .. } => message,
            Self::Canceled { .. } => canceled_error(),
            Self::Timeout(error) => error,
            Self::Sql(error) => error,
            Self::SqlWithPosition { message, .. } => message,
            Self::Legacy(error) => error,
        };
        // Defensive: any remaining transport marker (e.g. from a driver error
        // that never passed through the resolve step) must not reach clients.
        while crate::sql_error_position::take_marker(&mut message).is_some() {}
        message
    }

    pub fn into_backend_error(self) -> crate::backend_error::BackendError {
        match self {
            Self::Agent(error) => crate::backend_error::BackendError::from_agent_call_error(&error),
            Self::DuckDb { code, message } => {
                crate::backend_error::BackendError::from_duckdb_worker_error(&code, &message)
            }
            Self::Canceled { stage, operation_outcome } => {
                crate::backend_error::BackendError::from_canceled(stage, operation_outcome)
            }
            Self::Timeout(error) => crate::backend_error::BackendError::from_timeout_detail(&error),
            Self::Sql(error) => crate::backend_error::BackendError::from_sql_detail(&error),
            Self::SqlWithPosition { message, position } => {
                crate::backend_error::BackendError::from_sql_detail_with_position(&message, position)
            }
            Self::Legacy(error) => crate::backend_error::BackendError::from_legacy_string(&error),
        }
    }

    fn with_omitted_sql_context(self, sql: &str) -> Self {
        match self {
            Self::Agent(error) => Self::Agent(error),
            Self::DuckDb { code, message } => {
                Self::DuckDb { code, message: query_error_with_omitted_sql_context(&message, sql) }
            }
            canceled @ Self::Canceled { .. } => canceled,
            Self::Timeout(error) => Self::Timeout(query_error_with_omitted_sql_context(&error, sql)),
            Self::Sql(error) => Self::Sql(append_typed_sql_error_context(&error, sql)),
            Self::SqlWithPosition { message, position } => {
                Self::SqlWithPosition { message: append_typed_sql_error_context(&message, sql), position }
            }
            Self::Legacy(error) => Self::Legacy(query_error_with_omitted_sql_context(&error, sql)),
        }
    }

    fn with_context(self, context: &str) -> Self {
        match self {
            Self::Agent(error) => Self::Agent(error),
            Self::DuckDb { code, message } => Self::DuckDb { code, message: format!("{message}; {context}") },
            canceled @ Self::Canceled { .. } => canceled,
            Self::Timeout(error) => Self::Timeout(format!("{error}; {context}")),
            Self::Sql(error) => Self::Sql(format!("{error}; {context}")),
            Self::SqlWithPosition { message, position } => {
                Self::SqlWithPosition { message: format!("{message}; {context}"), position }
            }
            Self::Legacy(error) => Self::Legacy(format!("{error}; {context}")),
        }
    }

    fn as_agent_error(&self) -> Option<&AgentCallError> {
        match self {
            Self::Agent(error) => Some(error),
            Self::DuckDb { .. }
            | Self::Canceled { .. }
            | Self::Timeout(_)
            | Self::Sql(_)
            | Self::SqlWithPosition { .. }
            | Self::Legacy(_) => None,
        }
    }
}

impl std::fmt::Display for QueryExecutionError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Agent(error) => error.fmt(formatter),
            Self::DuckDb { message, .. } => formatter.write_str(message),
            Self::Canceled { .. } => formatter.write_str(QUERY_CANCELED),
            Self::Timeout(error) => formatter.write_str(error),
            Self::Sql(error) => formatter.write_str(error),
            Self::SqlWithPosition { message, .. } => formatter.write_str(message),
            Self::Legacy(error) => formatter.write_str(error),
        }
    }
}

impl From<AgentCallError> for QueryExecutionError {
    fn from(error: AgentCallError) -> Self {
        Self::Agent(error)
    }
}

impl From<String> for QueryExecutionError {
    fn from(error: String) -> Self {
        Self::Legacy(error)
    }
}

impl From<&str> for QueryExecutionError {
    fn from(error: &str) -> Self {
        Self::Legacy(error.to_string())
    }
}

fn query_error_with_omitted_sql_context(error: &str, _sql: &str) -> String {
    crate::db::agent_driver::append_legacy_error_context(error, SQL_OMITTED_ERROR_CONTEXT)
}

fn append_typed_sql_error_context(error: &str, _sql: &str) -> String {
    if error.contains(SQL_OMITTED_ERROR_CONTEXT) {
        return error.to_string();
    }
    let separator = if error.trim_start().starts_with("Server error:") { " " } else { "\n" };
    format!("{error}{separator}{SQL_OMITTED_ERROR_CONTEXT}")
}

/// A multi-statement result with metadata intended for query clients.
///
/// `execution_error` is emitted for synthesized per-statement errors so clients
/// can distinguish them from a successful result column named `Error`.
/// `statement_index` is emitted only after a concrete statement starts running.
/// `server_message` is emitted only for SQL Server TDS informational messages.
#[derive(Debug, Clone, Serialize)]
pub struct ExecuteMultiResult {
    #[serde(flatten)]
    pub result: db::QueryResult,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub large_value_cells: Vec<db::LargeValueCell>,
    #[serde(skip_serializing_if = "is_false")]
    pub execution_error: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub statement_index: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<crate::backend_error::BackendError>,
    #[serde(skip_serializing_if = "is_false")]
    pub server_message: bool,
    /// Manual-transaction UX metadata for sticky proven-read-only dialects
    /// (Oracle, OceanBase-Oracle, MySQL, PostgreSQL): true only for a statement
    /// proven to be an ordinary read by that dialect's strict heuristic.
    /// Absent/false for unproven statements and non-participating dialects.
    /// Not part of the reusable database-result model (`db::QueryResult`).
    #[serde(skip_serializing_if = "is_false")]
    pub manual_transaction_proven_read_only: bool,
    /// Manual-transaction UX metadata for the same dialects: true on the
    /// synthetic successful result when the manual-execution splitter found zero
    /// statements (empty/whitespace/comments-only script). Lets the frontend
    /// treat it as a no-op rather than an unproven statement.
    #[serde(skip_serializing_if = "is_false")]
    pub manual_transaction_no_statement: bool,
    /// MySQL auto-commit tab: set on every result of the batch when the tab's
    /// connection was settled. `Some(true)` means the connection still holds a
    /// transaction the user opened explicitly and DBX kept it open
    /// (`preserve_explicit_transaction`); `Some(false)` means the settlement ran
    /// and no such transaction is open. `None` means this execution never
    /// observed a tab-scoped MySQL connection, so it says nothing about the
    /// tab's state. The frontend mirrors `Some(..)` into the tab's transaction
    /// badge/actions so the state is never silent.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auto_commit_open_transaction: Option<bool>,
    /// MySQL auto-commit tab: set on every result of the batch when DBX rolled
    /// back a transaction the user opened explicitly (`BEGIN` /
    /// `START TRANSACTION`) and left open. Lets the UI report the cleanup
    /// instead of discarding the transaction silently.
    #[serde(skip_serializing_if = "is_false")]
    pub auto_commit_explicit_transaction_rolled_back: bool,
    /// MySQL auto-commit tab: set on every result of the batch when DBX rolled
    /// back a transaction nobody opened explicitly — the session turned
    /// auto-commit off (`SET autocommit = 0`), so its transactions are implicit.
    /// Reported separately from the explicit case: the user never asked for a
    /// transaction, so the tab shows a distinct notice that is raised once per
    /// connection instead of repeating "your explicit transaction was rolled
    /// back" after every execution.
    #[serde(skip_serializing_if = "is_false")]
    pub auto_commit_session_autocommit_rolled_back: bool,
}

#[derive(Debug, Clone, PartialEq)]
pub struct ExecuteMultiProgress {
    pub statement_index: usize,
    pub completed: usize,
    pub total: usize,
    pub success: bool,
    pub execution_time_ms: u128,
    pub affected_rows: u64,
    pub error: Option<crate::backend_error::BackendError>,
}

pub type ExecuteMultiProgressCallback = Arc<dyn Fn(ExecuteMultiProgress) + Send + Sync>;

fn report_execute_multi_progress(
    progress: Option<&ExecuteMultiProgressCallback>,
    statement_index: usize,
    total: usize,
    result: &db::QueryResult,
    success: bool,
    error: Option<crate::backend_error::BackendError>,
) {
    if let Some(progress) = progress {
        progress(ExecuteMultiProgress {
            statement_index,
            completed: statement_index + 1,
            total,
            success,
            execution_time_ms: result.execution_time_ms,
            affected_rows: result.affected_rows,
            error,
        });
    }
}

impl ExecuteMultiResult {
    fn execution_error(result: db::QueryResult) -> Self {
        let error = error_from_query_result(&result);
        Self {
            result,
            large_value_cells: Vec::new(),
            execution_error: true,
            statement_index: None,
            error,
            server_message: false,
            manual_transaction_proven_read_only: false,
            manual_transaction_no_statement: false,
            auto_commit_open_transaction: None,
            auto_commit_explicit_transaction_rolled_back: false,
            auto_commit_session_autocommit_rolled_back: false,
        }
    }

    #[cfg(test)]
    fn execution_error_with_index(result: db::QueryResult, statement_index: usize) -> Self {
        let error = error_from_query_result(&result);
        Self {
            result,
            large_value_cells: Vec::new(),
            execution_error: true,
            statement_index: Some(statement_index),
            error,
            server_message: false,
            manual_transaction_proven_read_only: false,
            manual_transaction_no_statement: false,
            auto_commit_open_transaction: None,
            auto_commit_explicit_transaction_rolled_back: false,
            auto_commit_session_autocommit_rolled_back: false,
        }
    }

    fn execution_error_with_backend(
        result: db::QueryResult,
        statement_index: Option<usize>,
        error: crate::backend_error::BackendError,
    ) -> Self {
        Self {
            result,
            large_value_cells: Vec::new(),
            execution_error: true,
            statement_index,
            error: Some(error),
            server_message: false,
            manual_transaction_proven_read_only: false,
            manual_transaction_no_statement: false,
            auto_commit_open_transaction: None,
            auto_commit_explicit_transaction_rolled_back: false,
            auto_commit_session_autocommit_rolled_back: false,
        }
    }

    fn success_with_index(mut result: db::QueryResult, statement_index: usize) -> Self {
        let large_value_cells = std::mem::take(&mut result.deferred_large_value_cells);
        Self {
            result,
            large_value_cells,
            execution_error: false,
            statement_index: Some(statement_index),
            error: None,
            server_message: false,
            manual_transaction_proven_read_only: false,
            manual_transaction_no_statement: false,
            auto_commit_open_transaction: None,
            auto_commit_explicit_transaction_rolled_back: false,
            auto_commit_session_autocommit_rolled_back: false,
        }
    }

    fn success_with_index_and_large_values(
        result: db::QueryResult,
        statement_index: usize,
        large_value_cells: Vec<db::LargeValueCell>,
        table_data_preview: bool,
    ) -> Self {
        Self::success_with_index_and_large_values_and_charset(
            result,
            statement_index,
            large_value_cells,
            table_data_preview,
            None,
        )
    }

    fn success_with_index_and_large_values_and_charset(
        mut result: db::QueryResult,
        statement_index: usize,
        mut large_value_cells: Vec<db::LargeValueCell>,
        table_data_preview: bool,
        data_charset: Option<db::mysql::MySqlDataCharset>,
    ) -> Self {
        large_value_cells =
            merge_large_value_cells(large_value_cells, std::mem::take(&mut result.deferred_large_value_cells));
        let server_cells = if table_data_preview {
            remap_large_value_cells_around_server_markers(&result, &mut large_value_cells);
            extract_server_large_value_markers_with_charset(&mut result, data_charset)
        } else {
            Vec::new()
        };
        Self {
            result,
            large_value_cells: merge_large_value_cells(large_value_cells, server_cells),
            execution_error: false,
            statement_index: Some(statement_index),
            error: None,
            server_message: false,
            manual_transaction_proven_read_only: false,
            manual_transaction_no_statement: false,
            auto_commit_open_transaction: None,
            auto_commit_explicit_transaction_rolled_back: false,
            auto_commit_session_autocommit_rolled_back: false,
        }
    }

    fn success_with_index_and_optional_server_large_values(
        result: db::QueryResult,
        statement_index: usize,
        table_data_preview: bool,
    ) -> Self {
        Self::success_with_index_and_large_values(result, statement_index, Vec::new(), table_data_preview)
    }

    fn success_with_optional_server_large_values(result: db::QueryResult, table_data_preview: bool) -> Self {
        Self::success_with_optional_server_large_values_and_charset(result, table_data_preview, None)
    }

    fn success_with_optional_server_large_values_and_charset(
        mut result: db::QueryResult,
        table_data_preview: bool,
        data_charset: Option<db::mysql::MySqlDataCharset>,
    ) -> Self {
        let mut deferred_cells = std::mem::take(&mut result.deferred_large_value_cells);
        let large_value_cells = if table_data_preview {
            remap_large_value_cells_around_server_markers(&result, &mut deferred_cells);
            extract_server_large_value_markers_with_charset(&mut result, data_charset)
        } else {
            Vec::new()
        };
        let large_value_cells = merge_large_value_cells(large_value_cells, deferred_cells);
        Self {
            result,
            large_value_cells,
            execution_error: false,
            statement_index: None,
            error: None,
            server_message: false,
            manual_transaction_proven_read_only: false,
            manual_transaction_no_statement: false,
            auto_commit_open_transaction: None,
            auto_commit_explicit_transaction_rolled_back: false,
            auto_commit_session_autocommit_rolled_back: false,
        }
    }

    fn with_manual_transaction_proven_read_only(mut self) -> Self {
        self.manual_transaction_proven_read_only = true;
        self
    }

    fn with_manual_transaction_no_statement(mut self) -> Self {
        self.manual_transaction_no_statement = true;
        self
    }

    pub fn without_error_detail(mut self) -> Self {
        self.error = self.error.map(crate::backend_error::BackendError::without_detail);
        self
    }

    fn into_query_result(self) -> db::QueryResult {
        self.result
    }
}

const SERVER_LARGE_VALUE_UNKNOWN_BYTES: usize = 64 * 1024 * 1024;

#[derive(Clone, Copy, PartialEq, Eq)]
enum ServerLargeValuePreviewKind {
    Text,
    Binary,
    Vector,
    Deferred,
}

#[derive(Clone, Copy)]
struct ServerLargeValueMarker {
    result_index: usize,
    source_index: usize,
    preview_kind: Option<ServerLargeValuePreviewKind>,
    source_type: Option<&'static str>,
}

#[derive(Clone, Copy)]
struct ServerLargeValueMarkerValue<'a> {
    kind: ServerLargeValuePreviewKind,
    preview_size: usize,
    original_bytes: Option<usize>,
    value_ref: Option<&'a str>,
}

/// Some PostgreSQL-compatible servers (for example KingbaseES instances with
/// case-insensitive identifiers) fold even quoted column aliases, so the
/// internal preview marker prefix must be matched ASCII case-insensitively.
fn strip_large_value_marker_prefix(column: &str) -> Option<&str> {
    let prefix = crate::sql_dialect::DBX_LARGE_VALUE_BYTES_COLUMN_PREFIX;
    if column.len() < prefix.len() {
        return None;
    }
    let head = column.get(..prefix.len())?;
    if !head.eq_ignore_ascii_case(prefix) {
        return None;
    }
    column.get(prefix.len()..)
}

fn server_large_value_alias(
    suffix: &str,
) -> Option<(usize, Option<ServerLargeValuePreviewKind>, Option<&'static str>)> {
    if let Ok(source_index) = suffix.parse::<usize>() {
        return Some((source_index, None, None));
    }
    let (kind, source_index) = suffix.split_once('_')?;
    let source_index = source_index.parse::<usize>().ok()?;
    let (preview_kind, source_type) = match kind {
        "T" | "t" => (ServerLargeValuePreviewKind::Text, None),
        "B" | "b" => (ServerLargeValuePreviewKind::Binary, None),
        "V" | "v" => (ServerLargeValuePreviewKind::Vector, Some("vector")),
        "J" | "j" => (ServerLargeValuePreviewKind::Text, Some("json")),
        "K" | "k" => (ServerLargeValuePreviewKind::Text, Some("jsonb")),
        "S" | "s" => (ServerLargeValuePreviewKind::Text, Some("tsvector")),
        "C" | "c" => (ServerLargeValuePreviewKind::Deferred, Some("clob")),
        "N" | "n" => (ServerLargeValuePreviewKind::Deferred, Some("nclob")),
        "L" | "l" => (ServerLargeValuePreviewKind::Deferred, Some("blob")),
        "F" | "f" => (ServerLargeValuePreviewKind::Deferred, Some("bfile")),
        _ => return None,
    };
    Some((source_index, Some(preview_kind), source_type))
}

fn server_large_value_marker(value: &serde_json::Value) -> Option<ServerLargeValueMarkerValue<'_>> {
    let mut parts = value.as_str()?.split(':');
    let kind = parts.next()?;
    let preview_size = parts.next()?;
    let kind = match kind {
        "T" => ServerLargeValuePreviewKind::Text,
        "B" => ServerLargeValuePreviewKind::Binary,
        "V" => ServerLargeValuePreviewKind::Vector,
        "D" => ServerLargeValuePreviewKind::Deferred,
        _ => return None,
    };
    let original_bytes = parts.next().and_then(|value| value.parse::<usize>().ok());
    let value_ref = parts.next().filter(|value| !value.is_empty());
    Some(ServerLargeValueMarkerValue {
        kind,
        preview_size: preview_size.parse::<usize>().ok()?.max(1),
        original_bytes,
        value_ref,
    })
}

fn truncate_server_large_value_preview(
    value: &mut serde_json::Value,
    kind: ServerLargeValuePreviewKind,
    preview_size: usize,
) -> bool {
    let serde_json::Value::String(text) = value else {
        return false;
    };
    if matches!(kind, ServerLargeValuePreviewKind::Deferred) {
        return true;
    }
    if matches!(kind, ServerLargeValuePreviewKind::Vector) {
        let truncated = text.chars().count() > preview_size;
        let vector_text = if truncated {
            let prefix: String = text.chars().take(preview_size).collect();
            let Some(last_separator) = prefix.rfind(',') else {
                return false;
            };
            format!("{}]", &prefix[..last_separator])
        } else {
            text.clone()
        };
        let Ok(vector) = serde_json::from_str::<Vec<serde_json::Value>>(&vector_text) else {
            return false;
        };
        *value = serde_json::Value::Array(vector);
        return truncated;
    }
    let truncate_at = match kind {
        ServerLargeValuePreviewKind::Text => text.char_indices().nth(preview_size).map(|(index, _)| index),
        ServerLargeValuePreviewKind::Binary => text
            .strip_prefix("0x")
            .or_else(|| text.strip_prefix("0X"))
            // PostgreSQL text-protocol queries (including rows with timestamps)
            // return bytea as \x hex instead of the binary driver's 0x form.
            .or_else(|| text.strip_prefix("\\x"))
            .filter(|hex| hex.len() > preview_size.saturating_mul(2))
            .map(|_| 2usize.saturating_add(preview_size.saturating_mul(2))),
        ServerLargeValuePreviewKind::Vector | ServerLargeValuePreviewKind::Deferred => unreachable!(),
    };
    let Some(truncate_at) = truncate_at else {
        return false;
    };
    text.truncate(truncate_at);
    text.push_str("...");
    true
}

fn server_large_value_markers(result: &db::QueryResult) -> Vec<ServerLargeValueMarker> {
    let mut markers = Vec::new();
    for (result_index, column) in result.columns.iter().enumerate() {
        let Some((source_index, preview_kind, source_type)) =
            strip_large_value_marker_prefix(column).and_then(server_large_value_alias)
        else {
            continue;
        };
        let expected_source_index = result_index.checked_sub(markers.len() + 1);
        if expected_source_index == Some(source_index) {
            markers.push(ServerLargeValueMarker { result_index, source_index, preview_kind, source_type });
        }
    }
    markers
}

#[cfg(test)]
fn extract_server_large_value_markers(result: &mut db::QueryResult) -> Vec<db::LargeValueCell> {
    extract_server_large_value_markers_with_charset(result, None)
}

fn extract_server_large_value_markers_with_charset(
    result: &mut db::QueryResult,
    data_charset: Option<db::mysql::MySqlDataCharset>,
) -> Vec<db::LargeValueCell> {
    let markers = server_large_value_markers(result);
    if markers.is_empty() {
        return Vec::new();
    }

    let mut large_value_cells = Vec::new();
    for marker in &markers {
        let source_result_index = marker.result_index.saturating_sub(1);
        let row_kind = result
            .rows
            .iter()
            .find_map(|row| row.get(marker.result_index).and_then(server_large_value_marker).map(|value| value.kind));
        let source_type = marker.source_type.or_else(|| {
            (marker.preview_kind.or(row_kind) == Some(ServerLargeValuePreviewKind::Vector)).then_some("vector")
        });
        if let (Some(source_type), Some(column_type)) = (source_type, result.column_types.get_mut(source_result_index))
        {
            *column_type = source_type.to_string();
        }
    }
    for (row_index, row) in result.rows.iter_mut().enumerate() {
        for marker in &markers {
            // Own the locator before mutating the preview in this row.
            let value_ref = row
                .get(marker.result_index)
                .and_then(server_large_value_marker)
                .and_then(|value| value.value_ref)
                .map(str::to_owned);
            let marker_value = row
                .get(marker.result_index)
                .and_then(server_large_value_marker)
                .map(|value| (value.kind, value.preview_size, value.original_bytes));
            let source_result_index = marker.result_index.saturating_sub(1);
            // The driver preserves this exact marker-adjacent text carrier.
            // LEFT's extra character proves server truncation; never decode a
            // prefix whose unreceived suffix may invalidate the whole value.
            let pending_decode = data_charset.is_some()
                && marker.preview_kind == Some(ServerLargeValuePreviewKind::Text)
                && marker.source_type.is_none()
                && marker_value.is_some_and(|value| {
                    value.0 == ServerLargeValuePreviewKind::Text
                        && row
                            .get(source_result_index)
                            .and_then(serde_json::Value::as_str)
                            .is_some_and(|text| text.chars().count() > value.1)
                });
            let truncated = marker_value.is_some_and(|value| {
                row.get_mut(source_result_index)
                    .is_some_and(|source| truncate_server_large_value_preview(source, value.0, value.1))
            });
            if pending_decode {
                // An empty payload cannot accidentally masquerade as a decoded
                // prefix. Consumers must materialize the marked cell to copy it.
                row[source_result_index] = serde_json::Value::String(String::new());
            } else if let Some(codec) = data_charset.filter(|_| {
                marker.preview_kind == Some(ServerLargeValuePreviewKind::Text) && marker.source_type.is_none()
            }) {
                if let Some(serde_json::Value::String(text)) = row.get_mut(source_result_index) {
                    *text = codec.interpret_text(text);
                }
            }
            if truncated {
                large_value_cells.push(db::LargeValueCell {
                    row_index,
                    column_index: marker.source_index,
                    original_bytes: marker_value.and_then(|value| value.2).unwrap_or(SERVER_LARGE_VALUE_UNKNOWN_BYTES),
                    value_ref,
                    pending_decode: pending_decode.then_some(true),
                });
            }
        }
    }

    let removed: std::collections::HashSet<usize> = markers.iter().map(|marker| marker.result_index).collect();
    let retained_index = |index: usize| index - removed.iter().filter(|removed_index| **removed_index < index).count();
    result.raw_key_cells.retain_mut(|cell| {
        if removed.contains(&cell.column_index) {
            return false;
        }
        cell.column_index = retained_index(cell.column_index);
        true
    });
    result.spatial_columns.retain_mut(|column| {
        if removed.contains(&column.column_index) {
            return false;
        }
        column.column_index = retained_index(column.column_index);
        true
    });
    for row in &mut result.rows {
        let mut index = 0;
        row.retain(|_| {
            let keep = !removed.contains(&index);
            index += 1;
            keep
        });
    }
    for row in &mut result.spatial_values {
        let mut index = 0;
        row.retain(|_| {
            let keep = !removed.contains(&index);
            index += 1;
            keep
        });
    }
    let mut index = 0;
    result.columns.retain(|_| {
        let keep = !removed.contains(&index);
        index += 1;
        keep
    });
    let mut index = 0;
    result.column_types.retain(|_| {
        let keep = !removed.contains(&index);
        index += 1;
        keep
    });
    let mut index = 0;
    result.column_sortables.retain(|_| {
        let keep = !removed.contains(&index);
        index += 1;
        keep
    });
    large_value_cells
}

fn remap_large_value_cells_around_server_markers(result: &db::QueryResult, cells: &mut Vec<db::LargeValueCell>) {
    let removed: std::collections::HashSet<usize> =
        server_large_value_markers(result).into_iter().map(|marker| marker.result_index).collect();
    if removed.is_empty() {
        return;
    }
    cells.retain_mut(|cell| {
        if removed.contains(&cell.column_index) {
            return false;
        }
        cell.column_index -= removed.iter().filter(|index| **index < cell.column_index).count();
        true
    });
}

fn merge_large_value_cells(
    mut driver_cells: Vec<db::LargeValueCell>,
    server_cells: Vec<db::LargeValueCell>,
) -> Vec<db::LargeValueCell> {
    if driver_cells.is_empty() {
        return server_cells;
    }
    if server_cells.is_empty() {
        return driver_cells;
    }
    let mut driver_indexes = driver_cells
        .iter()
        .enumerate()
        .map(|(index, cell)| ((cell.row_index, cell.column_index), index))
        .collect::<HashMap<_, _>>();
    for server_cell in server_cells {
        let key = (server_cell.row_index, server_cell.column_index);
        if let Some(index) = driver_indexes.get(&key).copied() {
            driver_cells[index] = server_cell;
        } else {
            driver_indexes.insert(key, driver_cells.len());
            driver_cells.push(server_cell);
        }
    }
    driver_cells
}

impl From<db::QueryResult> for ExecuteMultiResult {
    fn from(mut result: db::QueryResult) -> Self {
        let large_value_cells = std::mem::take(&mut result.deferred_large_value_cells);
        Self {
            result,
            large_value_cells,
            execution_error: false,
            statement_index: None,
            error: None,
            server_message: false,
            manual_transaction_proven_read_only: false,
            manual_transaction_no_statement: false,
            auto_commit_open_transaction: None,
            auto_commit_explicit_transaction_rolled_back: false,
            auto_commit_session_autocommit_rolled_back: false,
        }
    }
}

impl From<db::sqlserver::SqlServerBatchResult> for ExecuteMultiResult {
    fn from(result: db::sqlserver::SqlServerBatchResult) -> Self {
        Self {
            result: result.result,
            large_value_cells: Vec::new(),
            execution_error: false,
            statement_index: None,
            error: None,
            server_message: result.server_message,
            manual_transaction_proven_read_only: false,
            manual_transaction_no_statement: false,
            auto_commit_open_transaction: None,
            auto_commit_explicit_transaction_rolled_back: false,
            auto_commit_session_autocommit_rolled_back: false,
        }
    }
}

fn is_false(value: &bool) -> bool {
    !*value
}

fn error_from_query_result(result: &db::QueryResult) -> Option<crate::backend_error::BackendError> {
    result.rows.first()?.first()?.as_str().map(crate::backend_error::BackendError::from_legacy_string)
}

/// Check read-only protection for a connection, blocking write SQL statements.
/// Only clones the connection name when read-only mode is active, avoiding
/// unnecessary allocations otherwise.
/// Uses config_for_pool_key to correctly resolve configs when pool_key includes
/// a database suffix (e.g., "prod:app" → config stored under "prod").
pub async fn check_read_only_for_connection(state: &AppState, pool_key: &str, sql: &str) -> Result<(), String> {
    if mysql_data_charset_policy::enabled_for_connection(state, pool_key).await? {
        return mysql_data_charset_policy::check_sql(sql, pool_key);
    }
    let connection = {
        let configs = state.configs.read().await;
        crate::connection::config_for_pool_key(pool_key, &configs)
            .filter(|config| config.read_only)
            .map(|config| (config.id.clone(), config.name.clone(), config.db_type))
    };
    if let Some((connection_id, name, database_type)) = connection {
        if !state.write_unlock_windows.is_active(&connection_id).await {
            crate::query_execution_sql::check_read_only(sql, &name, database_type)?;
        }
    }
    Ok(())
}

/// Check read-only protection for a connection across multiple SQL statements.
pub async fn check_read_only_for_connection_multi(
    state: &AppState,
    pool_key: &str,
    statements: &[impl AsRef<str>],
) -> Result<(), String> {
    if mysql_data_charset_policy::enabled_for_connection(state, pool_key).await? {
        for sql in statements {
            mysql_data_charset_policy::check_sql(sql.as_ref(), pool_key)?;
        }
        return Ok(());
    }
    let connection = {
        let configs = state.configs.read().await;
        crate::connection::config_for_pool_key(pool_key, &configs)
            .filter(|config| config.read_only)
            .map(|config| (config.id.clone(), config.name.clone(), config.db_type))
    };
    if let Some((connection_id, name, database_type)) = connection {
        if !state.write_unlock_windows.is_active(&connection_id).await {
            for sql in statements {
                crate::query_execution_sql::check_read_only(sql.as_ref(), &name, database_type)?;
            }
        }
    }
    Ok(())
}

/// Check whether a connection has read-only mode enabled, returning the connection name if so.
/// This uses connection_id directly (not pool_key), so it is safe to call at command entry points
/// before any pool key is constructed.
pub async fn connection_readonly_name(state: &AppState, connection_id: &str) -> Option<String> {
    // Invalid compatibility configuration also fails closed at this Option-only
    // legacy action boundary; connection setup reports the validation details.
    if mysql_data_charset_policy::enabled_for_connection(state, connection_id).await.unwrap_or(true) {
        return Some(format!("{connection_id} (MySQL dataCharset read-only)"));
    }
    let name = {
        let configs = state.configs.read().await;
        configs.get(connection_id).filter(|c| c.read_only).map(|c| c.name.clone())?
    };
    if state.write_unlock_windows.is_active(connection_id).await {
        return None;
    }
    Some(name)
}

/// Shared by Desktop and Web; compatibility-readonly is never temporarily unlockable.
pub async fn unlock_connection_writes_core(
    state: &AppState,
    connection_id: &str,
    duration_secs: u64,
) -> Result<u64, String> {
    if !state.configs.read().await.contains_key(connection_id) {
        return Err("Connection not found".into());
    }
    mysql_data_charset_policy::reject_operation(state, connection_id, mysql_data_charset_policy::READ_ONLY).await?;
    state.write_unlock_windows.unlock(connection_id, duration_secs).await
}

async fn connection_is_mongodb(state: &AppState, connection_id: &str) -> bool {
    let configs = state.configs.read().await;
    configs.get(connection_id).is_some_and(|config| config.db_type == DatabaseType::MongoDb)
}

/// Idle window for a new manual transaction on `connection_id`.
pub(crate) async fn manual_transaction_idle_timeout(state: &AppState, connection_id: &str) -> std::time::Duration {
    let configs = state.configs.read().await;
    let secs = configs.get(connection_id).map(ConnectionConfig::effective_manual_transaction_timeout_secs);
    std::time::Duration::from_secs(secs.unwrap_or(MANUAL_TRANSACTION_IDLE_TIMEOUT_SECS))
}

async fn connection_database_type(state: &AppState, connection_id: &str) -> Option<DatabaseType> {
    let configs = state.configs.read().await;
    configs.get(connection_id).map(|config| config.db_type)
}

async fn connection_statement_split_database_type(state: &AppState, connection_id: &str) -> Option<DatabaseType> {
    let configs = state.configs.read().await;
    configs.get(connection_id).map(statement_split_database_type)
}

/// A generic JDBC connection to Oracle runs PL/SQL through the JDBC plugin, so its
/// script is split with the Oracle rules. The plain `Jdbc` profile would cut every
/// routine body at its first inner semicolon and replace the object with the fragment.
/// A JDBC connection to SQL Server runs T-SQL through the JDBC plugin, so its script
/// keeps top-level control-flow batches whole (#13514). Both mappings only change
/// statement-splitting semantics; the connection's `db_type` still routes the query.
pub fn statement_split_database_type(config: &ConnectionConfig) -> DatabaseType {
    if crate::schema::is_oracle_external_driver_config(config) {
        DatabaseType::Oracle
    } else if crate::schema::is_sqlserver_external_driver_config(config) {
        DatabaseType::SqlServer
    } else {
        config.db_type
    }
}

pub(crate) async fn connection_sql_compatibility_mode(
    state: &AppState,
    pool_key: &str,
    db_type: Option<DatabaseType>,
) -> Option<String> {
    if db_type == Some(DatabaseType::Kingbase) {
        let Some(PoolKind::Agent(client)) = state.pool_handle(pool_key).await else { return None };
        return client.compatibility_mode().map(str::to_owned);
    }
    if db_type != Some(DatabaseType::OpenGauss) {
        return None;
    }
    let pool = match state.pool_handle(pool_key).await {
        Some(PoolKind::Postgres(pool)) => pool,
        _ => return None,
    };
    db::postgres::opengauss_compatibility_mode(&pool).await.ok().flatten()
}

async fn connection_mysql_query_dialect(state: &AppState, connection_id: &str) -> db::mysql::MySqlQueryDialect {
    let configs = state.configs.read().await;
    configs
        .get(connection_id)
        .map(|config| db::mysql::MySqlQueryDialect::for_connection(config.db_type, config.driver_profile.as_deref()))
        .unwrap_or_default()
}

async fn connection_mysql_catalog_dialect(
    state: &AppState,
    connection_id: &str,
) -> Option<db::mysql::MySqlCatalogDialect> {
    let configs = state.configs.read().await;
    configs
        .get(connection_id)
        .and_then(|config| db::mysql::mysql_catalog_dialect(config.db_type, config.driver_profile.as_deref()))
}

async fn connection_mysql_catalog_dialect_for_pool_key(
    state: &AppState,
    pool_key: &str,
) -> Option<db::mysql::MySqlCatalogDialect> {
    let configs = state.configs.read().await;
    crate::connection::config_for_pool_key(pool_key, &configs)
        .and_then(|config| db::mysql::mysql_catalog_dialect(config.db_type, config.driver_profile.as_deref()))
}

async fn connection_database_type_for_pool_key(state: &AppState, pool_key: &str) -> Option<DatabaseType> {
    let configs = state.configs.read().await;
    configs
        .iter()
        .filter(|(connection_id, _)| {
            pool_key.strip_prefix(connection_id.as_str()).is_some_and(|rest| rest.is_empty() || rest.starts_with(':'))
        })
        .max_by_key(|(connection_id, _)| connection_id.len())
        .map(|(_, config)| config.db_type)
}

fn schema_for_execution_context(db_type: Option<DatabaseType>, schema: Option<&str>) -> Option<&str> {
    // SQL Server has no session-level schema switch. Data-grid DML already uses
    // qualified names, while legacy jTDS can mis-handle schema as catalog.
    if matches!(db_type, Some(DatabaseType::Iris | DatabaseType::SqlServer)) {
        None
    } else {
        schema
    }
}

#[cfg(test)]
fn sql_for_execution_context(db_type: Option<DatabaseType>, sql: &str, schema: Option<&str>) -> String {
    sql_for_execution_context_with_identifier_quote(db_type, sql, schema, None)
}

fn sql_for_execution_context_with_identifier_quote(
    db_type: Option<DatabaseType>,
    sql: &str,
    schema: Option<&str>,
    identifier_quote: Option<&str>,
) -> String {
    let Some(schema) = schema.map(str::trim).filter(|schema| !schema.is_empty()) else {
        return sql.to_string();
    };
    match db_type {
        Some(DatabaseType::Iris) => {
            qualify_iris_unqualified_dml(sql, schema, identifier_quote).unwrap_or_else(|| sql.to_string())
        }
        Some(DatabaseType::SqlServer) => {
            qualify_sqlserver_unqualified_dml(sql, schema).unwrap_or_else(|| sql.to_string())
        }
        Some(DatabaseType::Kingbase) => {
            qualify_kingbase_unqualified_relations(sql, schema, identifier_quote).unwrap_or_else(|| sql.to_string())
        }
        _ => sql.to_string(),
    }
}

fn qualify_iris_unqualified_dml(sql: &str, schema: &str, identifier_quote: Option<&str>) -> Option<String> {
    let dialect = GenericDialect {};
    let mut statements = Parser::parse_sql(&dialect, sql).ok()?;
    if statements.is_empty() {
        return None;
    }

    // Caché/IRIS installations may run with delimited identifiers disabled. The
    // JDBC preparser then turns a double-quoted name into a `:%qpar` parameter,
    // and the statement fails at prepare with "IDENTIFIER expected". Ordinary
    // schema names are case-insensitive there, so they must stay unquoted.
    let schema_identifier = Ident::new(quote_iris_identifier(schema, identifier_quote));
    let mut changed = false;
    for statement in &mut statements {
        if !statement_uses_schema_context(statement) {
            continue;
        }
        let cte_names = statement_cte_names(statement);
        let table_aliases = statement_table_aliases(statement);
        let _ = visit_relations_mut(statement, |name| {
            if qualify_unqualified_relation_name(name, &schema_identifier, &cte_names, &table_aliases) {
                changed = true;
            }
            ControlFlow::<()>::Continue(())
        });
    }

    changed.then(|| statements.iter().map(ToString::to_string).collect::<Vec<_>>().join("; "))
}

fn qualify_sqlserver_unqualified_dml(sql: &str, schema: &str) -> Option<String> {
    let dialect = MsSqlDialect {};
    let mut statements = Parser::parse_sql(&dialect, sql).ok()?;
    if statements.is_empty() {
        return None;
    }

    let mut changed = false;
    for statement in &mut statements {
        if !statement_uses_schema_context(statement) {
            continue;
        }
        let cte_names = statement_cte_names(statement);
        let table_aliases = statement_table_aliases(statement);
        let mut qualifier = SchemaRelationQualifier {
            schema_identifier: Ident::with_quote('[', schema),
            cte_names: &cte_names,
            table_aliases: &table_aliases,
            parameterized_table_depth: 0,
            changed: false,
        };
        let _ = statement.visit(&mut qualifier);
        changed |= qualifier.changed;
    }

    changed.then(|| statements.iter().map(ToString::to_string).collect::<Vec<_>>().join("; "))
}

fn qualify_kingbase_unqualified_relations(sql: &str, schema: &str, identifier_quote: Option<&str>) -> Option<String> {
    let dialect = PostgreSqlDialect {};
    let mut statements = Parser::parse_sql(&dialect, sql).ok()?;
    if statements.is_empty() {
        return None;
    }

    let mut changed = false;
    for statement in &mut statements {
        if !statement_uses_schema_context(statement) {
            continue;
        }
        let cte_names = statement_cte_names(statement);
        let table_aliases = statement_table_aliases(statement);
        let mut qualifier = SchemaRelationQualifier {
            schema_identifier: Ident::with_quote(identifier_quote_char(identifier_quote), schema),
            cte_names: &cte_names,
            table_aliases: &table_aliases,
            parameterized_table_depth: 0,
            changed: false,
        };
        let _ = statement.visit(&mut qualifier);
        changed |= qualifier.changed;
    }

    changed.then(|| statements.iter().map(ToString::to_string).collect::<Vec<_>>().join("; "))
}

struct SchemaRelationQualifier<'a> {
    schema_identifier: Ident,
    cte_names: &'a HashSet<String>,
    table_aliases: &'a HashSet<String>,
    parameterized_table_depth: usize,
    changed: bool,
}

impl VisitorMut for SchemaRelationQualifier<'_> {
    type Break = ();

    fn pre_visit_table_factor(&mut self, table_factor: &mut TableFactor) -> ControlFlow<Self::Break> {
        if matches!(table_factor, TableFactor::Table { args: Some(_), .. }) {
            self.parameterized_table_depth += 1;
        }
        ControlFlow::Continue(())
    }

    fn post_visit_table_factor(&mut self, table_factor: &mut TableFactor) -> ControlFlow<Self::Break> {
        if matches!(table_factor, TableFactor::Table { args: Some(_), .. }) {
            self.parameterized_table_depth = self.parameterized_table_depth.saturating_sub(1);
        }
        ControlFlow::Continue(())
    }

    fn post_visit_relation(&mut self, relation: &mut ObjectName) -> ControlFlow<Self::Break> {
        if self.parameterized_table_depth == 0
            && qualify_unqualified_relation_name(relation, &self.schema_identifier, self.cte_names, self.table_aliases)
        {
            self.changed = true;
        }
        ControlFlow::Continue(())
    }
}

fn statement_uses_schema_context(statement: &Statement) -> bool {
    matches!(
        statement,
        Statement::Query(_)
            | Statement::Insert(_)
            | Statement::Update(_)
            | Statement::Delete(_)
            | Statement::Truncate(_)
    )
}

/// Qualify a single-part relation with `schema_identifier`, which is already
/// rendered in the dialect's own spelling (quoted where the dialect needs it,
/// unquoted where quoting would break parsing).
fn qualify_unqualified_relation_name(
    name: &mut ObjectName,
    schema_identifier: &Ident,
    cte_names: &HashSet<String>,
    table_aliases: &HashSet<String>,
) -> bool {
    let [ObjectNamePart::Identifier(table)] = name.0.as_slice() else {
        return false;
    };
    let upper_name = table.value.to_ascii_uppercase();
    if cte_names.contains(&upper_name) || table_aliases.contains(&upper_name) {
        return false;
    }
    if table.value.starts_with('@') || table.value.starts_with('#') {
        return false;
    }

    let table = table.clone();
    name.0 = vec![ObjectNamePart::Identifier(schema_identifier.clone()), ObjectNamePart::Identifier(table)];
    true
}

fn identifier_quote_char(identifier_quote: Option<&str>) -> char {
    match identifier_quote.map(str::trim) {
        Some("`") => '`',
        Some("\"") => '"',
        _ => '"',
    }
}

fn statement_cte_names(statement: &Statement) -> HashSet<String> {
    let mut names = HashSet::new();
    collect_statement_cte_names(statement, &mut names);
    names
}

fn collect_statement_cte_names(statement: &Statement, names: &mut HashSet<String>) {
    match statement {
        Statement::Query(query) => collect_query_cte_names(query, names),
        Statement::Insert(insert) => {
            if let Some(source) = &insert.source {
                collect_query_cte_names(source, names);
            }
        }
        _ => {}
    }
}

fn collect_query_cte_names(query: &sqlparser::ast::Query, names: &mut HashSet<String>) {
    if let Some(with) = &query.with {
        for cte in &with.cte_tables {
            names.insert(cte.alias.name.value.to_ascii_uppercase());
            collect_query_cte_names(&cte.query, names);
        }
    }
}

struct TableAliasCollector<'a> {
    names: &'a mut HashSet<String>,
}

impl VisitorMut for TableAliasCollector<'_> {
    type Break = ();

    fn post_visit_table_factor(&mut self, table_factor: &mut TableFactor) -> ControlFlow<Self::Break> {
        if let TableFactor::Table { alias: Some(alias), .. } = table_factor {
            self.names.insert(alias.name.value.to_ascii_uppercase());
        }
        ControlFlow::Continue(())
    }
}

/// FROM-clause aliases resolve to their table, so a single-part relation that
/// matches one must never be schema-qualified (e.g. `UPDATE p ... FROM products p`).
fn statement_table_aliases(statement: &mut Statement) -> HashSet<String> {
    let mut names = HashSet::new();
    let mut collector = TableAliasCollector { names: &mut names };
    let _ = statement.visit(&mut collector);
    names
}

fn qualifies_unqualified_agent_relations(db_type: Option<DatabaseType>) -> bool {
    matches!(db_type, Some(DatabaseType::Iris | DatabaseType::Kingbase | DatabaseType::SqlServer))
}

#[derive(Clone, Copy, Debug, Default, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum QueryExecutionMode {
    #[default]
    Standard,
    Simple,
    PostgresReadOnlyTransaction,
}

#[derive(Clone, Debug, Default)]
pub struct QueryExecutionOptions {
    pub defer_lobs: bool,
    pub max_rows: Option<usize>,
    pub fetch_size: Option<usize>,
    pub page_size: Option<usize>,
    pub row_offset: Option<usize>,
    pub max_result_bytes: Option<usize>,
    /// Result columns that must stay exact because clients use them as stable
    /// row identifiers when fetching full large-cell values on demand.
    pub result_key_columns: Vec<String>,
    /// Enables extraction of hidden server-side preview metadata. This must be
    /// set only for generated table-data preview SQL, never arbitrary queries.
    pub table_data_preview: bool,
    /// Doris / StarRocks catalog selected for this query tab.
    pub catalog: Option<String>,
    pub result_session_id: Option<String>,
    pub client_session_id: Option<String>,
    /// Query timeout in seconds. `None` uses the default (30s).
    /// `Some(0)` disables the timeout entirely.
    pub timeout_secs: Option<u64>,
    /// Keep awaiting the database response after cancellation so callers can
    /// distinguish an interrupt request from a confirmed terminal state.
    pub await_cancel_completion: bool,
    pub execution_id: Option<String>,
    /// When `Some(true)`, multiple statements are executed within a single transaction
    /// (BEGIN … COMMIT) instead of auto-commit mode. `None` and `Some(false)` behave
    /// identically — auto-commit for each statement.
    pub use_transaction: Option<bool>,
    /// When `true`, multi-statement execution continues after a statement error instead
    /// of stopping at the first failure. Connection-level failures always stop the batch.
    pub continue_on_error: bool,
    /// Explicit low-level execution path. `Simple` is currently used by SQL Server
    /// SHOWPLAN so the source SQL bypasses result-set probing and query rewriting.
    /// `PostgresReadOnlyTransaction` executes on an isolated client session and
    /// always rolls the transaction back after the result is collected.
    pub execution_mode: QueryExecutionMode,
    /// MySQL auto-commit tabs only: keep a transaction the user opened
    /// explicitly (`BEGIN` / `START TRANSACTION`, or a batch that disabled
    /// auto-commit) open across executions instead of rolling it back when the
    /// batch finishes. Opt-in per execution (driven by the editor setting);
    /// `false` keeps the historical cleanup that stops a leftover transaction
    /// from pinning the tab's read view (#9479).
    pub preserve_explicit_transaction: bool,
}

fn validate_query_execution_mode(
    db_type: Option<DatabaseType>,
    sql: &str,
    options: &QueryExecutionOptions,
) -> Result<(), String> {
    if options.execution_mode != QueryExecutionMode::PostgresReadOnlyTransaction {
        return Ok(());
    }
    if db_type != Some(DatabaseType::Postgres) {
        return Err("PostgreSQL read-only transaction mode requires a PostgreSQL connection".to_string());
    }
    if options.client_session_id.as_deref().is_none_or(|session_id| session_id.trim().is_empty()) {
        return Err("PostgreSQL read-only transaction mode requires an isolated client session".to_string());
    }
    if crate::sql::split_sql_statements_for_database(sql, DatabaseType::Postgres).len() != 1 {
        return Err("PostgreSQL read-only transaction mode requires exactly one statement".to_string());
    }
    Ok(())
}

fn query_result_row_limit(max_rows: Option<usize>) -> usize {
    max_rows.unwrap_or(MAX_ROWS).max(1)
}

pub fn truncate_result(result: db::QueryResult) -> db::QueryResult {
    truncate_result_with_max_rows(result, None)
}

pub fn truncate_result_with_max_rows(mut result: db::QueryResult, max_rows: Option<usize>) -> db::QueryResult {
    let row_limit = query_result_row_limit(max_rows);
    if result.rows.len() > row_limit {
        result.rows.truncate(row_limit);
        result.raw_key_cells.retain(|cell| cell.row_index < row_limit);
        result.truncated = true;
    }
    result
}

pub fn agent_execute_query_params(
    sql: &str,
    database: Option<&str>,
    schema: Option<&str>,
    options: QueryExecutionOptions,
) -> serde_json::Value {
    let mut params = serde_json::json!({
        "sql": sql,
        "maxRows": agent_protocol_row_count(options.max_rows.unwrap_or(MAX_ROWS)),
        "deferLobs": options.table_data_preview,
    });
    if let Some(database) = database.map(str::trim).filter(|database| !database.is_empty()) {
        params["database"] = serde_json::json!(database);
    }
    if let Some(schema) = schema {
        params["schema"] = serde_json::json!(schema);
    }
    if let Some(fetch_size) = options.fetch_size {
        params["fetchSize"] = serde_json::json!(agent_protocol_row_count(fetch_size));
    }
    if let Some(row_offset) = options.row_offset {
        params["rowOffset"] = serde_json::json!(agent_protocol_row_offset(row_offset));
    }
    if let Some(timeout_secs) = options.timeout_secs {
        params["timeoutSecs"] = serde_json::json!(timeout_secs);
    }
    params
}

pub fn agent_execute_query_page_params(
    sql: &str,
    database: Option<&str>,
    schema: Option<&str>,
    options: QueryExecutionOptions,
) -> serde_json::Value {
    let mut params = serde_json::json!({
        "sql": sql,
        "pageSize": agent_protocol_row_count(options.page_size.unwrap_or(MAX_ROWS)),
        "maxRows": agent_protocol_row_count(options.max_rows.unwrap_or(MAX_ROWS)),
        "deferLobs": options.table_data_preview,
    });
    if let Some(database) = database.map(str::trim).filter(|database| !database.is_empty()) {
        params["database"] = serde_json::json!(database);
    }
    if let Some(schema) = schema {
        params["schema"] = serde_json::json!(schema);
    }
    if let Some(fetch_size) = options.fetch_size {
        params["fetchSize"] = serde_json::json!(agent_protocol_row_count(fetch_size));
    }
    if let Some(row_offset) = options.row_offset {
        params["rowOffset"] = serde_json::json!(agent_protocol_row_offset(row_offset));
    }
    if let Some(timeout_secs) = options.timeout_secs {
        params["timeoutSecs"] = serde_json::json!(timeout_secs);
    }
    params
}

pub fn agent_fetch_query_page_params(session_id: &str, page_size: usize) -> serde_json::Value {
    serde_json::json!({
        "sessionId": session_id,
        "pageSize": agent_protocol_row_count(page_size),
    })
}

fn agent_protocol_row_count(value: usize) -> usize {
    value.clamp(1, AGENT_PROTOCOL_MAX_ROWS)
}

fn agent_protocol_row_offset(value: usize) -> usize {
    value.min(AGENT_PROTOCOL_MAX_ROWS)
}

pub fn agent_close_query_session_params(session_id: &str) -> serde_json::Value {
    serde_json::json!({
        "sessionId": session_id,
    })
}

pub fn is_connection_error(err: &str) -> bool {
    let lower = err.to_lowercase();
    if is_dbx_query_timeout_error(&lower) || is_agent_rpc_timeout_error(&lower) || is_pool_saturation_error(err) {
        return false;
    }
    lower.contains("connection")
        || lower.contains("broken pipe")
        || lower.contains("reset by peer")
        || lower.contains("timed out")
        || (lower.contains("pool") && lower.contains("timeout"))
        || lower.contains("closed")
        || lower.contains("关闭的连接")
        || lower.contains("连接已关闭")
        || lower.contains("网络通信异常")
        || lower.contains("通信异常")
        || lower.contains("communications link failure")
        || lower.contains("sqlrecoverableexception")
        || lower.contains("sqlnontransientconnectionexception")
        || lower.contains("sqltransientconnectionexception")
        || lower.contains("eof")
        || lower.contains("i/o error")
        || lower.contains("input/output error")
        || lower.contains("not connected")
        || lower.contains("end-of-file")
        || lower.contains("idle")
        || lower.contains("agent stdin not available")
        || lower.contains("agent stdout not available")
        || lower.contains("agent runtime terminated")
        || lower.contains("agent runtime is unavailable")
        || lower.contains("agent runtime unavailable")
        || lower.contains("failed to write to agent stdin")
        || lower.contains("failed to flush agent stdin")
        || lower.contains("communicating with the server")
        || is_os_connection_error(&lower)
}

pub(crate) fn is_dbx_query_timeout_error(lower: &str) -> bool {
    lower.starts_with("query timed out after ")
}

fn is_agent_rpc_timeout_error(lower: &str) -> bool {
    lower.starts_with("agent rpc call timed out ")
}

fn is_schema_reset_cleanup_error(lower: &str) -> bool {
    lower.contains("schema.reset cleanup failed")
}

fn is_postgres_transaction_cleanup_error(lower: &str) -> bool {
    lower.contains("postgresql read-only transaction cleanup failed")
}

fn should_discard_agent_pool_after_error(err: &str) -> bool {
    crate::db::agent_driver::agent_recovery_decision(err, RecoveryScope::UserOperation).discards_session()
}

pub fn pool_error_action(db_type: Option<DatabaseType>, err: &str) -> PoolErrorAction {
    if db_type.is_some_and(|db_type| database_capabilities::is_agent_type(&db_type)) {
        return if should_discard_agent_pool_after_error(err) {
            // Agent user operations are never replayed from an error hint because
            // the database-side outcome may be unknown.
            PoolErrorAction::Discard
        } else {
            PoolErrorAction::Keep
        };
    }
    let lower = err.to_lowercase();
    if db::sqlserver::is_driver_panic_error(err)
        || (is_dbx_query_timeout_error(&lower) && should_discard_pool_after_query_timeout(db_type))
        || is_schema_reset_cleanup_error(&lower)
        || is_postgres_transaction_cleanup_error(&lower)
    {
        return PoolErrorAction::Discard;
    }

    if is_connection_error(err) {
        PoolErrorAction::ReconnectAndRetry
    } else {
        PoolErrorAction::Keep
    }
}

fn should_continue_batch_after_error(continue_on_error: bool, action: PoolErrorAction) -> bool {
    // A broken connection cannot safely execute the remaining statements even when
    // the user explicitly enabled continue-on-error.
    continue_on_error && action == PoolErrorAction::Keep
}

fn options_for_sequential_statements(
    options: &QueryExecutionOptions,
    statement_count: usize,
    db_type: Option<DatabaseType>,
) -> QueryExecutionOptions {
    let mut statement_options = options.clone();
    if statement_count <= 1
        || !matches!(db_type, Some(DatabaseType::Kingbase | DatabaseType::Vastbase | DatabaseType::Oracle))
        || statement_options.result_session_id.is_some()
    {
        return statement_options;
    }

    if let Some(page_size) = statement_options.page_size.take() {
        let page_size = page_size.max(1);
        statement_options.max_rows =
            Some(statement_options.max_rows.map_or(page_size, |max_rows| max_rows.min(page_size)));
    }
    statement_options
}

pub(crate) fn should_discard_pool_after_query_timeout(db_type: Option<DatabaseType>) -> bool {
    let Some(db_type) = db_type else {
        return false;
    };
    database_capabilities::is_agent_type(&db_type)
        || matches!(
            db_type,
            DatabaseType::Mysql
                | DatabaseType::Postgres
                | DatabaseType::Redshift
                | DatabaseType::Gaussdb
                | DatabaseType::Kwdb
                | DatabaseType::OpenGauss
                | DatabaseType::Questdb
                | DatabaseType::Doris
                | DatabaseType::StarRocks
                | DatabaseType::ManticoreSearch
                | DatabaseType::ClickHouse
                | DatabaseType::SqlServer
                | DatabaseType::Rqlite
                | DatabaseType::Turso
                | DatabaseType::CloudflareD1
                | DatabaseType::Elasticsearch
                | DatabaseType::Easysearch
                | DatabaseType::Qdrant
                | DatabaseType::Milvus
                | DatabaseType::Weaviate
                | DatabaseType::ChromaDb
                | DatabaseType::InfluxDb
                | DatabaseType::InfluxDb3
                | DatabaseType::VictoriaMetrics
        )
}

pub fn should_discard_pool_after_error(db_type: Option<DatabaseType>, err: &str) -> bool {
    matches!(pool_error_action(db_type, err), PoolErrorAction::Discard | PoolErrorAction::ReconnectAndRetry)
}

async fn discard_pool_after_error(state: &AppState, pool_key: &str, db_type: Option<DatabaseType>, error: &str) {
    let action = pool_error_action(db_type, error);
    if !matches!(action, PoolErrorAction::Discard | PoolErrorAction::ReconnectAndRetry) {
        return;
    }

    let replace_agent_runtime = db_type.is_some_and(|db_type| database_capabilities::is_agent_type(&db_type))
        && crate::db::agent_driver::agent_recovery_decision(error, RecoveryScope::UserOperation).replaces_runtime();
    if replace_agent_runtime {
        state.detach_pool_by_key(pool_key, true).await;
    } else {
        state.remove_pool_by_key(pool_key).await;
    }
}

async fn discard_agent_pool_after_typed_error(
    state: &AppState,
    pool_key: &str,
    client: &Arc<crate::db::agent_driver::PooledAgentClient>,
    error: &AgentCallError,
    scope: RecoveryScope,
) {
    discard_agent_pool_after_decision(state, pool_key, client, RecoveryPolicy::decide(error, scope)).await;
}

async fn discard_agent_pool_after_decision(
    state: &AppState,
    pool_key: &str,
    client: &Arc<crate::db::agent_driver::PooledAgentClient>,
    decision: RecoveryDecision,
) {
    if decision.discards_session() {
        state.detach_agent_pool_if_current(pool_key, client, decision.replaces_runtime()).await;
    }
}

fn query_pool_error_action(db_type: Option<DatabaseType>, sql: &str, err: &str) -> PoolErrorAction {
    match pool_error_action(db_type, err) {
        // A connection error does not prove that the database did not receive
        // a write. Only replay statements already accepted by the read-only
        // protection classifier; writes discard the stale pool without retry.
        PoolErrorAction::ReconnectAndRetry if is_write_sql(sql) => PoolErrorAction::Discard,
        action => action,
    }
}

fn native_postgres_compatibility_type(db_type: Option<DatabaseType>) -> bool {
    matches!(
        db_type,
        Some(
            DatabaseType::Postgres
                | DatabaseType::Gaussdb
                | DatabaseType::OpenGauss
                | DatabaseType::Kwdb
                | DatabaseType::Questdb
        )
    )
}

fn postgres_create_table_relation(sql: &str) -> Option<(Option<String>, String)> {
    let statements = Parser::parse_sql(&PostgreSqlDialect {}, sql).ok()?;
    let [Statement::CreateTable(table)] = statements.as_slice() else {
        return None;
    };
    if table.temporary {
        return None;
    }
    // PostgreSQL stores unquoted identifiers lower-cased, so the existence
    // probe must compare the folded spelling; quoted names keep their case.
    let mut parts = table.name.0.iter().filter_map(|part| {
        part.as_ident().map(|ident| match ident.quote_style {
            Some(_) => ident.value.clone(),
            None => ident.value.to_lowercase(),
        })
    });
    let table_name = parts.next_back()?;
    Some((parts.next_back(), table_name))
}

fn should_verify_postgres_create_table_after_connection_error(
    db_type: Option<DatabaseType>,
    sql: &str,
    error: &str,
) -> bool {
    native_postgres_compatibility_type(db_type)
        && is_connection_error(error)
        && postgres_create_table_relation(sql).is_some()
}

fn is_postgres_duplicate_relation_error(error: &str) -> bool {
    let lower = error.to_ascii_lowercase();
    lower.contains("already exists") && (lower.contains("relation") || lower.contains("table"))
}

fn query_execution_error_action(
    db_type: Option<DatabaseType>,
    sql: &str,
    error: &QueryExecutionError,
) -> PoolErrorAction {
    if let Some(agent_error) = error.as_agent_error() {
        return if RecoveryPolicy::decide(agent_error, RecoveryScope::UserOperation).discards_session() {
            PoolErrorAction::Discard
        } else {
            PoolErrorAction::Keep
        };
    }
    match error {
        QueryExecutionError::Canceled { .. } => PoolErrorAction::Keep,
        QueryExecutionError::DuckDb { message, .. } => query_pool_error_action(db_type, sql, message),
        QueryExecutionError::Timeout(message)
        | QueryExecutionError::Sql(message)
        | QueryExecutionError::SqlWithPosition { message, .. }
        | QueryExecutionError::Legacy(message) => query_pool_error_action(db_type, sql, message),
        QueryExecutionError::Agent(_) => unreachable!("Agent errors return above"),
    }
}

fn is_os_connection_error(lower: &str) -> bool {
    let os_error_codes = ["10053", "10054", "10057", "10058", "10060", "10061"];
    if let Some(pos) = lower.find("os error ") {
        let after = &lower[pos + 9..];
        let code: String = after.chars().take_while(|c| c.is_ascii_digit()).collect();
        return os_error_codes.contains(&code.as_str());
    }
    false
}

fn canceled_query_execution_error() -> QueryExecutionError {
    QueryExecutionError::Canceled { stage: AgentErrorStage::Cancel, operation_outcome: AgentOperationOutcome::Unknown }
}

fn pre_dispatch_canceled_query_execution_error() -> QueryExecutionError {
    QueryExecutionError::Canceled {
        stage: AgentErrorStage::Request,
        operation_outcome: AgentOperationOutcome::NotStarted,
    }
}

fn postgres_transaction_statement_error(
    statement_index: usize,
    message: &str,
    sql: &str,
    is_server_error: bool,
) -> QueryExecutionError {
    let detail = query_error_with_omitted_sql_context(&format!("Statement {statement_index} failed: {message}"), sql);
    let lower = message.to_ascii_lowercase();
    if is_dbx_query_timeout_error(&lower) {
        QueryExecutionError::Timeout(detail)
    } else if message == QUERY_CANCELED {
        canceled_query_execution_error()
    } else if is_server_error {
        QueryExecutionError::Sql(detail)
    } else {
        QueryExecutionError::Legacy(detail)
    }
}

async fn sqlserver_pool_is_current(
    state: &AppState,
    pool_key: &str,
    client: &Arc<tokio::sync::Mutex<db::sqlserver::SqlServerClient>>,
) -> bool {
    matches!(state.pool_handle(pool_key).await, Some(PoolKind::SqlServer(current)) if Arc::ptr_eq(&current, client))
}

fn query_pool_database<'a>(database: &'a str, catalog: Option<&str>) -> Option<&'a str> {
    if database.is_empty() || catalog.is_some() {
        None
    } else {
        Some(database)
    }
}

fn postgres_prefers_text_protocol(db_type: Option<DatabaseType>) -> bool {
    db_type == Some(DatabaseType::Redshift)
}

pub async fn operation_budget_for_pool_key(
    state: &AppState,
    pool_key: &str,
    query_timeout: Option<Duration>,
) -> DbOperationBudget {
    let mut budget = configured_operation_budget_for_pool_key(state, pool_key).await;
    budget.query_timeout = query_timeout;
    budget
}

async fn configured_operation_budget_for_pool_key(state: &AppState, pool_key: &str) -> DbOperationBudget {
    let configs = state.configs.read().await;
    crate::connection::config_for_pool_key(pool_key, &configs)
        .map(DbOperationBudget::from_connection_config)
        .unwrap_or_else(DbOperationBudget::with_defaults)
}

/// Override a transaction budget's query timeout from a per-call override (e.g. the MCP
/// global query-timeout policy). `None` leaves the budget unchanged; `Some(secs)` follows
/// `resolve_query_timeout` semantics (`Some(0)` clears the limit, meaning unlimited).
fn apply_query_timeout_override(budget: &mut DbOperationBudget, timeout_secs: Option<u64>) {
    if let Some(secs) = timeout_secs {
        budget.query_timeout = resolve_query_timeout(Some(secs));
    }
}

fn oceanbase_mysql_session_timeout_sql(config: Option<&ConnectionConfig>, timeout_secs: Option<u64>) -> Option<String> {
    let config = config?;
    let timeout_secs = timeout_secs.unwrap_or(config.query_timeout_secs);
    crate::db::oceanbase_mysql::query_timeout_sql(config, timeout_secs)
}

async fn apply_oceanbase_mysql_session_timeout(
    state: &AppState,
    pool_key: &str,
    conn: &mut mysql_async::Conn,
    timeout_secs: Option<u64>,
) -> Result<(), String> {
    let sql = {
        let configs = state.configs.read().await;
        oceanbase_mysql_session_timeout_sql(crate::connection::config_for_pool_key(pool_key, &configs), timeout_secs)
    };
    if let Some(sql) = sql {
        // OceanBase enforces query timeouts through a session variable; set it
        // on the checked-out connection in case the pooled session was reset.
        conn.query_drop(&sql).await.map_err(|err| format!("Failed to apply OceanBase query timeout: {err}"))?;
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
async fn do_execute_typed(
    state: &AppState,
    pool_key: &str,
    mysql_dialect: db::mysql::MySqlQueryDialect,
    database: Option<&str>,
    sql: &str,
    schema: Option<&str>,
    cancel_token: Option<CancellationToken>,
    options: QueryExecutionOptions,
    transaction_outcome: &mut Option<MysqlAutoCommitTransaction>,
) -> Result<db::QueryResult, QueryExecutionError> {
    crate::sql_diagnostics::debug_sql("do_execute", sql);
    if let Some(execution_id) = options.execution_id.as_deref() {
        state.running_queries.set_pool_key(execution_id, pool_key.to_string());
    }
    state.touch_pool_activity(pool_key).await;
    let _activity_touch = state.pool_activity_touch(pool_key);

    let query_timeout = resolve_query_timeout(options.timeout_secs);
    // Re-check at statement start so a write that becomes ready after the
    // temporary unlock window expires is still blocked, even if an earlier
    // statement in the same batch is still running.
    check_read_only_for_connection(state, pool_key, sql).await?;
    let operation_budget = operation_budget_for_pool_key(state, pool_key, query_timeout).await;
    let pool_db_type = connection_database_type_for_pool_key(state, pool_key).await;
    let mysql_catalog_dialect = connection_mysql_catalog_dialect_for_pool_key(state, pool_key).await;
    let pool = state.pool_handle(pool_key).await.ok_or("Connection not found")?;
    // Everything the driver dispatch does for this statement — pool checkout,
    // schema/search_path setup, the statement, and the post-statement cleanup —
    // is what the user actually waited for. Report that span instead of the
    // driver-internal timer, which starts only after a client is in hand and
    // therefore hides connection-pool stalls from the summary (#6097 fixed the
    // same mismatch for SQL Server's shared-connection lock).
    let dispatch_start = std::time::Instant::now();

    let mut typed_agent_error = None;
    #[cfg(feature = "duckdb-sidecar")]
    let mut typed_duckdb_error = None;
    let result: Result<db::QueryResult, String> = match &pool {
        #[cfg(feature = "duckdb-sidecar")]
        PoolKind::DuckDbWorker(client) => {
            let client = client.clone();
            let operation_cancel = cancel_token.unwrap_or_default();
            if let Some(ref execution_id) = options.execution_id {
                let operation_cancel = operation_cancel.clone();
                state.running_queries.register_interrupt(execution_id, move || {
                    operation_cancel.cancel();
                });
            }
            let cancel_token = Some(operation_cancel);
            let sql = sql.to_string();
            let database = database.map(str::to_string);
            let max_rows = options.max_rows;
            let result = if let Some(page_size) = options.page_size.filter(|_| !is_write_sql(&sql)) {
                client
                    .execute_query_page(
                        database,
                        sql,
                        max_rows,
                        page_size,
                        options.result_session_id.as_deref(),
                        cancel_token,
                        query_timeout,
                    )
                    .await
                    .map_err(db::duckdb_worker_protocol::DuckDbWorkerError::from)
            } else {
                client.execute_typed(database, sql, max_rows, cancel_token, query_timeout).await
            };
            match result {
                Ok(result) => Ok(result),
                Err(error) => {
                    let is_control_error = error.message == QUERY_CANCELED
                        || is_dbx_query_timeout_error(&error.message.to_ascii_lowercase());
                    if !is_control_error {
                        typed_duckdb_error = Some(error.clone());
                    }
                    Err(error.message)
                }
            }
        }
        #[cfg(not(feature = "duckdb-sidecar"))]
        PoolKind::DuckDbWorker(_) => {
            return Err("DuckDB worker support is not compiled in this build".into());
        }
        PoolKind::Mysql(p, mode) => {
            let p = p.clone();
            let bare = *mode == crate::connection::MysqlMode::Bare;
            let max_rows = options.max_rows;
            let max_result_bytes = options.max_result_bytes.filter(|value| *value > 0);
            let mut conn = match db::mysql::get_conn_with_health_check_with_cancel(
                &p,
                operation_budget.checkout_timeout,
                operation_budget.cleanup_timeout,
                cancel_token.as_ref(),
            )
            .await
            {
                Ok(conn) => conn,
                Err(err) if err == QUERY_CANCELED => {
                    state.remove_pool_by_key(pool_key).await;
                    return Err(err.into());
                }
                Err(err) => return Err(err.into()),
            };
            let connection_id = conn.id();
            if let Some(ref execution_id) = options.execution_id {
                let kill_opts = conn.opts().clone();
                // The interrupt runs after the user asked to cancel; give the
                // dedicated kill connection the same infrastructure budget as
                // the query path instead of the old compile-time 5 s.
                let kill_timeout = operation_budget.checkout_timeout;
                state.running_queries.register_interrupt(execution_id, move || {
                    let kill_opts = kill_opts.clone();
                    tokio::spawn(async move {
                        if let Err(error) =
                            db::mysql::kill_query_with_opts(kill_opts, connection_id, kill_timeout).await
                        {
                            log::warn!("Failed to cancel MySQL query {connection_id}: {error}");
                        }
                    });
                });
            }
            apply_oceanbase_mysql_session_timeout(state, pool_key, &mut conn, options.timeout_secs).await?;
            wait_for_result_opt(
                cancel_token.clone(),
                query_timeout,
                db::mysql::apply_catalog_database_context(
                    &mut conn,
                    mysql_catalog_dialect,
                    options.catalog.as_deref(),
                    database.unwrap_or_default(),
                ),
            )
            .await?;
            let execution_cancel_token = if options.await_cancel_completion { None } else { cancel_token };
            let statement_result = wait_for_result_opt(
                execution_cancel_token,
                query_timeout,
                db::mysql::execute_query_on_conn_with_limits_with_data_charset(
                    &mut conn,
                    sql,
                    bare,
                    max_rows,
                    max_result_bytes,
                    &options.result_key_columns,
                    mysql_dialect,
                    options.execution_id.as_deref(),
                    p.data_charset(),
                ),
            )
            .await
            .map(|result| result.result);

            // Client-session pools hold one connection for the whole tab and
            // skip COM_RESET_CONNECTION on return so session state survives
            // across executions. A single-statement BEGIN / START TRANSACTION
            // would therefore leave its transaction open and pin the
            // connection's REPEATABLE READ read view, so every later
            // auto-commit query in the tab would keep reading the same stale
            // snapshot until the connection was closed. Settle it here, exactly
            // like the multi-statement MySQL path does: roll the transaction
            // back, unless the tab keeps explicit user transactions open
            // (`preserve_explicit_transaction`) or a later execution already
            // decided to keep this one.
            if p.is_client_session_pool() {
                // A truncated or failed result, or result sets still pending
                // behind the one that was read, may leave the last status packet
                // short of the end, so only a complete response can prove that
                // the cleanup `ROLLBACK` is unnecessary.
                let status_is_final = *mode == crate::connection::MysqlMode::Normal
                    && statement_result.as_ref().is_ok_and(|result| !result.truncated)
                    && db::mysql::last_ok_ends_response(&conn);
                let transaction = settle_mysql_auto_commit_transaction_boxed(
                    state,
                    pool_key,
                    &mut conn,
                    options.preserve_explicit_transaction,
                    crate::query_execution_sql::mysql_statement_opens_explicit_transaction(sql),
                    status_is_final,
                )
                .await;
                match transaction {
                    Ok(transaction) => *transaction_outcome = Some(transaction),
                    Err(error) => {
                        log::warn!(
                            "[query][mysql] trace_id={} open_txn_rollback_failed error={}",
                            options.execution_id.as_deref().unwrap_or_default(),
                            error
                        );
                        let _ = tokio::time::timeout(Duration::from_secs(5), conn.disconnect()).await;
                        state.remove_pool_by_key(pool_key).await;
                    }
                }
            }
            statement_result
        }
        PoolKind::Postgres(p) => {
            let p = p.clone();
            let schema = schema.map(|s| s.to_string());
            let max_rows = options.max_rows;
            let prefer_text_protocol = postgres_prefers_text_protocol(pool_db_type);
            let execution_mode = options.execution_mode;
            let cancel_context = state.get_postgres_cancel_context(pool_key).await;
            let result = execute_postgres_pool_statement(
                &p,
                pool_db_type,
                schema.as_deref(),
                sql,
                max_rows,
                prefer_text_protocol,
                execution_mode,
                cancel_token.clone(),
                operation_budget.clone(),
                cancel_context.clone(),
            )
            .await;
            let retry_sql =
                result.as_ref().err().and_then(|error| postgres_preview_fallback_retry_sql(&options, error, sql));
            match retry_sql {
                Some(fallback_sql) => {
                    log::warn!(
                        "[query][postgres] preview failed with invalid UTF-8; retrying without generated left() wrappers"
                    );
                    execute_postgres_pool_statement(
                        &p,
                        pool_db_type,
                        schema.as_deref(),
                        &fallback_sql,
                        max_rows,
                        prefer_text_protocol,
                        execution_mode,
                        cancel_token,
                        operation_budget,
                        cancel_context,
                    )
                    .await
                }
                None => result,
            }
        }
        PoolKind::Sqlite(p) => {
            let p = p.clone();
            let max_rows = options.max_rows;
            // SQLite execution runs in spawn_blocking, so cancelling only the
            // awaitable future leaves the pooled connection occupied. Interrupt
            // the native statement as soon as the shared cancellation registry
            // receives the request so the same client session can run again.
            if let Some(execution_id) = options.execution_id.as_deref() {
                if let Ok(interrupt) = p.with_connection(|conn| Ok(conn.get_interrupt_handle())) {
                    state.running_queries.register_interrupt(execution_id, move || interrupt.interrupt());
                }
            }
            wait_for_query_opt(cancel_token, query_timeout, db::sqlite::execute_query_with_max_rows(&p, sql, max_rows))
                .await
        }
        PoolKind::Rqlite(client) => {
            let client = client.clone();
            let max_rows = options.max_rows;
            wait_for_query_opt(
                cancel_token,
                query_timeout,
                db::rqlite_driver::execute_query_with_max_rows(&client, sql, max_rows),
            )
            .await
        }
        PoolKind::Turso(client) => {
            let client = client.clone();
            let max_rows = options.max_rows;
            wait_for_query_opt(
                cancel_token,
                query_timeout,
                db::turso_driver::execute_query_with_max_rows(&client, sql, max_rows),
            )
            .await
        }
        PoolKind::CloudflareD1(client) => {
            let client = client.clone();
            let max_rows = options.max_rows;
            wait_for_query_opt(
                cancel_token,
                query_timeout,
                db::cloudflare_d1_driver::execute_query_with_max_rows(&client, sql, max_rows),
            )
            .await
        }
        PoolKind::ClickHouse(client) => {
            let client = client.clone();
            let database = pool_key.split(':').nth(1).unwrap_or("default").to_string();
            let max_rows = options.max_rows;
            let result = wait_for_query_opt(
                cancel_token,
                query_timeout,
                db::clickhouse_driver::execute_query_with_max_rows(&client, &database, sql, max_rows),
            )
            .await
            .map(|result| truncate_result_with_max_rows(result, max_rows));
            if matches!(result.as_ref(), Err(err) if should_discard_pool_after_error(pool_db_type, err)) {
                state.remove_pool_by_key(pool_key).await;
            }
            result
        }
        PoolKind::SqlServer(client) => {
            let client = client.clone();
            let max_rows = options.max_rows;
            let execution_mode = options.execution_mode;
            let sql = sql_for_execution_context_with_identifier_quote(pool_db_type, sql, schema, Some("["));
            let (mut client, lock_wait_ms) =
                match lock_shared_client_with_wait(&client, cancel_token.clone(), None).await {
                    Ok(value) => value,
                    Err(err) => return Err(err.into()),
                };
            // A display-bound execution must not pay for the rows the caller
            // discards: the driver stops the TDS stream at `max_rows` and marks
            // the connection as abandoned, which then gets dropped instead of
            // returned to the pool. Session-pinned executions keep the old
            // draining path because the connection carries user state.
            let stop_at_row_limit =
                max_rows.is_some() && options.client_session_id.is_none() && !options.use_transaction.unwrap_or(false);
            let execution = async {
                if execution_mode == QueryExecutionMode::Simple {
                    let mut results =
                        db::sqlserver::execute_simple_batch_with_max_rows(&mut client, &sql, max_rows).await?;
                    Ok(results.remove(0))
                } else if stop_at_row_limit {
                    db::sqlserver::execute_query_with_max_rows_stopping(&mut client, &sql, max_rows).await
                } else {
                    db::sqlserver::execute_query_with_max_rows(&mut client, &sql, max_rows).await
                }
            };
            let result = wait_for_query_opt(cancel_token, query_timeout, execution)
                .await
                .map(|result| truncate_result_with_max_rows(result, max_rows))
                .map(|mut result| {
                    result.execution_time_ms += lock_wait_ms;
                    result
                });
            let abandoned = client.take_abandoned_after_row_limit();
            drop(client);
            if abandoned {
                state.remove_pool_by_key(pool_key).await;
            }
            if matches!(result.as_ref(), Err(err) if should_discard_pool_after_error(pool_db_type, err)) {
                state.remove_pool_by_key(pool_key).await;
            }
            result
        }
        PoolKind::Elasticsearch(client) => {
            let client = client.clone();
            let sql = sql.to_string();
            let max_rows = options.max_rows;
            let result = wait_for_query_opt(
                cancel_token,
                query_timeout,
                db::elasticsearch_driver::execute_rest_query_with_cursor(
                    &client,
                    &sql,
                    options.result_session_id.as_deref(),
                ),
            )
            .await
            .map(|result| truncate_result_with_max_rows(result, max_rows));
            if matches!(result.as_ref(), Err(err) if should_discard_pool_after_error(pool_db_type, err)) {
                state.remove_pool_by_key(pool_key).await;
            }
            result
        }
        PoolKind::Easysearch(client) => {
            let client = client.clone();
            let sql = sql.to_string();
            let max_rows = options.max_rows;
            let result = wait_for_query_opt(
                cancel_token,
                query_timeout,
                db::easysearch_driver::execute_rest_query_with_cursor(
                    &client,
                    &sql,
                    options.result_session_id.as_deref(),
                ),
            )
            .await
            .map(|result| truncate_result_with_max_rows(result, max_rows));
            if matches!(result.as_ref(), Err(err) if should_discard_pool_after_error(pool_db_type, err)) {
                state.remove_pool_by_key(pool_key).await;
            }
            result
        }
        PoolKind::Solr(client) => {
            let client = client.clone();
            let sql = sql.to_string();
            let max_rows = options.max_rows;
            // cursorMark 分页只服务文档浏览器;REST 查询是一次性请求,不需要 session 游标。
            let result =
                wait_for_query_opt(cancel_token, query_timeout, db::solr_driver::execute_rest_query(&client, &sql))
                    .await
                    .map(|result| truncate_result_with_max_rows(result, max_rows));
            if matches!(result.as_ref(), Err(err) if should_discard_pool_after_error(pool_db_type, err)) {
                state.remove_pool_by_key(pool_key).await;
            }
            result
        }
        PoolKind::CouchDb(client) => {
            let client = client.clone();
            let sql = sql.to_string();
            let max_rows = options.max_rows;
            let result =
                wait_for_query_opt(cancel_token, query_timeout, db::couchdb_driver::execute_rest_query(&client, &sql))
                    .await
                    .map(|result| truncate_result_with_max_rows(result, max_rows));
            if matches!(result.as_ref(), Err(err) if should_discard_pool_after_error(pool_db_type, err)) {
                state.remove_pool_by_key(pool_key).await;
            }
            result
        }
        PoolKind::Meilisearch(client) => {
            let client = client.clone();
            let sql = sql.to_string();
            let max_rows = options.max_rows;
            let result = wait_for_query_opt(
                cancel_token,
                query_timeout,
                db::meilisearch_driver::execute_rest_query(&client, &sql),
            )
            .await
            .map(|result| truncate_result_with_max_rows(result, max_rows));
            if matches!(result.as_ref(), Err(err) if should_discard_pool_after_error(pool_db_type, err)) {
                state.remove_pool_by_key(pool_key).await;
            }
            result
        }
        PoolKind::Salesforce(client) => {
            let client = client.clone();
            let sql = sql.to_string();
            let max_rows = options.max_rows;
            let result = wait_for_query_opt(cancel_token, query_timeout, async move {
                if let Some(cursor) = options.result_session_id.as_deref() {
                    client.fetch_more(cursor).await
                } else {
                    client.execute_query(&sql, max_rows).await
                }
            })
            .await;
            if matches!(result.as_ref(), Err(err) if should_discard_pool_after_error(pool_db_type, err)) {
                state.remove_pool_by_key(pool_key).await;
            }
            result
        }
        PoolKind::VectorDb(client) => {
            let client = client.clone();
            let sql = sql.to_string();
            let max_rows = options.max_rows;
            let result =
                wait_for_query_opt(cancel_token, query_timeout, db::vector_driver::execute_rest_query(&client, &sql))
                    .await
                    .map(|result| truncate_result_with_max_rows(result, max_rows));
            if matches!(result.as_ref(), Err(err) if should_discard_pool_after_error(pool_db_type, err)) {
                state.remove_pool_by_key(pool_key).await;
            }
            result
        }
        PoolKind::Redis(_) => Err("Use Redis-specific commands".to_string()),
        PoolKind::MongoDb(_) => Err(MONGO_SHELL_COMMAND_HINT.to_string()),
        PoolKind::MessageQueue => Err("Use Message Queue-specific commands".to_string()),
        #[cfg(feature = "mq-admin")]
        PoolKind::Mqtt(_) => Err("Use MQTT-specific commands".to_string()),
        PoolKind::Nacos => Err("Use Nacos-specific commands".to_string()),
        PoolKind::InfluxDb(client) => {
            let client = client.clone();
            let database = pool_key.split(':').nth(1).unwrap_or("default").to_string();
            let max_rows = options.max_rows;
            let result = wait_for_query_opt(
                cancel_token,
                query_timeout,
                db::influxdb_driver::execute_query(&client, &database, sql),
            )
            .await
            .map(|result| truncate_result_with_max_rows(result, max_rows));
            if matches!(result.as_ref(), Err(err) if should_discard_pool_after_error(pool_db_type, err)) {
                state.remove_pool_by_key(pool_key).await;
            }
            result
        }
        PoolKind::InfluxDb3(client) => {
            let client = client.clone();
            let database = pool_key.split(':').nth(1).unwrap_or("default").to_string();
            let max_rows = options.max_rows;
            let result = wait_for_query_opt(
                cancel_token,
                query_timeout,
                db::influxdb3_driver::execute_query(&client, &database, sql, max_rows),
            )
            .await
            .map(|result| truncate_result_with_max_rows(result, max_rows));
            if matches!(result.as_ref(), Err(err) if should_discard_pool_after_error(pool_db_type, err)) {
                state.remove_pool_by_key(pool_key).await;
            }
            result
        }
        PoolKind::VictoriaMetrics(client) => {
            let client = client.clone();
            let max_rows = options.max_rows;
            let result = wait_for_query_opt(
                cancel_token,
                query_timeout,
                db::victoriametrics_driver::execute_query(&client, sql),
            )
            .await
            .map(|result| truncate_result_with_max_rows(result, max_rows));
            if matches!(result.as_ref(), Err(err) if should_discard_pool_after_error(pool_db_type, err)) {
                state.remove_pool_by_key(pool_key).await;
            }
            result
        }
        PoolKind::Agent(client) => {
            let client = client.clone();
            let source_client = client.clone();
            let sql =
                sql_for_execution_context_with_identifier_quote(pool_db_type, sql, schema, client.identifier_quote());
            let database = database.map(|s| s.to_string());
            let schema = schema_for_execution_context(pool_db_type, schema).map(|s| s.to_string());
            let max_rows = options.max_rows;
            let rpc_timeout = query_timeout;
            if is_canceled(&cancel_token) {
                return Err(canceled_error().into());
            }
            let cancel_for_agent = cancel_token.clone();
            let result = async move {
                let lock_started = std::time::Instant::now();
                let mut client = match cancel_for_agent.as_ref() {
                    Some(token) => {
                        tokio::select! {
                            biased;
                            _ = token.cancelled() => return Err(AgentCallError::Canceled {
                                stage: AgentErrorStage::Cancel,
                                operation_outcome: AgentOperationOutcome::Unknown,
                            }),
                            guard = client.lock() => guard,
                        }
                    }
                    None => client.lock().await,
                };
                let lock_ms = lock_started.elapsed().as_secs_f64() * 1000.0;
                let response: Result<db::QueryResult, AgentCallError> = if let Some(session_id) =
                    options.result_session_id.as_deref()
                {
                    let params = agent_fetch_query_page_params(session_id, options.page_size.unwrap_or(MAX_ROWS));
                    client
                        .fetch_query_page_typed_with_timeout_and_cancel(params, rpc_timeout, cancel_for_agent.clone())
                        .await
                } else if options.page_size.is_some() {
                    let params = agent_execute_query_page_params(&sql, database.as_deref(), schema.as_deref(), options);
                    client
                        .execute_query_page_typed_with_timeout_and_cancel(params, rpc_timeout, cancel_for_agent.clone())
                        .await
                } else {
                    let params = agent_execute_query_params(&sql, database.as_deref(), schema.as_deref(), options);
                    client
                        .execute_query_typed_with_timeout_and_cancel(params, rpc_timeout, cancel_for_agent.clone())
                        .await
                };
                response.map(|mut result| {
                    // Older agents have no phase map. Do not imply complete telemetry.
                    if let Some(timings) = result.query_timings_ms.as_mut() {
                        timings.insert("core_lock".into(), lock_ms);
                    }
                    result
                })
            }
            .await
            .map(|result| truncate_result_with_max_rows(result, max_rows));
            if let Err(err) = result.as_ref() {
                discard_agent_pool_after_typed_error(
                    state,
                    pool_key,
                    &source_client,
                    err,
                    RecoveryScope::UserOperation,
                )
                .await;
            }
            typed_agent_error = result.as_ref().err().cloned();
            result.map_err(AgentCallError::into_legacy_string)
        }
        PoolKind::ExternalDriver { config, session, .. } => {
            let config = config.clone();
            let session = session.clone();
            let sql = sql.to_string();
            let schema = schema.map(str::to_string);
            let database = database.unwrap_or_else(|| config.effective_database().unwrap_or("")).to_string();
            let max_rows = options.max_rows;
            let plugin_timeout = query_timeout;
            wait_for_query_opt(cancel_token, query_timeout, async move {
                if let Some(session_id) = options.result_session_id.as_deref() {
                    let params = external_driver_fetch_query_page_params(
                        config.as_ref(),
                        session_id,
                        options.page_size.unwrap_or(MAX_ROWS),
                    );
                    session.invoke_with_timeout::<db::QueryResult>("fetchQueryPage", params, plugin_timeout).await
                } else {
                    invoke_external_driver_query_with_preview_retry(
                        session.as_ref(),
                        config.as_ref(),
                        &sql,
                        &database,
                        schema.as_deref(),
                        &options,
                        plugin_timeout,
                    )
                    .await
                }
            })
            .await
            .map(|result| truncate_result_with_max_rows(result, max_rows))
        }
        PoolKind::PluginConnection(_) => Err("SQL execution is not supported for plugin connections".to_string()),
        PoolKind::HBase(_) => Err("SQL execution is not supported for HBase connections".to_string()),
        PoolKind::DynamoDb(client) => {
            let client = client.clone();
            let sql = sql.to_string();
            let max_rows = options.max_rows.unwrap_or(MAX_ROWS);
            // Keep the AWS SDK cold-path future off this already-large query dispatcher stack.
            let execution = Box::pin(db::dynamodb_driver::execute_statement(&client, &sql, max_rows));
            wait_for_query_opt(cancel_token, query_timeout, execution).await
        }
        PoolKind::Consul(_) => Err("SQL execution is not supported for Consul connections".to_string()),
    };
    result
        .map(|mut result| {
            result.execution_time_ms = result.execution_time_ms.max(dispatch_start.elapsed().as_millis());
            result
        })
        .map_err(|error| {
            #[cfg(feature = "duckdb-sidecar")]
            if let Some(duckdb_error) = typed_duckdb_error {
                return QueryExecutionError::DuckDb { code: duckdb_error.code, message: duckdb_error.message };
            }
            if let Some(agent_error) = typed_agent_error {
                return QueryExecutionError::Agent(agent_error);
            }
            // PostgreSQL reports a cursor position as a marker suffix on the
            // driver message. Resolve it here, while the executed statement text
            // is still available, into a typed field. The marker is stripped even
            // when it cannot be resolved, so it can never reach a user-facing
            // message. The PostgreSQL driver also backs Redshift/GaussDB/Kwdb/
            // QuestDB/openGauss, so this is not gated on `DatabaseType::Postgres`.
            if error.contains(crate::sql_error_position::SQL_ERROR_POSITION_MARKER) {
                let (message, position) = crate::sql_error_position::take_message_position(&error, sql);
                return match position {
                    Some(position) => QueryExecutionError::SqlWithPosition { message, position },
                    None => QueryExecutionError::Legacy(message),
                };
            }
            QueryExecutionError::Legacy(error)
        })
        .map_err(|error| classify_query_error(pool_db_type, error))
}

fn classify_query_error(db_type: Option<DatabaseType>, error: QueryExecutionError) -> QueryExecutionError {
    match error {
        QueryExecutionError::Legacy(message) if message == QUERY_CANCELED => canceled_query_execution_error(),
        QueryExecutionError::Legacy(message) if is_dbx_query_timeout_error(&message.to_ascii_lowercase()) => {
            QueryExecutionError::Timeout(message)
        }
        QueryExecutionError::Legacy(message) if is_native_sql_server_error(db_type, &message) => {
            QueryExecutionError::Sql(message)
        }
        other => other,
    }
}

fn is_native_sql_server_error(db_type: Option<DatabaseType>, message: &str) -> bool {
    let message = message.trim_start();
    match db_type {
        Some(DatabaseType::Postgres) => message.starts_with("ERROR:"),
        Some(DatabaseType::Mysql) => message.starts_with("Server error: `ERROR "),
        _ => false,
    }
}

pub async fn do_execute(
    state: &AppState,
    pool_key: &str,
    mysql_dialect: db::mysql::MySqlQueryDialect,
    database: Option<&str>,
    sql: &str,
    schema: Option<&str>,
    cancel_token: Option<CancellationToken>,
    options: QueryExecutionOptions,
) -> Result<db::QueryResult, String> {
    do_execute_typed(state, pool_key, mysql_dialect, database, sql, schema, cancel_token, options, &mut None)
        .await
        .map_err(QueryExecutionError::into_legacy_string)
}

async fn invoke_external_driver_query_page(
    session: &crate::plugins::PluginDriverSession,
    params: serde_json::Value,
    plugin_timeout: Option<Duration>,
) -> Result<db::QueryResult, String> {
    match session.invoke_with_timeout::<db::QueryResult>("executeQueryPage", params.clone(), plugin_timeout).await {
        Ok(result) => Ok(result),
        Err(error) if is_external_driver_method_unsupported(&error, "executeQueryPage") => {
            // Plugins installed by older DBX releases predate cursor pagination. Keep
            // basic queries usable until the user updates the plugin, without retrying
            // actual JDBC/SQL failures that may have side effects.
            log::warn!("[query][external-driver] executeQueryPage unsupported; falling back to executeQuery");
            session.invoke_with_timeout::<db::QueryResult>("executeQuery", params, plugin_timeout).await
        }
        Err(error) => Err(error),
    }
}

async fn invoke_external_driver_query_with_preview_retry(
    session: &crate::plugins::PluginDriverSession,
    config: &crate::models::connection::ConnectionConfig,
    sql: &str,
    database: &str,
    schema: Option<&str>,
    options: &QueryExecutionOptions,
    plugin_timeout: Option<Duration>,
) -> Result<db::QueryResult, String> {
    let result = if options.page_size.is_some() {
        let params = external_driver_query_params(config, sql, database, schema, options);
        invoke_external_driver_query_page(session, params, plugin_timeout).await
    } else {
        let params = external_driver_query_params(config, sql, database, schema, options);
        session.invoke_with_timeout::<db::QueryResult>("executeQuery", params, plugin_timeout).await
    };

    let Err(error) = result else {
        return result;
    };
    if !options.table_data_preview || !is_external_driver_invalid_utf8_error(&error) {
        return Err(error);
    }
    let Some(fallback_sql) = external_driver_preview_fallback_sql(sql) else {
        return Err(error);
    };

    log::warn!(
        "[query][external-driver] JDBC preview failed with invalid UTF-8; retrying without generated left() wrappers"
    );
    if options.page_size.is_some() {
        let params = external_driver_query_params(config, &fallback_sql, database, schema, options);
        invoke_external_driver_query_page(session, params, plugin_timeout).await
    } else {
        let params = external_driver_query_params(config, &fallback_sql, database, schema, options);
        session.invoke_with_timeout::<db::QueryResult>("executeQuery", params, plugin_timeout).await
    }
}

/// PostgreSQL can reject a character preview when legacy data contains an
/// invalid UTF-8 byte exactly at the `left`/`substring` boundary.  JDBC can
/// still return the unmodified text value, so table previews may safely retry
/// without the generated `left(...)` wrappers and let the existing marker
/// extraction truncate the value client-side.
fn is_external_driver_invalid_utf8_error(error: &str) -> bool {
    let normalized = error.to_ascii_lowercase();
    normalized.contains("invalid byte sequence for encoding") && normalized.contains("utf8")
}

/// Returns the SQL to retry when a native PostgreSQL table-data preview dies
/// with the server's invalid-UTF-8 error. On SQL_ASCII databases the generated
/// `left()` preview slices by byte and can split a multi-byte UTF-8 sequence
/// (#8919); like the JDBC path, re-run once without the generated wrappers and
/// let marker extraction truncate the value client-side. Only generated
/// preview SELECTs are rewritten — user SQL is never touched, and a genuinely
/// invalid value simply surfaces the original error again.
fn postgres_preview_fallback_retry_sql(options: &QueryExecutionOptions, error: &str, sql: &str) -> Option<String> {
    if !options.table_data_preview || !is_external_driver_invalid_utf8_error(error) {
        return None;
    }
    external_driver_preview_fallback_sql(sql)
}

/// MySQL-compatible servers that are not actually MySQL (for example HighGo's
/// MySQL protocol) do not have a `left(json, integer)` overload. The generated
/// table-data preview wraps JSON columns in `LEFT(...)`, so the preview fails
/// even though the table itself is readable. Retry once without the generated
/// wrappers and let the existing large-value markers truncate client-side.
fn preview_missing_left_function_fallback_sql(
    options: &QueryExecutionOptions,
    error: &str,
    sql: &str,
) -> Option<String> {
    if !options.table_data_preview || !is_preview_missing_left_function_error(error) {
        return None;
    }
    external_driver_preview_fallback_sql(sql)
}

fn is_preview_missing_left_function_error(error: &str) -> bool {
    let normalized = error.to_ascii_lowercase();
    normalized.contains("function left(") && normalized.contains("does not exist")
}

/// Dispatches one statement on a native PostgreSQL pool according to the
/// connection's execution mode and schema context.
#[allow(clippy::too_many_arguments)]
async fn execute_postgres_pool_statement(
    pool: &deadpool_postgres::Pool,
    db_type: Option<DatabaseType>,
    schema: Option<&str>,
    sql: &str,
    max_rows: Option<usize>,
    prefer_text_protocol: bool,
    execution_mode: QueryExecutionMode,
    cancel_token: Option<CancellationToken>,
    budget: DbOperationBudget,
    cancel_context: Option<db::postgres::PostgresCancelContext>,
) -> Result<db::QueryResult, String> {
    if execution_mode == QueryExecutionMode::PostgresReadOnlyTransaction {
        db::postgres::execute_query_in_read_only_transaction_with_rollback(
            pool,
            schema,
            sql,
            max_rows,
            cancel_token,
            budget,
            cancel_context,
        )
        .await
    } else if let Some(schema) = schema {
        db::postgres::execute_query_with_schema_and_max_rows_and_cancel(
            pool,
            db_type,
            schema,
            sql,
            max_rows,
            cancel_token,
            budget,
            cancel_context,
            prefer_text_protocol,
        )
        .await
    } else {
        db::postgres::execute_query_with_max_rows_and_cancel(
            pool,
            sql,
            max_rows,
            cancel_token,
            budget,
            cancel_context,
            prefer_text_protocol,
        )
        .await
    }
}

fn is_sql_word_byte(byte: u8) -> bool {
    byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'$')
}

fn sql_keyword_at(bytes: &[u8], index: usize, keyword: &[u8]) -> bool {
    let end = index.saturating_add(keyword.len());
    end <= bytes.len()
        && bytes[index..end].eq_ignore_ascii_case(keyword)
        && (index == 0 || !is_sql_word_byte(bytes[index - 1]))
        && (end == bytes.len() || !is_sql_word_byte(bytes[end]))
}

/// Skip a quoted literal/identifier or SQL comment and return the first byte
/// after it. This keeps keyword and parenthesis scans from interpreting commas
/// or `FROM` text embedded in user predicates/literals.
fn skip_sql_quoted_or_comment(bytes: &[u8], start: usize) -> Option<usize> {
    let quote = *bytes.get(start)?;
    let (end_byte, doubled) = match quote {
        b'\'' => (b'\'', true),
        b'"' => (b'"', true),
        b'`' => (b'`', true),
        b'[' => (b']', true),
        b'-' if bytes.get(start + 1) == Some(&b'-') => {
            return Some(
                bytes[start + 2..].iter().position(|byte| *byte == b'\n').map_or(bytes.len(), |p| start + 2 + p + 1),
            );
        }
        b'/' if bytes.get(start + 1) == Some(&b'*') => {
            return Some(
                bytes[start + 2..].windows(2).position(|pair| pair == b"*/").map_or(bytes.len(), |p| start + 2 + p + 2),
            );
        }
        _ => return None,
    };
    let mut index = start + 1;
    while index < bytes.len() {
        if quote == b'\'' && bytes[index] == b'\\' {
            index = (index + 2).min(bytes.len());
        } else if bytes[index] == end_byte {
            if doubled && bytes.get(index + 1) == Some(&end_byte) {
                index += 2;
            } else {
                return Some(index + 1);
            }
        } else {
            index += 1;
        }
    }
    Some(bytes.len())
}

fn find_top_level_sql_keyword(sql: &str, start: usize, keyword: &[u8]) -> Option<usize> {
    let bytes = sql.as_bytes();
    let mut depth = 0usize;
    let mut index = start;
    while index < bytes.len() {
        let byte = bytes[index];
        if let Some(next) = skip_sql_quoted_or_comment(bytes, index) {
            index = next;
            continue;
        }
        match byte {
            b'(' => depth = depth.saturating_add(1),
            b')' => depth = depth.saturating_sub(1),
            _ if depth == 0 && sql_keyword_at(bytes, index, keyword) => return Some(index),
            _ => {}
        }
        index += 1;
    }
    None
}

fn find_preview_left_call(sql: &str, name_start: usize) -> Option<(usize, usize, usize)> {
    let bytes = sql.as_bytes();
    let mut open = name_start + 4;
    while bytes.get(open).is_some_and(u8::is_ascii_whitespace) {
        open += 1;
    }
    if bytes.get(open) != Some(&b'(') {
        return None;
    }

    let mut depth = 0usize;
    let mut comma = None;
    let mut close = None;
    let mut index = open;
    while index < bytes.len() {
        let byte = bytes[index];
        if let Some(next) = skip_sql_quoted_or_comment(bytes, index) {
            index = next;
            continue;
        }
        match byte {
            b'(' => depth = depth.saturating_add(1),
            b')' => {
                depth = depth.saturating_sub(1);
                if depth == 0 {
                    close = Some(index);
                    break;
                }
            }
            b',' if depth == 1 => {
                if comma.is_some() {
                    return None;
                }
                comma = Some(index);
            }
            _ => {}
        }
        index += 1;
    }

    let close = close?;
    let comma = comma?;
    let first = &sql[open + 1..comma];
    let second = &sql[comma + 1..close];
    if first.trim().is_empty() || second.trim().is_empty() || !second.trim().bytes().all(|byte| byte.is_ascii_digit()) {
        return None;
    }
    let first_start = open + 1 + first.len() - first.trim_start().len();
    let first_end = open + 1 + first.trim_end().len();
    Some((close, first_start, first_end))
}

fn rewrite_external_driver_preview_projection(projection: &str) -> Option<String> {
    let bytes = projection.as_bytes();
    let mut replacements = Vec::new();
    let mut index = 0usize;
    while index < bytes.len() {
        if let Some(next) = skip_sql_quoted_or_comment(bytes, index) {
            index = next;
            continue;
        }
        if sql_keyword_at(bytes, index, b"left") {
            let previous = index.checked_sub(1).and_then(|position| bytes.get(position)).copied();
            if previous != Some(b'.') {
                if let Some((close, first_start, first_end)) = find_preview_left_call(projection, index) {
                    replacements.push((index, close + 1, projection[first_start..first_end].to_string()));
                    index = close + 1;
                    continue;
                }
            }
        }
        index += 1;
    }

    if replacements.is_empty() {
        return None;
    }
    let mut rewritten = projection.to_string();
    for (start, end, replacement) in replacements.into_iter().rev() {
        rewritten.replace_range(start..end, &replacement);
    }
    Some(rewritten)
}

fn external_driver_preview_fallback_sql(sql: &str) -> Option<String> {
    let select_start = find_top_level_sql_keyword(sql, 0, b"select")?;
    if !sql[..select_start].trim().is_empty() {
        return None;
    }
    let select_end = select_start + b"select".len();
    let from_start = find_top_level_sql_keyword(sql, select_end, b"from")?;
    let projection = &sql[select_end..from_start];
    if !projection.contains(crate::sql_dialect::DBX_LARGE_VALUE_BYTES_COLUMN_PREFIX) {
        return None;
    }
    let rewritten_projection = rewrite_external_driver_preview_projection(projection)?;
    Some(format!("{}{}{}", &sql[..select_end], rewritten_projection, &sql[from_start..]))
}

pub(crate) fn is_external_driver_method_unsupported(error: &str, method: &str) -> bool {
    let normalized = error.to_ascii_lowercase();
    let method = method.to_ascii_lowercase();
    normalized.contains(&method)
        && (normalized.contains("unsupported jdbc plugin method")
            || normalized.contains("unknown method")
            || normalized.contains("method not found"))
}

pub(crate) fn external_driver_query_params(
    config: &crate::models::connection::ConnectionConfig,
    sql: &str,
    database: &str,
    schema: Option<&str>,
    options: &QueryExecutionOptions,
) -> serde_json::Value {
    let mut params = serde_json::json!({
        "connection": config,
        "sql": sql,
        "database": database,
        "schema": schema,
        "maxRows": options.max_rows.unwrap_or(MAX_ROWS),
    });
    if options.defer_lobs {
        params["deferLobs"] = serde_json::json!(true);
    }
    if let Some(fetch_size) = options.fetch_size {
        params["fetchSize"] = serde_json::json!(fetch_size);
    }
    if let Some(row_offset) = options.row_offset {
        params["rowOffset"] = serde_json::json!(agent_protocol_row_offset(row_offset));
    }
    if let Some(timeout_secs) = options.timeout_secs {
        params["timeoutSecs"] = serde_json::json!(timeout_secs);
    }
    if let Some(page_size) = options.page_size {
        params["pageSize"] = serde_json::json!(page_size);
    }
    params
}

fn external_driver_fetch_query_page_params(
    config: &crate::models::connection::ConnectionConfig,
    session_id: &str,
    page_size: usize,
) -> serde_json::Value {
    serde_json::json!({
        "connection": config,
        "sessionId": session_id,
        "pageSize": page_size,
    })
}

pub async fn execute_sql_statement(
    state: &AppState,
    connection_id: &str,
    database: &str,
    sql: &str,
    schema: Option<&str>,
    cancel_token: Option<CancellationToken>,
) -> Result<db::QueryResult, String> {
    execute_sql_statement_with_options(
        state,
        connection_id,
        database,
        sql,
        schema,
        cancel_token,
        QueryExecutionOptions::default(),
    )
    .await
}

pub async fn execute_sql_statement_with_options_typed(
    state: &AppState,
    connection_id: &str,
    database: &str,
    sql: &str,
    schema: Option<&str>,
    cancel_token: Option<CancellationToken>,
    options: QueryExecutionOptions,
) -> Result<db::QueryResult, QueryExecutionError> {
    // `_with_outcome` is a second async layer in front of
    // `execute_sql_statement_with_options_typed_inner`. Awaiting it inline nests
    // one more concrete future type into every caller's generator, which pushed
    // Rust targets of this workspace past rustc's query depth limit on Linux
    // (`error: queries overflow the depth limit!`). Erasing the type keeps this
    // wrapper as shallow as it was before the outcome was added.
    let executed: ExecutedSqlStatement<'_> = Box::pin(execute_sql_statement_with_options_typed_with_outcome(
        state,
        connection_id,
        database,
        sql,
        schema,
        cancel_token,
        options,
    ));
    executed.await.map(|(result, _)| result)
}

/// Boxed, type-erased run of one SQL statement, awaiting it without nesting
/// another concrete future type into the caller's generator.
type ExecutedSqlStatement<'a> =
    std::pin::Pin<Box<dyn std::future::Future<Output = ExecutedSqlStatementOutcome> + Send + 'a>>;

/// Result of [`ExecutedSqlStatement`].
type ExecutedSqlStatementOutcome = Result<(db::QueryResult, Option<MysqlAutoCommitTransaction>), QueryExecutionError>;

/// Same as [`execute_sql_statement_with_options_typed`], but also returns how
/// the MySQL auto-commit settlement left the tab-scoped connection (see
/// [`MysqlAutoCommitTransaction`]). `None` means the statement did not run on a
/// tab-scoped MySQL connection, so there is no such state to report.
pub(crate) async fn execute_sql_statement_with_options_typed_with_outcome(
    state: &AppState,
    connection_id: &str,
    database: &str,
    sql: &str,
    schema: Option<&str>,
    cancel_token: Option<CancellationToken>,
    options: QueryExecutionOptions,
) -> Result<(db::QueryResult, Option<MysqlAutoCommitTransaction>), QueryExecutionError> {
    let db_type = connection_database_type(state, connection_id).await;
    let invalidate = crate::object_cache::sql_may_change_object_metadata(sql, db_type);
    let mut transaction = None;
    // Same contract as the multi-statement entry point: a single statement is
    // reported with the duration of the whole request, including creating or
    // reconnecting the pool, so the summary matches the loading indicator.
    let request_start = std::time::Instant::now();
    let result = execute_sql_statement_with_options_typed_inner(
        state,
        connection_id,
        database,
        sql,
        schema,
        cancel_token,
        options,
        &mut transaction,
    )
    .await;
    if invalidate {
        crate::object_cache::invalidate_connection_object_cache(&state.storage, connection_id).await;
    }
    result.map(|mut result| {
        result.execution_time_ms = result.execution_time_ms.max(request_start.elapsed().as_millis());
        (result, transaction)
    })
}

async fn recover_postgres_create_table_after_connection_error(
    state: &AppState,
    connection_id: &str,
    database: &str,
    schema: Option<&str>,
    sql: &str,
    cancel_token: Option<CancellationToken>,
    options: &QueryExecutionOptions,
    db_type: Option<DatabaseType>,
    initial_error: &QueryExecutionError,
) -> Option<Result<db::QueryResult, QueryExecutionError>> {
    if is_canceled(&cancel_token)
        || !should_verify_postgres_create_table_after_connection_error(db_type, sql, &initial_error.to_string())
    {
        return None;
    }

    let pool_database = query_pool_database(database, options.catalog.as_deref());
    let new_key = state
        .reconnect_pool_for_session(connection_id, pool_database, options.client_session_id.as_deref())
        .await
        .ok()?;
    let mysql_dialect = connection_mysql_query_dialect(state, connection_id).await;

    // A compatible PostgreSQL server may close the session after committing a
    // CREATE TABLE. Retry once on a fresh session: this covers a disconnect
    // before execution, while a duplicate relation below confirms that the
    // original request already took effect.
    let retry_error = match do_execute_typed(
        state,
        &new_key,
        mysql_dialect,
        Some(database),
        sql,
        schema,
        cancel_token.clone(),
        options.clone(),
        &mut None,
    )
    .await
    {
        Ok(result) => return Some(Ok(result)),
        Err(error) => error,
    };
    if !is_postgres_duplicate_relation_error(&retry_error.to_string()) {
        return None;
    }

    let (qualified_schema, table_name) = postgres_create_table_relation(sql)?;
    let schema_predicate = qualified_schema
        .or_else(|| schema.map(str::to_owned))
        .map(|schema_name| format!("n.nspname = {}", db::postgres::pg_quote_literal(&schema_name)))
        .unwrap_or_else(|| "n.nspname = current_schema()".to_string());
    let verify_sql = format!(
        "SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE {} AND c.relname = {}) AS dbx_create_table_applied",
        schema_predicate,
        db::postgres::pg_quote_literal(&table_name),
    );
    let verify_options = QueryExecutionOptions {
        max_rows: Some(1),
        client_session_id: options.client_session_id.clone(),
        ..Default::default()
    };
    let verified = do_execute_typed(
        state,
        &new_key,
        mysql_dialect,
        Some(database),
        &verify_sql,
        None,
        cancel_token,
        verify_options,
        &mut None,
    )
    .await
    .ok()
    .and_then(|result| result.rows.first().and_then(|row| row.first()).and_then(|value| value.as_bool()))
    .unwrap_or(false);
    verified.then(|| Ok(empty_query_result(0)))
}

async fn execute_sql_statement_with_options_typed_inner(
    state: &AppState,
    connection_id: &str,
    database: &str,
    sql: &str,
    schema: Option<&str>,
    cancel_token: Option<CancellationToken>,
    options: QueryExecutionOptions,
    transaction_outcome: &mut Option<MysqlAutoCommitTransaction>,
) -> Result<db::QueryResult, QueryExecutionError> {
    // MongoDB connections use shell-style commands dispatched through the
    // frontend parser. Queries that fall through to the generic SQL executor
    // (e.g. typos) must be rejected before any pool/key creation so that
    // session-scoped pools do not leak MongoDB Clients and SSH tunnels.
    if connection_is_mongodb(state, connection_id).await {
        return Err(MONGO_SHELL_COMMAND_HINT.into());
    }

    let db_type = connection_database_type(state, connection_id).await;
    validate_query_execution_mode(db_type, sql, &options)?;
    let has_executable_sql = db_type.map_or_else(
        || crate::sql::has_executable_sql(sql),
        |db_type| crate::sql::has_executable_sql_for_database(sql, db_type),
    );
    if !has_executable_sql && options.result_session_id.is_none() {
        return Ok(empty_query_result(0));
    }

    if let Some(target_database) = postgres_drop_database_target(db_type, sql) {
        return execute_postgres_drop_database(state, connection_id, &target_database, sql, cancel_token, options)
            .await
            .map_err(Into::into);
    }

    // When a query tab has a client session, keep even database-less execution
    // on that tab-scoped pool so connection-level state (for example MySQL @vars)
    // survives across runs.
    let pool_database = query_pool_database(database, options.catalog.as_deref());
    let pool_key = state
        .get_or_create_pool_for_session(connection_id, pool_database, options.client_session_id.as_deref())
        .await
        .map_err(|e| query_error_with_omitted_sql_context(&e, sql))?;

    if is_canceled(&cancel_token) {
        return Err(pre_dispatch_canceled_query_execution_error());
    }

    let mysql_dialect = connection_mysql_query_dialect(state, connection_id).await;
    let result = do_execute_typed(
        state,
        &pool_key,
        mysql_dialect,
        Some(database),
        sql,
        schema,
        cancel_token.clone(),
        options.clone(),
        transaction_outcome,
    )
    .await;

    let with_sql_context = |result: Result<db::QueryResult, QueryExecutionError>| {
        result.map_err(|error| error.with_omitted_sql_context(sql))
    };

    if let Err(error) = &result {
        if let Some(fallback_sql) = preview_missing_left_function_fallback_sql(&options, &error.to_string(), sql) {
            log::warn!(
                "[query] table data preview failed because the server has no left() overload; retrying without generated left() wrappers"
            );
            return with_sql_context(
                do_execute_typed(
                    state,
                    &pool_key,
                    mysql_dialect,
                    Some(database),
                    &fallback_sql,
                    schema,
                    cancel_token.clone(),
                    options.clone(),
                    &mut None,
                )
                .await,
            );
        }
    }

    let action = result.as_ref().err().map(|error| query_execution_error_action(db_type, sql, error));
    if let Some(initial_error) = result.as_ref().err() {
        if let Some(recovered) = recover_postgres_create_table_after_connection_error(
            state,
            connection_id,
            database,
            schema,
            sql,
            cancel_token.clone(),
            &options,
            db_type,
            initial_error,
        )
        .await
        {
            return with_sql_context(recovered);
        }
    }
    match action {
        Some(PoolErrorAction::ReconnectAndRetry) if !is_canceled(&cancel_token) => {
            let pool_database = query_pool_database(database, options.catalog.as_deref());
            let new_key = state
                .reconnect_pool_for_session(connection_id, pool_database, options.client_session_id.as_deref())
                .await
                .map_err(|e| query_error_with_omitted_sql_context(&e, sql))?;
            with_sql_context(
                do_execute_typed(
                    state,
                    &new_key,
                    mysql_dialect,
                    Some(database),
                    sql,
                    schema,
                    cancel_token,
                    options,
                    &mut None,
                )
                .await,
            )
        }
        Some(PoolErrorAction::Discard) => {
            // Agent execution owns structured quarantine/runtime replacement before
            // returning. Native drivers retain the existing caller-side cleanup.
            if !db_type.is_some_and(|db_type| database_capabilities::is_agent_type(&db_type)) {
                state.remove_pool_by_key(&pool_key).await;
            }
            with_sql_context(result)
        }
        _ => with_sql_context(result),
    }
}

pub async fn execute_sql_statement_with_options(
    state: &AppState,
    connection_id: &str,
    database: &str,
    sql: &str,
    schema: Option<&str>,
    cancel_token: Option<CancellationToken>,
    options: QueryExecutionOptions,
) -> Result<db::QueryResult, String> {
    execute_sql_statement_with_options_typed(state, connection_id, database, sql, schema, cancel_token, options)
        .await
        .map_err(QueryExecutionError::into_legacy_string)
}

async fn execute_postgres_drop_database(
    state: &AppState,
    connection_id: &str,
    target_database: &str,
    sql: &str,
    cancel_token: Option<CancellationToken>,
    options: QueryExecutionOptions,
) -> Result<db::QueryResult, String> {
    state.close_database_pool(connection_id, Some(target_database)).await?;

    let admin_database = postgres_drop_database_admin_database(target_database);
    let pool_key = state
        .get_or_create_pool_for_session(connection_id, Some(admin_database), options.client_session_id.as_deref())
        .await
        .map_err(|e| query_error_with_omitted_sql_context(&e, sql))?;
    if let Some(execution_id) = options.execution_id.as_deref() {
        state.running_queries.set_pool_key(execution_id, pool_key.clone());
    }
    state.touch_pool_activity(&pool_key).await;
    let _activity_touch = state.pool_activity_touch(pool_key.as_str());

    if is_canceled(&cancel_token) {
        return Err(canceled_error());
    }

    check_read_only_for_connection(state, &pool_key, sql).await?;
    let pool = {
        let pool_handle = state.pool_handle(&pool_key).await;
        match pool_handle.as_ref() {
            Some(PoolKind::Postgres(pool)) => pool.clone(),
            Some(_) => return Err("DROP DATABASE reconnect did not create a PostgreSQL connection".to_string()),
            None => return Err("Connection not found".to_string()),
        }
    };

    let query_timeout = resolve_query_timeout(options.timeout_secs);
    let max_rows = options.max_rows;
    wait_for_query_opt(cancel_token, query_timeout, async {
        db::postgres::terminate_current_user_database_backends(&pool, target_database).await?;
        db::postgres::execute_query_with_max_rows(&pool, sql, max_rows).await
    })
    .await
}

fn postgres_drop_database_target(db_type: Option<DatabaseType>, sql: &str) -> Option<String> {
    if db_type != Some(DatabaseType::Postgres) {
        return None;
    }
    parse_drop_database_target(sql)
}

fn postgres_drop_database_admin_database(target_database: &str) -> &'static str {
    if target_database.eq_ignore_ascii_case("postgres") {
        "template1"
    } else {
        "postgres"
    }
}

fn parse_drop_database_target(sql: &str) -> Option<String> {
    let dialect = PostgreSqlDialect {};
    let statements = Parser::parse_sql(&dialect, sql).ok()?;
    let [Statement::Drop { object_type, names, .. }] = statements.as_slice() else {
        return None;
    };
    if *object_type != ObjectType::Database || names.len() != 1 {
        return None;
    }

    let parts = &names[0].0;
    if parts.len() != 1 {
        return None;
    }
    parts[0].as_ident().map(|ident| ident.value.clone())
}

#[derive(Debug, Clone, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LargeValueRequest {
    pub connection_id: String,
    pub database: String,
    pub value_ref: String,
    #[serde(default)]
    pub offset: u64,
    #[serde(default)]
    pub limit: u32,
    pub execution_id: Option<String>,
    pub client_session_id: Option<String>,
    pub catalog: Option<String>,
    pub txn_session_id: Option<String>,
    pub download_encoding: Option<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
#[serde(rename_all = "lowercase")]
pub enum LargeValueCompletion {
    Failed,
    Cancelled,
    Unknown,
}

#[derive(Debug, Clone)]
pub struct LargeValueOperationError {
    pub status: LargeValueCompletion,
    pub message: String,
}

impl std::fmt::Display for LargeValueOperationError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str(&self.message)
    }
}

impl std::error::Error for LargeValueOperationError {}

impl From<String> for LargeValueOperationError {
    fn from(message: String) -> Self {
        Self { status: LargeValueCompletion::Failed, message }
    }
}

impl From<&str> for LargeValueOperationError {
    fn from(message: &str) -> Self {
        message.to_string().into()
    }
}

impl LargeValueOperationError {
    fn cancelled() -> Self {
        Self { status: LargeValueCompletion::Cancelled, message: canceled_error() }
    }

    fn unknown(message: String) -> Self {
        Self { status: LargeValueCompletion::Unknown, message }
    }

    fn from_agent(error: AgentCallError) -> Self {
        use crate::db::agent_driver::AgentErrorCategory;
        let status = match &error {
            AgentCallError::Structured { context, .. } if context.category == AgentErrorCategory::Canceled => {
                LargeValueCompletion::Cancelled
            }
            AgentCallError::Structured { .. } | AgentCallError::Legacy { .. } => LargeValueCompletion::Failed,
            _ => LargeValueCompletion::Unknown,
        };
        Self { status, message: error.into_legacy_string() }
    }
}

async fn wait_for_large_value_resource<T>(
    cancel: Option<CancellationToken>,
    timeout: Option<Duration>,
    future: impl std::future::Future<Output = T>,
    report_completion: bool,
) -> Result<T, LargeValueOperationError> {
    if !report_completion {
        return wait_for_value_opt(cancel, timeout, future).await.map_err(Into::into);
    }
    if let Some(token) = cancel {
        tokio::select! {
            biased;
            _ = token.cancelled() => Err(LargeValueOperationError::cancelled()),
            value = wait_for_value_opt(None, timeout, future) => value.map_err(Into::into),
        }
    } else {
        wait_for_value_opt(None, timeout, future).await.map_err(Into::into)
    }
}

/// Fetches only the original captured locator; never retries by selecting the current row.
pub async fn request_large_value(
    state: &AppState,
    request: LargeValueRequest,
    release: bool,
) -> Result<serde_json::Value, String> {
    if request.value_ref.len() > 128 || (!release && (request.limit == 0 || request.limit > 4096)) {
        return Err("Invalid LOB chunk request".to_string());
    }
    let registered = request.execution_id.as_ref().map(|id| {
        state.running_queries.register_task(
            id.clone(),
            crate::query_cancel::RunningTaskMetadata::query(
                request.connection_id.clone(),
                request.database.clone(),
                request.client_session_id.clone(),
            ),
        )
    });
    let cancel_token = registered.as_ref().map(|task| task.token());
    request_large_value_with_cancel(state, request, release, cancel_token).await
}

async fn request_large_value_with_cancel(
    state: &AppState,
    request: LargeValueRequest,
    release: bool,
    cancel_token: Option<CancellationToken>,
) -> Result<serde_json::Value, String> {
    request_large_value_internal(state, request, release, cancel_token, false).await.map_err(|error| error.message)
}

pub async fn request_large_value_reported(
    state: &AppState,
    request: LargeValueRequest,
) -> Result<serde_json::Value, LargeValueOperationError> {
    let registered = request.execution_id.as_ref().map(|id| {
        state.running_queries.register_task(
            id.clone(),
            crate::query_cancel::RunningTaskMetadata::query(
                request.connection_id.clone(),
                request.database.clone(),
                request.client_session_id.clone(),
            ),
        )
    });
    let cancel = registered.as_ref().map(|task| task.token());
    request_large_value_internal(state, request, false, cancel, true).await
}

async fn request_large_value_internal(
    state: &AppState,
    request: LargeValueRequest,
    release: bool,
    cancel_token: Option<CancellationToken>,
    report_completion: bool,
) -> Result<serde_json::Value, LargeValueOperationError> {
    if request.value_ref.len() > 128 || (!release && (request.limit == 0 || request.limit > 4096)) {
        return Err("Invalid LOB chunk request".into());
    }
    let method = if release { "release_large_value" } else { "read_large_value_chunk" };
    let params = serde_json::json!({ "valueRef": request.value_ref, "offset": request.offset, "limit": request.limit });
    let timeout = Some(std::time::Duration::from_secs(30));
    if let Some(txn_id) = request.txn_session_id.as_deref() {
        let connection = {
            let mut sessions = wait_for_large_value_resource(
                cancel_token.clone(),
                timeout,
                state.transaction_sessions.write(),
                report_completion,
            )
            .await?;
            let session = sessions.get_mut(txn_id).ok_or(MANUAL_TRANSACTION_SESSION_NOT_FOUND_ERROR)?;
            if session.connection_id != request.connection_id
                || session.busy
                || session.last_activity.elapsed() >= session.idle_timeout
            {
                return Err("LOB transaction context expired or busy".into());
            }
            session.last_activity = std::time::Instant::now();
            Arc::clone(&session.connection)
        };
        let connection =
            wait_for_large_value_resource(cancel_token.clone(), timeout, connection.lock(), report_completion).await?;
        let result = match &*connection {
            TxnConnection::Agent { client, .. } => {
                let mut client =
                    wait_for_large_value_resource(cancel_token.clone(), timeout, client.lock(), report_completion)
                        .await?;
                if report_completion {
                    if is_canceled(&cancel_token) {
                        return Err(LargeValueOperationError::cancelled());
                    }
                    client
                        .call_typed_with_timeout_and_cancel(method, params, timeout, None)
                        .await
                        .map_err(LargeValueOperationError::from_agent)
                } else {
                    client
                        .call_with_timeout_and_cancel(method, params, timeout, cancel_token.clone())
                        .await
                        .map_err(Into::into)
                }
            }
            TxnConnection::ExternalDriver { session, config, .. } => {
                let mut params = params;
                params["connection"] = serde_json::json!(config.as_ref());
                if report_completion {
                    if is_canceled(&cancel_token) {
                        return Err(LargeValueOperationError::cancelled());
                    }
                    session
                        .invoke_with_timeout::<serde_json::Value>(method, params, timeout)
                        .await
                        .map_err(LargeValueOperationError::unknown)
                } else {
                    wait_for_value_opt(
                        cancel_token.clone(),
                        timeout,
                        session.invoke_with_timeout::<serde_json::Value>(method, params, timeout),
                    )
                    .await?
                    .map_err(Into::into)
                }
            }
            _ => return Err("LOB snapshot chunks require a supported transaction".into()),
        };
        if report_completion {
            // An unconfirmed RPC must not become a settled local error after a
            // concurrent transaction removal or cancellation.
            let value = result?;
            if is_canceled(&cancel_token) {
                return Err(LargeValueOperationError::cancelled());
            }
            if !wait_for_large_value_resource(cancel_token, timeout, state.transaction_sessions.read(), true)
                .await?
                .contains_key(txn_id)
            {
                return Err("LOB transaction context expired".into());
            }
            return Ok(value);
        }
        if !wait_for_value_opt(cancel_token, timeout, state.transaction_sessions.read()).await?.contains_key(txn_id) {
            return Err("LOB transaction context expired".into());
        }
        return result;
    }
    let pool_database = query_pool_database(&request.database, request.catalog.as_deref());
    let (pool_key, handle) = wait_for_large_value_resource(
        cancel_token.clone(),
        timeout,
        state.existing_workload_pool_for_session(
            &request.connection_id,
            pool_database,
            request.client_session_id.as_deref(),
        ),
        report_completion,
    )
    .await?
    .ok_or_else(|| "LOB context has expired; rerun the query".to_string())?;
    let _activity = state.pool_activity_touch(pool_key.as_str());
    match handle {
        PoolKind::Agent(client) => {
            let mut client =
                wait_for_large_value_resource(cancel_token.clone(), timeout, client.lock(), report_completion).await?;
            if report_completion {
                if is_canceled(&cancel_token) {
                    return Err(LargeValueOperationError::cancelled());
                }
                let value = client
                    .call_typed_with_timeout_and_cancel(method, params, timeout, None)
                    .await
                    .map_err(LargeValueOperationError::from_agent)?;
                if is_canceled(&cancel_token) {
                    return Err(LargeValueOperationError::cancelled());
                }
                Ok(value)
            } else {
                client.call_with_timeout_and_cancel(method, params, timeout, cancel_token).await.map_err(Into::into)
            }
        }
        PoolKind::ExternalDriver { session, config, .. } => {
            let mut params = params;
            params["connection"] = serde_json::json!(config.as_ref());
            if report_completion {
                if is_canceled(&cancel_token) {
                    return Err(LargeValueOperationError::cancelled());
                }
                let value = session
                    .invoke_with_timeout::<serde_json::Value>(method, params, timeout)
                    .await
                    .map_err(LargeValueOperationError::unknown)?;
                if is_canceled(&cancel_token) {
                    return Err(LargeValueOperationError::cancelled());
                }
                Ok(value)
            } else {
                wait_for_value_opt(
                    cancel_token,
                    timeout,
                    session.invoke_with_timeout::<serde_json::Value>(method, params, timeout),
                )
                .await?
                .map_err(Into::into)
            }
        }
        _ => Err("LOB snapshot chunks require a supported connection".into()),
    }
}

pub async fn write_large_value_snapshot(
    state: &AppState,
    mut request: LargeValueRequest,
    output: &mut std::fs::File,
) -> Result<u64, String> {
    use std::io::Write;
    let registered = request.execution_id.as_ref().map(|id| {
        state.running_queries.register_task(
            id.clone(),
            crate::query_cancel::RunningTaskMetadata::query(
                request.connection_id.clone(),
                request.database.clone(),
                request.client_session_id.clone(),
            ),
        )
    });
    let cancel = registered.as_ref().map(|task| task.token());
    request.offset = 0;
    request.limit = 4096;
    let mut written = 0;
    let mut value_kind: Option<String> = None;
    let mut decoder = match request.download_encoding.as_deref() {
        None | Some("binary") => None,
        Some("utf8") => Some(encoding_rs::UTF_8.new_decoder_without_bom_handling()),
        Some("gbk") => Some(encoding_rs::GBK.new_decoder_without_bom_handling()),
        Some(_) => return Err("Unsupported LOB download encoding".to_string()),
    };
    let mut first_decoded_output = request.download_encoding.as_deref() == Some("utf8");
    loop {
        if is_canceled(&cancel) {
            return Err(canceled_error());
        }
        let chunk = request_large_value_with_cancel(state, request.clone(), false, cancel.clone()).await?;
        if is_canceled(&cancel) {
            return Err(canceled_error());
        }
        let (data, next, eof, kind) = snapshot_export::checked_chunk(&chunk, request.offset)?;
        if value_kind.as_deref().is_some_and(|original| original != kind) {
            return Err("LOB chunk type changed".to_string());
        }
        value_kind = Some(kind.to_string());
        if next < request.offset || (!eof && next == request.offset) {
            return Err("Invalid LOB offset".to_string());
        }
        let bytes = if kind == "binary" {
            let bytes = decode_lob_hex_chunk(data)?;
            if bytes.len() as u64 != next - request.offset {
                return Err("Invalid LOB byte count".to_string());
            }
            if let Some(decoder) = decoder.as_mut() {
                let mut text = decode_lob_text_chunk(decoder, &bytes, eof)?;
                if first_decoded_output && !text.is_empty() {
                    if text.starts_with('\u{feff}') {
                        text.drain(..3);
                    }
                    first_decoded_output = false;
                }
                text.into_bytes()
            } else {
                bytes
            }
        } else if kind == "text" {
            if data.chars().count() as u64 != next - request.offset {
                return Err("Invalid LOB character count".to_string());
            }
            data.as_bytes().to_vec()
        } else {
            return Err("Invalid LOB type".to_string());
        };
        output.write_all(&bytes).map_err(|error| error.to_string())?;
        written += bytes.len() as u64;
        if eof {
            break;
        }
        request.offset = next;
    }
    if is_canceled(&cancel) {
        return Err(canceled_error());
    }
    output.flush().map_err(|error| error.to_string())?;
    Ok(written)
}

fn decode_lob_hex_chunk(hex: &str) -> Result<Vec<u8>, String> {
    if hex.len() > 8192 || !hex.len().is_multiple_of(2) || !hex.is_ascii() {
        return Err("Invalid LOB binary encoding".to_string());
    }
    hex.as_bytes()
        .chunks_exact(2)
        .map(|pair| {
            u8::from_str_radix(std::str::from_utf8(pair).map_err(|_| "Invalid LOB binary encoding".to_string())?, 16)
                .map_err(|_| "Invalid LOB binary encoding".to_string())
        })
        .collect()
}

fn decode_lob_text_chunk(decoder: &mut encoding_rs::Decoder, bytes: &[u8], eof: bool) -> Result<String, String> {
    let mut text = String::with_capacity(bytes.len() * 3 + 12);
    let (status, consumed, _) = decoder.decode_to_string(bytes, &mut text, eof);
    if status != encoding_rs::CoderResult::InputEmpty || consumed != bytes.len() {
        return Err("LOB download decoding did not consume the complete chunk".to_string());
    }
    Ok(text)
}

pub async fn close_query_session(
    state: &AppState,
    connection_id: &str,
    database: &str,
    session_id: &str,
    client_session_id: Option<&str>,
    catalog: Option<&str>,
) -> Result<bool, String> {
    let pool_database = query_pool_database(database, catalog);
    let pool_key = state.get_or_create_pool_for_session(connection_id, pool_database, client_session_id).await?;

    let pool_handle = state.pool_handle(&pool_key).await;
    let pool = pool_handle.as_ref().ok_or("Connection not found")?;
    match pool {
        #[cfg(feature = "duckdb-sidecar")]
        PoolKind::DuckDbWorker(client) => client.close_query_session(session_id).await,
        PoolKind::Agent(client) => {
            let client = client.clone();
            let mut client = client.lock().await;
            client.close_query_session(session_id).await
        }
        PoolKind::ExternalDriver { config, session, .. } => {
            let config = config.clone();
            let session = session.clone();
            let params = external_driver_fetch_query_page_params(config.as_ref(), session_id, 1);
            session
                .invoke::<serde_json::Value>("closeQuerySession", params)
                .await
                .map(|value| value.get("ok").and_then(|ok| ok.as_bool()).unwrap_or(false))
        }
        PoolKind::Elasticsearch(client) => {
            let client = client.clone();
            db::elasticsearch_driver::close_cursor(&client, session_id).await?;
            Ok(true)
        }
        PoolKind::Easysearch(client) => {
            let client = client.clone();
            db::easysearch_driver::close_cursor(&client, session_id).await?;
            Ok(true)
        }
        PoolKind::Solr(client) => {
            let client = client.clone();
            db::solr_driver::close_cursor(&client, session_id).await?;
            Ok(true)
        }
        PoolKind::CouchDb(client) => {
            let client = client.clone();
            db::couchdb_driver::close_cursor(&client, session_id).await?;
            Ok(true)
        }
        _ => Ok(false),
    }
}

/// Close only the result cursor held by this transaction; never open a normal
/// pool or commit/roll back the transaction as part of result cleanup.
pub async fn close_manual_transaction_query_session(
    state: &AppState,
    connection_id: &str,
    txn_session_id: &str,
    session_id: &str,
) -> Result<bool, String> {
    let connection = {
        let sessions = state.transaction_sessions.read().await;
        let Some(session) = sessions.get(txn_session_id) else {
            return Ok(false);
        };
        if session.connection_id != connection_id {
            return Err("Transaction session belongs to a different connection".to_string());
        }
        Arc::clone(&session.connection)
    };
    let connection = connection.lock().await;
    match &*connection {
        TxnConnection::Agent { client, .. } => client.lock().await.close_query_session(session_id).await,
        _ => Ok(false),
    }
}

/// Whether an open manual transaction can page a query through an Agent
/// result-session cursor. Only Agent transaction connections host cursors;
/// native (PostgreSQL/MySQL/SQL Server) and external-driver transactions page
/// with SQL only.
pub async fn manual_transaction_supports_agent_cursor(state: &AppState, txn_session_id: &str) -> bool {
    let sessions = state.transaction_sessions.read().await;
    let Some(session) = sessions.get(txn_session_id) else {
        return false;
    };
    let Ok(connection) = session.connection.try_lock() else {
        return false;
    };
    matches!(&*connection, TxnConnection::Agent { .. })
}

pub async fn execute_multi_core(
    state: &AppState,
    connection_id: &str,
    database: &str,
    sql: &str,
    schema: Option<&str>,
    cancel_token: Option<CancellationToken>,
) -> Result<Vec<db::QueryResult>, String> {
    execute_multi_core_with_options(
        state,
        connection_id,
        database,
        sql,
        schema,
        cancel_token,
        QueryExecutionOptions::default(),
    )
    .await
}

pub async fn execute_multi_core_with_options(
    state: &AppState,
    connection_id: &str,
    database: &str,
    sql: &str,
    schema: Option<&str>,
    cancel_token: Option<CancellationToken>,
    options: QueryExecutionOptions,
) -> Result<Vec<db::QueryResult>, String> {
    execute_multi_core_with_options_for_client(state, connection_id, database, sql, schema, cancel_token, options)
        .await
        .map(|results| results.into_iter().map(ExecuteMultiResult::into_query_result).collect())
}

/// Execute a SQL batch and retain client-facing metadata for synthesized errors.
pub async fn execute_multi_core_with_options_for_client(
    state: &AppState,
    connection_id: &str,
    database: &str,
    sql: &str,
    schema: Option<&str>,
    cancel_token: Option<CancellationToken>,
    options: QueryExecutionOptions,
) -> Result<Vec<ExecuteMultiResult>, String> {
    execute_multi_core_with_options_for_client_typed(state, connection_id, database, sql, schema, cancel_token, options)
        .await
        .map_err(QueryExecutionError::into_legacy_string)
}

/// Execute a SQL batch for a client while preserving typed failures.
pub async fn execute_multi_core_with_options_for_client_typed(
    state: &AppState,
    connection_id: &str,
    database: &str,
    sql: &str,
    schema: Option<&str>,
    cancel_token: Option<CancellationToken>,
    options: QueryExecutionOptions,
) -> Result<Vec<ExecuteMultiResult>, QueryExecutionError> {
    execute_multi_core_with_options_for_client_and_progress_typed(
        state,
        connection_id,
        database,
        sql,
        schema,
        cancel_token,
        options,
        None,
    )
    .await
}

/// Executes a SQL batch and reports each completed statement to the optional callback.
pub async fn execute_multi_core_with_options_for_client_and_progress(
    state: &AppState,
    connection_id: &str,
    database: &str,
    sql: &str,
    schema: Option<&str>,
    cancel_token: Option<CancellationToken>,
    options: QueryExecutionOptions,
    progress: Option<ExecuteMultiProgressCallback>,
) -> Result<Vec<ExecuteMultiResult>, String> {
    execute_multi_core_with_options_for_client_and_progress_typed(
        state,
        connection_id,
        database,
        sql,
        schema,
        cancel_token,
        options,
        progress,
    )
    .await
    .map_err(QueryExecutionError::into_legacy_string)
}

/// Executes a SQL batch without erasing structured query errors at transport boundaries.
pub async fn execute_multi_core_with_options_for_client_and_progress_typed(
    state: &AppState,
    connection_id: &str,
    database: &str,
    sql: &str,
    schema: Option<&str>,
    cancel_token: Option<CancellationToken>,
    options: QueryExecutionOptions,
    progress: Option<ExecuteMultiProgressCallback>,
) -> Result<Vec<ExecuteMultiResult>, QueryExecutionError> {
    let db_type = connection_database_type(state, connection_id).await;
    let invalidate = crate::object_cache::sql_may_change_object_metadata(sql, db_type);
    // A run that produced a single statement result is reported back as one
    // duration, so make it the duration of the whole request: pool creation or
    // reconnection for a cold pool happens outside the statement dispatcher and
    // would otherwise be invisible while the loading indicator counts it.
    let request_start = std::time::Instant::now();
    let result = execute_multi_core_with_options_for_client_and_progress_typed_inner(
        state,
        connection_id,
        database,
        sql,
        schema,
        cancel_token,
        options,
        progress,
    )
    .await;
    if invalidate {
        crate::object_cache::invalidate_connection_object_cache(&state.storage, connection_id).await;
    }
    result.map(|mut results| {
        if results.len() == 1 {
            if let Some(item) = results.first_mut() {
                item.result.execution_time_ms = item.result.execution_time_ms.max(request_start.elapsed().as_millis());
            }
        }
        results
    })
}

async fn execute_multi_core_with_options_for_client_and_progress_typed_inner(
    state: &AppState,
    connection_id: &str,
    database: &str,
    sql: &str,
    schema: Option<&str>,
    cancel_token: Option<CancellationToken>,
    options: QueryExecutionOptions,
    progress: Option<ExecuteMultiProgressCallback>,
) -> Result<Vec<ExecuteMultiResult>, QueryExecutionError> {
    let pool_database = query_pool_database(database, options.catalog.as_deref());
    // Reject MongoDB queries that fall through to the generic executor.
    if connection_is_mongodb(state, connection_id).await {
        return Err(MONGO_SHELL_COMMAND_HINT.into());
    }

    let db_type = connection_database_type(state, connection_id).await;
    validate_query_execution_mode(db_type, sql, &options)?;
    if options.execution_mode == QueryExecutionMode::PostgresReadOnlyTransaction {
        let result =
            execute_sql_statement_with_options(state, connection_id, database, sql, schema, cancel_token, options)
                .await?;
        return Ok(vec![result.into()]);
    }

    let pool_key = state
        .get_or_create_pool_for_session(connection_id, pool_database, options.client_session_id.as_deref())
        .await
        .map_err(|e| query_error_with_omitted_sql_context(&e, sql))?;
    if let Some(execution_id) = options.execution_id.as_deref() {
        state.running_queries.set_pool_key(execution_id, pool_key.clone());
    }
    state.touch_pool_activity(&pool_key).await;
    let _activity_touch = state.pool_activity_touch(pool_key.as_str());

    let (is_sqlserver, is_sqlserver_agent) = {
        match state.pool_handle(&pool_key).await {
            Some(PoolKind::SqlServer(_)) => (true, false),
            Some(PoolKind::Agent(_)) if db_type == Some(DatabaseType::SqlServer) => (false, true),
            _ => (false, false),
        }
    };

    let compatibility_mode = connection_sql_compatibility_mode(state, &pool_key, db_type).await;
    let complete_sqlserver_agent_batch =
        is_sqlserver_agent && sqlserver_agent_batch::requires_complete_results(sql, &options);
    let split_db_type = connection_statement_split_database_type(state, connection_id).await;
    let execution_plan =
        query_execution_plan_with_compatibility(sql, split_db_type, is_sqlserver_agent, compatibility_mode.as_deref());
    let continue_on_error = options.continue_on_error && !execution_plan.stop_on_error;
    let statements = execution_plan.statements;
    if statements.is_empty() {
        return Ok(vec![empty_query_result(0).into()]);
    }

    // Check the transaction request before database-specific fast paths. Otherwise
    // a backend such as SQL Server or HTTP SQLite can return successful
    // auto-commit results while the API has promised a rollbackable batch.
    // The DDL cap applies only to this opt-in use_transaction contract: a script
    // that the user asked to run atomically must not silently produce partial
    // effects. Other callers that share the transaction kernel (schema-diff
    // deploy, imports) document a mixed-outcome-on-failure behaviour instead, so
    // they are deliberately not capped here.
    if options.use_transaction == Some(true) && statements.len() > 1 {
        if batch_transaction_ddl_is_unrollbackable(db_type, &statements) {
            return Err(
                "use_transaction cannot be used with a batch whose DDL cannot be rolled back: DDL statements implicitly commit and cannot be undone. Run the batch without use_transaction (auto-commit, one result per statement) or split the DDL and DML into separate calls."
                    .to_string()
                    .into(),
            );
        }
        let result = execute_statements_in_transaction_typed(
            state,
            connection_id,
            database,
            &statements,
            schema,
            options.catalog.as_deref(),
            options.timeout_secs,
        )
        .await?;
        return Ok(vec![result.into()]);
    }

    if is_sqlserver {
        return execute_multi_sqlserver(state, &pool_key, sql, schema, cancel_token, options).await.map_err(Into::into);
    }

    let is_http_sqlite = {
        let configs = state.configs.read().await;
        configs
            .get(connection_id)
            .is_some_and(|c| matches!(c.db_type, DatabaseType::Turso | DatabaseType::CloudflareD1))
    };

    // HTTP SQLite providers send all statements in one request so the provider
    // can preserve batch ordering and atomicity in the default batch mode.
    if is_http_sqlite {
        let table_data_preview = options.table_data_preview;
        return single_statement_multi_result(
            execute_sql_statement_with_options_typed(
                state,
                connection_id,
                database,
                sql,
                schema,
                cancel_token,
                options,
            )
            .await,
            table_data_preview,
            None,
        );
    }

    let mysql_pool = {
        let pool_handle = state.pool_handle(&pool_key).await;
        match pool_handle.as_ref() {
            Some(PoolKind::Mysql(pool, mode)) => Some((pool.clone(), *mode)),
            _ => None,
        }
    };

    if statements.len() == 1
        && !complete_sqlserver_agent_batch
        && !mysql_single_statement_uses_batch_route(
            db_type,
            mysql_pool.is_some(),
            &statements[0],
            options.max_result_bytes,
        )
    {
        let single_sql = statements.into_iter().next().unwrap_or_default();
        let table_data_preview = options.table_data_preview;
        let executed = execute_sql_statement_with_options_typed_with_outcome(
            state,
            connection_id,
            database,
            &single_sql,
            schema,
            cancel_token,
            options,
        )
        .await;
        let (result, transaction) = match executed {
            Ok((result, transaction)) => (Ok(result), transaction),
            Err(error) => (Err(error), None),
        };
        return single_statement_multi_result_with_charset(
            result,
            table_data_preview,
            transaction,
            mysql_pool.as_ref().and_then(|(pool, _)| pool.data_charset()),
        );
    }

    if let Some((pool, mode)) = mysql_pool {
        // Read-only check for MySQL batch path
        check_read_only_for_connection_multi(state, &pool_key, &statements).await?;
        let mysql_dialect = connection_mysql_query_dialect(state, connection_id).await;
        let mysql_catalog_dialect = connection_mysql_catalog_dialect(state, connection_id).await;
        return execute_multi_mysql(
            state,
            &pool_key,
            db_type,
            &pool,
            mode,
            mysql_dialect,
            mysql_catalog_dialect,
            database,
            &statements,
            cancel_token,
            options,
            progress.as_ref(),
        )
        .await
        .map_err(Into::into);
    }

    // Some Agent drivers cannot execute another statement while a paged result
    // cursor remains open on the same physical connection. Multi-result execution
    // therefore reads a bounded first page without retaining those cursors.
    let statement_options = options_for_sequential_statements(&options, statements.len(), db_type);
    let mut results = Vec::with_capacity(statements.len());
    for (statement_index, stmt) in statements.iter().enumerate() {
        if is_canceled(&cancel_token) {
            let error = canceled_query_execution_error();
            results.push(ExecuteMultiResult::execution_error_with_backend(
                error_query_result(error.clone().into_legacy_string()),
                Some(statement_index),
                error.into_backend_error(),
            ));
            break;
        }
        let execution = if complete_sqlserver_agent_batch {
            Box::pin(sqlserver_agent_batch::execute(
                state,
                &pool_key,
                database,
                stmt,
                schema,
                cancel_token.clone(),
                statement_options.clone(),
            ))
            .await
        } else {
            execute_sql_statement_with_options_typed(
                state,
                connection_id,
                database,
                stmt,
                schema,
                cancel_token.clone(),
                statement_options.clone(),
            )
            .await
            .map(|result| vec![result])
        };
        match execution {
            Ok(batch_results) => {
                for result in batch_results {
                    report_execute_multi_progress(
                        progress.as_ref(),
                        statement_index,
                        statements.len(),
                        &result,
                        true,
                        None,
                    );
                    results.push(if complete_sqlserver_agent_batch {
                        // One GO batch can contain several statements/results;
                        // do not label every result as its first statement.
                        ExecuteMultiResult::success_with_optional_server_large_values(
                            result,
                            options.table_data_preview,
                        )
                    } else {
                        ExecuteMultiResult::success_with_index_and_optional_server_large_values(
                            result,
                            statement_index,
                            options.table_data_preview,
                        )
                    });
                }
            }
            Err(error) => {
                let action = query_execution_error_action(db_type, stmt, &error);
                let result = error_query_result(error.clone().into_legacy_string());
                let backend_error = error.into_backend_error();
                report_execute_multi_progress(
                    progress.as_ref(),
                    statement_index,
                    statements.len(),
                    &result,
                    false,
                    Some(backend_error.clone()),
                );
                results.push(ExecuteMultiResult::execution_error_with_backend(
                    result,
                    Some(statement_index),
                    backend_error,
                ));
                if !should_continue_batch_after_error(continue_on_error, action) {
                    break;
                }
            }
        }
    }

    Ok(results)
}

/// Split a batch script into the statements the core will execute, using the
/// same dialect-aware splitter for every caller. SQL Server agent pools split on
/// `GO` batch separators (`split_sql_batches`); everything else uses the
/// database-dialect statement splitter. Public so the MCP pre-check can align its
/// transaction-entry decision with the core's actual script split.
pub fn query_execution_plan(
    sql: &str,
    db_type: Option<DatabaseType>,
    preserve_sqlserver_batches: bool,
) -> crate::sql::SqlExecutionPlan {
    query_execution_plan_with_compatibility(sql, db_type, preserve_sqlserver_batches, None)
}

/// Same as [`query_execution_plan`], but lets openGauss callers pass the
/// database compatibility mode so A-mode PL/SQL (package) bodies are never split
/// on inner semicolons. `None` keeps the conservative PL/SQL-capable openGauss
/// profile while the probe is cold or unavailable.
pub fn query_execution_plan_with_compatibility(
    sql: &str,
    db_type: Option<DatabaseType>,
    preserve_sqlserver_batches: bool,
    compatibility_mode: Option<&str>,
) -> crate::sql::SqlExecutionPlan {
    if preserve_sqlserver_batches && db_type == Some(DatabaseType::SqlServer) {
        return crate::sql::SqlExecutionPlan { statements: split_sql_batches(sql), stop_on_error: false };
    }

    db_type.map_or_else(
        || crate::sql::SqlExecutionPlan { statements: split_sql_statements(sql), stop_on_error: false },
        |db_type| crate::sql::sql_execution_plan_for_database_with_compatibility(sql, db_type, compatibility_mode),
    )
}

/// Whether a connection's pool is the SQL Server agent driver, mirroring the
/// predicate the core uses to select the batch splitter in [`query_execution_plan`].
/// Reads the existing pool without creating one so callers (the MCP pre-check)
/// can align their splitter without opening a connection. `false` for non-SQL
/// Server connections and when no pool is open yet. A SQL Server connection
/// backed by the legacy agent driver connects as `PoolKind::Agent` and is
/// detected here; the native driver connects as `PoolKind::SqlServer` and
/// returns `false`.
pub async fn connection_pool_is_sqlserver_agent(state: &AppState, connection_id: &str, database: &str) -> bool {
    let db_type = connection_database_type(state, connection_id).await;
    if db_type != Some(DatabaseType::SqlServer) {
        return false;
    }

    // SQL Server base pool keys are either `connection_id` (no database) or
    // `connection_id:database`. Peek both without creating a pool.
    let candidates = match database.trim() {
        "" => vec![connection_id.to_string()],
        db => vec![format!("{connection_id}:{db}"), connection_id.to_string()],
    };
    for key in candidates {
        if let Some(pool) = state.pool_handle(&key).await {
            return match pool {
                PoolKind::Agent(_) => true,
                PoolKind::SqlServer(_) => false,
                _ => false,
            };
        }
    }
    false
}

fn single_statement_multi_result(
    result: Result<db::QueryResult, QueryExecutionError>,
    table_data_preview: bool,
    transaction: Option<MysqlAutoCommitTransaction>,
) -> Result<Vec<ExecuteMultiResult>, QueryExecutionError> {
    single_statement_multi_result_with_charset(result, table_data_preview, transaction, None)
}

fn single_statement_multi_result_with_charset(
    result: Result<db::QueryResult, QueryExecutionError>,
    table_data_preview: bool,
    transaction: Option<MysqlAutoCommitTransaction>,
    data_charset: Option<db::mysql::MySqlDataCharset>,
) -> Result<Vec<ExecuteMultiResult>, QueryExecutionError> {
    result.map(|result| {
        let mut result = ExecuteMultiResult::success_with_optional_server_large_values_and_charset(
            result,
            table_data_preview,
            data_charset,
        );
        if let Some(transaction) = transaction {
            transaction.mark(&mut result);
        }
        vec![result]
    })
}

fn mysql_single_statement_uses_batch_route(
    db_type: Option<DatabaseType>,
    has_mysql_pool: bool,
    sql: &str,
    max_result_bytes: Option<usize>,
) -> bool {
    has_mysql_pool
        && (max_result_bytes.is_some_and(|value| value > 0)
            || (db_type == Some(DatabaseType::Mysql)
                && starts_with_executable_sql_keyword_for_database(sql, &["CALL"], DatabaseType::Mysql)))
}

trait MysqlBatchStatementExecutor {
    fn data_charset(&self) -> Option<db::mysql::MySqlDataCharset> {
        None
    }

    fn table_data_preview(&self) -> bool {
        false
    }

    async fn execute_statement(&mut self, statement: &str) -> Result<Vec<db::mysql::MySqlQueryResult>, String>;

    async fn execute_non_result_batch(
        &mut self,
        statements: &[String],
        on_result: &mut (dyn FnMut(usize, &db::QueryResult) + Send),
    ) -> db::mysql::MySqlNonResultBatchOutcome {
        let mut results = Vec::with_capacity(statements.len());
        for (statement_index, statement) in statements.iter().enumerate() {
            match self.execute_statement(statement).await {
                Ok(statement_results) if statement_results.len() == 1 => {
                    let result = statement_results.into_iter().next().expect("single MySQL batch result").result;
                    on_result(statement_index, &result);
                    results.push(result);
                }
                Ok(_) => {
                    return db::mysql::MySqlNonResultBatchOutcome {
                        results,
                        error: Some("A non-result MySQL batch statement returned multiple results.".to_string()),
                    };
                }
                Err(error) => return db::mysql::MySqlNonResultBatchOutcome { results, error: Some(error) },
            }
        }
        db::mysql::MySqlNonResultBatchOutcome { results, error: None }
    }
}

struct MysqlBatchConnection<'a> {
    data_charset: Option<db::mysql::MySqlDataCharset>,
    conn: &'a mut mysql_async::Conn,
    cancel_token: Option<CancellationToken>,
    query_timeout: Option<Duration>,
    bare: bool,
    max_rows: Option<usize>,
    max_result_bytes: Option<usize>,
    result_key_columns: &'a [String],
    table_data_preview: bool,
    dialect: db::mysql::MySqlQueryDialect,
    diagnostic_trace_id: Option<&'a str>,
}

impl MysqlBatchStatementExecutor for MysqlBatchConnection<'_> {
    fn data_charset(&self) -> Option<db::mysql::MySqlDataCharset> {
        self.data_charset
    }

    fn table_data_preview(&self) -> bool {
        self.table_data_preview
    }

    async fn execute_statement(&mut self, statement: &str) -> Result<Vec<db::mysql::MySqlQueryResult>, String> {
        wait_for_result_opt(
            self.cancel_token.clone(),
            self.query_timeout,
            db::mysql::execute_query_results_on_conn_with_limits_with_data_charset(
                &mut *self.conn,
                statement,
                self.bare,
                self.max_rows,
                self.max_result_bytes,
                self.result_key_columns,
                self.dialect,
                self.diagnostic_trace_id,
                self.data_charset,
            ),
        )
        .await
    }

    async fn execute_non_result_batch(
        &mut self,
        statements: &[String],
        on_result: &mut (dyn FnMut(usize, &db::QueryResult) + Send),
    ) -> db::mysql::MySqlNonResultBatchOutcome {
        let sql = statements.join(";\n");
        let mut completed_results = Vec::with_capacity(statements.len());
        let mut record_result = |statement_index: usize, result: &db::QueryResult| {
            completed_results.push(result.clone());
            on_result(statement_index, result);
        };
        match wait_for_result_opt(
            self.cancel_token.clone(),
            self.query_timeout,
            db::mysql::execute_non_result_batch_on_conn(&mut *self.conn, &sql, statements.len(), &mut record_result),
        )
        .await
        {
            Ok(outcome) => outcome,
            Err(error) => db::mysql::MySqlNonResultBatchOutcome { results: completed_results, error: Some(error) },
        }
    }
}

const MYSQL_MULTI_STATEMENT_BATCH_MAX_STATEMENTS: usize = 50;
const MYSQL_MULTI_STATEMENT_BATCH_MAX_BYTES: usize = 4 * 1024 * 1024;

fn mysql_batch_pool_error_action(db_type: Option<DatabaseType>, error: &str) -> PoolErrorAction {
    if error == QUERY_CANCELED {
        // Dropping an in-flight COM_QUERY future can leave unread packets on the
        // connection. Do not return that connection to the pool.
        PoolErrorAction::Discard
    } else {
        pool_error_action(db_type, error)
    }
}

fn mysql_batch_backend_error(action: PoolErrorAction, error: &str) -> crate::backend_error::BackendError {
    if action == PoolErrorAction::Keep {
        crate::backend_error::BackendError::from_sql_detail(error)
    } else {
        crate::backend_error::BackendError::from_legacy_backend(error)
    }
}

fn mysql_non_result_batch_end(
    statements: &[String],
    start: usize,
    dialect: db::mysql::MySqlQueryDialect,
    max_bytes: usize,
) -> usize {
    let Some(first) = statements.get(start) else {
        return start;
    };
    if !db::mysql::is_batchable_non_result_query(first, dialect) {
        return start + 1;
    }

    let mut end = start;
    let mut byte_len = 0usize;
    while let Some(statement) = statements.get(end) {
        if end - start >= MYSQL_MULTI_STATEMENT_BATCH_MAX_STATEMENTS
            || !db::mysql::is_batchable_non_result_query(statement, dialect)
        {
            break;
        }
        let next_len = byte_len.saturating_add(statement.len()).saturating_add(2);
        if end > start && next_len > max_bytes {
            break;
        }
        byte_len = next_len;
        end += 1;
    }
    end.max(start + 1)
}

fn mysql_non_result_pipeline_enabled(
    statement_count: usize,
    continue_on_error: bool,
    mode: crate::connection::MysqlMode,
) -> bool {
    statement_count > 1 && !continue_on_error && mode == crate::connection::MysqlMode::Normal
}

async fn execute_mysql_batch_statements<E>(
    executor: &mut E,
    statements: &[String],
    db_type: Option<DatabaseType>,
    mysql_dialect: db::mysql::MySqlQueryDialect,
    cancel_token: Option<CancellationToken>,
    continue_on_error: bool,
    pipeline_non_result_max_bytes: Option<usize>,
    progress: Option<&ExecuteMultiProgressCallback>,
) -> (Vec<ExecuteMultiResult>, Option<PoolErrorAction>)
where
    E: MysqlBatchStatementExecutor,
{
    let mut results = Vec::with_capacity(statements.len());
    let mut statement_index = 0usize;
    let table_data_preview = executor.table_data_preview();
    while statement_index < statements.len() {
        if is_canceled(&cancel_token) {
            results.push(ExecuteMultiResult::execution_error(error_query_result(canceled_error())));
            return (results, None);
        }

        let batch_end = if let Some(max_bytes) = pipeline_non_result_max_bytes {
            mysql_non_result_batch_end(statements, statement_index, mysql_dialect, max_bytes)
        } else {
            statement_index + 1
        };
        if batch_end > statement_index + 1 {
            let mut report_result = |offset: usize, result: &db::QueryResult| {
                report_execute_multi_progress(progress, statement_index + offset, statements.len(), result, true, None);
            };
            let outcome =
                executor.execute_non_result_batch(&statements[statement_index..batch_end], &mut report_result).await;
            let completed = outcome.results.len();
            for (offset, result) in outcome.results.into_iter().enumerate() {
                results.push(ExecuteMultiResult::success_with_index(result, statement_index + offset));
            }
            if let Some(error) = outcome.error {
                let action = mysql_batch_pool_error_action(db_type, &error);
                if completed >= batch_end - statement_index {
                    return (results, Some(action));
                }
                let failed_index = statement_index + completed;
                let result = error_query_result(error.clone());
                let backend_error = mysql_batch_backend_error(action, &error);
                report_execute_multi_progress(
                    progress,
                    failed_index,
                    statements.len(),
                    &result,
                    false,
                    Some(backend_error.clone()),
                );
                results.push(ExecuteMultiResult::execution_error_with_backend(
                    result,
                    Some(failed_index),
                    backend_error,
                ));
                return (results, Some(action));
            }
            statement_index = batch_end;
            continue;
        }

        let statement = &statements[statement_index];

        match executor.execute_statement(statement).await {
            Ok(statement_results) => {
                if let Some(result) = statement_results.last() {
                    report_execute_multi_progress(
                        progress,
                        statement_index,
                        statements.len(),
                        &result.result,
                        true,
                        None,
                    );
                }
                results.extend(statement_results.into_iter().map(|result| {
                    ExecuteMultiResult::success_with_index_and_large_values_and_charset(
                        result.result,
                        statement_index,
                        result.large_value_cells,
                        table_data_preview,
                        executor.data_charset(),
                    )
                }));
            }
            Err(err) => {
                let action = mysql_batch_pool_error_action(db_type, &err);
                let result = error_query_result(err.clone());
                let backend_error = mysql_batch_backend_error(action, &err);
                report_execute_multi_progress(
                    progress,
                    statement_index,
                    statements.len(),
                    &result,
                    false,
                    Some(backend_error.clone()),
                );
                results.push(ExecuteMultiResult::execution_error_with_backend(
                    result,
                    Some(statement_index),
                    backend_error,
                ));
                // Statement errors are safe to collect, but connection-level failures leave
                // the protocol state unusable and must still trigger pool cleanup.
                if !should_continue_batch_after_error(continue_on_error, action) {
                    return (results, Some(action));
                }
            }
        }
        statement_index += 1;
    }

    (results, None)
}

/// Settled transaction state of a tab-scoped MySQL connection after one
/// execution finished.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum MysqlAutoCommitTransaction {
    /// No transaction is open on the connection.
    None,
    /// A transaction the user opened explicitly is still open and was kept.
    Preserved,
    /// DBX rolled back a transaction the user opened explicitly.
    RolledBackExplicit,
    /// DBX rolled back a transaction the session opened implicitly because
    /// auto-commit was turned off (`SET autocommit = 0`).
    RolledBackSessionAutocommit,
}

impl MysqlAutoCommitTransaction {
    fn mark(self, result: &mut ExecuteMultiResult) {
        result.auto_commit_open_transaction = Some(self == Self::Preserved);
        result.auto_commit_explicit_transaction_rolled_back = self == Self::RolledBackExplicit;
        result.auto_commit_session_autocommit_rolled_back = self == Self::RolledBackSessionAutocommit;
    }
}

/// What the settlement did with the transaction that was still open.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum MysqlAutoCommitRollback {
    /// Nothing was rolled back.
    None,
    /// A transaction the batch opened explicitly (`BEGIN` / `START TRANSACTION`).
    Explicit,
    /// A transaction the session opened implicitly: auto-commit was off
    /// (`SET autocommit = 0`), so every statement starts one.
    SessionAutocommit,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct MysqlAutoCommitDecision {
    preserve: bool,
    rollback: MysqlAutoCommitRollback,
}

/// Decides what to do with a transaction that is still open when an
/// auto-commit tab finished executing.
///
/// A `BEGIN` / `START TRANSACTION` the user typed (or a batch that turned
/// auto-commit off) is an explicit request to hold the changes until `COMMIT` /
/// `ROLLBACK`, so `preserve_explicit_transaction` keeps it — including the
/// later executions that never mention a transaction, which is what
/// `already_preserved` carries. Everything else keeps the historical cleanup
/// that stops a leftover transaction from pinning the tab's read view (#9479),
/// and is reported so the cleanup is never silent.
fn decide_mysql_auto_commit_transaction(
    allow_preserve: bool,
    already_preserved: bool,
    explicit_start_in_batch: bool,
    status: Option<db::mysql::MySqlSessionStatus>,
) -> MysqlAutoCommitDecision {
    // A session or batch that turned auto-commit off opens its transactions
    // implicitly, so the server status is the only signal for those.
    let explicit_transaction = explicit_start_in_batch || status.is_some_and(|status| !status.autocommit);
    let transaction_open = match status {
        Some(status) => status.in_transaction,
        // No usable status packet (the last statement ended with an ERR packet
        // and the refresh failed): an explicit opener is the only evidence left.
        None => explicit_start_in_batch || already_preserved,
    };
    if !transaction_open {
        return MysqlAutoCommitDecision { preserve: false, rollback: MysqlAutoCommitRollback::None };
    }
    if already_preserved || (allow_preserve && explicit_transaction) {
        return MysqlAutoCommitDecision { preserve: true, rollback: MysqlAutoCommitRollback::None };
    }
    // Distinguish who opened it: only a batch with its own `BEGIN` /
    // `START TRANSACTION` is a user transaction; `autocommit = 0` opens
    // transactions implicitly for every statement.
    let rollback = if explicit_start_in_batch {
        MysqlAutoCommitRollback::Explicit
    } else if status.is_some_and(|status| !status.autocommit) {
        MysqlAutoCommitRollback::SessionAutocommit
    } else {
        MysqlAutoCommitRollback::None
    };
    MysqlAutoCommitDecision { preserve: false, rollback }
}

/// Whether the cleanup `ROLLBACK` after an execution would be a server no-op.
///
/// Every final status packet carries `SERVER_STATUS_IN_TRANS`, and MariaDB
/// Connector/J skips `ROLLBACK`/`COMMIT` on the same flag. DBX only relies on it
/// when the packet provably closes the execution (`status_is_final`: a native
/// MySQL-mode connection whose response was read to the end without an error,
/// so neither an earlier command's packet nor a pending result set is left
/// behind) and the batch did not open a transaction itself. A batch with
/// `BEGIN` keeps the historical cleanup, so a server or proxy that never
/// reports the flag cannot leave the user's transaction open.
fn mysql_cleanup_rollback_is_noop(
    status_is_final: bool,
    explicit_start_in_batch: bool,
    status: Option<db::mysql::MySqlSessionStatus>,
) -> bool {
    status_is_final
        && !explicit_start_in_batch
        && status.is_some_and(|status| !status.in_transaction && status.autocommit)
}

/// Type-erased entry point for [`settle_mysql_auto_commit_transaction`].
///
/// `do_execute_typed` is one of the largest async fns in the crate and is
/// reached transitively by the workspace's live tests and examples. Awaiting the
/// settle future inline nests its concrete future type inside that generator,
/// which pushed `dbx-core` targets past rustc's query depth limit on Linux
/// (`error: queries overflow the depth limit!`). Erasing the type keeps the
/// enclosing generators as shallow as they were before the settlement moved in.
fn settle_mysql_auto_commit_transaction_boxed<'a>(
    state: &'a AppState,
    pool_key: &'a str,
    conn: &'a mut mysql_async::Conn,
    allow_preserve: bool,
    explicit_start_in_batch: bool,
    status_is_final: bool,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<MysqlAutoCommitTransaction, String>> + Send + 'a>> {
    Box::pin(settle_mysql_auto_commit_transaction(
        state,
        pool_key,
        conn,
        allow_preserve,
        explicit_start_in_batch,
        status_is_final,
    ))
}

/// Settles the open transaction of a tab-scoped MySQL connection after an
/// execution: keeps an explicit user transaction when the caller opted in,
/// otherwise rolls it back exactly as before.
///
/// `Err` means the transaction state could not be settled, so the caller must
/// discard the connection instead of returning it to the pool.
async fn settle_mysql_auto_commit_transaction(
    state: &AppState,
    pool_key: &str,
    conn: &mut mysql_async::Conn,
    allow_preserve: bool,
    explicit_start_in_batch: bool,
    status_is_final: bool,
) -> Result<MysqlAutoCommitTransaction, String> {
    let already_preserved = state.has_preserved_explicit_transaction(pool_key).await;
    let mut status = db::mysql::session_status_from_last_ok(conn);
    if status.is_none() && (allow_preserve || already_preserved) {
        // Only pay for the extra round trip when the answer can change the
        // outcome: without the opt-in the historical cleanup runs regardless.
        status = db::mysql::ping_session_status_on_conn(conn).await.ok();
    }
    let decision =
        decide_mysql_auto_commit_transaction(allow_preserve, already_preserved, explicit_start_in_batch, status);
    if decision.preserve {
        state.mark_preserved_explicit_transaction(pool_key).await;
        return Ok(MysqlAutoCommitTransaction::Preserved);
    }
    state.clear_preserved_explicit_transaction(pool_key).await;
    // Historical cleanup: `ROLLBACK` releases the read view of a transaction
    // that is still open. Skip the round trip only when the final status
    // packet proves nothing is open.
    if !mysql_cleanup_rollback_is_noop(status_is_final, explicit_start_in_batch, status) {
        db::mysql::rollback_open_transaction(conn).await?;
    }
    Ok(match decision.rollback {
        MysqlAutoCommitRollback::Explicit => MysqlAutoCommitTransaction::RolledBackExplicit,
        MysqlAutoCommitRollback::SessionAutocommit => MysqlAutoCommitTransaction::RolledBackSessionAutocommit,
        MysqlAutoCommitRollback::None => MysqlAutoCommitTransaction::None,
    })
}

#[allow(clippy::too_many_arguments)]
async fn execute_multi_mysql(
    state: &AppState,
    pool_key: &str,
    db_type: Option<DatabaseType>,
    pool: &db::mysql::MySqlPool,
    mode: crate::connection::MysqlMode,
    dialect: db::mysql::MySqlQueryDialect,
    catalog_dialect: Option<db::mysql::MySqlCatalogDialect>,
    database: &str,
    statements: &[String],
    cancel_token: Option<CancellationToken>,
    options: QueryExecutionOptions,
    progress: Option<&ExecuteMultiProgressCallback>,
) -> Result<Vec<ExecuteMultiResult>, String> {
    let trace_id = options.execution_id.as_deref().unwrap_or("none");
    let total_started_at = std::time::Instant::now();
    let query_timeout = resolve_query_timeout(options.timeout_secs);
    let operation_budget = operation_budget_for_pool_key(state, pool_key, query_timeout).await;
    let bare = mode == crate::connection::MysqlMode::Bare;
    let max_rows = options.max_rows;
    let max_result_bytes = options.max_result_bytes.filter(|value| *value > 0);
    let pipeline_non_result_statements =
        mysql_non_result_pipeline_enabled(statements.len(), options.continue_on_error, mode);
    let checkout_started_at = std::time::Instant::now();
    let mut conn = match db::mysql::get_conn_with_health_check_with_cancel(
        pool,
        operation_budget.checkout_timeout,
        operation_budget.cleanup_timeout,
        cancel_token.as_ref(),
    )
    .await
    {
        Ok(conn) => conn,
        Err(err) => {
            if matches!(pool_error_action(db_type, &err), PoolErrorAction::Discard | PoolErrorAction::ReconnectAndRetry)
                || err == QUERY_CANCELED
            {
                state.remove_pool_by_key(pool_key).await;
            }
            return Ok(vec![ExecuteMultiResult::execution_error(error_query_result(err))]);
        }
    };
    let checkout_ms = checkout_started_at.elapsed().as_millis();
    apply_oceanbase_mysql_session_timeout(state, pool_key, &mut conn, options.timeout_secs).await?;
    let catalog_started_at = std::time::Instant::now();
    wait_for_result_opt(
        cancel_token.clone(),
        query_timeout,
        db::mysql::apply_catalog_database_context(&mut conn, catalog_dialect, options.catalog.as_deref(), database),
    )
    .await?;
    let catalog_ms = catalog_started_at.elapsed().as_millis();
    let pipeline_non_result_max_bytes = if pipeline_non_result_statements {
        db::mysql::max_allowed_packet_on_conn(&mut conn)
            .await
            .ok()
            .and_then(db::mysql::mysql_sql_statement_hard_limit)
            .map(|limit| limit.min(MYSQL_MULTI_STATEMENT_BATCH_MAX_BYTES))
    } else {
        None
    };

    let mut executor = MysqlBatchConnection {
        data_charset: pool.data_charset(),
        conn: &mut conn,
        cancel_token: cancel_token.clone(),
        query_timeout,
        bare,
        max_rows,
        max_result_bytes,
        result_key_columns: &options.result_key_columns,
        table_data_preview: options.table_data_preview,
        dialect,
        diagnostic_trace_id: options.execution_id.as_deref(),
    };
    let statements_started_at = std::time::Instant::now();
    let (mut results, error_action) = execute_mysql_batch_statements(
        &mut executor,
        statements,
        db_type,
        dialect,
        cancel_token,
        options.continue_on_error,
        pipeline_non_result_max_bytes,
        progress,
    )
    .await;
    let statements_ms = statements_started_at.elapsed().as_millis();
    drop(executor);

    // Tab-scoped single-connection pools disable COM_RESET_CONNECTION on return
    // (to preserve session state like temporary tables), so an open transaction
    // left on the connection — a user-typed BEGIN/START TRANSACTION without
    // COMMIT, or a canceled/aborted batch that skipped its cleanup — would pin
    // the REPEATABLE READ snapshot for every later auto-commit query on that
    // tab, making the tab read stale rows until disconnect. Closing any open
    // transaction before returning the connection restores the auto-commit
    // contract, unless the tab keeps explicit user transactions open
    // (`preserve_explicit_transaction`). The ROLLBACK round trip is skipped when
    // the final status packet proves nothing is open, and a failure here only
    // discards this connection.
    {
        let tab_scoped = pool.is_client_session_pool();
        let explicit_start_in_batch = statements
            .iter()
            .any(|statement| crate::query_execution_sql::mysql_statement_opens_explicit_transaction(statement));
        // An error, a truncated result, or result sets still pending may leave
        // the last status packet short of the end, so only a batch whose
        // response was read completely can prove that the cleanup `ROLLBACK`
        // is unnecessary.
        let status_is_final = mode == crate::connection::MysqlMode::Normal
            && error_action.is_none()
            && results.iter().all(|result| !result.execution_error && !result.result.truncated)
            && db::mysql::last_ok_ends_response(&conn);
        let rollback_started_at = std::time::Instant::now();
        let transaction = if tab_scoped {
            settle_mysql_auto_commit_transaction(
                state,
                pool_key,
                &mut conn,
                options.preserve_explicit_transaction,
                explicit_start_in_batch,
                status_is_final,
            )
            .await
        } else if mysql_cleanup_rollback_is_noop(
            status_is_final,
            explicit_start_in_batch,
            db::mysql::session_status_from_last_ok(&conn),
        ) {
            Ok(MysqlAutoCommitTransaction::None)
        } else {
            db::mysql::rollback_open_transaction(&mut conn).await.map(|()| MysqlAutoCommitTransaction::None)
        };
        match transaction {
            Ok(transaction) => {
                if rollback_started_at.elapsed() > std::time::Duration::from_millis(5) {
                    log::info!(
                        "[query][mysql-batch] trace_id={} open_txn_rollback_ms={} preserved={}",
                        trace_id,
                        rollback_started_at.elapsed().as_millis(),
                        transaction == MysqlAutoCommitTransaction::Preserved
                    );
                }
                for result in &mut results {
                    transaction.mark(result);
                }
            }
            Err(error) => {
                // A failed ROLLBACK leaves the transaction state unknown: drop
                // the connection instead of returning it to the session pool.
                log::warn!("[query][mysql-batch] trace_id={} open_txn_rollback_failed error={}", trace_id, error);
                let _ = tokio::time::timeout(Duration::from_secs(5), conn.disconnect()).await;
                state.remove_pool_by_key(pool_key).await;
                return Ok(results);
            }
        }
    }

    log::info!(
        "[query][mysql-batch] trace_id={} checkout_ms={} catalog_ms={} statements_ms={} total_ms={} result_count={} row_counts={:?}",
        trace_id,
        checkout_ms,
        catalog_ms,
        statements_ms,
        total_started_at.elapsed().as_millis(),
        results.len(),
        results.iter().map(|result| result.result.rows.len()).collect::<Vec<_>>()
    );

    if matches!(error_action, Some(PoolErrorAction::Discard | PoolErrorAction::ReconnectAndRetry)) {
        state.remove_pool_by_key(pool_key).await;
    }

    Ok(results)
}

fn error_query_result(message: String) -> db::QueryResult {
    db::QueryResult {
        columns: vec!["Error".to_string()],
        column_types: Vec::new(),
        deferred_large_value_cells: Vec::new(),
        column_sortables: vec![],
        spatial_columns: vec![],
        raw_key_cells: Vec::new(),
        spatial_values: vec![],
        rows: vec![vec![serde_json::Value::String(message)]],
        affected_rows: 0,
        execution_time_ms: 0,
        server_execute_time_us: None,
        query_timings_ms: None,
        truncated: false,
        session_id: None,
        has_more: false,
        elasticsearch_raw_body: None,
        messages: Vec::new(),
    }
}

fn empty_query_result(execution_time_ms: u128) -> db::QueryResult {
    db::QueryResult {
        columns: vec![],
        column_types: Vec::new(),
        deferred_large_value_cells: Vec::new(),
        column_sortables: vec![],
        spatial_columns: vec![],
        raw_key_cells: Vec::new(),
        spatial_values: vec![],
        rows: vec![],
        affected_rows: 0,
        execution_time_ms,
        server_execute_time_us: None,
        query_timings_ms: None,
        truncated: false,
        session_id: None,
        has_more: false,
        elasticsearch_raw_body: None,
        messages: Vec::new(),
    }
}

fn sqlserver_batch_results(results: Vec<db::sqlserver::SqlServerBatchResult>) -> Vec<ExecuteMultiResult> {
    results.into_iter().map(ExecuteMultiResult::from).collect()
}

async fn execute_multi_sqlserver(
    state: &AppState,
    pool_key: &str,
    sql: &str,
    schema: Option<&str>,
    cancel_token: Option<CancellationToken>,
    options: QueryExecutionOptions,
) -> Result<Vec<ExecuteMultiResult>, String> {
    let batches = split_sql_batches(sql);

    // Read-only check for SQL Server batch path
    check_read_only_for_connection_multi(state, pool_key, &batches).await?;
    let mut all_results = Vec::new();
    let max_rows = options.max_rows;
    let query_timeout = resolve_query_timeout(options.timeout_secs);
    let execution_mode = options.execution_mode;

    for batch in &batches {
        if is_canceled(&cancel_token) {
            let error = canceled_error();
            all_results.push(ExecuteMultiResult::execution_error_with_backend(
                error_query_result(error.clone()),
                None,
                crate::backend_error::BackendError::from_legacy_backend(&error),
            ));
            break;
        }

        let pool_handle = state.pool_handle(pool_key).await;
        let pool = pool_handle.as_ref().ok_or("Connection not found")?;
        let client = match pool {
            PoolKind::SqlServer(c) => c.clone(),
            _ => return Err("Expected SQL Server connection".to_string()),
        };

        let (mut client_guard, lock_wait_ms) =
            match lock_shared_client_with_wait(&client, cancel_token.clone(), query_timeout).await {
                Ok(value) => value,
                Err(err) => {
                    all_results.push(ExecuteMultiResult::execution_error_with_backend(
                        error_query_result(err.clone()),
                        None,
                        crate::backend_error::BackendError::from_legacy_backend(&err),
                    ));
                    break;
                }
            };

        if !sqlserver_pool_is_current(state, pool_key, &client).await {
            let error = "SQL Server connection was reset while waiting for the query lock; please retry.".to_string();
            all_results.push(ExecuteMultiResult::execution_error_with_backend(
                error_query_result(error.clone()),
                None,
                crate::backend_error::BackendError::from_legacy_backend(&error),
            ));
            break;
        }

        let execution_sql =
            sql_for_execution_context_with_identifier_quote(Some(DatabaseType::SqlServer), batch, schema, Some("["));
        // Only a single-statement batch can abandon its stream early: a real
        // multi-statement script must keep draining so the later statements
        // still produce their own results. Session-pinned tabs keep draining
        // because the pooled connection carries their temporary objects.
        let stop_at_row_limit = batches.len() == 1
            && max_rows.is_some()
            && options.client_session_id.is_none()
            && !options.use_transaction.unwrap_or(false);
        let execution = async {
            if execution_mode == QueryExecutionMode::Simple {
                db::sqlserver::execute_simple_batch_with_max_rows_metadata(&mut client_guard, &execution_sql, max_rows)
                    .await
            } else if stop_at_row_limit {
                db::sqlserver::execute_batch_with_max_rows_metadata_stopping(
                    &mut client_guard,
                    &execution_sql,
                    max_rows,
                )
                .await
            } else {
                db::sqlserver::execute_batch_with_max_rows_metadata(&mut client_guard, &execution_sql, max_rows).await
            }
        };
        let result = wait_for_result_opt(cancel_token.clone(), query_timeout, execution).await;
        let abandoned = client_guard.take_abandoned_after_row_limit();
        drop(client_guard);
        if abandoned {
            state.remove_pool_by_key(pool_key).await;
        }

        match result {
            Ok(results) => all_results.extend(sqlserver_batch_results(results).into_iter().map(|mut item| {
                item.result.execution_time_ms += lock_wait_ms;
                item
            })),
            Err(e) => {
                let action = pool_error_action(Some(DatabaseType::SqlServer), &e);
                all_results.push(ExecuteMultiResult::execution_error_with_backend(
                    error_query_result(e.clone()),
                    None,
                    crate::backend_error::BackendError::from_legacy_backend(&e),
                ));
                if matches!(action, PoolErrorAction::Discard | PoolErrorAction::ReconnectAndRetry) {
                    state.remove_pool_by_key(pool_key).await;
                }
                if !should_continue_batch_after_error(options.continue_on_error, action) {
                    break;
                }
            }
        }
    }

    if all_results.is_empty() {
        all_results.push(empty_query_result(0).into());
    }

    Ok(all_results)
}

async fn execute_multi_agent(
    client: &mut db::agent_driver::AgentDriverClient,
    database: Option<&str>,
    statements: &[String],
    schema: Option<&str>,
    timeout_secs: Option<u64>,
) -> Result<db::QueryResult, AgentCallError> {
    client.execute_batch_typed(database, statements, schema, resolve_query_timeout(timeout_secs)).await
}

pub async fn execute_statements(
    state: &AppState,
    connection_id: &str,
    database: &str,
    statements: &[String],
    schema: Option<&str>,
    timeout_secs: Option<u64>,
) -> Result<db::QueryResult, String> {
    let db_type = connection_database_type(state, connection_id).await;
    let invalidate = statements.iter().any(|sql| crate::object_cache::sql_may_change_object_metadata(sql, db_type));
    let result = execute_statements_inner(state, connection_id, database, statements, schema, timeout_secs).await;
    if invalidate {
        crate::object_cache::invalidate_connection_object_cache(&state.storage, connection_id).await;
    }
    result
}

/// Execute a batch, optionally on a single transaction.
///
/// `use_transaction` is opt-in and only the structure editor sets it, for
/// batches that change a partition hierarchy: a mid-batch failure there would
/// otherwise leave a half-created hierarchy behind. The shared transaction
/// kernel rejects backends whose DDL cannot roll back, and statements that
/// cannot run inside a transaction block (`... CONCURRENTLY`) are refused up
/// front rather than half-applied.
pub async fn execute_statements_with_transaction_option(
    state: &AppState,
    connection_id: &str,
    database: &str,
    statements: &[String],
    schema: Option<&str>,
    use_transaction: bool,
    timeout_secs: Option<u64>,
) -> Result<db::QueryResult, String> {
    if use_transaction && statements.len() > 1 {
        if batch_has_concurrently_statement(statements) {
            return Err(
                "use_transaction cannot wrap CONCURRENTLY statements: they cannot run inside a transaction block. Run the batch without use_transaction."
                    .to_string(),
            );
        }
        let db_type = connection_database_type(state, connection_id).await;
        if batch_transaction_ddl_is_unrollbackable(db_type, statements) {
            return Err(
                "use_transaction cannot be used with a batch whose DDL cannot be rolled back: DDL statements implicitly commit and cannot be undone. Run the batch without use_transaction (auto-commit, one result per statement) or split the DDL and DML into separate calls."
                    .to_string(),
            );
        }
        let result = execute_statements_in_transaction_typed(
            state,
            connection_id,
            database,
            statements,
            schema,
            None,
            timeout_secs,
        )
        .await
        .map_err(|error| error.into_legacy_string())?;
        let invalidate = statements.iter().any(|sql| crate::object_cache::sql_may_change_object_metadata(sql, db_type));
        if invalidate {
            crate::object_cache::invalidate_connection_object_cache(&state.storage, connection_id).await;
        }
        return Ok(result);
    }
    execute_statements(state, connection_id, database, statements, schema, timeout_secs).await
}

/// Whether a batch contains a statement PostgreSQL-family servers refuse to run
/// inside a transaction block (`CREATE/DROP INDEX CONCURRENTLY`,
/// `ALTER TABLE ... DETACH PARTITION CONCURRENTLY`, ...).
fn batch_has_concurrently_statement(statements: &[String]) -> bool {
    statements.iter().any(|statement| {
        let upper = statement.to_ascii_uppercase();
        // Real CONCURRENTLY statements always start with one of these verbs.
        // Requiring the verb plus a standalone keyword keeps the word inside
        // string literals, comments or plain identifiers (e.g. a table named
        // `concurrently`) from silently demoting the batch to auto-commit; the
        // direction stays fail-safe because no CONCURRENTLY statement form
        // starts with another verb.
        let trimmed = upper.trim_start();
        let starts_with_ddl_verb =
            ["CREATE ", "DROP ", "REINDEX", "REFRESH ", "ALTER "].iter().any(|verb| trimmed.starts_with(verb));
        starts_with_ddl_verb && contains_standalone_concurrently_keyword(&upper)
    })
}

fn contains_standalone_concurrently_keyword(upper: &str) -> bool {
    let keyword = "CONCURRENTLY";
    let mut search_from = 0;
    while let Some(pos) = upper[search_from..].find(keyword) {
        let pos = search_from + pos;
        let boundary =
            |ch: Option<char>| ch.map(|ch| ch.is_ascii_alphanumeric() || ch == '_' || ch == '"').unwrap_or(false);
        let before = boundary(upper[..pos].chars().next_back());
        let after = boundary(upper[pos + keyword.len()..].chars().next());
        if !before && !after {
            return true;
        }
        search_from = pos + 1;
    }
    false
}

pub async fn execute_blob_bound_statements(
    state: &AppState,
    connection_id: &str,
    database: &str,
    previews: &[String],
    bound: &[dbx_types::types::BlobBoundStatement],
    schema: Option<&str>,
    use_transaction: bool,
    timeout_secs: Option<u64>,
) -> Result<db::QueryResult, String> {
    dbx_types::types::validate_blob_bound_statements(previews, bound)?;
    if connection_database_type(state, connection_id).await != Some(DatabaseType::OceanbaseOracle) {
        return Err("Bound BLOB saves are only supported for OceanBase Oracle.".into());
    }
    let pool_key = if database.is_empty() {
        connection_id.to_string()
    } else {
        state.get_or_create_pool(connection_id, Some(database)).await?
    };
    let sql = bound.iter().map(|statement| statement.sql.clone()).collect::<Vec<_>>();
    check_read_only_for_connection_multi(state, &pool_key, &sql).await?;
    let pool = state.pool_handle(&pool_key).await;
    let Some(PoolKind::Agent(source_client)) = pool.as_ref() else {
        return Err("Bound BLOB saves require an updated JDBC Agent.".into());
    };
    let mut client = source_client.lock().await;
    let result = client
        .execute_blob_bound_typed(
            if database.is_empty() { None } else { Some(database) },
            previews,
            bound,
            schema,
            use_transaction,
            resolve_query_timeout(timeout_secs),
        )
        .await;
    drop(client);
    match result {
        Ok(result) => Ok(result),
        Err(error) => {
            discard_agent_pool_after_typed_error(state, &pool_key, source_client, &error, RecoveryScope::UserOperation)
                .await;
            Err(error.into_legacy_string())
        }
    }
}

async fn execute_statements_inner(
    state: &AppState,
    connection_id: &str,
    database: &str,
    statements: &[String],
    schema: Option<&str>,
    timeout_secs: Option<u64>,
) -> Result<db::QueryResult, String> {
    let sql_ctx = statements.first().map(|s| s.as_str()).unwrap_or("");
    let pool_key = if database.is_empty() {
        connection_id.to_string()
    } else {
        state
            .get_or_create_pool(connection_id, Some(database))
            .await
            .map_err(|e| query_error_with_omitted_sql_context(&e, sql_ctx))?
    };

    let mut total_affected: u64 = 0;
    let start = std::time::Instant::now();
    let mysql_dialect = connection_mysql_query_dialect(state, connection_id).await;

    let agent_client = {
        let pool_handle = state.pool_handle(&pool_key).await;
        match pool_handle.as_ref() {
            Some(PoolKind::Agent(client)) => Some(client.clone()),
            _ => None,
        }
    };
    if let Some(client) = agent_client {
        let source_client = client.clone();
        check_read_only_for_connection_multi(state, &pool_key, statements).await?;
        let db_type = connection_database_type_for_pool_key(state, &pool_key).await;
        let execution_schema = schema_for_execution_context(db_type, schema);
        let rewritten_statements;
        let statements = if qualifies_unqualified_agent_relations(db_type) {
            rewritten_statements = statements
                .iter()
                .map(|sql| {
                    sql_for_execution_context_with_identifier_quote(db_type, sql, schema, client.identifier_quote())
                })
                .collect::<Vec<_>>();
            rewritten_statements.as_slice()
        } else {
            statements
        };
        let mut client = client.lock().await;
        let database = if database.trim().is_empty() { None } else { Some(database) };
        let result = execute_multi_agent(&mut client, database, statements, execution_schema, timeout_secs).await;
        drop(client);
        match result {
            Ok(result) => return Ok(db::QueryResult { execution_time_ms: start.elapsed().as_millis(), ..result }),
            Err(err) => {
                if is_agent_execute_batch_unsupported(&err.to_string()) {
                    log::warn!(
                        "Agent does not support execute_batch; falling back to statement-by-statement execution"
                    );
                } else {
                    discard_agent_pool_after_typed_error(
                        state,
                        &pool_key,
                        &source_client,
                        &err,
                        RecoveryScope::UserOperation,
                    )
                    .await;
                    return Err(query_error_with_omitted_sql_context(&err.into_legacy_string(), sql_ctx));
                }
            }
        }
    }

    for (i, sql) in statements.iter().enumerate() {
        match do_execute(
            state,
            &pool_key,
            mysql_dialect,
            Some(database),
            sql,
            schema,
            None,
            QueryExecutionOptions { timeout_secs, ..Default::default() },
        )
        .await
        {
            Ok(result) => {
                total_affected += result.affected_rows;
            }
            Err(e) => {
                let db_type = connection_database_type(state, connection_id).await;
                match pool_error_action(db_type, &e) {
                    PoolErrorAction::ReconnectAndRetry => {
                        let db_opt = if database.is_empty() { None } else { Some(database) };
                        let _ = state.reconnect_pool(connection_id, db_opt).await;
                    }
                    PoolErrorAction::Discard
                        if !db_type.is_some_and(|db_type| database_capabilities::is_agent_type(&db_type)) =>
                    {
                        let _ = state.remove_pool_by_key(&pool_key).await;
                    }
                    PoolErrorAction::Discard | PoolErrorAction::Keep => {}
                }
                let error = crate::db::agent_driver::append_legacy_error_context(
                    &e,
                    &format!("Statement {} failed; previous {} statement(s) may have been committed.", i + 1, i),
                );
                return Err(query_error_with_omitted_sql_context(&error, sql));
            }
        }
    }

    Ok(db::QueryResult {
        columns: vec![],
        column_types: Vec::new(),
        deferred_large_value_cells: Vec::new(),
        column_sortables: vec![],
        spatial_columns: vec![],
        raw_key_cells: Vec::new(),
        spatial_values: vec![],
        rows: vec![],
        affected_rows: total_affected,
        execution_time_ms: start.elapsed().as_millis(),
        server_execute_time_us: None,
        query_timings_ms: None,
        truncated: false,
        session_id: None,
        has_more: false,
        elasticsearch_raw_body: None,
        messages: Vec::new(),
    })
}

fn is_agent_execute_batch_unsupported(error: &str) -> bool {
    let lower = error.to_lowercase();
    lower.contains("execute_batch") && (lower.contains("unknown method") || lower.contains("method not found"))
}

/// Deploy result for Schema Diff: single-connection transactional execution.
/// On statement failure the transaction is rolled back by the underlying path.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SchemaDiffDeployResult {
    pub transaction_id: String,
    pub status: String,
    pub participants: Vec<crate::two_phase_commit::ParticipantInfo>,
    pub created_at: String,
    pub updated_at: String,
    pub executed_count: usize,
    pub statement_count: usize,
    pub error: Option<String>,
    pub metadata: serde_json::Value,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SchemaDiffAtomicity {
    GuaranteedRollback,
    PartialEffectsPossible,
}

impl SchemaDiffAtomicity {
    fn ddl_atomic(self) -> bool {
        matches!(self, Self::GuaranteedRollback)
    }
}

fn database_supports_transactional_ddl(db_type: DatabaseType) -> bool {
    if resolve_for_db(db_type).has_capability(CAP_TRANSACTIONAL_DDL) {
        return true;
    }
    // SQLite-family DDL is transactional even when dialect registry omits the flag.
    matches!(db_type, DatabaseType::Sqlite | DatabaseType::Rqlite | DatabaseType::Turso | DatabaseType::CloudflareD1)
}

fn classify_schema_diff_atomicity(
    db_type: Option<DatabaseType>,
    statements: &[String],
    has_transactional_path: bool,
) -> SchemaDiffAtomicity {
    if !has_transactional_path {
        return SchemaDiffAtomicity::PartialEffectsPossible;
    }

    let mut contains_ddl = false;
    for statement in statements {
        let risk = match db_type {
            Some(db_type) => classify_sql_risk_for_database(statement, db_type).ok(),
            None => None,
        };

        match risk {
            Some(SqlRisk::Ddl) => {
                contains_ddl = true;
            }
            Some(SqlRisk::Write | SqlRisk::ReadOnly) => {}
            Some(SqlRisk::Transaction) | None => {
                return SchemaDiffAtomicity::PartialEffectsPossible;
            }
        }
    }

    if !contains_ddl {
        return SchemaDiffAtomicity::GuaranteedRollback;
    }

    match db_type {
        Some(db_type) if database_supports_transactional_ddl(db_type) => SchemaDiffAtomicity::GuaranteedRollback,
        _ => SchemaDiffAtomicity::PartialEffectsPossible,
    }
}

fn executed_count_before_error(error: &str, statement_count: usize) -> usize {
    let Some(rest) = error.strip_prefix("Statement ") else {
        return statement_count;
    };
    let digits: String = rest.chars().take_while(|ch| ch.is_ascii_digit()).collect();
    let Ok(statement_number) = digits.parse::<usize>() else {
        return statement_count;
    };
    statement_number.saturating_sub(1).min(statement_count)
}

fn is_destructive_schema_diff_statement(statement: &str) -> bool {
    let normalized = strip_sql_comments_and_literals(statement).to_ascii_uppercase();
    let tokens = normalized
        .split(|ch: char| !(ch.is_ascii_alphanumeric() || ch == '_'))
        .filter(|token| !token.is_empty())
        .collect::<Vec<_>>();
    match tokens.first().copied() {
        Some("DROP" | "TRUNCATE") => true,
        Some("ALTER") => tokens.iter().skip(1).any(|token| *token == "DROP"),
        _ => false,
    }
}

/// Pure failure mapping used by deploy and unit tests.
fn schema_diff_failure_outcome(
    atomicity: SchemaDiffAtomicity,
    error: &str,
    statement_count: usize,
) -> (crate::two_phase_commit::TransactionStatus, usize) {
    if atomicity.ddl_atomic() {
        (crate::two_phase_commit::TransactionStatus::RolledBack, 0)
    } else {
        (crate::two_phase_commit::TransactionStatus::Mixed, executed_count_before_error(error, statement_count))
    }
}

fn pool_kind_has_transactional_path(pool: &PoolKind) -> bool {
    match pool {
        PoolKind::Postgres(_)
        | PoolKind::Mysql(_, _)
        | PoolKind::Sqlite(_)
        | PoolKind::CloudflareD1(_)
        | PoolKind::ClickHouse(_)
        | PoolKind::Rqlite(_)
        | PoolKind::Turso(_)
        | PoolKind::SqlServer(_)
        | PoolKind::Agent(_) => true,
        PoolKind::MessageQueue
        | PoolKind::Nacos
        | PoolKind::Consul(_)
        | PoolKind::PluginConnection(_)
        | PoolKind::HBase(_)
        | PoolKind::DuckDbWorker(_)
        | PoolKind::Redis(_)
        | PoolKind::MongoDb(_)
        | PoolKind::DynamoDb(_)
        | PoolKind::Elasticsearch(_)
        | PoolKind::Easysearch(_)
        | PoolKind::Solr(_)
        | PoolKind::CouchDb(_)
        | PoolKind::Meilisearch(_)
        | PoolKind::Salesforce(_)
        | PoolKind::VectorDb(_)
        | PoolKind::InfluxDb(_)
        | PoolKind::InfluxDb3(_)
        | PoolKind::VictoriaMetrics(_)
        | PoolKind::ExternalDriver { .. } => false,
        #[cfg(feature = "mq-admin")]
        PoolKind::Mqtt(_) => false,
    }
}

/// Execute Schema Diff deploy SQL as one real single-connection transaction.
///
/// - Uses [`execute_statements_in_transaction`] so partial success rolls back.
/// - Returns a structured result (never re-executes statements to probe status).
/// - Comment-only / empty scripts succeed as `committed` with zero statements.
/// - When the target path cannot guarantee DDL atomicity (MySQL/Oracle DDL
///   auto-commit, unsupported batch transaction paths, etc.), a failure reports `mixed` and
///   `executed_count` reflects the statements that were issued before the
///   error, so the caller can warn the user that partial effects may persist.
pub async fn execute_schema_diff_deploy(
    state: &AppState,
    connection_id: &str,
    database: &str,
    statements: &[String],
    schema: Option<&str>,
    destructive_confirmed: bool,
) -> SchemaDiffDeployResult {
    let tx_id = format!("deploy_{}", uuid::Uuid::new_v4());
    let now = chrono::Utc::now().to_rfc3339();
    let db_type = connection_database_type(state, connection_id).await;

    // Resolve the live session mode, not the potentially stale saved UI metadata.
    let compatibility_mode = if matches!(db_type, Some(DatabaseType::OpenGauss | DatabaseType::Kingbase)) {
        match state.get_or_create_pool(connection_id, if database.is_empty() { None } else { Some(database) }).await {
            Ok(pool_key) => connection_sql_compatibility_mode(state, &pool_key, db_type).await,
            Err(_) => None,
        }
    } else {
        None
    };

    let parsed: Vec<String> = statements
        .iter()
        .flat_map(|s| {
            db_type.map_or_else(
                || split_sql_statements(s),
                |dt| {
                    crate::sql::split_sql_statements_for_database_with_compatibility(
                        s,
                        dt,
                        compatibility_mode.as_deref(),
                    )
                },
            )
        })
        .map(|s| s.trim().to_string())
        .filter(|s| {
            !s.is_empty()
                && !s.lines().all(|line| {
                    let t = line.trim();
                    t.is_empty() || t.starts_with("--")
                })
        })
        .collect();

    let participant = crate::two_phase_commit::ParticipantInfo {
        id: "connection".to_string(),
        name: format!("{connection_id}/{database}"),
        role: "database".to_string(),
    };

    let destructive_statement_count =
        parsed.iter().filter(|statement| is_destructive_schema_diff_statement(statement)).count();
    if destructive_statement_count > 0 && !destructive_confirmed {
        return SchemaDiffDeployResult {
            transaction_id: tx_id,
            status: crate::two_phase_commit::TransactionStatus::RolledBack.as_str().to_string(),
            participants: vec![participant],
            created_at: now.clone(),
            updated_at: now,
            executed_count: 0,
            statement_count: parsed.len(),
            error: Some("Destructive schema diff SQL requires explicit confirmation".to_string()),
            metadata: serde_json::json!({
                "source": "schema_diff_deploy",
                "mode": "single_connection_tx",
                "blocked": "destructive_confirmation_required",
                "destructive_statement_count": destructive_statement_count,
            }),
        };
    }

    if parsed.is_empty() {
        return SchemaDiffDeployResult {
            transaction_id: tx_id,
            status: crate::two_phase_commit::TransactionStatus::Committed.as_str().to_string(),
            participants: vec![participant],
            created_at: now.clone(),
            updated_at: now,
            executed_count: 0,
            statement_count: 0,
            error: None,
            metadata: serde_json::json!({"source": "schema_diff_deploy", "mode": "single_connection_tx"}),
        };
    }

    let pool_key = if database.is_empty() {
        connection_id.to_string()
    } else {
        match state.get_or_create_pool(connection_id, Some(database)).await {
            Ok(key) => key,
            Err(_) => {
                return SchemaDiffDeployResult {
                    transaction_id: tx_id.clone(),
                    status: crate::two_phase_commit::TransactionStatus::RolledBack.as_str().to_string(),
                    participants: vec![participant],
                    created_at: now.clone(),
                    updated_at: chrono::Utc::now().to_rfc3339(),
                    executed_count: 0,
                    statement_count: parsed.len(),
                    error: Some("Connection not available for deploy".to_string()),
                    metadata: serde_json::json!({"source": "schema_diff_deploy", "mode": "single_connection_tx"}),
                };
            }
        }
    };
    let has_transactional_path =
        { state.pool_handle(&pool_key).await.as_ref().is_some_and(pool_kind_has_transactional_path) };
    let atomicity = classify_schema_diff_atomicity(db_type, &parsed, has_transactional_path);

    match execute_statements_in_transaction_on_pool(state, &pool_key, connection_id, database, &parsed, schema, None)
        .await
    {
        Ok(result) => SchemaDiffDeployResult {
            transaction_id: tx_id,
            status: crate::two_phase_commit::TransactionStatus::Committed.as_str().to_string(),
            participants: vec![participant],
            created_at: now.clone(),
            updated_at: chrono::Utc::now().to_rfc3339(),
            executed_count: parsed.len(),
            statement_count: parsed.len(),
            error: None,
            metadata: serde_json::json!({
                "source": "schema_diff_deploy",
                "mode": "single_connection_tx",
                "affected_rows": result.affected_rows,
                "execution_time_ms": result.execution_time_ms,
            }),
        },
        Err(e) => {
            let (status, executed_count) = schema_diff_failure_outcome(atomicity, &e, parsed.len());
            SchemaDiffDeployResult {
                transaction_id: tx_id,
                status: status.as_str().to_string(),
                participants: vec![participant],
                created_at: now.clone(),
                updated_at: chrono::Utc::now().to_rfc3339(),
                executed_count,
                statement_count: parsed.len(),
                error: Some(e.clone()),
                metadata: serde_json::json!({
                    "source": "schema_diff_deploy",
                    "mode": "single_connection_tx",
                    "ddl_atomic": atomicity.ddl_atomic(),
                    "atomicity": match atomicity {
                        SchemaDiffAtomicity::GuaranteedRollback => "guaranteed_rollback",
                        SchemaDiffAtomicity::PartialEffectsPossible => "partial_effects_possible",
                    },
                    "error": e,
                }),
            }
        }
    }
}

/// Execute multiple SQL statements within a single transaction.
/// For pooled drivers (Postgres/MySQL), uses the driver transaction API.
/// For SQLite and SQL Server, uses a transaction on the driver's shared connection.
/// Agent drivers must provide the same rollbackable transaction contract.
/// Backends without a verified rollbackable path are rejected instead of being
/// silently executed one statement at a time in auto-commit mode.
pub async fn execute_statements_in_transaction(
    state: &AppState,
    connection_id: &str,
    database: &str,
    statements: &[String],
    schema: Option<&str>,
    catalog: Option<&str>,
) -> Result<db::QueryResult, String> {
    execute_statements_in_transaction_typed(state, connection_id, database, statements, schema, catalog, None)
        .await
        .map_err(QueryExecutionError::into_legacy_string)
}

/// Execute multiple SQL statements transactionally while retaining typed failures.
pub async fn execute_statements_in_transaction_typed(
    state: &AppState,
    connection_id: &str,
    database: &str,
    statements: &[String],
    schema: Option<&str>,
    catalog: Option<&str>,
    timeout_secs: Option<u64>,
) -> Result<db::QueryResult, QueryExecutionError> {
    let sql_ctx = statements.first().map(|s| s.as_str()).unwrap_or("");
    let pool_database = query_pool_database(database, catalog);
    let pool_key = state
        .get_or_create_pool(connection_id, pool_database)
        .await
        .map_err(|e| query_error_with_omitted_sql_context(&e, sql_ctx))?;

    execute_statements_in_transaction_on_pool_typed(
        state,
        &pool_key,
        connection_id,
        database,
        statements,
        schema,
        catalog,
        timeout_secs,
    )
    .await
}

/// Execute multiple SQL statements transactionally on an already-resolved pool.
/// This preserves session-scoped pools used by long-running imports.
pub async fn execute_statements_in_transaction_on_pool(
    state: &AppState,
    pool_key: &str,
    connection_id: &str,
    database: &str,
    statements: &[String],
    schema: Option<&str>,
    catalog: Option<&str>,
) -> Result<db::QueryResult, String> {
    execute_statements_in_transaction_on_pool_typed(
        state,
        pool_key,
        connection_id,
        database,
        statements,
        schema,
        catalog,
        None,
    )
    .await
    .map_err(QueryExecutionError::into_legacy_string)
}

/// Execute a transaction on an already-resolved pool without erasing typed failures.
pub async fn execute_statements_in_transaction_on_pool_typed(
    state: &AppState,
    pool_key: &str,
    connection_id: &str,
    database: &str,
    statements: &[String],
    schema: Option<&str>,
    catalog: Option<&str>,
    timeout_secs: Option<u64>,
) -> Result<db::QueryResult, QueryExecutionError> {
    let db_type = connection_database_type(state, connection_id).await;

    // Read-only check: intercept all transaction paths before dispatching
    check_read_only_for_connection_multi(state, pool_key, statements).await?;

    let start = std::time::Instant::now();
    let mysql_catalog_dialect = connection_mysql_catalog_dialect(state, connection_id).await;
    let mut operation_budget = configured_operation_budget_for_pool_key(state, pool_key).await;
    apply_query_timeout_override(&mut operation_budget, timeout_secs);

    // This is the single capability model for explicit batch transactions.
    // Do not add a backend here unless its execution path keeps every statement
    // on one transaction-capable connection and can roll back on failure.
    // Agent drivers are delegated because their transaction RPC has the same
    // contract and rejects drivers that cannot provide it.
    // Clone the pool handle within the lock, then drop it before any async work.
    let path = { state.pool_handle(pool_key).await.as_ref().map(batch_transaction_path) };

    let result = match path {
        Some(BatchTransactionPath::Pg(pool)) => {
            let cancel_context = state.get_postgres_cancel_context(pool_key).await;
            exec_tx_pg_inner(pool, db_type, statements, schema, start, operation_budget.clone(), cancel_context).await
        }
        Some(BatchTransactionPath::Mysql(pool)) => exec_tx_mysql_inner(
            state,
            pool_key,
            pool,
            statements,
            start,
            operation_budget.clone(),
            mysql_catalog_dialect,
            catalog,
            database,
        )
        .await
        .map_err(Into::into),
        Some(BatchTransactionPath::Sqlite(pool)) => {
            exec_tx_sqlite_inner(pool, statements, start, &operation_budget).await.map_err(Into::into)
        }
        Some(BatchTransactionPath::Agent(client)) => {
            let result = exec_tx_agent_inner(
                client.clone(),
                db_type,
                Some(database),
                statements,
                schema,
                start,
                &operation_budget,
            )
            .await;
            if let Err(error) = result.as_ref() {
                discard_agent_pool_after_typed_error(state, pool_key, &client, error, RecoveryScope::UserOperation)
                    .await;
            }
            return result.map_err(QueryExecutionError::Agent);
        }
        Some(BatchTransactionPath::Explicit) => {
            let mysql_dialect = connection_mysql_query_dialect(state, connection_id).await;
            exec_tx_explicit_inner(state, pool_key, mysql_dialect, Some(database), statements, schema, start)
                .await
                .map_err(Into::into)
        }
        Some(BatchTransactionPath::Unsupported) => Err(
            "The active backend cannot provide a rollbackable transaction for a batch; run without use_transaction."
                .to_string()
                .into(),
        ),
        None => Err("Connection not found for transaction".to_string().into()),
    };

    if let Err(err) = result.as_ref() {
        discard_pool_after_error(state, pool_key, db_type, &err.to_string()).await;
    }

    result
}

/// Whether an opt-in explicit batch transaction (`use_transaction`) must be
/// rejected because the backend's DDL statements implicitly commit and cannot be
/// rolled back. Used by the opt-in batch-transaction entry point (the
/// `use_transaction` branch of [`execute_multi_core_with_options_for_client_and_progress_typed`])
/// and by the MCP layer's identical pre-check. It covers every backend whose DDL
/// is not rollbackable (MySQL-family and Oracle) via the single capability
/// predicate (`database_supports_transactional_ddl`) without re-listing engines
/// here. Paths that document a mixed-outcome-on-failure behaviour (schema-diff
/// deploy, imports) do not call this and keep running-and-reporting.
pub fn batch_transaction_ddl_is_unrollbackable(db_type: Option<DatabaseType>, statements: &[String]) -> bool {
    let Some(db_type) = db_type else {
        return false;
    };
    if database_supports_transactional_ddl(db_type) {
        return false;
    }
    statements.iter().any(|statement| matches!(classify_sql_risk_for_database(statement, db_type), Ok(SqlRisk::Ddl)))
}

/// Owned transaction-capable pool variants for safe dispatch across async boundaries.
enum BatchTransactionPath {
    Pg(deadpool_postgres::Pool),
    Mysql(db::mysql::MySqlPool),
    Sqlite(db::sqlite::SqliteHandle),
    Agent(Arc<crate::db::agent_driver::PooledAgentClient>),
    Explicit,
    Unsupported,
}

fn batch_transaction_path(pool: &PoolKind) -> BatchTransactionPath {
    match pool {
        PoolKind::Postgres(pg) => BatchTransactionPath::Pg(pg.clone()),
        PoolKind::Mysql(pool, _mode) => BatchTransactionPath::Mysql(pool.clone()),
        PoolKind::Sqlite(pool) => BatchTransactionPath::Sqlite(pool.clone()),
        PoolKind::SqlServer(_) => BatchTransactionPath::Explicit,
        PoolKind::Agent(client) => BatchTransactionPath::Agent(client.clone()),
        PoolKind::MessageQueue
        | PoolKind::Nacos
        | PoolKind::PluginConnection(_)
        | PoolKind::Consul(_)
        | PoolKind::HBase(_) => BatchTransactionPath::Unsupported,
        #[cfg(feature = "mq-admin")]
        PoolKind::Mqtt(_) => BatchTransactionPath::Unsupported,
        PoolKind::DuckDbWorker(_)
        | PoolKind::Redis(_)
        | PoolKind::MongoDb(_)
        | PoolKind::DynamoDb(_)
        | PoolKind::ClickHouse(_)
        | PoolKind::Rqlite(_)
        | PoolKind::Turso(_)
        | PoolKind::CloudflareD1(_)
        | PoolKind::Elasticsearch(_)
        | PoolKind::Easysearch(_)
        | PoolKind::Solr(_)
        | PoolKind::CouchDb(_)
        | PoolKind::Meilisearch(_)
        | PoolKind::Salesforce(_)
        | PoolKind::VectorDb(_)
        | PoolKind::InfluxDb(_)
        | PoolKind::InfluxDb3(_)
        | PoolKind::VictoriaMetrics(_)
        | PoolKind::ExternalDriver { .. } => BatchTransactionPath::Unsupported,
    }
}

// Each of these acquires a dedicated connection and runs all statements within
// BEGIN ... COMMIT/ROLLBACK, guaranteeing a single physical connection.

async fn exec_tx_pg_inner(
    pool: deadpool_postgres::Pool,
    db_type: Option<DatabaseType>,
    statements: &[String],
    schema: Option<&str>,
    start: std::time::Instant,
    budget: DbOperationBudget,
    cancel_context: Option<db::postgres::PostgresCancelContext>,
) -> Result<db::QueryResult, QueryExecutionError> {
    let mut client = db::postgres::checkout_postgres_client(&pool, None, budget.checkout_timeout)
        .await
        .map_err(|e| format!("Failed to acquire connection: {}", e))?;
    let had_schema = schema.is_some();
    if let Some(s) = schema {
        db::postgres::set_postgres_search_path(
            &client,
            s,
            db::postgres::PostgresSearchPathContext::Transaction,
            budget.recycle_timeout,
        )
        .await
        .map_err(|e| format!("SET search_path failed: {}", e))?;
    }
    let tx_result = exec_tx_pg_statements(&mut client, statements, &budget, cancel_context).await;

    // GaussDB/openGauss reject PostgreSQL's RESET search_path syntax.
    let reset_result = if had_schema {
        db::postgres::execute_postgres_infra_statement(
            &client,
            db::postgres::reset_search_path_sql(db_type),
            budget.cleanup_timeout,
            "schema.reset",
        )
        .await
        .map_err(|err| QueryExecutionError::Legacy(format!("PostgreSQL schema.reset cleanup failed: {err}")))
    } else {
        Ok(0)
    };

    match (tx_result, reset_result) {
        (Ok(total_affected), Ok(_)) => Ok(db::QueryResult {
            columns: vec![],
            column_types: Vec::new(),
            deferred_large_value_cells: Vec::new(),
            column_sortables: vec![],
            spatial_columns: vec![],
            raw_key_cells: Vec::new(),
            spatial_values: vec![],
            rows: vec![],
            affected_rows: total_affected,
            execution_time_ms: start.elapsed().as_millis(),
            server_execute_time_us: None,
            query_timings_ms: None,
            truncated: false,
            session_id: None,
            has_more: false,
            elasticsearch_raw_body: None,
            messages: Vec::new(),
        }),
        (Err(e), Ok(_)) => Err(e),
        (Ok(_), Err(reset_err)) => Err(reset_err),
        (Err(e), Err(reset_err)) => Err(e.with_context(&reset_err.to_string())),
    }
}

async fn exec_tx_pg_statements(
    client: &mut deadpool_postgres::Client,
    statements: &[String],
    budget: &DbOperationBudget,
    cancel_context: Option<db::postgres::PostgresCancelContext>,
) -> Result<u64, QueryExecutionError> {
    let statements =
        statements.iter().map(|sql| db::postgres::encode_postgres_sql(client, sql)).collect::<Result<Vec<_>, _>>()?;
    let tx = tokio::time::timeout(budget.recycle_timeout, client.transaction())
        .await
        .map_err(|_| {
            format!("Failed to begin transaction: timed out after {} seconds", budget.recycle_timeout.as_secs())
        })?
        .map_err(|e| format!("Failed to begin transaction: {}", e))?;
    let mut total_affected: u64 = 0;
    for (i, sql) in statements.iter().enumerate() {
        let pg_cancel_token = tx.client().cancel_token();
        let mut is_server_error = false;
        let affected = db::postgres::wait_postgres_operation(
            pg_cancel_token,
            cancel_context.clone(),
            budget.query_timeout,
            budget.cancel_timeout,
            async {
                tx.execute(sql, &[]).await.map_err(|error| {
                    is_server_error = error.as_db_error().is_some();
                    error.to_string()
                })
            },
        )
        .await
        .map_err(|e| postgres_transaction_statement_error(i + 1, &e, sql, is_server_error))?;
        total_affected += affected;
    }
    tokio::time::timeout(budget.cleanup_timeout, tx.commit())
        .await
        .map_err(|_| format!("COMMIT timed out after {} seconds", budget.cleanup_timeout.as_secs()))?
        .map_err(|e| format!("COMMIT failed: {}", e))?;
    Ok(total_affected)
}

async fn exec_tx_mysql_inner(
    state: &AppState,
    pool_key: &str,
    pool: db::mysql::MySqlPool,
    statements: &[String],
    start: std::time::Instant,
    budget: DbOperationBudget,
    catalog_dialect: Option<db::mysql::MySqlCatalogDialect>,
    catalog: Option<&str>,
    database: &str,
) -> Result<db::QueryResult, String> {
    let mut conn = db::mysql::get_conn_with_health_check_with_timeout(&pool, budget.checkout_timeout).await?;
    apply_oceanbase_mysql_session_timeout(state, pool_key, &mut conn, None).await?;
    db::mysql::apply_catalog_database_context(&mut conn, catalog_dialect, catalog, database).await?;
    mysql_query_drop_with_timeout(
        &mut conn,
        "START TRANSACTION",
        budget.recycle_timeout,
        "Failed to begin transaction",
    )
    .await?;
    let mut total_affected: u64 = 0;
    for (i, sql) in statements.iter().enumerate() {
        match mysql_query_iter_with_timeout(&mut conn, sql, budget.query_timeout).await {
            Ok(affected) => total_affected += affected,
            Err(e) => {
                let _ = mysql_query_drop_with_timeout(&mut conn, "ROLLBACK", budget.cleanup_timeout, "ROLLBACK failed")
                    .await;
                return Err(query_error_with_omitted_sql_context(&format!("Statement {} failed: {}", i + 1, e), sql));
            }
        }
    }
    mysql_query_drop_with_timeout(&mut conn, "COMMIT", budget.cleanup_timeout, "COMMIT failed").await?;
    Ok(db::QueryResult {
        columns: vec![],
        column_types: Vec::new(),
        deferred_large_value_cells: Vec::new(),
        column_sortables: vec![],
        spatial_columns: vec![],
        raw_key_cells: Vec::new(),
        spatial_values: vec![],
        rows: vec![],
        affected_rows: total_affected,
        execution_time_ms: start.elapsed().as_millis(),
        server_execute_time_us: None,
        query_timings_ms: None,
        truncated: false,
        session_id: None,
        has_more: false,
        elasticsearch_raw_body: None,
        messages: Vec::new(),
    })
}

async fn mysql_query_drop_with_timeout(
    conn: &mut mysql_async::Conn,
    sql: &str,
    timeout_duration: Duration,
    context: &str,
) -> Result<(), String> {
    tokio::time::timeout(timeout_duration, conn.query_drop(sql))
        .await
        .map_err(|_| format!("{context}: timed out after {} seconds", timeout_duration.as_secs()))?
        .map_err(|e| format!("{context}: {e}"))
}

async fn mysql_query_iter_with_timeout(
    conn: &mut mysql_async::Conn,
    sql: &str,
    timeout_duration: Option<Duration>,
) -> Result<u64, String> {
    match timeout_duration {
        Some(timeout_duration) => tokio::time::timeout(timeout_duration, conn.query_iter(sql))
            .await
            .map_err(|_| format!("Query timed out after {} seconds", timeout_duration.as_secs()))?
            .map(|result| result.affected_rows())
            .map_err(|e| e.to_string()),
        None => conn.query_iter(sql).await.map(|result| result.affected_rows()).map_err(|e| e.to_string()),
    }
}

async fn exec_tx_sqlite_inner(
    pool: db::sqlite::SqliteHandle,
    statements: &[String],
    start: std::time::Instant,
    budget: &DbOperationBudget,
) -> Result<db::QueryResult, String> {
    let statements = statements.to_vec();
    let query_timeout = budget.query_timeout;
    if let Some(worker) = pool.worker() {
        return exec_tx_sqlite_worker_inner(worker, statements, start, query_timeout).await;
    }
    tokio::task::spawn_blocking(move || {
        pool.with_connection(|conn| {
            conn.execute_batch("BEGIN").map_err(|e| format!("Failed to begin transaction: {}", e))?;

            // rusqlite's synchronous API blocks for the full duration of a single
            // statement, so a per-statement-boundary elapsed check alone cannot
            // interrupt a statement that itself runs past the query budget. Use a
            // cross-thread watchdog: it sleeps on a condvar for the budget, then
            // fires `InterruptHandle::interrupt()` (SQLITE_INTERRUPT) which aborts
            // the currently executing statement mid-flight. The watchdog is disarmed
            // and joined before COMMIT so a stale interrupt can never land on a
            // future, unrelated query of this pooled connection.
            let interrupt = conn.get_interrupt_handle();
            let armed = Arc::new((Mutex::new(true), Condvar::new()));
            let watchdog = match query_timeout {
                Some(timeout) => {
                    let armed = armed.clone();
                    Some(std::thread::spawn(move || {
                        let (lock, cvar) = &*armed;
                        let mut guard = lock.lock().unwrap();
                        let mut should_interrupt = false;
                        if *guard {
                            // Armed at wait start: wait for the budget or until the
                            // main thread disarms (notifies) after finishing. A
                            // spurious wakeup re-waits for the remaining budget, so
                            // interrupt() only fires when the budget genuinely
                            // elapsed while still armed.
                            let wait_start = std::time::Instant::now();
                            let mut remaining = timeout;
                            loop {
                                let (guard2, wait_result) = cvar
                                    .wait_timeout(guard, remaining)
                                    .expect("sqlite tx watchdog condvar wait poisoned");
                                guard = guard2;
                                if !*guard {
                                    // Disarmed: the main thread finished first.
                                    break;
                                }
                                if wait_result.timed_out() || wait_start.elapsed() >= timeout {
                                    // Budget elapsed while still armed: interrupt.
                                    should_interrupt = true;
                                    break;
                                }
                                remaining = timeout.saturating_sub(wait_start.elapsed());
                            }
                        }
                        if should_interrupt {
                            interrupt.interrupt();
                        }
                    }))
                }
                None => None,
            };

            let mut timeout_error: Option<String> = None;
            let mut statement_error: Option<String> = None;
            let mut total_affected: u64 = 0;
            let result = (|| {
                for (i, sql) in statements.iter().enumerate() {
                    // Defense in depth: the boundary check still runs (it also
                    // guarantees we never COMMIT past the budget even if the
                    // watchdog was not armed because the timeout is None).
                    if let Some(timeout) = query_timeout {
                        if start.elapsed() >= timeout {
                            timeout_error = Some(format!("Query timed out after {} seconds", timeout.as_secs()));
                            return Err(());
                        }
                    }
                    match conn.execute_batch(sql) {
                        Ok(_) => total_affected += conn.changes(),
                        Err(e) => {
                            // The watchdog interrupt aborts the statement with
                            // SQLITE_INTERRUPT; surface it as a query timeout
                            // (matching `mysql_query_iter_with_timeout` wording so
                            // `is_dbx_query_timeout_error` recognizes it). The
                            // interrupt is detected by the SQLITE_INTERRUPT error
                            // code, never by matching "interrupt" in the message
                            // text (user/trigger/constraint text could otherwise
                            // be misclassified as a timeout).
                            //
                            // The SQLITE_INTERRUPT error-code match is only
                            // consulted when a query budget is set: the watchdog
                            // (this function's only in-process source of
                            // SQLITE_INTERRUPT intended to be a timeout) is only
                            // armed when query_timeout is Some. An EXTERNAL
                            // interrupt (e.g. the query_cancel mechanism) with no
                            // budget must surface as a normal statement error, not
                            // a timeout — misclassifying it panics on
                            // `query_timeout.unwrap()` below (None) and skips the
                            // ROLLBACK/disarm below, leaking the open transaction.
                            let timed_out = query_timeout.is_some_and(|timeout| {
                                start.elapsed() >= timeout
                                    || matches!(
                                        e.sqlite_error_code(),
                                        Some(rusqlite::ffi::ErrorCode::OperationInterrupted)
                                    )
                            });
                            if timed_out {
                                timeout_error = Some(format!(
                                    "Query timed out after {} seconds",
                                    query_timeout.unwrap_or_default().as_secs()
                                ));
                                return Err(());
                            }
                            statement_error = Some(query_error_with_omitted_sql_context(
                                &format!("Statement {} failed: {}", i + 1, e),
                                sql,
                            ));
                            return Err(());
                        }
                    }
                }
                Ok(())
            })();

            // Disarm and join the watchdog before issuing COMMIT (or ROLLBACK) so
            // no interrupt can be delivered after the transaction ends.
            {
                let (lock, cvar) = &*armed;
                *lock.lock().unwrap() = false;
                cvar.notify_all();
            }
            if let Some(watchdog) = watchdog {
                watchdog.join().expect("sqlite tx watchdog thread joined");
            }

            match result {
                Ok(()) => {
                    // The boundary check above runs at the top of each loop
                    // iteration only. After the last statement the loop returns
                    // Ok and COMMIT would run without any elapsed re-check, so a
                    // statement that started under budget and finished after the
                    // budget elapsed could still COMMIT. Guard once more here,
                    // before COMMIT, so we never COMMIT past the budget.
                    if let Some(timeout) = query_timeout {
                        if start.elapsed() >= timeout {
                            let _ = conn.execute_batch("ROLLBACK");
                            return Err(format!("Query timed out after {} seconds", timeout.as_secs()));
                        }
                    }
                    conn.execute_batch("COMMIT").map_err(|e| format!("COMMIT failed: {}", e)).map(|_| db::QueryResult {
                        columns: vec![],
                        column_types: Vec::new(),
                        deferred_large_value_cells: Vec::new(),
                        column_sortables: vec![],
                        spatial_columns: vec![],
                        raw_key_cells: Vec::new(),
                        spatial_values: vec![],
                        rows: vec![],
                        affected_rows: total_affected,
                        execution_time_ms: start.elapsed().as_millis(),
                        server_execute_time_us: None,
                        query_timings_ms: None,
                        truncated: false,
                        session_id: None,
                        has_more: false,
                        elasticsearch_raw_body: None,
                        messages: Vec::new(),
                    })
                }
                Err(()) => {
                    let _ = conn.execute_batch("ROLLBACK");
                    Err(timeout_error
                        .or(statement_error)
                        .unwrap_or_else(|| "Statement execution failed inside the transaction".to_string()))
                }
            }
        })
    })
    .await
    .map_err(|e| e.to_string())?
}

async fn exec_tx_sqlite_worker_inner(
    worker: Arc<db::sqlite_worker::SqliteWorkerClient>,
    statements: Vec<String>,
    start: std::time::Instant,
    query_timeout: Option<Duration>,
) -> Result<db::QueryResult, String> {
    // Detached like the local spawn_blocking path, so a dropped caller cannot leave the
    // worker connection inside an open transaction. One session keeps other requests out.
    tokio::spawn(async move {
        let mut session = worker.session().await;
        session.query("BEGIN", None).await.map_err(|e| format!("Failed to begin transaction: {e}"))?;
        // ponytail: the worker cannot interrupt a running statement, so the budget is only
        // checked between statements; bounding one slow statement needs a worker interrupt op.
        let within_budget = || match query_timeout {
            Some(timeout) if start.elapsed() >= timeout => {
                Err(format!("Query timed out after {} seconds", timeout.as_secs()))
            }
            _ => Ok(()),
        };
        let outcome: Result<db::QueryResult, String> = async {
            let mut total_affected = 0;
            for (i, sql) in statements.iter().enumerate() {
                within_budget()?;
                total_affected += session
                    .query(sql, None)
                    .await
                    .map_err(|e| {
                        query_error_with_omitted_sql_context(&format!("Statement {} failed: {}", i + 1, e), sql)
                    })?
                    .affected_rows;
            }
            within_budget()?;
            let committed = session.query("COMMIT", None).await.map_err(|e| format!("COMMIT failed: {e}"))?;
            Ok(db::QueryResult {
                affected_rows: total_affected,
                execution_time_ms: start.elapsed().as_millis(),
                ..committed
            })
        }
        .await;
        if outcome.is_err() {
            let _ = session.query("ROLLBACK", None).await;
        }
        outcome
    })
    .await
    .map_err(|e| e.to_string())?
}

async fn exec_tx_explicit_inner(
    state: &AppState,
    pool_key: &str,
    mysql_dialect: db::mysql::MySqlQueryDialect,
    database: Option<&str>,
    statements: &[String],
    schema: Option<&str>,
    start: std::time::Instant,
) -> Result<db::QueryResult, String> {
    do_execute(
        state,
        pool_key,
        mysql_dialect,
        database,
        "BEGIN TRANSACTION",
        schema,
        None,
        QueryExecutionOptions::default(),
    )
    .await
    .map_err(|e| format!("Failed to begin transaction: {}", e))?;

    let mut total_affected: u64 = 0;
    for (i, sql) in statements.iter().enumerate() {
        match do_execute(state, pool_key, mysql_dialect, database, sql, schema, None, QueryExecutionOptions::default())
            .await
        {
            Ok(result) => {
                total_affected += result.affected_rows;
            }
            Err(e) => {
                if let Err(rb_err) = do_execute(
                    state,
                    pool_key,
                    mysql_dialect,
                    database,
                    "ROLLBACK",
                    schema,
                    None,
                    QueryExecutionOptions::default(),
                )
                .await
                {
                    log::error!("ROLLBACK failed after statement {} error: {}", i + 1, rb_err);
                }
                return Err(query_error_with_omitted_sql_context(&format!("Statement {} failed: {}", i + 1, e), sql));
            }
        }
    }

    do_execute(state, pool_key, mysql_dialect, database, "COMMIT", schema, None, QueryExecutionOptions::default())
        .await
        .map_err(|e| format!("COMMIT failed: {}", e))?;

    Ok(db::QueryResult {
        columns: vec![],
        column_types: Vec::new(),
        deferred_large_value_cells: Vec::new(),
        column_sortables: vec![],
        spatial_columns: vec![],
        raw_key_cells: Vec::new(),
        spatial_values: vec![],
        rows: vec![],
        affected_rows: total_affected,
        execution_time_ms: start.elapsed().as_millis(),
        server_execute_time_us: None,
        query_timings_ms: None,
        truncated: false,
        session_id: None,
        has_more: false,
        elasticsearch_raw_body: None,
        messages: Vec::new(),
    })
}

async fn exec_tx_agent_inner(
    client: Arc<crate::db::agent_driver::PooledAgentClient>,
    db_type: Option<DatabaseType>,
    database: Option<&str>,
    statements: &[String],
    schema: Option<&str>,
    start: std::time::Instant,
    budget: &DbOperationBudget,
) -> Result<db::QueryResult, AgentCallError> {
    let execution_schema = schema_for_execution_context(db_type, schema);
    let rewritten_statements;
    let statements = if qualifies_unqualified_agent_relations(db_type) {
        rewritten_statements = statements
            .iter()
            .map(|sql| sql_for_execution_context_with_identifier_quote(db_type, sql, schema, client.identifier_quote()))
            .collect::<Vec<_>>();
        rewritten_statements.as_slice()
    } else {
        statements
    };
    let mut client = client.lock().await;
    let result: db::QueryResult =
        client.execute_transaction_typed(database, statements, execution_schema, budget.query_timeout).await?;
    Ok(db::QueryResult { execution_time_ms: start.elapsed().as_millis(), ..result })
}

/// Start a manual transaction session, holding a connection from the pool.
/// Returns a transaction session ID that must be passed to subsequent calls.
pub async fn begin_manual_transaction(
    state: &AppState,
    connection_id: &str,
    database: &str,
    schema: Option<&str>,
    catalog: Option<&str>,
) -> Result<String, String> {
    begin_transaction_session(state, connection_id, database, schema, catalog, false).await
}

/// Start a read-only, repeatable snapshot for a database backup.
pub async fn begin_database_backup_snapshot(
    state: &AppState,
    connection_id: &str,
    database: &str,
) -> Result<String, String> {
    begin_transaction_session(state, connection_id, database, None, None, true).await
}

fn postgres_transaction_begin_sql(consistent_snapshot: bool) -> &'static str {
    if consistent_snapshot {
        "BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY"
    } else {
        "BEGIN"
    }
}

fn mysql_transaction_begin_sql_candidates(consistent_snapshot: bool) -> &'static [&'static str] {
    if consistent_snapshot {
        &[
            "START TRANSACTION WITH CONSISTENT SNAPSHOT, READ ONLY",
            "START TRANSACTION WITH CONSISTENT SNAPSHOT",
            "START TRANSACTION",
        ]
    } else {
        &["START TRANSACTION"]
    }
}

fn mysql_transaction_isolation_sql(consistent_snapshot: bool) -> Option<&'static str> {
    consistent_snapshot.then_some("SET TRANSACTION ISOLATION LEVEL REPEATABLE READ")
}

fn mysql_error_is_syntax_error(error: &mysql_async::Error) -> bool {
    match error {
        mysql_async::Error::Server(server_error) if server_error.code == 1064 => true,
        // Doris (and some other MySQL-compatible servers) report unsupported
        // transaction clauses as ER_UNKNOWN_ERROR (1105) with a parser
        // diagnostic instead of MySQL's ER_PARSE_ERROR (1064).  Only treat
        // diagnostics that clearly identify a syntax/parser failure as
        // retryable; generic 1105 errors may indicate a real server failure.
        mysql_async::Error::Server(server_error) if server_error.code == 1105 => {
            let message = server_error.message.to_ascii_lowercase();
            message.contains("syntax error") && (message.contains("encountered:") || message.contains("expected"))
        }
        // PolarDB-X exposes parser failures such as PXC-4500 / ERR_PARSER
        // through the generic MySQL protocol ERR_HANDLE_DATA code (3009).
        mysql_async::Error::Server(server_error) if server_error.code == 3009 => {
            let message = server_error.message.to_ascii_lowercase();
            message.contains("[err_parser]") || message.contains("syntax error") || message.contains("语法错误")
        }
        _ => false,
    }
}

/// Compute per-execution-statement proven-read-only markers for the sticky
/// manual-transaction UX (#7122 Oracle, #9018 MySQL/PostgreSQL, ... GaussDB).
/// The user-facing classification SQL is split with the same dialect-aware
/// splitter as the execution SQL and paired by count/position; any mismatch is
/// fail-closed (no markers). Oracle/OceanBase-Oracle reach the same AST proof as
/// the PostgreSQL family through `is_oracle_proven_read_only_statement`, which
/// keeps the Oracle-specific lexical guard rails (see `sql_risk`); MySQL and the
/// PostgreSQL family use the strict `sql_risk` proof directly; every other
/// dialect is unproven.
fn classify_manual_transaction_statements(
    database_type: Option<DatabaseType>,
    execution_statement_count: usize,
    classification_sql: Option<&str>,
) -> Vec<bool> {
    let Some(database_type) = database_type.filter(|database_type| {
        matches!(
            database_type,
            DatabaseType::Oracle
                | DatabaseType::OceanbaseOracle
                | DatabaseType::Mysql
                | DatabaseType::Postgres
                | DatabaseType::Gaussdb
                | DatabaseType::OpenGauss
        )
    }) else {
        return Vec::new();
    };
    let Some(classification_sql) = classification_sql else {
        return Vec::new();
    };
    let user_statements = crate::sql::split_sql_statements_for_database(classification_sql, database_type);
    let paired = user_statements.len() == execution_statement_count && !user_statements.is_empty();
    if !paired {
        return Vec::new();
    }
    user_statements
        .iter()
        .map(|statement| match database_type {
            DatabaseType::Mysql | DatabaseType::Postgres | DatabaseType::Gaussdb | DatabaseType::OpenGauss => {
                crate::sql_risk::prove_read_only_for_database(statement, database_type)
                    == crate::sql_risk::ReadProof::ProvenReadOnly
            }
            _ => is_oracle_proven_read_only_statement(statement),
        })
        .collect()
}

async fn begin_transaction_session(
    state: &AppState,
    connection_id: &str,
    database: &str,
    schema: Option<&str>,
    catalog: Option<&str>,
    consistent_snapshot: bool,
) -> Result<String, String> {
    if !consistent_snapshot && connection_database_type(state, connection_id).await == Some(DatabaseType::SqlServer) {
        return sqlserver_manual_transaction::begin(state, connection_id, database, schema, catalog).await;
    }
    let mysql_catalog_dialect = connection_mysql_catalog_dialect(state, connection_id).await;
    let pool_database = query_pool_database(database, catalog);
    // Probe the primary pool to learn the backend kind. Agent manual TX opens a
    // dedicated multi_session workload pool so sticky TX state is isolated.
    let probe_pool_key = state.get_or_create_pool(connection_id, pool_database).await?;

    // Clone the pool handle under a brief read lock, then drop the lock before
    // any async I/O — same pattern as do_execute throughout this file.
    enum TxnPoolHandle {
        Postgres(deadpool_postgres::Pool),
        Mysql(db::mysql::MySqlPool),
        Agent,
        ExternalDriver,
    }
    let pool_handle = {
        let pool = state.pool_handle(&probe_pool_key).await.ok_or("Connection not found")?;
        match &pool {
            PoolKind::Postgres(pg) => TxnPoolHandle::Postgres(pg.clone()),
            PoolKind::Mysql(mp, _) => TxnPoolHandle::Mysql(mp.clone()),
            PoolKind::Agent(_) if !consistent_snapshot => TxnPoolHandle::Agent,
            PoolKind::ExternalDriver { .. } if !consistent_snapshot => TxnPoolHandle::ExternalDriver,
            _ => return Err("Manual transaction is not supported for this database type".to_string()),
        }
    }; // connections lock released here
    let mysql_data_charset = match &pool_handle {
        TxnPoolHandle::Mysql(pool) => pool.data_charset(),
        _ => None,
    };

    let (txn_conn, pool_key) = match pool_handle {
        TxnPoolHandle::Postgres(pg_pool) => {
            let conn = pg_pool.get().await.map_err(|e| format!("Failed to get Postgres connection: {e}"))?;
            let begin_sql = postgres_transaction_begin_sql(consistent_snapshot);
            conn.execute_typed(begin_sql, &[]).await.map_err(|e| format!("BEGIN failed: {e}"))?;
            if let Some(schema) = schema {
                db::postgres::set_postgres_search_path(
                    &conn,
                    schema,
                    db::postgres::PostgresSearchPathContext::LocalTransaction,
                    db::connection_timeout(),
                )
                .await
                .map_err(|e| format!("SET search_path failed: {e}"))?;
            }
            (TxnConnection::Postgres(Box::new(conn)), probe_pool_key.clone())
        }
        TxnPoolHandle::Mysql(mysql_pool) => {
            let mut conn = mysql_pool.get_conn().await.map_err(|e| format!("Failed to get MySQL connection: {e}"))?;
            db::mysql::apply_catalog_database_context(&mut conn, mysql_catalog_dialect, catalog, database).await?;
            if let Some(isolation_sql) = mysql_transaction_isolation_sql(consistent_snapshot) {
                conn.query_drop(isolation_sql).await.map_err(|e| format!("SET TRANSACTION failed: {e}"))?;
            }
            let mut syntax_errors = Vec::new();
            for begin_sql in mysql_transaction_begin_sql_candidates(consistent_snapshot) {
                match conn.query_drop(*begin_sql).await {
                    Ok(()) => {
                        syntax_errors.clear();
                        break;
                    }
                    Err(error) if mysql_error_is_syntax_error(&error) => {
                        syntax_errors.push(format!("{begin_sql}: {error}"));
                    }
                    Err(error) => return Err(format!("START TRANSACTION failed: {error}")),
                }
            }
            if !syntax_errors.is_empty() {
                return Err(format!("START TRANSACTION failed for all compatible forms: {}", syntax_errors.join("; ")));
            }
            (TxnConnection::Mysql(Some(conn)), probe_pool_key.clone())
        }
        TxnPoolHandle::Agent => {
            let db_type = connection_database_type(state, connection_id).await;
            let client_session_id = format!("manual-txn-{}", uuid::Uuid::new_v4());
            let agent_pool_key =
                state.get_or_create_pool_for_session(connection_id, pool_database, Some(&client_session_id)).await?;
            let client = {
                let pool_handle = state.pool_handle(&agent_pool_key).await;
                match pool_handle.as_ref() {
                    Some(PoolKind::Agent(client)) => client.clone(),
                    _ => {
                        let _ = state.close_client_session_pool(connection_id, pool_database, &client_session_id).await;
                        return Err("Agent connection not found for manual transaction".to_string());
                    }
                }
            };
            let begin_result = {
                let mut locked = client.lock().await;
                locked
                    .begin_manual_transaction::<serde_json::Value>(schema_for_execution_context(db_type, schema))
                    .await
            };
            if let Err(error) = begin_result {
                let _ = state.close_client_session_pool(connection_id, pool_database, &client_session_id).await;
                return Err(format!("BEGIN manual transaction failed: {error}"));
            }
            let Some(cleanup_guard) =
                state.workload_session_pool_cleanup_guard(connection_id, pool_database, &client_session_id).await
            else {
                let _ = state.close_client_session_pool(connection_id, pool_database, &client_session_id).await;
                return Err("Manual transaction requires a dedicated Agent session".to_string());
            };
            (
                TxnConnection::Agent {
                    client,
                    client_session_id,
                    database: pool_database.map(str::to_string),
                    cleanup_guard,
                },
                agent_pool_key,
            )
        }
        TxnPoolHandle::ExternalDriver => {
            let client_session_id = format!("manual-txn-{}", uuid::Uuid::new_v4());
            let external_pool_key =
                state.get_or_create_pool_for_session(connection_id, pool_database, Some(&client_session_id)).await?;
            let (config, session) = {
                let pool_handle = state.pool_handle(&external_pool_key).await;
                match pool_handle.as_ref() {
                    Some(PoolKind::ExternalDriver { config, session, .. }) => (config.clone(), session.clone()),
                    _ => {
                        let _ = state.close_client_session_pool(connection_id, pool_database, &client_session_id).await;
                        return Err("External driver connection not found for manual transaction".to_string());
                    }
                }
            };
            let begin_params = serde_json::json!({
                "connection": config.as_ref(),
                "database": database,
                "schema": schema,
            });
            if let Err(error) = session
                .invoke_with_timeout::<serde_json::Value>(
                    "beginManualTransaction",
                    begin_params,
                    query_timeout_duration(Some(config.effective_query_timeout_secs())),
                )
                .await
            {
                let _ = state.close_client_session_pool(connection_id, pool_database, &client_session_id).await;
                return Err(format!("BEGIN manual transaction failed: {error}"));
            }
            let Some(cleanup_guard) =
                state.workload_session_pool_cleanup_guard(connection_id, pool_database, &client_session_id).await
            else {
                let _ = state.close_client_session_pool(connection_id, pool_database, &client_session_id).await;
                return Err("Manual transaction requires a dedicated external driver session".to_string());
            };
            (
                TxnConnection::ExternalDriver {
                    session,
                    config,
                    client_session_id,
                    database: pool_database.map(str::to_string),
                    cleanup_guard,
                },
                external_pool_key,
            )
        }
    };

    let txn_session_id = uuid::Uuid::new_v4().to_string();
    let session = TransactionSession {
        mysql_data_charset,
        sqlserver: None,
        connection: Arc::new(tokio::sync::Mutex::new(txn_conn)),
        pool_key: pool_key.clone(),
        last_activity: std::time::Instant::now(),
        idle_timeout: manual_transaction_idle_timeout(state, connection_id).await,
        busy: false,
        snapshot_rotation_safe: !consistent_snapshot,
        connection_id: connection_id.to_string(),
        database: database.to_string(),
        schema: schema.map(|s| s.to_string()),
    };

    {
        let mut sessions = state.transaction_sessions.write().await;
        sessions.insert(txn_session_id.clone(), session);
    }

    // Schedule idle timeout watcher
    spawn_txn_idle_watcher(state, txn_session_id.clone());

    log::info!("[query][manual_txn:begin] session_id={}", txn_session_id);
    Ok(txn_session_id)
}

pub struct ManualTransactionKeepAlive {
    task: tokio::task::JoinHandle<()>,
    sessions: Arc<tokio::sync::RwLock<std::collections::HashMap<String, TransactionSession>>>,
    txn_session_id: String,
}

impl Drop for ManualTransactionKeepAlive {
    fn drop(&mut self) {
        self.task.abort();
        spawn_txn_idle_watcher_for_sessions(Arc::clone(&self.sessions), self.txn_session_id.clone());
    }
}

/// Keep an existing transaction session alive while a caller prepares work for
/// that session. The caller must retain the returned guard for the full period;
/// dropping it restores the normal five-minute idle rollback behavior.
pub async fn keep_manual_transaction_alive(
    state: &AppState,
    txn_session_id: &str,
) -> Result<ManualTransactionKeepAlive, String> {
    {
        let mut sessions = state.transaction_sessions.write().await;
        let session =
            sessions.get_mut(txn_session_id).ok_or_else(|| MANUAL_TRANSACTION_SESSION_NOT_FOUND_ERROR.to_string())?;
        if !session.busy {
            session.last_activity = std::time::Instant::now();
        }
    }

    let sessions = Arc::clone(&state.transaction_sessions);
    let keep_alive_sessions = Arc::clone(&sessions);
    let txn_session_id = txn_session_id.to_string();
    let keep_alive_txn_session_id = txn_session_id.clone();
    let task = tokio::spawn(async move {
        const KEEPALIVE_INTERVAL: Duration = Duration::from_secs(30);
        loop {
            tokio::time::sleep(KEEPALIVE_INTERVAL).await;
            let should_continue = {
                let mut guard = sessions.write().await;
                if let Some(session) = guard.get_mut(&keep_alive_txn_session_id) {
                    if !session.busy {
                        session.last_activity = std::time::Instant::now();
                    }
                    true
                } else {
                    false
                }
            };
            if !should_continue {
                break;
            }
        }
    });
    Ok(ManualTransactionKeepAlive { task, sessions: keep_alive_sessions, txn_session_id })
}

/// Execute SQL within an existing manual transaction session.
pub async fn execute_in_manual_transaction(
    state: &AppState,
    txn_session_id: &str,
    sql: &str,
    database: &str,
    schema: Option<&str>,
    max_rows: Option<usize>,
) -> Result<Vec<db::QueryResult>, String> {
    execute_in_manual_transaction_with_options(
        state,
        txn_session_id,
        sql,
        database,
        schema,
        ManualTransactionExecutionOptions { max_rows, ..Default::default() },
    )
    .await
    .map(|results| results.into_iter().map(ExecuteMultiResult::into_query_result).collect())
}

#[derive(Clone, Debug, Default)]
pub struct ManualTransactionExecutionOptions {
    pub defer_lobs: bool,
    pub bound_statements: Option<Vec<dbx_types::types::BlobBoundStatement>>,
    pub max_rows: Option<usize>,
    pub table_data_preview: bool,
    pub execution_id: Option<String>,
    pub timeout_secs: Option<u64>,
    pub page_size: Option<usize>,
    pub result_session_id: Option<String>,
    /// User-facing SQL to classify (Oracle-only). When present, the core
    /// classifies each split statement of this SQL rather than the rewritten
    /// execution SQL, pairing them by count and position so DBX-owned
    /// read-preserving rewrites (hidden primary keys, sort wrappers,
    /// pagination) do not change toolbar semantics. Fail-closed on mismatch.
    pub classification_sql: Option<String>,
}

pub async fn execute_in_manual_transaction_with_options(
    state: &AppState,
    txn_session_id: &str,
    sql: &str,
    database: &str,
    schema: Option<&str>,
    options: ManualTransactionExecutionOptions,
) -> Result<Vec<ExecuteMultiResult>, String> {
    if sqlserver_manual_transaction::is_session_id(txn_session_id) {
        if options.bound_statements.is_some() {
            return Err("Bound BLOB saves are only supported for OceanBase Oracle.".into());
        }
        return sqlserver_manual_transaction::execute(state, txn_session_id, sql, database, schema, options).await;
    }
    // Resolve statements and validate before taking the per-session connection
    // lock. The session stays visible in the map so close/disconnect cleanup can
    // remove it and roll back once the current DB operation releases the lock.
    let (pool_key, connection_id, mysql_data_charset) = {
        let sessions = state.transaction_sessions.read().await;
        let session = sessions.get(txn_session_id).ok_or(MANUAL_TRANSACTION_SESSION_NOT_FOUND_ERROR)?;
        (session.pool_key.clone(), session.connection_id.clone(), session.mysql_data_charset)
    };

    let db_type = connection_database_type(state, &connection_id).await;
    let compatibility_mode = connection_sql_compatibility_mode(state, &pool_key, db_type).await;
    // Split with the connection's statement-splitting dialect, not its routing
    // `db_type`: a JDBC SQL Server connection keeps control-flow batches whole
    // (#13514) while still executing through the generic external-driver path.
    let split_db_type = connection_statement_split_database_type(state, &connection_id).await;
    let statements = if let Some(bound) = options.bound_statements.as_ref() {
        if db_type != Some(DatabaseType::OceanbaseOracle) {
            return Err("Bound BLOB saves are only supported for OceanBase Oracle.".into());
        }
        let previews = bound.iter().map(|statement| statement.preview_sql.clone()).collect::<Vec<_>>();
        if sql != previews.join(";\n") {
            return Err("The reviewed SQL changed; regenerate the BLOB save preview before saving.".into());
        }
        dbx_types::types::validate_blob_bound_statements(&previews, bound)?;
        previews
    } else {
        split_db_type.map_or_else(
            || split_sql_statements(sql),
            |split_db_type| {
                crate::sql::split_sql_statements_for_database_with_compatibility(
                    sql,
                    split_db_type,
                    compatibility_mode.as_deref(),
                )
            },
        )
    };
    if statements.is_empty() {
        // Sticky-dialect UX marker: the no-op is Core's decision that the script
        // (empty/whitespace/comments-only) has no statements, so the frontend
        // must not treat it as an unproven statement. Every other database
        // receives the plain empty result.
        let mut result = ExecuteMultiResult::success_with_optional_server_large_values(
            empty_query_result(0),
            options.table_data_preview,
        );
        if matches!(
            db_type,
            Some(
                DatabaseType::Oracle
                    | DatabaseType::OceanbaseOracle
                    | DatabaseType::Mysql
                    | DatabaseType::Postgres
                    | DatabaseType::Gaussdb
                    | DatabaseType::OpenGauss
            )
        ) {
            result = result.with_manual_transaction_no_statement();
        }
        return Ok(vec![result]);
    }
    // A result-session cursor can only track one result set, so pagination is
    // single-statement only. Multi-statement scripts predate pagination: keep
    // the legacy sequential execution and ignore the pagination options rather
    // than failing the whole script.
    let options = if statements.len() != 1 && (options.page_size.is_some() || options.result_session_id.is_some()) {
        ManualTransactionExecutionOptions { page_size: None, result_session_id: None, ..options }
    } else {
        options
    };
    if options.result_session_id.is_some() && options.page_size.is_none() {
        return Err("Manual transaction result pagination requires a page size".to_string());
    }

    // Read-only check while the session is still in the map. If this fails the
    // session remains intact.
    check_read_only_for_connection_multi(state, &pool_key, &statements).await?;
    if let Some(bound) = options.bound_statements.as_ref() {
        check_read_only_for_connection_multi(
            state,
            &pool_key,
            &bound.iter().map(|statement| statement.sql.clone()).collect::<Vec<_>>(),
        )
        .await?;
        let connection = state
            .transaction_sessions
            .read()
            .await
            .get(txn_session_id)
            .map(|session| Arc::clone(&session.connection))
            .ok_or(MANUAL_TRANSACTION_SESSION_NOT_FOUND_ERROR)?;
        let connection = connection.lock().await;
        if !matches!(&*connection, TxnConnection::Agent { client, .. } if client.lock().await.supports_capability(crate::db::agent_driver::AgentCapability::BlobBindStatementsV1))
        {
            return Err("This Agent does not support bound BLOB saves; update the Agent before saving.".into());
        }
    }

    let classification: Vec<bool> =
        classify_manual_transaction_statements(db_type, statements.len(), options.classification_sql.as_deref());

    let connection = {
        let mut sessions = state.transaction_sessions.write().await;
        let Some(session) = sessions.get_mut(txn_session_id) else {
            return Err(MANUAL_TRANSACTION_SESSION_NOT_FOUND_ERROR.to_string());
        };
        if session.busy {
            return Err("Transaction session is already executing".to_string());
        }
        if session.last_activity.elapsed() > session.idle_timeout {
            let session = sessions.remove(txn_session_id).expect("session exists");
            Some(session)
        } else {
            session.busy = true;
            session.snapshot_rotation_safe &=
                classification.len() == statements.len() && classification.iter().all(|proven| *proven);
            session.last_activity = std::time::Instant::now();
            None
        }
    };
    if let Some(session) = connection {
        let mut conn = session.connection.lock().await;
        let _ = rollback_manual_txn_connection(&mut conn).await;
        release_manual_txn_session_pool(state, &session.connection_id, &mut conn).await;
        return Err(MANUAL_TRANSACTION_IDLE_ROLLBACK_ERROR.to_string());
    }

    let connection = {
        let sessions = state.transaction_sessions.read().await;
        sessions
            .get(txn_session_id)
            .map(|session| Arc::clone(&session.connection))
            .ok_or(MANUAL_TRANSACTION_SESSION_NOT_FOUND_ERROR)?
    };
    let row_limit = options.max_rows.unwrap_or(MAX_ROWS).max(1);
    let mut results = Vec::with_capacity(statements.len());

    let mut conn = connection.lock().await;
    let mut bound_failure_can_keep = false;
    for (i, statement) in statements.iter().enumerate() {
        if options.bound_statements.is_some() && i > 0 {
            break;
        }
        let result = match &mut *conn {
            TxnConnection::SqlServer { .. } => {
                return Err("SQL Server transaction must use its batch executor".to_string())
            }
            TxnConnection::Postgres(conn) => {
                execute_manual_txn_postgres_statement(conn.as_ref(), statement, row_limit).await
            }
            TxnConnection::Mysql(conn) => {
                execute_manual_txn_mysql_statement(
                    conn.as_mut().ok_or_else(|| MANUAL_TRANSACTION_SESSION_NOT_FOUND_ERROR.to_string())?,
                    statement,
                    row_limit,
                    mysql_data_charset,
                )
                .await
            }
            TxnConnection::Agent { client, .. } => {
                if let Some(bound) = options.bound_statements.as_ref() {
                    match client
                        .lock()
                        .await
                        .execute_blob_bound_typed(
                            if database.is_empty() { None } else { Some(database) },
                            &statements,
                            bound,
                            schema,
                            false,
                            resolve_query_timeout(options.timeout_secs),
                        )
                        .await
                    {
                        Ok(result) => Ok(result),
                        Err(error) => {
                            bound_failure_can_keep = error.session_disposition()
                                == Some(crate::db::agent_driver::AgentSessionDisposition::Keep);
                            Err(error.into_legacy_string())
                        }
                    }
                } else {
                    execute_manual_txn_agent_statement(
                        client,
                        db_type,
                        statement,
                        database,
                        schema,
                        row_limit,
                        options.table_data_preview,
                        options.page_size,
                        options.result_session_id.as_deref(),
                    )
                    .await
                }
            }
            TxnConnection::ExternalDriver { session, config, .. } => {
                execute_manual_txn_external_driver_statement(
                    session,
                    config,
                    statement,
                    database,
                    schema,
                    row_limit,
                    options.defer_lobs,
                )
                .await
            }
        };
        match result {
            Ok(query_result) => {
                let mut executed = ExecuteMultiResult::success_with_optional_server_large_values_and_charset(
                    query_result,
                    options.table_data_preview,
                    mysql_data_charset,
                );
                if classification.get(i).copied().unwrap_or(false) {
                    executed = executed.with_manual_transaction_proven_read_only();
                }
                results.push(executed);
            }
            Err(e) => {
                if bound_failure_can_keep {
                    if let Some(session) = state.transaction_sessions.write().await.get_mut(txn_session_id) {
                        session.busy = false;
                        session.last_activity = std::time::Instant::now();
                    }
                    return Err(e);
                }
                // Statement failure ends the transaction. If another cleanup path
                // already removed the session, it owns the final rollback.
                let should_rollback = {
                    let mut sessions = state.transaction_sessions.write().await;
                    sessions.remove(txn_session_id).is_some()
                };
                if should_rollback {
                    let _ = rollback_manual_txn_connection(&mut conn).await;
                    release_manual_txn_session_pool(state, &connection_id, &mut conn).await;
                }
                return Err(if options.bound_statements.is_some() {
                    format!("Bound BLOB save failed: {e}. The transaction session was discarded; the operation outcome may be unknown.")
                } else {
                    format!("Statement {} failed: {}. The manual transaction was rolled back.", i + 1, e)
                });
            }
        }
    }

    // Snapshot rotation for fully proven read-only batches (native MySQL/PG
    // connections only): see rotate_clean_read_only_snapshot. Runs while the
    // session is still marked busy so no concurrent execution can observe the
    // half-rotated transaction. A rotation failure tears the session down and
    // leans on the frontend rolled-back-session recovery (next execution opens
    // a fresh session) instead of failing the already-successful batch.
    if manual_txn_batch_fully_proven_read_only(&results) && {
        let sessions = state.transaction_sessions.read().await;
        sessions.get(txn_session_id).map(|session| session.can_rotate_read_only_snapshot(&conn)).unwrap_or(false)
    } {
        if let Err(rotation_error) = rotate_clean_read_only_snapshot(&mut conn, schema).await {
            let removed = {
                let mut sessions = state.transaction_sessions.write().await;
                sessions.remove(txn_session_id).is_some()
            };
            if removed {
                let _ = rollback_manual_txn_connection(&mut conn).await;
                release_manual_txn_session_pool(state, &connection_id, &mut conn).await;
            }
            let _ = rotation_error;
        }
    }
    drop(conn);

    let should_watch = {
        let mut sessions = state.transaction_sessions.write().await;
        if let Some(session) = sessions.get_mut(txn_session_id) {
            session.busy = false;
            session.last_activity = std::time::Instant::now();
            true
        } else {
            false
        }
    };
    if should_watch {
        spawn_txn_idle_watcher(state, txn_session_id.to_string());
    }

    Ok(results)
}

/// Stream a read query through an existing transaction without materializing
/// the whole result set. The callback runs once per batch while the same held
/// connection and transaction snapshot remain active.
pub async fn stream_rows_in_manual_transaction<F>(
    state: &AppState,
    txn_session_id: &str,
    sql: &str,
    batch_size: usize,
    on_batch: F,
) -> Result<u64, String>
where
    F: FnMut(Vec<Vec<serde_json::Value>>) -> Result<(), String> + Send,
{
    stream_rows_in_manual_transaction_with_cancel(state, txn_session_id, sql, batch_size, None, on_batch).await
}

pub(crate) async fn stream_rows_in_manual_transaction_with_cancel<F>(
    state: &AppState,
    txn_session_id: &str,
    sql: &str,
    batch_size: usize,
    cancel_token: Option<CancellationToken>,
    mut on_batch: F,
) -> Result<u64, String>
where
    F: FnMut(Vec<Vec<serde_json::Value>>) -> Result<(), String> + Send,
{
    let pool_key = {
        let sessions = state.transaction_sessions.read().await;
        sessions.get(txn_session_id).ok_or(MANUAL_TRANSACTION_SESSION_NOT_FOUND_ERROR)?.pool_key.clone()
    };
    check_read_only_for_connection(state, &pool_key, sql).await?;
    let expired_connection = {
        let mut sessions = state.transaction_sessions.write().await;
        let Some(session) = sessions.get_mut(txn_session_id) else {
            return Err(MANUAL_TRANSACTION_SESSION_NOT_FOUND_ERROR.to_string());
        };
        if session.busy {
            return Err("Transaction session is already executing".to_string());
        }
        if session.last_activity.elapsed() > session.idle_timeout {
            Some(sessions.remove(txn_session_id).expect("session exists").connection)
        } else {
            session.busy = true;
            session.snapshot_rotation_safe = false;
            session.last_activity = std::time::Instant::now();
            None
        }
    };
    if let Some(connection) = expired_connection {
        let mut conn = connection.lock().await;
        let _ = rollback_manual_txn_connection(&mut conn).await;
        return Err(MANUAL_TRANSACTION_IDLE_ROLLBACK_ERROR.to_string());
    }

    let (connection, pool_key, connection_id, mysql_data_charset) = {
        let sessions = state.transaction_sessions.read().await;
        sessions
            .get(txn_session_id)
            .map(|session| {
                (
                    Arc::clone(&session.connection),
                    session.pool_key.clone(),
                    session.connection_id.clone(),
                    session.mysql_data_charset,
                )
            })
            .ok_or(MANUAL_TRANSACTION_SESSION_NOT_FOUND_ERROR)?
    };
    let operation_budget = {
        let configs = state.configs.read().await;
        configs
            .get(&connection_id)
            .map(DbOperationBudget::from_connection_config)
            .unwrap_or_else(DbOperationBudget::with_defaults)
    };
    let postgres_cancel_context = state.get_postgres_cancel_context(&pool_key).await;
    let batch_size = batch_size.max(1);
    let mut conn = connection.lock().await;
    let stream_result = match &mut *conn {
        TxnConnection::Postgres(conn) => {
            let mut batch = Vec::with_capacity(batch_size);
            let mut total_rows = 0_u64;
            let result = db::postgres::stream_select_query_inner_unnamed_with_cancel(
                conn,
                sql,
                None,
                &mut |item| {
                    if let db::postgres::PostgresQueryStreamItem::Row(row) = item {
                        batch.push(row);
                        total_rows += 1;
                        if batch.len() >= batch_size {
                            on_batch(std::mem::take(&mut batch))?;
                            batch = Vec::with_capacity(batch_size);
                        }
                    }
                    Ok(())
                },
                cancel_token.as_ref(),
                &operation_budget,
                postgres_cancel_context.as_ref(),
            )
            .await;
            match result {
                Ok(_) if !batch.is_empty() => on_batch(batch).map(|_| total_rows),
                Ok(_) => Ok(total_rows),
                Err(error) => Err(error),
            }
        }
        TxnConnection::Mysql(Some(conn)) => {
            // The query timeout is an inactivity budget reset by every received row,
            // not a cap on the total duration of a long backup/export stream.
            let progress_clock = Arc::new(StreamProgressClock::new());
            let progress_clock_for_rows = progress_clock.clone();
            let timeout_error = format!(
                "Query timed out after {} seconds",
                operation_budget.query_timeout.map_or(0, |timeout| timeout.as_secs())
            );
            let stream_future = async {
                let mut result = conn.query_iter(sql).await.map_err(|error| format!("Query failed: {error}"))?;
                let Some(mut stream) =
                    result.stream::<mysql_async::Row>().await.map_err(|error| format!("Query failed: {error}"))?
                else {
                    return Err("Empty result set stream".to_string());
                };

                let mut batch = Vec::with_capacity(batch_size);
                let mut total_rows = 0_u64;
                while let Some(row_result) = stream.next().await {
                    match row_result {
                        Ok(row) => {
                            batch.push(
                                (0..row.len())
                                    .map(|index| {
                                        db::mysql::mysql_value_to_json_with_data_charset(
                                            &row,
                                            index,
                                            mysql_data_charset,
                                        )
                                    })
                                    .collect(),
                            );
                            total_rows += 1;
                            if batch.len() >= batch_size {
                                on_batch(std::mem::take(&mut batch))?;
                                batch = Vec::with_capacity(batch_size);
                            }
                        }
                        Err(err) => return Err(format!("Query failed: {err}")),
                    }
                    progress_clock_for_rows.mark();
                }
                if !batch.is_empty() {
                    on_batch(batch)?;
                }
                Ok(total_rows)
            };
            await_stream_with_progress_timeout(
                stream_future,
                operation_budget.query_timeout,
                progress_clock,
                cancel_token.as_ref(),
                timeout_error,
            )
            .await
        }
        TxnConnection::Mysql(None) => Err(MANUAL_TRANSACTION_SESSION_NOT_FOUND_ERROR.to_string()),
        TxnConnection::SqlServer { .. } => Err("Streaming SQL Server manual transactions is not supported".to_owned()),
        TxnConnection::Agent { .. } => {
            Err("Streaming rows inside an agent manual transaction is not supported".to_string())
        }
        TxnConnection::ExternalDriver { .. } => {
            Err("Streaming rows inside an external driver manual transaction is not supported".to_string())
        }
    };

    if let Err(err) = &stream_result {
        let removed = {
            let mut sessions = state.transaction_sessions.write().await;
            sessions.remove(txn_session_id)
        };
        if let Some(session) = removed {
            let rollback_result = if err == QUERY_CANCELED {
                discard_mysql_manual_txn_connection(&mut conn, operation_budget.cleanup_timeout).await
            } else {
                rollback_manual_txn_connection_with_postgres_timeout(&mut conn, Some(operation_budget.cleanup_timeout))
                    .await
            };
            release_manual_txn_session_pool(state, &session.connection_id, &mut conn).await;
            if let Err(rollback_error) = rollback_result {
                return Err(format!("{err}. Transaction cleanup failed: {rollback_error}"));
            }
        }
        return Err(format!("{err}. Transaction was auto-rolled back."));
    }
    drop(conn);

    let should_watch = {
        let mut sessions = state.transaction_sessions.write().await;
        if let Some(session) = sessions.get_mut(txn_session_id) {
            session.busy = false;
            session.last_activity = std::time::Instant::now();
            true
        } else {
            false
        }
    };
    if should_watch {
        spawn_txn_idle_watcher(state, txn_session_id.to_string());
    }
    stream_result
}

/// Whether a finished batch was fully proven read-only. Session history is
/// checked separately before rotating its snapshot.
fn manual_txn_batch_fully_proven_read_only(results: &[ExecuteMultiResult]) -> bool {
    !results.is_empty() && results.iter().all(|result| result.manual_transaction_proven_read_only)
}

/// Rotate the transaction on a natively-connected MySQL/PostgreSQL session after
/// a fully proven read-only batch. MySQL REPEATABLE READ (and PostgreSQL when
/// the server default was changed to a snapshot isolation) pins the read view of
/// the first SELECT for the whole transaction: a user who keeps polling a clean
/// read-only session would otherwise never see rows committed by others, and the
/// clean-state toolbar hides Commit/Rollback, leaving disconnect/reconnect as
/// the only visible way out. A rollback of a read-only transaction and a fresh
/// BEGIN are both cheap metadata operations. The session-history gate excludes
/// any prior write or uncertain batch. Failures never fail the successful batch:
/// the session is torn down instead and the frontend's existing
/// rolled-back-session recovery transparently opens a new one on the next run.
async fn rotate_clean_read_only_snapshot(conn: &mut TxnConnection, schema: Option<&str>) -> Result<(), String> {
    match conn {
        TxnConnection::Mysql(Some(conn)) => {
            conn.query_drop("ROLLBACK").await.map_err(|e| format!("ROLLBACK failed: {e}"))?;
            conn.query_drop("START TRANSACTION").await.map_err(|e| format!("START TRANSACTION failed: {e}"))?;
            Ok(())
        }
        TxnConnection::Postgres(conn) => {
            conn.execute_typed("ROLLBACK", &[]).await.map_err(|e| format!("ROLLBACK failed: {e}"))?;
            conn.execute_typed("BEGIN", &[]).await.map_err(|e| format!("BEGIN failed: {e}"))?;
            if let Some(schema) = schema {
                db::postgres::set_postgres_search_path(
                    conn,
                    schema,
                    db::postgres::PostgresSearchPathContext::LocalTransaction,
                    db::connection_timeout(),
                )
                .await
                .map_err(|e| format!("SET search_path failed: {e}"))?;
            }
            Ok(())
        }
        // Agent and external-driver sessions cannot reopen in place (their
        // rollback path closes the dedicated session), so they keep the
        // snapshot semantics; proven-read-only markers for those dialects do
        // not reach this helper's native variants in practice.
        _ => Ok(()),
    }
}

pub(crate) async fn rollback_manual_txn_connection(conn: &mut TxnConnection) -> Result<(), String> {
    rollback_manual_txn_connection_with_postgres_timeout(conn, None).await
}

async fn rollback_manual_txn_connection_with_postgres_timeout(
    conn: &mut TxnConnection,
    postgres_timeout: Option<Duration>,
) -> Result<(), String> {
    match conn {
        TxnConnection::SqlServer { client, client_session_id: _client_session_id, .. } => {
            if let Some(client) = client {
                let mut client = client.lock().await;
                let count = db::sqlserver::manual_transaction_status(&mut client).await?.count;
                if count == 0 {
                    return Err("The transaction has already ended; rollback cannot be confirmed".to_string());
                }
                #[cfg(feature = "test-support")]
                sqlserver_manual_transaction::live_test_hooks::run(
                    _client_session_id,
                    sqlserver_manual_transaction::live_test_hooks::Phase::BeforeRollback,
                );
                db::sqlserver::execute_simple_batch_with_max_rows_metadata(
                    &mut client,
                    "ROLLBACK TRANSACTION",
                    Some(1),
                )
                .await?;
            } else {
                return Err("The transaction connection was discarded; rollback cannot be confirmed".to_string());
            }
        }
        TxnConnection::Postgres(conn) => {
            if let Some(timeout) = postgres_timeout {
                db::postgres::execute_postgres_infra_statement(conn, "ROLLBACK", timeout, "manual_txn.rollback")
                    .await
                    .map_err(|error| format!("ROLLBACK failed: {error}"))?;
            } else {
                conn.execute_typed("ROLLBACK", &[]).await.map_err(|e| format!("ROLLBACK failed: {e}"))?;
            }
        }
        TxnConnection::Mysql(Some(conn)) => {
            conn.query_drop("ROLLBACK").await.map_err(|e| format!("ROLLBACK failed: {e}"))?;
        }
        TxnConnection::Mysql(None) => return Ok(()),
        TxnConnection::Agent { client, .. } => {
            let mut locked = client.lock().await;
            match locked.rollback_manual_transaction::<serde_json::Value>().await {
                Ok(_) => {}
                Err(error) if error.to_ascii_lowercase().contains("no manual transaction") => {}
                Err(error) => return Err(format!("ROLLBACK failed: {error}")),
            }
            // Close the dedicated agent session so Oracle resources are released even if
            // the pool map entry is cleaned up later.
            let _ = locked.disconnect().await;
        }
        TxnConnection::ExternalDriver { session, config, .. } => {
            match session
                .invoke_with_timeout::<serde_json::Value>(
                    "rollbackManualTransaction",
                    serde_json::json!({ "connection": config.as_ref() }),
                    query_timeout_duration(Some(config.effective_query_timeout_secs())),
                )
                .await
            {
                Ok(_) => {}
                Err(error) if error.to_ascii_lowercase().contains("no manual transaction") => {}
                Err(error) => return Err(format!("ROLLBACK failed: {error}")),
            }
        }
    }
    Ok(())
}

/// A cancelled MySQL row stream may still have unread result packets.  Do not
/// send ROLLBACK on that connection: mysql_async only consumes a dropped stream
/// when the next result set is requested.  Discarding the dedicated connection
/// makes MySQL roll the transaction back and prevents protocol desynchronization.
async fn discard_mysql_manual_txn_connection(conn: &mut TxnConnection, timeout: Duration) -> Result<(), String> {
    let TxnConnection::Mysql(conn) = conn else {
        return rollback_manual_txn_connection_with_postgres_timeout(conn, Some(timeout)).await;
    };
    let Some(conn) = conn.take() else {
        return Ok(());
    };
    match tokio::time::timeout(timeout, conn.disconnect()).await {
        Ok(Ok(())) => Ok(()),
        Ok(Err(error)) => Err(format!("Failed to discard cancelled MySQL connection: {error}")),
        Err(_) => Err(format!("Discarding cancelled MySQL connection timed out after {} seconds", timeout.as_secs())),
    }
}

async fn release_manual_txn_session_pool(state: &AppState, connection_id: &str, conn: &mut TxnConnection) {
    let (client_session_id, database, cleanup_guard) = match conn {
        TxnConnection::SqlServer { client_session_id, database, cleanup_guard, .. }
        | TxnConnection::Agent { client_session_id, database, cleanup_guard, .. }
        | TxnConnection::ExternalDriver { client_session_id, database, cleanup_guard, .. } => {
            (client_session_id, database, cleanup_guard)
        }
        _ => return,
    };
    if state.detach_client_session_pool(connection_id, database.as_deref(), client_session_id).await.unwrap_or(false) {
        cleanup_guard.disarm();
    }
}

fn manual_txn_agent_query_options(
    row_limit: usize,
    table_data_preview: bool,
    page_size: Option<usize>,
    result_session_id: Option<&str>,
) -> QueryExecutionOptions {
    QueryExecutionOptions {
        max_rows: Some(row_limit.max(1)),
        table_data_preview,
        page_size,
        result_session_id: result_session_id.map(str::to_owned),
        ..QueryExecutionOptions::default()
    }
}

#[derive(Debug)]
enum ManualTxnAgentQueryRequest {
    Execute(serde_json::Value),
    ExecutePage(serde_json::Value),
    FetchPage(serde_json::Value),
}

fn manual_txn_agent_query_request(
    sql: &str,
    database: Option<&str>,
    schema: Option<&str>,
    options: QueryExecutionOptions,
) -> ManualTxnAgentQueryRequest {
    if let Some(session_id) = options.result_session_id.as_deref() {
        return ManualTxnAgentQueryRequest::FetchPage(agent_fetch_query_page_params(
            session_id,
            options.page_size.unwrap_or(MAX_ROWS),
        ));
    }
    if options.page_size.is_some() {
        return ManualTxnAgentQueryRequest::ExecutePage(agent_execute_query_page_params(
            sql, database, schema, options,
        ));
    }
    ManualTxnAgentQueryRequest::Execute(agent_execute_query_params(sql, database, schema, options))
}

async fn execute_manual_txn_agent_statement(
    client: &Arc<crate::db::agent_driver::PooledAgentClient>,
    db_type: Option<DatabaseType>,
    statement: &str,
    database: &str,
    schema: Option<&str>,
    row_limit: usize,
    table_data_preview: bool,
    page_size: Option<usize>,
    result_session_id: Option<&str>,
) -> Result<db::QueryResult, String> {
    let sql = sql_for_execution_context_with_identifier_quote(db_type, statement, schema, client.identifier_quote());
    let execution_schema = schema_for_execution_context(db_type, schema);
    let options = manual_txn_agent_query_options(row_limit, table_data_preview, page_size, result_session_id);
    let request = manual_txn_agent_query_request(
        &sql,
        Some(database).filter(|value| !value.is_empty()),
        execution_schema,
        options,
    );
    let lock_started = std::time::Instant::now();
    let mut locked = client.lock().await;
    let lock_ms = lock_started.elapsed().as_secs_f64() * 1000.0;
    let result = match request {
        ManualTxnAgentQueryRequest::Execute(params) => {
            locked.execute_query_typed_with_timeout::<db::QueryResult>(params, None).await
        }
        ManualTxnAgentQueryRequest::ExecutePage(params) => {
            locked.execute_query_page_typed_with_timeout_and_cancel::<db::QueryResult>(params, None, None).await
        }
        ManualTxnAgentQueryRequest::FetchPage(params) => {
            locked.fetch_query_page_typed_with_timeout_and_cancel::<db::QueryResult>(params, None, None).await
        }
    };
    result
        .map(|mut result| {
            if let Some(timings) = result.query_timings_ms.as_mut() {
                timings.insert("core_lock".into(), lock_ms);
            }
            truncate_result_with_max_rows(result, Some(row_limit.max(1)))
        })
        .map_err(|error| error.into_legacy_string())
}

async fn execute_manual_txn_external_driver_statement(
    session: &Arc<crate::plugins::PluginDriverSession>,
    config: &Arc<crate::models::connection::ConnectionConfig>,
    statement: &str,
    database: &str,
    schema: Option<&str>,
    row_limit: usize,
    defer_lobs: bool,
) -> Result<db::QueryResult, String> {
    let timeout_secs = config.effective_query_timeout_secs();
    let options = QueryExecutionOptions {
        defer_lobs,
        max_rows: Some(row_limit.max(1)),
        timeout_secs: Some(timeout_secs),
        ..QueryExecutionOptions::default()
    };
    let params = external_driver_query_params(config.as_ref(), statement, database, schema, &options);
    session
        .invoke_with_timeout::<db::QueryResult>(
            "executeInManualTransaction",
            params,
            query_timeout_duration(Some(timeout_secs)),
        )
        .await
        .map(|result| truncate_result_with_max_rows(result, Some(row_limit.max(1))))
}

/// Spawn a background task that removes and rolls back a transaction session
/// after its idle timeout. The task does not hold the global lock across
/// I/O: it briefly checks the map, and if the session exists and is expired,
/// removes it, drops the lock, then rolls back the held connection.
///
/// Safety: if multiple watchers exist for the same session ID (e.g. due to
/// a race), only the one that actually finds the session in the map and
/// observes an elapsed time >= timeout will remove and roll back. Others
/// will see a missing session or a non-expired one and exit harmlessly.
fn spawn_txn_idle_watcher(state: &AppState, txn_session_id: String) {
    let sessions = Arc::clone(&state.transaction_sessions);
    spawn_txn_idle_watcher_for_sessions(sessions, txn_session_id);
}

fn spawn_txn_idle_watcher_for_sessions(
    sessions: Arc<tokio::sync::RwLock<std::collections::HashMap<String, TransactionSession>>>,
    txn_session_id: String,
) {
    tokio::spawn(async move {
        // The window is fixed per session at BEGIN; a vanished session needs no watcher.
        let Some(idle_timeout) = sessions.read().await.get(&txn_session_id).map(|session| session.idle_timeout) else {
            return;
        };
        tokio::time::sleep(idle_timeout).await;

        let removed: Option<TransactionSession> = {
            let mut guard = sessions.write().await;
            match guard.get(&txn_session_id) {
                Some(session) if !session.busy && session.last_activity.elapsed() >= session.idle_timeout => {
                    guard.remove(&txn_session_id)
                }
                _ => None,
            }
        };

        if let Some(session) = removed {
            let mut conn = session.connection.lock().await;
            let _ = rollback_manual_txn_connection(&mut conn).await;
            log::info!(
                "[query][manual_txn:idle_timeout] session_id={} auto-rolled back after {}s of inactivity",
                txn_session_id,
                session.idle_timeout.as_secs()
            );
        }
    });
}

async fn execute_manual_txn_postgres_statement(
    conn: &deadpool_postgres::Object,
    sql: &str,
    row_limit: usize,
) -> Result<db::QueryResult, String> {
    db::postgres::execute_query_on_client_unnamed(conn, sql, row_limit).await
}

async fn execute_manual_txn_mysql_statement(
    conn: &mut mysql_async::Conn,
    sql: &str,
    row_limit: usize,
    data_charset: Option<db::mysql::MySqlDataCharset>,
) -> Result<db::QueryResult, String> {
    if data_charset.is_some() {
        return db::mysql::execute_query_on_conn_with_limits_with_data_charset(
            conn,
            sql,
            false,
            Some(row_limit),
            None,
            &[],
            db::mysql::MySqlQueryDialect::default(),
            None,
            data_charset,
        )
        .await
        .map(|result| result.result);
    }
    if db::mysql::is_result_set_query(sql, db::mysql::MySqlQueryDialect::default()) {
        let start = std::time::Instant::now();
        let mut result = conn.query_iter(sql).await.map_err(|e| format!("Query failed: {e}"))?;
        let columns: Vec<String> = result.columns_ref().iter().map(|c| c.name_str().to_string()).collect();
        let column_types: Vec<String> = result.columns_ref().iter().map(db::mysql::mysql_column_type_name).collect();
        // Some statements only *may* return rows — `EXECUTE` of a prepared DML statement
        // finishes with a plain OK packet instead — so report the affected rows rather than
        // failing on the missing result set.
        if columns.is_empty() {
            let affected_rows = result.affected_rows();
            result.drop_result().await.map_err(|e| format!("Query failed: {e}"))?;
            return Ok(db::QueryResult {
                columns: Vec::new(),
                column_types: Vec::new(),
                deferred_large_value_cells: Vec::new(),
                column_sortables: vec![],
                spatial_columns: vec![],
                raw_key_cells: Vec::new(),
                spatial_values: vec![],
                rows: vec![],
                affected_rows,
                execution_time_ms: start.elapsed().as_millis(),
                server_execute_time_us: None,
                query_timings_ms: None,
                truncated: false,
                session_id: None,
                has_more: false,
                elasticsearch_raw_body: None,
                messages: Vec::new(),
            });
        }
        let mut data: Vec<Vec<serde_json::Value>> = Vec::with_capacity(row_limit.min(1024));
        let mut stream = result
            .stream::<mysql_async::Row>()
            .await
            .map_err(|e| format!("Query failed: {e}"))?
            .ok_or_else(|| "Empty result set stream".to_string())?;
        let mut truncated = false;
        while let Some(row) = stream.next().await {
            if data.len() >= row_limit {
                truncated = true;
                break;
            }
            let row = row.map_err(|e| format!("Query failed: {e}"))?;
            data.push(
                (0..row.len())
                    .map(|i| db::mysql::mysql_value_to_json_with_data_charset(&row, i, data_charset))
                    .collect(),
            );
        }
        Ok(db::QueryResult {
            columns,
            column_types,
            deferred_large_value_cells: Vec::new(),
            column_sortables: vec![],
            spatial_columns: vec![],
            raw_key_cells: Vec::new(),
            spatial_values: vec![],
            rows: data,
            affected_rows: 0,
            execution_time_ms: start.elapsed().as_millis(),
            server_execute_time_us: None,
            query_timings_ms: None,
            truncated,
            session_id: None,
            has_more: false,
            elasticsearch_raw_body: None,
            messages: Vec::new(),
        })
    } else {
        let result = conn.query_iter(sql).await.map_err(|e| format!("Query failed: {e}"))?;
        let affected_rows = result.affected_rows();
        result.drop_result().await.map_err(|e| format!("Query failed: {e}"))?;
        Ok(db::QueryResult {
            columns: vec![],
            column_types: Vec::new(),
            deferred_large_value_cells: Vec::new(),
            column_sortables: vec![],
            spatial_columns: vec![],
            raw_key_cells: Vec::new(),
            spatial_values: vec![],
            rows: vec![],
            affected_rows,
            execution_time_ms: 0,
            server_execute_time_us: None,
            query_timings_ms: None,
            truncated: false,
            session_id: None,
            has_more: false,
            elasticsearch_raw_body: None,
            messages: Vec::new(),
        })
    }
}

/// Commit an existing manual transaction session.
pub async fn commit_manual_transaction(state: &AppState, txn_session_id: &str) -> Result<db::QueryResult, String> {
    if sqlserver_manual_transaction::is_session_id(txn_session_id) {
        return sqlserver_manual_transaction::finish(state, txn_session_id, true).await;
    }
    let session = {
        let mut sessions = state.transaction_sessions.write().await;
        sessions.remove(txn_session_id).ok_or("Transaction session not found")?
    };

    let mut conn = session.connection.lock().await;
    match &mut *conn {
        TxnConnection::SqlServer { .. } => return Err("SQL Server transaction must use its commit handler".to_string()),
        TxnConnection::Postgres(conn) => {
            conn.execute_typed("COMMIT", &[]).await.map_err(|e| format!("COMMIT failed: {e}"))?;
        }
        TxnConnection::Mysql(Some(conn)) => {
            conn.query_drop("COMMIT").await.map_err(|e| format!("COMMIT failed: {e}"))?;
        }
        TxnConnection::Mysql(None) => return Err(MANUAL_TRANSACTION_SESSION_NOT_FOUND_ERROR.to_string()),
        TxnConnection::Agent { client, .. } => {
            let mut locked = client.lock().await;
            locked
                .commit_manual_transaction::<serde_json::Value>()
                .await
                .map_err(|error| format!("COMMIT failed: {error}"))?;
            let _ = locked.disconnect().await;
        }
        TxnConnection::ExternalDriver { session, config, .. } => {
            session
                .invoke_with_timeout::<serde_json::Value>(
                    "commitManualTransaction",
                    serde_json::json!({ "connection": config.as_ref() }),
                    query_timeout_duration(Some(config.effective_query_timeout_secs())),
                )
                .await
                .map_err(|error| format!("COMMIT failed: {error}"))?;
        }
    }
    release_manual_txn_session_pool(state, &session.connection_id, &mut conn).await;

    log::info!("[query][manual_txn:commit] session_id={}", txn_session_id);
    Ok(db::QueryResult {
        columns: vec![],
        column_types: Vec::new(),
        deferred_large_value_cells: Vec::new(),
        column_sortables: vec![],
        spatial_columns: vec![],
        raw_key_cells: Vec::new(),
        spatial_values: vec![],
        rows: vec![],
        affected_rows: 0,
        execution_time_ms: 0,
        server_execute_time_us: None,
        query_timings_ms: None,
        truncated: false,
        session_id: None,
        has_more: false,
        elasticsearch_raw_body: None,
        messages: Vec::new(),
    })
}

/// Rollback an existing manual transaction session.
pub async fn rollback_manual_transaction(state: &AppState, txn_session_id: &str) -> Result<db::QueryResult, String> {
    if sqlserver_manual_transaction::is_session_id(txn_session_id) {
        return sqlserver_manual_transaction::finish(state, txn_session_id, false).await;
    }
    let session = {
        let mut sessions = state.transaction_sessions.write().await;
        sessions.remove(txn_session_id).ok_or("Transaction session not found")?
    };

    let mut conn = session.connection.lock().await;
    rollback_manual_txn_connection(&mut conn).await?;
    release_manual_txn_session_pool(state, &session.connection_id, &mut conn).await;

    log::info!("[query][manual_txn:rollback] session_id={}", txn_session_id);
    Ok(db::QueryResult {
        columns: vec![],
        column_types: Vec::new(),
        deferred_large_value_cells: Vec::new(),
        column_sortables: vec![],
        spatial_columns: vec![],
        raw_key_cells: Vec::new(),
        spatial_values: vec![],
        rows: vec![],
        affected_rows: 0,
        execution_time_ms: 0,
        server_execute_time_us: None,
        query_timings_ms: None,
        truncated: false,
        session_id: None,
        has_more: false,
        elasticsearch_raw_body: None,
        messages: Vec::new(),
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::query_cancel::RunningTaskMetadata;
    use std::sync::atomic::Ordering;
    use tokio::time::timeout;

    #[test]
    fn deferred_cells_reach_all_multi_result_success_constructors_once() {
        let sample = || {
            serde_json::from_value::<db::QueryResult>(serde_json::json!({
                "columns": ["__DBX_LARGE_VALUE_BYTES_user_column"], "rows": [["preview"]],
                "affected_rows": 0, "execution_time_ms": 0,
                "large_value_cells": [{ "row_index": 0, "column_index": 0,
                    "original_bytes": 67108864, "value_ref": "original-reference" }]
            }))
            .unwrap()
        };
        for result in [
            ExecuteMultiResult::from(sample()),
            ExecuteMultiResult::success_with_index(sample(), 0),
            ExecuteMultiResult::success_with_index_and_large_values(sample(), 0, Vec::new(), false),
            ExecuteMultiResult::success_with_optional_server_large_values(sample(), false),
        ] {
            assert!(result.result.deferred_large_value_cells.is_empty());
            assert_eq!(result.large_value_cells.len(), 1);
            assert_eq!(result.result.columns, vec!["__DBX_LARGE_VALUE_BYTES_user_column"]);
            let wire = serde_json::to_string(&result).unwrap();
            assert_eq!(wire.matches("\"large_value_cells\":").count(), 1);
            assert!(wire.contains("original-reference"));
        }
    }

    #[test]
    fn deferred_cells_follow_columns_when_nclob_markers_are_removed() {
        let sample = || {
            serde_json::from_value::<db::QueryResult>(serde_json::json!({
            "columns": ["ID", "N", "__DBX_LARGE_VALUE_BYTES_T_1", "C"],
            "rows": [[1, "national preview", "T:3", "clob preview"]],
            "affected_rows": 0, "execution_time_ms": 0,
            "large_value_cells": [{"row_index":0,"column_index":3,"original_bytes":67108864,"value_ref":"clob-original"}]
        })).unwrap()
        };
        for result in [
            ExecuteMultiResult::success_with_optional_server_large_values(sample(), true),
            ExecuteMultiResult::success_with_index_and_large_values(sample(), 0, Vec::new(), true),
        ] {
            assert_eq!(result.result.columns, vec!["ID", "N", "C"]);
            let deferred = result.large_value_cells.iter().find(|cell| cell.value_ref.is_some()).unwrap();
            assert_eq!(deferred.column_index, 2);
            assert_eq!(deferred.value_ref.as_deref(), Some("clob-original"));
            assert!(result.large_value_cells.iter().any(|cell| cell.column_index == 1 && cell.value_ref.is_none()));
        }
    }

    mod manual_transaction_snapshot_tests {
        use super::*;
        use tokio::io::{AsyncReadExt, AsyncWriteExt};

        struct SnapshotTestConnect {
            commands: Arc<Mutex<Vec<String>>>,
            fail_once: Option<&'static str>,
        }

        fn record_snapshot_command(commands: &Mutex<Vec<String>>, statement: &str) {
            // Infrastructure identity lookup for PostgreSQL notice attribution is
            // unrelated to manual-transaction snapshot rotation semantics.
            if statement.starts_with("SELECT pg_backend_pid()") {
                return;
            }
            commands.lock().unwrap().push(statement.to_string());
        }

        impl deadpool_postgres::Connect for SnapshotTestConnect {
            fn connect(
                &self,
                config: &tokio_postgres::Config,
            ) -> futures::future::BoxFuture<
                '_,
                Result<(tokio_postgres::Client, tokio::task::JoinHandle<()>), tokio_postgres::Error>,
            > {
                let mut config = config.clone();
                config.user("test").ssl_mode(tokio_postgres::config::SslMode::Disable);
                let commands = Arc::clone(&self.commands);
                let mut fail_once = self.fail_once;
                Box::pin(async move {
                    let (client_socket, mut server_socket) = tokio::io::duplex(8192);
                    tokio::spawn(async move {
                        let startup_length = server_socket.read_u32().await.unwrap();
                        let mut startup = vec![0; startup_length as usize - 4];
                        server_socket.read_exact(&mut startup).await.unwrap();
                        server_socket.write_all(b"R\0\0\0\x08\0\0\0\0Z\0\0\0\x05I").await.unwrap();
                        let mut statement = String::new();
                        while let Ok(message_type) = server_socket.read_u8().await {
                            let length = server_socket.read_u32().await.unwrap();
                            let mut payload = vec![0; length as usize - 4];
                            server_socket.read_exact(&mut payload).await.unwrap();
                            let (response_type, response) = match message_type {
                                b'P' => {
                                    statement =
                                        String::from_utf8(payload.split(|byte| *byte == 0).nth(1).unwrap().to_vec())
                                            .unwrap();
                                    (b'1', Vec::new())
                                }
                                b'B' => (b'2', Vec::new()),
                                b'D' => {
                                    if payload.first() == Some(&b'S') {
                                        server_socket.write_all(b"t\0\0\0\x06\0\0").await.unwrap();
                                    }
                                    (b'n', Vec::new())
                                }
                                b'E' => {
                                    record_snapshot_command(&commands, &statement);
                                    if fail_once == Some(statement.as_str()) {
                                        fail_once = None;
                                        (b'E', b"SERROR\0CXX000\0Mtest statement failed\0\0".to_vec())
                                    } else {
                                        let tag = if statement.starts_with("UPDATE") {
                                            "UPDATE 1"
                                        } else if statement.starts_with("SELECT") {
                                            "SELECT 0"
                                        } else {
                                            statement.as_str()
                                        };
                                        (b'C', format!("{tag}\0").into_bytes())
                                    }
                                }
                                b'S' => (b'Z', vec![b'I']),
                                b'C' => (b'3', Vec::new()),
                                b'X' => break,
                                other => panic!("unexpected PostgreSQL message: {other}"),
                            };
                            server_socket.write_u8(response_type).await.unwrap();
                            server_socket.write_u32((response.len() + 4) as u32).await.unwrap();
                            server_socket.write_all(&response).await.unwrap();
                        }
                    });
                    let (client, connection) = config.connect_raw(client_socket, tokio_postgres::NoTls).await?;
                    let task = tokio::spawn(async move {
                        let _ = connection.await;
                    });
                    Ok((client, task))
                })
            }
        }

        async fn snapshot_state(
            fail_once: Option<&'static str>,
        ) -> (AppState, Arc<Mutex<Vec<String>>>, tempfile::TempDir) {
            let directory = tempfile::tempdir().unwrap();
            let state = AppState::new(
                crate::persistence::test_storage::open(&directory.path().join("storage.db")).await.unwrap(),
            );
            let config = test_connection_config(DatabaseType::Postgres);
            state.configs.write().await.insert(config.id.clone(), config);
            let commands = Arc::new(Mutex::new(Vec::new()));
            let manager = deadpool_postgres::Manager::from_connect(
                tokio_postgres::Config::new(),
                SnapshotTestConnect { commands: Arc::clone(&commands), fail_once },
                deadpool_postgres::ManagerConfig::default(),
            );
            let pool = deadpool_postgres::Pool::builder(manager).max_size(1).build().unwrap();
            let connection = timeout(Duration::from_secs(5), pool.get()).await.unwrap().unwrap();
            state.transaction_sessions.write().await.insert(
                "snapshot".to_string(),
                TransactionSession {
                    mysql_data_charset: None,
                    sqlserver: None,
                    connection: Arc::new(tokio::sync::Mutex::new(TxnConnection::Postgres(Box::new(connection)))),
                    pool_key: "conn-1".to_string(),
                    last_activity: std::time::Instant::now(),
                    idle_timeout: std::time::Duration::from_secs(MANUAL_TRANSACTION_IDLE_TIMEOUT_SECS),
                    busy: false,
                    snapshot_rotation_safe: true,
                    connection_id: "conn-1".to_string(),
                    database: "test".to_string(),
                    schema: None,
                },
            );
            (state, commands, directory)
        }

        async fn execute_snapshot_batch(
            state: &AppState,
            sql: &str,
            classification_sql: Option<&str>,
        ) -> Result<Vec<ExecuteMultiResult>, String> {
            timeout(
                Duration::from_secs(5),
                execute_in_manual_transaction_with_options(
                    state,
                    "snapshot",
                    sql,
                    "test",
                    None,
                    ManualTransactionExecutionOptions {
                        classification_sql: classification_sql.map(str::to_string),
                        ..Default::default()
                    },
                ),
            )
            .await
            .expect("manual transaction must not deadlock")
        }

        #[tokio::test]
        async fn snapshot_predicate_uses_the_already_held_connection_guard() {
            let (state, _, _directory) = snapshot_state(None).await;
            let connection = Arc::clone(&state.transaction_sessions.read().await["snapshot"].connection);
            let guard = connection.lock().await;
            assert!(connection.try_lock().is_err());
            let mut sessions = state.transaction_sessions.write().await;
            let session = sessions.get_mut("snapshot").unwrap();
            assert!(session.can_rotate_read_only_snapshot(&guard));
            session.snapshot_rotation_safe = false;
            assert!(!session.can_rotate_read_only_snapshot(&guard));
            session.snapshot_rotation_safe = true;
            assert!(!session.can_rotate_read_only_snapshot(&TxnConnection::Mysql(None)));
        }

        #[tokio::test]
        async fn repeated_reads_rotate_and_no_statement_preserves_clean_history() {
            let (state, commands, _directory) = snapshot_state(None).await;
            execute_snapshot_batch(&state, "-- no statement", None).await.unwrap();
            assert!(commands.lock().unwrap().is_empty());
            for _ in 0..2 {
                let results = execute_snapshot_batch(&state, "SELECT 1", Some("SELECT 1")).await.unwrap();
                assert!(manual_txn_batch_fully_proven_read_only(&results));
                let sessions = state.transaction_sessions.read().await;
                assert!(!sessions["snapshot"].busy);
                assert!(sessions["snapshot"].snapshot_rotation_safe);
            }
            assert_eq!(*commands.lock().unwrap(), ["SELECT 1", "ROLLBACK", "BEGIN", "SELECT 1", "ROLLBACK", "BEGIN"]);
        }

        #[tokio::test]
        async fn writes_unknown_and_mixed_batches_permanently_prevent_rotation() {
            for (sql, classification) in [
                ("UPDATE users SET id = 2", Some("UPDATE users SET id = 2")),
                ("SELECT 1", None),
                ("SELECT 1", Some("SELECT unknown_function()")),
                ("SELECT 1; UPDATE users SET id = 2", Some("SELECT 1; UPDATE users SET id = 2")),
            ] {
                let (state, commands, _directory) = snapshot_state(None).await;
                execute_snapshot_batch(&state, sql, classification).await.unwrap();
                for _ in 0..2 {
                    execute_snapshot_batch(&state, "SELECT 1", Some("SELECT 1")).await.unwrap();
                }
                {
                    let sessions = state.transaction_sessions.read().await;
                    assert!(!sessions["snapshot"].snapshot_rotation_safe);
                    assert!(!sessions["snapshot"].busy);
                }
                assert!(!commands.lock().unwrap().iter().any(|sql| sql == "ROLLBACK" || sql == "BEGIN"));
                commit_manual_transaction(&state, "snapshot").await.unwrap();
                assert_eq!(commands.lock().unwrap().last().unwrap(), "COMMIT");
            }
        }

        #[tokio::test]
        async fn unclassified_stream_prevents_later_snapshot_rotation() {
            let (state, commands, _directory) = snapshot_state(None).await;
            stream_rows_in_manual_transaction(&state, "snapshot", "SELECT 1", 10, |_| Ok(())).await.unwrap();
            execute_snapshot_batch(&state, "SELECT 1", Some("SELECT 1")).await.unwrap();
            assert!(!state.transaction_sessions.read().await["snapshot"].snapshot_rotation_safe);
            assert_eq!(*commands.lock().unwrap(), ["SELECT 1", "SELECT 1"]);
        }

        #[tokio::test]
        async fn rotation_failure_removes_session_without_failing_successful_read() {
            for failure in ["ROLLBACK", "BEGIN"] {
                let (state, commands, _directory) = snapshot_state(Some(failure)).await;
                let results = execute_snapshot_batch(&state, "SELECT 1", Some("SELECT 1")).await.unwrap();
                assert!(manual_txn_batch_fully_proven_read_only(&results));
                assert!(!state.transaction_sessions.read().await.contains_key("snapshot"));
                let expected = if failure == "BEGIN" {
                    vec!["SELECT 1", "ROLLBACK", "BEGIN", "ROLLBACK"]
                } else {
                    vec!["SELECT 1", "ROLLBACK", "ROLLBACK"]
                };
                assert_eq!(*commands.lock().unwrap(), expected);
            }
        }

        #[tokio::test]
        async fn failed_batch_after_write_keeps_existing_rollback_cleanup() {
            let (state, commands, _directory) = snapshot_state(Some("SELECT 1")).await;
            execute_snapshot_batch(&state, "UPDATE users SET id = 2", Some("UPDATE users SET id = 2")).await.unwrap();
            let error = execute_snapshot_batch(&state, "SELECT 1", Some("SELECT 1")).await.unwrap_err();
            assert!(error.contains("manual transaction was rolled back"));
            assert!(!state.transaction_sessions.read().await.contains_key("snapshot"));
            assert_eq!(*commands.lock().unwrap(), ["UPDATE users SET id = 2", "SELECT 1", "ROLLBACK"]);
        }

        #[tokio::test]
        async fn cancelled_stream_after_write_keeps_existing_rollback_cleanup() {
            let (state, commands, _directory) = snapshot_state(None).await;
            execute_snapshot_batch(&state, "UPDATE users SET id = 2", Some("UPDATE users SET id = 2")).await.unwrap();
            let cancel = CancellationToken::new();
            cancel.cancel();
            let result =
                stream_rows_in_manual_transaction_with_cancel(&state, "snapshot", "SELECT 1", 10, Some(cancel), |_| {
                    Ok(())
                })
                .await;
            assert!(result.is_err());
            assert!(!state.transaction_sessions.read().await.contains_key("snapshot"));
            let commands = commands.lock().unwrap();
            assert_eq!(commands.last().unwrap(), "ROLLBACK");
            assert!(!commands.iter().any(|sql| sql == "BEGIN"));
        }
    }

    #[test]
    fn manual_txn_batch_proven_read_only_requires_non_empty_all_proven_results() {
        // Empty batch (e.g. comments-only script) must not rotate: there is no
        // snapshot to renew and rotating would churn a BEGIN for nothing.
        assert!(!manual_txn_batch_fully_proven_read_only(&[]));

        let unproven =
            vec![ExecuteMultiResult::success_with_optional_server_large_values(empty_query_result(1), false)];
        assert!(!manual_txn_batch_fully_proven_read_only(&unproven));

        let proven = unproven
            .clone()
            .into_iter()
            .map(|result| result.with_manual_transaction_proven_read_only())
            .collect::<Vec<_>>();
        assert!(manual_txn_batch_fully_proven_read_only(&proven));

        // One write statement anywhere in the batch keeps the transaction.
        let mixed = vec![
            ExecuteMultiResult::success_with_optional_server_large_values(empty_query_result(1), false)
                .with_manual_transaction_proven_read_only(),
            ExecuteMultiResult::success_with_optional_server_large_values(empty_query_result(1), false),
        ];
        assert!(!manual_txn_batch_fully_proven_read_only(&mixed));
    }

    #[test]
    fn redshift_queries_prefer_text_protocol() {
        assert!(postgres_prefers_text_protocol(Some(DatabaseType::Redshift)));
        assert!(!postgres_prefers_text_protocol(Some(DatabaseType::Postgres)));
        assert!(!postgres_prefers_text_protocol(None));
    }

    #[test]
    fn apply_query_timeout_override_respects_resolve_semantics() {
        // None leaves the budget unchanged.
        let mut budget = DbOperationBudget::with_defaults();
        let original = budget.query_timeout;
        apply_query_timeout_override(&mut budget, None);
        assert_eq!(budget.query_timeout, original);

        // Some(5) sets query_timeout to 5s.
        let mut budget = DbOperationBudget::with_defaults();
        apply_query_timeout_override(&mut budget, Some(5));
        assert_eq!(budget.query_timeout, Some(Duration::from_secs(5)));

        // Some(0) clears the limit (unlimited), matching resolve_query_timeout.
        let mut budget = DbOperationBudget::with_defaults();
        apply_query_timeout_override(&mut budget, Some(0));
        assert_eq!(budget.query_timeout, None);
    }

    #[tokio::test]
    async fn stream_progress_timeout_survives_steady_progress_past_the_budget() {
        // The timeout is an inactivity window, not a wall clock: a stream that keeps
        // marking progress survives well past the budget, as long as each gap between
        // marks is shorter than the timeout.
        let clock = Arc::new(StreamProgressClock::new());
        let clock_for_rows = clock.clone();
        let result = await_stream_with_progress_timeout(
            async move {
                for _ in 0..20 {
                    tokio::time::sleep(Duration::from_millis(30)).await;
                    clock_for_rows.mark();
                }
                Ok::<_, String>(42)
            },
            Some(Duration::from_millis(200)),
            clock,
            None,
            "timed out".to_string(),
        )
        .await;
        assert_eq!(result, Ok(42));
    }

    #[tokio::test]
    async fn stream_progress_timeout_fires_when_no_progress_arrives() {
        // A genuine stall — no progress for the whole budget — must still time out.
        let clock = Arc::new(StreamProgressClock::new());
        let result = await_stream_with_progress_timeout(
            async {
                tokio::time::sleep(Duration::from_millis(600)).await;
                Ok::<_, String>(42)
            },
            Some(Duration::from_millis(200)),
            clock,
            None,
            "timed out".to_string(),
        )
        .await;
        assert_eq!(result, Err("timed out".to_string()));
    }

    #[tokio::test]
    async fn sqlite_transaction_timeout_rolls_back_and_commits_nothing() {
        use std::sync::mpsc;

        let pool = db::sqlite::connect_path(":memory:").await.expect("connect in-memory SQLite");
        db::sqlite::execute_query(&pool, "CREATE TABLE t (id INTEGER PRIMARY KEY, val TEXT)")
            .await
            .expect("create table");

        // Deterministic timeout: hold the connection lock from a helper thread so the
        // transaction's first statement-boundary check is guaranteed to observe
        // elapsed time >= the 1ms query budget, regardless of machine speed.
        let mut budget = DbOperationBudget::with_defaults();
        budget.query_timeout = Some(Duration::from_millis(1));
        let (lock_held_tx, lock_held_rx) = mpsc::channel();
        let holder = {
            let pool = pool.clone();
            std::thread::spawn(move || {
                pool.with_connection(|_conn| {
                    let _ = lock_held_tx.send(());
                    std::thread::sleep(Duration::from_millis(200));
                    Ok(())
                })
                .expect("helper holds sqlite connection lock");
            })
        };
        lock_held_rx.recv().expect("helper acquired sqlite connection lock");

        let error = exec_tx_sqlite_inner(
            pool.clone(),
            &["INSERT INTO t (val) VALUES ('one')".to_string(), "INSERT INTO t (val) VALUES ('two')".to_string()],
            std::time::Instant::now(),
            &budget,
        )
        .await
        .expect_err("sqlite transaction must time out");

        assert!(error.contains("Query timed out after"), "unexpected error: {error}");

        // The transaction was rolled back (or never got past the first statement):
        // no partial rows may survive.
        holder.join().expect("helper thread joined");
        let result = db::sqlite::execute_query(&pool, "SELECT COUNT(*) AS n FROM t").await.expect("count rows");
        assert_eq!(result.rows[0][0], serde_json::json!(0));
    }

    #[tokio::test]
    async fn sqlite_slow_statement_is_interrupted_and_rolled_back() {
        // A statement that genuinely BLOCKS past the query budget must be aborted
        // mid-flight by the watchdog (SQLITE_INTERRUPT) rather than running to
        // completion and then committing. This is the gap the lock-holder test
        // above does not cover: that one only makes the first statement-boundary
        // check observe an already-elapsed budget.
        //
        // Lock-holder approach verified empirically and REJECTED: holding a write
        // lock from a second connection and letting the main handle busy-wait is
        // NOT interrupted by sqlite3_interrupt on the bundled SQLite 3.45.3
        // (rusqlite 0.32). `pager_wait_on_lock` loops on the busy handler without
        // re-checking `db->u1.isInterrupted`, so the wait runs out the full busy
        // timeout (a 60s wait with a 50ms budget confirmed it) instead of failing
        // fast. So the deterministic proof uses a slow-but-bounded statement whose
        // VDBE loop re-checks the interrupt flag every iteration (WITH RECURSIVE
        // row generator) — the watchdog interrupts it mid-flight.
        let dir = tempfile::tempdir().expect("temp dir");
        let db_path = dir.path().join("slow.db");
        let db_path = db_path.to_str().expect("utf8 temp path");

        let pool = db::sqlite::connect_path_create_if_missing(db_path).await.expect("connect sqlite");
        db::sqlite::execute_query(&pool, "CREATE TABLE t (id INTEGER PRIMARY KEY, val TEXT)")
            .await
            .expect("create table");

        let mut budget = DbOperationBudget::with_defaults();
        budget.query_timeout = Some(Duration::from_millis(50));

        // Generating 50M rows takes seconds, so this single statement is provably
        // still running when the 50ms watchdog fires. A busy-wait would not prove
        // the point (see above); this statement is interrupted mid-flight. It is
        // the LAST statement of a 2-statement batch so it also exercises the
        // loop-exit-then-final-guard path: a fast first statement, then a slow
        // second one that is still running when the budget elapses.
        let slow_sql = "INSERT INTO t (val) SELECT 'slow' FROM (WITH RECURSIVE cnt(x) AS \
                        (SELECT 1 UNION ALL SELECT x + 1 FROM cnt WHERE x < 50000000) SELECT x FROM cnt)";
        let error = exec_tx_sqlite_inner(
            pool.clone(),
            &["INSERT INTO t (val) VALUES ('fast')".to_string(), slow_sql.to_string()],
            std::time::Instant::now(),
            &budget,
        )
        .await
        .expect_err("sqlite transaction with a slow statement must time out");

        assert!(error.contains("Query timed out after"), "unexpected error: {error}");
        let result = db::sqlite::execute_query(&pool, "SELECT COUNT(*) AS n FROM t").await.expect("count rows");
        assert_eq!(result.rows[0][0], serde_json::json!(0));
    }

    #[tokio::test]
    async fn sqlite_legitimate_interrupt_text_error_is_not_reported_as_timeout() {
        // A genuine statement error whose MESSAGE contains "interrupt" (here a
        // missing column named `interrupted_at`) must NOT be misclassified as a
        // watchdog timeout. The interrupt is detected by the SQLITE_INTERRUPT
        // error code only, never by matching the message text. A large 60s budget
        // guarantees elapsed can never trigger the timeout path, so only the
        // error-code match could classify it.
        let pool = db::sqlite::connect_path(":memory:").await.expect("connect in-memory SQLite");
        db::sqlite::execute_query(&pool, "CREATE TABLE t (id INTEGER PRIMARY KEY, val TEXT)")
            .await
            .expect("create table");

        let mut budget = DbOperationBudget::with_defaults();
        budget.query_timeout = Some(Duration::from_secs(60));

        let error = exec_tx_sqlite_inner(
            pool.clone(),
            &["INSERT INTO t (interrupted_at) VALUES (1)".to_string()],
            std::time::Instant::now(),
            &budget,
        )
        .await
        .expect_err("sqlite transaction must fail with the statement error");

        assert!(
            !error.contains("Query timed out after"),
            "legitimate 'interrupt'-text error must not be masked as a timeout: {error}"
        );
        assert!(error.contains("Statement 1 failed") && error.contains("interrupted_at"), "unexpected error: {error}");
    }

    #[tokio::test]
    async fn sqlite_ssh_worker_transaction_rolls_back_on_failure_and_commits_on_success() {
        let remote = Arc::new(Mutex::new(rusqlite::Connection::open_in_memory().expect("open remote SQLite")));
        remote.lock().unwrap().execute_batch("CREATE TABLE t (id INTEGER PRIMARY KEY)").expect("create table");
        let (client_stream, worker_stream) = tokio::io::duplex(64 * 1024);
        tokio::spawn(fake_sqlite_ssh_worker(worker_stream, remote.clone()));
        let pool = db::sqlite::SqliteHandle::from_worker(Arc::new(
            db::sqlite_worker::SqliteWorkerClient::from_test_stream(client_stream),
        ));
        let budget = DbOperationBudget::with_defaults();
        let row_count =
            || remote.lock().unwrap().query_row("SELECT COUNT(*) FROM t", [], |row| row.get::<_, i64>(0)).unwrap();

        let error = exec_tx_sqlite_inner(
            pool.clone(),
            &["INSERT INTO t VALUES (1)".to_string(), "INSERT INTO missing VALUES (2)".to_string()],
            std::time::Instant::now(),
            &budget,
        )
        .await
        .expect_err("second statement fails");
        assert!(error.contains("Statement 2 failed") && error.contains("no such table: missing"), "{error}");
        assert_eq!(row_count(), 0);

        let result = exec_tx_sqlite_inner(
            pool,
            &["INSERT INTO t VALUES (1)".to_string(), "INSERT INTO t VALUES (2)".to_string()],
            std::time::Instant::now(),
            &budget,
        )
        .await
        .expect("transaction commits");
        assert_eq!(result.affected_rows, 2);
        assert_eq!(row_count(), 2);
    }

    /// Answers SQLite worker JSONL requests from a real SQLite connection.
    async fn fake_sqlite_ssh_worker(stream: tokio::io::DuplexStream, conn: Arc<Mutex<rusqlite::Connection>>) {
        use tokio::io::{AsyncBufReadExt, AsyncWriteExt};
        let (reader, mut writer) = tokio::io::split(stream);
        let mut lines = tokio::io::BufReader::new(reader).lines();
        while let Ok(Some(line)) = lines.next_line().await {
            let request: serde_json::Value = serde_json::from_str(&line).expect("worker request");
            let mut response = {
                let conn = conn.lock().unwrap();
                match conn.execute_batch(request["sql"].as_str().unwrap_or_default()) {
                    Ok(()) => serde_json::json!({ "affected_rows": conn.changes() }),
                    Err(error) => serde_json::json!({ "error": error.to_string() }),
                }
            };
            response["id"] = request["id"].clone();
            writer.write_all(format!("{response}\n").as_bytes()).await.expect("worker response");
        }
    }

    #[tokio::test]
    async fn sqlite_external_interrupt_without_budget_does_not_panic() {
        // Regression: with query_timeout = None (MCP policy can set it to 0 =
        // unlimited; resolve_query_timeout(Some(0)) -> None), an SQLITE_INTERRUPT
        // arriving from an EXTERNAL source (e.g. the query_cancel mechanism, not
        // this function's watchdog, which is only armed when query_timeout is
        // Some) used to be misclassified as a timeout, then `query_timeout.unwrap()`
        // panicked on None. The panic fired before the watchdog disarm/join and
        // before ROLLBACK, leaking the watchdog thread and leaving the BEGIN
        // transaction open on the pooled connection.
        //
        // Determinism: the bundled SQLite 3.45.3 clears the interrupt flag at
        // VDBE step start whenever `nVdbeActive == 0` (sqlite3Step), so a
        // pre-set interrupt fires only if it lands mid-statement. This was
        // verified empirically below. So the interrupt is issued from a timer
        // thread DURING a slow-but-bounded statement whose VDBE loop re-checks
        // the interrupt flag every iteration (WITH RECURSIVE row generator —
        // same technique the watchdog test uses; a lock busy-wait would not be
        // aborted because pager_wait_on_lock does not recheck isInterrupted).
        let pool = db::sqlite::connect_path(":memory:").await.expect("connect in-memory SQLite");
        db::sqlite::execute_query(&pool, "CREATE TABLE t (id INTEGER PRIMARY KEY, val TEXT)")
            .await
            .expect("create table");

        let interrupt =
            pool.with_connection(|conn| Ok(conn.get_interrupt_handle())).expect("get sqlite interrupt handle");
        let mut budget = DbOperationBudget::with_defaults();
        budget.query_timeout = None; // external interrupt: no in-process watchdog is armed

        // Arm the timer BEFORE the statement starts and send the go signal first,
        // so the interrupt can land mid-statement. A single pre-set interrupt
        // would be cleared (nVdbeActive==0 at step start), so the timer fires in
        // a short retry loop: as soon as the slow statement is running, the flag
        // sticks and the VDBE aborts on its next interrupt check. The `done` flag
        // stops the loop as soon as exec returns so no stray interrupt can hit a
        // later query, and we join the timer before any further query anyway.
        let done = Arc::new(std::sync::atomic::AtomicBool::new(false));
        let (timer_tx, timer_rx) = std::sync::mpsc::channel();
        let timer = {
            let done = done.clone();
            std::thread::spawn(move || {
                timer_rx.recv().expect("go signal");
                let deadline = std::time::Instant::now() + Duration::from_secs(5);
                while !done.load(Ordering::Relaxed) && std::time::Instant::now() < deadline {
                    interrupt.interrupt(); // external interrupt mid-statement
                    std::thread::sleep(Duration::from_millis(5));
                }
            })
        };
        timer_tx.send(()).expect("send go signal");

        // Single slow statement that takes seconds to run, so an interrupt that
        // lands within milliseconds of its start always hits mid-execution.
        let slow_sql = "INSERT INTO t (val) SELECT 'slow' FROM (WITH RECURSIVE cnt(x) AS \
                        (SELECT 1 UNION ALL SELECT x + 1 FROM cnt WHERE x < 10000000) SELECT x FROM cnt)";
        let error = exec_tx_sqlite_inner(pool.clone(), &[slow_sql.to_string()], std::time::Instant::now(), &budget)
            .await
            .expect_err("sqlite transaction must fail with the external interrupt");

        done.store(true, Ordering::Relaxed);
        timer.join().expect("timer thread joined");

        // Must NOT panic, must NOT be masked as a timeout (no budget), and must
        // surface as a plain statement error like any other failure.
        assert!(
            !error.contains("Query timed out after"),
            "external interrupt with no budget must not be reported as a timeout: {error}"
        );
        assert!(
            error.contains("Statement 1 failed") && error.contains("interrupted"),
            "expected a plain interrupted-statement error, got: {error}"
        );

        // The external interrupt must still roll back the transaction (0 rows),
        // just like any other statement failure.
        let result = db::sqlite::execute_query(&pool, "SELECT COUNT(*) AS n FROM t").await.expect("count rows");
        assert_eq!(result.rows[0][0], serde_json::json!(0));
    }

    #[tokio::test]
    async fn sqlite_cancelled_query_can_execute_again_on_same_client_session() {
        use std::sync::atomic::{AtomicBool, Ordering};

        let dir = tempfile::tempdir().expect("temp dir");
        let storage =
            crate::persistence::test_storage::open(&dir.path().join("storage.db")).await.expect("open test storage");
        let state = Arc::new(AppState::new(storage));
        let connection_id = "sqlite-cancel-session";
        let client_session_id = "query-tab-8414";
        let execution_id = "sqlite-cancel-execution";
        let db_path = dir.path().join("query.db");
        std::fs::File::create(&db_path).expect("create SQLite database file");
        let mut config = test_connection_config(DatabaseType::Sqlite);
        config.id = connection_id.to_string();
        config.host = db_path.to_string_lossy().into_owned();
        config.query_timeout_secs = 0;
        state.configs.write().await.insert(connection_id.to_string(), config);

        let pool_key = state
            .get_or_create_pool_for_session(connection_id, Some(""), Some(client_session_id))
            .await
            .expect("create SQLite query-tab session pool");
        let sqlite = match state.pool_handle(&pool_key).await.expect("SQLite pool") {
            PoolKind::Sqlite(pool) => pool,
            _ => panic!("expected SQLite pool"),
        };
        db::sqlite::execute_query(&sqlite, "CREATE TABLE t (value TEXT)").await.expect("create test table");

        let cleanup_interrupt =
            sqlite.with_connection(|conn| Ok(conn.get_interrupt_handle())).expect("get SQLite interrupt handle");
        let started = Arc::new(AtomicBool::new(false));
        let started_by_sqlite = started.clone();
        sqlite
            .with_connection(|conn| {
                conn.create_scalar_function(
                    "dbx_test_query_started",
                    0,
                    rusqlite::functions::FunctionFlags::SQLITE_UTF8,
                    move |_ctx| {
                        started_by_sqlite.store(true, Ordering::SeqCst);
                        Ok(1_i64)
                    },
                )
                .map_err(|error| error.to_string())
            })
            .expect("register query-start marker");

        let registered = state.running_queries.register_task(
            execution_id.to_string(),
            RunningTaskMetadata::query(connection_id, "", Some(client_session_id.to_string())),
        );
        let first_state = state.clone();
        let first = tokio::spawn(async move {
            execute_sql_statement_with_options_typed(
                first_state.as_ref(),
                connection_id,
                "",
                "INSERT INTO t (value) SELECT 'slow' FROM (WITH RECURSIVE cnt(x) AS (SELECT dbx_test_query_started() UNION ALL SELECT x + 1 FROM cnt WHERE x < 100000000) SELECT x FROM cnt)",
                None,
                Some(registered.token()),
                QueryExecutionOptions {
                    client_session_id: Some(client_session_id.to_string()),
                    execution_id: Some(execution_id.to_string()),
                    timeout_secs: Some(0),
                    ..Default::default()
                },
            )
            .await
        });

        let started_deadline = std::time::Instant::now() + Duration::from_secs(10);
        while !started.load(Ordering::SeqCst) {
            assert!(!first.is_finished(), "slow SQLite query exited before it started");
            assert!(std::time::Instant::now() < started_deadline, "slow SQLite query did not start");
            tokio::time::sleep(Duration::from_millis(5)).await;
        }

        assert!(state.running_queries.cancel(execution_id), "query cancellation was not registered");
        let first_result = tokio::time::timeout(Duration::from_secs(2), first)
            .await
            .expect("cancelled SQLite query did not return")
            .expect("join cancelled SQLite query")
            .expect_err("cancelled SQLite query unexpectedly succeeded");
        assert!(
            matches!(first_result, QueryExecutionError::Canceled { .. }),
            "unexpected cancellation error: {first_result}"
        );

        let next_execution_id = "sqlite-cancel-execution-next";
        let next_registered = state.running_queries.register_task(
            next_execution_id.to_string(),
            RunningTaskMetadata::query(connection_id, "", Some(client_session_id.to_string())),
        );
        let next_state = state.clone();
        let mut next = tokio::spawn(async move {
            execute_sql_statement_with_options_typed(
                next_state.as_ref(),
                connection_id,
                "",
                "SELECT 1",
                None,
                Some(next_registered.token()),
                QueryExecutionOptions {
                    client_session_id: Some(client_session_id.to_string()),
                    execution_id: Some(next_execution_id.to_string()),
                    timeout_secs: Some(0),
                    ..Default::default()
                },
            )
            .await
        });
        let next_wait = tokio::time::timeout(Duration::from_secs(2), &mut next).await;
        let next_timed_out = next_wait.is_err();
        if next_timed_out {
            // Keep the regression test bounded even on the unfixed implementation:
            // the manually retained handle interrupts the still-running blocking
            // SQLite statement so the test runtime can shut down cleanly.
            cleanup_interrupt.interrupt();
        }
        let next_result = match next_wait {
            Ok(result) => result.expect("join follow-up SQLite query"),
            Err(_) => tokio::time::timeout(Duration::from_secs(2), &mut next)
                .await
                .expect("follow-up SQLite query did not finish after cleanup interrupt")
                .expect("join follow-up SQLite query after cleanup"),
        };
        assert!(!next_timed_out, "same query-tab SQLite session remained blocked after cancellation");
        let next_result = next_result.expect("follow-up SELECT 1 failed");
        assert_eq!(next_result.rows, vec![vec![serde_json::json!(1)]]);
    }

    #[tokio::test]
    async fn sqlite_pre_set_interrupt_is_cleared_before_next_statement() {
        // Empirical probe for the pre-set-interrupt question: `sqlite3_interrupt`
        // unconditionally sets the interrupt flag, but bundled SQLite 3.45.3
        // clears it at VDBE step start whenever `nVdbeActive == 0` (sqlite3Step,
        // "prevents a call to sqlite3_interrupt from interrupting a statement
        // that has not yet started"). Verified here: a pre-set interrupt (with
        // nothing running) is cleared before the NEXT statement begins, so that
        // statement runs to completion. A regression test therefore cannot rely
        // on a pre-set interrupt — it must interrupt DURING a running statement
        // (see sqlite_external_interrupt_without_budget_does_not_panic).
        let pool = db::sqlite::connect_path(":memory:").await.expect("connect in-memory SQLite");
        db::sqlite::execute_query(&pool, "CREATE TABLE t (id INTEGER PRIMARY KEY, val TEXT)")
            .await
            .expect("create table");

        let interrupt =
            pool.with_connection(|conn| Ok(conn.get_interrupt_handle())).expect("get sqlite interrupt handle");

        // Pre-set the interrupt, then run a fast statement on the SAME
        // connection: the flag is cleared at step start, so the statement
        // succeeds (it does NOT fail with SQLITE_INTERRUPT).
        pool.with_connection(|conn| {
            interrupt.interrupt(); // pre-set, nothing running yet
            conn.execute_batch("INSERT INTO t (val) VALUES ('one')").map_err(|e| e.to_string())
        })
        .expect("pre-set interrupt must be cleared before the statement runs");

        // Same for a transaction: pre-set the interrupt, then the transaction's
        // first statement must still run and commit.
        pool.with_connection(|_conn| {
            interrupt.interrupt(); // pre-set, nothing running yet
            Ok(())
        })
        .expect("pre-set interrupt on idle connection");

        let mut budget = DbOperationBudget::with_defaults();
        budget.query_timeout = None;
        let result = exec_tx_sqlite_inner(
            pool.clone(),
            &["INSERT INTO t (val) VALUES ('two')".to_string()],
            std::time::Instant::now(),
            &budget,
        )
        .await
        .expect("transaction with a pre-set interrupt must still succeed (flag cleared at step start)");
        assert_eq!(result.affected_rows, 1);

        let count = db::sqlite::execute_query(&pool, "SELECT COUNT(*) AS n FROM t").await.expect("count rows");
        assert_eq!(count.rows[0][0], serde_json::json!(2));
    }

    #[test]
    fn execute_multi_result_manual_transaction_markers_serialize_conditionally() {
        let plain = ExecuteMultiResult::success_with_optional_server_large_values(empty_query_result(0), false);
        let plain_value = serde_json::to_value(&plain).unwrap();
        assert!(plain_value.get("manual_transaction_proven_read_only").is_none());
        assert!(plain_value.get("manual_transaction_no_statement").is_none());

        let proven = plain.clone().with_manual_transaction_proven_read_only();
        let proven_value = serde_json::to_value(&proven).unwrap();
        assert_eq!(proven_value.get("manual_transaction_proven_read_only"), Some(&serde_json::Value::Bool(true)));
        assert!(proven_value.get("manual_transaction_no_statement").is_none());

        let no_statement = plain.with_manual_transaction_no_statement();
        let no_statement_value = serde_json::to_value(&no_statement).unwrap();
        assert_eq!(no_statement_value.get("manual_transaction_no_statement"), Some(&serde_json::Value::Bool(true)));
        assert!(no_statement_value.get("manual_transaction_proven_read_only").is_none());
    }

    #[test]
    fn schema_diff_destructive_detection_covers_drop_and_alter_drop() {
        assert!(is_destructive_schema_diff_statement("DROP INDEX idx_users_email ON users"));
        assert!(is_destructive_schema_diff_statement("TRUNCATE TABLE audit_log"));
        assert!(is_destructive_schema_diff_statement(
            "ALTER TABLE users DROP COLUMN legacy_code, DROP INDEX idx_legacy"
        ));
    }

    #[test]
    fn schema_diff_destructive_detection_ignores_comments_literals_and_identifiers() {
        assert!(!is_destructive_schema_diff_statement("-- DROP INDEX idx_fake\nSELECT 1"));
        assert!(!is_destructive_schema_diff_statement("SELECT 'DROP TABLE users'"));
        assert!(!is_destructive_schema_diff_statement("ALTER TABLE \"DROP INDEX audit\" ADD COLUMN note TEXT"));
    }
    use crate::db::agent_driver::{AgentDriverClient, AgentLaunchSpec};
    use crate::models::connection::{default_redis_key_separator, ConnectionConfig, DatabaseType};
    #[cfg(unix)]
    use crate::plugins::{
        InstalledPlugin, PluginCompatibility, PluginDriverManifest, PluginDriverSession, PluginManifest,
        PluginRuntimeEnv,
    };

    #[cfg(unix)]
    async fn spawn_agent_batch_timeout_test_client() -> (AgentDriverClient, tempfile::NamedTempFile) {
        use std::io::Write;

        let mut script = tempfile::NamedTempFile::new().unwrap();
        write!(
            script,
            r#"import json
import sys
import time

print(json.dumps({{"ready": True}}), flush=True)
for line in sys.stdin:
    request = json.loads(line)
    statements = request.get("params", {{}}).get("statements", [])
    time.sleep(1.2 if statements == ["slow"] else 0.05)
    result = {{
        "columns": [],
        "column_types": [],
        "column_sortables": [],
        "rows": [],
        "affected_rows": 1,
        "execution_time_ms": 50,
        "truncated": False,
        "session_id": None,
        "has_more": False
    }}
    print(json.dumps({{"jsonrpc": "2.0", "id": request["id"], "result": result}}), flush=True)
"#
        )
        .unwrap();
        script.flush().unwrap();

        let client = AgentDriverClient::spawn(
            AgentLaunchSpec::new("python3").with_args([script.path().to_string_lossy().to_string()]),
        )
        .await
        .unwrap();
        (client, script)
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn execute_multi_agent_zero_timeout_waits_for_response() {
        let (mut client, _script) = spawn_agent_batch_timeout_test_client().await;

        let result = execute_multi_agent(&mut client, None, &["fast".to_string()], None, Some(0)).await.unwrap();

        assert_eq!(result.affected_rows, 1);
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn execute_multi_agent_positive_timeout_still_expires() {
        let (mut client, _script) = spawn_agent_batch_timeout_test_client().await;

        let error = execute_multi_agent(&mut client, None, &["slow".to_string()], None, Some(1)).await.unwrap_err();

        assert!(matches!(
            error,
            AgentCallError::Timeout {
                stage: AgentErrorStage::Execute,
                operation_outcome: AgentOperationOutcome::Unknown,
            }
        ));
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn execute_multi_agent_default_timeout_keeps_normal_execution() {
        let (mut client, _script) = spawn_agent_batch_timeout_test_client().await;

        let result = execute_multi_agent(&mut client, None, &["fast".to_string()], None, None).await.unwrap();

        assert_eq!(result.affected_rows, 1);
        assert_eq!(resolve_query_timeout(None), Some(QUERY_TIMEOUT));
    }

    #[test]
    fn external_catalog_queries_do_not_bind_database_during_pool_creation() {
        assert_eq!(query_pool_database("bi", Some("paimon_catalog")), None);
        assert_eq!(query_pool_database("bi", None), Some("bi"));
        assert_eq!(query_pool_database("", None), None);
    }

    #[tokio::test]
    async fn query_and_transaction_paths_resolve_catalog_dialect_from_connection() {
        let dir = std::env::temp_dir().join(format!("dbx-catalog-dialect-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&dir).unwrap();
        let storage = crate::persistence::test_storage::open(&dir.join("storage.db")).await.unwrap();
        let state = AppState::new(storage);

        let mut doris = test_connection_config(DatabaseType::Doris);
        doris.id = "doris".to_string();
        let mut starrocks = test_connection_config(DatabaseType::StarRocks);
        starrocks.id = "starrocks".to_string();
        {
            let mut configs = state.configs.write().await;
            configs.insert(doris.id.clone(), doris);
            configs.insert(starrocks.id.clone(), starrocks);
        }

        assert_eq!(
            connection_mysql_catalog_dialect_for_pool_key(&state, "doris:bi").await,
            Some(db::mysql::MySqlCatalogDialect::Doris)
        );
        assert_eq!(
            connection_mysql_catalog_dialect(&state, "starrocks").await,
            Some(db::mysql::MySqlCatalogDialect::StarRocks)
        );

        drop(state);
        let _ = std::fs::remove_dir_all(dir);
    }

    #[test]
    fn schema_diff_atomicity_marks_mysql_ddl_as_partial() {
        let atomicity = classify_schema_diff_atomicity(
            Some(DatabaseType::Mysql),
            &["CREATE TABLE users (id INT)".to_string(), "ALTER TABLE users ADD COLUMN name VARCHAR(32)".to_string()],
            true,
        );

        assert_eq!(atomicity, SchemaDiffAtomicity::PartialEffectsPossible);
    }

    #[test]
    fn schema_diff_atomicity_marks_oracle_ddl_as_partial() {
        let atomicity = classify_schema_diff_atomicity(
            Some(DatabaseType::Oracle),
            &["CREATE TABLE users (id INT)".to_string(), "ALTER TABLE users ADD name VARCHAR2(32)".to_string()],
            true,
        );

        assert_eq!(atomicity, SchemaDiffAtomicity::PartialEffectsPossible);
    }

    #[test]
    fn schema_diff_atomicity_keeps_postgres_ddl_atomic() {
        let atomicity = classify_schema_diff_atomicity(
            Some(DatabaseType::Postgres),
            &["CREATE TABLE users (id INT)".to_string(), "ALTER TABLE users ADD COLUMN name VARCHAR(32)".to_string()],
            true,
        );

        assert_eq!(atomicity, SchemaDiffAtomicity::GuaranteedRollback);
    }

    #[test]
    fn schema_diff_atomicity_keeps_dml_atomic_when_tx_path_exists() {
        let atomicity = classify_schema_diff_atomicity(
            Some(DatabaseType::Mysql),
            &["INSERT INTO users VALUES (1)".to_string(), "UPDATE users SET active = 1 WHERE id = 1".to_string()],
            true,
        );

        assert_eq!(atomicity, SchemaDiffAtomicity::GuaranteedRollback);
    }

    #[test]
    fn schema_diff_atomicity_marks_missing_transaction_path_as_partial() {
        let atomicity = classify_schema_diff_atomicity(
            Some(DatabaseType::Postgres),
            &["CREATE TABLE users (id INT)".to_string()],
            false,
        );

        assert_eq!(atomicity, SchemaDiffAtomicity::PartialEffectsPossible);
    }

    #[test]
    fn executed_count_before_error_uses_failing_statement_index() {
        assert_eq!(executed_count_before_error("Statement 1 failed: syntax error", 3), 0);
        assert_eq!(executed_count_before_error("Statement 2 failed: syntax error", 3), 1);
        assert_eq!(executed_count_before_error("Statement 3 failed: syntax error", 3), 2);
    }

    #[test]
    fn schema_diff_failure_outcome_rolls_back_when_atomic() {
        let (status, executed) =
            schema_diff_failure_outcome(SchemaDiffAtomicity::GuaranteedRollback, "Statement 2 failed: syntax error", 3);
        assert_eq!(status, crate::two_phase_commit::TransactionStatus::RolledBack);
        assert_eq!(executed, 0);
    }

    #[test]
    fn schema_diff_failure_outcome_reports_mixed_with_partial_count() {
        let (status, executed) = schema_diff_failure_outcome(
            SchemaDiffAtomicity::PartialEffectsPossible,
            "Statement 2 failed: syntax error",
            3,
        );
        assert_eq!(status, crate::two_phase_commit::TransactionStatus::Mixed);
        assert_eq!(executed, 1);
    }

    #[test]
    fn schema_diff_atomicity_keeps_sqlite_ddl_atomic() {
        let atomicity = classify_schema_diff_atomicity(
            Some(DatabaseType::Sqlite),
            &["CREATE TABLE users (id INTEGER)".to_string()],
            true,
        );
        assert_eq!(atomicity, SchemaDiffAtomicity::GuaranteedRollback);
    }

    /// MySQL: first DDL may already commit; second fails → mixed + executed_count = 1.
    #[test]
    fn mysql_second_ddl_failure_maps_to_mixed_with_partial_executed_count() {
        let stmts = ["CREATE TABLE t1 (id INT)".to_string(), "CREATE TABLE t2 (id INT)".to_string()];
        let atomicity = classify_schema_diff_atomicity(Some(DatabaseType::Mysql), &stmts, true);
        assert_eq!(atomicity, SchemaDiffAtomicity::PartialEffectsPossible);
        let (status, executed) =
            schema_diff_failure_outcome(atomicity, "Statement 2 failed: table already exists", stmts.len());
        assert_eq!(status, crate::two_phase_commit::TransactionStatus::Mixed);
        assert_eq!(executed, 1);
    }

    /// Oracle: same non-transactional DDL semantics as MySQL for deploy status.
    #[test]
    fn oracle_second_ddl_failure_maps_to_mixed_with_partial_executed_count() {
        let stmts = ["CREATE TABLE t1 (id NUMBER)".to_string(), "ALTER TABLE t1 ADD name VARCHAR2(32)".to_string()];
        let atomicity = classify_schema_diff_atomicity(Some(DatabaseType::Oracle), &stmts, true);
        assert_eq!(atomicity, SchemaDiffAtomicity::PartialEffectsPossible);
        let (status, executed) = schema_diff_failure_outcome(atomicity, "Statement 2 failed: ORA-00942", stmts.len());
        assert_eq!(status, crate::two_phase_commit::TransactionStatus::Mixed);
        assert_eq!(executed, 1);
    }

    /// Postgres transactional DDL: second fails → rolled_back + executed_count = 0.
    #[test]
    fn postgres_second_ddl_failure_maps_to_rolled_back_zero_executed() {
        let stmts = ["CREATE TABLE t1 (id INT)".to_string(), "CREATE TABLE t2 (id INT)".to_string()];
        let atomicity = classify_schema_diff_atomicity(Some(DatabaseType::Postgres), &stmts, true);
        assert_eq!(atomicity, SchemaDiffAtomicity::GuaranteedRollback);
        let (status, executed) =
            schema_diff_failure_outcome(atomicity, "Statement 2 failed: relation already exists", stmts.len());
        assert_eq!(status, crate::two_phase_commit::TransactionStatus::RolledBack);
        assert_eq!(executed, 0);
    }

    #[test]
    fn query_execution_mode_deserializes_simple_client_value() {
        let mode: QueryExecutionMode = serde_json::from_str("\"simple\"").unwrap();

        assert_eq!(mode, QueryExecutionMode::Simple);
        assert_eq!(QueryExecutionMode::default(), QueryExecutionMode::Standard);
    }

    #[test]
    fn query_execution_mode_deserializes_postgres_read_only_transaction() {
        let mode: QueryExecutionMode = serde_json::from_str("\"postgres_read_only_transaction\"").unwrap();

        assert_eq!(mode, QueryExecutionMode::PostgresReadOnlyTransaction);
    }

    #[test]
    fn postgres_read_only_transaction_requires_postgres_and_isolated_session() {
        let mut options = QueryExecutionOptions {
            execution_mode: QueryExecutionMode::PostgresReadOnlyTransaction,
            ..Default::default()
        };

        assert!(validate_query_execution_mode(Some(DatabaseType::Mysql), "SELECT 1", &options).is_err());
        assert!(validate_query_execution_mode(Some(DatabaseType::Postgres), "SELECT 1", &options).is_err());

        options.client_session_id = Some("tab:explain:execution".to_string());
        assert_eq!(validate_query_execution_mode(Some(DatabaseType::Postgres), "SELECT 1", &options), Ok(()));
        assert!(validate_query_execution_mode(Some(DatabaseType::Postgres), "SELECT 1; SELECT 2", &options).is_err());
    }

    pub(super) fn idle_test_session(idle_timeout: Duration) -> TransactionSession {
        TransactionSession {
            mysql_data_charset: None,
            sqlserver: None,
            connection: Arc::new(tokio::sync::Mutex::new(TxnConnection::Mysql(None))),
            pool_key: "conn-1".to_string(),
            last_activity: std::time::Instant::now(),
            idle_timeout,
            busy: false,
            snapshot_rotation_safe: false,
            connection_id: "conn-1".to_string(),
            database: "test".to_string(),
            schema: None,
        }
    }

    #[tokio::test]
    async fn idle_watcher_rolls_back_after_the_session_own_timeout() {
        let sessions = Arc::new(tokio::sync::RwLock::new(std::collections::HashMap::new()));
        {
            let mut guard = sessions.write().await;
            guard.insert("short".to_string(), idle_test_session(Duration::from_millis(50)));
            guard.insert("long".to_string(), idle_test_session(Duration::from_secs(3600)));
        }
        spawn_txn_idle_watcher_for_sessions(Arc::clone(&sessions), "short".to_string());
        spawn_txn_idle_watcher_for_sessions(Arc::clone(&sessions), "long".to_string());

        tokio::time::sleep(Duration::from_millis(400)).await;

        let guard = sessions.read().await;
        assert!(!guard.contains_key("short"), "a 50ms session must be reaped long before the 300s default");
        assert!(guard.contains_key("long"), "a session with a longer window must survive the same wait");
    }

    #[tokio::test]
    async fn manual_transaction_idle_timeout_follows_connection_config() {
        let dir = std::env::temp_dir().join(format!("dbx-txn-timeout-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&dir).unwrap();
        let storage = crate::persistence::test_storage::open(&dir.join("storage.db")).await.unwrap();
        let state = AppState::new(storage);
        let mut config = test_connection_config(DatabaseType::Postgres);
        config.manual_transaction_timeout_secs = 1_800;
        state.configs.write().await.insert(config.id.clone(), config);

        assert_eq!(manual_transaction_idle_timeout(&state, "conn-1").await, Duration::from_secs(1_800));
        // An unknown connection keeps the historical five-minute window.
        assert_eq!(manual_transaction_idle_timeout(&state, "missing").await, Duration::from_secs(300));
        let _ = std::fs::remove_dir_all(dir);
    }

    pub(super) fn test_connection_config(db_type: DatabaseType) -> ConnectionConfig {
        ConnectionConfig {
            oracle_oci_nls_lang: None,
            oracle_oci_tns_admin: None,
            docs_notes_path: None,
            id: "conn-1".to_string(),
            name: "Connection".to_string(),
            note: String::new(),
            db_type,
            driver_profile: None,
            driver_label: None,
            url_params: None,
            agent_java_options: Vec::new(),
            host: "localhost".to_string(),
            port: 0,
            username: String::new(),
            password: String::new(),
            database: None,
            default_schema: None,
            visible_databases: None,
            visible_database_patterns: None,
            visible_schemas: None,
            show_system_schemas: false,
            sidebar_auto_load_all_tables: false,
            show_database_links: None,
            show_native_jobs: None,
            show_user_admin: None,
            attached_databases: Vec::new(),
            init_script: None,
            color: None,
            transport_layers: Vec::new(),
            connect_timeout_secs: 10,
            query_timeout_secs: 30,
            idle_timeout_secs: 60,
            manual_transaction_timeout_secs: 300,
            idle_disconnect_secs: 0,
            keepalive_interval_secs: 30,
            ssl: false,
            ca_cert_path: String::new(),
            client_cert_path: String::new(),
            client_key_path: String::new(),
            sysdba: false,
            oracle_connection_type: None,
            connection_string: None,
            redis_connection_mode: None,
            redis_sentinel_master: String::new(),
            redis_sentinel_nodes: String::new(),
            redis_sentinel_username: String::new(),
            redis_sentinel_password: String::new(),
            redis_sentinel_tls: false,
            redis_cluster_nodes: String::new(),
            redis_key_separator: default_redis_key_separator(),
            redis_scan_page_size: None,
            redis_database_aliases: Default::default(),
            redis_key_templates: Vec::new(),
            redis_key_filter: None,
            redis_key_grouping: None,
            etcd_endpoints: String::new(),
            gbase_server: String::new(),
            informix_server: String::new(),
            external_config: None,
            plugin_id: None,
            plugin_connection_provider: None,
            plugin_connection_type: None,
            connection_secrets: Default::default(),
            jdbc_driver_class: None,
            jdbc_driver_paths: Vec::new(),
            one_time: false,
            save_password: true,
            read_only: false,
            is_production: false,
            production_databases: vec![],
            production_badge_scope: None,
            database_info: None,
        }
    }

    #[cfg(feature = "dynamodb")]
    #[tokio::test]
    #[ignore = "requires DBX_DYNAMODB_ENDPOINT and an orders table"]
    async fn live_dynamodb_editor_scan_serializes_one_thousand_rows() {
        let endpoint = std::env::var("DBX_DYNAMODB_ENDPOINT").expect("DBX_DYNAMODB_ENDPOINT is required");
        let (ssl, address) = endpoint
            .strip_prefix("https://")
            .map(|address| (true, address))
            .or_else(|| endpoint.strip_prefix("http://").map(|address| (false, address)))
            .expect("DynamoDB endpoint must start with http:// or https://");
        let (host, port) = address.rsplit_once(':').expect("DynamoDB endpoint must include a port");
        let dir = std::env::temp_dir().join(format!("dbx-query-dynamodb-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&dir).unwrap();
        let storage = crate::persistence::test_storage::open(&dir.join("storage.db")).await.unwrap();
        let state = AppState::new(storage);
        let mut config = test_connection_config(DatabaseType::DynamoDb);
        config.host = host.to_string();
        config.port = port.parse().expect("valid DynamoDB port");
        config.username = "dummy".to_string();
        config.password = "dummy".to_string();
        config.database = Some("us-east-1".to_string());
        config.ssl = ssl;
        let client = db::dynamodb_driver::connect(&config, host, config.port).unwrap();
        db::dynamodb_driver::test_connection(&client, Duration::from_secs(5)).await.unwrap();
        state.configs.write().await.insert(config.id.clone(), config.clone());
        state
            .update_connection_pools(|connections| {
                connections.insert(config.id.clone(), PoolKind::DynamoDb(client));
            })
            .await;

        let results = execute_multi_core_with_options_for_client_and_progress_typed(
            &state,
            &config.id,
            "us-east-1",
            "DBX DYNAMODB SCAN\ntable: \"orders\"\nlimit: 1000",
            None,
            None,
            QueryExecutionOptions { max_rows: Some(1000), ..Default::default() },
            None,
        )
        .await
        .unwrap();

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].result.rows.len(), 1000);
        let serialized = serde_json::to_string(&results).unwrap();
        assert!(!serialized.is_empty());

        drop(state);
        let _ = std::fs::remove_dir_all(dir);
    }

    async fn sqlserver_agent_echo_state(
    ) -> (AppState, std::path::PathBuf, std::sync::Arc<crate::db::agent_driver::AgentRuntimeClient>) {
        let dir = std::env::temp_dir().join(format!("dbx-query-sqlserver-agent-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&dir).unwrap();
        let script_path = dir.join("agent.py");
        std::fs::write(
            &script_path,
            r#"import json, sys
print(json.dumps({'ready': True}), flush=True)
for line in sys.stdin:
    req = json.loads(line)
    with open(sys.argv[1], 'a') as calls:
        calls.write(json.dumps(req) + '\n')
    if req['method'] == 'handshake':
        result = {'protocolVersion': 2, 'agentProtocolVersion': 2, 'capabilities': ['multi_session']}
    elif req['method'] in ('execute_query', 'execute_query_page'):
        result = {
            'columns': ['sql'], 'column_types': ['nvarchar'], 'column_sortables': [],
            'rows': [[req['params']['sql']]], 'affected_rows': 0, 'execution_time_ms': 1,
            'truncated': False, 'session_id': None, 'has_more': False
        }
        if req['params'].get('returnAllResults'):
            result = [result]
            sql = req['params']['sql']
            if 'RAISERROR' in sql:
                print(json.dumps({'jsonrpc':'2.0', 'id':req['id'], 'error': {
                    'code': -1, 'message': 'late SQL Server batch error', 'data': {
                        'category': 'sql', 'sqlState': 'S0001', 'vendorCode': 50000,
                        'retryable': False, 'sessionDisposition': 'keep', 'stage': 'execute'
                    }}}), flush=True)
                continue
            if 'SELECT 101' in sql and 'SELECT 303' in sql:
                result = [dict(result[0], columns=[name], rows=rows) for name, rows in [
                    ('first_result', [[101], [102], [103]]), ('second_result', [[202]]), ('third_result', [[303]])]]
    elif req['method'] == 'fetch_query_page':
        result = {'columns': ['next_page'], 'rows': [[2]], 'affected_rows': 0, 'execution_time_ms': 1}
    elif req['method'] in ('execute_batch', 'execute_transaction'):
        if req['params'].get('schema') is not None:
            print(json.dumps({
                'jsonrpc': '2.0', 'id': req['id'],
                'error': {'code': -1, 'message': 'legacy SQL Server schema switch attempted'}
            }), flush=True)
            continue
        result = {
            'columns': [], 'column_types': [], 'column_sortables': [], 'rows': [],
            'affected_rows': 1, 'execution_time_ms': 1, 'truncated': False,
            'session_id': None, 'has_more': False
        }
    else:
        result = {}
    print(json.dumps({'jsonrpc': '2.0', 'id': req['id'], 'result': result}), flush=True)
"#,
        )
        .unwrap();

        let python = if cfg!(windows) { "python" } else { "python3" };
        let runtime = crate::db::agent_driver::AgentRuntimeClient::spawn(
            crate::db::agent_driver::AgentLaunchSpec::new(python).with_args([
                script_path.to_string_lossy().to_string(),
                dir.join("calls.jsonl").to_string_lossy().to_string(),
            ]),
            "test",
        )
        .await
        .unwrap();
        runtime.increment_session_count();

        let storage = crate::persistence::test_storage::open(&dir.join("storage.db")).await.unwrap();
        let state = AppState::new(storage);
        state.configs.write().await.insert("conn-1".to_string(), test_connection_config(DatabaseType::SqlServer));
        state
            .update_connection_pools(|connections| {
                connections.insert(
                    "conn-1".to_string(),
                    PoolKind::agent(crate::db::agent_driver::AgentDriverClient::shared_session(
                        runtime.clone(),
                        "session-1".to_string(),
                    )),
                );
            })
            .await;

        (state, dir, runtime)
    }

    #[tokio::test]
    async fn sqlserver_agent_multi_execution_sends_table_variable_script_as_one_batch() {
        let (state, dir, runtime) = sqlserver_agent_echo_state().await;
        let sql = "DECLARE @TargetTables TABLE (TableName NVARCHAR(128));\n\
                   INSERT INTO @TargetTables VALUES ('Bill_Record');\n\
                   SELECT TableName FROM @TargetTables;";

        let results = execute_multi_core_with_options_for_client_and_progress_typed(
            &state,
            "conn-1",
            "",
            sql,
            None,
            None,
            QueryExecutionOptions::default(),
            None,
        )
        .await
        .unwrap();

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].result.rows, vec![vec![serde_json::Value::String(sql.to_string())]]);

        runtime.kill();
        drop(state);
        let _ = std::fs::remove_dir_all(dir);
    }

    fn sqlserver_agent_calls(dir: &std::path::Path) -> Vec<serde_json::Value> {
        std::fs::read_to_string(dir.join("calls.jsonl"))
            .unwrap()
            .lines()
            .map(|line| serde_json::from_str(line).unwrap())
            .filter(|request: &serde_json::Value| {
                matches!(request["method"].as_str(), Some("execute_query" | "execute_query_page" | "fetch_query_page"))
            })
            .collect()
    }

    #[tokio::test]
    async fn sqlserver_agent_multi_execution_collects_all_results_without_cursor_or_replay() {
        for page_size in [None, Some(100)] {
            let (state, dir, runtime) = sqlserver_agent_echo_state().await;
            let sql = "SELECT 101 AS first_result; SELECT 202 AS second_result; SELECT 303 AS third_result;";
            let results = execute_multi_core_with_options_for_client_and_progress_typed(
                &state,
                "conn-1",
                "",
                sql,
                None,
                None,
                QueryExecutionOptions { max_rows: Some(1), page_size, ..Default::default() },
                None,
            )
            .await
            .unwrap();
            assert_eq!(results.len(), 3);
            assert_eq!(results[0].result.rows, vec![vec![serde_json::json!(101)]]);
            assert!(results[0].result.truncated);
            assert_eq!(results[1].result.rows, vec![vec![serde_json::json!(202)]]);
            assert_eq!(results[2].result.rows, vec![vec![serde_json::json!(303)]]);
            assert!(results.iter().all(|result| result.statement_index.is_none()));
            let calls = sqlserver_agent_calls(&dir);
            assert_eq!(calls.len(), 1);
            assert_eq!(calls[0]["method"], "execute_query");
            assert_eq!(calls[0]["params"]["returnAllResults"], true);
            assert_eq!(calls[0]["params"]["sql"], sql);
            assert_eq!(calls[0]["params"]["maxRows"], 1);
            runtime.kill();
            drop(state);
            let _ = std::fs::remove_dir_all(dir);
        }
    }

    #[tokio::test]
    async fn sqlserver_agent_multi_execution_keeps_go_batches_and_variable_scope() {
        let (state, dir, runtime) = sqlserver_agent_echo_state().await;
        let first = "DECLARE @n INT; SET @n=1; SELECT @n;";
        let second = "SELECT 2 AS n;";
        let results = execute_multi_core_with_options_for_client_and_progress_typed(
            &state,
            "conn-1",
            "",
            &format!("{first}\nGO\n{second}"),
            None,
            None,
            QueryExecutionOptions::default(),
            None,
        )
        .await
        .unwrap();
        assert_eq!(results.len(), 2);
        let calls = sqlserver_agent_calls(&dir);
        assert_eq!(calls.len(), 2);
        assert_eq!(calls[0]["params"]["sql"], first);
        assert_eq!(calls[1]["params"]["sql"], second);
        assert!(calls.iter().all(|call| call["params"]["returnAllResults"] == true));
        runtime.kill();
        drop(state);
        let _ = std::fs::remove_dir_all(dir);
    }

    #[tokio::test]
    async fn sqlserver_agent_multi_execution_preserves_errors_and_go_continue_policy() {
        for continue_on_error in [false, true] {
            let (state, dir, runtime) = sqlserver_agent_echo_state().await;
            let results = execute_multi_core_with_options_for_client_and_progress_typed(
                &state,
                "conn-1",
                "",
                "SELECT 1; RAISERROR('late',16,1);\nGO\nSELECT 2;",
                None,
                None,
                QueryExecutionOptions { continue_on_error, ..Default::default() },
                None,
            )
            .await
            .unwrap();
            assert!(results[0].execution_error);
            assert!(results[0].result.rows[0][0].as_str().unwrap().contains("late SQL Server batch error"));
            assert!(results[0].error.is_some());
            assert_eq!(results.len(), if continue_on_error { 2 } else { 1 });
            assert_eq!(sqlserver_agent_calls(&dir).len(), if continue_on_error { 2 } else { 1 });
            assert!(matches!(state.pool_handle("conn-1").await, Some(PoolKind::Agent(_))));
            runtime.kill();
            drop(state);
            let _ = std::fs::remove_dir_all(dir);
        }
    }

    #[tokio::test]
    async fn sqlserver_agent_multi_execution_preserves_single_query_and_fetch_cursors() {
        for session_id in [None, Some("cursor-1".to_string())] {
            let (state, dir, runtime) = sqlserver_agent_echo_state().await;
            let results = execute_multi_core_with_options_for_client_and_progress_typed(
                &state,
                "conn-1",
                "",
                "SELECT 1 AS n;",
                None,
                None,
                QueryExecutionOptions {
                    page_size: Some(100),
                    result_session_id: session_id.clone(),
                    ..Default::default()
                },
                None,
            )
            .await
            .unwrap();
            assert_eq!(results.len(), 1);
            let calls = sqlserver_agent_calls(&dir);
            assert_eq!(calls.len(), 1);
            assert_eq!(
                calls[0]["method"],
                if session_id.is_some() { "fetch_query_page" } else { "execute_query_page" }
            );
            assert!(calls[0]["params"].get("returnAllResults").is_none());
            runtime.kill();
            drop(state);
            let _ = std::fs::remove_dir_all(dir);
        }
    }

    #[tokio::test]
    async fn sqlserver_agent_multi_execution_preserves_read_only_and_pre_dispatch_cancel() {
        for read_only in [true, false] {
            let (state, dir, runtime) = sqlserver_agent_echo_state().await;
            state.configs.write().await.get_mut("conn-1").unwrap().read_only = read_only;
            let cancel = CancellationToken::new();
            if !read_only {
                cancel.cancel();
            }
            let results = execute_multi_core_with_options_for_client_and_progress_typed(
                &state,
                "conn-1",
                "",
                "SELECT 1; UPDATE users SET n=2;",
                None,
                Some(cancel),
                QueryExecutionOptions::default(),
                None,
            )
            .await
            .unwrap();
            assert_eq!(results.len(), 1);
            assert!(results[0].execution_error);
            assert!(sqlserver_agent_calls(&dir).is_empty());
            runtime.kill();
            drop(state);
            let _ = std::fs::remove_dir_all(dir);
        }
    }

    #[tokio::test]
    async fn sqlserver_agent_write_paths_do_not_request_schema_switch() {
        let (state, dir, runtime) = sqlserver_agent_echo_state().await;
        let statements = ["UPDATE [dbo].[users] SET [active] = 1 WHERE [id] = 7".to_string()];

        let batch = execute_statements(&state, "conn-1", "", &statements, Some("dbo"), None).await.unwrap();
        let transaction =
            execute_statements_in_transaction_on_pool(&state, "conn-1", "conn-1", "", &statements, Some("dbo"), None)
                .await
                .unwrap();

        assert_eq!(batch.affected_rows, 1);
        assert_eq!(transaction.affected_rows, 1);

        runtime.kill();
        drop(state);
        let _ = std::fs::remove_dir_all(dir);
    }

    async fn agent_error_state(
        disposition: &str,
    ) -> (AppState, std::path::PathBuf, std::sync::Arc<crate::db::agent_driver::AgentRuntimeClient>) {
        let dir = std::env::temp_dir().join(format!("dbx-query-agent-error-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&dir).unwrap();
        let script_path = dir.join("agent.py");
        std::fs::write(
            &script_path,
            format!(
                r#"import json, sys
print(json.dumps({{'ready': True}}), flush=True)
for line in sys.stdin:
    req = json.loads(line)
    if req['method'] == 'handshake':
        response = {{
            'jsonrpc': '2.0',
            'id': req['id'],
            'result': {{'protocolVersion': 2, 'agentProtocolVersion': 2, 'capabilities': ['multi_session']}}
        }}
    elif req['method'] in ('execute_query', 'execute_batch', 'execute_transaction'):
        response = {{
            'jsonrpc': '2.0',
            'id': req['id'],
            'error': {{
                'code': -1,
                'message': 'injected Agent failure',
                'data': {{
                    'category': 'resource',
                    'retryable': False,
                    'sessionDisposition': '{disposition}',
                    'stage': 'execute'
                }}
            }}
        }}
    else:
        response = {{'jsonrpc': '2.0', 'id': req['id'], 'result': {{}}}}
    print(json.dumps(response), flush=True)
"#
            ),
        )
        .unwrap();

        let python = if cfg!(windows) { "python" } else { "python3" };
        let runtime = crate::db::agent_driver::AgentRuntimeClient::spawn(
            crate::db::agent_driver::AgentLaunchSpec::new(python)
                .with_args([script_path.to_string_lossy().to_string()]),
            "test",
        )
        .await
        .unwrap();
        runtime.increment_session_count();

        let storage = crate::persistence::test_storage::open(&dir.join("storage.db")).await.unwrap();
        let state = AppState::new(storage);
        state.configs.write().await.insert("conn-1".to_string(), test_connection_config(DatabaseType::Dameng));
        state
            .update_connection_pools(|connections| {
                connections.insert(
                    "conn-1".to_string(),
                    PoolKind::agent(crate::db::agent_driver::AgentDriverClient::shared_session(
                        runtime.clone(),
                        "session-1".to_string(),
                    )),
                );
            })
            .await;

        (state, dir, runtime)
    }

    #[tokio::test]
    async fn agent_query_replace_runtime_error_detaches_pool_and_stops_runtime() {
        let (state, dir, runtime) = agent_error_state("replace_runtime").await;

        let error = execute_sql_statement(&state, "conn-1", "", "SELECT 1", None, None).await.unwrap_err();

        assert!(error.contains("injected Agent failure"));
        assert!(state.pool_handle("conn-1").await.is_none());
        assert!(runtime.is_failed());

        runtime.kill();
        let _ = std::fs::remove_dir_all(dir);
    }

    #[tokio::test]
    async fn single_statement_multi_core_keeps_query_error_typed() {
        let (state, dir, runtime) = agent_error_state("keep").await;

        let error = execute_multi_core_with_options_for_client_and_progress_typed(
            &state,
            "conn-1",
            "",
            "SELECT 1",
            None,
            None,
            QueryExecutionOptions::default(),
            None,
        )
        .await
        .unwrap_err();

        assert!(matches!(error, QueryExecutionError::Agent(_)));

        runtime.kill();
        let _ = std::fs::remove_dir_all(dir);
    }

    #[tokio::test]
    async fn native_pre_dispatch_cancellation_stays_typed() {
        let dir = std::env::temp_dir().join(format!("dbx-query-native-cancel-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&dir).unwrap();
        let storage = crate::persistence::test_storage::open(&dir.join("storage.db")).await.unwrap();
        let state = AppState::new(storage);
        let connection_id = "sqlite-cancel";
        let sqlite = db::sqlite::connect_path_create_if_missing(dir.join("query.db").to_str().unwrap()).await.unwrap();
        state
            .update_connection_pools(|connections| {
                connections.insert(connection_id.to_string(), PoolKind::Sqlite(sqlite));
            })
            .await;
        state.configs.write().await.insert(connection_id.to_string(), test_connection_config(DatabaseType::Sqlite));
        let cancel_token = CancellationToken::new();
        cancel_token.cancel();

        let error = execute_sql_statement_with_options_typed(
            &state,
            connection_id,
            "",
            "SELECT 1",
            None,
            Some(cancel_token),
            QueryExecutionOptions::default(),
        )
        .await
        .unwrap_err();

        assert!(matches!(
            &error,
            QueryExecutionError::Canceled {
                stage: AgentErrorStage::Request,
                operation_outcome: AgentOperationOutcome::NotStarted,
            }
        ));
        let backend_error = error.into_backend_error();
        assert_eq!(backend_error.code(), "DBX-JDBC-2003");
        assert_eq!(backend_error.source(), crate::backend_error::BackendErrorSource::LegacyBackend);
        assert_eq!(backend_error.operation_outcome(), crate::backend_error::BackendOperationOutcome::NotStarted);

        let _ = std::fs::remove_dir_all(dir);
    }

    #[tokio::test]
    async fn agent_pre_dispatch_cancellation_stays_local_and_typed() {
        let (state, dir, runtime) = agent_error_state("keep").await;
        let cancel_token = CancellationToken::new();
        cancel_token.cancel();

        let error = execute_sql_statement_with_options_typed(
            &state,
            "conn-1",
            "",
            "SELECT 1",
            None,
            Some(cancel_token),
            QueryExecutionOptions::default(),
        )
        .await
        .unwrap_err();

        assert!(matches!(
            &error,
            QueryExecutionError::Canceled {
                stage: AgentErrorStage::Request,
                operation_outcome: AgentOperationOutcome::NotStarted,
            }
        ));
        let backend_error = error.into_backend_error();
        assert_eq!(backend_error.code(), "DBX-JDBC-2003");
        assert_eq!(backend_error.source(), crate::backend_error::BackendErrorSource::LegacyBackend);
        assert_eq!(backend_error.operation_outcome(), crate::backend_error::BackendOperationOutcome::NotStarted);
        assert!(!runtime.is_failed());

        runtime.kill();
        let _ = std::fs::remove_dir_all(dir);
    }

    #[tokio::test]
    async fn agent_transaction_replace_runtime_error_detaches_pool_and_stops_runtime() {
        let (state, dir, runtime) = agent_error_state("replace_runtime").await;

        let error = execute_statements_in_transaction_on_pool(
            &state,
            "conn-1",
            "conn-1",
            "",
            &["UPDATE test_table SET value = 1".to_string()],
            None,
            None,
        )
        .await
        .unwrap_err();

        assert!(error.contains("injected Agent failure"));
        assert!(state.pool_handle("conn-1").await.is_none());
        assert!(runtime.is_failed());

        runtime.kill();
        let _ = std::fs::remove_dir_all(dir);
    }

    #[tokio::test]
    async fn agent_batch_replace_runtime_error_detaches_pool_and_stops_runtime() {
        let (state, dir, runtime) = agent_error_state("replace_runtime").await;

        let error =
            execute_statements(&state, "conn-1", "", &["UPDATE test_table SET value = 1".to_string()], None, None)
                .await
                .unwrap_err();

        assert!(error.contains("injected Agent failure"));
        assert!(state.pool_handle("conn-1").await.is_none());
        assert!(runtime.is_failed());

        runtime.kill();
        let _ = std::fs::remove_dir_all(dir);
    }

    #[tokio::test]
    async fn agent_quarantine_error_removes_only_target_pool() {
        let (state, dir, runtime) = agent_error_state("quarantine").await;

        let error = execute_sql_statement(&state, "conn-1", "", "SELECT 1", None, None).await.unwrap_err();

        assert!(error.contains("injected Agent failure"));
        assert!(state.pool_handle("conn-1").await.is_none());
        assert!(!runtime.is_failed());

        runtime.kill();
        let _ = std::fs::remove_dir_all(dir);
    }

    struct FakeMysqlBatchExecutor {
        outcomes: std::collections::VecDeque<Result<Vec<db::mysql::MySqlQueryResult>, String>>,
        executed: Vec<String>,
    }

    impl MysqlBatchStatementExecutor for FakeMysqlBatchExecutor {
        async fn execute_statement(&mut self, statement: &str) -> Result<Vec<db::mysql::MySqlQueryResult>, String> {
            self.executed.push(statement.to_string());
            self.outcomes.pop_front().expect("test outcome for statement")
        }
    }

    fn mysql_query_result(result: db::QueryResult) -> db::mysql::MySqlQueryResult {
        db::mysql::MySqlQueryResult { result, large_value_cells: Vec::new() }
    }

    fn mysql_batch_result(result: db::QueryResult) -> Result<Vec<db::mysql::MySqlQueryResult>, String> {
        Ok(vec![mysql_query_result(result)])
    }

    struct FakePipelinedMysqlBatchExecutor {
        batch_outcomes: std::collections::VecDeque<db::mysql::MySqlNonResultBatchOutcome>,
        statement_outcomes: std::collections::VecDeque<Result<Vec<db::mysql::MySqlQueryResult>, String>>,
        batches: Vec<Vec<String>>,
        statements: Vec<String>,
    }

    impl MysqlBatchStatementExecutor for FakePipelinedMysqlBatchExecutor {
        async fn execute_statement(&mut self, statement: &str) -> Result<Vec<db::mysql::MySqlQueryResult>, String> {
            self.statements.push(statement.to_string());
            self.statement_outcomes.pop_front().expect("test outcome for single statement")
        }

        async fn execute_non_result_batch(
            &mut self,
            statements: &[String],
            on_result: &mut (dyn FnMut(usize, &db::QueryResult) + Send),
        ) -> db::mysql::MySqlNonResultBatchOutcome {
            self.batches.push(statements.to_vec());
            let outcome = self.batch_outcomes.pop_front().expect("test outcome for pipelined statements");
            for (statement_index, result) in outcome.results.iter().enumerate() {
                on_result(statement_index, result);
            }
            outcome
        }
    }

    #[tokio::test]
    async fn ddl_schema_cache_invalidates_persisted_object_snapshots() {
        let dir = tempfile::tempdir().unwrap();
        let storage = crate::persistence::test_storage::open(&dir.path().join("storage.db")).await.unwrap();
        let state = AppState::new(storage);
        let connection_id = "schema-cache";
        let sqlite =
            db::sqlite::connect_path_create_if_missing(dir.path().join("query.db").to_str().unwrap()).await.unwrap();
        state
            .update_connection_pools(|pools| {
                pools.insert(connection_id.to_string(), PoolKind::Sqlite(sqlite));
            })
            .await;
        state.configs.write().await.insert(connection_id.to_string(), test_connection_config(DatabaseType::Sqlite));
        let keys = [
            "object-meta:v1:schema-cache:db:public:users::TABLE:columns:",
            "object-meta:v1:schema-cache:db:public:users::backend-columns:",
            "object-ddl:v1:schema-cache:db:public:users::TABLE:",
        ];
        for key in keys {
            state.storage.save_schema_cache(key, &serde_json::json!({"old": true})).await.unwrap();
        }
        let unrelated = "object-meta:v1:schema-cache-other:db:public:users::backend-columns:";
        state.storage.save_schema_cache(unrelated, &serde_json::json!([])).await.unwrap();
        execute_sql_statement(&state, connection_id, "", "SELECT 1", None, None).await.unwrap();
        for key in keys {
            assert!(state.storage.load_schema_cache(key).await.unwrap().is_some());
        }
        execute_sql_statement(&state, connection_id, "", "CREATE TABLE users (id INTEGER)", None, None).await.unwrap();
        for key in keys {
            assert!(state.storage.load_schema_cache(key).await.unwrap().is_none(), "stale cache: {key}");
        }
        assert!(state.storage.load_schema_cache(unrelated).await.unwrap().is_some());
    }

    #[tokio::test]
    async fn ddl_schema_cache_batch_outcomes_and_transaction_completion() {
        for (sql, use_transaction, expected_error, expected_column) in [
            ("ALTER TABLE users ADD COLUMN added INTEGER; SELECT 1", false, false, true),
            ("ALTER TABLE users ADD COLUMN added INTEGER; SELECT * FROM missing_table", false, true, true),
            ("ALTER TABLE users ADD COLUMN added INTEGER; SELECT 1", true, false, true),
            ("ALTER TABLE users ADD COLUMN added INTEGER; SELECT * FROM missing_table", true, true, false),
            ("BEGIN; ALTER TABLE users ADD COLUMN added INTEGER; COMMIT", false, false, true),
        ] {
            let dir = tempfile::tempdir().unwrap();
            let storage = crate::persistence::test_storage::open(&dir.path().join("storage.db")).await.unwrap();
            let state = AppState::new(storage);
            let sqlite = db::sqlite::connect_path_create_if_missing(dir.path().join("query.db").to_str().unwrap())
                .await
                .unwrap();
            state
                .update_connection_pools(|pools| {
                    pools.insert("cache".into(), PoolKind::Sqlite(sqlite));
                })
                .await;
            state.configs.write().await.insert("cache".into(), test_connection_config(DatabaseType::Sqlite));
            execute_sql_statement(&state, "cache", "", "CREATE TABLE users (id INTEGER)", None, None).await.unwrap();
            let key = "object-meta:v1:cache:db:public:users::backend-columns:";
            state.storage.save_schema_cache(key, &serde_json::json!([])).await.unwrap();
            let result = execute_multi_core_with_options(
                &state,
                "cache",
                "",
                sql,
                None,
                None,
                QueryExecutionOptions { use_transaction: Some(use_transaction), ..Default::default() },
            )
            .await;
            let failed = match &result {
                Err(_) => true,
                Ok(results) => results.iter().any(|r| r.columns == vec!["Error"]),
            };
            assert_eq!(failed, expected_error, "{sql}");
            assert!(state.storage.load_schema_cache(key).await.unwrap().is_none(), "{sql}");
            let columns =
                execute_sql_statement(&state, "cache", "", "PRAGMA table_info(users)", None, None).await.unwrap();
            assert_eq!(columns.rows.iter().any(|row| row[1] == "added"), expected_column, "{sql}");
            // A failed DDL still keeps its execution error while clearing cache.
            state.storage.save_schema_cache(key, &serde_json::json!([])).await.unwrap();
            let error = execute_sql_statement(
                &state,
                "cache",
                "",
                "ALTER TABLE missing_table ADD COLUMN x INTEGER",
                None,
                None,
            )
            .await
            .unwrap_err();
            assert!(error.contains("missing_table"));
            assert!(state.storage.load_schema_cache(key).await.unwrap().is_none());
            for sql in ["BEGIN", "COMMIT", "BEGIN", "ROLLBACK"] {
                state.storage.save_schema_cache(key, &serde_json::json!([])).await.unwrap();
                execute_sql_statement(&state, "cache", "", sql, None, None).await.unwrap();
                assert_eq!(state.storage.load_schema_cache(key).await.unwrap().is_some(), sql == "BEGIN");
            }
        }
    }

    #[tokio::test]
    async fn ddl_schema_cache_storage_failure_preserves_sql_outcome() {
        let dir = tempfile::tempdir().unwrap();
        let storage_path = dir.path().join("storage.db");
        let state = AppState::new(crate::persistence::test_storage::open(&storage_path).await.unwrap());
        let sqlite =
            db::sqlite::connect_path_create_if_missing(dir.path().join("query.db").to_str().unwrap()).await.unwrap();
        state
            .update_connection_pools(|pools| {
                pools.insert("cache".into(), PoolKind::Sqlite(sqlite));
            })
            .await;
        state.configs.write().await.insert("cache".into(), test_connection_config(DatabaseType::Sqlite));
        let key = "object-meta:v1:cache:db:public:users::backend-columns:";
        state.storage.save_schema_cache(key, &serde_json::json!([])).await.unwrap();
        // Fail only deletion; SQL execution and cache reads remain available.
        let fault = rusqlite::Connection::open(&storage_path).unwrap();
        fault.execute_batch("CREATE TRIGGER reject_cache_delete BEFORE DELETE ON schema_cache BEGIN SELECT RAISE(FAIL, 'injected cache delete failure'); END;").unwrap();
        execute_sql_statement(&state, "cache", "", "CREATE TABLE users (id INTEGER)", None, None).await.unwrap();
        assert!(state.storage.load_schema_cache(key).await.unwrap().is_some());
        let result = execute_sql_statement(&state, "cache", "", "SELECT id FROM users", None, None).await.unwrap();
        assert_eq!(result.columns, vec!["id"]);
        let error =
            execute_sql_statement(&state, "cache", "", "ALTER TABLE missing_table ADD COLUMN x INTEGER", None, None)
                .await
                .unwrap_err();
        assert!(error.contains("missing_table"));
        assert!(!error.contains("injected cache delete failure"));
    }

    async fn assert_sqlite_batch_error_behavior(failure_first: bool, continue_on_error: bool) {
        let dir = std::env::temp_dir().join(format!("dbx-query-batch-error-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&dir).unwrap();
        let storage = crate::persistence::test_storage::open(&dir.join("storage.db")).await.unwrap();
        let state = AppState::new(storage);
        let connection_id = "sqlite-batch";
        let sqlite = db::sqlite::connect_path_create_if_missing(dir.join("query.db").to_str().unwrap()).await.unwrap();
        state
            .update_connection_pools(|connections| {
                connections.insert(connection_id.to_string(), PoolKind::Sqlite(sqlite));
            })
            .await;
        state.configs.write().await.insert(connection_id.to_string(), test_connection_config(DatabaseType::Sqlite));

        let sql = if failure_first {
            "INSERT INTO missing_table VALUES (1); CREATE TABLE executed_after_error (id INTEGER);"
        } else {
            "CREATE TABLE before_error (id INTEGER); INSERT INTO missing_table VALUES (1); CREATE TABLE executed_after_error (id INTEGER);"
        };
        let results = execute_multi_core_with_options(
            &state,
            connection_id,
            "",
            sql,
            None,
            None,
            QueryExecutionOptions { continue_on_error, ..Default::default() },
        )
        .await
        .unwrap();
        let error_index = usize::from(!failure_first);
        assert_eq!(results[error_index].columns, vec!["Error"]);
        assert_eq!(
            results.len(),
            if failure_first { 1 + usize::from(continue_on_error) } else { 2 + usize::from(continue_on_error) }
        );

        let table_check = execute_sql_statement(
            &state,
            connection_id,
            "",
            "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'executed_after_error'",
            None,
            None,
        )
        .await
        .unwrap();
        assert_eq!(!table_check.rows.is_empty(), continue_on_error);
    }

    #[tokio::test]
    async fn transactional_sqlite_batch_rolls_back_when_a_later_statement_fails() {
        let dir = std::env::temp_dir().join(format!("dbx-query-transaction-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&dir).unwrap();
        let storage = crate::persistence::test_storage::open(&dir.join("storage.db")).await.unwrap();
        let state = AppState::new(storage);
        let connection_id = "sqlite-transaction";
        let sqlite = db::sqlite::connect_path_create_if_missing(dir.join("query.db").to_str().unwrap()).await.unwrap();
        state
            .update_connection_pools(|pools| {
                pools.insert(connection_id.to_string(), PoolKind::Sqlite(sqlite));
            })
            .await;
        state.configs.write().await.insert(connection_id.to_string(), test_connection_config(DatabaseType::Sqlite));

        let error = execute_multi_core_with_options(
            &state,
            connection_id,
            "",
            "CREATE TABLE rolled_back_table (id INTEGER); INSERT INTO missing_table VALUES (1);",
            None,
            None,
            QueryExecutionOptions { use_transaction: Some(true), ..Default::default() },
        )
        .await
        .unwrap_err();
        assert!(error.contains("missing_table"), "unexpected transaction error: {error}");

        let table_check = execute_sql_statement(
            &state,
            connection_id,
            "",
            "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'rolled_back_table'",
            None,
            None,
        )
        .await
        .unwrap();
        assert!(table_check.rows.is_empty(), "the failed batch must roll back its preceding DDL");

        let _ = std::fs::remove_dir_all(dir);
    }

    #[tokio::test]
    async fn transactional_batch_rejects_an_unsupported_backend_before_execution() {
        let dir = std::env::temp_dir().join(format!("dbx-query-unsupported-transaction-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&dir).unwrap();
        let storage = crate::persistence::test_storage::open(&dir.join("storage.db")).await.unwrap();
        let state = AppState::new(storage);
        let connection_id = "message-queue-transaction";
        state
            .update_connection_pools(|pools| {
                pools.insert(connection_id.to_string(), PoolKind::MessageQueue);
            })
            .await;
        state.configs.write().await.insert(connection_id.to_string(), test_connection_config(DatabaseType::Redis));

        let error = execute_multi_core_with_options(
            &state,
            connection_id,
            "",
            "INSERT INTO first_statement VALUES (1); INSERT INTO second_statement VALUES (2);",
            None,
            None,
            QueryExecutionOptions { use_transaction: Some(true), ..Default::default() },
        )
        .await
        .unwrap_err();
        assert!(error.contains("cannot provide a rollbackable transaction"), "unexpected transaction error: {error}");

        let _ = std::fs::remove_dir_all(dir);
    }

    #[test]
    fn agent_execute_batch_unsupported_detects_case_insensitive_method_errors() {
        assert!(is_agent_execute_batch_unsupported("Agent RPC error (-1): unknown method: execute_batch"));
        assert!(is_agent_execute_batch_unsupported("Agent RPC error (-1): Unknown method: execute_batch"));
        assert!(is_agent_execute_batch_unsupported("Agent RPC error (-32601): Method not found: execute_batch"));
    }

    #[test]
    fn agent_execute_batch_unsupported_ignores_unrelated_errors() {
        assert!(!is_agent_execute_batch_unsupported("ORA-00955: name is already used by an existing object"));
        assert!(!is_agent_execute_batch_unsupported("Agent RPC error (-1): unknown method: execute_query"));
    }

    #[test]
    fn batch_transaction_ddl_is_unrollbackable_covers_oracle_and_mysql_family() {
        let ddl = vec!["CREATE TABLE test_table (id INT)".to_string()];
        let dml = vec!["INSERT INTO test_table VALUES (1)".to_string()];

        // MySQL-family and Oracle DDL implicitly commit; explicit transactions
        // over such DDL cannot be rolled back, so use_transaction is rejected.
        for db in [DatabaseType::Mysql, DatabaseType::Goldendb, DatabaseType::Oracle] {
            assert!(batch_transaction_ddl_is_unrollbackable(Some(db), &ddl), "expected {db:?} to reject DDL");
        }
        assert!(!batch_transaction_ddl_is_unrollbackable(Some(DatabaseType::Mysql), &dml));
        // Postgres and SQLite have transactional DDL and must both be allowed.
        assert!(!batch_transaction_ddl_is_unrollbackable(Some(DatabaseType::Postgres), &ddl));
        assert!(!batch_transaction_ddl_is_unrollbackable(Some(DatabaseType::Sqlite), &ddl));
        // An unknown db type cannot be verified — do not risk rejecting valid DDL batches.
        assert!(!batch_transaction_ddl_is_unrollbackable(None, &ddl));
    }

    #[test]
    fn batch_has_concurrently_statement_detects_transaction_block_escapees() {
        assert!(batch_has_concurrently_statement(&["CREATE INDEX CONCURRENTLY idx ON t (id)".to_string(),]));
        assert!(batch_has_concurrently_statement(&[
            "ALTER TABLE parent DETACH PARTITION child CONCURRENTLY".to_string(),
        ]));
        // Case-insensitive, and a single CONCURRENTLY anywhere in the batch is enough.
        assert!(batch_has_concurrently_statement(&[
            "ALTER TABLE t ADD COLUMN c int".to_string(),
            "drop index concurrently idx".to_string(),
        ]));
        assert!(!batch_has_concurrently_statement(&[
            "CREATE TABLE child PARTITION OF parent FOR VALUES FROM (0) TO (1)".to_string(),
        ]));
        // The word inside literals or plain identifiers must not demote a batch
        // that would otherwise run in one transaction.
        assert!(!batch_has_concurrently_statement(&[
            "INSERT INTO notes (body) VALUES ('run CREATE INDEX CONCURRENTLY later')".to_string(),
        ]));
        assert!(!batch_has_concurrently_statement(&[
            "SELECT id FROM concurrently WHERE label = 'drop index concurrently idx'".to_string(),
        ]));
        assert!(!batch_has_concurrently_statement(&[
            "-- refresh materialized view concurrently next".to_string(),
            "SELECT 1".to_string(),
        ]));
        // REINDEX/REFRESH forms still detect.
        assert!(batch_has_concurrently_statement(&["REINDEX TABLE CONCURRENTLY t".to_string()]));
        assert!(batch_has_concurrently_statement(&["REFRESH MATERIALIZED VIEW CONCURRENTLY mv".to_string()]));
    }

    #[tokio::test]
    async fn connection_pool_is_sqlserver_agent_detects_agent_and_native_pools() {
        let dir = std::env::temp_dir().join(format!("dbx-query-sqlserver-agent-flag-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&dir).unwrap();
        let storage = crate::persistence::test_storage::open(&dir.join("storage.db")).await.unwrap();
        let state = AppState::new(storage);

        // Non-SQL-Server connections never use the SQL Server agent splitter.
        state.configs.write().await.insert("pg".to_string(), test_connection_config(DatabaseType::Postgres));
        assert!(!connection_pool_is_sqlserver_agent(&state, "pg", "").await);

        // SQL Server with no pool yet defaults to the native (non-agent) splitter.
        state.configs.write().await.insert("mssql".to_string(), test_connection_config(DatabaseType::SqlServer));
        assert!(!connection_pool_is_sqlserver_agent(&state, "mssql", "").await);

        // A SQL Server connection backed by the agent driver is detected, so the
        // MCP pre-check uses the same GO-batch splitter as the core.
        state
            .update_connection_pools(|pools| {
                pools.insert(
                    "mssql".to_string(),
                    PoolKind::agent(crate::db::agent_driver::AgentDriverClient::test_stub()),
                );
            })
            .await;
        assert!(connection_pool_is_sqlserver_agent(&state, "mssql", "").await);

        let _ = std::fs::remove_dir_all(dir);
    }

    #[tokio::test]
    async fn transaction_kernel_dispatches_by_backend_capability_not_ddl_content() {
        // Regression for the maintainer review: the unrollbackable-DDL cap now lives
        // only in the opt-in use_transaction entry point. The shared transaction
        // kernel dispatches on the backend's transaction capability, so the
        // use_transaction error never leaks to callers that share the kernel but do
        // not set it (schema-diff deploy, imports), which document a mixed outcome
        // on failure for DDL that cannot be rolled back.
        let dir = std::env::temp_dir().join(format!("dbx-query-tx-predispatch-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&dir).unwrap();
        let storage = crate::persistence::test_storage::open(&dir.join("storage.db")).await.unwrap();
        let state = AppState::new(storage);
        let connection_id = "message-queue-tx-ddl";
        state
            .update_connection_pools(|pools| {
                pools.insert(connection_id.to_string(), PoolKind::MessageQueue);
            })
            .await;
        state.configs.write().await.insert(connection_id.to_string(), test_connection_config(DatabaseType::Redis));

        let error = execute_statements_in_transaction_on_pool(
            &state,
            connection_id,
            connection_id,
            "",
            &["CREATE TABLE t (id INT)".to_string()],
            None,
            None,
        )
        .await
        .unwrap_err();

        assert!(
            error.contains("cannot provide a rollbackable transaction"),
            "expected a backend-capability reject, got: {error}"
        );
        assert!(
            !error.contains("whose DDL cannot be rolled back"),
            "the opt-in use_transaction DDL cap leaked into the shared transaction kernel: {error}"
        );

        let _ = std::fs::remove_dir_all(dir);
    }

    #[test]
    fn query_pool_error_policy_retries_reads_but_not_writes() {
        assert_eq!(
            query_pool_error_action(Some(DatabaseType::Postgres), "SELECT * FROM users", "connection reset by peer"),
            PoolErrorAction::ReconnectAndRetry
        );
        assert_eq!(
            query_pool_error_action(
                Some(DatabaseType::Postgres),
                "UPDATE users SET active = true",
                "connection reset by peer"
            ),
            PoolErrorAction::Discard
        );
    }

    #[tokio::test]
    async fn sqlite_batch_stops_when_the_first_statement_fails() {
        assert_sqlite_batch_error_behavior(true, false).await;
    }

    #[tokio::test]
    async fn sqlite_batch_continues_when_the_first_statement_fails_and_enabled() {
        assert_sqlite_batch_error_behavior(true, true).await;
    }

    #[tokio::test]
    async fn sqlite_batch_stops_when_a_middle_statement_fails() {
        assert_sqlite_batch_error_behavior(false, false).await;
    }

    #[tokio::test]
    async fn sqlite_batch_continues_when_a_middle_statement_fails_and_enabled() {
        assert_sqlite_batch_error_behavior(false, true).await;
    }

    #[tokio::test]
    async fn postgres_family_on_error_stop_overrides_continue_on_error() {
        for db_type in [DatabaseType::Postgres, DatabaseType::OpenGauss, DatabaseType::Gaussdb] {
            assert_psql_on_error_stop_overrides_continue_on_error(db_type).await;
        }
    }

    async fn assert_psql_on_error_stop_overrides_continue_on_error(db_type: DatabaseType) {
        let dir = std::env::temp_dir().join(format!("dbx-query-gaussdb-on-error-stop-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&dir).unwrap();
        let storage = crate::persistence::test_storage::open(&dir.join("storage.db")).await.unwrap();
        let state = AppState::new(storage);
        let connection_id = "gaussdb-on-error-stop";
        let sqlite = db::sqlite::connect_path_create_if_missing(dir.join("query.db").to_str().unwrap()).await.unwrap();
        state
            .update_connection_pools(|connections| {
                connections.insert(connection_id.to_string(), PoolKind::Sqlite(sqlite));
            })
            .await;
        state.configs.write().await.insert(connection_id.to_string(), test_connection_config(db_type));

        let results = execute_multi_core_with_options(
            &state,
            connection_id,
            "",
            "\\set ON_ERROR_STOP on\nINSERT INTO missing_table VALUES (1); CREATE TABLE must_not_run (id INTEGER);",
            None,
            None,
            QueryExecutionOptions { continue_on_error: true, ..Default::default() },
        )
        .await
        .unwrap();

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].columns, vec!["Error"]);
        let table_check = execute_sql_statement(
            &state,
            connection_id,
            "",
            "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'must_not_run'",
            None,
            None,
        )
        .await
        .unwrap();
        assert!(table_check.rows.is_empty());

        drop(state);
        let _ = std::fs::remove_dir_all(dir);
    }

    #[tokio::test]
    async fn mysql_batch_stops_after_the_first_statement_error() {
        let statements = vec!["first".to_string(), "fails".to_string(), "must-not-run".to_string()];
        let mut executor = FakeMysqlBatchExecutor {
            outcomes: std::collections::VecDeque::from([
                mysql_batch_result(empty_query_result(0)),
                Err("Duplicate entry".to_string()),
                mysql_batch_result(empty_query_result(0)),
            ]),
            executed: Vec::new(),
        };

        let (results, error_action) = execute_mysql_batch_statements(
            &mut executor,
            &statements,
            Some(DatabaseType::Mysql),
            db::mysql::MySqlQueryDialect::default(),
            None,
            false,
            None,
            None,
        )
        .await;

        assert_eq!(executor.executed, vec!["first", "fails"]);
        assert_eq!(results.len(), 2);
        assert_eq!(results[0].statement_index, Some(0));
        assert_eq!(results[1].statement_index, Some(1));
        assert!(results[1].execution_error);
        assert_eq!(error_action, Some(PoolErrorAction::Keep));
    }

    #[tokio::test]
    async fn mysql_batch_reports_progress_for_each_completed_statement() {
        let statements = vec!["first".to_string(), "fails".to_string(), "must-not-run".to_string()];
        let mut executor = FakeMysqlBatchExecutor {
            outcomes: std::collections::VecDeque::from([
                mysql_batch_result(empty_query_result(0)),
                Err("Duplicate entry".to_string()),
            ]),
            executed: Vec::new(),
        };
        let progress_events = Arc::new(std::sync::Mutex::new(Vec::new()));
        let progress: ExecuteMultiProgressCallback = {
            let progress_events = Arc::clone(&progress_events);
            Arc::new(move |event| progress_events.lock().unwrap().push(event))
        };

        let (results, error_action) = execute_mysql_batch_statements(
            &mut executor,
            &statements,
            Some(DatabaseType::Mysql),
            db::mysql::MySqlQueryDialect::default(),
            None,
            false,
            None,
            Some(&progress),
        )
        .await;

        assert_eq!(executor.executed, vec!["first", "fails"]);
        assert_eq!(results.len(), 2);
        assert_eq!(
            *progress_events.lock().unwrap(),
            vec![
                ExecuteMultiProgress {
                    statement_index: 0,
                    completed: 1,
                    total: 3,
                    success: true,
                    execution_time_ms: 0,
                    affected_rows: 0,
                    error: None,
                },
                ExecuteMultiProgress {
                    statement_index: 1,
                    completed: 2,
                    total: 3,
                    success: false,
                    execution_time_ms: 0,
                    affected_rows: 0,
                    error: Some(crate::backend_error::BackendError::from_sql_detail("Duplicate entry")),
                },
            ]
        );
        assert_eq!(error_action, Some(PoolErrorAction::Keep));
    }

    #[tokio::test]
    async fn mysql_batch_pipelines_adjacent_non_result_statements() {
        let statements = vec![
            "SET @batch = 1".to_string(),
            "INSERT INTO users(id) VALUES (1)".to_string(),
            "INSERT INTO users(id) VALUES (2)".to_string(),
            "SELECT COUNT(*) FROM users".to_string(),
        ];
        let mut executor = FakePipelinedMysqlBatchExecutor {
            batch_outcomes: std::collections::VecDeque::from([db::mysql::MySqlNonResultBatchOutcome {
                results: vec![empty_query_result(2), empty_query_result(3)],
                error: None,
            }]),
            statement_outcomes: std::collections::VecDeque::from([
                mysql_batch_result(empty_query_result(1)),
                mysql_batch_result(db::QueryResult {
                    columns: vec!["COUNT(*)".to_string()],
                    column_types: vec!["BIGINT".to_string()],
                    deferred_large_value_cells: Vec::new(),
                    column_sortables: vec![],
                    spatial_columns: vec![],
                    raw_key_cells: Vec::new(),
                    spatial_values: vec![],
                    rows: vec![vec![serde_json::json!(2)]],
                    affected_rows: 0,
                    execution_time_ms: 4,
                    server_execute_time_us: None,
                    query_timings_ms: None,
                    truncated: false,
                    session_id: None,
                    has_more: false,
                    elasticsearch_raw_body: None,
                    messages: Vec::new(),
                }),
            ]),
            batches: Vec::new(),
            statements: Vec::new(),
        };
        let progress_events = Arc::new(std::sync::Mutex::new(Vec::new()));
        let progress: ExecuteMultiProgressCallback = {
            let progress_events = Arc::clone(&progress_events);
            Arc::new(move |event| progress_events.lock().unwrap().push(event))
        };

        let (results, error_action) = execute_mysql_batch_statements(
            &mut executor,
            &statements,
            Some(DatabaseType::Mysql),
            db::mysql::MySqlQueryDialect::default(),
            None,
            false,
            Some(MYSQL_MULTI_STATEMENT_BATCH_MAX_BYTES),
            Some(&progress),
        )
        .await;

        assert_eq!(executor.batches, vec![statements[1..3].to_vec()]);
        assert_eq!(executor.statements, vec![statements[0].clone(), statements[3].clone()]);
        assert_eq!(results.len(), 4);
        assert_eq!(
            results.iter().map(|result| result.statement_index).collect::<Vec<_>>(),
            vec![Some(0), Some(1), Some(2), Some(3)]
        );
        assert_eq!(
            progress_events.lock().unwrap().iter().map(|event| event.completed).collect::<Vec<_>>(),
            vec![1, 2, 3, 4]
        );
        assert_eq!(error_action, None);
    }

    #[tokio::test]
    async fn mysql_pipelined_batch_reports_the_first_failed_statement() {
        let statements = vec![
            "INSERT INTO users(id) VALUES (1)".to_string(),
            "INSERT INTO users(id) VALUES (1)".to_string(),
            "INSERT INTO users(id) VALUES (2)".to_string(),
        ];
        let mut executor = FakePipelinedMysqlBatchExecutor {
            batch_outcomes: std::collections::VecDeque::from([db::mysql::MySqlNonResultBatchOutcome {
                results: vec![empty_query_result(1)],
                error: Some("Duplicate entry".to_string()),
            }]),
            statement_outcomes: std::collections::VecDeque::new(),
            batches: Vec::new(),
            statements: Vec::new(),
        };
        let progress_events = Arc::new(std::sync::Mutex::new(Vec::new()));
        let progress: ExecuteMultiProgressCallback = {
            let progress_events = Arc::clone(&progress_events);
            Arc::new(move |event| progress_events.lock().unwrap().push(event))
        };

        let (results, error_action) = execute_mysql_batch_statements(
            &mut executor,
            &statements,
            Some(DatabaseType::Mysql),
            db::mysql::MySqlQueryDialect::default(),
            None,
            false,
            Some(MYSQL_MULTI_STATEMENT_BATCH_MAX_BYTES),
            Some(&progress),
        )
        .await;

        assert_eq!(executor.batches, vec![statements]);
        assert!(executor.statements.is_empty());
        assert_eq!(results.len(), 2);
        assert_eq!(results[0].statement_index, Some(0));
        assert_eq!(results[1].statement_index, Some(1));
        assert!(results[1].execution_error);
        assert_eq!(results[1].error.as_ref().map(|error| error.code()), Some("DBX-JDBC-4001"));
        assert_eq!(
            progress_events
                .lock()
                .unwrap()
                .iter()
                .map(|event| (event.statement_index, event.success))
                .collect::<Vec<_>>(),
            vec![(0, true), (1, false)]
        );
        assert_eq!(error_action, Some(PoolErrorAction::Keep));
    }

    #[tokio::test]
    async fn mysql_pipelined_batch_discards_a_cancelled_connection() {
        let statements =
            vec!["INSERT INTO users(id) VALUES (1)".to_string(), "INSERT INTO users(id) VALUES (2)".to_string()];
        let mut executor = FakePipelinedMysqlBatchExecutor {
            batch_outcomes: std::collections::VecDeque::from([db::mysql::MySqlNonResultBatchOutcome {
                results: Vec::new(),
                error: Some(QUERY_CANCELED.to_string()),
            }]),
            statement_outcomes: std::collections::VecDeque::new(),
            batches: Vec::new(),
            statements: Vec::new(),
        };

        let (results, error_action) = execute_mysql_batch_statements(
            &mut executor,
            &statements,
            Some(DatabaseType::Mysql),
            db::mysql::MySqlQueryDialect::default(),
            None,
            false,
            Some(MYSQL_MULTI_STATEMENT_BATCH_MAX_BYTES),
            None,
        )
        .await;

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].statement_index, Some(0));
        assert!(results[0].execution_error);
        assert_eq!(error_action, Some(PoolErrorAction::Discard));
    }

    #[tokio::test]
    async fn mysql_pipelined_batch_does_not_add_a_failure_after_every_statement_completed() {
        let statements =
            vec!["INSERT INTO users(id) VALUES (1)".to_string(), "INSERT INTO users(id) VALUES (2)".to_string()];
        let mut executor = FakePipelinedMysqlBatchExecutor {
            batch_outcomes: std::collections::VecDeque::from([db::mysql::MySqlNonResultBatchOutcome {
                results: vec![empty_query_result(1), empty_query_result(1)],
                error: Some(QUERY_CANCELED.to_string()),
            }]),
            statement_outcomes: std::collections::VecDeque::new(),
            batches: Vec::new(),
            statements: Vec::new(),
        };
        let progress_events = Arc::new(std::sync::Mutex::new(Vec::new()));
        let progress: ExecuteMultiProgressCallback = {
            let progress_events = Arc::clone(&progress_events);
            Arc::new(move |event| progress_events.lock().unwrap().push(event))
        };

        let (results, error_action) = execute_mysql_batch_statements(
            &mut executor,
            &statements,
            Some(DatabaseType::Mysql),
            db::mysql::MySqlQueryDialect::default(),
            None,
            false,
            Some(MYSQL_MULTI_STATEMENT_BATCH_MAX_BYTES),
            Some(&progress),
        )
        .await;

        assert_eq!(results.len(), statements.len());
        assert!(results.iter().all(|result| !result.execution_error));
        assert_eq!(results.iter().map(|result| result.statement_index).collect::<Vec<_>>(), vec![Some(0), Some(1)]);
        assert_eq!(
            progress_events
                .lock()
                .unwrap()
                .iter()
                .map(|event| (event.statement_index, event.completed, event.total, event.success))
                .collect::<Vec<_>>(),
            vec![(0, 1, 2, true), (1, 2, 2, true)]
        );
        assert_eq!(error_action, Some(PoolErrorAction::Discard));
    }

    #[test]
    fn mysql_non_result_batches_respect_the_statement_limit() {
        let statements = (0..51).map(|index| format!("INSERT INTO users(id) VALUES ({index})")).collect::<Vec<_>>();

        assert_eq!(
            mysql_non_result_batch_end(
                &statements,
                0,
                db::mysql::MySqlQueryDialect::default(),
                MYSQL_MULTI_STATEMENT_BATCH_MAX_BYTES,
            ),
            MYSQL_MULTI_STATEMENT_BATCH_MAX_STATEMENTS
        );
        assert_eq!(
            mysql_non_result_batch_end(
                &statements,
                MYSQL_MULTI_STATEMENT_BATCH_MAX_STATEMENTS,
                db::mysql::MySqlQueryDialect::default(),
                MYSQL_MULTI_STATEMENT_BATCH_MAX_BYTES,
            ),
            51
        );
    }

    #[test]
    fn mysql_non_result_batches_respect_the_byte_limit() {
        let payload = "x".repeat(1_500_000);
        let statements = (0..3)
            .map(|index| format!("INSERT INTO users(id, payload) VALUES ({index}, '{payload}')"))
            .collect::<Vec<_>>();

        assert_eq!(
            mysql_non_result_batch_end(
                &statements,
                0,
                db::mysql::MySqlQueryDialect::default(),
                MYSQL_MULTI_STATEMENT_BATCH_MAX_BYTES,
            ),
            2
        );
    }

    #[test]
    fn mysql_single_call_uses_multi_result_route() {
        assert!(mysql_single_statement_uses_batch_route(
            Some(DatabaseType::Mysql),
            true,
            "# generated call\nCALL testA()",
            None,
        ));
    }

    #[test]
    fn ordinary_mysql_single_statements_keep_singular_route() {
        for sql in ["SELECT 1", "SHOW TABLES", "UPDATE users SET active = 1"] {
            assert!(!mysql_single_statement_uses_batch_route(Some(DatabaseType::Mysql), true, sql, None));
        }
    }

    #[test]
    fn mysql_call_route_requires_native_mysql_type_and_pool() {
        assert!(!mysql_single_statement_uses_batch_route(Some(DatabaseType::Doris), true, "CALL testA()", None,));
        assert!(!mysql_single_statement_uses_batch_route(Some(DatabaseType::Mysql), false, "CALL testA()", None,));
    }

    #[test]
    fn mysql_result_byte_limit_keeps_existing_batch_route() {
        assert!(mysql_single_statement_uses_batch_route(
            Some(DatabaseType::Mysql),
            true,
            "SELECT * FROM users",
            Some(1024),
        ));
        assert!(!mysql_single_statement_uses_batch_route(
            Some(DatabaseType::Mysql),
            true,
            "SELECT * FROM users",
            Some(0),
        ));
    }

    #[test]
    fn single_mysql_batch_route_never_probes_non_result_pipeline_limits() {
        assert!(!mysql_non_result_pipeline_enabled(1, false, crate::connection::MysqlMode::Normal));
        assert!(mysql_non_result_pipeline_enabled(2, false, crate::connection::MysqlMode::Normal));
        assert!(!mysql_non_result_pipeline_enabled(2, true, crate::connection::MysqlMode::Normal));
        assert!(!mysql_non_result_pipeline_enabled(2, false, crate::connection::MysqlMode::Bare));
    }

    #[tokio::test]
    async fn mysql_batch_preserves_multiple_result_sets_from_one_statement() {
        let statements = vec!["CALL testA()".to_string(), "UPDATE users SET active = 1".to_string()];
        let result_set = |value| db::QueryResult {
            columns: vec!["value".to_string()],
            column_types: vec!["INT".to_string()],
            deferred_large_value_cells: Vec::new(),
            column_sortables: vec![],
            spatial_columns: vec![],
            raw_key_cells: Vec::new(),
            spatial_values: vec![],
            rows: vec![vec![serde_json::json!(value)]],
            affected_rows: 0,
            execution_time_ms: 1,
            server_execute_time_us: None,
            query_timings_ms: None,
            truncated: false,
            session_id: None,
            has_more: false,
            elasticsearch_raw_body: None,
            messages: Vec::new(),
        };
        let mut executor = FakeMysqlBatchExecutor {
            outcomes: std::collections::VecDeque::from([
                Ok(vec![
                    mysql_query_result(result_set(1)),
                    mysql_query_result(result_set(2)),
                    mysql_query_result(result_set(3)),
                ]),
                mysql_batch_result(empty_query_result(1)),
            ]),
            executed: Vec::new(),
        };

        let (results, error_action) = execute_mysql_batch_statements(
            &mut executor,
            &statements,
            Some(DatabaseType::Mysql),
            db::mysql::MySqlQueryDialect::default(),
            None,
            false,
            None,
            None,
        )
        .await;

        assert_eq!(executor.executed, statements);
        assert_eq!(results.len(), 4);
        assert_eq!(
            results.iter().map(|result| result.statement_index).collect::<Vec<_>>(),
            vec![Some(0), Some(0), Some(0), Some(1)]
        );
        assert_eq!(
            results[..3].iter().map(|result| result.result.rows[0][0].clone()).collect::<Vec<_>>(),
            vec![serde_json::json!(1), serde_json::json!(2), serde_json::json!(3)]
        );
        assert_eq!(error_action, None);
    }

    #[tokio::test]
    async fn mysql_batch_stops_when_the_first_statement_fails() {
        let statements = vec!["fails".to_string(), "must-not-run".to_string()];
        let mut executor = FakeMysqlBatchExecutor {
            outcomes: std::collections::VecDeque::from([
                Err("Duplicate entry".to_string()),
                mysql_batch_result(empty_query_result(0)),
            ]),
            executed: Vec::new(),
        };

        let (results, error_action) = execute_mysql_batch_statements(
            &mut executor,
            &statements,
            Some(DatabaseType::Mysql),
            db::mysql::MySqlQueryDialect::default(),
            None,
            false,
            None,
            None,
        )
        .await;

        assert_eq!(executor.executed, vec!["fails"]);
        assert_eq!(results.len(), 1);
        assert!(results[0].execution_error);
        assert_eq!(error_action, Some(PoolErrorAction::Keep));
    }

    #[tokio::test]
    async fn mysql_batch_continues_after_statement_errors_when_enabled() {
        let statements = vec!["first".to_string(), "fails".to_string(), "third".to_string()];
        let mut executor = FakeMysqlBatchExecutor {
            outcomes: std::collections::VecDeque::from([
                mysql_batch_result(empty_query_result(0)),
                Err("Duplicate entry".to_string()),
                mysql_batch_result(empty_query_result(0)),
            ]),
            executed: Vec::new(),
        };

        let (results, error_action) = execute_mysql_batch_statements(
            &mut executor,
            &statements,
            Some(DatabaseType::Mysql),
            db::mysql::MySqlQueryDialect::default(),
            None,
            true,
            None,
            None,
        )
        .await;

        assert_eq!(executor.executed, statements);
        assert_eq!(results.len(), 3);
        assert_eq!(
            results.iter().map(|result| result.statement_index).collect::<Vec<_>>(),
            vec![Some(0), Some(1), Some(2)]
        );
        assert!(results[1].execution_error);
        assert_eq!(error_action, None);
    }

    #[tokio::test]
    async fn mysql_batch_continues_when_the_first_statement_fails_and_enabled() {
        let statements = vec!["fails".to_string(), "second".to_string()];
        let mut executor = FakeMysqlBatchExecutor {
            outcomes: std::collections::VecDeque::from([
                Err("Duplicate entry".to_string()),
                mysql_batch_result(empty_query_result(0)),
            ]),
            executed: Vec::new(),
        };

        let (results, error_action) = execute_mysql_batch_statements(
            &mut executor,
            &statements,
            Some(DatabaseType::Mysql),
            db::mysql::MySqlQueryDialect::default(),
            None,
            true,
            None,
            None,
        )
        .await;

        assert_eq!(executor.executed, statements);
        assert_eq!(results.len(), 2);
        assert!(results[0].execution_error);
        assert_eq!(error_action, None);
    }

    #[tokio::test]
    async fn mysql_batch_stops_on_connection_errors_when_continue_is_enabled() {
        let statements = vec!["first".to_string(), "disconnects".to_string(), "must-not-run".to_string()];
        let mut executor = FakeMysqlBatchExecutor {
            outcomes: std::collections::VecDeque::from([
                mysql_batch_result(empty_query_result(0)),
                Err("connection reset by peer".to_string()),
                mysql_batch_result(empty_query_result(0)),
            ]),
            executed: Vec::new(),
        };

        let (results, error_action) = execute_mysql_batch_statements(
            &mut executor,
            &statements,
            Some(DatabaseType::Mysql),
            db::mysql::MySqlQueryDialect::default(),
            None,
            true,
            None,
            None,
        )
        .await;

        assert_eq!(executor.executed, vec!["first", "disconnects"]);
        assert_eq!(results.len(), 2);
        assert_eq!(results.iter().map(|result| result.statement_index).collect::<Vec<_>>(), vec![Some(0), Some(1)]);
        assert!(results[1].execution_error);
        assert_eq!(results[1].error.as_ref().map(|error| error.code()), Some("DBX-LEGACY-0001"));
        assert_eq!(error_action, Some(PoolErrorAction::ReconnectAndRetry));
    }

    #[test]
    fn execute_multi_result_serializes_client_metadata_only_when_present() {
        let success = serde_json::to_value(ExecuteMultiResult::from(empty_query_result(0))).unwrap();
        assert!(success.get("execution_error").is_none());
        assert!(success.get("statement_index").is_none());
        assert!(success.get("server_message").is_none());
        assert!(success.get("large_value_cells").is_none());

        let mut error_column = empty_query_result(0);
        error_column.columns = vec!["Error".to_string()];
        error_column.rows = vec![vec![serde_json::json!("valid query value")]];
        let error_column = serde_json::to_value(ExecuteMultiResult::success_with_index(error_column, 0)).unwrap();
        assert!(error_column.get("execution_error").is_none());
        assert!(error_column.get("error").is_none());

        let failure = serde_json::to_value(ExecuteMultiResult::execution_error_with_index(
            error_query_result("failed".to_string()),
            2,
        ))
        .unwrap();
        assert_eq!(failure.get("execution_error"), Some(&serde_json::Value::Bool(true)));
        assert_eq!(failure.get("statement_index"), Some(&serde_json::json!(2)));
        assert_eq!(failure.get("columns"), Some(&serde_json::json!(["Error"])));
        assert_eq!(
            failure.get("error").and_then(|value| value.get("code")),
            Some(&serde_json::json!("DBX-LEGACY-0001"))
        );

        let redacted = serde_json::to_value(
            ExecuteMultiResult::execution_error_with_index(error_query_result("safe failure".to_string()), 2)
                .without_error_detail(),
        )
        .unwrap();
        assert!(redacted.get("error").and_then(|value| value.get("detail")).is_none());
    }

    #[test]
    fn execute_multi_result_serializes_large_value_metadata_only_when_present() {
        let result = ExecuteMultiResult::success_with_index_and_large_values(
            empty_query_result(1),
            0,
            vec![db::LargeValueCell {
                row_index: 2,
                column_index: 3,
                original_bytes: 65_536,
                pending_decode: None,
                value_ref: None,
            }],
            false,
        );

        let serialized = serde_json::to_value(result).unwrap();
        assert_eq!(
            serialized.get("large_value_cells"),
            Some(&serde_json::json!([{"row_index": 2, "column_index": 3, "original_bytes": 65_536}]))
        );
    }

    #[test]
    fn large_value_cell_merge_replaces_driver_entries_and_appends_server_entries() {
        let driver_cells = vec![
            db::LargeValueCell {
                row_index: 1,
                column_index: 2,
                original_bytes: 10,
                pending_decode: None,
                value_ref: None,
            },
            db::LargeValueCell {
                row_index: 3,
                column_index: 4,
                original_bytes: 20,
                pending_decode: None,
                value_ref: None,
            },
        ];
        let server_cells = vec![
            db::LargeValueCell {
                row_index: 1,
                column_index: 2,
                original_bytes: 100,
                pending_decode: None,
                value_ref: None,
            },
            db::LargeValueCell {
                row_index: 5,
                column_index: 6,
                original_bytes: 200,
                pending_decode: None,
                value_ref: None,
            },
        ];

        assert_eq!(
            merge_large_value_cells(driver_cells, server_cells),
            vec![
                db::LargeValueCell {
                    row_index: 1,
                    column_index: 2,
                    original_bytes: 100,
                    pending_decode: None,
                    value_ref: None
                },
                db::LargeValueCell {
                    row_index: 3,
                    column_index: 4,
                    original_bytes: 20,
                    pending_decode: None,
                    value_ref: None
                },
                db::LargeValueCell {
                    row_index: 5,
                    column_index: 6,
                    original_bytes: 200,
                    pending_decode: None,
                    value_ref: None
                },
            ]
        );
    }

    #[test]
    fn large_value_cell_merge_preserves_single_source_inputs() {
        let driver_cell = db::LargeValueCell {
            row_index: 1,
            column_index: 2,
            original_bytes: 10,
            pending_decode: None,
            value_ref: None,
        };
        let server_cell = db::LargeValueCell {
            row_index: 3,
            column_index: 4,
            original_bytes: 20,
            pending_decode: None,
            value_ref: None,
        };

        assert_eq!(merge_large_value_cells(vec![driver_cell.clone()], Vec::new()), vec![driver_cell]);
        assert_eq!(merge_large_value_cells(Vec::new(), vec![server_cell.clone()]), vec![server_cell]);
    }

    #[test]
    fn large_value_cell_merge_handles_large_disjoint_inputs() {
        const CELL_COUNT: usize = 100_000;
        let driver_cells = (0..CELL_COUNT)
            .map(|row_index| db::LargeValueCell {
                row_index,
                column_index: 0,
                original_bytes: 10,
                pending_decode: None,
                value_ref: None,
            })
            .collect();
        let server_cells = (0..CELL_COUNT)
            .map(|row_index| db::LargeValueCell {
                row_index,
                column_index: 1,
                original_bytes: 20,
                pending_decode: None,
                value_ref: None,
            })
            .collect();

        let merged = merge_large_value_cells(driver_cells, server_cells);

        assert_eq!(merged.len(), CELL_COUNT * 2);
        assert_eq!(merged[CELL_COUNT].row_index, 0);
        assert_eq!(merged[CELL_COUNT * 2 - 1].row_index, CELL_COUNT - 1);
    }

    #[test]
    fn sqlserver_batch_results_do_not_claim_statement_indexes() {
        assert_eq!(split_sql_batches("SELECT 1; SELECT 2;").len(), 1);

        let results = sqlserver_batch_results(vec![
            db::sqlserver::SqlServerBatchResult { result: empty_query_result(1), server_message: false },
            db::sqlserver::SqlServerBatchResult { result: empty_query_result(2), server_message: true },
        ]);

        assert_eq!(results.iter().map(|result| result.statement_index).collect::<Vec<_>>(), vec![None, None]);
        assert!(!results[0].server_message);
        assert!(results[1].server_message);

        let serialized = serde_json::to_value(&results[1]).unwrap();
        assert_eq!(serialized.get("server_message"), Some(&serde_json::Value::Bool(true)));
    }

    #[test]
    fn sqlserver_agent_execution_plan_preserves_table_variable_batch() {
        let sql = "DECLARE @TargetTables TABLE (TableName NVARCHAR(128));\n\
                   INSERT INTO @TargetTables VALUES ('Bill_Record');\n\
                   SELECT TableName FROM @TargetTables;";

        let plan = query_execution_plan(sql, Some(DatabaseType::SqlServer), true);

        assert_eq!(plan.statements, vec![sql]);
    }

    #[test]
    fn sqlserver_agent_execution_plan_splits_only_on_go() {
        let sql = "DECLARE @x TABLE (id INT);\nINSERT INTO @x VALUES (1);\nGO\nSELECT 2;";

        let plan = query_execution_plan(sql, Some(DatabaseType::SqlServer), true);

        assert_eq!(plan.statements, vec!["DECLARE @x TABLE (id INT);\nINSERT INTO @x VALUES (1);", "SELECT 2;"]);
    }

    #[test]
    fn jdbc_oracle_execution_plan_keeps_plsql_units_whole() {
        let mut oracle = test_connection_config(DatabaseType::Jdbc);
        oracle.connection_string = Some("jdbc:oracle:thin:@//127.0.0.1:1521/FREEPDB1".to_string());
        let function = "CREATE OR REPLACE EDITIONABLE FUNCTION \"APP\".\"F\" (p VARCHAR2) RETURN VARCHAR2 IS\n\
                        l_ret VARCHAR2(200);\nBEGIN\n  SELECT p INTO l_ret FROM dual;\n  RETURN l_ret;\nEND;";

        let plan = query_execution_plan(function, Some(statement_split_database_type(&oracle)), false);

        assert_eq!(plan.statements, vec![function]);

        let mut other = test_connection_config(DatabaseType::Jdbc);
        other.connection_string = Some("jdbc:h2:mem:test".to_string());
        assert_eq!(statement_split_database_type(&other), DatabaseType::Jdbc);
    }

    // #13514: a generic JDBC connection to SQL Server splits with the SQL Server
    // rules, so the reported TRY/CATCH script keeps its batch whole while the
    // leading `SET` stays its own statement. Other JDBC configs stay on the
    // plain `Jdbc` profile.
    #[test]
    fn jdbc_sqlserver_execution_plan_keeps_control_flow_batches_whole() {
        let mut sqlserver = test_connection_config(DatabaseType::Jdbc);
        sqlserver.connection_string = Some("jdbc:sqlserver://127.0.0.1:1433;databaseName=demo".to_string());
        assert_eq!(statement_split_database_type(&sqlserver), DatabaseType::SqlServer);

        let sql = "SET XACT_ABORT ON;\n\
                   BEGIN TRY\n\
                   BEGIN TRANSACTION;\n\
                   UPDATE dbo.Documents SET Deleted = 0 WHERE Deleted = 1;\n\
                   COMMIT TRANSACTION;\n\
                   END TRY\n\
                   BEGIN CATCH\n\
                   IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION;\n\
                   THROW;\n\
                   END CATCH;";
        let block = &sql[sql.find("BEGIN TRY").unwrap()..];

        let plan = query_execution_plan(sql, Some(statement_split_database_type(&sqlserver)), false);
        assert_eq!(plan.statements, vec!["SET XACT_ABORT ON".to_string(), block.to_string()]);

        let mut other = test_connection_config(DatabaseType::Jdbc);
        other.connection_string = Some("jdbc:h2:mem:test".to_string());
        let plan = query_execution_plan("SELECT 1; SELECT 2;", Some(statement_split_database_type(&other)), false);
        assert_eq!(plan.statements, vec!["SELECT 1", "SELECT 2"]);

        let mut postgres = test_connection_config(DatabaseType::Jdbc);
        postgres.connection_string = Some("jdbc:postgresql://127.0.0.1:5432/demo".to_string());
        assert_eq!(statement_split_database_type(&postgres), DatabaseType::Jdbc);

        let mut jtds = test_connection_config(DatabaseType::Jdbc);
        jtds.connection_string = Some("jdbc:jtds:sqlserver://127.0.0.1:1433/demo".to_string());
        jtds.jdbc_driver_class = Some("net.sourceforge.jtds.jdbc.Driver".to_string());
        assert_eq!(statement_split_database_type(&jtds), DatabaseType::SqlServer);
    }

    #[test]
    fn ordinary_agent_execution_plan_still_splits_semicolon_statements() {
        let plan = query_execution_plan("SELECT 1; SELECT 2;", Some(DatabaseType::Dameng), false);

        assert_eq!(plan.statements, vec!["SELECT 1", "SELECT 2"]);
    }

    // Regression test for #6097: SQL Server queries share a single mutex-guarded
    // connection (see PoolKind::SqlServer), so a fast query can queue for seconds
    // behind another operation (e.g. autocomplete/schema metadata) holding that
    // same connection. Before this fix, `execution_time_ms` was measured only
    // from inside db::sqlserver's own timers, which start *after* the lock is
    // acquired — so that queueing time was invisible to the user, producing a
    // reported duration (e.g. "6-8ms") wildly smaller than what they actually
    // waited (e.g. "10s"). `lock_shared_client_with_wait` is the exact helper
    // both PoolKind::SqlServer call sites now use to fold that wait back in.
    #[tokio::test]
    async fn lock_shared_client_with_wait_reports_time_queued_behind_another_holder() {
        let client = Arc::new(tokio::sync::Mutex::new(0u8));
        let holder_guard = client.lock().await;

        let waiter_client = client.clone();
        let waiter = tokio::spawn(async move {
            let (_guard, wait_ms) = lock_shared_client_with_wait(&waiter_client, None, None).await.unwrap();
            wait_ms
        });

        // Give the spawned task a chance to actually start waiting on the lock
        // before the holder releases it, so the measured wait is meaningful.
        tokio::time::sleep(Duration::from_millis(150)).await;
        drop(holder_guard);

        let wait_ms = waiter.await.unwrap();
        assert!(wait_ms >= 150, "expected queued wait time to be captured, got {wait_ms}ms");
    }

    #[tokio::test]
    async fn lock_shared_client_with_wait_is_near_zero_when_uncontended() {
        let client = Arc::new(tokio::sync::Mutex::new(0u8));

        let (_guard, wait_ms) = lock_shared_client_with_wait(&client, None, None).await.unwrap();

        assert!(wait_ms < 50, "expected an uncontended lock to report negligible wait, got {wait_ms}ms");
    }

    #[test]
    fn query_execution_error_preserves_structured_catalog_identity() {
        let error = QueryExecutionError::Agent(AgentCallError::Structured {
            rpc_code: -1,
            message: "connection lost".to_string(),
            context: crate::db::agent_driver::AgentErrorContext {
                contract_version: 1,
                category: crate::db::agent_driver::AgentErrorCategory::Connection,
                retryable: false,
                session_disposition: crate::db::agent_driver::AgentSessionDisposition::Quarantine,
                stage: AgentErrorStage::Execute,
                operation_outcome: AgentOperationOutcome::Unknown,
                agent_session_id: Some("session-1".to_string()),
                sql_state: None,
                vendor_code: None,
                exception_class: None,
            },
        });

        assert_eq!(error.into_backend_error().code(), "DBX-JDBC-1002");
    }

    #[test]
    fn postgres_sql_error_with_position_survives_classification_and_legacy_rendering() {
        let sql = "SELECT *\nFROM no_such_table";
        let cursor = sql.find("no_such_table").unwrap() as u32 + 1;
        let raw = format!(
            "ERROR: relation \"no_such_table\" does not exist{}",
            crate::sql_error_position::encode_marker(cursor)
        );
        let (message, position) = crate::sql_error_position::take_message_position(&raw, sql);
        let error = QueryExecutionError::SqlWithPosition { message, position: position.unwrap() };

        // The transport marker must never reach the user-facing message.
        let legacy = error.clone().into_legacy_string();
        assert!(!legacy.contains(crate::sql_error_position::SQL_ERROR_POSITION_MARKER));
        assert_eq!(legacy, "ERROR: relation \"no_such_table\" does not exist");

        // Classification must not downgrade the typed variant.
        let classified = classify_query_error(Some(DatabaseType::Postgres), error.clone());
        assert!(matches!(classified, QueryExecutionError::SqlWithPosition { .. }));

        let backend_error = classified.into_backend_error();
        assert_eq!(backend_error.code(), "DBX-JDBC-4001");
        let position = backend_error.error_position().expect("position must survive into the envelope");
        assert_eq!((position.line, position.column), (2, 6));
    }

    #[test]
    fn legacy_rendering_strips_a_leftover_transport_marker() {
        // Guards the driver-error path that never went through the resolve step
        // (e.g. a PostgreSQL-family driver error shown as a legacy string).
        let error = QueryExecutionError::Legacy(format!(
            "ERROR: boom{}{}",
            crate::sql_error_position::encode_marker(9),
            crate::sql_error_position::encode_marker(2)
        ));
        let rendered = error.into_legacy_string();
        assert_eq!(rendered, "ERROR: boom");
        assert!(!rendered.contains(crate::sql_error_position::SQL_ERROR_POSITION_MARKER));
    }

    #[test]
    fn duckdb_worker_error_preserves_catalog_identity_and_detail() {
        let error = QueryExecutionError::DuckDb {
            code: "duckdb_execute_failed".to_string(),
            message: "Catalog Error: Table missing_table does not exist".to_string(),
        };
        let backend_error = error.into_backend_error();

        assert_eq!(backend_error.code(), "DBX-JDBC-4001");
        assert_eq!(backend_error.detail(), Some("Catalog Error: Table missing_table does not exist"));
    }

    #[test]
    fn query_timeout_preserves_timeout_catalog_identity_and_detail() {
        let error = classify_query_error(
            Some(DatabaseType::Postgres),
            QueryExecutionError::Legacy("Query timed out after 1 seconds".to_string()),
        )
        .with_omitted_sql_context("SELECT pg_sleep(10)");
        let backend_error = error.into_backend_error();

        assert_eq!(backend_error.code(), "DBX-JDBC-2002");
        assert_eq!(
            backend_error.message_params().get("stage"),
            Some(&crate::backend_error::BackendMessageParam::String("execute".to_string()))
        );
        assert_eq!(
            backend_error.detail(),
            Some(
                "Query timed out after 1 seconds\nSQL text omitted from user-facing error; enable debug SQL diagnostics to inspect the original statement."
            )
        );
    }

    #[test]
    fn postgres_server_error_preserves_sql_catalog_identity_and_detail() {
        let error = classify_query_error(
            Some(DatabaseType::Postgres),
            QueryExecutionError::Legacy("ERROR: relation \"dbx_table_that_does_not_exist\" does not exist".to_string()),
        )
        .with_omitted_sql_context("SELECT * FROM dbx_table_that_does_not_exist");
        let backend_error = error.into_backend_error();

        assert_eq!(backend_error.code(), "DBX-JDBC-4001");
        assert_eq!(
            backend_error.message_params().get("stage"),
            Some(&crate::backend_error::BackendMessageParam::String("execute".to_string()))
        );
        assert_eq!(
            backend_error.detail(),
            Some(
                "ERROR: relation \"dbx_table_that_does_not_exist\" does not exist\nSQL text omitted from user-facing error; enable debug SQL diagnostics to inspect the original statement."
            )
        );
    }

    #[test]
    fn mysql_server_error_preserves_sql_catalog_identity_and_detail() {
        let error = classify_query_error(
            Some(DatabaseType::Mysql),
            QueryExecutionError::Legacy(
                "Server error: `ERROR 1064 (42000): You have an error in your SQL syntax`".to_string(),
            ),
        )
        .with_omitted_sql_context("SELECT 111 AS first_value FROM DUAL");
        let backend_error = error.into_backend_error();

        assert_eq!(backend_error.code(), "DBX-JDBC-4001");
        assert_eq!(backend_error.message_key(), "backendErrors.jdbc.sqlFailed");
        assert_eq!(
            backend_error.message_params().get("stage"),
            Some(&crate::backend_error::BackendMessageParam::String("execute".to_string()))
        );
        assert_eq!(
            backend_error.detail(),
            Some(
                "Server error: `ERROR 1064 (42000): You have an error in your SQL syntax` SQL text omitted from user-facing error; enable debug SQL diagnostics to inspect the original statement."
            )
        );
    }

    #[test]
    fn single_statement_multi_result_preserves_sql_error_type() {
        let error = classify_query_error(
            Some(DatabaseType::Postgres),
            QueryExecutionError::Legacy("ERROR: relation \"dbx_table_that_does_not_exist\" does not exist".to_string()),
        )
        .with_omitted_sql_context("SELECT * FROM dbx_table_that_does_not_exist");

        let error = single_statement_multi_result(Err(error), false, None).unwrap_err();
        let backend_error = error.into_backend_error();

        assert_eq!(backend_error.code(), "DBX-JDBC-4001");
        assert_eq!(backend_error.message_key(), "backendErrors.jdbc.sqlFailed");
        assert_eq!(
            backend_error.detail(),
            Some(
                "ERROR: relation \"dbx_table_that_does_not_exist\" does not exist\nSQL text omitted from user-facing error; enable debug SQL diagnostics to inspect the original statement."
            )
        );
    }

    #[test]
    fn postgres_transaction_statement_error_preserves_sql_catalog_identity() {
        let error = postgres_transaction_statement_error(
            1,
            "ERROR: relation \"dbx_table_that_does_not_exist\" does not exist",
            "SELECT * FROM dbx_table_that_does_not_exist",
            true,
        );
        let backend_error = error.into_backend_error();

        assert_eq!(backend_error.code(), "DBX-JDBC-4001");
        assert_eq!(backend_error.message_key(), "backendErrors.jdbc.sqlFailed");
        assert_eq!(
            backend_error.detail(),
            Some(
                "Statement 1 failed: ERROR: relation \"dbx_table_that_does_not_exist\" does not exist\nSQL text omitted from user-facing error; enable debug SQL diagnostics to inspect the original statement."
            )
        );
    }

    #[test]
    fn postgres_transaction_uses_driver_fact_not_error_wording() {
        let error = postgres_transaction_statement_error(
            1,
            "ERROR: relation \"connection_closed\" does not exist",
            "SELECT * FROM connection_closed",
            true,
        );

        assert_eq!(error.into_backend_error().code(), "DBX-JDBC-4001");
    }

    #[test]
    fn sequential_multi_cancellation_uses_canceled_catalog() {
        let backend_error = canceled_query_execution_error().into_backend_error();

        assert_eq!(backend_error.code(), "DBX-JDBC-2003");
        assert_eq!(backend_error.message_key(), "backendErrors.jdbc.operationCanceled");
    }

    #[test]
    fn external_driver_method_unsupported_detects_legacy_plugin_errors() {
        assert!(is_external_driver_method_unsupported(
            "Unsupported JDBC plugin method: executeQueryPage",
            "executeQueryPage"
        ));
        assert!(is_external_driver_method_unsupported(
            "Plugin RPC error (-32601): Method not found: executeQueryPage",
            "executeQueryPage"
        ));
        assert!(is_external_driver_method_unsupported("Unknown method executeQueryPage", "executeQueryPage"));
    }

    #[test]
    fn external_driver_method_unsupported_ignores_query_and_other_method_errors() {
        assert!(!is_external_driver_method_unsupported(
            "The JDBC driver does not support this SQL operation",
            "executeQueryPage"
        ));
        assert!(!is_external_driver_method_unsupported(
            "Unsupported JDBC plugin method: listTables",
            "executeQueryPage"
        ));
        assert!(!is_external_driver_method_unsupported(
            "Unknown column executeQueryPage in field list",
            "executeQueryPage"
        ));
    }

    #[test]
    fn original_lob_ref_survives_production_result_normalization_without_polluting_preview_values() {
        let mut result = empty_query_result(0);
        result.columns = vec!["Payload".to_string(), "__DBX_LARGE_VALUE_BYTES_C_0".to_string()];
        result.column_types = vec!["CLOB".to_string(), "VARCHAR".to_string()];
        result.rows = vec![
            vec![serde_json::json!("原😀值"), serde_json::json!("D:1:unknown:original-ref")],
            vec![serde_json::Value::Null, serde_json::Value::Null],
            vec![serde_json::json!(""), serde_json::Value::Null],
        ];
        let normalized = ExecuteMultiResult::success_with_optional_server_large_values(result, true);
        assert_eq!(normalized.result.columns, vec!["Payload"]);
        assert_eq!(
            normalized.result.rows,
            vec![vec![serde_json::json!("原😀值")], vec![serde_json::Value::Null], vec![serde_json::json!("")]]
        );
        assert_eq!(
            normalized.large_value_cells,
            vec![db::LargeValueCell {
                row_index: 0,
                column_index: 0,
                original_bytes: SERVER_LARGE_VALUE_UNKNOWN_BYTES,
                pending_decode: None,
                value_ref: Some("original-ref".to_string()),
            }]
        );
        let wire = serde_json::to_value(normalized).unwrap();
        assert_eq!(wire["large_value_cells"][0]["value_ref"], "original-ref");
        assert_eq!(wire["rows"][0][0], "原😀值");
        let legacy: db::LargeValueCell = serde_json::from_value(serde_json::json!({
            "row_index": 0, "column_index": 1, "original_bytes": 128,
        }))
        .unwrap();
        assert_eq!(legacy.value_ref, None);
        assert!(serde_json::to_value(legacy).unwrap().get("value_ref").is_none());
    }

    #[test]
    fn lob_binary_download_decodes_every_byte_without_text_conversion() {
        let original: Vec<u8> = (0..=255).collect();
        let hex: String = original.iter().map(|value| format!("{value:02x}")).collect();
        assert_eq!(decode_lob_hex_chunk(&hex).unwrap(), original);
        assert_eq!(decode_lob_hex_chunk("").unwrap(), Vec::<u8>::new());
        assert_eq!(decode_lob_hex_chunk("00FF").unwrap(), vec![0, 255]);
        assert!(decode_lob_hex_chunk("0").is_err());
        assert!(decode_lob_hex_chunk("zz").is_err());
        assert!(decode_lob_hex_chunk("00".repeat(4097).as_str()).is_err());
    }

    #[test]
    fn lob_download_text_modes_preserve_multibyte_characters_split_across_chunks() {
        let original = "中文😀末尾";
        let mut utf8 = encoding_rs::UTF_8.new_decoder_without_bom_handling();
        let bytes = original.as_bytes();
        let mut decoded = String::new();
        for (index, byte) in bytes.iter().enumerate() {
            decoded.push_str(&decode_lob_text_chunk(&mut utf8, &[*byte], index + 1 == bytes.len()).unwrap());
        }
        assert_eq!(decoded, original);
        let (bytes, _, errors) = encoding_rs::GBK.encode("中文末尾");
        assert!(!errors);
        let mut gbk = encoding_rs::GBK.new_decoder_without_bom_handling();
        let mut decoded = String::new();
        for (index, byte) in bytes.iter().enumerate() {
            decoded.push_str(&decode_lob_text_chunk(&mut gbk, &[*byte], index + 1 == bytes.len()).unwrap());
        }
        assert_eq!(decoded, "中文末尾");
    }

    #[test]
    fn external_driver_preview_fallback_rewrites_generated_postgres_projections() {
        let sql = concat!(
            "SELECT \"id\", left(\"description\", 140) AS \"description\", ",
            "left(\"metadata\"::text, 141) AS \"metadata\", ",
            "'left(\"literal\", 1)' AS \"note\", ",
            "'T:139' AS \"__DBX_LARGE_VALUE_BYTES_T_1\" FROM \"job_details\" WHERE left(note, 1) = 'x' LIMIT 100"
        );
        assert_eq!(
            external_driver_preview_fallback_sql(sql).as_deref(),
            Some(
                "SELECT \"id\", \"description\" AS \"description\", \"metadata\"::text AS \"metadata\", 'left(\"literal\", 1)' AS \"note\", 'T:139' AS \"__DBX_LARGE_VALUE_BYTES_T_1\" FROM \"job_details\" WHERE left(note, 1) = 'x' LIMIT 100"
            )
        );
    }

    #[test]
    fn external_driver_preview_fallback_handles_nested_commas_without_touching_literals() {
        let sql = concat!(
            "SELECT left(coalesce(\"description\", concat('a,b', \"fallback\")), 140) AS \"description\", ",
            "'FROM left(\"literal\", 1)' AS \"note\", ",
            "'T:140' AS \"__DBX_LARGE_VALUE_BYTES_T_0\" FROM \"job_details\""
        );
        assert_eq!(
            external_driver_preview_fallback_sql(sql).as_deref(),
            Some(
                "SELECT coalesce(\"description\", concat('a,b', \"fallback\")) AS \"description\", 'FROM left(\"literal\", 1)' AS \"note\", 'T:140' AS \"__DBX_LARGE_VALUE_BYTES_T_0\" FROM \"job_details\""
            )
        );
    }

    #[test]
    fn external_driver_preview_fallback_requires_generated_marker_and_select_shape() {
        assert!(external_driver_preview_fallback_sql("SELECT left(value, 10) FROM t").is_none());
        assert!(external_driver_preview_fallback_sql(
            "WITH rows AS (SELECT left(value, 10) AS value FROM t) SELECT value FROM rows"
        )
        .is_none());
        assert!(external_driver_preview_fallback_sql(
            "SELECT left(value, 10) AS value FROM t WHERE note = '__DBX_LARGE_VALUE_BYTES_T_0'"
        )
        .is_none());
    }

    #[test]
    fn external_driver_invalid_utf8_error_detection_is_narrow() {
        assert!(is_external_driver_invalid_utf8_error("ERROR: invalid byte sequence for encoding \"UTF8\": 0xe2"));
        assert!(is_external_driver_invalid_utf8_error(
            "org.postgresql.util.PSQLException: ERROR: invalid byte sequence for encoding \"UTF8\": 0xe2"
        ));
        assert!(!is_external_driver_invalid_utf8_error("ERROR: invalid byte sequence for encoding \"LATIN1\": 0xe2"));
        assert!(!is_external_driver_invalid_utf8_error("Incorrect syntax near SELECT"));
    }

    #[test]
    fn postgres_preview_fallback_retries_only_generated_projections() {
        let options = QueryExecutionOptions { table_data_preview: true, ..Default::default() };
        let sql = concat!(
            "SELECT \"id\", left(\"content\", 227) AS \"content\", ",
            "'T:226' AS \"__DBX_LARGE_VALUE_BYTES_T_1\" FROM \"t_large\" LIMIT 100"
        );
        let error = "ERROR: invalid byte sequence for encoding \"UTF8\": 0xe5 0xa4";
        assert_eq!(
            postgres_preview_fallback_retry_sql(&options, error, sql).as_deref(),
            Some(
                "SELECT \"id\", \"content\" AS \"content\", 'T:226' AS \"__DBX_LARGE_VALUE_BYTES_T_1\" FROM \"t_large\" LIMIT 100"
            )
        );

        // User SQL without the generated marker is never rewritten, unrelated
        // errors never trigger a retry, and the data-grid flag is required.
        assert!(postgres_preview_fallback_retry_sql(&options, error, "SELECT left(value, 10) FROM t").is_none());
        assert!(postgres_preview_fallback_retry_sql(&options, "ERROR: syntax error at or near \"left\"", sql).is_none());
        assert!(postgres_preview_fallback_retry_sql(&QueryExecutionOptions::default(), error, sql).is_none());
    }

    #[test]
    fn preview_missing_left_function_retry_rewrites_generated_mysql_previews() {
        let options = QueryExecutionOptions { table_data_preview: true, ..Default::default() };
        let sql = concat!(
            "SELECT `id`, LEFT(`Words`, 101) AS `Words`, ",
            "CONCAT('T:100:', LENGTH(`Words`)) AS `__DBX_LARGE_VALUE_BYTES_T_1` FROM `vocab` LIMIT 100"
        );
        let error = "Server error: `ERROR 32900 (HY000): function left(json, integer) does not exist'";

        assert_eq!(
            preview_missing_left_function_fallback_sql(&options, error, sql).as_deref(),
            Some(
                "SELECT `id`, `Words` AS `Words`, CONCAT('T:100:', LENGTH(`Words`)) AS `__DBX_LARGE_VALUE_BYTES_T_1` FROM `vocab` LIMIT 100"
            )
        );

        // User SQL without the generated marker is never rewritten, unrelated
        // errors never trigger a retry, and the data-grid flag is required.
        assert!(
            preview_missing_left_function_fallback_sql(&options, error, "SELECT LEFT(Words, 100) FROM vocab").is_none()
        );
        assert!(preview_missing_left_function_fallback_sql(&options, "ERROR: syntax error", sql).is_none());
        assert!(preview_missing_left_function_fallback_sql(&QueryExecutionOptions::default(), error, sql).is_none());
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn external_driver_preview_retry_preserves_marker_truncation() {
        use std::os::unix::fs::PermissionsExt;

        let dir = std::env::temp_dir().join(format!("dbx-jdbc-preview-retry-test-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&dir).unwrap();
        let executable = dir.join("plugin.sh");
        let calls = dir.join("calls.log");
        std::fs::write(
            &executable,
            format!(
                "#!/bin/sh\nwhile IFS= read -r line; do\n  id=$(printf '%s' \"$line\" | sed -E 's/.*\"id\":([0-9]+).*/\\1/')\n  case \"$line\" in\n    *'\"method\":\"executeQueryPage\"'*)\n      echo executeQueryPage >> '{}'\n      case \"$line\" in\n        *'left('*) printf '{{\"id\":%s,\"error\":{{\"message\":\"ERROR: invalid byte sequence for encoding UTF8: 0xe2\"}}}}\\n' \"$id\" ;;\n        *) printf '{{\"id\":%s,\"result\":{{\"columns\":[\"id\",\"description\",\"__DBX_LARGE_VALUE_BYTES_T_1\"],\"column_types\":[\"integer\",\"text\",\"text\"],\"rows\":[[1,\"abcdef\",\"T:3\"]],\"affected_rows\":0,\"execution_time_ms\":1,\"truncated\":false}}}}\\n' \"$id\" ;;\n      esac\n      ;;\n    *'\"method\":\"executeQuery\"'*)\n      echo executeQuery >> '{}'\n      case \"$line\" in\n        *'left('*) printf '{{\"id\":%s,\"error\":{{\"message\":\"ERROR: invalid byte sequence for encoding UTF8: 0xe2\"}}}}\\n' \"$id\" ;;\n        *) printf '{{\"id\":%s,\"result\":{{\"columns\":[\"id\",\"description\",\"__DBX_LARGE_VALUE_BYTES_T_1\"],\"column_types\":[\"integer\",\"text\",\"text\"],\"rows\":[[1,\"abcdef\",\"T:3\"]],\"affected_rows\":0,\"execution_time_ms\":1,\"truncated\":false}}}}\\n' \"$id\" ;;\n      esac\n      ;;\n  esac\ndone\n",
                calls.display(),
                calls.display()
            ),
        )
        .unwrap();
        let mut permissions = std::fs::metadata(&executable).unwrap().permissions();
        permissions.set_mode(0o755);
        std::fs::set_permissions(&executable, permissions).unwrap();

        let plugin = InstalledPlugin {
            manifest: PluginManifest {
                id: "jdbc".to_string(),
                name: "JDBC".to_string(),
                version: "preview-retry".to_string(),
                protocol_version: 1,
                description: String::new(),
                executable: Some("plugin.sh".to_string()),
                drivers: vec![PluginDriverManifest {
                    id: "jdbc".to_string(),
                    label: "JDBC".to_string(),
                    kind: "external".to_string(),
                    database_type: Some("jdbc".to_string()),
                }],
                ..Default::default()
            },
            path: dir.clone(),
            compatibility: PluginCompatibility {
                compatible: true,
                backend_executable: Some(dir.join("plugin.sh")),
                ..Default::default()
            },
            provenance: None,
        };
        let session = PluginDriverSession::start_for_test(plugin, "jdbc".to_string(), PluginRuntimeEnv::default())
            .await
            .expect("preview retry plugin should start");
        let mut config = test_connection_config(DatabaseType::Jdbc);
        config.id = "jdbc-preview".to_string();
        config.connection_string = Some("jdbc:test".to_string());
        let marker = format!("{}T_1", crate::sql_dialect::DBX_LARGE_VALUE_BYTES_COLUMN_PREFIX);
        let sql = format!(
            "SELECT \"id\", left(\"description\", 140) AS \"description\", 'T:3' AS \"{marker}\" FROM \"job_details\""
        );
        let options = QueryExecutionOptions {
            max_rows: Some(100),
            page_size: Some(100),
            table_data_preview: true,
            ..Default::default()
        };
        let mut result = invoke_external_driver_query_with_preview_retry(
            &session,
            &config,
            &sql,
            "",
            None,
            &options,
            Some(Duration::from_secs(5)),
        )
        .await
        .expect("preview retry should return the unwrapped value");
        let cells = extract_server_large_value_markers(&mut result);

        assert_eq!(result.rows, vec![vec![serde_json::json!(1), serde_json::json!("abc...")]]);
        assert_eq!(
            cells,
            vec![db::LargeValueCell {
                row_index: 0,
                column_index: 1,
                original_bytes: SERVER_LARGE_VALUE_UNKNOWN_BYTES,
                pending_decode: None,
                value_ref: None
            }]
        );

        let options = QueryExecutionOptions { max_rows: Some(100), table_data_preview: true, ..Default::default() };
        let mut result = invoke_external_driver_query_with_preview_retry(
            &session,
            &config,
            &sql,
            "",
            None,
            &options,
            Some(Duration::from_secs(5)),
        )
        .await
        .expect("non-paged preview retry should return the unwrapped value");
        let cells = extract_server_large_value_markers(&mut result);
        assert_eq!(result.rows, vec![vec![serde_json::json!(1), serde_json::json!("abc...")]]);
        assert_eq!(cells.len(), 1);
        assert_eq!(std::fs::read_to_string(&calls).unwrap().lines().count(), 4);

        session.shutdown().await.unwrap();
        let _ = std::fs::remove_dir_all(dir);
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn external_driver_manual_transaction_executes_commits_and_releases_session_pool() {
        use std::os::unix::fs::PermissionsExt;

        let dir = std::env::temp_dir().join(format!("dbx-jdbc-manual-txn-test-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&dir).unwrap();
        let executable = dir.join("plugin.sh");
        let calls = dir.join("calls.log");
        std::fs::write(
            &executable,
            format!(
                r#"#!/bin/sh
while IFS= read -r line; do
  id=$(printf '%s' "$line" | sed -E 's/.*"id":([0-9]+).*/\1/')
  case "$line" in
    *'"method":"executeInManualTransaction"'*)
      echo executeInManualTransaction >> '{}'
      printf '{{"id":%s,"result":{{"columns":["value"],"column_types":[],"column_sortables":[],"rows":[[42]],"affected_rows":0,"execution_time_ms":1,"truncated":false,"session_id":null,"has_more":false}}}}\n' "$id"
      ;;
    *'"method":"beginManualTransaction"'*)
      echo beginManualTransaction >> '{}'
      printf '{{"id":%s,"result":{{"ok":true}}}}\n' "$id"
      ;;
    *'"method":"read_large_value_chunk"'*|*'"method":"release_large_value"'*)
      echo lob >> '{}'
      printf '{{"id":%s,"result":{{"status":"ok","data":"original","next_offset":8,"eof":true,"value_kind":"text"}}}}\n' "$id"
      ;;
    *'"method":"commitManualTransaction"'*)
      echo commitManualTransaction >> '{}'
      printf '{{"id":%s,"result":{{"ok":true}}}}\n' "$id"
      ;;
  esac
done
"#,
                calls.display(),
                calls.display(),
                calls.display(),
                calls.display()
            ),
        )
        .unwrap();
        let mut permissions = std::fs::metadata(&executable).unwrap().permissions();
        permissions.set_mode(0o755);
        std::fs::set_permissions(&executable, permissions).unwrap();

        let plugin = InstalledPlugin {
            manifest: PluginManifest {
                id: "jdbc".to_string(),
                name: "JDBC".to_string(),
                version: "test".to_string(),
                protocol_version: 1,
                description: String::new(),
                executable: Some("plugin.sh".to_string()),
                drivers: vec![PluginDriverManifest {
                    id: "jdbc".to_string(),
                    label: "JDBC".to_string(),
                    kind: "external".to_string(),
                    database_type: Some("jdbc".to_string()),
                }],
                ..Default::default()
            },
            path: dir.clone(),
            compatibility: PluginCompatibility {
                compatible: true,
                backend_executable: Some(dir.join("plugin.sh")),
                ..Default::default()
            },
            provenance: None,
        };
        let session = Arc::new(
            PluginDriverSession::start_for_test(plugin, "jdbc".to_string(), PluginRuntimeEnv::default())
                .await
                .expect("plugin should start"),
        );
        let storage = crate::persistence::test_storage::open(&dir.join("storage.db")).await.unwrap();
        let state = AppState::new(storage);
        let mut config = test_connection_config(DatabaseType::Jdbc);
        config.id = "jdbc-conn".to_string();
        config.database = Some("dbx_test".to_string());
        config.connection_string = Some("jdbc:test".to_string());
        state.configs.write().await.insert(config.id.clone(), config.clone());

        session
            .invoke::<serde_json::Value>(
                "beginManualTransaction",
                serde_json::json!({ "connection": &config, "database": "dbx_test" }),
            )
            .await
            .unwrap();

        let client_session_id = "manual-txn-test";
        let pool_key = "jdbc-conn:session:manual-txn-test";
        let config = Arc::new(config);
        state
            .update_connection_pools(|connections| {
                connections.insert(
                    pool_key.to_string(),
                    PoolKind::ExternalDriver {
                        driver_id: "jdbc".to_string(),
                        config: config.clone(),
                        session: session.clone(),
                    },
                );
            })
            .await;
        let cleanup_guard =
            state.workload_session_pool_cleanup_guard("jdbc-conn", Some("dbx_test"), client_session_id).await.unwrap();
        state.transaction_sessions.write().await.insert(
            "txn-test".to_string(),
            TransactionSession {
                mysql_data_charset: None,
                sqlserver: None,
                connection: Arc::new(tokio::sync::Mutex::new(TxnConnection::ExternalDriver {
                    session,
                    config,
                    client_session_id: client_session_id.to_string(),
                    database: Some("dbx_test".to_string()),
                    cleanup_guard,
                })),
                pool_key: pool_key.to_string(),
                last_activity: std::time::Instant::now(),
                idle_timeout: std::time::Duration::from_secs(MANUAL_TRANSACTION_IDLE_TIMEOUT_SECS),
                busy: false,
                snapshot_rotation_safe: true,
                connection_id: "jdbc-conn".to_string(),
                database: "dbx_test".to_string(),
                schema: None,
            },
        );

        let results =
            execute_in_manual_transaction(&state, "txn-test", "SELECT 42", "dbx_test", None, Some(10)).await.unwrap();
        assert_eq!(results[0].rows, vec![vec![serde_json::json!(42)]]);
        let request: LargeValueRequest = serde_json::from_value(serde_json::json!({
            "connectionId":"jdbc-conn", "database":"dbx_test", "txnSessionId":"txn-test",
            "valueRef":"original-ref", "offset":0, "limit":4096
        }))
        .unwrap();
        for release in [false, true] {
            let mut other_connection = request.clone();
            other_connection.connection_id = "other-connection".into();
            assert_eq!(
                request_large_value(&state, other_connection, release).await.unwrap_err(),
                "LOB transaction context expired or busy"
            );
            state.transaction_sessions.write().await.get_mut("txn-test").unwrap().busy = true;
            assert_eq!(
                request_large_value(&state, request.clone(), release).await.unwrap_err(),
                "LOB transaction context expired or busy"
            );
            state.transaction_sessions.write().await.get_mut("txn-test").unwrap().busy = false;
            state.transaction_sessions.write().await.get_mut("txn-test").unwrap().last_activity =
                std::time::Instant::now() - Duration::from_secs(MANUAL_TRANSACTION_IDLE_TIMEOUT_SECS + 1);
            assert_eq!(
                request_large_value(&state, request.clone(), release).await.unwrap_err(),
                "LOB transaction context expired or busy"
            );
            state.transaction_sessions.write().await.get_mut("txn-test").unwrap().last_activity =
                std::time::Instant::now();
            assert_eq!(request_large_value(&state, request.clone(), release).await.unwrap()["data"], "original");
        }
        commit_manual_transaction(&state, "txn-test").await.unwrap();
        assert!(state.pool_handle(pool_key).await.is_none());
        for release in [false, true] {
            assert_eq!(
                request_large_value(&state, request.clone(), release).await.unwrap_err(),
                MANUAL_TRANSACTION_SESSION_NOT_FOUND_ERROR
            );
        }
        assert_eq!(
            std::fs::read_to_string(&calls).unwrap(),
            "beginManualTransaction\nexecuteInManualTransaction\nlob\nlob\ncommitManualTransaction\n"
        );

        state.shutdown(Duration::from_secs(1)).await;
        let _ = std::fs::remove_dir_all(dir);
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn external_driver_query_page_falls_back_to_legacy_execute_query() {
        use std::os::unix::fs::PermissionsExt;

        let dir = std::env::temp_dir().join(format!("dbx-legacy-jdbc-plugin-test-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&dir).unwrap();
        let executable = dir.join("plugin.sh");
        let calls = dir.join("calls.log");
        std::fs::write(
            &executable,
            format!(
                "#!/bin/sh\nwhile IFS= read -r line; do\n  id=$(printf '%s' \"$line\" | sed -E 's/.*\"id\":([0-9]+).*/\\1/')\n  case \"$line\" in\n    *'\"method\":\"executeQueryPage\"'*)\n      echo executeQueryPage >> '{}'\n      printf '{{\"id\":%s,\"error\":{{\"message\":\"Unsupported JDBC plugin method: executeQueryPage\"}}}}\\n' \"$id\"\n      ;;\n    *'\"method\":\"executeQuery\"'*)\n      echo executeQuery >> '{}'\n      printf '{{\"id\":%s,\"result\":{{\"columns\":[\"value\"],\"rows\":[[42]],\"affected_rows\":0,\"execution_time_ms\":1,\"truncated\":false}}}}\\n' \"$id\"\n      ;;\n  esac\ndone\n",
                calls.display(),
                calls.display()
            ),
        )
        .unwrap();
        let mut permissions = std::fs::metadata(&executable).unwrap().permissions();
        permissions.set_mode(0o755);
        std::fs::set_permissions(&executable, permissions).unwrap();

        let plugin = InstalledPlugin {
            manifest: PluginManifest {
                id: "jdbc".to_string(),
                name: "JDBC".to_string(),
                version: "legacy".to_string(),
                protocol_version: 1,
                description: String::new(),
                executable: Some("plugin.sh".to_string()),
                drivers: vec![PluginDriverManifest {
                    id: "jdbc".to_string(),
                    label: "JDBC".to_string(),
                    kind: "external".to_string(),
                    database_type: Some("jdbc".to_string()),
                }],
                ..Default::default()
            },
            path: dir.clone(),
            compatibility: PluginCompatibility {
                compatible: true,
                backend_executable: Some(dir.join("plugin.sh")),
                ..Default::default()
            },
            provenance: None,
        };
        let session = PluginDriverSession::start_for_test(plugin, "jdbc".to_string(), PluginRuntimeEnv::default())
            .await
            .expect("legacy plugin should start");

        let result = invoke_external_driver_query_page(
            &session,
            serde_json::json!({ "sql": "SELECT 42", "pageSize": 100 }),
            Some(Duration::from_secs(5)),
        )
        .await
        .expect("legacy executeQuery fallback should succeed");

        assert_eq!(result.columns, vec!["value"]);
        assert_eq!(result.rows, vec![vec![serde_json::json!(42)]]);
        assert_eq!(std::fs::read_to_string(&calls).unwrap(), "executeQueryPage\nexecuteQuery\n");

        session.shutdown().await.unwrap();
        let _ = std::fs::remove_dir_all(dir);
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn external_driver_query_page_does_not_retry_jdbc_errors() {
        use std::os::unix::fs::PermissionsExt;

        let dir = std::env::temp_dir().join(format!("dbx-jdbc-query-error-test-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&dir).unwrap();
        let executable = dir.join("plugin.sh");
        let calls = dir.join("calls.log");
        std::fs::write(
            &executable,
            format!(
                "#!/bin/sh\nwhile IFS= read -r line; do\n  id=$(printf '%s' \"$line\" | sed -E 's/.*\"id\":([0-9]+).*/\\1/')\n  echo request >> '{}'\n  printf '{{\"id\":%s,\"error\":{{\"message\":\"Incorrect syntax near SELECT\"}}}}\\n' \"$id\"\ndone\n",
                calls.display()
            ),
        )
        .unwrap();
        let mut permissions = std::fs::metadata(&executable).unwrap().permissions();
        permissions.set_mode(0o755);
        std::fs::set_permissions(&executable, permissions).unwrap();

        let plugin = InstalledPlugin {
            manifest: PluginManifest {
                id: "jdbc".to_string(),
                name: "JDBC".to_string(),
                version: "current".to_string(),
                protocol_version: 1,
                description: String::new(),
                executable: Some("plugin.sh".to_string()),
                drivers: vec![PluginDriverManifest {
                    id: "jdbc".to_string(),
                    label: "JDBC".to_string(),
                    kind: "external".to_string(),
                    database_type: Some("jdbc".to_string()),
                }],
                ..Default::default()
            },
            path: dir.clone(),
            compatibility: PluginCompatibility {
                compatible: true,
                backend_executable: Some(dir.join("plugin.sh")),
                ..Default::default()
            },
            provenance: None,
        };
        let session = PluginDriverSession::start_for_test(plugin, "jdbc".to_string(), PluginRuntimeEnv::default())
            .await
            .expect("plugin should start");

        let error = invoke_external_driver_query_page(
            &session,
            serde_json::json!({ "sql": "SELECT broken", "pageSize": 100 }),
            Some(Duration::from_secs(5)),
        )
        .await
        .expect_err("JDBC query errors must be returned without retrying");

        assert_eq!(error, "Incorrect syntax near SELECT");
        assert_eq!(std::fs::read_to_string(&calls).unwrap(), "request\n");

        session.shutdown().await.unwrap();
        let _ = std::fs::remove_dir_all(dir);
    }

    #[test]
    fn oceanbase_mysql_session_timeout_sql_uses_connection_timeout_by_default() {
        let mut config = test_connection_config(DatabaseType::Mysql);
        config.driver_profile = Some("oceanbase".to_string());
        config.query_timeout_secs = 300_000;

        assert_eq!(
            oceanbase_mysql_session_timeout_sql(Some(&config), None),
            Some("SET ob_query_timeout = 300000000000".to_string())
        );
    }

    #[test]
    fn oceanbase_mysql_session_timeout_sql_prefers_execution_timeout_override() {
        let mut config = test_connection_config(DatabaseType::Mysql);
        config.driver_profile = Some("oceanbase".to_string());
        config.query_timeout_secs = 30;

        assert_eq!(
            oceanbase_mysql_session_timeout_sql(Some(&config), Some(600)),
            Some("SET ob_query_timeout = 600000000".to_string())
        );
    }

    #[test]
    fn oceanbase_mysql_session_timeout_sql_skips_plain_mysql() {
        let config = test_connection_config(DatabaseType::Mysql);

        assert_eq!(oceanbase_mysql_session_timeout_sql(Some(&config), Some(600)), None);
    }

    #[tokio::test]
    async fn wait_for_query_returns_cancelled_when_token_is_cancelled() {
        let token = CancellationToken::new();
        token.cancel();

        let result = wait_for_query(Some(token), async {
            tokio::time::sleep(Duration::from_secs(30)).await;
            Ok(db::QueryResult {
                columns: vec![],
                column_types: Vec::new(),
                deferred_large_value_cells: Vec::new(),
                column_sortables: vec![],
                spatial_columns: vec![],
                raw_key_cells: Vec::new(),
                spatial_values: vec![],
                rows: vec![],
                affected_rows: 0,
                execution_time_ms: 0,
                server_execute_time_us: None,
                query_timings_ms: None,
                truncated: false,
                session_id: None,
                has_more: false,
                elasticsearch_raw_body: None,
                messages: Vec::new(),
            })
        })
        .await;

        assert_eq!(result.unwrap_err(), QUERY_CANCELED);
    }

    #[tokio::test]
    async fn wait_for_query_without_token_still_times_out() {
        let result = wait_for_query_with_timeout(None, Duration::from_millis(10), async {
            tokio::time::sleep(Duration::from_secs(1)).await;
            Ok(db::QueryResult {
                columns: vec![],
                column_types: Vec::new(),
                deferred_large_value_cells: Vec::new(),
                column_sortables: vec![],
                spatial_columns: vec![],
                raw_key_cells: Vec::new(),
                spatial_values: vec![],
                rows: vec![],
                affected_rows: 0,
                execution_time_ms: 0,
                server_execute_time_us: None,
                query_timings_ms: None,
                truncated: false,
                session_id: None,
                has_more: false,
                elasticsearch_raw_body: None,
                messages: Vec::new(),
            })
        })
        .await;

        assert_eq!(result.unwrap_err(), timeout_error_for(Duration::from_millis(10)));
    }

    #[tokio::test]
    async fn wait_for_value_opt_times_out_while_waiting_for_lock() {
        let lock = tokio::sync::Mutex::new(());
        let _guard = lock.lock().await;

        let result = wait_for_value_opt(None, Some(Duration::from_millis(10)), lock.lock()).await;

        assert_eq!(result.unwrap_err(), timeout_error_for(Duration::from_millis(10)));
    }

    #[tokio::test]
    async fn wait_for_value_opt_can_cancel_while_waiting_for_lock() {
        let lock = tokio::sync::Mutex::new(());
        let _guard = lock.lock().await;
        let token = CancellationToken::new();
        token.cancel();

        let result = wait_for_value_opt(Some(token), Some(Duration::from_secs(30)), lock.lock()).await;

        assert_eq!(result.unwrap_err(), QUERY_CANCELED);
    }

    #[test]
    fn db_operation_budget_from_config() {
        let budget = DbOperationBudget::from_config(10, Some(30));
        assert_eq!(budget.checkout_timeout, Duration::from_secs(10));
        assert_eq!(budget.connect_timeout, Duration::from_secs(10));
        assert_eq!(budget.recycle_timeout, Duration::from_secs(10));
        assert_eq!(budget.query_timeout, Some(Duration::from_secs(30)));
        assert_eq!(budget.cancel_timeout, Duration::from_secs(5));
        assert_eq!(budget.cleanup_timeout, Duration::from_secs(3));
    }

    #[test]
    fn db_operation_budget_from_connection_config_uses_connection_settings() {
        let mut config = test_connection_config(DatabaseType::Postgres);
        config.connect_timeout_secs = 12;
        config.query_timeout_secs = 0;

        let budget = DbOperationBudget::from_connection_config(&config);

        assert_eq!(budget.checkout_timeout, Duration::from_secs(12));
        assert_eq!(budget.connect_timeout, Duration::from_secs(12));
        assert_eq!(budget.recycle_timeout, Duration::from_secs(12));
        assert_eq!(budget.query_timeout, None);
        assert_eq!(budget.cancel_timeout, Duration::from_secs(5));
        assert_eq!(budget.cleanup_timeout, Duration::from_secs(3));
    }

    #[test]
    fn db_operation_budget_query_timeout_zero_means_no_limit() {
        let budget = DbOperationBudget::from_config(10, Some(0));
        assert_eq!(budget.query_timeout, None);
        // Infrastructure timeouts still have hard limits
        assert_eq!(budget.checkout_timeout, Duration::from_secs(10));
        assert_eq!(budget.cancel_timeout, Duration::from_secs(5));
    }

    #[test]
    fn db_operation_budget_query_timeout_zero_keeps_transaction_infra_limits() {
        let mut config = test_connection_config(DatabaseType::Mysql);
        config.connect_timeout_secs = 7;
        config.query_timeout_secs = 0;

        let budget = DbOperationBudget::from_connection_config(&config);

        assert_eq!(budget.query_timeout, None);
        assert_eq!(budget.checkout_timeout, Duration::from_secs(7));
        assert_eq!(budget.recycle_timeout, Duration::from_secs(7));
        assert_eq!(budget.cleanup_timeout, Duration::from_secs(3));
    }

    #[test]
    fn db_operation_budget_clamps_infra_timeout() {
        let budget = DbOperationBudget::from_config(0, Some(30));
        assert_eq!(budget.checkout_timeout, Duration::from_secs(1)); // clamped to min 1s
        let budget = DbOperationBudget::from_config(600, Some(30));
        assert_eq!(budget.checkout_timeout, Duration::from_secs(300)); // clamped to max 300s
    }

    #[test]
    fn db_operation_budget_with_defaults() {
        let budget = DbOperationBudget::with_defaults();
        assert_eq!(budget.checkout_timeout, db::connection_timeout());
        assert_eq!(budget.query_timeout, Some(QUERY_TIMEOUT));
    }

    #[test]
    fn is_connection_error_detects_english_messages() {
        assert!(is_connection_error("connection reset"));
        assert!(is_connection_error("broken pipe"));
        assert!(is_connection_error("reset by peer"));
        assert!(is_connection_error("Connection timed out"));
        assert!(is_connection_error("socket closed"));
        assert!(is_connection_error("unexpected eof"));
        assert!(is_connection_error("Error occurred while creating a new object: error communicating with the server"));
    }

    #[test]
    fn postgres_create_table_recovery_only_targets_persistent_tables() {
        assert_eq!(
            postgres_create_table_relation("CREATE TABLE \"app\".\"events\" (id bigint)"),
            Some((Some("app".to_string()), "events".to_string()))
        );
        assert_eq!(
            postgres_create_table_relation("-- ddl\nCREATE TABLE events (id bigint)"),
            Some((None, "events".to_string()))
        );
        assert_eq!(postgres_create_table_relation("CREATE TEMP TABLE events (id bigint)"), None);
        assert_eq!(postgres_create_table_relation("ALTER TABLE events ADD COLUMN note text"), None);
    }

    #[test]
    fn postgres_create_table_recovery_folds_unquoted_identifiers() {
        assert_eq!(
            postgres_create_table_relation("CREATE TABLE MyTable (id bigint)"),
            Some((None, "mytable".to_string()))
        );
        assert_eq!(
            postgres_create_table_relation("CREATE TABLE Core.Orders (id bigint)"),
            Some((Some("core".to_string()), "orders".to_string()))
        );
        assert_eq!(
            postgres_create_table_relation("CREATE TABLE \"Core\".\"Orders\" (id bigint)"),
            Some((Some("Core".to_string()), "Orders".to_string()))
        );
    }

    #[test]
    fn postgres_create_table_recovery_requires_native_connection_loss() {
        let sql = "CREATE TABLE events (id bigint)";
        assert!(should_verify_postgres_create_table_after_connection_error(
            Some(DatabaseType::Postgres),
            sql,
            "connection closed; PostgreSQL schema.reset cleanup failed: connection closed"
        ));
        assert!(should_verify_postgres_create_table_after_connection_error(
            Some(DatabaseType::Gaussdb),
            sql,
            "connection reset by peer"
        ));
        assert!(!should_verify_postgres_create_table_after_connection_error(
            Some(DatabaseType::Postgres),
            sql,
            "ERROR: permission denied"
        ));
        assert!(!should_verify_postgres_create_table_after_connection_error(
            Some(DatabaseType::Vastbase),
            sql,
            "connection closed"
        ));
        assert!(is_postgres_duplicate_relation_error("ERROR: relation \"events\" already exists"));
        assert!(!is_postgres_duplicate_relation_error("ERROR: permission denied for schema public"));
    }

    #[test]
    fn query_error_context_omits_raw_sql_and_is_not_duplicated() {
        let sql = "select 'secret-123' as token";
        let error = query_error_with_omitted_sql_context("driver rejected statement", sql);

        assert!(error.contains("driver rejected statement"));
        assert!(error.contains(SQL_OMITTED_ERROR_CONTEXT));
        assert!(!error.contains("secret-123"));
        assert!(!error.contains("SQL:"));

        let repeated = query_error_with_omitted_sql_context(&error, sql);
        assert_eq!(repeated.matches(SQL_OMITTED_ERROR_CONTEXT).count(), 1);
    }

    #[test]
    fn typed_sql_error_context_keeps_driver_text_on_one_line() {
        let error = QueryExecutionError::Sql("Server error: `ERROR 1064 (42000): syntax error`".to_string())
            .with_omitted_sql_context("SELECT * FROM users")
            .into_backend_error();

        assert_eq!(
            error.detail(),
            Some(
                "Server error: `ERROR 1064 (42000): syntax error` SQL text omitted from user-facing error; enable debug SQL diagnostics to inspect the original statement."
            )
        );
    }

    #[test]
    fn reconnect_retry_error_context_omits_raw_sql() {
        let sql = "select 'secret-123' as token";
        let reconnect_error = query_error_with_omitted_sql_context("connection reset after reconnect", sql);

        assert!(reconnect_error.contains("connection reset after reconnect"));
        assert!(reconnect_error.contains(SQL_OMITTED_ERROR_CONTEXT));
        assert!(!reconnect_error.contains("secret-123"));
    }

    #[test]
    fn execute_statements_error_omits_raw_sql() {
        let sql = "select 'secret-token' as t";
        let err = query_error_with_omitted_sql_context(
            &format!(
                "Statement {} failed: {}. Previous {} statement(s) may have been committed.",
                2, "driver error", 1
            ),
            sql,
        );

        assert!(err.contains("driver error"));
        assert!(err.contains(SQL_OMITTED_ERROR_CONTEXT));
        assert!(!err.contains("secret-token"));
        assert!(!err.contains("SQL:"));
        assert!(err.contains("Statement 2 failed:"));
    }

    #[test]
    fn batch_transaction_error_omits_raw_sql() {
        let sql = "delete from users where id = 'secret-id'";
        let err = query_error_with_omitted_sql_context(
            &format!("Statement {} failed: {}. No transaction support for this database type.", 3, "batch error"),
            sql,
        );

        assert!(err.contains("batch error"));
        assert!(err.contains(SQL_OMITTED_ERROR_CONTEXT));
        assert!(!err.contains("secret-id"));
        assert!(err.contains("Statement 3 failed:"));
    }

    #[test]
    fn is_connection_error_detects_oracle_idle_timeout() {
        assert!(is_connection_error("ORA-02396: exceeded maximum idle time, please connect again"));
        assert!(is_connection_error(
            "Agent RPC error (-32603): ORA-02396: exceeded maximum idle time, please connect again"
        ));
        assert!(is_connection_error("ORA-03113: end-of-file on communication channel"));
        assert!(is_connection_error("ORA-03114: not connected to Oracle"));
        assert!(is_connection_error("ORA-03135: connection lost contact"));
        assert!(is_connection_error("Agent RPC error (-1): java.sql.SQLRecoverableException: 关闭的连接"));
        assert!(is_connection_error("java.sql.SQLRecoverableException: 连接已关闭"));
    }

    #[test]
    fn is_connection_error_detects_localized_io_errors() {
        assert!(is_connection_error("I/O error: 远程主机强迫关闭了一个现有的连接。 (os error 10054)"));
        assert!(is_connection_error(
            "I/O error: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。 (os error 10060)"
        ));
        assert!(is_connection_error("Agent RPC error (-1): dm.jdbc.driver.DMException: 网络通信异常"));
        assert!(is_connection_error(
            "Agent RPC error (-1): java.sql.SQLRecoverableException: IO 错误: Got minus one from a read call"
        ));
        assert!(is_connection_error(
            "Agent RPC error (-1): com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure"
        ));
    }

    #[test]
    fn is_connection_error_detects_os_error_codes() {
        assert!(is_connection_error("os error 10053"));
        assert!(is_connection_error("os error 10054"));
        assert!(is_connection_error("os error 10060"));
        assert!(is_connection_error("os error 10061"));
    }

    #[test]
    fn is_connection_error_rejects_non_connection_errors() {
        assert!(!is_connection_error("Query timed out after 30 seconds"));
        assert!(!is_connection_error("ORA-00942: table or view does not exist"));
        assert!(!is_connection_error("syntax error at position 5"));
        assert!(!is_connection_error("os error 13"));
    }

    #[test]
    fn is_connection_error_distinguishes_checkout_saturation_from_connection_timeouts() {
        // deadpool-postgres PoolError::Timeout messages (contain "pool" + "timeout" but not "timed out")
        assert!(is_connection_error("pool wait timeout"));
        assert!(is_connection_error("pool create timeout"));
        assert!(is_connection_error("pool recycle timeout"));
        // checkout helper timeout messages
        assert!(!is_connection_error("PostgreSQL connection pool checkout timed out [stage=wait, timeout_ms=5000]"));
        assert!(is_connection_error("PostgreSQL connection pool checkout timed out [stage=create, timeout_ms=5000]"));
        assert!(is_connection_error("PostgreSQL connection pool checkout timed out [stage=recycle, timeout_ms=5000]"));
        assert!(!is_connection_error("MySQL connection pool checkout timed out [stage=wait, timeout_ms=5000]"));
        assert!(!is_connection_error(METADATA_POOL_BUSY_ERROR));
        assert!(is_connection_error("MySQL ping timed out"));
        assert!(is_connection_error("MySQL kill connection checkout timed out"));
        assert!(is_connection_error("MySQL KILL QUERY timed out"));

        assert!(is_pool_saturation_error(
            "PostgreSQL connection pool checkout timed out [stage=wait, timeout_ms=5000]"
        ));
        assert!(is_pool_saturation_error("MySQL connection pool checkout timed out [stage=wait, timeout_ms=5000]"));
        assert!(!is_pool_saturation_error(
            "PostgreSQL connection pool checkout timed out [stage=create, timeout_ms=5000]"
        ));
        assert!(is_pool_saturation_error(METADATA_POOL_BUSY_ERROR));
        assert_eq!(
            pool_error_action(
                Some(DatabaseType::Mysql),
                "MySQL connection pool checkout timed out [stage=wait, timeout_ms=5000]"
            ),
            PoolErrorAction::Keep
        );
        assert_eq!(
            pool_error_action(
                Some(DatabaseType::Postgres),
                "PostgreSQL connection pool checkout timed out [stage=wait, timeout_ms=5000]"
            ),
            PoolErrorAction::Keep
        );
    }

    #[test]
    fn pool_error_action_discards_sqlserver_driver_panic_without_retry() {
        let err = format!("{} the current client will be rebuilt.", db::sqlserver::SQLSERVER_DRIVER_PANIC_ERROR_PREFIX);

        assert_eq!(pool_error_action(Some(DatabaseType::SqlServer), &err), PoolErrorAction::Discard);
        assert!(should_discard_pool_after_error(Some(DatabaseType::SqlServer), &err));
        assert!(!is_connection_error(&err));
    }

    #[test]
    fn pool_error_action_discards_sqlserver_timeout_without_retry() {
        let err = "Query timed out after 30 seconds";

        assert_eq!(pool_error_action(Some(DatabaseType::SqlServer), err), PoolErrorAction::Discard);
        assert_eq!(pool_error_action(Some(DatabaseType::Mysql), err), PoolErrorAction::Discard);
        assert_eq!(pool_error_action(Some(DatabaseType::Postgres), err), PoolErrorAction::Discard);
        assert_eq!(pool_error_action(Some(DatabaseType::ClickHouse), err), PoolErrorAction::Discard);
        assert_eq!(pool_error_action(Some(DatabaseType::Oracle), err), PoolErrorAction::Discard);
        assert_eq!(pool_error_action(Some(DatabaseType::Sqlite), err), PoolErrorAction::Keep);
        assert_eq!(pool_error_action(Some(DatabaseType::DuckDb), err), PoolErrorAction::Keep);
    }

    #[test]
    fn pool_error_action_discards_schema_reset_cleanup_without_retry() {
        let err = "PostgreSQL schema.reset cleanup failed: PostgreSQL schema.reset timed out after 3 seconds";

        assert_eq!(pool_error_action(Some(DatabaseType::Postgres), err), PoolErrorAction::Discard);
        assert_eq!(pool_error_action(Some(DatabaseType::OpenGauss), err), PoolErrorAction::Discard);
        assert!(should_discard_pool_after_error(Some(DatabaseType::Postgres), err));
    }

    #[test]
    fn pool_error_action_discards_postgres_read_only_transaction_cleanup_without_retry() {
        let err = "PostgreSQL read-only transaction cleanup failed: PostgreSQL explain_analyze.rollback timed out after 3 seconds";

        assert_eq!(pool_error_action(Some(DatabaseType::Postgres), err), PoolErrorAction::Discard);
        assert!(should_discard_pool_after_error(Some(DatabaseType::Postgres), err));
    }

    #[test]
    fn pool_error_action_reconnects_non_agent_errors_but_never_replays_agent_operations() {
        let err = "connection reset by peer";

        assert_eq!(pool_error_action(Some(DatabaseType::SqlServer), err), PoolErrorAction::ReconnectAndRetry);
        assert_eq!(pool_error_action(Some(DatabaseType::Postgres), err), PoolErrorAction::ReconnectAndRetry);

        let dameng_err = "Agent RPC error (-1): dm.jdbc.driver.DMException: 网络通信异常";
        assert_eq!(pool_error_action(Some(DatabaseType::Dameng), dameng_err), PoolErrorAction::Discard);
    }

    #[test]
    fn external_driver_query_params_include_database_and_schema_context() {
        let config = ConnectionConfig {
            oracle_oci_nls_lang: None,
            oracle_oci_tns_admin: None,
            docs_notes_path: None,
            id: "jdbc-1".to_string(),
            name: "JDBC".to_string(),
            note: String::new(),
            db_type: DatabaseType::Jdbc,
            driver_profile: None,
            driver_label: None,
            url_params: None,
            agent_java_options: Vec::new(),
            host: "localhost".to_string(),
            port: 0,
            username: String::new(),
            password: String::new(),
            database: None,
            default_schema: None,
            visible_databases: None,
            visible_database_patterns: None,
            visible_schemas: None,
            show_system_schemas: false,
            sidebar_auto_load_all_tables: false,
            show_database_links: None,
            show_native_jobs: None,
            show_user_admin: None,
            attached_databases: Vec::new(),
            init_script: None,
            color: None,
            transport_layers: Vec::new(),
            connect_timeout_secs: 5,
            query_timeout_secs: 30,
            idle_timeout_secs: 60,
            manual_transaction_timeout_secs: 300,
            idle_disconnect_secs: 0,
            keepalive_interval_secs: 0,
            ssl: false,
            ca_cert_path: String::new(),
            client_cert_path: String::new(),
            client_key_path: String::new(),
            sysdba: false,
            oracle_connection_type: None,
            connection_string: Some("jdbc:h2:mem:test".to_string()),
            redis_connection_mode: None,
            redis_sentinel_master: String::new(),
            redis_sentinel_nodes: String::new(),
            redis_sentinel_username: String::new(),
            redis_sentinel_password: String::new(),
            redis_sentinel_tls: false,
            redis_cluster_nodes: String::new(),
            redis_key_separator: default_redis_key_separator(),
            redis_scan_page_size: None,
            redis_database_aliases: Default::default(),
            redis_key_templates: Vec::new(),
            redis_key_filter: None,
            redis_key_grouping: None,
            etcd_endpoints: String::new(),
            gbase_server: String::new(),
            informix_server: String::new(),
            external_config: None,
            plugin_id: None,
            plugin_connection_provider: None,
            plugin_connection_type: None,
            connection_secrets: Default::default(),
            jdbc_driver_class: None,
            jdbc_driver_paths: Vec::new(),
            one_time: false,
            save_password: true,
            read_only: false,
            is_production: false,
            production_databases: vec![],
            production_badge_scope: None,
            database_info: None,
        };

        let params = external_driver_query_params(
            &config,
            "SELECT * FROM events",
            "analytics",
            Some("app"),
            &QueryExecutionOptions {
                max_rows: Some(500),
                fetch_size: Some(250),
                row_offset: Some(100),
                timeout_secs: Some(600),
                ..Default::default()
            },
        );

        assert_eq!(params["connection"]["id"], "jdbc-1");
        assert_eq!(params["sql"], "SELECT * FROM events");
        assert_eq!(params["database"], "analytics");
        assert_eq!(params["schema"], "app");
        assert_eq!(params["maxRows"], 500);
        assert_eq!(params["fetchSize"], 250);
        assert_eq!(params["rowOffset"], 100);
        assert_eq!(params["timeoutSecs"], 600);
    }

    #[test]
    fn agent_execute_query_params_include_row_and_fetch_limits() {
        let params = agent_execute_query_params(
            "SELECT * FROM events",
            Some("analytics"),
            Some("app"),
            QueryExecutionOptions {
                max_rows: Some(500),
                fetch_size: Some(250),
                row_offset: Some(100),
                timeout_secs: Some(600),
                table_data_preview: true,
                ..Default::default()
            },
        );

        assert_eq!(params["sql"], "SELECT * FROM events");
        assert_eq!(params["database"], "analytics");
        assert_eq!(params["schema"], "app");
        assert_eq!(params["maxRows"], 500);
        assert_eq!(params["fetchSize"], 250);
        assert_eq!(params["rowOffset"], 100);
        assert_eq!(params["timeoutSecs"], 600);
        assert_eq!(params["deferLobs"], true);
    }

    #[test]
    fn manual_transaction_agent_query_options_preserve_preview_mode() {
        let params = agent_execute_query_params(
            "SELECT * FROM documents",
            Some("ORCL"),
            Some("APP"),
            manual_txn_agent_query_options(250, true, None, None),
        );

        assert_eq!(params["maxRows"], 250);
        assert_eq!(params["deferLobs"], true);
    }

    #[test]
    fn agent_execution_context_omits_unsupported_schema_switches() {
        assert_eq!(schema_for_execution_context(Some(DatabaseType::Iris), Some("SQLUser")), None);
        assert_eq!(schema_for_execution_context(Some(DatabaseType::SqlServer), Some("dbo")), None);
        assert_eq!(schema_for_execution_context(Some(DatabaseType::Oracle), Some("APP")), Some("APP"));
        assert_eq!(schema_for_execution_context(None, Some("APP")), Some("APP"));
    }

    #[test]
    fn iris_execution_context_qualifies_unqualified_dml_tables() {
        assert_eq!(
            sql_for_execution_context(Some(DatabaseType::Iris), "SELECT * FROM TABLES", Some("INFORMATION_SCHEMA")),
            "SELECT * FROM INFORMATION_SCHEMA.TABLES"
        );
        let qualified_join = sql_for_execution_context(
            Some(DatabaseType::Iris),
            "SELECT * FROM orders o JOIN customers c ON c.id = o.customer_id",
            Some("Sales"),
        );
        assert!(qualified_join.contains("FROM Sales.orders"));
        assert!(qualified_join.contains("JOIN Sales.customers"));
        assert!(qualified_join.contains("c.id = o.customer_id"));
        assert_eq!(
            sql_for_execution_context(Some(DatabaseType::Iris), "SELECT * FROM INFORMATION_SCHEMA.TABLES", Some("APP")),
            "SELECT * FROM INFORMATION_SCHEMA.TABLES"
        );
    }

    #[test]
    fn iris_execution_context_qualifies_nested_dml_tables_but_not_ctes() {
        assert_eq!(
            sql_for_execution_context(
                Some(DatabaseType::Iris),
                "WITH recent AS (SELECT * FROM events) SELECT * FROM recent WHERE EXISTS (SELECT 1 FROM audits)",
                Some("APP")
            ),
            "WITH recent AS (SELECT * FROM APP.events) SELECT * FROM recent WHERE EXISTS (SELECT 1 FROM APP.audits)"
        );
        assert_eq!(
            sql_for_execution_context(
                Some(DatabaseType::Iris),
                "INSERT INTO events SELECT * FROM staging_events",
                Some("APP")
            ),
            "INSERT INTO APP.events SELECT * FROM APP.staging_events"
        );
        assert_eq!(
            sql_for_execution_context(
                Some(DatabaseType::Iris),
                "UPDATE events SET status = 'done' WHERE id IN (SELECT event_id FROM audit_events)",
                Some("APP")
            ),
            "UPDATE APP.events SET status = 'done' WHERE id IN (SELECT event_id FROM APP.audit_events)"
        );
    }

    #[test]
    fn iris_execution_context_keeps_schema_unquoted_for_delimited_identifier_less_servers() {
        // A double-quoted schema is turned into a `:%qpar` parameter by the
        // Caché/IRIS JDBC preparser when delimited identifiers are disabled,
        // which fails at prepare. Ordinary names must stay unquoted; only
        // spellings that need a delimited name keep the quote characters.
        let qualified = sql_for_execution_context_with_identifier_quote(
            Some(DatabaseType::Iris),
            "SELECT * FROM events",
            Some("SQLUser"),
            Some("\""),
        );
        assert_eq!(qualified, "SELECT * FROM SQLUser.events");
        assert!(!qualified.contains('"'));

        let quoted = sql_for_execution_context_with_identifier_quote(
            Some(DatabaseType::Iris),
            "SELECT * FROM events",
            Some("My Schema"),
            Some("\""),
        );
        assert_eq!(quoted, "SELECT * FROM \"My Schema\".events");
    }

    #[test]
    fn iris_execution_context_leaves_ddl_and_unparseable_sql_unchanged() {
        assert_eq!(
            sql_for_execution_context(Some(DatabaseType::Iris), "CREATE TABLE events (id INT)", Some("APP")),
            "CREATE TABLE events (id INT)"
        );
        assert_eq!(
            sql_for_execution_context(Some(DatabaseType::Iris), "SELECT %ID FROM", Some("APP")),
            "SELECT %ID FROM"
        );
        assert_eq!(
            sql_for_execution_context(Some(DatabaseType::Postgres), "SELECT * FROM events", Some("APP")),
            "SELECT * FROM events"
        );
    }

    #[test]
    fn sqlserver_execution_context_qualifies_unqualified_dml_tables() {
        assert_eq!(
            sql_for_execution_context(Some(DatabaseType::SqlServer), "SELECT TOP 1 * FROM products", Some("core")),
            "SELECT TOP 1 * FROM [core].products"
        );

        let qualified_join = sql_for_execution_context(
            Some(DatabaseType::SqlServer),
            "SELECT p.id FROM products p JOIN customers c ON c.id = p.customer_id",
            Some("sales"),
        );
        assert!(qualified_join.contains("FROM [sales].products p"), "{qualified_join}");
        assert!(qualified_join.contains("JOIN [sales].customers c"), "{qualified_join}");

        assert_eq!(
            sql_for_execution_context(
                Some(DatabaseType::SqlServer),
                "UPDATE products SET status = 'done' WHERE id IN (SELECT product_id FROM audit_products)",
                Some("core")
            ),
            "UPDATE [core].products SET status = 'done' WHERE id IN (SELECT product_id FROM [core].audit_products)"
        );
    }

    #[test]
    fn sqlserver_execution_context_skips_update_target_aliases() {
        let qualified = sql_for_execution_context(
            Some(DatabaseType::SqlServer),
            "UPDATE p SET p.status = 'done' FROM products p JOIN customers c ON c.id = p.customer_id",
            Some("core"),
        );
        assert!(qualified.starts_with("UPDATE p SET"), "{qualified}");
        assert!(qualified.contains("FROM [core].products p"), "{qualified}");
        assert!(qualified.contains("JOIN [core].customers c ON c.id = p.customer_id"), "{qualified}");
        assert!(!qualified.contains("UPDATE [core].p"), "{qualified}");
        assert!(!qualified.contains("[core].c ON"), "{qualified}");
    }

    #[test]
    fn sqlserver_execution_context_preserves_ctes_qualified_tables_and_temp_tables() {
        assert_eq!(
            sql_for_execution_context(
                Some(DatabaseType::SqlServer),
                "WITH recent AS (SELECT * FROM products) SELECT * FROM recent",
                Some("core")
            ),
            "WITH recent AS (SELECT * FROM [core].products) SELECT * FROM recent"
        );
        assert_eq!(
            sql_for_execution_context(
                Some(DatabaseType::SqlServer),
                "SELECT * FROM [archive].products UNION ALL SELECT * FROM #staged_products",
                Some("core")
            ),
            "SELECT * FROM [archive].products UNION ALL SELECT * FROM #staged_products"
        );
        assert_eq!(
            sql_for_execution_context(Some(DatabaseType::SqlServer), "CREATE TABLE products (id INT)", Some("core")),
            "CREATE TABLE products (id INT)"
        );
    }

    #[test]
    fn kingbase_execution_context_qualifies_only_unqualified_relations() {
        assert_eq!(
            sql_for_execution_context(Some(DatabaseType::Kingbase), "SELECT * FROM sys_user", Some("app")),
            "SELECT * FROM \"app\".sys_user"
        );
        assert_eq!(
            sql_for_execution_context(Some(DatabaseType::Kingbase), "SELECT * FROM other_schema.sys_user", Some("app")),
            "SELECT * FROM other_schema.sys_user"
        );

        let mixed = sql_for_execution_context(
            Some(DatabaseType::Kingbase),
            "SELECT pg_typeof(u.id) FROM generate_series(1, 2) AS n JOIN sys_user u ON true",
            Some("app"),
        );
        assert!(mixed.contains("FROM generate_series(1, 2) AS n"), "{mixed}");
        assert!(mixed.contains("JOIN \"app\".sys_user u"), "{mixed}");
        assert!(mixed.contains("pg_typeof(u.id)"), "{mixed}");
    }

    #[test]
    fn kingbase_execution_context_preserves_ctes_functions_types_and_unsupported_sql() {
        assert_eq!(
            sql_for_execution_context(
                Some(DatabaseType::Kingbase),
                "WITH current_user AS (SELECT * FROM sys_user) SELECT * FROM current_user",
                Some("APP")
            ),
            "WITH current_user AS (SELECT * FROM \"APP\".sys_user) SELECT * FROM current_user"
        );
        assert_eq!(
            sql_for_execution_context(
                Some(DatabaseType::Kingbase),
                "SELECT pg_typeof(1::int), current_user",
                Some("APP")
            ),
            "SELECT pg_typeof(1::int), current_user"
        );
        assert_eq!(
            sql_for_execution_context(Some(DatabaseType::Kingbase), "CREATE TABLE sys_user (id INT)", Some("APP")),
            "CREATE TABLE sys_user (id INT)"
        );
        assert_eq!(
            sql_for_execution_context(Some(DatabaseType::Kingbase), "SELECT * FROM", Some("APP")),
            "SELECT * FROM"
        );
    }

    #[test]
    fn kingbase_execution_context_uses_driver_identifier_quote() {
        assert_eq!(
            sql_for_execution_context_with_identifier_quote(
                Some(DatabaseType::Kingbase),
                "SELECT * FROM sys_user",
                Some("audit-schema"),
                Some("`"),
            ),
            "SELECT * FROM `audit-schema`.sys_user"
        );
        assert_eq!(
            sql_for_execution_context_with_identifier_quote(
                Some(DatabaseType::Kingbase),
                "SELECT * FROM sys_user",
                Some("audit-schema"),
                Some("\""),
            ),
            "SELECT * FROM \"audit-schema\".sys_user"
        );
        assert_eq!(
            sql_for_execution_context_with_identifier_quote(
                Some(DatabaseType::Kingbase),
                "SELECT * FROM sys_user",
                Some("audit-schema"),
                Some("unsupported"),
            ),
            "SELECT * FROM \"audit-schema\".sys_user"
        );
    }

    #[test]
    fn parses_postgres_drop_database_target() {
        assert_eq!(parse_drop_database_target("DROP DATABASE vaultwarden;"), Some("vaultwarden".to_string()));
        assert_eq!(parse_drop_database_target("drop database if exists \"app db\";"), Some("app db".to_string()));
        assert_eq!(
            parse_drop_database_target("/*x*/ DROP DATABASE \"app\"\"db\" -- trailing\n;"),
            Some("app\"db".to_string())
        );
    }

    #[test]
    fn ignores_non_single_drop_database_statements() {
        assert_eq!(parse_drop_database_target("DROP TABLE vaultwarden;"), None);
        assert_eq!(parse_drop_database_target("DROP DATABASE vaultwarden; SELECT 1;"), None);
        assert_eq!(parse_drop_database_target("DROP DATABASE 123bad;"), None);
    }

    #[test]
    fn chooses_safe_postgres_drop_database_admin_database() {
        assert_eq!(postgres_drop_database_admin_database("vaultwarden"), "postgres");
        assert_eq!(postgres_drop_database_admin_database("postgres"), "template1");
    }

    #[test]
    fn agent_execute_query_params_default_to_safety_row_limit() {
        let params = agent_execute_query_params("SELECT * FROM events", None, None, QueryExecutionOptions::default());

        assert_eq!(params["sql"], "SELECT * FROM events");
        assert!(params.get("database").is_none());
        assert!(params.get("schema").is_none());
        assert_eq!(params["maxRows"], MAX_ROWS);
        assert!(params.get("fetchSize").is_none());
        assert!(params.get("rowOffset").is_none());
        assert!(params.get("timeoutSecs").is_none());
    }

    #[test]
    fn agent_execute_query_page_params_include_page_fetch_and_safety_limits() {
        let params = agent_execute_query_page_params(
            "SELECT * FROM events",
            Some("analytics"),
            Some("app"),
            QueryExecutionOptions {
                page_size: Some(500),
                fetch_size: Some(250),
                row_offset: Some(100),
                timeout_secs: Some(600),
                table_data_preview: true,
                ..Default::default()
            },
        );

        assert_eq!(params["sql"], "SELECT * FROM events");
        assert_eq!(params["database"], "analytics");
        assert_eq!(params["schema"], "app");
        assert_eq!(params["pageSize"], 500);
        assert_eq!(params["fetchSize"], 250);
        assert_eq!(params["rowOffset"], 100);
        assert_eq!(params["timeoutSecs"], 600);
        assert_eq!(params["maxRows"], MAX_ROWS);
        assert_eq!(params["deferLobs"], true);
    }

    #[test]
    fn agent_query_row_counts_are_clamped_to_java_signed_int_range() {
        let oversized = AGENT_PROTOCOL_MAX_ROWS.saturating_add(1);
        let params = agent_execute_query_page_params(
            "SELECT * FROM events",
            None,
            None,
            QueryExecutionOptions {
                page_size: Some(oversized),
                fetch_size: Some(oversized),
                max_rows: Some(oversized),
                row_offset: Some(oversized),
                ..Default::default()
            },
        );

        assert_eq!(params["pageSize"], AGENT_PROTOCOL_MAX_ROWS);
        assert_eq!(params["fetchSize"], AGENT_PROTOCOL_MAX_ROWS);
        assert_eq!(params["maxRows"], AGENT_PROTOCOL_MAX_ROWS);
        assert_eq!(params["rowOffset"], AGENT_PROTOCOL_MAX_ROWS);
        assert_eq!(agent_fetch_query_page_params("session-1", oversized)["pageSize"], AGENT_PROTOCOL_MAX_ROWS);
    }

    #[test]
    fn agent_fetch_query_page_params_include_session_and_page_size() {
        let params = agent_fetch_query_page_params("session-1", 500);

        assert_eq!(params["sessionId"], "session-1");
        assert_eq!(params["pageSize"], 500);
    }

    #[test]
    fn manual_transaction_agent_query_uses_cursor_for_later_pages() {
        let first_page = manual_txn_agent_query_request(
            "SELECT ID FROM APP.EVENTS ORDER BY ID",
            Some("ORCL"),
            Some("APP"),
            manual_txn_agent_query_options(10_000, false, Some(100), None),
        );
        let ManualTxnAgentQueryRequest::ExecutePage(first_params) = first_page else {
            panic!("first page must start an Agent query cursor");
        };
        assert_eq!(first_params["pageSize"], 100);
        assert_eq!(first_params["maxRows"], 10_000);

        let second_page = manual_txn_agent_query_request(
            "SELECT ID FROM APP.EVENTS ORDER BY ID",
            Some("ORCL"),
            Some("APP"),
            manual_txn_agent_query_options(10_000, false, Some(100), Some("oracle-go-1")),
        );
        let ManualTxnAgentQueryRequest::FetchPage(second_params) = second_page else {
            panic!("later pages must continue the existing Agent query cursor");
        };
        assert_eq!(second_params["sessionId"], "oracle-go-1");
        assert_eq!(second_params["pageSize"], 100);
        assert!(second_params.get("sql").is_none());
    }

    /// Spawns a fake Python agent and registers a manual transaction session in
    /// the app state so `execute_in_manual_transaction_with_options` can run
    /// end to end without a live database.
    #[test]
    fn manual_transaction_classification_pairs_statements_and_fails_closed() {
        // MySQL/PostgreSQL route through the strict sql_risk proof.
        assert_eq!(classify_manual_transaction_statements(Some(DatabaseType::Mysql), 1, Some("SELECT 1")), vec![true]);
        assert_eq!(
            classify_manual_transaction_statements(Some(DatabaseType::Postgres), 1, Some("SELECT * FROM users")),
            vec![true]
        );
        // GaussDB/openGauss share the PostgreSQL-family proof.
        assert_eq!(
            classify_manual_transaction_statements(Some(DatabaseType::Gaussdb), 1, Some("SELECT * FROM users")),
            vec![true]
        );
        assert_eq!(
            classify_manual_transaction_statements(Some(DatabaseType::OpenGauss), 1, Some("UPDATE users SET x = 1")),
            vec![false]
        );
        // Mixed script: every statement is classified individually.
        assert_eq!(
            classify_manual_transaction_statements(Some(DatabaseType::Mysql), 2, Some("SELECT 1; DELETE FROM t")),
            vec![true, false]
        );
        // Session-state writes fail the proof (SELECT ... INTO @var).
        assert_eq!(
            classify_manual_transaction_statements(Some(DatabaseType::Mysql), 1, Some("SELECT 1 INTO @x")),
            vec![false]
        );
        // Count mismatch, missing classification SQL, non-participating dialects
        // and unknown connections are all fail-closed (no markers).
        assert!(classify_manual_transaction_statements(Some(DatabaseType::Mysql), 3, Some("SELECT 1")).is_empty());
        assert!(classify_manual_transaction_statements(Some(DatabaseType::Mysql), 1, None).is_empty());
        assert!(classify_manual_transaction_statements(Some(DatabaseType::Doris), 1, Some("SELECT 1")).is_empty());
        assert!(classify_manual_transaction_statements(None, 1, Some("SELECT 1")).is_empty());
        // Oracle/OceanBase-Oracle share the AST proof and keep the pairing
        // behavior; built-in functions are now provable (#11788) while the
        // Oracle guard rails (dblink, sequence pseudocolumns, set operators)
        // still fail the proof.
        assert_eq!(
            classify_manual_transaction_statements(Some(DatabaseType::Oracle), 1, Some("SELECT * FROM EMP")),
            vec![true]
        );
        assert_eq!(
            classify_manual_transaction_statements(Some(DatabaseType::Oracle), 1, Some("SELECT LENGTH(1) FROM dual")),
            vec![true]
        );
        assert_eq!(
            classify_manual_transaction_statements(
                Some(DatabaseType::OceanbaseOracle),
                2,
                Some("SELECT COUNT(*) FROM EMP; SELECT * FROM EMP@LINK")
            ),
            vec![true, false]
        );
        assert_eq!(
            classify_manual_transaction_statements(Some(DatabaseType::OceanbaseOracle), 1, Some("DELETE FROM EMP")),
            vec![false]
        );
    }

    /// Spawns a fake Python agent and registers a manual transaction session in
    /// the app state so `execute_in_manual_transaction_with_options` can run
    /// end to end without a live database.
    async fn manual_transaction_test_state(db_type: DatabaseType) -> (AppState, String, std::path::PathBuf) {
        use std::io::Write;

        let mut script = tempfile::NamedTempFile::new().unwrap();
        write!(
            script,
            r#"import json
import sys

print(json.dumps({{"ready": True}}), flush=True)
cursor_ids = {{"cursor-1"}}
for line in sys.stdin:
    request = json.loads(line)
    method = request.get("method")
    if method == "close_query_session":
        cursor_id = request.get("params", {{}}).get("sessionId")
        closed = cursor_id in cursor_ids
        cursor_ids.discard(cursor_id)
        print(json.dumps({{"jsonrpc": "2.0", "id": request["id"], "result": closed}}), flush=True)
        continue
    if method == "execute_query_page":
        # First cursor page: two rows and an open result session on this
        # transaction connection.
        params = request.get("params", {{}})
        page_size = int(params.get("pageSize") or 1)
        rows = [["page-row-" + str(index)] for index in range(1, min(page_size, 2) + 1)]
        result = {{
            "columns": ["val"],
            "column_types": [],
            "column_sortables": [],
            "rows": rows,
            "affected_rows": 0,
            "execution_time_ms": 1,
            "truncated": False,
            "session_id": "cursor-1",
            "has_more": True
        }}
        print(json.dumps({{"jsonrpc": "2.0", "id": request["id"], "result": result}}), flush=True)
        continue
    if method == "fetch_query_page":
        # Continuation page of the same cursor: one last row.
        result = {{
            "columns": ["val"],
            "column_types": [],
            "column_sortables": [],
            "rows": [["page-row-3"]],
            "affected_rows": 0,
            "execution_time_ms": 1,
            "truncated": False,
            "session_id": "cursor-1",
            "has_more": False
        }}
        print(json.dumps({{"jsonrpc": "2.0", "id": request["id"], "result": result}}), flush=True)
        continue
    if method == "execute_query":
        params = request.get("params", {{}})
        sql = params.get("sql", "")
        if "REQUIRE_APP_SCHEMA" in sql and params.get("schema") != "APP":
            print(json.dumps({{"jsonrpc": "2.0", "id": request["id"], "error": {{"code": -32000, "message": "schema was not forwarded"}}}}), flush=True)
            continue
        if "RAISE_FILE_ERROR" in sql:
            print(json.dumps({{"jsonrpc": "2.0", "id": request["id"], "error": {{"code": -32000, "message": "file statement rejected"}}}}), flush=True)
            continue
        row = [sql]
    else:
        # commit_manual_transaction / rollback_manual_transaction / disconnect
        row = []
    result = {{
        "columns": ["stmt"],
        "column_types": [],
        "column_sortables": [],
        "rows": [row],
        "affected_rows": 0,
        "execution_time_ms": 1,
        "truncated": False,
        "session_id": None,
        "has_more": False
    }}
    print(json.dumps({{"jsonrpc": "2.0", "id": request["id"], "result": result}}), flush=True)
"#
        )
        .unwrap();
        script.flush().unwrap();

        let python = if cfg!(windows) { "python" } else { "python3" };
        let client = AgentDriverClient::spawn(
            AgentLaunchSpec::new(python).with_args([script.path().to_string_lossy().to_string()]),
        )
        .await
        .unwrap();

        let dir = std::env::temp_dir().join(format!("dbx-manual-txn-{}", uuid::Uuid::new_v4().simple()));
        std::fs::create_dir_all(&dir).unwrap();
        let storage = crate::persistence::test_storage::open(&dir.join("storage.db")).await.unwrap();
        let state = AppState::new(storage);
        let mut config = test_connection_config(db_type);
        config.id = "agent-conn".to_string();
        state.configs.write().await.insert(config.id.clone(), config);

        let txn_session_id = uuid::Uuid::new_v4().to_string();
        let client_session_id = format!("manual-txn-{}", uuid::Uuid::new_v4());
        // The session pool is not registered in the state map; on commit or
        // rollback the detach reports "not found" and the cleanup guard is
        // disarmed, matching production flows where the pool was already gone.
        let cleanup_guard = state
            .workload_session_pool_cleanup_guard("agent-conn", None, &client_session_id)
            .await
            .expect("agent connections get a session-scoped cleanup guard");
        state.transaction_sessions.write().await.insert(
            txn_session_id.clone(),
            TransactionSession {
                mysql_data_charset: None,
                sqlserver: None,
                connection: Arc::new(tokio::sync::Mutex::new(TxnConnection::Agent {
                    client: Arc::new(crate::db::agent_driver::PooledAgentClient::new(client)),
                    client_session_id,
                    database: None,
                    cleanup_guard,
                })),
                pool_key: "agent-conn".to_string(),
                last_activity: std::time::Instant::now(),
                idle_timeout: std::time::Duration::from_secs(MANUAL_TRANSACTION_IDLE_TIMEOUT_SECS),
                busy: false,
                snapshot_rotation_safe: true,
                connection_id: "agent-conn".to_string(),
                database: "ORCL".to_string(),
                schema: Some("APP".to_string()),
            },
        );
        (state, txn_session_id, dir)
    }

    #[tokio::test]
    async fn manual_transaction_cursor_cleanup_uses_its_owner_without_ending_transaction() {
        let (state, txn_id, dir) = manual_transaction_test_state(DatabaseType::Oracle).await;
        assert!(close_manual_transaction_query_session(&state, "other-connection", &txn_id, "cursor-1").await.is_err());
        assert!(!close_manual_transaction_query_session(&state, "agent-conn", "missing-txn", "cursor-1")
            .await
            .unwrap());
        // Only the held transaction has an Agent. A lookup through the normal
        // connection pool cannot close this cursor.
        assert!(close_manual_transaction_query_session(&state, "agent-conn", &txn_id, "cursor-1").await.unwrap());
        assert!(!close_manual_transaction_query_session(&state, "agent-conn", &txn_id, "cursor-1").await.unwrap());
        let result = execute_in_manual_transaction_with_options(
            &state,
            &txn_id,
            "SELECT 1 FROM DUAL",
            "ORCL",
            Some("APP"),
            ManualTransactionExecutionOptions::default(),
        )
        .await
        .unwrap();
        assert_eq!(result[0].result.rows[0][0], "SELECT 1 FROM DUAL");
        assert!(state.transaction_sessions.read().await.contains_key(&txn_id));
        rollback_manual_transaction(&state, &txn_id).await.unwrap();
        assert!(!close_manual_transaction_query_session(&state, "agent-conn", &txn_id, "cursor-1").await.unwrap());
        drop(state);
        let _ = std::fs::remove_dir_all(dir);
    }

    #[tokio::test]
    async fn manual_transaction_agent_cursor_support_is_agent_only() {
        let (state, txn_id, dir) = manual_transaction_test_state(DatabaseType::Oracle).await;
        // The fake Oracle transaction holds an Agent connection, so exports may
        // page it through an Agent result session.
        assert!(manual_transaction_supports_agent_cursor(&state, &txn_id).await);
        assert!(!manual_transaction_supports_agent_cursor(&state, "missing-txn").await);

        // A native transaction connection only pages with SQL.
        let mysql_state = AppState::new(crate::persistence::test_storage::open(&dir.join("mysql.db")).await.unwrap());
        let mysql_txn_id = uuid::Uuid::new_v4().to_string();
        mysql_state.transaction_sessions.write().await.insert(
            mysql_txn_id.clone(),
            TransactionSession {
                mysql_data_charset: None,
                sqlserver: None,
                connection: Arc::new(tokio::sync::Mutex::new(TxnConnection::Mysql(None))),
                pool_key: "mysql-conn".to_string(),
                last_activity: std::time::Instant::now(),
                idle_timeout: std::time::Duration::from_secs(60),
                busy: false,
                snapshot_rotation_safe: false,
                connection_id: "mysql-conn".to_string(),
                database: String::new(),
                schema: None,
            },
        );
        assert!(!manual_transaction_supports_agent_cursor(&mysql_state, &mysql_txn_id).await);

        rollback_manual_transaction(&state, &txn_id).await.unwrap();
        drop(mysql_state);
        drop(state);
        let _ = std::fs::remove_dir_all(dir);
    }

    /// Builds a query-result export request for the fake Agent transaction above.
    fn manual_transaction_export_request(
        txn_id: &str,
        file_path: &std::path::Path,
        sql: &str,
        page_size: usize,
    ) -> crate::data::query_result_export::QueryResultExportRequest {
        serde_json::from_value(serde_json::json!({
            "exportId": "export-1",
            "connectionId": "agent-conn",
            "database": "ORCL",
            "schema": "APP",
            "sql": sql,
            "queryBaseSql": sql,
            "databaseType": "oracle",
            "useAgentCursor": true,
            "filePath": file_path.to_string_lossy(),
            "format": "csv",
            "pageSize": page_size,
            "txnSessionId": txn_id,
        }))
        .unwrap()
    }

    #[tokio::test]
    async fn oracle_query_result_export_pages_through_the_manual_transaction_cursor() {
        let (state, txn_id, dir) = manual_transaction_test_state(DatabaseType::Oracle).await;
        let csv_path = dir.join("cursor-export.csv");
        // Wildcard projection: no safe ROWNUM wrapper, so the export needs the
        // Agent result session to reach the second page — inside the transaction.
        let request = manual_transaction_export_request(&txn_id, &csv_path, "SELECT * FROM APP.EVENTS", 2);
        let progress = Arc::new(std::sync::Mutex::new(Vec::new()));
        let progress_sink = progress.clone();
        crate::data::query_result_export::export_query_result_core(&state, &request, None, move |update| {
            progress_sink.lock().unwrap().push(update);
        })
        .await
        .expect("manual-transaction export pages through the transaction cursor");

        let csv = std::fs::read_to_string(&csv_path).unwrap();
        for row in ["page-row-1", "page-row-2", "page-row-3"] {
            assert!(csv.contains(row), "missing {row} in {csv}");
        }
        let terminal = progress.lock().unwrap().last().cloned().unwrap();
        assert!(matches!(terminal.status, crate::database_export::ExportStatus::Done));
        assert_eq!(terminal.rows_exported, 3);

        rollback_manual_transaction(&state, &txn_id).await.unwrap();
        drop(state);
        let _ = std::fs::remove_dir_all(dir);
    }

    #[tokio::test]
    async fn oracle_rownum_bounded_export_runs_once_inside_the_manual_transaction() {
        let (state, txn_id, dir) = manual_transaction_test_state(DatabaseType::Oracle).await;
        let csv_path = dir.join("bounded-export.csv");
        let sql = "SELECT FJFY, JCBW FROM T_YL_JCZD01 WHERE ROWNUM < 2000;";
        let request = manual_transaction_export_request(&txn_id, &csv_path, sql, 2000);
        crate::data::query_result_export::export_query_result_core(&state, &request, None, |_| {})
            .await
            .expect("ROWNUM-bounded export runs as one bounded transaction execution");

        let csv = std::fs::read_to_string(&csv_path).unwrap();
        // The fake transaction connection answers the statement itself (no
        // cursor page fetch), proving the single-execution path was taken. The
        // transaction executor strips the trailing semicolon, hence the prefix
        // assertion rather than a byte-for-byte match.
        assert!(csv.contains("FJFY") && csv.contains("ROWNUM < 2000"), "unexpected export content: {csv}");
        assert!(!csv.contains("page-row"), "bounded export must not open a cursor: {csv}");

        rollback_manual_transaction(&state, &txn_id).await.unwrap();
        drop(state);
        let _ = std::fs::remove_dir_all(dir);
    }

    fn manual_sql_file_request(session_id: &str) -> crate::sql::SqlFileRequest {
        crate::sql::SqlFileRequest {
            txn_session_id: Some(session_id.to_string()),
            execution_id: "manual-file-test".to_string(),
            connection_id: "agent-conn".to_string(),
            database: "ORCL".to_string(),
            schema: Some("APP".to_string()),
            file_path: String::new(),
            continue_on_error: false,
            selected_tables: None,
            part_cooldown_ms: 0,
            skip_relational_constraints: false,
            failure_log_enabled: true,
            resolve_includes: false,
            encoding: Default::default(),
        }
    }

    #[tokio::test]
    async fn manual_sql_file_retains_session_until_commit() {
        let (state, session_id, dir) = manual_transaction_test_state(DatabaseType::OceanbaseOracle).await;
        let request = manual_sql_file_request(&session_id);
        let mut events = Vec::new();
        // Only the held transaction has a connected agent. A fallback through
        // the ordinary pool cannot execute these statements successfully.
        crate::data::sql_file_import::execute_sql_file_content(
            &state,
            &request,
            "SELECT REQUIRE_APP_SCHEMA; UPDATE T SET V = 2;",
            CancellationToken::new(),
            std::time::Instant::now(),
            |event| events.push(event),
        )
        .await
        .unwrap();
        let terminal = events.last().unwrap();
        assert_eq!(terminal.status, crate::sql::SqlFileStatus::Done);
        assert_eq!(terminal.success_count, 2);
        assert!(state.transaction_sessions.read().await.contains_key(&session_id));
        commit_manual_transaction(&state, &session_id).await.unwrap();
        assert!(!state.transaction_sessions.read().await.contains_key(&session_id));
        drop(state);
        let _ = std::fs::remove_dir_all(dir);
    }

    #[tokio::test]
    async fn manual_sql_file_cancel_stops_next_statement_and_releases_session() {
        let (state, session_id, dir) = manual_transaction_test_state(DatabaseType::OceanbaseOracle).await;
        let request = manual_sql_file_request(&session_id);
        let token = CancellationToken::new();
        let mut events = Vec::new();
        crate::data::sql_file_import::execute_sql_file_content(
            &state,
            &request,
            "UPDATE T SET V = 1; UPDATE T SET V = 2;",
            token.clone(),
            std::time::Instant::now(),
            |event| {
                if event.success_count == 1 {
                    token.cancel();
                }
                events.push(event);
            },
        )
        .await
        .unwrap();
        let terminal = events.last().unwrap();
        assert_eq!(terminal.status, crate::sql::SqlFileStatus::Cancelled);
        assert_eq!(terminal.success_count, 1);
        assert!(!state.transaction_sessions.read().await.contains_key(&session_id));
        drop(state);
        let _ = std::fs::remove_dir_all(dir);
    }

    #[tokio::test]
    async fn manual_sql_file_read_failure_rolls_back_held_session() {
        let (state, session_id, dir) = manual_transaction_test_state(DatabaseType::OceanbaseOracle).await;
        let request = manual_sql_file_request(&session_id);
        let result = crate::data::sql_file_import::execute_sql_file_path(
            &state,
            &request,
            &dir.join("missing.sql"),
            CancellationToken::new(),
            std::time::Instant::now(),
            |_| {},
        )
        .await;
        assert!(result.is_err());
        assert!(!state.transaction_sessions.read().await.contains_key(&session_id));
        drop(state);
        let _ = std::fs::remove_dir_all(dir);
    }

    #[tokio::test]
    async fn manual_sql_file_statement_failure_rolls_back_without_retry_or_later_execution() {
        let (state, session_id, dir) = manual_transaction_test_state(DatabaseType::OceanbaseOracle).await;
        let request = manual_sql_file_request(&session_id);
        let mut events = Vec::new();
        let error = crate::data::sql_file_import::execute_sql_file_content(
            &state,
            &request,
            "UPDATE T SET V = 1; UPDATE RAISE_FILE_ERROR SET V = 2; UPDATE T SET V = 3;",
            CancellationToken::new(),
            std::time::Instant::now(),
            |event| events.push(event),
        )
        .await
        .unwrap_err();
        assert!(error.contains("file statement rejected"));
        let terminal = events.last().unwrap();
        assert_eq!(terminal.status, crate::sql::SqlFileStatus::Error);
        assert_eq!(terminal.success_count, 1);
        assert_eq!(terminal.failure_count, 1);
        assert_eq!(terminal.statement_index, 2);
        assert!(!state.transaction_sessions.read().await.contains_key(&session_id));
        drop(state);
        let _ = std::fs::remove_dir_all(dir);
    }

    #[tokio::test]
    async fn manual_sql_file_rejects_different_target_without_using_or_closing_its_session() {
        let (state, session_id, dir) = manual_transaction_test_state(DatabaseType::OceanbaseOracle).await;
        for mismatch_connection in [false, true] {
            let mut request = manual_sql_file_request(&session_id);
            if mismatch_connection {
                request.connection_id = "other".to_string();
            } else {
                request.database = "other".to_string();
            }
            let mut events = Vec::new();
            let error = crate::data::sql_file_import::execute_sql_file_content(
                &state,
                &request,
                "UPDATE T SET V = 1;",
                CancellationToken::new(),
                std::time::Instant::now(),
                |event| events.push(event),
            )
            .await
            .unwrap_err();
            assert!(error.contains("target does not match"));
            assert!(events.is_empty());
            assert!(state.transaction_sessions.read().await.contains_key(&session_id));
        }
        let mut request = manual_sql_file_request(&session_id);
        request.schema = Some("OTHER".to_string());
        let mut events = Vec::new();
        let error = crate::data::sql_file_import::execute_sql_file_content(
            &state,
            &request,
            "UPDATE T SET V = 1;",
            CancellationToken::new(),
            std::time::Instant::now(),
            |event| events.push(event),
        )
        .await
        .unwrap_err();
        assert!(error.contains("target does not match"));
        assert!(events.is_empty());
        assert!(state.transaction_sessions.read().await.contains_key(&session_id));

        rollback_manual_transaction(&state, &session_id).await.unwrap();
        let error = crate::data::sql_file_import::execute_sql_file_content(
            &state,
            &manual_sql_file_request(&session_id),
            "UPDATE T SET V = 1;",
            CancellationToken::new(),
            std::time::Instant::now(),
            |_| {},
        )
        .await
        .unwrap_err();
        assert!(error.contains("session not found"));
        drop(state);
        let _ = std::fs::remove_dir_all(dir);
    }

    /// Regression: multi-statement scripts under a manual transaction must keep
    /// executing sequentially even when pagination options are set — they are
    /// ignored instead of failing the script (single-statement pagination stays
    /// cursor-based).
    #[cfg(unix)]
    #[tokio::test]
    async fn manual_transaction_multi_statement_script_ignores_pagination_options() {
        let (state, txn_session_id, dir) = manual_transaction_test_state(DatabaseType::Oracle).await;

        let results = execute_in_manual_transaction_with_options(
            &state,
            &txn_session_id,
            "SELECT 1 FROM DUAL; SELECT 2 FROM DUAL",
            "ORCL",
            None,
            ManualTransactionExecutionOptions { max_rows: Some(100), page_size: Some(100), ..Default::default() },
        )
        .await
        .expect("multi-statement script executes sequentially");

        assert_eq!(results.len(), 2);
        assert_eq!(results[0].result.rows[0][0], serde_json::json!("SELECT 1 FROM DUAL"));
        assert_eq!(results[1].result.rows[0][0], serde_json::json!("SELECT 2 FROM DUAL"));
        // Both statements ran on the plain execute path — no cursor session was
        // opened, so nothing leaks a query cursor on the agent.
        assert!(results.iter().all(|result| result.result.session_id.is_none()));

        assert!(rollback_manual_transaction(&state, &txn_session_id).await.is_ok());
        let _ = std::fs::remove_dir_all(dir);
    }

    /// #11788: the Oracle manual-transaction marker must follow the user's SQL
    /// through the real execution path — `execute_in_manual_transaction_with_options`
    /// → statement/classification pairing → per-statement marker — because that
    /// marker is what the frontend aggregates into `tab.txnPossiblyDirty` and the
    /// toolbar's Commit/Rollback visibility. The fake agent echoes each statement,
    /// so this needs no live database. It does NOT cover the Java driver or a real
    /// Oracle server, and it does not render the toolbar; the two-way real-Oracle
    /// acceptance run is still outstanding.
    #[tokio::test]
    async fn oracle_manual_transaction_marks_only_proven_read_only_statements() {
        async fn markers(state: &AppState, txn_session_id: &str, sql: &str) -> Vec<bool> {
            execute_in_manual_transaction_with_options(
                state,
                txn_session_id,
                sql,
                "ORCL",
                None,
                ManualTransactionExecutionOptions { classification_sql: Some(sql.to_string()), ..Default::default() },
            )
            .await
            .expect("statement executes through the manual transaction session")
            .into_iter()
            .map(|result| result.manual_transaction_proven_read_only)
            .collect()
        }

        let (state, txn_session_id, dir) = manual_transaction_test_state(DatabaseType::Oracle).await;

        // Proven reads: the toolbar must stay clean, including the built-in
        // functions and non-call parentheses the old blanket `(` rule rejected.
        for sql in [
            "SELECT LENGTH(1) FROM dual",
            "SELECT COUNT(*) FROM EMP",
            "SELECT UPPER(name) FROM EMP",
            "SELECT NVL(name, 'x') FROM EMP",
            "SELECT * FROM EMP WHERE id IN (1, 2, 3)",
            "SELECT CURRENT_TIME FROM dual",
        ] {
            assert_eq!(markers(&state, &txn_session_id, sql).await, vec![true], "expected proven: {sql}");
        }

        // Unproven statements must still dirty the session.
        for sql in [
            "SELECT my_udf(id) FROM EMP",
            "SELECT DBMS_LOCK.SLEEP(1) FROM dual",
            "SELECT * FROM EMP@LINK",
            "SELECT seq.NEXTVAL FROM dual",
            "SELECT * FROM EMP FOR UPDATE",
            "UPDATE EMP SET name = 'x'",
        ] {
            assert_eq!(markers(&state, &txn_session_id, sql).await, vec![false], "expected unproven: {sql}");
        }

        // A mixed script is classified per statement and paired by position, so
        // one unproven statement keeps the session dirty.
        assert_eq!(
            markers(&state, &txn_session_id, "SELECT COUNT(*) FROM EMP; SELECT * FROM EMP@LINK").await,
            vec![true, false]
        );

        // The marker is the frontend contract: present in JSON only when proven.
        let proven = execute_in_manual_transaction_with_options(
            &state,
            &txn_session_id,
            "SELECT UPPER(name) FROM EMP",
            "ORCL",
            None,
            ManualTransactionExecutionOptions {
                classification_sql: Some("SELECT UPPER(name) FROM EMP".to_string()),
                ..Default::default()
            },
        )
        .await
        .expect("proven statement executes");
        assert_eq!(
            serde_json::to_value(&proven[0]).unwrap()["manual_transaction_proven_read_only"],
            serde_json::json!(true)
        );
        let unproven = execute_in_manual_transaction_with_options(
            &state,
            &txn_session_id,
            "SELECT my_udf(id) FROM EMP",
            "ORCL",
            None,
            ManualTransactionExecutionOptions {
                classification_sql: Some("SELECT my_udf(id) FROM EMP".to_string()),
                ..Default::default()
            },
        )
        .await
        .expect("unproven statement executes");
        assert!(serde_json::to_value(&unproven[0]).unwrap().get("manual_transaction_proven_read_only").is_none());

        assert!(rollback_manual_transaction(&state, &txn_session_id).await.is_ok());
        let _ = std::fs::remove_dir_all(dir);
    }

    #[test]
    fn agent_close_query_session_params_include_session() {
        let params = agent_close_query_session_params("session-1");

        assert_eq!(params["sessionId"], "session-1");
    }

    #[test]
    fn multi_statement_execution_does_not_retain_query_cursors() {
        let options = QueryExecutionOptions {
            max_rows: Some(100_000),
            fetch_size: Some(100),
            page_size: Some(100),
            timeout_secs: Some(30),
            ..Default::default()
        };

        for db_type in [DatabaseType::Kingbase, DatabaseType::Vastbase, DatabaseType::Oracle] {
            let adjusted = options_for_sequential_statements(&options, 2, Some(db_type));

            assert_eq!(adjusted.page_size, None);
            assert_eq!(adjusted.max_rows, Some(100));
            assert_eq!(adjusted.fetch_size, Some(100));
            assert_eq!(adjusted.timeout_secs, Some(30));
        }
    }

    #[test]
    fn multi_statement_execution_preserves_smaller_row_limit() {
        let options = QueryExecutionOptions { max_rows: Some(25), page_size: Some(100), ..Default::default() };

        let adjusted = options_for_sequential_statements(&options, 2, Some(DatabaseType::Kingbase));

        assert_eq!(adjusted.page_size, None);
        assert_eq!(adjusted.max_rows, Some(25));
    }

    #[test]
    fn single_statement_and_existing_cursor_execution_keep_paging_options() {
        let first_page = QueryExecutionOptions { max_rows: Some(100_000), page_size: Some(100), ..Default::default() };
        let next_page = QueryExecutionOptions {
            max_rows: Some(100_000),
            page_size: Some(100),
            result_session_id: Some("session-1".to_string()),
            ..Default::default()
        };

        let single = options_for_sequential_statements(&first_page, 1, Some(DatabaseType::Kingbase));
        let existing_cursor = options_for_sequential_statements(&next_page, 2, Some(DatabaseType::Kingbase));

        assert_eq!(single.page_size, Some(100));
        assert_eq!(single.max_rows, Some(100_000));
        assert_eq!(existing_cursor.page_size, Some(100));
        assert_eq!(existing_cursor.result_session_id.as_deref(), Some("session-1"));
    }

    #[test]
    fn other_databases_keep_multi_statement_cursor_options() {
        let options = QueryExecutionOptions { max_rows: Some(100_000), page_size: Some(100), ..Default::default() };

        let adjusted = options_for_sequential_statements(&options, 2, Some(DatabaseType::Dameng));

        assert_eq!(adjusted.page_size, Some(100));
        assert_eq!(adjusted.max_rows, Some(100_000));
    }

    #[test]
    fn agent_timeout_discards_pool_but_does_not_retry_same_query() {
        assert!(should_discard_agent_pool_after_error("Query timed out after 30 seconds"));
        assert!(should_discard_agent_pool_after_error("Agent RPC call timed out (30s)"));
        assert!(!is_connection_error("Agent RPC call timed out (30s)"));
        assert_eq!(
            pool_error_action(Some(DatabaseType::Oracle), "Agent RPC call timed out (30s)"),
            PoolErrorAction::Discard
        );
    }

    #[test]
    fn structured_agent_disposition_controls_pool_recovery() {
        let quarantined = "Agent RPC error (-1): lost\nDBX_AGENT_ERROR_DATA:{\"category\":\"connection\",\"sessionDisposition\":\"quarantine\"}";
        let replace_runtime = "Agent RPC error (-1): saturated\nDBX_AGENT_ERROR_DATA:{\"category\":\"resource\",\"sessionDisposition\":\"replace_runtime\"}";

        assert!(should_discard_agent_pool_after_error(quarantined));
        assert!(should_discard_agent_pool_after_error(replace_runtime));
        assert_eq!(pool_error_action(Some(DatabaseType::Oracle), quarantined), PoolErrorAction::Discard);
        assert_eq!(pool_error_action(Some(DatabaseType::Oracle), replace_runtime), PoolErrorAction::Discard);
    }

    #[test]
    fn unavailable_agent_pipes_discard_without_replaying_user_operations() {
        assert!(should_discard_agent_pool_after_error("Agent stdin not available"));
        assert!(should_discard_agent_pool_after_error("Agent stdout not available"));
        assert!(is_connection_error("Agent stdin not available"));
        assert!(is_connection_error("Agent stdout not available"));
        assert!(is_connection_error("Agent runtime terminated"));
        assert!(is_connection_error("Agent runtime is unavailable"));
        assert_eq!(
            pool_error_action(Some(DatabaseType::Oracle), "Agent stdin not available"),
            PoolErrorAction::Discard
        );
        assert_eq!(pool_error_action(Some(DatabaseType::Oracle), "Agent runtime terminated"), PoolErrorAction::Discard);
        assert_eq!(
            pool_error_action(Some(DatabaseType::Oracle), "Agent runtime is unavailable"),
            PoolErrorAction::Discard
        );
    }

    #[test]
    fn query_results_convert_unsafe_json_integers_to_strings_for_js() {
        let result = db::QueryResult {
            columns: vec!["id".to_string(), "nested".to_string()],
            column_types: Vec::new(),
            deferred_large_value_cells: Vec::new(),
            column_sortables: vec![],
            spatial_columns: vec![],
            raw_key_cells: Vec::new(),
            spatial_values: vec![],
            rows: vec![vec![
                serde_json::json!(2_041_797_190_226_354_178_i64),
                serde_json::json!([1, 2_041_797_190_226_354_178_i64]),
            ]],
            affected_rows: 0,
            execution_time_ms: 0,
            server_execute_time_us: None,
            query_timings_ms: None,
            truncated: false,
            session_id: None,
            has_more: false,
            elasticsearch_raw_body: None,
            messages: Vec::new(),
        };

        let serialized = serde_json::to_value(result).unwrap();

        assert_eq!(serialized["rows"][0][0], serde_json::json!("2041797190226354178"));
        assert_eq!(serialized["rows"][0][1], serde_json::json!([1, "2041797190226354178"]));
    }

    #[test]
    fn extracts_server_large_value_markers_and_restores_source_columns() {
        let mut result = db::QueryResult {
            columns: vec![
                "id".to_string(),
                "payload".to_string(),
                format!("{}T_1", crate::sql_dialect::DBX_LARGE_VALUE_BYTES_COLUMN_PREFIX),
                "note".to_string(),
            ],
            column_types: vec!["integer".to_string(), "text".to_string(), "bigint".to_string(), "text".to_string()],
            deferred_large_value_cells: Vec::new(),
            column_sortables: vec![true, true, true, true],
            spatial_columns: Vec::new(),
            raw_key_cells: Vec::new(),
            spatial_values: Vec::new(),
            rows: vec![
                vec![
                    serde_json::json!(1),
                    serde_json::json!("预览文本多"),
                    serde_json::json!("T:4"),
                    serde_json::json!("a"),
                ],
                vec![serde_json::json!(2), serde_json::json!("短值"), serde_json::json!("T:4"), serde_json::json!("b")],
            ],
            affected_rows: 0,
            execution_time_ms: 0,
            server_execute_time_us: None,
            query_timings_ms: None,
            truncated: false,
            session_id: None,
            has_more: false,
            elasticsearch_raw_body: None,
            messages: Vec::new(),
        };

        let cells = extract_server_large_value_markers(&mut result);

        assert_eq!(result.columns, vec!["id", "payload", "note"]);
        assert_eq!(result.column_types, vec!["integer", "text", "text"]);
        assert_eq!(
            result.rows[0],
            vec![serde_json::json!(1), serde_json::json!("预览文本..."), serde_json::json!("a")]
        );
        assert_eq!(result.rows[1], vec![serde_json::json!(2), serde_json::json!("短值"), serde_json::json!("b")]);
        assert_eq!(
            cells,
            vec![db::LargeValueCell {
                row_index: 0,
                column_index: 1,
                original_bytes: SERVER_LARGE_VALUE_UNKNOWN_BYTES,
                pending_decode: None,
                value_ref: None
            }]
        );
    }

    #[test]
    fn extracts_oracle_rowid_preview_markers_indexed_by_result_position() {
        // 无主键 Oracle 基表的截断投影以 __DBX_ROWID 开头,marker 下标是结果集
        // 位置(源列在结果中的下标),不是表列下标——位置校验失败会整组拒识。
        let mut result = db::QueryResult {
            columns: vec![
                "__DBX_ROWID".to_string(),
                "ID".to_string(),
                "PAYLOAD".to_string(),
                format!("{}T_2", crate::sql_dialect::DBX_LARGE_VALUE_BYTES_COLUMN_PREFIX),
            ],
            column_types: vec![
                "varchar2".to_string(),
                "number".to_string(),
                "varchar2".to_string(),
                "varchar2".to_string(),
            ],
            deferred_large_value_cells: Vec::new(),
            column_sortables: vec![true, true, true, true],
            spatial_columns: Vec::new(),
            raw_key_cells: Vec::new(),
            spatial_values: Vec::new(),
            rows: vec![
                vec![
                    serde_json::json!("AAAR1yAAHAAAAFjAAA"),
                    serde_json::json!(1),
                    serde_json::json!("x".repeat(1000)),
                    serde_json::json!("T:999:5231"),
                ],
                vec![
                    serde_json::json!("AAAR1yAAHAAAAFjAAB"),
                    serde_json::json!(2),
                    serde_json::Value::Null,
                    serde_json::Value::Null,
                ],
            ],
            affected_rows: 0,
            execution_time_ms: 0,
            server_execute_time_us: None,
            query_timings_ms: None,
            truncated: false,
            session_id: None,
            has_more: false,
            elasticsearch_raw_body: None,
            messages: Vec::new(),
        };

        let cells = extract_server_large_value_markers(&mut result);

        assert_eq!(result.columns, vec!["__DBX_ROWID", "ID", "PAYLOAD"]);
        assert_eq!(result.rows.len(), 2);
        assert_eq!(result.rows[0][2], serde_json::json!(format!("{}...", "x".repeat(999))));
        assert_eq!(result.rows[1][2], serde_json::Value::Null);
        assert_eq!(
            cells,
            vec![db::LargeValueCell {
                row_index: 0,
                column_index: 2,
                original_bytes: 5231,
                pending_decode: None,
                value_ref: None
            }]
        );
    }

    #[test]
    fn extracts_lowercased_server_large_value_markers_from_case_folding_servers() {
        // KingbaseES instances with case-insensitive identifiers return even
        // quoted marker aliases folded to lowercase; the markers must still be
        // consumed and stripped instead of leaking into the data grid.
        let mut result = db::QueryResult {
            columns: vec![
                "id".to_string(),
                "payload".to_string(),
                "__dbx_large_value_bytes_t_1".to_string(),
                "note".to_string(),
                "__dbx_large_value_bytes_t_2".to_string(),
            ],
            column_types: vec![
                "integer".to_string(),
                "text".to_string(),
                "text".to_string(),
                "text".to_string(),
                "text".to_string(),
            ],
            deferred_large_value_cells: Vec::new(),
            column_sortables: vec![true; 5],
            spatial_columns: Vec::new(),
            raw_key_cells: Vec::new(),
            spatial_values: Vec::new(),
            rows: vec![
                vec![
                    serde_json::json!(1),
                    serde_json::json!("长文本预览内容"),
                    serde_json::json!("T:4"),
                    serde_json::json!("abcdefgh"),
                    serde_json::json!("T:4"),
                ],
                vec![
                    serde_json::json!(2),
                    serde_json::json!("短值"),
                    serde_json::json!("T:4"),
                    serde_json::json!("b"),
                    serde_json::json!("T:4"),
                ],
            ],
            affected_rows: 0,
            execution_time_ms: 0,
            server_execute_time_us: None,
            query_timings_ms: None,
            truncated: false,
            session_id: None,
            has_more: false,
            elasticsearch_raw_body: None,
            messages: Vec::new(),
        };

        let cells = extract_server_large_value_markers(&mut result);

        assert_eq!(result.columns, vec!["id", "payload", "note"]);
        assert_eq!(result.column_types, vec!["integer", "text", "text"]);
        assert_eq!(
            result.rows[0],
            vec![serde_json::json!(1), serde_json::json!("长文本预..."), serde_json::json!("abcd...")]
        );
        assert_eq!(result.rows[1], vec![serde_json::json!(2), serde_json::json!("短值"), serde_json::json!("b")]);
        assert_eq!(
            cells,
            vec![
                db::LargeValueCell {
                    row_index: 0,
                    column_index: 1,
                    original_bytes: SERVER_LARGE_VALUE_UNKNOWN_BYTES,
                    pending_decode: None,
                    value_ref: None
                },
                db::LargeValueCell {
                    row_index: 0,
                    column_index: 2,
                    original_bytes: SERVER_LARGE_VALUE_UNKNOWN_BYTES,
                    pending_decode: None,
                    value_ref: None
                },
            ]
        );
    }

    #[test]
    fn truncates_server_binary_preview_after_the_configured_byte_count() {
        let mut result = db::QueryResult {
            columns: vec![
                "raw_value".to_string(),
                format!("{}B_0", crate::sql_dialect::DBX_LARGE_VALUE_BYTES_COLUMN_PREFIX),
            ],
            column_types: vec!["bytea".to_string(), "text".to_string()],
            deferred_large_value_cells: Vec::new(),
            column_sortables: vec![true, true],
            spatial_columns: Vec::new(),
            raw_key_cells: Vec::new(),
            spatial_values: Vec::new(),
            rows: ["0x", "0X", "\\x"]
                .into_iter()
                .flat_map(|prefix| {
                    [0, 1, 4, 5].into_iter().map(move |bytes| {
                        vec![
                            serde_json::json!(format!("{prefix}{}", "ab".repeat(bytes))),
                            serde_json::json!(format!("B:4:{bytes}")),
                        ]
                    })
                })
                .chain([vec![serde_json::Value::Null, serde_json::Value::Null]])
                .collect(),
            affected_rows: 0,
            execution_time_ms: 0,
            server_execute_time_us: None,
            query_timings_ms: None,
            truncated: false,
            session_id: None,
            has_more: false,
            elasticsearch_raw_body: None,
            messages: Vec::new(),
        };

        let cells = extract_server_large_value_markers(&mut result);

        assert_eq!(result.columns, vec!["raw_value"]);
        assert_eq!(result.column_types, vec!["bytea"]);
        for (prefix_index, prefix) in ["0x", "0X", "\\x"].into_iter().enumerate() {
            for (size_index, bytes) in [0, 1, 4, 5].into_iter().enumerate() {
                let suffix = if bytes > 4 { "..." } else { "" };
                assert_eq!(
                    result.rows[prefix_index * 4 + size_index],
                    vec![serde_json::json!(format!("{prefix}{}{suffix}", "ab".repeat(bytes.min(4))))]
                );
            }
        }
        assert_eq!(result.rows[12], vec![serde_json::Value::Null]);
        assert_eq!(
            cells,
            [3, 7, 11]
                .into_iter()
                .map(|row_index| db::LargeValueCell {
                    row_index,
                    column_index: 0,
                    original_bytes: 5,
                    pending_decode: None,
                    value_ref: None
                })
                .collect::<Vec<_>>()
        );
    }

    #[tokio::test]
    #[ignore = "requires DBX_TEST_POSTGRES_URL pointing at PostgreSQL (read-only queries)"]
    async fn live_postgres_bytea_preview_preserves_size_and_full_value() {
        use crate::sql_dialect::{build_table_data_select_sql, TableDataSelectSqlOptions};
        use sha2::{Digest, Sha256};

        let url = std::env::var("DBX_TEST_POSTGRES_URL").expect("DBX_TEST_POSTGRES_URL");
        let pool = db::postgres::connect(&url, Duration::from_secs(5)).await.expect("connect PostgreSQL");
        let preview_size = 3276;
        let sizes = [None, Some(0), Some(1), Some(3275), Some(3276), Some(3277), Some(9645), Some(261007)];
        let pattern = db::hex_encode(&(0..=255).collect::<Vec<u8>>());
        let inputs = sizes
            .iter()
            .enumerate()
            .map(|(id, size)| {
                format!("({id}, {})", size.map_or_else(|| "NULL::integer".to_string(), |n| n.to_string()))
            })
            .collect::<Vec<_>>()
            .join(", ");
        // A CTE supplies repeatable bytea data without creating database objects.
        let fixture = format!(
            "WITH payloads AS (SELECT id, substring(decode(repeat('{pattern}', (size + 255) / 256), 'hex') \
             from 1 for size) AS content FROM (VALUES {inputs}) AS inputs(id, size)), \
             bytea_preview AS (SELECT id, content, now() AS created_at, octet_length(content) AS bytes, \
             encode(sha256(content), 'hex') AS checksum FROM payloads) "
        );

        for text_protocol in [false, true] {
            let mut columns = vec!["id".to_string(), "content".to_string()];
            let mut column_types = vec!["int4".to_string(), "bytea".to_string()];
            if text_protocol {
                // A temporal column makes the driver choose PostgreSQL's text protocol.
                columns.push("created_at".to_string());
                column_types.push("timestamptz".to_string());
            }
            let select = build_table_data_select_sql(TableDataSelectSqlOptions {
                database_type: Some(DatabaseType::Postgres),
                table_name: "bytea_preview".to_string(),
                primary_keys: vec!["id".to_string()],
                columns: columns.clone(),
                column_types: column_types.clone(),
                large_value_preview_size: Some(preview_size),
                limit: Some(sizes.len()),
                order_by: Some("id".to_string()),
                ..Default::default()
            });
            let raw = db::postgres::execute_query(&pool, &format!("{fixture}{select}")).await.unwrap();
            let result = ExecuteMultiResult::success_with_optional_server_large_values(raw, true);
            assert_eq!(result.result.columns, columns);
            assert_eq!(result.result.column_types, column_types);
            assert_eq!(result.result.rows.len(), sizes.len());
            let prefix = if text_protocol { "\\x" } else { "0x" };

            for (row_index, size) in sizes.into_iter().enumerate() {
                let Some(size) = size else {
                    assert!(result.result.rows[row_index][1].is_null());
                    continue;
                };
                let bytes = (0..size).map(|index| (index % 256) as u8).collect::<Vec<_>>();
                let suffix = if size > preview_size { "..." } else { "" };
                assert_eq!(
                    result.result.rows[row_index][1],
                    serde_json::json!(format!("{prefix}{}{suffix}", db::hex_encode(&bytes[..size.min(preview_size)])))
                );
                if size <= preview_size {
                    continue;
                }
                // The download path fetches by primary key without preview options.
                let full_select = build_table_data_select_sql(TableDataSelectSqlOptions {
                    database_type: Some(DatabaseType::Postgres),
                    table_name: "bytea_preview".to_string(),
                    primary_keys: vec!["id".to_string()],
                    columns: ["id", "content", "bytes", "checksum"].map(str::to_string).to_vec(),
                    where_input: Some(format!("id = {row_index}")),
                    limit: Some(1),
                    ..Default::default()
                });
                let full = db::postgres::execute_query(&pool, &format!("{fixture}{full_select}")).await.unwrap();
                assert_eq!(full.rows.len(), 1);
                // The table-data builder can select *, so map columns by name like
                // the grid does. A returned timestamp also makes this fetch use text.
                let column = |name: &str| &full.rows[0][full.columns.iter().position(|c| c == name).unwrap()];
                let value = column("content").as_str().unwrap();
                let hex = value.strip_prefix("0x").or_else(|| value.strip_prefix("\\x")).unwrap();
                assert_eq!(hex.len(), size * 2);
                assert!(hex == db::hex_encode(&bytes), "full bytea differs for row {row_index}");
                let actual_size =
                    column("bytes").as_u64().or_else(|| column("bytes").as_str().and_then(|value| value.parse().ok()));
                assert_eq!(actual_size, Some(size as u64));
                assert_eq!(column("checksum"), &serde_json::json!(db::hex_encode(&Sha256::digest(&bytes))));
            }
            assert_eq!(
                result.large_value_cells,
                sizes
                    .into_iter()
                    .enumerate()
                    .filter_map(|(row_index, size)| size.filter(|size| *size > preview_size).map(|original_bytes| {
                        db::LargeValueCell {
                            row_index,
                            column_index: 1,
                            original_bytes,
                            pending_decode: None,
                            value_ref: None,
                        }
                    }))
                    .collect::<Vec<_>>()
            );
        }
    }

    #[test]
    fn extracts_deferred_oracle_clob_markers_without_changing_placeholder() {
        let mut result = db::QueryResult {
            columns: vec![
                "id".to_string(),
                "payload".to_string(),
                format!("{}C_1", crate::sql_dialect::DBX_LARGE_VALUE_BYTES_COLUMN_PREFIX),
            ],
            column_types: vec!["number".to_string(), "varchar2".to_string(), "varchar2".to_string()],
            deferred_large_value_cells: Vec::new(),
            column_sortables: vec![true; 3],
            spatial_columns: Vec::new(),
            raw_key_cells: Vec::new(),
            spatial_values: Vec::new(),
            rows: vec![
                vec![serde_json::json!(1), serde_json::json!("<CLOB>"), serde_json::json!("D:1")],
                vec![serde_json::json!(2), serde_json::Value::Null, serde_json::Value::Null],
            ],
            affected_rows: 0,
            execution_time_ms: 0,
            server_execute_time_us: None,
            query_timings_ms: None,
            truncated: false,
            session_id: None,
            has_more: false,
            elasticsearch_raw_body: None,
            messages: Vec::new(),
        };

        let cells = extract_server_large_value_markers(&mut result);

        assert_eq!(result.columns, vec!["id", "payload"]);
        assert_eq!(result.column_types, vec!["number", "clob"]);
        assert_eq!(result.rows[0], vec![serde_json::json!(1), serde_json::json!("<CLOB>")]);
        assert_eq!(result.rows[1], vec![serde_json::json!(2), serde_json::Value::Null]);
        assert_eq!(
            cells,
            vec![db::LargeValueCell {
                row_index: 0,
                column_index: 1,
                original_bytes: SERVER_LARGE_VALUE_UNKNOWN_BYTES,
                pending_decode: None,
                value_ref: None
            }]
        );
    }

    #[test]
    fn restores_pgvector_previews_as_arrays_and_marks_only_truncated_values() {
        let marker = format!("{}V_0", crate::sql_dialect::DBX_LARGE_VALUE_BYTES_COLUMN_PREFIX);
        let mut result = db::QueryResult {
            columns: vec!["embedding".to_string(), marker],
            column_types: vec!["text".to_string(), "text".to_string()],
            deferred_large_value_cells: Vec::new(),
            column_sortables: vec![true, true],
            spatial_columns: Vec::new(),
            raw_key_cells: Vec::new(),
            spatial_values: Vec::new(),
            rows: vec![
                vec![serde_json::json!("[0.1,0.2,0.3]"), serde_json::json!("V:9")],
                vec![serde_json::json!("[0.1,0.2]"), serde_json::json!("V:20")],
            ],
            affected_rows: 0,
            execution_time_ms: 0,
            server_execute_time_us: None,
            query_timings_ms: None,
            truncated: false,
            session_id: None,
            has_more: false,
            elasticsearch_raw_body: None,
            messages: Vec::new(),
        };

        let cells = extract_server_large_value_markers(&mut result);

        assert_eq!(result.column_types, vec!["vector"]);
        assert_eq!(result.rows[0][0], serde_json::json!([0.1, 0.2]));
        assert_eq!(result.rows[1][0], serde_json::json!([0.1, 0.2]));
        assert_eq!(cells.len(), 1);
        assert_eq!(cells[0].row_index, 0);
    }

    #[test]
    fn restores_server_preview_types_without_rows() {
        let mut result = db::QueryResult {
            columns: vec![
                "embedding".to_string(),
                format!("{}V_0", crate::sql_dialect::DBX_LARGE_VALUE_BYTES_COLUMN_PREFIX),
                "metadata".to_string(),
                format!("{}K_1", crate::sql_dialect::DBX_LARGE_VALUE_BYTES_COLUMN_PREFIX),
            ],
            column_types: vec!["text".to_string(), "text".to_string(), "text".to_string(), "text".to_string()],
            deferred_large_value_cells: Vec::new(),
            column_sortables: vec![true; 4],
            spatial_columns: Vec::new(),
            raw_key_cells: Vec::new(),
            spatial_values: Vec::new(),
            rows: Vec::new(),
            affected_rows: 0,
            execution_time_ms: 0,
            server_execute_time_us: None,
            query_timings_ms: None,
            truncated: false,
            session_id: None,
            has_more: false,
            elasticsearch_raw_body: None,
            messages: Vec::new(),
        };

        let cells = extract_server_large_value_markers(&mut result);

        assert_eq!(result.columns, vec!["embedding", "metadata"]);
        assert_eq!(result.column_types, vec!["vector", "jsonb"]);
        assert!(cells.is_empty());
    }

    #[test]
    fn ordinary_queries_preserve_columns_that_resemble_preview_markers() {
        let marker = format!("{}0", crate::sql_dialect::DBX_LARGE_VALUE_BYTES_COLUMN_PREFIX);
        let mut result = empty_query_result(1);
        result.columns = vec!["payload".to_string(), marker.clone()];
        result.column_types = vec!["text".to_string(), "bigint".to_string()];
        result.column_sortables = vec![true, true];
        result.rows = vec![vec![serde_json::json!("value"), serde_json::json!(123)]];

        let ordinary = ExecuteMultiResult::success_with_index_and_optional_server_large_values(result, 0, false);

        assert_eq!(ordinary.result.columns, vec!["payload".to_string(), marker]);
        assert_eq!(ordinary.result.rows[0], vec![serde_json::json!("value"), serde_json::json!(123)]);
        assert!(ordinary.large_value_cells.is_empty());
    }

    #[test]
    fn single_statement_preview_preserves_absent_statement_index() {
        let result = single_statement_multi_result(Ok(empty_query_result(1)), true, None).unwrap();

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].statement_index, None);
    }

    #[test]
    fn query_execution_options_default_use_transaction_is_none() {
        let opts = QueryExecutionOptions::default();
        assert_eq!(opts.use_transaction, None);
    }

    #[test]
    fn query_execution_options_use_transaction_some_true_is_preserved() {
        let opts = QueryExecutionOptions { use_transaction: Some(true), ..Default::default() };
        assert_eq!(opts.use_transaction, Some(true));
    }

    #[test]
    fn query_execution_options_use_transaction_some_false_is_preserved() {
        let opts = QueryExecutionOptions { use_transaction: Some(false), ..Default::default() };
        assert_eq!(opts.use_transaction, Some(false));
    }

    fn mysql_status(in_transaction: bool, autocommit: bool) -> db::mysql::MySqlSessionStatus {
        db::mysql::MySqlSessionStatus { in_transaction, autocommit }
    }

    #[test]
    fn mysql_auto_commit_default_rolls_back_an_explicitly_opened_transaction() {
        // The historical behavior for the default setting: a `BEGIN` a user
        // left open is rolled back, but the cleanup is now reported so the tab
        // can tell the user instead of discarding the transaction silently.
        let decision = decide_mysql_auto_commit_transaction(false, false, true, Some(mysql_status(true, true)));
        assert!(!decision.preserve);
        assert_eq!(decision.rollback, MysqlAutoCommitRollback::Explicit);
    }

    #[test]
    fn mysql_auto_commit_opt_in_keeps_an_explicitly_opened_transaction() {
        // The opt-in decision is made from the *submitted* statement list, not
        // from how much of the batch actually ran. A batch that opens a
        // transaction and is then cancelled (or aborted by an error) has only
        // executed a prefix of its statements, yet the opener is still part of
        // the batch, so the connection keeps whatever the prefix did and stays
        // in the transaction. Preserving is what makes the abandoned work
        // visible — the tab shows the open-transaction badge and the manual
        // rollback action — instead of silently rolling back a transaction the
        // user asked for. `decide(true, false, true, ..)` below is exactly that
        // cancelled-batch case: the caller still passes the opener it was
        // about to run, so the decision must be preserve, not roll back.
        let decision = decide_mysql_auto_commit_transaction(true, false, true, Some(mysql_status(true, true)));
        assert!(decision.preserve);
        assert_eq!(decision.rollback, MysqlAutoCommitRollback::None);
    }

    #[test]
    fn mysql_auto_commit_opt_in_keeps_the_transaction_of_a_later_execution() {
        // `START TRANSACTION` ran first, this execution only ran an UPDATE, so
        // the batch itself carries no opener: the kept state is the only signal.
        let decision = decide_mysql_auto_commit_transaction(true, true, false, Some(mysql_status(true, true)));
        assert!(decision.preserve);
        assert_eq!(decision.rollback, MysqlAutoCommitRollback::None);

        // The kept state survives even when the caller does not pass the option
        // (auxiliary queries, result paging): an open user transaction is never
        // destroyed from another code path.
        let no_option = decide_mysql_auto_commit_transaction(false, true, false, Some(mysql_status(true, true)));
        assert!(no_option.preserve);
        assert_eq!(no_option.rollback, MysqlAutoCommitRollback::None);
    }

    #[test]
    fn mysql_auto_commit_opt_in_keeps_a_transaction_opened_by_auto_commit_off() {
        let decision = decide_mysql_auto_commit_transaction(true, false, false, Some(mysql_status(true, false)));
        assert!(decision.preserve);

        // Without the opt-in the same connection is cleaned up as before, but
        // reported as a session-level implicit transaction: nobody typed
        // `BEGIN`, the connection simply runs with auto-commit off.
        let default = decide_mysql_auto_commit_transaction(false, false, false, Some(mysql_status(true, false)));
        assert!(!default.preserve);
        assert_eq!(default.rollback, MysqlAutoCommitRollback::SessionAutocommit);
    }

    #[test]
    fn mysql_auto_commit_reports_a_batch_opener_ahead_of_autocommit_off() {
        // Both signals at once: a `BEGIN` in the batch wins, so the notice is
        // the explicit-transaction one.
        let decision = decide_mysql_auto_commit_transaction(false, false, true, Some(mysql_status(true, false)));
        assert!(!decision.preserve);
        assert_eq!(decision.rollback, MysqlAutoCommitRollback::Explicit);
    }

    #[test]
    fn mysql_auto_commit_rolls_back_a_leftover_transaction_without_reporting_it() {
        // No opener in the batch and auto-commit still on: this is a leftover
        // transaction (cancelled or aborted batch), not a user transaction.
        let decision = decide_mysql_auto_commit_transaction(true, false, false, Some(mysql_status(true, true)));
        assert!(!decision.preserve);
        assert_eq!(decision.rollback, MysqlAutoCommitRollback::None);
    }

    #[test]
    fn mysql_auto_commit_leaves_a_clean_connection_alone() {
        // `BEGIN; ...; COMMIT;` in one batch ends with nothing open.
        let decision = decide_mysql_auto_commit_transaction(true, false, true, Some(mysql_status(false, true)));
        assert!(!decision.preserve);
        assert_eq!(decision.rollback, MysqlAutoCommitRollback::None);

        // A kept transaction that the user committed in this batch must be
        // forgotten, so later executions are auto-commit again.
        let after_commit = decide_mysql_auto_commit_transaction(true, true, false, Some(mysql_status(false, true)));
        assert!(!after_commit.preserve);
        assert_eq!(after_commit.rollback, MysqlAutoCommitRollback::None);
    }

    #[test]
    fn mysql_auto_commit_uses_the_batch_opener_when_the_status_packet_is_missing() {
        // The last statement ended with an ERR packet, so no status is cached
        // and the COM_PING refresh failed.
        let kept = decide_mysql_auto_commit_transaction(true, false, true, None);
        assert!(kept.preserve);
        assert_eq!(kept.rollback, MysqlAutoCommitRollback::None);

        let cleaned = decide_mysql_auto_commit_transaction(false, false, true, None);
        assert!(!cleaned.preserve);
        assert_eq!(cleaned.rollback, MysqlAutoCommitRollback::Explicit);

        let unknown_without_opener = decide_mysql_auto_commit_transaction(true, false, false, None);
        assert!(!unknown_without_opener.preserve);
        assert_eq!(unknown_without_opener.rollback, MysqlAutoCommitRollback::None);
    }

    #[test]
    fn mysql_cleanup_rollback_is_skipped_only_when_the_final_status_proves_nothing_is_open() {
        assert!(mysql_cleanup_rollback_is_noop(true, false, Some(mysql_status(false, true))));

        // A transaction is open, or auto-commit is off: the cleanup must run.
        assert!(!mysql_cleanup_rollback_is_noop(true, false, Some(mysql_status(true, true))));
        assert!(!mysql_cleanup_rollback_is_noop(true, false, Some(mysql_status(false, false))));
        // The last statement failed, so no status packet is cached.
        assert!(!mysql_cleanup_rollback_is_noop(true, false, None));
        // A truncated or failed result may leave an earlier command's packet.
        assert!(!mysql_cleanup_rollback_is_noop(false, false, Some(mysql_status(false, true))));
        // A batch that opened a transaction keeps the cleanup even when the
        // server reports nothing open, in case it never sets the flag.
        assert!(!mysql_cleanup_rollback_is_noop(true, true, Some(mysql_status(false, true))));
    }

    #[test]
    fn database_backup_transactions_request_consistent_snapshots() {
        assert_eq!(postgres_transaction_begin_sql(true), "BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY");
        assert_eq!(mysql_transaction_isolation_sql(true), Some("SET TRANSACTION ISOLATION LEVEL REPEATABLE READ"));
        assert_eq!(
            mysql_transaction_begin_sql_candidates(true),
            [
                "START TRANSACTION WITH CONSISTENT SNAPSHOT, READ ONLY",
                "START TRANSACTION WITH CONSISTENT SNAPSHOT",
                "START TRANSACTION",
            ]
        );
        assert_eq!(mysql_transaction_isolation_sql(false), None);
        assert_eq!(mysql_transaction_begin_sql_candidates(false), ["START TRANSACTION"]);
    }

    #[test]
    fn mysql_backup_transaction_only_falls_back_for_syntax_errors() {
        let syntax_error = mysql_async::Error::Server(mysql_async::ServerError {
            code: 1064,
            message: "unsupported transaction characteristic".to_string(),
            state: "42000".to_string(),
        });
        let permission_error = mysql_async::Error::Server(mysql_async::ServerError {
            code: 1044,
            message: "access denied".to_string(),
            state: "42000".to_string(),
        });

        assert!(mysql_error_is_syntax_error(&syntax_error));
        assert!(!mysql_error_is_syntax_error(&permission_error));
    }

    #[test]
    fn mysql_backup_transaction_falls_back_for_doris_parser_syntax_errors() {
        let doris_syntax_error = mysql_async::Error::Server(mysql_async::ServerError {
            code: 1105,
            message: "Syntax error in line 1: START TRANSACTION WITH CONSISTENT SNAPSHOT, READ ONLY ^ Encountered: COMMA Expected: EOF".to_string(),
            state: "HY000".to_string(),
        });
        let generic_unknown_error = mysql_async::Error::Server(mysql_async::ServerError {
            code: 1105,
            message: "Unknown error".to_string(),
            state: "HY000".to_string(),
        });

        assert!(mysql_error_is_syntax_error(&doris_syntax_error));
        assert!(!mysql_error_is_syntax_error(&generic_unknown_error));
    }

    #[test]
    fn mysql_backup_transaction_falls_back_for_polardbx_parser_errors() {
        for message in [
            "[PXC-4500][ERR_PARSER] syntax error, expect EOF, actual COMMA after WITH CONSISTENT SNAPSHOT",
            "START TRANSACTION WITH CONSISTENT SNAPSHOT, READ ONLY 语法错误",
        ] {
            let polardbx_parser_error = mysql_async::Error::Server(mysql_async::ServerError {
                code: 3009,
                message: message.to_string(),
                state: "HY000".to_string(),
            });
            assert!(mysql_error_is_syntax_error(&polardbx_parser_error));
        }
        let polardbx_non_parser_error = mysql_async::Error::Server(mysql_async::ServerError {
            code: 3009,
            message: "Failed to handle data".to_string(),
            state: "HY000".to_string(),
        });

        assert!(!mysql_error_is_syntax_error(&polardbx_non_parser_error));
    }
}