pub struct QueryBuilder<'a, Schema = (), State = (), Table = ()> {
pub sql: SQL<'a, SQLiteValue<'a>>,
/* private fields */
}Expand description
Main query builder for SQLite operations.
QueryBuilder provides a type-safe, fluent API for building SQL queries. It uses compile-time
type checking to ensure queries are valid and properly structured.
§Type Parameters
Schema: The database schema type, ensuring queries only reference valid tablesState: The current builder state, enforcing proper query construction orderTable: The table type being operated on (for single-table operations)
§Basic Usage
use drizzle::sqlite::prelude::*;
use drizzle::sqlite::builder::QueryBuilder;
#[SQLiteTable(name = "users")]
struct User {
#[column(primary)]
id: i32,
name: String,
}
#[derive(SQLiteSchema)]
struct Schema {
user: User,
}
// Create a query builder for your schema
let builder = QueryBuilder::new::<Schema>();
let Schema { user } = Schema::new();
// Build queries using the fluent API
let query = builder
.select(user.name)
.from(user);
assert_eq!(query.to_sql().sql(), r#"SELECT "users"."name" FROM "users""#);§Query Types
The builder supports all major SQL operations:
§SELECT Queries
let query = builder.select(user.name).from(user);
let query = builder.select((user.id, user.name)).from(user).r#where(gt(user.id, 10));§INSERT Queries
let query = builder
.insert(user)
.values([InsertUser::new("Alice")]);§UPDATE Queries
let query = builder
.update(user)
.set(UpdateUser::default().with_name("Bob"))
.r#where(eq(user.id, 1));§DELETE Queries
let query = builder
.delete(user)
.r#where(lt(user.id, 10));§Common Table Expressions (CTEs)
The builder supports WITH clauses for complex queries with typed field access:
// Create a CTE with typed field access using .as_cte()
let active_users = builder
.select((user.id, user.name))
.from(user)
.as_cte("active_users");
// Use the CTE with typed column access via Deref
let query = builder
.with(&active_users)
.select(active_users.name) // Typed field access!
.from(&active_users);
assert_eq!(
query.to_sql().sql(),
r#"WITH active_users AS (SELECT "users"."id", "users"."name" FROM "users") SELECT "active_users"."name" FROM "active_users""#
);Fields§
§sql: SQL<'a, SQLiteValue<'a>>Implementations§
Source§impl<'a, S, T> QueryBuilder<'a, S, DeleteInitial, T>
impl<'a, S, T> QueryBuilder<'a, S, DeleteInitial, T>
Sourcepub fn where(
self,
condition: impl ToSQL<'a, SQLiteValue<'a>>,
) -> DeleteBuilder<'a, S, DeleteWhereSet, T>
pub fn where( self, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> DeleteBuilder<'a, S, DeleteWhereSet, T>
Adds a WHERE clause to specify which rows to delete.
Warning: Without a WHERE clause, ALL rows in the table will be deleted! Always use this method unless you specifically intend to truncate the entire table.
§Examples
// Delete specific row by ID
let query = builder
.delete(user)
.r#where(eq(user.id, 1));
assert_eq!(query.to_sql().sql(), r#"DELETE FROM "users" WHERE "users"."id" = ?"#);
// Delete with complex conditions
let query = builder
.delete(user)
.r#where(and([
gt(user.id, 100),
or([eq(user.name, "test"), eq(user.age, 0)])
]));Sourcepub fn returning(
self,
columns: impl ToSQL<'a, SQLiteValue<'a>>,
) -> DeleteBuilder<'a, S, DeleteReturningSet, T>
pub fn returning( self, columns: impl ToSQL<'a, SQLiteValue<'a>>, ) -> DeleteBuilder<'a, S, DeleteReturningSet, T>
Adds a RETURNING clause to the query
Source§impl<'a, S, T> QueryBuilder<'a, S, DeleteWhereSet, T>
impl<'a, S, T> QueryBuilder<'a, S, DeleteWhereSet, T>
Sourcepub fn returning(
self,
columns: impl ToSQL<'a, SQLiteValue<'a>>,
) -> DeleteBuilder<'a, S, DeleteReturningSet, T>
pub fn returning( self, columns: impl ToSQL<'a, SQLiteValue<'a>>, ) -> DeleteBuilder<'a, S, DeleteReturningSet, T>
Adds a RETURNING clause after WHERE
Source§impl<'a, Schema, Table> QueryBuilder<'a, Schema, InsertInitial, Table>where
Table: SQLiteTable<'a>,
impl<'a, Schema, Table> QueryBuilder<'a, Schema, InsertInitial, Table>where
Table: SQLiteTable<'a>,
Sourcepub fn values<I, T>(
self,
values: I,
) -> InsertBuilder<'a, Schema, InsertValuesSet, Table>
pub fn values<I, T>( self, values: I, ) -> InsertBuilder<'a, Schema, InsertValuesSet, Table>
Specifies the values to insert into the table.
This method accepts an iterable of insert value objects generated by the
SQLiteTable macro (e.g., InsertUser). You can insert single values or
multiple values for batch operations.
§Examples
// Single insert
let query = builder
.insert(user)
.values([InsertUser::new("Alice")]);
assert_eq!(query.to_sql().sql(), r#"INSERT INTO "users" ("name") VALUES (?)"#);
// Batch insert (all values must have the same fields set)
let query = builder
.insert(user)
.values([
InsertUser::new("Alice").with_email("alice@example.com"),
InsertUser::new("Bob").with_email("bob@example.com"),
]);
assert_eq!(
query.to_sql().sql(),
r#"INSERT INTO "users" ("name", "email") VALUES (?, ?), (?, ?)"#
);Source§impl<'a, S, T> QueryBuilder<'a, S, InsertValuesSet, T>
impl<'a, S, T> QueryBuilder<'a, S, InsertValuesSet, T>
Sourcepub fn on_conflict<TI>(
self,
conflict: Conflict<'a, TI>,
) -> InsertBuilder<'a, S, InsertOnConflictSet, T>
pub fn on_conflict<TI>( self, conflict: Conflict<'a, TI>, ) -> InsertBuilder<'a, S, InsertOnConflictSet, T>
Adds conflict resolution to handle constraint violations.
SQLite supports various conflict resolution strategies when inserting data that would violate unique constraints or primary keys. This method allows you to specify how to handle such conflicts.
§Examples
// Ignore conflicts (do nothing)
let query = builder
.insert(user)
.values([InsertUser::new("Alice")])
.on_conflict(Conflict::default());
assert_eq!(
query.to_sql().sql(),
r#"INSERT INTO "users" ("name") VALUES (?) ON CONFLICT DO NOTHING"#
);Sourcepub fn returning(
self,
columns: impl ToSQL<'a, SQLiteValue<'a>>,
) -> InsertBuilder<'a, S, InsertReturningSet, T>
pub fn returning( self, columns: impl ToSQL<'a, SQLiteValue<'a>>, ) -> InsertBuilder<'a, S, InsertReturningSet, T>
Adds a RETURNING clause and transitions to ReturningSet state
Source§impl<'a, S, T> QueryBuilder<'a, S, InsertOnConflictSet, T>
impl<'a, S, T> QueryBuilder<'a, S, InsertOnConflictSet, T>
Sourcepub fn returning(
self,
columns: impl ToSQL<'a, SQLiteValue<'a>>,
) -> InsertBuilder<'a, S, InsertReturningSet, T>
pub fn returning( self, columns: impl ToSQL<'a, SQLiteValue<'a>>, ) -> InsertBuilder<'a, S, InsertReturningSet, T>
Adds a RETURNING clause after ON CONFLICT
Source§impl<'a, S> QueryBuilder<'a, S, SelectInitial>
impl<'a, S> QueryBuilder<'a, S, SelectInitial>
Sourcepub fn from<T>(self, query: T) -> SelectBuilder<'a, S, SelectFromSet, T>where
T: ToSQL<'a, SQLiteValue<'a>>,
pub fn from<T>(self, query: T) -> SelectBuilder<'a, S, SelectFromSet, T>where
T: ToSQL<'a, SQLiteValue<'a>>,
Specifies the table or subquery to select FROM.
This method transitions the builder from the initial state to the FROM state, enabling subsequent WHERE, JOIN, ORDER BY, and other clauses.
§Examples
// Select from a table
let query = builder.select(user.name).from(user);
assert_eq!(query.to_sql().sql(), r#"SELECT "users"."name" FROM "users""#);Source§impl<'a, S, T> QueryBuilder<'a, S, SelectFromSet, T>
impl<'a, S, T> QueryBuilder<'a, S, SelectFromSet, T>
Sourcepub fn join<U: SQLiteTable<'a>>(
self,
table: U,
condition: impl ToSQL<'a, SQLiteValue<'a>>,
) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
Adds an INNER JOIN clause to the query.
Joins another table to the current query using the specified condition. The joined table must be part of the schema and the condition should relate columns from both tables.
let query = builder
.select((user.name, post.title))
.from(user)
.join(post, eq(user.id, post.user_id));
assert_eq!(
query.to_sql().sql(),
r#"SELECT "users"."name", "posts"."title" FROM "users" JOIN "posts" ON "users"."id" = "posts"."user_id""#
);pub fn natural_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn natural_left_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn left_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn left_outer_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn natural_left_outer_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn natural_right_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn right_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn right_outer_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn natural_right_outer_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn natural_full_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn full_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn full_outer_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn natural_full_outer_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn inner_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn cross_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
Sourcepub fn where(
self,
condition: impl ToSQL<'a, SQLiteValue<'a>>,
) -> SelectBuilder<'a, S, SelectWhereSet, T>
pub fn where( self, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectWhereSet, T>
Adds a WHERE clause to filter query results.
This method applies conditions to filter the rows returned by the query.
You can use various condition functions from drizzle::core::expr::conditions.
// Single condition
let query = builder
.select(user.name)
.from(user)
.r#where(gt(user.id, 10));
assert_eq!(
query.to_sql().sql(),
r#"SELECT "users"."name" FROM "users" WHERE "users"."id" > ?"#
);
// Multiple conditions
let query = builder
.select(user.name)
.from(user)
.r#where(and([gt(user.id, 10), eq(user.name, "Alice")]));Sourcepub fn group_by(
self,
expressions: impl IntoIterator<Item = impl ToSQL<'a, SQLiteValue<'a>>>,
) -> SelectBuilder<'a, S, SelectGroupSet, T>
pub fn group_by( self, expressions: impl IntoIterator<Item = impl ToSQL<'a, SQLiteValue<'a>>>, ) -> SelectBuilder<'a, S, SelectGroupSet, T>
Adds a GROUP BY clause to the query
Sourcepub fn limit(self, limit: usize) -> SelectBuilder<'a, S, SelectLimitSet, T>
pub fn limit(self, limit: usize) -> SelectBuilder<'a, S, SelectLimitSet, T>
Limits the number of rows returned
Sourcepub fn offset(self, offset: usize) -> SelectBuilder<'a, S, SelectOffsetSet, T>
pub fn offset(self, offset: usize) -> SelectBuilder<'a, S, SelectOffsetSet, T>
Sets the offset for the query results
Sourcepub fn order_by<TOrderBy>(
self,
expressions: TOrderBy,
) -> SelectBuilder<'a, S, SelectOrderSet, T>where
TOrderBy: ToSQL<'a, SQLiteValue<'a>>,
pub fn order_by<TOrderBy>(
self,
expressions: TOrderBy,
) -> SelectBuilder<'a, S, SelectOrderSet, T>where
TOrderBy: ToSQL<'a, SQLiteValue<'a>>,
Sorts the query results
Sourcepub fn as_cte(
self,
name: &'static str,
) -> CTEView<'a, <T as SQLTable<'a, SQLiteSchemaType, SQLiteValue<'a>>>::Aliased, Self>
pub fn as_cte( self, name: &'static str, ) -> CTEView<'a, <T as SQLTable<'a, SQLiteSchemaType, SQLiteValue<'a>>>::Aliased, Self>
Converts this SELECT query into a CTE (Common Table Expression) with the given name.
The returned CTEView provides typed access to the table’s columns through
an aliased table instance, allowing you to reference CTE columns in subsequent queries.
§Type Parameters
The T (Table) type parameter from .from(table) determines the aliased type,
enabling type-safe field access on the returned CTE.
§Examples
// Create a CTE from a select query
let active_users = builder
.select((user.id, user.name))
.from(user)
.as_cte("active_users");
// Use the CTE with typed field access
let query = builder
.with(&active_users)
.select(active_users.name) // Deref gives access to aliased table fields
.from(&active_users);
assert_eq!(
query.to_sql().sql(),
r#"WITH active_users AS (SELECT "users"."id", "users"."name" FROM "users") SELECT "active_users"."name" FROM "active_users""#
);Source§impl<'a, S, T> QueryBuilder<'a, S, SelectJoinSet, T>
impl<'a, S, T> QueryBuilder<'a, S, SelectJoinSet, T>
Sourcepub fn where(
self,
condition: impl ToSQL<'a, SQLiteValue<'a>>,
) -> SelectBuilder<'a, S, SelectWhereSet, T>
pub fn where( self, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectWhereSet, T>
Adds a WHERE condition after a JOIN
Sourcepub fn order_by<TOrderBy>(
self,
expressions: TOrderBy,
) -> SelectBuilder<'a, S, SelectOrderSet, T>where
TOrderBy: ToSQL<'a, SQLiteValue<'a>>,
pub fn order_by<TOrderBy>(
self,
expressions: TOrderBy,
) -> SelectBuilder<'a, S, SelectOrderSet, T>where
TOrderBy: ToSQL<'a, SQLiteValue<'a>>,
Sorts the query results
Sourcepub fn join<U: SQLiteTable<'a>>(
self,
table: U,
condition: impl ToSQL<'a, SQLiteValue<'a>>,
) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
Adds a JOIN clause to the query
pub fn natural_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn natural_left_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn left_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn left_outer_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn natural_left_outer_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn natural_right_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn right_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn right_outer_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn natural_right_outer_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn natural_full_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn full_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn full_outer_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn natural_full_outer_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn inner_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
pub fn cross_join<U: SQLiteTable<'a>>( self, table: U, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, T>
Source§impl<'a, S, T> QueryBuilder<'a, S, SelectJoinSet, T>
impl<'a, S, T> QueryBuilder<'a, S, SelectJoinSet, T>
Sourcepub fn as_cte(
self,
name: &'static str,
) -> CTEView<'a, <T as SQLTable<'a, SQLiteSchemaType, SQLiteValue<'a>>>::Aliased, Self>
pub fn as_cte( self, name: &'static str, ) -> CTEView<'a, <T as SQLTable<'a, SQLiteSchemaType, SQLiteValue<'a>>>::Aliased, Self>
Converts this SELECT query into a CTE (Common Table Expression) with the given name.
Source§impl<'a, S, T> QueryBuilder<'a, S, SelectWhereSet, T>
impl<'a, S, T> QueryBuilder<'a, S, SelectWhereSet, T>
Sourcepub fn group_by(
self,
expressions: impl IntoIterator<Item = impl ToSQL<'a, SQLiteValue<'a>>>,
) -> SelectBuilder<'a, S, SelectGroupSet, T>
pub fn group_by( self, expressions: impl IntoIterator<Item = impl ToSQL<'a, SQLiteValue<'a>>>, ) -> SelectBuilder<'a, S, SelectGroupSet, T>
Adds a GROUP BY clause after a WHERE
Sourcepub fn order_by<TOrderBy>(
self,
expressions: TOrderBy,
) -> SelectBuilder<'a, S, SelectOrderSet, T>where
TOrderBy: ToSQL<'a, SQLiteValue<'a>>,
pub fn order_by<TOrderBy>(
self,
expressions: TOrderBy,
) -> SelectBuilder<'a, S, SelectOrderSet, T>where
TOrderBy: ToSQL<'a, SQLiteValue<'a>>,
Adds an ORDER BY clause after a WHERE
Sourcepub fn limit(self, limit: usize) -> SelectBuilder<'a, S, SelectLimitSet, T>
pub fn limit(self, limit: usize) -> SelectBuilder<'a, S, SelectLimitSet, T>
Adds a LIMIT clause after a WHERE
Source§impl<'a, S, T> QueryBuilder<'a, S, SelectWhereSet, T>
impl<'a, S, T> QueryBuilder<'a, S, SelectWhereSet, T>
Sourcepub fn as_cte(
self,
name: &'static str,
) -> CTEView<'a, <T as SQLTable<'a, SQLiteSchemaType, SQLiteValue<'a>>>::Aliased, Self>
pub fn as_cte( self, name: &'static str, ) -> CTEView<'a, <T as SQLTable<'a, SQLiteSchemaType, SQLiteValue<'a>>>::Aliased, Self>
Converts this SELECT query into a CTE (Common Table Expression) with the given name.
Source§impl<'a, S, T> QueryBuilder<'a, S, SelectGroupSet, T>
impl<'a, S, T> QueryBuilder<'a, S, SelectGroupSet, T>
Sourcepub fn having(
self,
condition: impl ToSQL<'a, SQLiteValue<'a>>,
) -> SelectBuilder<'a, S, SelectGroupSet, T>
pub fn having( self, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectGroupSet, T>
Adds a HAVING clause after GROUP BY
Sourcepub fn order_by<TOrderBy>(
self,
expressions: TOrderBy,
) -> SelectBuilder<'a, S, SelectOrderSet, T>where
TOrderBy: ToSQL<'a, SQLiteValue<'a>>,
pub fn order_by<TOrderBy>(
self,
expressions: TOrderBy,
) -> SelectBuilder<'a, S, SelectOrderSet, T>where
TOrderBy: ToSQL<'a, SQLiteValue<'a>>,
Adds an ORDER BY clause after GROUP BY
Source§impl<'a, S, T> QueryBuilder<'a, S, SelectGroupSet, T>
impl<'a, S, T> QueryBuilder<'a, S, SelectGroupSet, T>
Sourcepub fn as_cte(
self,
name: &'static str,
) -> CTEView<'a, <T as SQLTable<'a, SQLiteSchemaType, SQLiteValue<'a>>>::Aliased, Self>
pub fn as_cte( self, name: &'static str, ) -> CTEView<'a, <T as SQLTable<'a, SQLiteSchemaType, SQLiteValue<'a>>>::Aliased, Self>
Converts this SELECT query into a CTE (Common Table Expression) with the given name.
Source§impl<'a, S, T> QueryBuilder<'a, S, SelectOrderSet, T>
impl<'a, S, T> QueryBuilder<'a, S, SelectOrderSet, T>
Sourcepub fn limit(self, limit: usize) -> SelectBuilder<'a, S, SelectLimitSet, T>
pub fn limit(self, limit: usize) -> SelectBuilder<'a, S, SelectLimitSet, T>
Adds a LIMIT clause after ORDER BY
Source§impl<'a, S, T> QueryBuilder<'a, S, SelectOrderSet, T>
impl<'a, S, T> QueryBuilder<'a, S, SelectOrderSet, T>
Sourcepub fn as_cte(
self,
name: &'static str,
) -> CTEView<'a, <T as SQLTable<'a, SQLiteSchemaType, SQLiteValue<'a>>>::Aliased, Self>
pub fn as_cte( self, name: &'static str, ) -> CTEView<'a, <T as SQLTable<'a, SQLiteSchemaType, SQLiteValue<'a>>>::Aliased, Self>
Converts this SELECT query into a CTE (Common Table Expression) with the given name.
Source§impl<'a, S, T> QueryBuilder<'a, S, SelectLimitSet, T>
impl<'a, S, T> QueryBuilder<'a, S, SelectLimitSet, T>
Sourcepub fn offset(self, offset: usize) -> SelectBuilder<'a, S, SelectOffsetSet, T>
pub fn offset(self, offset: usize) -> SelectBuilder<'a, S, SelectOffsetSet, T>
Adds an OFFSET clause after LIMIT
Source§impl<'a, S, T> QueryBuilder<'a, S, SelectLimitSet, T>
impl<'a, S, T> QueryBuilder<'a, S, SelectLimitSet, T>
Sourcepub fn as_cte(
self,
name: &'static str,
) -> CTEView<'a, <T as SQLTable<'a, SQLiteSchemaType, SQLiteValue<'a>>>::Aliased, Self>
pub fn as_cte( self, name: &'static str, ) -> CTEView<'a, <T as SQLTable<'a, SQLiteSchemaType, SQLiteValue<'a>>>::Aliased, Self>
Converts this SELECT query into a CTE (Common Table Expression) with the given name.
Source§impl<'a, S, T> QueryBuilder<'a, S, SelectOffsetSet, T>
impl<'a, S, T> QueryBuilder<'a, S, SelectOffsetSet, T>
Sourcepub fn as_cte(
self,
name: &'static str,
) -> CTEView<'a, <T as SQLTable<'a, SQLiteSchemaType, SQLiteValue<'a>>>::Aliased, Self>
pub fn as_cte( self, name: &'static str, ) -> CTEView<'a, <T as SQLTable<'a, SQLiteSchemaType, SQLiteValue<'a>>>::Aliased, Self>
Converts this SELECT query into a CTE (Common Table Expression) with the given name.
Source§impl<'a, S, State, T> QueryBuilder<'a, S, State, T>where
State: ExecutableState,
impl<'a, S, State, T> QueryBuilder<'a, S, State, T>where
State: ExecutableState,
Sourcepub fn union(
self,
other: impl ToSQL<'a, SQLiteValue<'a>>,
) -> SelectBuilder<'a, S, SelectSetOpSet, T>
pub fn union( self, other: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectSetOpSet, T>
Combines this query with another using UNION.
Sourcepub fn union_all(
self,
other: impl ToSQL<'a, SQLiteValue<'a>>,
) -> SelectBuilder<'a, S, SelectSetOpSet, T>
pub fn union_all( self, other: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectSetOpSet, T>
Combines this query with another using UNION ALL.
Sourcepub fn intersect(
self,
other: impl ToSQL<'a, SQLiteValue<'a>>,
) -> SelectBuilder<'a, S, SelectSetOpSet, T>
pub fn intersect( self, other: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectSetOpSet, T>
Combines this query with another using INTERSECT.
Sourcepub fn intersect_all(
self,
other: impl ToSQL<'a, SQLiteValue<'a>>,
) -> SelectBuilder<'a, S, SelectSetOpSet, T>
pub fn intersect_all( self, other: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectSetOpSet, T>
Combines this query with another using INTERSECT ALL.
Sourcepub fn except(
self,
other: impl ToSQL<'a, SQLiteValue<'a>>,
) -> SelectBuilder<'a, S, SelectSetOpSet, T>
pub fn except( self, other: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectSetOpSet, T>
Combines this query with another using EXCEPT.
Sourcepub fn except_all(
self,
other: impl ToSQL<'a, SQLiteValue<'a>>,
) -> SelectBuilder<'a, S, SelectSetOpSet, T>
pub fn except_all( self, other: impl ToSQL<'a, SQLiteValue<'a>>, ) -> SelectBuilder<'a, S, SelectSetOpSet, T>
Combines this query with another using EXCEPT ALL.
Source§impl<'a, S, T> QueryBuilder<'a, S, SelectSetOpSet, T>
impl<'a, S, T> QueryBuilder<'a, S, SelectSetOpSet, T>
Sourcepub fn order_by<TOrderBy>(
self,
expressions: TOrderBy,
) -> SelectBuilder<'a, S, SelectOrderSet, T>where
TOrderBy: ToSQL<'a, SQLiteValue<'a>>,
pub fn order_by<TOrderBy>(
self,
expressions: TOrderBy,
) -> SelectBuilder<'a, S, SelectOrderSet, T>where
TOrderBy: ToSQL<'a, SQLiteValue<'a>>,
Sorts the results of a set operation.
Sourcepub fn limit(self, limit: usize) -> SelectBuilder<'a, S, SelectLimitSet, T>
pub fn limit(self, limit: usize) -> SelectBuilder<'a, S, SelectLimitSet, T>
Limits the results of a set operation.
Sourcepub fn offset(self, offset: usize) -> SelectBuilder<'a, S, SelectOffsetSet, T>
pub fn offset(self, offset: usize) -> SelectBuilder<'a, S, SelectOffsetSet, T>
Offsets the results of a set operation.
Source§impl<'a, Schema, Table> QueryBuilder<'a, Schema, UpdateInitial, Table>where
Table: SQLiteTable<'a>,
impl<'a, Schema, Table> QueryBuilder<'a, Schema, UpdateInitial, Table>where
Table: SQLiteTable<'a>,
Sourcepub fn set(
self,
values: Table::Update,
) -> UpdateBuilder<'a, Schema, UpdateSetClauseSet, Table>
pub fn set( self, values: Table::Update, ) -> UpdateBuilder<'a, Schema, UpdateSetClauseSet, Table>
Specifies which columns to update and their new values.
This method accepts update expressions that specify which columns should
be modified. You can update single or multiple columns using condition
functions from drizzle_core::expressions::conditions.
§Examples
// Update single column
let query = builder
.update(user)
.set(UpdateUser::default().with_name("New Name"));
assert_eq!(query.to_sql().sql(), r#"UPDATE "users" SET "name" = ?"#);
// Update multiple columns
let query = builder
.update(user)
.set(UpdateUser::default().with_name("New Name").with_email("new@example.com"));Source§impl<'a, S, T> QueryBuilder<'a, S, UpdateSetClauseSet, T>
impl<'a, S, T> QueryBuilder<'a, S, UpdateSetClauseSet, T>
Sourcepub fn where(
self,
condition: impl ToSQL<'a, SQLiteValue<'a>>,
) -> UpdateBuilder<'a, S, UpdateWhereSet, T>
pub fn where( self, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> UpdateBuilder<'a, S, UpdateWhereSet, T>
Adds a WHERE clause to specify which rows to update.
Without a WHERE clause, all rows in the table would be updated. This method allows you to specify conditions to limit which rows are affected by the update.
§Examples
// Update specific row by ID
let query = builder
.update(user)
.set(UpdateUser::default().with_name("Updated Name"))
.r#where(eq(user.id, 1));
assert_eq!(
query.to_sql().sql(),
r#"UPDATE "users" SET "name" = ? WHERE "users"."id" = ?"#
);
// Update multiple rows with complex condition
let query = builder
.update(user)
.set(UpdateUser::default().with_name("Updated"))
.r#where(and([gt(user.id, 10), eq(user.age, 25)]));Sourcepub fn returning(
self,
columns: impl ToSQL<'a, SQLiteValue<'a>>,
) -> UpdateBuilder<'a, S, UpdateReturningSet, T>
pub fn returning( self, columns: impl ToSQL<'a, SQLiteValue<'a>>, ) -> UpdateBuilder<'a, S, UpdateReturningSet, T>
Adds a RETURNING clause and transitions to the ReturningSet state
Source§impl<'a, S, T> QueryBuilder<'a, S, UpdateWhereSet, T>
impl<'a, S, T> QueryBuilder<'a, S, UpdateWhereSet, T>
Sourcepub fn returning(
self,
columns: impl ToSQL<'a, SQLiteValue<'a>>,
) -> UpdateBuilder<'a, S, UpdateReturningSet, T>
pub fn returning( self, columns: impl ToSQL<'a, SQLiteValue<'a>>, ) -> UpdateBuilder<'a, S, UpdateReturningSet, T>
Adds a RETURNING clause after WHERE
Source§impl<'a> QueryBuilder<'a>
impl<'a> QueryBuilder<'a>
Sourcepub const fn new<S>() -> QueryBuilder<'a, S, BuilderInit>
pub const fn new<S>() -> QueryBuilder<'a, S, BuilderInit>
Creates a new query builder for the given schema type.
This is the entry point for building SQL queries. The schema type parameter ensures that only valid tables from your schema can be used in queries.
§Examples
use drizzle::sqlite::prelude::*;
use drizzle::sqlite::builder::QueryBuilder;
#[SQLiteTable(name = "users")]
struct User {
#[column(primary)]
id: i32,
name: String,
}
#[derive(SQLiteSchema)]
struct MySchema {
user: User,
}
let builder = QueryBuilder::new::<MySchema>();Source§impl<'a, Schema> QueryBuilder<'a, Schema, BuilderInit>
impl<'a, Schema> QueryBuilder<'a, Schema, BuilderInit>
Sourcepub fn select<T>(&self, columns: T) -> SelectBuilder<'a, Schema, SelectInitial>where
T: ToSQL<'a, SQLiteValue<'a>>,
pub fn select<T>(&self, columns: T) -> SelectBuilder<'a, Schema, SelectInitial>where
T: ToSQL<'a, SQLiteValue<'a>>,
Begins a SELECT query with the specified columns.
This method starts building a SELECT statement. You can select individual columns,
multiple columns as a tuple, or use () to select all columns.
§Examples
// Select a single column
let query = builder.select(user.name).from(user);
assert_eq!(query.to_sql().sql(), r#"SELECT "users"."name" FROM "users""#);
// Select multiple columns
let query = builder.select((user.id, user.name)).from(user);
assert_eq!(query.to_sql().sql(), r#"SELECT "users"."id", "users"."name" FROM "users""#);Sourcepub fn select_distinct<T>(
&self,
columns: T,
) -> SelectBuilder<'a, Schema, SelectInitial>where
T: ToSQL<'a, SQLiteValue<'a>>,
pub fn select_distinct<T>(
&self,
columns: T,
) -> SelectBuilder<'a, Schema, SelectInitial>where
T: ToSQL<'a, SQLiteValue<'a>>,
Begins a SELECT DISTINCT query with the specified columns.
SELECT DISTINCT removes duplicate rows from the result set.
§Examples
let query = builder.select_distinct(user.name).from(user);
assert_eq!(query.to_sql().sql(), r#"SELECT DISTINCT "users"."name" FROM "users""#);Source§impl<'a, Schema> QueryBuilder<'a, Schema, CTEInit>
impl<'a, Schema> QueryBuilder<'a, Schema, CTEInit>
pub fn select<T>(&self, columns: T) -> SelectBuilder<'a, Schema, SelectInitial>where
T: ToSQL<'a, SQLiteValue<'a>>,
Sourcepub fn select_distinct<T>(
&self,
columns: T,
) -> SelectBuilder<'a, Schema, SelectInitial>where
T: ToSQL<'a, SQLiteValue<'a>>,
pub fn select_distinct<T>(
&self,
columns: T,
) -> SelectBuilder<'a, Schema, SelectInitial>where
T: ToSQL<'a, SQLiteValue<'a>>,
Begins a SELECT DISTINCT query with the specified columns after a CTE.
Sourcepub fn insert<Table>(
&self,
table: Table,
) -> InsertBuilder<'a, Schema, InsertInitial, Table>where
Table: SQLiteTable<'a>,
pub fn insert<Table>(
&self,
table: Table,
) -> InsertBuilder<'a, Schema, InsertInitial, Table>where
Table: SQLiteTable<'a>,
Begins an INSERT query after a CTE.
Sourcepub fn update<Table>(
&self,
table: Table,
) -> UpdateBuilder<'a, Schema, UpdateInitial, Table>where
Table: SQLiteTable<'a>,
pub fn update<Table>(
&self,
table: Table,
) -> UpdateBuilder<'a, Schema, UpdateInitial, Table>where
Table: SQLiteTable<'a>,
Begins an UPDATE query after a CTE.
Sourcepub fn delete<Table>(
&self,
table: Table,
) -> DeleteBuilder<'a, Schema, DeleteInitial, Table>where
Table: SQLiteTable<'a>,
pub fn delete<Table>(
&self,
table: Table,
) -> DeleteBuilder<'a, Schema, DeleteInitial, Table>where
Table: SQLiteTable<'a>,
Begins a DELETE query after a CTE.
pub fn with<C>(&self, cte: C) -> QueryBuilder<'a, Schema, CTEInit>where
C: CTEDefinition<'a>,
Source§impl<'a, Schema> QueryBuilder<'a, Schema, BuilderInit>
impl<'a, Schema> QueryBuilder<'a, Schema, BuilderInit>
Sourcepub fn insert<Table>(
&self,
table: Table,
) -> InsertBuilder<'a, Schema, InsertInitial, Table>where
Table: SQLiteTable<'a>,
pub fn insert<Table>(
&self,
table: Table,
) -> InsertBuilder<'a, Schema, InsertInitial, Table>where
Table: SQLiteTable<'a>,
Begins an INSERT query for the specified table.
This method starts building an INSERT statement. The table must be part of the schema and will be type-checked at compile time.
§Examples
let query = builder
.insert(user)
.values([InsertUser::new("Alice")]);
assert_eq!(query.to_sql().sql(), r#"INSERT INTO "users" ("name") VALUES (?)"#);Sourcepub fn update<Table>(
&self,
table: Table,
) -> UpdateBuilder<'a, Schema, UpdateInitial, Table>where
Table: SQLiteTable<'a>,
pub fn update<Table>(
&self,
table: Table,
) -> UpdateBuilder<'a, Schema, UpdateInitial, Table>where
Table: SQLiteTable<'a>,
Begins an UPDATE query for the specified table.
This method starts building an UPDATE statement. The table must be part of the schema and will be type-checked at compile time.
§Examples
let query = builder
.update(user)
.set(UpdateUser::default().with_name("Bob"))
.r#where(eq(user.id, 1));
assert_eq!(query.to_sql().sql(), r#"UPDATE "users" SET "name" = ? WHERE "users"."id" = ?"#);Sourcepub fn delete<Table>(
&self,
table: Table,
) -> DeleteBuilder<'a, Schema, DeleteInitial, Table>where
Table: SQLiteTable<'a>,
pub fn delete<Table>(
&self,
table: Table,
) -> DeleteBuilder<'a, Schema, DeleteInitial, Table>where
Table: SQLiteTable<'a>,
Begins a DELETE query for the specified table.
This method starts building a DELETE statement. The table must be part of the schema and will be type-checked at compile time.
§Examples
let query = builder
.delete(user)
.r#where(lt(user.id, 10));
assert_eq!(query.to_sql().sql(), r#"DELETE FROM "users" WHERE "users"."id" < ?"#);pub fn with<C>(&self, cte: C) -> QueryBuilder<'a, Schema, CTEInit>where
C: CTEDefinition<'a>,
Trait Implementations§
Source§impl<'a, Schema: Clone, State: Clone, Table: Clone> Clone for QueryBuilder<'a, Schema, State, Table>
impl<'a, Schema: Clone, State: Clone, Table: Clone> Clone for QueryBuilder<'a, Schema, State, Table>
Source§fn clone(&self) -> QueryBuilder<'a, Schema, State, Table>
fn clone(&self) -> QueryBuilder<'a, Schema, State, Table>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more