skytable/macros.rs
1/*
2 * Copyright 2023, Sayan Nandan <nandansayan@outlook.com>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15*/
16
17#[macro_export]
18/// This macro can be used to create a [`Query`](struct@crate::Query), almost like a variadic function
19///
20/// ## Examples
21/// ```
22/// use skytable::query;
23///
24/// fn get_username() -> String { "sayan".to_owned() }
25/// fn get_counter() -> u64 { 100 }
26///
27/// let query1 = query!("select * from myspace.mymodel WHERE username = ?", get_username());
28/// assert_eq!(query1.param_cnt(), 1);
29/// let query2 = query!("update myspace.mymodel set counter += ? WHERE username = ?", get_counter(), get_username());
30/// assert_eq!(query2.param_cnt(), 2);
31/// ```
32macro_rules! query {
33    ($query_str:expr) => { $crate::Query::from($query_str) };
34    ($query_str:expr$(, $($query_param:expr),* $(,)?)?) => {{
35        let mut q = $crate::Query::from($query_str); $($(q.push_param($query_param);)*)*q
36    }};
37}
38
39macro_rules! pushlen {
40    ($buf:expr, $len:expr) => {{
41        let mut buf = ::itoa::Buffer::new();
42        let r = ::itoa::Buffer::format(&mut buf, $len);
43        $buf.extend(str::as_bytes(r));
44        $buf.push(b'\n');
45    }};
46}
47
48#[macro_export]
49/// The `pipe!` macro is used to create pipelines
50///
51/// ## Example usage
52/// ```
53/// use skytable::{pipe, query};
54///
55/// let pipe = pipe!(query!("sysctl report status"), query!("use $current"));
56/// assert_eq!(pipe.query_count(), 2);
57/// ```
58macro_rules! pipe {
59    ($($query:expr),+) => {{
60        let mut p = $crate::Pipeline::new();
61        $(p.push_owned($query);)*p
62    }}
63}