1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * Created on Mon Aug 23 2021
 *
 * Copyright (c) 2021 Sayan Nandan <nandansayan@outlook.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *    http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
*/

//! # Data definition Language (DDL) Queries
//!
//! This module contains other modules, types, traits and functions to use the DDL
//! abilities of Skytable efficiently.
//!
//! ## Example: creating tables
//!
//! ```no_run
//! use skytable::ddl::{Ddl, Keymap, KeymapType};
//! use skytable::sync::Connection;
//!
//! let mut con = Connection::new("127.0.0.1", 2003).unwrap();
//! let table = Keymap::new("mykeyspace:mytable")
//!     .set_ktype(KeymapType::Str)
//!     .set_vtype(KeymapType::Binstr);
//! con.create_table(table).unwrap();
//! ```
//!

use crate::error::{errorstring, SkyhashError};
use crate::types::{Array, FlatElement};
use crate::Element;
use crate::IntoSkyhashBytes;
use crate::Query;
use crate::RespCode;
use crate::SkyResult;

cfg_async! {
    use crate::AsyncResult;
    use crate::actions::AsyncSocket;
}

cfg_sync! {
    use crate::actions::SyncSocket;
}

#[non_exhaustive]
#[derive(Debug, PartialEq)]
/// Data types for the Keymap data model
pub enum KeymapType {
    /// An unicode string
    Str,
    /// A binary string
    Binstr,
    /// A custom type
    Other(String),
}

/// A convenient representation for the `whereami` action
pub enum WhereAmI {
    /// The ID of the keyspace
    Keyspace(String),
    /// The ID of the keyspace (element 0) and the ID of the table (element 1)
    Table(String, String),
}

impl WhereAmI {
    /// Returns an entity group formatted string for this instance. For example:
    /// - if your connection level entity is just a keyspace, say `default`, then you'll get a string with "default"
    /// - Now, if you're in a table, say table `default` in the `default` keyspace, then you'll get a string with "default:default"
    pub fn into_entity_repr(self) -> String {
        match self {
            Self::Keyspace(ks) => ks,
            Self::Table(ks, tbl) => {
                format!("{ks}:{tbl}", ks = ks, tbl = tbl)
            }
        }
    }
}

impl KeymapType {
    fn priv_to_string(&self) -> String {
        match self {
            Self::Str => "str".to_owned(),
            Self::Binstr => "binstr".to_owned(),
            Self::Other(oth) => oth.clone(),
        }
    }
}

#[derive(Debug, PartialEq)]
/// A Keymap Model Table
///
pub struct Keymap {
    entity: String,
    ktype: Option<KeymapType>,
    vtype: Option<KeymapType>,
    volatile: bool,
}

impl Keymap {
    /// Create a new Keymap model with the provided entity and default types: `(binstr,binstr)`
    /// and the default volatility (by default a table is **not** volatile)
    pub fn new(entity: impl AsRef<str>) -> Self {
        Self {
            entity: entity.as_ref().to_owned(),
            ktype: None,
            vtype: None,
            volatile: false,
        }
    }
    /// Set the key type (defaults to `binstr`)
    pub fn set_ktype(mut self, ktype: KeymapType) -> Self {
        self.ktype = Some(ktype);
        self
    }
    /// Set the value type (defaults to `binstr`)
    pub fn set_vtype(mut self, vtype: KeymapType) -> Self {
        self.vtype = Some(vtype);
        self
    }
    /// Make the table volatile (defaults to `false`)
    pub fn set_volatile(mut self) -> Self {
        self.volatile = true;
        self
    }
}

/// Any object that represents a table and that can be turned into a query
pub trait CreateTableIntoQuery: Send + Sync {
    /// Turns self into a query
    fn into_query(self) -> Query;
}

impl CreateTableIntoQuery for Keymap {
    fn into_query(self) -> Query {
        let arg = format!(
            "keymap({ktype},{vtype})",
            ktype = self
                .ktype
                .as_ref()
                .unwrap_or(&KeymapType::Binstr)
                .priv_to_string(),
            vtype = self
                .vtype
                .as_ref()
                .unwrap_or(&KeymapType::Binstr)
                .priv_to_string(),
        );
        let q = Query::from("CREATE").arg("TABLE").arg(self.entity).arg(arg);
        if self.volatile {
            q.arg("volatile")
        } else {
            q
        }
    }
}

macro_rules! implement_ddl {
    (
        $(
            $(#[$attr:meta])+
            fn $name:ident$(<$($tyargs:ident : $ty:ident $(+$tye:lifetime)*),*>)?(
                $($argname:ident: $argty:ty),*) -> $ret:ty {
                    $($block:block)?
                    $($($mtch:pat)|+ => $expect:expr),+
                }
        )*
    ) => {
        #[cfg(feature = "sync")]
        #[cfg_attr(docsrs, doc(cfg(feature = "sync")))]
        /// [DDL queries](https://docs.skytable.io/ddl) that can be run on a sync socket
        /// connections
        pub trait Ddl: SyncSocket {
            $(
                $(#[$attr])*
                #[inline]
                fn $name<'s, $($($tyargs: $ty $(+$tye)*, )*)?>(&'s mut self $(, $argname: $argty)*) -> SkyResult<$ret> {
                    gen_match!(self.run($($block)?), $($($mtch)+, $expect),*)
                }
            )*
        }
        #[cfg(feature = "aio")]
        #[cfg_attr(docsrs, doc(cfg(feature = "aio")))]
        /// [DDL queries](https://docs.skytable.io/ddl) that can be run on async socket
        /// connections
        pub trait AsyncDdl: AsyncSocket {
            $(
                $(#[$attr])*
                #[inline]
                fn $name<'s, $($($tyargs: $ty $(+$tye)*, )*)?>(&'s mut self $(, $argname: $argty)*) -> AsyncResult<SkyResult<$ret>> {
                    Box::pin(async move {gen_match!(self.run($($block)?).await, $($($mtch)+, $expect),*)})
                }
            )*
        }
    };
}

cfg_async! {
    impl<T> AsyncDdl for T where T: AsyncSocket {}
}

cfg_sync! {
    impl<T> Ddl for T where T: SyncSocket {}
}

implement_ddl! {
    /// This function switches to the provided entity.
    ///
    /// This is equivalent to:
    /// ```text
    /// USE <entity>
    /// ```
    ///
    /// ## Example
    ///
    /// ```no_run
    /// use skytable::ddl::Ddl;
    /// use skytable::sync::Connection;
    ///
    /// let mut con = Connection::new("127.0.0.1", 2003).unwrap();
    /// con.switch("mykeyspace:mytable").unwrap();
    /// ```
    ///
    fn switch<T: IntoSkyhashBytes + 's>(entity: T) -> () {
        { Query::from("use").arg(entity) }
        Element::RespCode(RespCode::Okay) => ()
    }
    /// Create the provided keyspace
    ///
    /// This is equivalent to:
    /// ```text
    /// CREATE KEYSPACE <ksname>
    /// ```
    /// This will return true if the keyspace was created or false if the keyspace
    /// already exists
    fn create_keyspace(ks: impl IntoSkyhashBytes + 's) -> bool {
        { Query::from("CREATE").arg("KEYSPACE").arg(ks) }
        Element::RespCode(RespCode::Okay) => true,
        Element::RespCode(RespCode::ErrorString(estr)) => match_estr! {
            estr,
            errorstring::ERR_ALREADY_EXISTS => false
        }
    }
    /// Create a table from the provided configuration
    fn create_table(table: impl CreateTableIntoQuery + 's) -> () {
        { table.into_query() }
        Element::RespCode(RespCode::Okay) => ()
    }
    /// Drop the provided table
    ///
    /// This returns true if the table was removed for false if the table didn't exist
    fn drop_table(table: impl IntoSkyhashBytes + 's) -> bool {
        { Query::from("DROP").arg("TABLE").arg(table) }
        Element::RespCode(RespCode::Okay) => true,
        Element::RespCode(RespCode::ErrorString(estr)) => match_estr! {
            estr,
            errorstring::CONTAINER_NOT_FOUND => false
        }
    }
    /// Drop the provided keyspace
    ///
    fn drop_keyspace(keyspace: impl IntoSkyhashBytes + 's, force: bool) -> () {
        {
            let q = Query::from("DROP").arg("KEYSPACE").arg(keyspace);
            if force {
                q.arg("force")
            } else {
                q
            }
        }
        Element::RespCode(RespCode::Okay) => {}
    }
    /// Check what keyspace this connection is currently connected to
    fn whereami() -> WhereAmI {
        {
            Query::from("whereami")
        }
        Element::Array(
            Array::Flat(mut frr)
        ) => {
            if frr.iter().all(|v| matches!(v, FlatElement::String(_))) {
                return Err(SkyhashError::InvalidResponse.into());
            }
            match frr.len() {
                1 => WhereAmI::Keyspace(match frr.swap_remove(0) {
                    FlatElement::String(st) => st,
                    _ => unsafe {
                        core::hint::unreachable_unchecked()
                    }
                }),
                2 => {
                    let (ks, tbl) = match (frr.swap_remove(0), frr.swap_remove(1)) {
                        (FlatElement::String(ks),FlatElement::String(tbl)) => (ks, tbl),
                        _ => unsafe {
                            core::hint::unreachable_unchecked()
                        }
                    };
                    WhereAmI::Table(ks, tbl)
                }
                _ => return Err(SkyhashError::InvalidResponse.into()),
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{CreateTableIntoQuery, Keymap, KeymapType};
    #[test]
    fn test_query_generation_from_keymap() {
        let qgen = Keymap::new("mytbl")
            .set_ktype(KeymapType::Str)
            .set_vtype(KeymapType::Binstr)
            .into_query();
        let qman = crate::query!("CREATE", "TABLE", "mytbl", "keymap(str,binstr)");
        assert_eq!(qgen, qman);
    }
}