utopia_query/lib.rs
1//! Queries as data, and everything that turns them into statements.
2//!
3//! A [`Query`] is one filter, order, page, selection, aggregate, join or
4//! union, as data: built in code (`Query::equal(...)`) or parsed from the
5//! JSON clients send ([`Query::parse`]). Builders compile lists of queries
6//! into statements for MySQL, MariaDB, PostgreSQL, SQLite, ClickHouse and
7//! MongoDB ([`builder`]), with parameter bindings, joins, aggregates,
8//! window functions, upserts and dialect features. Schema builders write
9//! DDL ([`schema`]). A SQL tokenizer ([`tokenizer`]), parser and serializer
10//! ([`ast`]) and a statement classifier ([`classifier`]) read SQL back.
11//!
12//! Values are PHP values ([`Value`], from `php-std`): queries and statements
13//! are shared with PHP, so integers stay distinct from floats and arrays
14//! keep their keys and order.
15//!
16//! PHP: ports `utopia-php/query`.
17//!
18//! | PHP | Rust |
19//! |---|---|
20//! | `Query` | [`Query`] |
21//! | `Method` | [`Method`] |
22//! | `Type`, `CursorDirection`, `OrderDirection`, `NullsPosition` | [`Type`], [`CursorDirection`], [`OrderDirection`], [`NullsPosition`] |
23//! | `Compiler` | [`Compiler`] |
24//! | `QuotesIdentifiers` | [`quote`], [`quote_literal`] |
25//! | `Builder\ParsedQuery` | [`ParsedQuery`] |
26//! | `Exception`, `Exception\ValidationException`, `Exception\UnsupportedException` | [`Error::Query`], [`Error::Validation`], [`Error::Unsupported`] |
27//! | `Builder\*` | [`builder`] |
28//! | `Hook\*` | [`hook`] |
29//! | `Schema\*` | [`schema`] |
30//! | `Tokenizer\*` | [`tokenizer`] |
31//! | `AST\*` | [`ast`] |
32//! | `Classifier\*` | [`classifier`] |
33//!
34//! ```
35//! use utopia_query::Query;
36//! use utopia_query::builder::MySql;
37//!
38//! let statement = MySql::new()
39//! .from("users", "")
40//! .select(["name", "email"])
41//! .filter([Query::equal("status", ["active"]), Query::greater_than("age", 18)])
42//! .limit(25)
43//! .build()?;
44//! assert_eq!(statement.query, "SELECT `name`, `email` FROM `users` WHERE `status` IN (?) AND `age` > ? LIMIT ?");
45//! # Ok::<(), utopia_query::Error>(())
46//! ```
47//!
48//! ```php
49//! use Utopia\Query\Builder\MySQL;
50//! use Utopia\Query\Query;
51//!
52//! $statement = (new MySQL())
53//! ->from('users')
54//! ->select(['name', 'email'])
55//! ->filter([Query::equal('status', ['active']), Query::greaterThan('age', 18)])
56//! ->limit(25)
57//! ->build();
58//! echo $statement->query, "\n"; // SELECT `name`, `email` FROM `users` WHERE `status` IN (?) AND `age` > ? LIMIT ?
59//! ```
60
61#![warn(missing_docs)]
62
63pub mod ast;
64pub mod builder;
65pub mod classifier;
66mod enums;
67mod error;
68pub mod hook;
69mod literal;
70mod method;
71mod query;
72mod quote;
73pub mod schema;
74pub mod tokenizer;
75mod value;
76
77pub use enums::{CursorDirection, NullsPosition, OrderDirection, Type};
78pub use error::{Code, Error, Result};
79pub use literal::StringLiteral;
80pub use method::Method;
81pub use query::{Compiler, DEFAULT_ALIAS, GROUP_BY_TIME_BUCKET_INTERVALS, ParsedQuery, Query, QueryOrJson, TimeBucket};
82pub use quote::{quote, quote_literal};
83pub use value::{Array, IntoValue, IntoValues, Object, Value, list, string};
84
85/// The getting started guide (`guide.md`), compiled and run with the doctests.
86#[cfg(doctest)]
87#[doc = include_str!("../guide.md")]
88pub struct GettingStarted;