pub struct SelectBuilder { /* private fields */ }Expand description
Fluent builder for constructing SELECT statements.
Created by the select() or from() entry-point functions. Methods on this
builder return self so they can be chained. Call .build()
to obtain an Expression, or .to_sql() to generate a
SQL string directly.
§Examples
use polyglot_sql::builder::*;
let sql = select(["u.id", "u.name"])
.from("users")
.left_join("orders", col("u.id").eq(col("o.user_id")))
.where_(col("u.active").eq(boolean(true)))
.group_by(["u.id", "u.name"])
.order_by([col("u.name").asc()])
.limit(100)
.to_sql();Implementations§
Source§impl SelectBuilder
impl SelectBuilder
Sourcepub fn select_cols<I, E>(self, expressions: I) -> Selfwhere
I: IntoIterator<Item = E>,
E: IntoExpr,
pub fn select_cols<I, E>(self, expressions: I) -> Selfwhere
I: IntoIterator<Item = E>,
E: IntoExpr,
Sourcepub fn from(self, table_name: &str) -> Self
pub fn from(self, table_name: &str) -> Self
Set the FROM clause to reference the given table by name.
Sourcepub fn from_expr(self, expr: Expr) -> Self
pub fn from_expr(self, expr: Expr) -> Self
Set the FROM clause to an arbitrary expression (e.g. a subquery or table function).
Use this instead of SelectBuilder::from() when the source is not a simple
table name – for example, a subquery() or a table-valued function.
Sourcepub fn join(self, table_name: &str, on: Expr) -> Self
pub fn join(self, table_name: &str, on: Expr) -> Self
Add an inner JOIN clause with the given ON condition.
Sourcepub fn left_join(self, table_name: &str, on: Expr) -> Self
pub fn left_join(self, table_name: &str, on: Expr) -> Self
Add a LEFT JOIN clause with the given ON condition.
Sourcepub fn group_by<I, E>(self, expressions: I) -> Selfwhere
I: IntoIterator<Item = E>,
E: IntoExpr,
pub fn group_by<I, E>(self, expressions: I) -> Selfwhere
I: IntoIterator<Item = E>,
E: IntoExpr,
Set the GROUP BY clause with the given grouping expressions.
Sourcepub fn having(self, condition: Expr) -> Self
pub fn having(self, condition: Expr) -> Self
Set the HAVING clause to filter groups by the given condition.
Sourcepub fn order_by<I, E>(self, expressions: I) -> Selfwhere
I: IntoIterator<Item = E>,
E: IntoExpr,
pub fn order_by<I, E>(self, expressions: I) -> Selfwhere
I: IntoIterator<Item = E>,
E: IntoExpr,
Sourcepub fn sort_by<I, E>(self, expressions: I) -> Selfwhere
I: IntoIterator<Item = E>,
E: IntoExpr,
pub fn sort_by<I, E>(self, expressions: I) -> Selfwhere
I: IntoIterator<Item = E>,
E: IntoExpr,
Sourcepub fn limit(self, count: usize) -> Self
pub fn limit(self, count: usize) -> Self
Set the LIMIT clause to restrict the result set to count rows.
Sourcepub fn qualify(self, condition: Expr) -> Self
pub fn qualify(self, condition: Expr) -> Self
Add a QUALIFY clause to filter rows after window function evaluation.
QUALIFY is supported by Snowflake, BigQuery, DuckDB, and Databricks. It acts like a WHERE clause but is applied after window functions are computed.
Sourcepub fn right_join(self, table_name: &str, on: Expr) -> Self
pub fn right_join(self, table_name: &str, on: Expr) -> Self
Add a RIGHT JOIN clause with the given ON condition.
Sourcepub fn cross_join(self, table_name: &str) -> Self
pub fn cross_join(self, table_name: &str) -> Self
Add a CROSS JOIN clause (Cartesian product, no ON condition).
Sourcepub fn lateral_view<S: AsRef<str>>(
self,
table_function: Expr,
table_alias: &str,
column_aliases: impl IntoIterator<Item = S>,
) -> Self
pub fn lateral_view<S: AsRef<str>>( self, table_function: Expr, table_alias: &str, column_aliases: impl IntoIterator<Item = S>, ) -> Self
Add a LATERAL VIEW clause for Hive/Spark user-defined table function (UDTF)
expansion.
table_function is the UDTF expression (e.g. func("EXPLODE", [col("arr")])),
table_alias names the virtual table, and column_aliases name the output
columns produced by the function.
Sourcepub fn window(self, name: &str, def: WindowDefBuilder) -> Self
pub fn window(self, name: &str, def: WindowDefBuilder) -> Self
Add a named WINDOW clause definition.
The window name can then be referenced in window function OVER clauses
elsewhere in the query. The definition is constructed via WindowDefBuilder.
Multiple calls append additional named windows.
Sourcepub fn for_update(self) -> Self
pub fn for_update(self) -> Self
Add a FOR UPDATE locking clause.
Appends a FOR UPDATE lock to the SELECT statement. This is used by
databases (PostgreSQL, MySQL, Oracle) to lock selected rows for update.
Add a FOR SHARE locking clause.
Appends a FOR SHARE lock to the SELECT statement. This allows other
transactions to read the locked rows but prevents updates.
Sourcepub fn hint(self, hint_text: &str) -> Self
pub fn hint(self, hint_text: &str) -> Self
Add a query hint (e.g., Oracle /*+ FULL(t) */).
Hints are rendered for Oracle, MySQL, Spark, Hive, Databricks, and PostgreSQL dialects. Multiple calls append additional hints.
Sourcepub fn ctas(self, table_name: &str) -> Expression
pub fn ctas(self, table_name: &str) -> Expression
Convert this SELECT into a CREATE TABLE AS SELECT statement.
Consumes the builder and returns an Expression::CreateTable with this
query as the as_select source.
§Examples
use polyglot_sql::builder::*;
let sql = polyglot_sql::generator::Generator::sql(
&select(["*"]).from("t").ctas("new_table")
).unwrap();
assert_eq!(sql, "CREATE TABLE new_table AS SELECT * FROM t");Sourcepub fn union(self, other: SelectBuilder) -> SetOpBuilder
pub fn union(self, other: SelectBuilder) -> SetOpBuilder
Combine this SELECT with another via UNION (duplicate elimination).
Returns a SetOpBuilder for further chaining (e.g. .order_by(), .limit()).
Sourcepub fn union_all(self, other: SelectBuilder) -> SetOpBuilder
pub fn union_all(self, other: SelectBuilder) -> SetOpBuilder
Combine this SELECT with another via UNION ALL (keep duplicates).
Returns a SetOpBuilder for further chaining.
Sourcepub fn intersect(self, other: SelectBuilder) -> SetOpBuilder
pub fn intersect(self, other: SelectBuilder) -> SetOpBuilder
Combine this SELECT with another via INTERSECT (rows common to both).
Returns a SetOpBuilder for further chaining.
Sourcepub fn except_(self, other: SelectBuilder) -> SetOpBuilder
pub fn except_(self, other: SelectBuilder) -> SetOpBuilder
Combine this SELECT with another via EXCEPT (rows in left but not right).
Returns a SetOpBuilder for further chaining.
Sourcepub fn build(self) -> Expression
pub fn build(self) -> Expression
Consume this builder and produce the final Expression::Select AST node.
Sourcepub fn to_sql(self) -> String
pub fn to_sql(self) -> String
Consume this builder, generate, and return the SQL string.
Equivalent to calling .build() followed by Generator::sql(). Returns an
empty string if generation fails.