zendb/
create.rs

1use std::marker::PhantomData;
2
3use postgres::{types::ToSql, Client};
4
5use crate::{
6    Create, CreateBuilder, KeyNotPresent, KeyPresent, TableNotPresent, TablePresent,
7    ValueNotPresent, ValuePresent,
8};
9
10impl<'create> CreateBuilder<'create, TableNotPresent, KeyNotPresent, ValueNotPresent> {
11    pub fn new(conn: &'create mut Client) -> Self {
12        Self {
13            conn: Some(conn),
14            table: Vec::new(),
15            key: Vec::new(),
16            value: Vec::new(),
17            table_state: std::marker::PhantomData,
18            key_state: std::marker::PhantomData,
19            value_state: std::marker::PhantomData,
20        }
21    }
22    pub fn table(
23        mut self,
24        table: &'create str,
25    ) -> CreateBuilder<TablePresent, KeyNotPresent, ValueNotPresent> {
26        self.table.push(table.to_string());
27        CreateBuilder {
28            conn: self.conn,
29            table: self.table,
30            key: self.key,
31            value: self.value,
32            table_state: PhantomData,
33            key_state: PhantomData,
34            value_state: PhantomData,
35        }
36    }
37}
38
39impl<'create> CreateBuilder<'create, TablePresent, KeyNotPresent, ValueNotPresent> {
40    pub fn key(
41        mut self,
42        key: &'create [&str],
43    ) -> CreateBuilder<'create, TablePresent, KeyPresent, ValueNotPresent> {
44        self.key.push(key);
45        CreateBuilder {
46            conn: self.conn,
47            table: self.table,
48            key: self.key,
49            value: self.value,
50            table_state: std::marker::PhantomData,
51            key_state: std::marker::PhantomData,
52            value_state: std::marker::PhantomData,
53        }
54    }
55}
56
57impl<'create> CreateBuilder<'create, TablePresent, KeyPresent, ValueNotPresent> {
58    pub fn value(
59        mut self,
60        value: &'create [&'create (dyn ToSql + Sync)],
61    ) -> CreateBuilder<TablePresent, KeyPresent, ValuePresent> {
62        self.value.push(value);
63        CreateBuilder {
64            conn: self.conn,
65            table: self.table,
66            key: self.key,
67            value: self.value,
68            table_state: std::marker::PhantomData,
69            key_state: std::marker::PhantomData,
70            value_state: std::marker::PhantomData,
71        }
72    }
73}
74
75impl<'create> CreateBuilder<'create, TablePresent, KeyPresent, ValuePresent> {
76    pub fn confirm(self) -> Create<'create> {
77        Create {
78            conn: self.conn.unwrap(),
79            key: self.key,
80            table: self.table,
81            value: self.value,
82        }
83    }
84}
85
86impl<'create> Create<'create> {
87    pub fn build(mut self) -> Result<u64, postgres::Error> {
88        let mut connection = &mut self.conn;
89        let keys = self.key;
90        let value = self.value;
91        let tables = &self.table;
92
93        let mut keys_ = String::new();
94        let mut id = String::new();
95        let mut table_ = String::new();
96
97        // for value in value {
98        //     values.push_str(value.to_vec().iter());
99        // }
100
101        for key in keys.iter() {
102            for (index, key) in key.iter().enumerate() {
103                let keys = format!("{},", key);
104                keys_.push_str(&keys);
105                // println!("{:?}", key);
106                // println!("{:?}", value[index]);
107                let key_id = format!("${},", index + 1);
108                id.push_str(&key_id);
109            }
110        }
111        for table in tables {
112            table_.push_str(&table);
113        }
114        let keys = keys_.trim_end_matches(",");
115        let id = id.trim_end_matches(",");
116
117        let create = format!("INSERT INTO {} ({}) VALUES ({})", table_, keys, id);
118
119        // creating
120        let mut values: &[&(dyn ToSql + Sync)] = &Vec::new();
121        for value in value {
122            values = value
123        }
124
125        let create = connection.execute(&create, &values);
126        match create {
127            Ok(success) => Ok(success),
128            Err(err) => Err(err),
129        }
130    }
131}