sqlw 0.1.0

Compile-time SQL query building with schema-safe field references and automatic parameter binding
Documentation
#![warn(missing_docs)]

//! Compile-time SQL query building for Rust.
//!
//! This crate provides a compile-time SQL macro with schema-safe field references
//! and automatic parameter binding.
//!
//! # Quick Start
//!
//! ```ignore
//! use sqlw::{schema, query_qmark as query};
//!
//! // Define your table schema
//! schema!(User "users" {
//!     ID: i64 "id",
//!     NAME: String "name",
//!     EMAIL: String "email",
//! });
//!
//! // Build queries with schema-safe field references
//! let user_id = 42;
//! let query = query!(
//!     SELECT User::NAME, User::EMAIL
//!     FROM User::TABLE
//!     WHERE User::ID = {user_id}
//! );
//!
//! query.sql();   // "SELECT name, email FROM users WHERE id = ?"
//! query.args();  // [Int(42)]
//! ```
//!
//! # Modules
//!
//! - [`backend`] - Query execution trait and row-mapping types
//! - [`schema!`] - Table schema definitions
//! - [`value`] - SQL value types and conversions
//!
//! [`sqlw-backend`](https://docs.rs/sqlw-backend) ([local](../sqlw_backend/index.html), [git](https://github.com/mrVncius/sqlw))

pub mod backend;
pub mod schema;
pub mod value;

pub use backend::*;
pub use schema::*;
pub use sqlw_macro::*;
pub use value::*;

/// A compiled SQL query with bound parameters.
///
/// Created by the [`query_qmark!`] or [`query_numbered!`] macros.
/// Use [`split`](Self::split) to get the SQL string and arguments for execution,
/// or [`sql`](Self::sql) and [`args`](Self::args) to get them separately.
#[derive(Debug, Clone, PartialEq)]
pub struct Query {
    sql: String,
    args: Vec<value::Value>,
}

impl Query {
    /// Creates a query from SQL and arguments.
    pub fn new(sql: String, args: Vec<value::Value>) -> Self {
        Query { sql, args }
    }

    /// Returns a reference to the SQL string.
    pub fn sql(&self) -> &String {
        &self.sql
    }

    /// Returns a reference to the bound arguments.
    pub fn args(&self) -> &[value::Value] {
        &self.args
    }

    /// Consumes the query, returning SQL and arguments.
    pub fn split(self) -> (String, Vec<value::Value>) {
        (self.sql, self.args)
    }
}