rust_query/
lib.rs

1#![allow(private_bounds, private_interfaces)]
2#![doc = include_str!("../README.md")]
3
4extern crate self as rust_query;
5
6#[macro_use]
7extern crate static_assertions;
8
9mod alias;
10mod ast;
11mod db;
12mod joinable;
13mod lazy;
14mod migrate;
15mod mymap;
16mod query;
17mod rows;
18mod schema;
19mod select;
20mod transaction;
21mod value;
22mod writable;
23
24pub use db::TableRow;
25pub use lazy::Lazy;
26use private::Reader;
27pub use rust_query_macros::{FromExpr, Select};
28use schema::from_macro::TypBuilder;
29pub use select::{IntoSelect, Select};
30pub use transaction::{Database, Transaction, TransactionWeak};
31use value::MyTyp;
32pub use value::aggregate::aggregate;
33pub use value::trivial::FromExpr;
34pub use value::{Expr, IntoExpr, UnixEpoch, optional::optional};
35pub use writable::Update;
36
37use crate::alias::JoinableTable;
38
39/// Types that are used as closure arguments.
40///
41/// You generally don't need to import these types.
42pub mod args {
43    pub use crate::query::Query;
44    pub use crate::rows::Rows;
45    pub use crate::value::aggregate::Aggregate;
46    pub use crate::value::optional::Optional;
47}
48
49/// Types to declare schemas and migrations.
50///
51/// A good starting point is too look at [crate::migration::schema].
52pub mod migration {
53    pub use crate::migrate::{
54        Migrator,
55        config::{Config, ForeignKeys, Synchronous},
56        migration::{Migrated, TransactionMigrate},
57    };
58    #[cfg(feature = "dev")]
59    pub use crate::schema::dev::hash_schema;
60    pub use rust_query_macros::schema;
61}
62
63/// These items are only exposed for use by the proc macros.
64/// Direct use is unsupported.
65#[doc(hidden)]
66pub mod private {
67    use std::marker::PhantomData;
68
69    pub use crate::joinable::Joinable;
70    pub use crate::migrate::{
71        Schema, SchemaMigration, TableTypBuilder,
72        migration::{Migration, SchemaBuilder},
73    };
74    pub use crate::query::get_plan;
75    pub use crate::schema::from_macro::TypBuilder;
76    pub use crate::value::{
77        DynTypedExpr, MyTyp, Typed, ValueBuilder, adhoc_expr, new_column, unique_from_joinable,
78    };
79    pub use crate::writable::{Reader, TableInsert};
80
81    pub struct Lazy<'t>(PhantomData<&'t ()>);
82    pub struct Ignore;
83    pub struct Custom<T>(PhantomData<T>);
84    pub struct AsUpdate;
85    pub struct AsExpr<'t>(PhantomData<&'t ()>);
86
87    pub trait Apply {
88        type Out<T: MyTyp, S>;
89    }
90
91    impl<'t> Apply for Lazy<'t> {
92        type Out<T: MyTyp, S> = T::Lazy<'t>;
93    }
94
95    impl Apply for Ignore {
96        type Out<T: MyTyp, S> = ();
97    }
98
99    impl<X> Apply for Custom<X> {
100        type Out<T: MyTyp, S> = X;
101    }
102
103    impl Apply for AsUpdate {
104        type Out<T: MyTyp, S> = crate::Update<S, T>;
105    }
106
107    impl<'t> Apply for AsExpr<'t> {
108        type Out<T: MyTyp, S> = crate::Expr<'t, S, T>;
109    }
110
111    pub trait UpdateOrUnit<S, T>: Default {}
112    impl<S, T: MyTyp> UpdateOrUnit<S, T> for crate::Update<S, T> {}
113    impl<S, T> UpdateOrUnit<S, T> for () {}
114
115    pub mod doctest {
116        use crate::{Database, Transaction, migrate::config::Config, migration};
117
118        #[migration::schema(Empty)]
119        pub mod vN {
120            pub struct User {
121                #[unique]
122                pub name: String,
123            }
124        }
125        pub use v0::*;
126
127        pub fn get_txn(f: impl Send + FnOnce(&'static mut Transaction<Empty>)) {
128            let db = Database::migrator(Config::open_in_memory())
129                .unwrap()
130                .finish()
131                .unwrap();
132            db.transaction_mut_ok(|txn| {
133                txn.insert(User { name: "Alice" }).unwrap();
134                f(txn)
135            })
136        }
137    }
138}
139
140/// This trait is implemented for all table types as generated by the [crate::migration::schema] macro.
141///
142/// **You can not implement this trait yourself!**
143pub trait Table: Sized + 'static {
144    #[doc(hidden)]
145    type Ext2<'t>;
146
147    #[doc(hidden)]
148    fn covariant_ext<'x, 't>(val: &'x Self::Ext2<'static>) -> &'x Self::Ext2<'t>;
149
150    #[doc(hidden)]
151    fn build_ext2<'t>(val: &Expr<'t, Self::Schema, Self>) -> Self::Ext2<'t>;
152
153    /// The schema that this table is a part of.
154    type Schema;
155
156    #[doc(hidden)]
157    /// The table that this table can be migrated from.
158    type MigrateFrom: MyTyp;
159
160    /// The type of conflict that can result from inserting a row in this table.
161    /// This is the same type that is used for row updates too.
162    type Conflict;
163
164    /// The type of updates used by [Transaction::update_ok].
165    type UpdateOk;
166    /// The type of updates used by [Transaction::update].
167    type Update;
168    /// The type of error when a delete fails due to a foreign key constraint.
169    type Referer;
170
171    #[doc(hidden)]
172    type Lazy<'t>;
173    #[doc(hidden)]
174    type Insert;
175
176    #[doc(hidden)]
177    fn read(val: &Self::Insert, f: &mut Reader<Self::Schema>);
178
179    #[doc(hidden)]
180    fn get_conflict_unchecked(
181        txn: &Transaction<Self::Schema>,
182        val: &Self::Insert,
183    ) -> Self::Conflict;
184
185    #[doc(hidden)]
186    fn update_into_try_update(val: Self::UpdateOk) -> Self::Update;
187
188    #[doc(hidden)]
189    fn apply_try_update(val: Self::Update, old: Expr<'static, Self::Schema, Self>) -> Self::Insert;
190
191    #[doc(hidden)]
192    fn get_referer_unchecked() -> Self::Referer;
193
194    #[doc(hidden)]
195    fn get_lazy<'t>(txn: &'t Transaction<Self::Schema>, row: TableRow<Self>) -> Self::Lazy<'t>;
196
197    // used for the first join (useful for pragmas)
198    #[doc(hidden)]
199    fn name(&self) -> JoinableTable {
200        JoinableTable::Normal(Self::NAME.into())
201    }
202    #[doc(hidden)]
203    fn typs(f: &mut TypBuilder<Self::Schema>);
204
205    #[doc(hidden)]
206    const SPAN: (usize, usize);
207
208    #[doc(hidden)]
209    const ID: &'static str;
210    #[doc(hidden)]
211    const NAME: &'static str;
212}
213
214#[test]
215fn compile_tests() {
216    let t = trybuild::TestCases::new();
217    t.compile_fail("tests/compile/*.rs");
218}