Expand description
Fluent SQL Builder API
Provides a programmatic way to construct SQL Expression trees without parsing raw SQL
strings. The API mirrors Python sqlglot’s builder functions (select(), from_(),
condition(), etc.) and is the primary entry point for constructing queries
programmatically in Rust.
§Design
The builder is organized around a few key concepts:
- Expression helpers (
col,lit,star,null,boolean,func,cast,alias,sql_expr,condition) create leaf-levelExprvalues. - Query starters (
select,from,delete,insert_into,update) return fluent builder structs (SelectBuilder,DeleteBuilder, etc.). Exprwraps anExpressionand exposes operator methods (.eq(),.gt(),.and(),.like(), etc.) so conditions can be built without manual AST construction.IntoExprandIntoLiteralallow ergonomic coercion of&str,i64,f64, and other primitives wherever an expression or literal is expected.
§Examples
use polyglot_sql::builder::*;
// SELECT id, name FROM users WHERE age > 18 ORDER BY name LIMIT 10
let expr = select(["id", "name"])
.from("users")
.where_(col("age").gt(lit(18)))
.order_by(["name"])
.limit(10)
.build();use polyglot_sql::builder::*;
// CASE WHEN x > 0 THEN 'positive' ELSE 'non-positive' END
let expr = case()
.when(col("x").gt(lit(0)), lit("positive"))
.else_(lit("non-positive"))
.build();use polyglot_sql::builder::*;
// SELECT id FROM a UNION ALL SELECT id FROM b ORDER BY id LIMIT 5
let expr = union_all(
select(["id"]).from("a"),
select(["id"]).from("b"),
)
.order_by(["id"])
.limit(5)
.build();Structs§
- Case
Builder - Fluent builder for SQL
CASEexpressions (both searched and simple forms). - Delete
Builder - Fluent builder for constructing
DELETE FROMstatements. - Expr
- A thin wrapper around
Expressionthat provides fluent operator methods. - Insert
Builder - Fluent builder for constructing
INSERT INTOstatements. - Merge
Builder - Fluent builder for constructing
MERGE INTOstatements. - Select
Builder - Fluent builder for constructing
SELECTstatements. - SetOp
Builder - Fluent builder for
UNION,INTERSECT, andEXCEPTset operations. - Update
Builder - Fluent builder for constructing
UPDATEstatements. - Window
DefBuilder - Builder for constructing named
WINDOWclause definitions.
Traits§
- Into
Expr - Conversion trait for types that can be turned into an
Expr. - Into
Literal - Conversion trait for types that can be turned into a SQL literal
Expr.
Functions§
- alias
- Create an
expr AS namealias expression. - and
- Combine two expressions with
AND. - boolean
- Create a SQL boolean literal expression (
TRUEorFALSE). - case
- Start building a searched CASE expression (
CASE WHEN cond THEN result ... END). - case_of
- Start building a simple CASE expression (
CASE operand WHEN value THEN result ... END). - cast
- Create a
CAST(expr AS type)expression. - col
- Create a column reference expression.
- condition
- Parse a SQL condition string into an
Expr. - delete
- Start building a
DELETE FROMstatement targeting the given table. - except_
- Create an
EXCEPT(rows in left but not right) of two SELECT queries. - except_
all - Create an
EXCEPT ALL(keep duplicate difference rows) of two SELECT queries. - from
- Start building a SELECT query beginning with a FROM clause.
- func
- Create a SQL function call expression.
- insert_
into - Start building an
INSERT INTOstatement targeting the given table. - intersect
- Create an
INTERSECT(rows common to both) of two SELECT queries. - intersect_
all - Create an
INTERSECT ALL(keep duplicate common rows) of two SELECT queries. - lit
- Create a literal expression from any type implementing
IntoLiteral. - merge_
into - Start building a
MERGE INTOstatement targeting the given table. - not
- Create a
NOT exprunary expression. - null
- Create a SQL
NULLliteral expression. - or
- Combine two expressions with
OR. - select
- Start building a SELECT query with the given column expressions.
- sql_
expr - Parse a raw SQL expression fragment into an
Expr. - star
- Create a star (
*) expression, typically used inSELECT *. - subquery
- Wrap a
SelectBuilderas a named subquery for use in FROM or JOIN clauses. - subquery_
expr - Wrap an existing
Expressionas a named subquery. - table
- Create a table reference expression.
- union
- Create a
UNION(duplicate elimination) of two SELECT queries. - union_
all - Create a
UNION ALL(keep duplicates) of two SELECT queries. - update
- Start building an
UPDATEstatement targeting the given table.