sqlx_type/
lib.rs

1//! Proc macros to perform type sql queries similarly to sqlx::query, but without the need
2//! to run `cargo sqlx prepare`
3//!
4//! A schema definition must be placed in "sqlx-type-schema.sql" in the root of a using crate:
5//!
6//! ```sql
7//! DROP TABLE IF EXISTS `t1`;
8//! CREATE TABLE `t1` (
9//!     `id` int(11) NOT NULL,
10//!     `cbool` tinyint(1) NOT NULL,
11//!     `cu8` tinyint UNSIGNED NOT NULL,
12//!     `cu16` smallint UNSIGNED NOT NULL,
13//!     `cu32` int UNSIGNED NOT NULL,
14//!     `cu64` bigint UNSIGNED NOT NULL,
15//!     `ci8` tinyint,
16//!     `ci16` smallint,
17//!     `ci32` int,
18//!     `ci64` bigint,
19//!     `ctext` varchar(100) NOT NULL,
20//!     `cbytes` blob,
21//!     `cf32` float,
22//!     `cf64` double
23//! ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
24//!
25//! ALTER TABLE `t1`
26//!     MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
27//! ```
28//! See [sql_type::schema] for a detailed description.
29//!
30//! [sql_type::schema]: https://docs.rs/sql-type/latest/sql_type/schema/index.html
31//!
32//! This schema can then be used to type queries:
33//!
34//! ``` no_run
35//! use {std::env, sqlx::MySqlPool, sqlx_type::query};
36//!
37//! async fn test() -> Result<(), sqlx::Error> {
38//!     let pool = MySqlPool::connect(&env::var("DATABASE_URL").unwrap()).await?;
39//!
40//!     let id = query!("INSERT INTO `t1` (`cbool`, `cu8`, `cu16`, `cu32`, `cu64`, `ctext`)
41//!         VALUES (?, ?, ?, ?, ?, ?)", true, 8, 1243, 42, 42, "Hello world")
42//!         .execute(&pool).await?.last_insert_id();
43//!
44//!     let row = query!("SELECT `cu16`, `ctext`, `ci32` FROM `t1` WHERE `id`=?", id)
45//!         .fetch_one(&pool).await?;
46//!
47//!     assert_eq!(row.cu16, 1234);
48//!     assert_eq!(row.ctext, "Hello would");
49//!     assert!(row.ci32.is_none());
50//!     Ok(())
51//! }
52//! ```
53#![forbid(unsafe_code)]
54use sqlx_type_macro;
55
56pub use crate::sqlx_type_macro::{query, query_as};
57
58/// Tag type for integer input
59#[doc(hidden)]
60pub struct Integer;
61
62/// Tag type for float input
63#[doc(hidden)]
64pub struct Float;
65
66/// Tag type for timestamp input
67#[doc(hidden)]
68pub struct Timestamp;
69
70/// Tag type for datetime input
71#[doc(hidden)]
72pub struct DateTime;
73
74/// Tag type for date input
75#[doc(hidden)]
76pub struct Date;
77
78/// Tag type for time input
79#[doc(hidden)]
80pub struct Time;
81
82/// Tag type for time input
83#[doc(hidden)]
84pub struct Any;
85
86
87/// If ArgIn<T> is implemented for J, it means that J can be used as for arguments of type T
88#[doc(hidden)]
89pub trait ArgIn<T> {}
90pub trait ArgOut<T, const IDX: usize> {}
91
92macro_rules! arg_io {
93    ( $dst: ty, $t: ty ) => {
94        impl ArgIn<$dst> for $t {}
95        impl ArgIn<$dst> for &$t {}
96        impl ArgIn<Option<$dst>> for $t {}
97        impl ArgIn<Option<$dst>> for &$t {}
98        impl ArgIn<Option<$dst>> for Option<$t> {}
99        impl ArgIn<Option<$dst>> for Option<&$t> {}
100        impl ArgIn<Option<$dst>> for &Option<$t> {}
101        impl ArgIn<Option<$dst>> for &Option<&$t> {}
102
103        impl<const IDX: usize> ArgOut<$dst, IDX> for $t {}
104        impl<const IDX: usize> ArgOut<Option<$dst>, IDX> for Option<$t> {}
105        impl<const IDX: usize> ArgOut<$dst, IDX> for Option<$t> {}
106    };
107}
108
109
110arg_io!(Any, u64);
111arg_io!(Any, i64);
112arg_io!(Any, u32);
113arg_io!(Any, i32);
114arg_io!(Any, u16);
115arg_io!(Any, i16);
116arg_io!(Any, u8);
117arg_io!(Any, i8);
118arg_io!(Any, String);
119arg_io!(Any, f64);
120arg_io!(Any, f32);
121arg_io!(Any, &str);
122
123arg_io!(Integer, u64);
124arg_io!(Integer, i64);
125arg_io!(Integer, u32);
126arg_io!(Integer, i32);
127arg_io!(Integer, u16);
128arg_io!(Integer, i16);
129arg_io!(Integer, u8);
130arg_io!(Integer, i8);
131
132arg_io!(String, String);
133
134arg_io!(Float, f64);
135arg_io!(Float, f32);
136
137arg_io!(u64, u64);
138arg_io!(i64, i64);
139arg_io!(u32, u32);
140arg_io!(i32, i32);
141arg_io!(u16, u16);
142arg_io!(i16, i16);
143arg_io!(u8, u8);
144arg_io!(i8, i8);
145arg_io!(bool, bool);
146arg_io!(f32, f32);
147arg_io!(f64, f64);
148
149arg_io!(&str, &str);
150arg_io!(&str, String);
151arg_io!(&str, std::borrow::Cow<'_, str>);
152
153arg_io!(&[u8], &[u8]);
154arg_io!(&[u8], Vec<u8>);
155arg_io!(Vec<u8>, Vec<u8>);
156
157arg_io!(Timestamp, chrono::NaiveDateTime);
158arg_io!(DateTime, chrono::NaiveDateTime);
159arg_io!(chrono::DateTime<chrono::Utc>, chrono::DateTime<chrono::Utc>);
160arg_io!(Timestamp, chrono::DateTime<chrono::Utc>);
161
162#[doc(hidden)]
163pub fn check_arg<T, T2: ArgIn<T>>(_: &T2) {}
164
165#[doc(hidden)]
166pub fn check_arg_list_hack<T, T2: ArgIn<T>>(_: &[T2]) {}
167
168#[doc(hidden)]
169pub fn arg_out<T, T2: ArgOut<T, IDX>, const IDX: usize>(v: T2) -> T2 {
170    v
171}
172
173#[doc(hidden)]
174pub fn convert_list_query(query: &str, list_sizes: &[usize]) -> String {
175    let mut query_iter = query.split("_LIST_");
176    let mut query = query_iter.next().expect("None empty query").to_string();
177    for size in list_sizes {
178        if *size == 0 {
179            query.push_str("NULL");
180        } else {
181            for i in 0..*size {
182                if i == 0 {
183                    query.push('?');
184                } else {
185                    query.push_str(", ?");
186                }
187            }
188        }
189        query.push_str(query_iter.next().expect("More _LIST_ in query"));
190    }
191    if query_iter.next().is_some() {
192        panic!("Too many _LIST_ in query");
193    }
194    query
195}
196
197#[cfg(test)]
198mod tests {
199    use super::*;
200
201    #[test]
202    fn test_convert_list_query() {
203        // This assert would fire and test will fail.
204        // Please note, that private functions can be tested too!
205        assert_eq!(
206            &convert_list_query("FOO (_LIST_) X _LIST_ O _LIST_ BAR (_LIST_)", &[0, 1, 2, 3]),
207            "FOO (NULL) X ? O ?, ? BAR (?, ?, ?)"
208        );
209    }
210}