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