Skip to main content

Crate linq_rs_sql

Crate linq_rs_sql 

Source
Expand description

§linq_rs_sql — a typed SQL query builder

Compiles in-Rust column and predicate expressions into a SQL string plus its bound parameters. Columns and their types are declared once with table!, and from then on the compiler checks them: a misspelt column or a type mismatch is a build error, and every value you compare against becomes a bound parameter rather than text spliced into a query.

use linq_rs_sql::*;

linq_rs_sql::table! {
    employees (id) { id -> Integer, name -> Text, salary -> Integer }
}

let q = employees::table()
    .filter(employees::salary.gt(100_000))
    .filter(employees::name.like("A%"))
    .order_by_desc(employees::salary)
    .limit(5)
    .to_sql();

assert!(q.sql.starts_with("SELECT * FROM employees WHERE"));
assert_eq!(q.params.len(), 2);   // both values are bound, not interpolated

§What this is not

It does not execute anything. to_sql() hands you a String and a Vec<SqlValue>; you pass those to whatever driver you already use — rusqlite, tokio-postgres, sqlx-core. There is no connection, no pool, no async runtime, and no dependency of any kind.

It is not linq_rs. The sibling crate is a LINQ-shaped query surface over in-memory iterators, and the two vocabularies are deliberately separate: this one says filter, that one says where_, and no value passes between them. Keeping them in one crate would have meant two names for one concept in a single API — see DECISIONS.md D-205 in the repository. Joining them properly is the two-interpreter design tracked as D-002, and this crate is intended to become its rendering backend.

§Phase 1 scope

  • SELECT with column lists or all-columns
  • WHERE with =, !=, <, <=, >, >=, AND, OR, NOT, LIKE, IS NULL
  • ORDER BY (ascending / descending)
  • LIMIT / OFFSET
  • Parameter binding via ? placeholders
  • Compile-time SQL type checking via marker types

Out of scope for Phase 1 (in roadmap order): JOINs, subqueries, GROUP BY, INSERT / UPDATE / DELETE, migrations, driver integration, async.

§Quick start

use linq_rs_sql::*;

linq_rs_sql::table! {
    users (id) {
        id    -> Integer,
        name  -> Text,
        age   -> Integer,
    }
}

let q = users::table()
    .filter(users::age.gt(18))
    .select((users::id, users::name))
    .to_sql();

assert_eq!(q.sql, "SELECT id, name FROM users WHERE (age > ?)");
assert_eq!(q.params, vec![SqlValue::Integer(18)]);

Re-exports§

pub use column::BoolOps;
pub use column::Column;
pub use column::ExprExt;
pub use column::FloatOps;
pub use column::IntOps;
pub use column::TextOps;
pub use expr::not;
pub use expr::And;
pub use expr::Eq;
pub use expr::Expr;
pub use expr::Gt;
pub use expr::GtEq;
pub use expr::IsNull;
pub use expr::Like;
pub use expr::Lit;
pub use expr::Lt;
pub use expr::LtEq;
pub use expr::Not;
pub use expr::NotEq;
pub use expr::Or;
pub use query::All;
pub use query::Direction;
pub use query::Query;
pub use query::QueryOutput;
pub use query::Selection;
pub use query::Table;
pub use value::SqlValue;
pub use types::Boolean;
pub use types::Float;
pub use types::Integer;
pub use types::SqlType;
pub use types::Text;

Modules§

column
Column trait + per-SQL-type operator extension traits.
expr
Expression tree — every node carries an associated SqlType so the compiler can reject mismatched comparisons at the call site.
pred
pred! — write a filter the way you would write a closure.
prelude
Everything pred! and the builder need, in one import.
query
Query builder, Table trait, Selection trait, and QueryOutput.
rows
The two-interpreter seam (D-002, W-20).
types
SQL type tags — zero-sized marker types used at the type level to enforce column / literal compatibility at compile time.
value
Runtime SQL values. These are what end up in the params vec returned by Query::to_sql — they’re the bind values the user will pass to a driver alongside the SQL string.

Macros§

entity
Bind a Rust struct to a table! module and give every column its accessor.
pred
Builds a predicate from closure-shaped source. See the module docs for the grammar.
table
Declare a SQL table with typed columns.