1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//! # Database: Query Builder
//!
//! ## Introduction
//!
//! Fabrique's database query builder provides a convenient, fluent interface to
//! creating and running database queries. It can be used to perform most
//! database operations in your application and works perfectly with all of
//! Fabrique's supported database systems.
//!
//! The Fabrique query builder uses the [sqlx::QueryBuilder] parameter binding
//! to protect your application against SQL injection attacks. There is no need
//! to clean or sanitize strings passed to the query builder as query bindings.
//!
//! ## Running Database Queries
//!
//! ### Retrieving All Rows From a Table
//!
//! You may use the [table][QueryBuilder::table] method provided by the
//! [QueryBuilder] struct to begin a query. The [table][QueryBuilder::table]
//! method returns a fluent query builder instance for the given table, allowing
//! you to chain more constraints onto the query and then finally retrieve the
//! results of the query using the [get][QueryBuilder::get] method:
//!
//! ```rust
//! # extern crate fabrique;
//! # extern crate sqlx;
//! # extern crate tokio;
//! # extern crate uuid;
//! # use fabrique::sql::QueryBuilder;
//! # use fabrique::prelude::*;
//! #
//! # #[fabrique::doctest]
//! # async fn main(pool: Pool<sqlx::Sqlite>) -> Result<(), fabrique::Error> {
//! let rows: Vec<(String, String, i32)> = QueryBuilder::table("products")
//! .select(&["id", "name", "price_cents"])
//! .get(&pool)
//! .await?;
//! # Ok(())
//! # }
//! ```
//!
//! The [get][QueryBuilder::get] method returns a `Vec` of database rows.
//! You may access each column's value by using the [Row::get] method, which
//! requires you to specify the expected type for each column:
//!
//! ```rust
//! # extern crate fabrique;
//! # extern crate sqlx;
//! # extern crate tokio;
//! # extern crate uuid;
//! # use fabrique::sql::QueryBuilder;
//! # use fabrique::prelude::*;
//! #
//! # #[fabrique::doctest]
//! # async fn main(pool: Pool<sqlx::Sqlite>) -> Result<(), fabrique::Error> {
//! let rows: Vec<(String, String, i32)> = QueryBuilder::table("products")
//! .select(&["id", "name", "price_cents"])
//! .get(&pool)
//! .await?;
//!
//! for (id, name, price_cents) in rows {
//! println!("Product {} costs {} cents", name, price_cents);
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ### Retrieving a Single Row From a Table
//!
//! If you just need to retrieve a single row from a database table, you may use
//! the [QueryBuilder][QueryBuilder] [first][QueryBuilder::first] method. This
//! method will return a single [Row][sqlx::Row] object:
//!
//! ```rust
//! # extern crate fabrique;
//! # extern crate sqlx;
//! # extern crate tokio;
//! # extern crate uuid;
//! # use fabrique::sql::QueryBuilder;
//! # use fabrique::prelude::*;
//! #
//! # #[fabrique::doctest]
//! # async fn main(pool: Pool<sqlx::Sqlite>) -> Result<(), fabrique::Error> {
//! let row: Option<(String, String, i32)> = QueryBuilder::table("products")
//! .select(&["id", "name", "price_cents"])
//! .first(&pool)
//! .await?;
//! # Ok(())
//! # }
//! ```
//!
//! If you would like to retrieve a single row from a database table, but throw
//! an error if no matching row is found, you may use the
//! [first_or_fail][QueryBuilder::first_or_fail] method:
//!
//! ```rust
//! # extern crate fabrique;
//! # extern crate sqlx;
//! # extern crate tokio;
//! # extern crate uuid;
//! # use fabrique::sql::QueryBuilder;
//! # use fabrique::prelude::*;
//! #
//! # #[fabrique::doctest]
//! # async fn main(pool: Pool<sqlx::Sqlite>) -> Result<(), fabrique::Error> {
//! # sqlx::query("INSERT INTO products (id, name, price_cents) VALUES ('550e8400-e29b-41d4-a716-446655440000', 'Widget', 1200)")
//! # .execute(&pool).await?;
//! let row: (String, String, i32) = QueryBuilder::table("products")
//! .select(&["id", "name", "price_cents"])
//! .first_or_fail(&pool)
//! .await?;
//! # Ok(())
//! # }
//! ```
pub use *;