quaint_forked/lib.rs
1//! # quaint
2//!
3//! A database client abstraction for reading and writing to an SQL database in a
4//! safe manner.
5//!
6//! ### Goals
7//!
8//! - Query generation when the database and conditions are not known at compile
9//! time.
10//! - Parameterized queries.
11//! - A modular design, separate AST for query building and visitors for
12//! different databases.
13//! - Database support behind a feature flag.
14//!
15//! ### Non-goals
16//!
17//! - Database-level type-safety in query building or being an ORM.
18//!
19//! ## Databases
20//!
21//! - SQLite
22//! - PostgreSQL
23//! - MySQL
24//! - Microsoft SQL Server
25//!
26//! ### Methods of connecting
27//!
28//! Quaint provides two options to connect to the underlying database.
29//!
30//! The [single connection method](single/struct.Quaint.html):
31//!
32//! ``` rust
33//! use quaint::{prelude::*, single::Quaint};
34//!
35//! #[tokio::main]
36//! async fn main() -> Result<(), quaint::error::Error> {
37//! let conn = Quaint::new("file:///tmp/example.db").await?;
38//! let result = conn.select(Select::default().value(1)).await?;
39//!
40//! assert_eq!(
41//! Some(1),
42//! result.into_iter().nth(0).and_then(|row| row[0].as_i64()),
43//! );
44//!
45//! Ok(())
46//! }
47//! ```
48//!
49//! The [pooled method](pooled/struct.Quaint.html):
50//!
51//! ``` rust
52//! use quaint::{prelude::*, pooled::Quaint};
53//!
54//! #[tokio::main]
55//! async fn main() -> Result<(), quaint::error::Error> {
56//! let pool = Quaint::builder("file:///tmp/example.db")?.build();
57//! let conn = pool.check_out().await?;
58//! let result = conn.select(Select::default().value(1)).await?;
59//!
60//! assert_eq!(
61//! Some(1),
62//! result.into_iter().nth(0).and_then(|row| row[0].as_i64()),
63//! );
64//!
65//! Ok(())
66//! }
67//! ```
68//!
69//! ### Using the AST module
70//!
71//! The crate can be used as an SQL string builder using the [ast](ast/index.html) and
72//! [visitor](visitor/index.html) modules.
73//!
74//! AST is generic for all databases and the visitors generate correct SQL
75//! syntax for the database.
76//!
77//! The visitor returns the query as a string and its parameters as a vector.
78//!
79//! ```
80//! # use quaint::{prelude::*, visitor::{Sqlite, Visitor}};
81//! # fn main() -> Result<(), quaint::error::Error> {
82//! let conditions = "word"
83//! .equals("meow")
84//! .and("age".less_than(10))
85//! .and("paw".equals("warm"));
86//!
87//! let query = Select::from_table("naukio").so_that(conditions);
88//! let (sql_str, params) = Sqlite::build(query)?;
89//!
90//! assert_eq!(
91//! "SELECT `naukio`.* FROM `naukio` WHERE (`word` = ? AND `age` < ? AND `paw` = ?)",
92//! sql_str,
93//! );
94//!
95//! assert_eq!(
96//! vec![
97//! Value::from("meow"),
98//! Value::from(10),
99//! Value::from("warm"),
100//! ],
101//! params
102//! );
103//! # Ok(())
104//! # }
105//! ```
106
107
108#[cfg(not(any(feature = "sqlite", feature = "postgresql", feature = "mysql", feature = "mssql")))]
109compile_error!("one of 'sqlite', 'postgresql', 'mysql' or 'mssql' features must be enabled");
110
111#[macro_use]
112mod macros;
113
114#[macro_use]
115extern crate metrics;
116
117#[cfg(feature = "bigdecimal")]
118extern crate bigdecimal_ as bigdecimal;
119
120pub mod ast;
121pub mod connector;
122pub mod error;
123#[cfg(feature = "pooled")]
124pub mod pooled;
125pub mod prelude;
126#[cfg(feature = "serde-support")]
127pub mod serde;
128pub mod single;
129#[cfg(test)]
130mod tests;
131pub mod visitor;
132
133pub use ast::Value;
134
135pub type Result<T> = std::result::Result<T, error::Error>;