Skip to main content

turso_sql/
lib.rs

1//! SQL AST and query builder for the Turso dialect.
2//!
3//! This crate is the SQL layer of [turso-orm]. It owns the abstract syntax
4//! tree of a statement — identifiers, values, expressions, DML and DDL
5//! builders — and the single writer that renders that tree to SQL text with
6//! `?` placeholders and the values to bind. It deliberately targets one
7//! dialect, the SQLite grammar as implemented by Turso, which is why it stays
8//! small, has no runtime dependency and binds parameters directly as Turso
9//! storage-class [`Value`]s rather than through a generic value model.
10//!
11//! The crate does not execute anything: it never touches a connection and
12//! knows nothing about rows. Executing a [`Statement`] is the job of
13//! `turso-orm-driver`, and mapping entities onto statements is the job of
14//! `turso-orm`.
15//!
16//! - [`Iden`], [`Ident`], [`TableRef`], [`ColumnRef`]: identifiers, always
17//!   rendered double-quoted so reserved words and mixed case are safe;
18//! - [`Value`]: the five SQLite storage classes, with `From` conversions for
19//!   the common Rust types and, behind feature flags, `chrono`, `uuid`,
20//!   `serde_json` and `rust_decimal`;
21//! - [`Expr`] and [`Condition`]: expressions and composable `AND` / `OR`
22//!   groups that parenthesise themselves so precedence is never ambiguous;
23//! - [`Query`] and [`Table`]: entry points to the DML and DDL builders;
24//! - [`Build`] and [`Statement`]: rendering to SQL plus bound values.
25//!
26//! # Example
27//!
28//! ```
29//! use turso_sql::prelude::*;
30//!
31//! let (sql, values) = Query::select()
32//!     .column("id")
33//!     .column("name")
34//!     .from("user")
35//!     .and_where(Expr::col("age").gte(18))
36//!     .order_by("name", Order::Asc)
37//!     .limit(10)
38//!     .build();
39//! assert_eq!(
40//!     sql,
41//!     r#"SELECT "id", "name" FROM "user" WHERE "age" >= ? ORDER BY "name" ASC LIMIT ?"#
42//! );
43//! assert_eq!(values, vec![Value::Integer(18), Value::Integer(10)]);
44//! ```
45//!
46//! [turso-orm]: https://github.com/aartintelligent/turso-orm
47#![cfg_attr(docsrs, feature(doc_cfg))]
48
49mod expr;
50mod iden;
51mod query;
52mod schema;
53mod value;
54mod writer;
55
56pub use expr::{Condition, Expr, Func, IntoCondition, Order};
57pub use iden::{ColumnRef, Iden, Ident, IntoIden, TableRef};
58pub use query::{
59    Delete, Insert, Join, JoinType, OnConflict, Query, Returning, Select, SelectItem, Update,
60};
61pub use schema::{
62    AlterTable, ColumnDef, ColumnType, CreateIndex, CreateTable, DropIndex, DropTable, ForeignKey,
63    ForeignKeyAction, Table, TableConstraint,
64};
65#[cfg(feature = "with-chrono")]
66#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))]
67pub use value::NAIVE_DATETIME_FORMAT;
68pub use value::Value;
69pub use writer::{Build, Statement};
70
71/// Everything needed to build queries, for a single glob import.
72///
73/// The prelude re-exports every builder, expression and identifier type of
74/// the crate so that application code can write `use turso_sql::prelude::*;`
75/// and have the whole query language in scope.
76pub mod prelude {
77    pub use crate::{
78        AlterTable, Build, ColumnDef, ColumnRef, ColumnType, Condition, CreateIndex, CreateTable,
79        Delete, DropIndex, DropTable, Expr, ForeignKey, ForeignKeyAction, Func, Iden, Ident,
80        Insert, IntoCondition, IntoIden, Join, JoinType, OnConflict, Order, Query, Returning,
81        Select, SelectItem, Statement, Table, TableConstraint, TableRef, Update, Value,
82    };
83}