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§
- abs
- Create an
ABS(expr)expression. - alias
- Create an
expr AS namealias expression. - and
- Combine two expressions with
AND. - approx_
distinct - Create an
APPROX_DISTINCT(expr)expression. - avg
- Create an
AVG(expr)expression. - 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. - ceil
- Create a
CEIL(expr)expression. - coalesce
- Create a
COALESCE(exprs...)expression. - col
- Create a column reference expression.
- concat_
ws - Create a
CONCAT_WS(separator, exprs...)expression. - condition
- Parse a SQL condition string into an
Expr. - count
- Create a
COUNT(expr)expression. - count_
distinct - Create a
COUNT(DISTINCT expr)expression. - count_
star - Create a
COUNT(*)expression. - current_
date_ - Create a
CURRENT_DATEexpression. - current_
time_ - Create a
CURRENT_TIMEexpression. - current_
timestamp_ - Create a
CURRENT_TIMESTAMPexpression. - delete
- Start building a
DELETE FROMstatement targeting the given table. - dense_
rank - Create a
DENSE_RANK()expression. - 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. - exp_
- Create an
EXP(expr)expression. Namedexp_to avoid conflict withstd::f64::consts. - extract_
- Create an
EXTRACT(field FROM expr)expression. - floor
- Create a
FLOOR(expr)expression. - from
- Start building a SELECT query beginning with a FROM clause.
- func
- Create a SQL function call expression.
- greatest
- Create a
GREATEST(exprs...)expression. - if_null
- Create an
IFNULL(expr, fallback)expression. - initcap
- Create an
INITCAP(expr)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. - least
- Create a
LEAST(exprs...)expression. - length
- Create a
LENGTH(expr)expression. - lit
- Create a literal expression from any type implementing
IntoLiteral. - ln
- Create a
LN(expr)expression. - lower
- Create a
LOWER(expr)expression. - ltrim
- Create an
LTRIM(expr)expression. - max_
- Create a
MAX(expr)expression. Namedmax_to avoid conflict withstd::cmp::max. - merge_
into - Start building a
MERGE INTOstatement targeting the given table. - min_
- Create a
MIN(expr)expression. Namedmin_to avoid conflict withstd::cmp::min. - not
- Create a
NOT exprunary expression. - null
- Create a SQL
NULLliteral expression. - null_if
- Create a
NULLIF(expr1, expr2)expression. - or
- Combine two expressions with
OR. - power
- Create a
POWER(base, exp)expression. - rank_
- Create a
RANK()expression. Namedrank_to avoid confusion withRankstruct. - replace_
- Create a
REPLACE(expr, old, new)expression. Namedreplace_to avoid conflict with thestr::replacemethod. - reverse
- Create a
REVERSE(expr)expression. - round
- Create a
ROUND(expr, decimals)expression. - row_
number - Create a
ROW_NUMBER()expression. - rtrim
- Create an
RTRIM(expr)expression. - select
- Start building a SELECT query with the given column expressions.
- sign
- Create a
SIGN(expr)expression. - sql_
expr - Parse a raw SQL expression fragment into an
Expr. - sqrt
- Create a
SQRT(expr)expression. - 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. - substring
- Create a
SUBSTRING(expr, start, len)expression. - sum
- Create a
SUM(expr)expression. - table
- Create a table reference expression.
- trim
- Create a
TRIM(expr)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. - upper
- Create an
UPPER(expr)expression.