Skip to main content

Crate turso_sql

Crate turso_sql 

Source
Expand description

SQL AST and query builder for the Turso dialect.

This crate is the SQL layer of turso-orm. It owns the abstract syntax tree of a statement — identifiers, values, expressions, DML and DDL builders — and the single writer that renders that tree to SQL text with ? placeholders and the values to bind. It deliberately targets one dialect, the SQLite grammar as implemented by Turso, which is why it stays small, has no runtime dependency and binds parameters directly as Turso storage-class Values rather than through a generic value model.

The crate does not execute anything: it never touches a connection and knows nothing about rows. Executing a Statement is the job of turso-orm-driver, and mapping entities onto statements is the job of turso-orm.

  • Iden, Ident, TableRef, ColumnRef: identifiers, always rendered double-quoted so reserved words and mixed case are safe;
  • Value: the five SQLite storage classes, with From conversions for the common Rust types and, behind feature flags, chrono, uuid, serde_json and rust_decimal;
  • Expr and Condition: expressions and composable AND / OR groups that parenthesise themselves so precedence is never ambiguous;
  • Query and Table: entry points to the DML and DDL builders;
  • Build and Statement: rendering to SQL plus bound values.

§Example

use turso_sql::prelude::*;

let (sql, values) = Query::select()
    .column("id")
    .column("name")
    .from("user")
    .and_where(Expr::col("age").gte(18))
    .order_by("name", Order::Asc)
    .limit(10)
    .build();
assert_eq!(
    sql,
    r#"SELECT "id", "name" FROM "user" WHERE "age" >= ? ORDER BY "name" ASC LIMIT ?"#
);
assert_eq!(values, vec![Value::Integer(18), Value::Integer(10)]);

Modules§

prelude
Everything needed to build queries, for a single glob import.

Structs§

AlterTable
An ALTER TABLE statement.
ColumnDef
A column definition.
Condition
A composable WHERE / HAVING condition.
CreateIndex
A CREATE INDEX statement.
CreateTable
A CREATE TABLE statement.
Delete
A DELETE statement.
DropIndex
A DROP INDEX statement.
DropTable
A DROP TABLE statement.
ForeignKey
A foreign-key constraint.
Func
A function call.
Ident
An owned identifier.
Insert
An INSERT statement.
Join
A join clause.
OnConflict
An ON CONFLICT clause for inserts.
Query
The entry point to the DML builders, mirroring the Query::select() style.
Select
A SELECT statement.
Statement
A rendered statement: SQL with ? placeholders and the values to bind.
Table
The entry point to the DDL builders.
TableRef
A table reference with an optional alias.
Update
An UPDATE statement.

Enums§

ColumnRef
A column reference, optionally qualified by table.
ColumnType
The logical column types.
Expr
A SQL expression.
ForeignKeyAction
The referential actions of a foreign key.
JoinType
The join kinds.
Order
A sort direction.
Returning
A RETURNING clause.
SelectItem
An item of a select list.
TableConstraint
The table-level constraints.
Value
A bound parameter or literal.

Constants§

NAIVE_DATETIME_FORMATwith-chrono
The text format used for naive timestamps.

Traits§

Build
Anything that renders to a Statement.
Iden
Something that names a table, column or alias.
IntoCondition
Anything usable as a condition: an Expr or a Condition.
IntoIden
Conversion into an Ident.