Skip to main content

Module builder

Module builder 

Source
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:

§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§

CaseBuilder
Fluent builder for SQL CASE expressions (both searched and simple forms).
DeleteBuilder
Fluent builder for constructing DELETE FROM statements.
Expr
A thin wrapper around Expression that provides fluent operator methods.
InsertBuilder
Fluent builder for constructing INSERT INTO statements.
MergeBuilder
Fluent builder for constructing MERGE INTO statements.
SelectBuilder
Fluent builder for constructing SELECT statements.
SetOpBuilder
Fluent builder for UNION, INTERSECT, and EXCEPT set operations.
UpdateBuilder
Fluent builder for constructing UPDATE statements.
WindowDefBuilder
Builder for constructing named WINDOW clause definitions.

Traits§

IntoExpr
Conversion trait for types that can be turned into an Expr.
IntoLiteral
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 name alias 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 (TRUE or FALSE).
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_DATE expression.
current_time_
Create a CURRENT_TIME expression.
current_timestamp_
Create a CURRENT_TIMESTAMP expression.
delete
Start building a DELETE FROM statement 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. Named exp_ to avoid conflict with std::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 INTO statement 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. Named max_ to avoid conflict with std::cmp::max.
merge_into
Start building a MERGE INTO statement targeting the given table.
min_
Create a MIN(expr) expression. Named min_ to avoid conflict with std::cmp::min.
not
Create a NOT expr unary expression.
null
Create a SQL NULL literal 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. Named rank_ to avoid confusion with Rank struct.
replace_
Create a REPLACE(expr, old, new) expression. Named replace_ to avoid conflict with the str::replace method.
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 in SELECT *.
subquery
Wrap a SelectBuilder as a named subquery for use in FROM or JOIN clauses.
subquery_expr
Wrap an existing Expression as 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 UPDATE statement targeting the given table.
upper
Create an UPPER(expr) expression.