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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
//! Derive of the crate `cassandra_macro`
//!
//! Create Cassandra tables and CRUD CQL prepared statements
//! from Rust structs.
//!
//! This crate intends to reduce the boilerplate when create
//! and executing basic Cassandra statements. We can see that
//! the Rust struct holds the metadata for executing basic
//! operations. Before this crate, you will need to write the
//! Cassandra Table definition, create the Rust struct and
//! manually write all CQL statements for all operations.
//!
//! Worse than duplicated metadata, is refactoring code when you
//! add a new table, or change tables names and the statements
//! blows up because the table no longer exists.
//!
//! This crate is not perfect, and you are welcome to contribute.
//!
//! # Installation
//!
//! The crate `cdrs` is required since we make prepared statements
//! and the values of your struct must be mapped into Cassandra
//! datatypes and this crate does this mapping, and also, you will
//! need a driver between Rust code and your Cassandra cluster.
//!
//! ```toml
//! [dependencies]
//! cdrs = { version = "2" }
//! cassandra_macro = "0.1.2"
//! cassandra_macro_derive = "0.1.2"
//! ```
//!
//! In your `main.rs`
//!
//! ```
//! #[macro_use]
//! extern crate cdrs;
//! ```
//!
//! # Example
//! ```
//! #[macro_use]
//!extern crate cdrs;
//!
//!use std::sync::Arc;
//!
//!use cassandra_macro::{CassandraTable, DeleteQuery, Projection, UpdateQuery};
//!use cassandra_macro::StoreQuery;
//!use cassandra_macro_derive::CassandraTable;
//!use cdrs::authenticators::StaticPasswordAuthenticator;
//!use cdrs::cluster::{ClusterTcpConfig, NodeTcpConfigBuilder, TcpConnectionPool};
//!use cdrs::cluster::session::{new_lz4, Session};
//!use cdrs::Error as CassandraDriverError;
//!use cdrs::frame::TryFromRow;
//!use cdrs::load_balancing::RoundRobinSync;
//!use cdrs::query::{QueryExecutor, QueryValues};
//!use cdrs::types::ByName;
//!use cdrs::types::rows::Row;
//!use cdrs::types::value::Value;
//!use chrono::Utc;
//!use uuid::Uuid;
//!
//!#[table(keyspace = "test", options = "comment='Only for RUST users' | COMPACTION = {'class':'SizeTieredCompactionStrategy'}")]
//!#[derive(Debug, CassandraTable)]
//!pub struct User {
//!    #[column(type = "TEXT", primary_key)]
//!    username: String,
//!
//!    #[column(type = "UUID")]
//!    user_internal_id: Uuid,
//!
//!    #[column(type = "TEXT")]
//!    first_name: String,
//!
//!    #[column(type = "TIMESTAMP", cluster_key(order = "ASC", position = 1))]
//!    created: i64,
//!
//!    #[column(type = "TIMESTAMP")]
//!    updated: i64,
//!}
//!
//!impl User {
//!    fn set_first_name(&mut self, first_name: String) {
//!        self.first_name = first_name;
//!    }
//!}
//!
//!impl Default for User {
//!    fn default() -> Self {
//!        User {
//!            username: "Rust".to_string(),
//!            user_internal_id: Uuid::new_v4(),
//!            first_name: "rust".to_string(),
//!            created: Utc::now().timestamp_millis(),
//!            updated: Utc::now().timestamp_millis(),
//!        }
//!    }
//!}
//!
//!impl TryFromRow for User {
//!    fn try_from_row(row: Row) -> Result<Self, cdrs::Error> {
//!        let username = row.r_by_name::<String>("username")?;
//!        let user_internal_id = row.r_by_name::<Uuid>("user_internal_id")?;
//!        let first_name = row.r_by_name::<String>("first_name")?;
//!        let created: i64 = row.r_by_name::<i64>("created")?;
//!        let updated: i64 = row.r_by_name::<i64>("updated")?;
//!
//!        Ok(User {
//!            username,
//!            user_internal_id,
//!            first_name,
//!            created,
//!            updated,
//!        })
//!    }
//!}
//!
//!
//!pub struct CassandraConfig {
//!    nodes: Vec<String>,
//!    user: String,
//!    password: String,
//!}
//!
//!pub struct CassandraDriver {
//!    connection: Arc<Session<RoundRobinSync<TcpConnectionPool<StaticPasswordAuthenticator>>>>
//!}
//!
//!impl CassandraDriver {
//!    pub fn execute_simple_statement<Q: ToString>(&self, query: Q) -> Result<bool, CassandraDriverError> {
//!        match self.connection.query(query) {
//!            Ok(_) => Ok(true),
//!            Err(e) => {
//!                Err(e)
//!            }
//!        }
//!    }
//!
//!    pub fn execute_store_query(&self, query: &StoreQuery) -> Result<bool, CassandraDriverError> {
//!        self.execute_query(query.query(), query.values())
//!    }
//!
//!    pub fn execute_update_query(&self, query: &UpdateQuery) -> Result<bool, CassandraDriverError> {
//!        self.execute_query(query.query(), query.values())
//!    }
//!
//!    pub fn execute_delete_query(&self, query: &DeleteQuery) -> Result<bool, CassandraDriverError> {
//!        self.execute_query(query.query(), query.values())
//!    }
//!
//!    pub fn execute_query(&self, query: &String, values: &QueryValues) -> Result<bool, CassandraDriverError> {
//!        let result = self.connection
//!            .query_with_values(query, values.to_owned());
//!
//!        result.map(|_| true)
//!    }
//!
//!    pub fn find<T: TryFromRow + CassandraTable>(&self, keys: Vec<String>) -> Result<Option<T>, CassandraDriverError> {
//!        let stmt = T::select_by_primary_keys(Projection::All);
//!
//!        let values = keys.iter().map(|k| Value::from(k.to_string())).collect::<Vec<Value>>();
//!
//!        let result_frame = self.connection.query_with_values(stmt, QueryValues::SimpleValues(values))?;
//!
//!        Ok(result_frame.get_body()?.into_rows()
//!            .map(|r| { r.first().map(|r| T::try_from_row(r.to_owned()).unwrap()) }).flatten())
//!    }
//!
//!    pub fn new_from_config(cassandra_configs: &CassandraConfig) -> Self {
//!        let mut nodes = Vec::with_capacity(cassandra_configs.nodes.len());
//!
//!        for node in cassandra_configs.nodes.iter() {
//!            let authenticator: StaticPasswordAuthenticator = StaticPasswordAuthenticator::new(cassandra_configs.user.clone(),
//!                                                                                              cassandra_configs.password.clone());
//!
//!            let node_tcp = NodeTcpConfigBuilder::new(node.as_str(), authenticator).build();
//!
//!            nodes.push(node_tcp);
//!        }
//!
//!        let cluster_config = ClusterTcpConfig(nodes);
//!
//!        let cassandra_session = new_lz4(&cluster_config, RoundRobinSync::new())
//!            .expect("Cassandra session must be created");
//!
//!        CassandraDriver {
//!            connection: Arc::new(cassandra_session)
//!        }
//!    }
//!}
//!
//!fn main() {
//!    let driver_conf = CassandraConfig {
//!        nodes: vec!["aella:9042".to_string()],
//!        user: String::from("test"),
//!        password: String::from("test"),
//!    };
//!
//!    let connection = CassandraDriver::new_from_config(&driver_conf);
//!
//!    println!("Keyspace:.{}.", User::key_space());
//!    println!("Table name:.{}.", User::table_name());
//!    println!("Creating table:{}", User::create_table_cql());
//!    connection.execute_simple_statement(User::create_table_cql()).expect("Must create table");
//!
//!    println!("You can test those by yourself");
//!    println!("{}", User::select_by_primary_keys(Projection::Columns(vec!["created".to_string()])));
//!    println!("{}", User::select_by_primary_and_cluster_keys(Projection::All));
//!    println!("{}", User::update_by_primary_keys(vec!["updated".to_string()]));
//!    println!("{}", User::update_by_primary_and_cluster_keys(vec!["updated".to_string()]));
//!    println!("{}", User::delete_by_primary_keys());
//!    println!("{}", User::delete_by_primary_and_cluster_keys());
//!
//!    let mut rust_user = User::default();
//!
//!    println!("Storing rust: {}", rust_user.store_query().query());
//!    connection.execute_store_query(&rust_user.store_query()).expect("User must be stored");
//!
//!    let rust_user_from_db: Option<User> = connection.find::<User>(vec!["Rust".to_string()]).unwrap();
//!    assert!(rust_user_from_db.unwrap().username.eq(&rust_user.username), "Must be the same");
//!
//!    println!("Update rust:{}", rust_user.update_query().unwrap().query());
//!    rust_user.set_first_name(String::from("IamRoot"));
//!
//!    connection.execute_update_query(&rust_user.update_query().unwrap()).unwrap();
//!
//!    let rust_user_from_db_1 = connection.find::<User>(vec!["Rust".to_string()]).unwrap();
//!
//!    assert!(rust_user_from_db_1.unwrap().username.eq(&rust_user.username), "Must be the same");
//!
//!    println!("Delete:{}", rust_user.delete_query().query());
//!    connection.execute_delete_query(&rust_user.delete_query()).expect("Must be deleted");
//!
//!    println!("Dropping table: {}", User::drop_table_cql());
//!    connection.execute_simple_statement(User::drop_table_cql()).expect("Table must be removed");
//!}
//! ```
#![recursion_limit = "128"]
extern crate proc_macro;

use proc_macro::TokenStream;
use std::collections::{BTreeMap, HashMap};

use syn;
use syn::NestedMeta;
use std::str::FromStr;

use quote::{quote, ToTokens};

#[proc_macro_derive(CassandraTable, attributes(column, table))]
pub fn cassandra_macro_derive(input: TokenStream) -> TokenStream {
    // Construct a representation of Rust code as a syntax tree
    // that we can manipulate
    let ast = syn::parse(input).unwrap();

    // Build the trait implementation
    impl_cassandra_macro(&ast)
}

fn impl_cassandra_macro(ast: &syn::DeriveInput) -> TokenStream {
    let table_name = pascal_case_to_snake_case(&ast.ident.to_string());

    let mut table_meta = TableMeta::with_name(&table_name);

    // Ensure the macro is on a struct with named fields
    let fields = match ast.data {
        syn::Data::Struct(syn::DataStruct { ref fields, .. }) => {
            if fields.iter().any(|field| field.ident.is_none()) {
                panic!("struct has unnamed fields");
            }
            fields.iter().cloned().collect()
        }
        _ => panic!("#[derive(CassandraConfig)] can only be used with structs"),
    };

    extract_struct_attributes(&mut table_meta, &fields);

    for attr in ast.attrs.iter() {
        match attr.parse_meta() {
            Ok(syn::Meta::List(syn::MetaList { ref path, ref nested, .. })) => {
                let ident = path.get_ident().unwrap();
                match ident.to_string().as_ref() {
                    "table" => {
                        let mut meta_items_iter = nested.iter();

                        let mut meta_items = Vec::new();

                        while let Some(n) = meta_items_iter.next() {
                            meta_items.push(n);
                        }

                        let (key_space, options) = extract_table_properties(&meta_items);

                        table_meta.set_key_space(&key_space);
                        table_meta.set_table_options(&options);
                    }
                    _ => {}
                }
            }
            Err(_) => unreachable!(
                "Got something other than a list of attributes while checking table attribute"),
            _ => {}
        }
    }

    let create_table_sql = table_meta.create_table_cql();
    let drop_table_sql = table_meta.drop_table_cql();
    let key_space = table_meta.key_space();
    let table_name = table_meta.table_name();
    let select_by_key = table_meta.select_by_key();
    let select_by_keys = table_meta.select_by_keys();

    let update_by_key = table_meta.update_by_key();
    let update_by_keys = table_meta.update_by_keys();

    let delete_by_key = table_meta.delete_by_key();
    let delete_by_keys = table_meta.delete_by_keys();

    let store_stmt = table_meta.store_stmt();
    let store_values = table_meta.store_values();

    let (update_stmt, update_values) = table_meta.update_stmt()
        .unwrap_or((String::new(), proc_macro2::TokenStream::new()));

    let (delete_stmt, delete_values) = table_meta.delete_stmt();

    let ident = &ast.ident;

    // Helper is provided for handling complex generic types correctly and effortlessly
    let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
    let impl_ast = quote!(

        impl #impl_generics CassandraTable for #ident #ty_generics #where_clause {

            fn create_table_cql() -> &'static str {
                &#create_table_sql
            }

            fn drop_table_cql() -> &'static str {
                &#drop_table_sql
            }

            fn key_space() -> &'static str {
                &#key_space
            }

            fn table_name() -> &'static str {
                &#table_name
            }

            fn select_by_primary_keys(projection: cassandra_macro::Projection) -> String {
                match projection {
                    Projection::Count => {
                         #select_by_key.to_string().replace("*", "count(*) as count")
                    },
                    Projection::All => {
                        #select_by_key.to_string()
                    },
                    Projection::Columns(c) => {
                         let column_selection : String = c.join(",");

                         #select_by_key.to_string().replace("*", column_selection.as_str())
                    }
                }
            }

            fn select_by_primary_and_cluster_keys(projection: cassandra_macro::Projection) -> String {
                match projection {
                    Projection::Count => {
                         #select_by_keys.to_string().replace("*", "count(*) as count")
                    },
                    Projection::All => {
                        #select_by_keys.to_string()
                    },
                    Projection::Columns(c) => {
                         let column_selection : String = c.join(",");

                         #select_by_keys.to_string().replace("*", column_selection.as_str())
                    }
                }
            }

            fn update_by_primary_keys(columns: Vec<String>) -> String {

                let update_columns = columns.iter().map(|c| format!(" {}=?", c)).collect::<Vec<String>>().join(",");

                #update_by_key.to_string().replace(":columns", update_columns.as_str())
            }

            fn update_by_primary_and_cluster_keys(columns: Vec<String>) -> String {

                let update_columns = columns.iter().map(|c| format!(" {}=?", c)).collect::<Vec<String>>().join(",");

                #update_by_keys.to_string().replace(":columns", update_columns.as_str())
            }

            fn delete_by_primary_keys() -> String {
                #delete_by_key.to_string()
            }

            fn delete_by_primary_and_cluster_keys() -> String {
                #delete_by_keys.to_string()
            }

            fn store_query(&self) -> cassandra_macro::StoreQuery {
                cassandra_macro::StoreQuery::new(#store_stmt.to_string(), query_values!(#store_values))
            }

            fn update_query(&self) -> Result<cassandra_macro::UpdateQuery, cassandra_macro::TableWithNoUpdatableColumnsError>
            {
               if #update_stmt.to_string().is_empty() {
                    return Err(cassandra_macro::TableWithNoUpdatableColumnsError::new(format!("Table {} does not have any updatable column", #table_name)) );
               }

               Ok(cassandra_macro::UpdateQuery::new(#update_stmt.to_string(), query_values!(#update_values)))
            }

            fn delete_query(&self) -> cassandra_macro::DeleteQuery {
                cassandra_macro::DeleteQuery::new(#delete_stmt.to_string(), query_values!(#delete_values))
            }

        }
    );

    impl_ast.into()
}

struct TableMeta {
    name: String,
    key_space: String,
    table_options: String,
    columns: HashMap<String, String>,
    static_columns: Vec<String>,
    primary_keys: BTreeMap<u8, String>,
    cluster_keys: BTreeMap<u8, (String, String)>,
}

/// @TODO Refactor duplicated code
impl TableMeta {
    fn with_name(name: &String) -> Self {
        TableMeta {
            name: name.to_owned(),
            key_space: String::new(),
            table_options: String::new(),
            columns: HashMap::new(),
            static_columns: Vec::new(),
            primary_keys: BTreeMap::new(),
            cluster_keys: BTreeMap::new(),
        }
    }

    fn delete_stmt(&self) -> (String, proc_macro2::TokenStream) {
        let pk_values: Vec<String> = self.primary_keys.values().map(|p| p.to_owned()).collect();

        let ck_values: Vec<String> = self.cluster_keys.values().map(|(c, _)| c.to_owned()).collect();

        let keys: Vec<(String, String)> = [&pk_values[..], &ck_values[..]]
            .concat()
            .iter()
            .map(|c| {
                (format!("{}=?", c), format!("self.{}.clone()", c))
            })
            .collect::<Vec<(String, String)>>();

        (format!("DELETE FROM {}.{} WHERE {}",
                 self.key_space,
                 self.name,
                 keys.iter().map(|(v, _)| v.to_owned())
                     .collect::<Vec<String>>()
                     .join(" AND ")),
         proc_macro2::TokenStream::from_str(keys.iter().map(|(_, v)| v.to_owned())
             .collect::<Vec<String>>()
             .join(",").as_str()).unwrap()
        )
    }

    fn update_stmt(&self) -> Option<(String, proc_macro2::TokenStream)> {
        let mut updatable_columns = Vec::new();

        for (column_name, _) in self.columns.iter() {
            if self.primary_keys.values().any(|p| p.eq(column_name)) {
                continue;
            }

            if self.cluster_keys.values().any(|ck| ck.0.eq(column_name)) {
                continue;
            }

            updatable_columns.push(column_name);
        }

        if updatable_columns.is_empty() {
            return None;
        }

        let update_values = updatable_columns.iter().map(|c| {
            (format!("{}=?", c), format!("self.{}.clone()", c))
        }).collect::<Vec<(String, String)>>();

        let p_keys = self.primary_keys.iter().map(|(_, pk)| {
            (format!("{}=?", pk), format!("self.{}.clone()", pk))
        }).collect::<Vec<(String, String)>>();

        let ck_keys = self.cluster_keys.iter().map(|(_, (ck, _))| {
            (format!("{}=?", ck), format!("self.{}.clone()", ck))
        }).collect::<Vec<(String, String)>>();

        let values: String = [&update_values[..], &p_keys[..], &ck_keys[..]]
            .concat()
            .iter()
            .map(|(_, c)| c.to_owned())
            .collect::<Vec<String>>()
            .join(",");

        let pk_values: Vec<String> = self.primary_keys.values().map(|p| p.to_owned()).collect();

        let ck_values: Vec<String> = self.cluster_keys.values().map(|(c, _)| c.to_owned()).collect();

        let keys: Vec<(String, String)> = [&pk_values[..], &ck_values[..]]
            .concat()
            .iter()
            .map(|c| {
                (format!("{}=?", c), format!("self.{}.clone()", c))
            })
            .collect::<Vec<(String, String)>>();

        Some((format!("UPDATE {}.{} SET {} WHERE {}",
                      self.key_space,
                      self.name,
                      update_values.iter().map(|(v, _)| v.to_owned()).collect::<Vec<String>>().join(","),
                      keys.iter().map(|(v, _)| v.to_owned()).collect::<Vec<String>>().join(" AND ")),
              proc_macro2::TokenStream::from_str(values.as_str()).unwrap()
        ))
    }

    fn store_stmt(&self) -> String {
        let fields = self.columns.iter().map(|(n, _)| n.to_owned()).collect::<Vec<String>>().join(",");

        let mut bind_marks = "?,".repeat(self.columns.len());
        bind_marks.pop();

        format!("INSERT INTO {}.{} ({}) VALUES ({})", self.key_space, self.name, fields, bind_marks)
    }

    fn store_values(&self) -> proc_macro2::TokenStream {
        let fields_tokens = self.columns.iter().map(|(v, _)| {
            format!("self.{}.clone()", v.to_owned())
        }).collect::<Vec<String>>().join(",");

        proc_macro2::TokenStream::from_str(fields_tokens.as_str()).unwrap()
    }

    fn set_key_space(&mut self, key_space: &String) {
        self.key_space = key_space.to_owned();
    }

    fn select_by_key(&self) -> String {
        let where_part = self.primary_keys
            .iter()
            .map(|(_, v)| format!(" {}=? ", v))
            .collect::<Vec<String>>()
            .join("AND");

        format!("SELECT * FROM {}.{} WHERE {}", self.key_space, self.name, where_part)
    }

    fn select_by_keys(&self) -> String {
        let pk_select = self.select_by_key();

        if self.cluster_keys.is_empty() {
            pk_select
        } else {
            let where_part = self.cluster_keys
                .iter()
                .map(|(_, (c, _))| format!(" {}=? ", c))
                .collect::<Vec<String>>()
                .join("AND");

            format!("{} AND {}", pk_select, where_part)
        }
    }

    fn update_by_key(&self) -> String {
        let where_part = self.primary_keys
            .iter()
            .map(|(_, v)| format!(" {}=? ", v))
            .collect::<Vec<String>>()
            .join("AND");

        format!("UPDATE {}.{} SET :columns WHERE {}", self.key_space, self.name, where_part)
    }

    fn update_by_keys(&self) -> String {
        let pk_select = self.update_by_key();

        if self.cluster_keys.is_empty() {
            pk_select
        } else {
            let where_part = self.cluster_keys
                .iter()
                .map(|(_, (c, _))| format!(" {}=? ", c))
                .collect::<Vec<String>>()
                .join("AND");

            format!("{} AND {}", pk_select, where_part)
        }
    }

    fn delete_by_key(&self) -> String {
        let where_part = self.primary_keys
            .iter()
            .map(|(_, v)| format!(" {}=? ", v))
            .collect::<Vec<String>>()
            .join("AND");

        format!("DELETE FROM {}.{} WHERE {}", self.key_space, self.name, where_part)
    }

    fn delete_by_keys(&self) -> String {
        let pk_select = self.delete_by_key();

        if self.cluster_keys.is_empty() {
            pk_select
        } else {
            let where_part = self.cluster_keys
                .iter()
                .map(|(_, (c, _))| format!(" {}=? ", c))
                .collect::<Vec<String>>()
                .join("AND");

            format!("{} AND {}", pk_select, where_part)
        }
    }

    fn set_table_options(&mut self, table_options: &String) {
        self.table_options = table_options.to_owned();
    }

    fn new_column(&mut self, name: &String, data_type: &String) {
        self.columns.insert(name.to_owned(), data_type.to_owned());
    }

    fn set_column_as_static(&mut self, name: &String) {
        self.static_columns.push(name.to_owned());
    }

    fn new_primary_key(&mut self, key: &String, position: Option<u8>) {
        self.primary_keys.insert(position.unwrap_or(1), key.to_owned());
    }

    fn new_cluster_key(&mut self, name: &String, order: &String, position: Option<u8>) {
        self.cluster_keys.insert(position.unwrap_or(1), (name.to_owned(), order.to_owned()));
    }

    fn key_space(&self) -> &String {
        &self.key_space
    }

    fn table_name(&self) -> &String {
        &self.name
    }

    fn drop_table_cql(&self) -> String {
        format!("DROP TABLE IF EXISTS {}.{}", self.key_space, self.name)
    }

    fn create_table_cql(&self) -> String {
        let mut table_options = String::new();
        let mut c_order = Vec::new();
        let mut c_keys = Vec::new();

        let columns: String = self.columns
            .iter()
            .map(|(k, t)| {
                if self.static_columns.contains(k) {
                    format!("{} {} STATIC", k, t)
                } else {
                    format!("{} {}", k, t.to_uppercase())
                }
            })
            .collect::<Vec<String>>()
            .join(",");

        let opt_parts: Vec<&str> = self.table_options.split("|").filter(|opt| !opt.is_empty()).collect();

        if self.cluster_keys.len() > 0 {
            for (_, (column, order)) in self.cluster_keys.iter() {
                c_order.push(format!("{} {}", column, order));
                c_keys.push(format!("{}", column))
            }
            table_options = format!("WITH CLUSTERING ORDER BY ({})", c_order.join(","));

            if opt_parts.len() > 0 {
                table_options = format!("{} AND {}", table_options, opt_parts.join(" AND "))
            }
        } else {
            if opt_parts.len() > 0 {
                table_options = format!("WITH {}", opt_parts.join(" AND "))
            }
        }

        let primary_keys: String = self.primary_keys
            .iter()
            .map(|(_, k)| format!("{}", k))
            .collect::<Vec<String>>()
            .join(",");

        let create_stmt = format!("CREATE TABLE IF NOT EXISTS {}.{} ", self.key_space, self.name);

        if c_keys.len() > 0 {
            format!("{} ({}, PRIMARY KEY (({}), {}) ) {}", create_stmt, columns, primary_keys, c_keys.join(","), table_options)
        } else {
            format!("{} ({}, PRIMARY KEY ({}) ) {}", create_stmt, columns, primary_keys, table_options)
        }
    }
}

/// Parse struct attributes
fn extract_struct_attributes(table_meta: &mut TableMeta, fields: &Vec<syn::Field>) {
    for field in fields {
        let field_ident = field.ident.clone().unwrap().to_string();

        if field.attrs.len() > 0 {
            for attr in &field.attrs {
                if !attr.path.to_token_stream().to_string().contains("column") {
                    continue;
                }

                match attr.parse_meta() {
                    Ok(syn::Meta::List(syn::MetaList { ref nested, .. })) => {
                        let mut meta_items_iter = nested.iter();

                        let mut meta_items = Vec::new();

                        while let Some(n) = meta_items_iter.next() {
                            meta_items.push(n);
                        }

                        // only validation from there on
                        for meta_item in meta_items {
                            match *meta_item {
                                syn::NestedMeta::Meta(ref item) => match *item {
                                    syn::Meta::Path(ref name) => {
                                        match name.get_ident().unwrap().to_string().as_ref() {
                                            "primary_key" => {
                                                table_meta.new_primary_key(&field_ident, None);
                                            }
                                            "static" => {
                                                table_meta.set_column_as_static(&field_ident);
                                            }
                                            _ => panic!("Unexpected validator: {:?}", name.get_ident()),
                                        }
                                    }
                                    syn::Meta::NameValue(syn::MetaNameValue { ref path, ref lit, .. }) => {
                                        let ident = path.get_ident().unwrap();
                                        match ident.to_string().as_ref() {
                                            "type" => {
                                                table_meta.new_column(&field_ident.clone(), &lit_to_string(lit).unwrap_or(String::new()));
                                            }
                                            v => panic!("unexpected name value validator: {:?}", v),
                                        };
                                    }
                                    syn::Meta::List(syn::MetaList { ref path, ref nested, .. }) => {
                                        let mut meta_items_iter = nested.iter();

                                        let mut meta_items: Vec<&NestedMeta> = Vec::new();

                                        while let Some(n) = meta_items_iter.next() {
                                            meta_items.push(n);
                                        }

                                        let ident = path.get_ident().unwrap();
                                        match ident.to_string().as_ref() {
                                            "cluster_key" => {
                                                let (order, position) = extract_cluster_properties(&meta_items);

                                                table_meta.new_cluster_key(&field_ident, &order, Some(position));
                                            }
                                            "compound_key" => {
                                                let (_, position) = extract_cluster_properties(&meta_items);

                                                table_meta.new_primary_key(&field_ident, Some(position))
                                            }
                                            v => panic!("unexpected list validator: {:?}", v),
                                        }
                                    }
                                },
                                _ => unreachable!("Found a non Meta while looking for validators"),
                            };
                        }
                    }
                    Ok(syn::Meta::NameValue(_)) => panic!("Unexpected name=value argument"),
                    Err(e) => unreachable!(
                        "Got something other than a list of attributes while checking field `{}`: {:?}",
                        field_ident, e
                    ),
                    _ => {}
                }
            }
        }
    }
}

fn lit_to_string(lit: &syn::Lit) -> Option<String> {
    match *lit {
        syn::Lit::Str(ref s) => Some(s.value()),
        _ => None,
    }
}

fn lit_to_int(lit: &syn::Lit) -> Option<i64> {
    match *lit {
        syn::Lit::Int(ref s) => Some(s.base10_parse().unwrap()),
        _ => None,
    }
}

fn extract_cluster_properties(meta_items: &Vec<&syn::NestedMeta>) -> (String, u8) {
    let mut order = String::from("DESC");
    let mut position = 1;

    for meta_item in meta_items {
        if let syn::NestedMeta::Meta(ref item) = **meta_item {
            if let syn::Meta::NameValue(syn::MetaNameValue { ref path, ref lit, .. }) = *item {
                let ident = path.get_ident().unwrap();
                match ident.to_string().as_ref() {
                    "order" => {
                        order = lit_to_string(lit).unwrap_or(String::from("DESC"))
                    }
                    "position" => {
                        position = lit_to_int(lit).unwrap_or(1) as u8;
                    }
                    v => panic!("unknown argument `{}` for column `cluster_key`", v)
                }
            } else {
                panic!("unexpected item while parsing `cluster_key` column of field")
            }
        }
    }

    (order, position)
}

fn extract_table_properties(meta_items: &Vec<&syn::NestedMeta>) -> (String, String) {
    let mut keyspace = String::new();
    let mut options = String::new();

    for meta_item in meta_items {
        if let syn::NestedMeta::Meta(ref item) = **meta_item {
            if let syn::Meta::NameValue(syn::MetaNameValue { ref path, ref lit, .. }) = *item {
                let ident = path.get_ident().unwrap();
                match ident.to_string().as_ref() {
                    "keyspace" => {
                        keyspace = lit_to_string(lit).unwrap_or(String::new())
                    }
                    "options" => {
                        options = lit_to_string(lit).unwrap_or(String::new());
                    }
                    v => panic!("unknown argument `{}` for column `table`", v)
                }
            } else {
                panic!("unexpected item while parsing `table` column of field")
            }
        }
    }

    (keyspace, options)
}

const OFFSET: u8 = 32;
const UNDERSCORE: u8 = 95;

fn pascal_case_to_snake_case(table_name: &String) -> String {
    let word_size = table_name.len();

    if word_size < 2 {
        return String::from(table_name);
    }

    let mut counter = 1;
    let chars = table_name.as_bytes();
    let mut sk_table_name: Vec<u8> = Vec::new();

    if chars[0] < 90 {
        sk_table_name.push(chars[0] + OFFSET);
    } else {
        sk_table_name.push(chars[0]);
    }

    while counter < word_size {
        let current = chars[counter];

        if current < 90 {
            sk_table_name.push(UNDERSCORE);
            sk_table_name.push(current + OFFSET)
        } else {
            sk_table_name.push(current);
        }

        counter += 1;
    }

    String::from_utf8(sk_table_name).unwrap()
}

#[cfg(test)]
mod tests {
    use crate::pascal_case_to_snake_case;

    #[test]
    fn test_pascal_case_to_snake_case() {
        let table_1 = String::from("Test");

        let new_table_1 = pascal_case_to_snake_case(&table_1);

        assert_eq!(new_table_1, String::from("test"));

        let table_2 = String::from("TestHello");

        let new_table_2 = pascal_case_to_snake_case(&table_2);

        assert_eq!(new_table_2, String::from("test_hello"));
    }
}