Skip to main content

SelectBuilder

Struct SelectBuilder 

Source
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

Source

pub fn select_cols<I, E>(self, expressions: I) -> Self
where I: IntoIterator<Item = E>, E: IntoExpr,

Append columns to the SELECT list.

Accepts any iterable of IntoExpr items. This is primarily useful when the builder was created via from() and columns need to be added afterward.

Source

pub fn from(self, table_name: &str) -> Self

Set the FROM clause to reference the given table by name.

Source

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.

Source

pub fn join(self, table_name: &str, on: Expr) -> Self

Add an inner JOIN clause with the given ON condition.

Source

pub fn left_join(self, table_name: &str, on: Expr) -> Self

Add a LEFT JOIN clause with the given ON condition.

Source

pub fn where_(self, condition: Expr) -> Self

Set the WHERE clause to filter rows by the given condition.

Calling this multiple times replaces the previous WHERE condition. To combine multiple predicates, chain them with .and() or .or() on a single Expr.

Source

pub fn group_by<I, E>(self, expressions: I) -> Self
where I: IntoIterator<Item = E>, E: IntoExpr,

Set the GROUP BY clause with the given grouping expressions.

Source

pub fn having(self, condition: Expr) -> Self

Set the HAVING clause to filter groups by the given condition.

Source

pub fn order_by<I, E>(self, expressions: I) -> Self
where I: IntoIterator<Item = E>, E: IntoExpr,

Set the ORDER BY clause with the given sort expressions.

Expressions that are not already wrapped with .asc() or .desc() default to ascending order. String values are interpreted as column names via IntoExpr.

Source

pub fn sort_by<I, E>(self, expressions: I) -> Self
where I: IntoIterator<Item = E>, E: IntoExpr,

Set the SORT BY clause with the given sort expressions.

SORT BY is used in Hive/Spark to sort data within each reducer (partition), as opposed to ORDER BY which sorts globally. Expressions that are not already wrapped with .asc() or .desc() default to ascending order.

Source

pub fn limit(self, count: usize) -> Self

Set the LIMIT clause to restrict the result set to count rows.

Source

pub fn offset(self, count: usize) -> Self

Set the OFFSET clause to skip the first count rows.

Source

pub fn distinct(self) -> Self

Enable the DISTINCT modifier on the SELECT clause.

Source

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.

Source

pub fn right_join(self, table_name: &str, on: Expr) -> Self

Add a RIGHT JOIN clause with the given ON condition.

Source

pub fn cross_join(self, table_name: &str) -> Self

Add a CROSS JOIN clause (Cartesian product, no ON condition).

Source

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.

Source

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.

Source

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.

Source

pub fn for_share(self) -> Self

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.

Source

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.

Source

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");
Source

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()).

Source

pub fn union_all(self, other: SelectBuilder) -> SetOpBuilder

Combine this SELECT with another via UNION ALL (keep duplicates).

Returns a SetOpBuilder for further chaining.

Source

pub fn intersect(self, other: SelectBuilder) -> SetOpBuilder

Combine this SELECT with another via INTERSECT (rows common to both).

Returns a SetOpBuilder for further chaining.

Source

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.

Source

pub fn build(self) -> Expression

Consume this builder and produce the final Expression::Select AST node.

Source

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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.