dbcli 0.1.0

Convert SQL query results to JSON without struct mapping, supporting MySQL/PostgreSQL/SQLite/Odbc
Documentation
use serde::{Deserialize, Serialize};
use crate::column_info::ColumnBaseInfo;

/// Result of executing a single SQL statement.
///
/// Each SQL statement in a batch produces one `SqlResult` entry.
/// Statements that return rows (SELECT, SHOW, EXPLAIN, etc.) produce a [`SqlResult::Query`];
/// statements that do not return rows (INSERT, UPDATE, DELETE, CREATE, DROP, etc.)
/// produce a [`SqlResult::Execute`].
///
/// # Serialization
///
/// `SqlResult` is tagged with a `"type"` discriminant when serialized to JSON:
///
/// ```json
/// // Query result
/// { "type": "query", "data": [...], "columns": [...] }
///
/// // Execution result
/// { "type": "execute", "rows_affected": 3 }
/// ```
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(tag = "type")]
pub enum SqlResult {
    /// Query result with data rows (SELECT, SHOW, EXPLAIN, etc.)
    ///
    /// Contains the JSON-serialized rows and the column metadata.
    #[serde(rename = "query")]
    Query {
        /// JSON rows; each element is a JSON object with column names as keys.
        data: Vec<serde_json::Value>,
        /// Column metadata (name, type, zero-based index).
        columns: Vec<ColumnBaseInfo>,
    },

    /// Execution result without data rows (INSERT, UPDATE, DELETE, CREATE, DROP, etc.)
    ///
    /// Contains the number of rows affected by the statement.
    #[serde(rename = "execute")]
    Execute {
        /// Number of rows affected by the statement.
        rows_affected: u64,
    },
}