rustorm 0.10.8

A complete rewrite of rustorm
Documentation
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
use rustorm_dao::TableName;
use rustorm_dao::ToColumnNames;
use rustorm_dao::ToTableName;
use rustorm_dao::{FromDao, ToDao};
use rustorm_dao::{Value};
use database::Database;
use error::{DataError, DbError};
use platform::DBPlatform;
use table::SchemaContent;
use table::Table;
use users::User;
use users::Role;
use database::DatabaseName;
use rustorm_dao::ToValue;

pub struct EntityManager(pub DBPlatform);

impl EntityManager {

    pub fn set_session_user(&self, username: &str) -> Result<(), DbError> {
        //let sql = format!("SET SESSION AUTHORIZATION '{}'", username);
        let sql = format!("SET SESSION ROLE '{}'", username);
        self.0.execute_sql_with_return(&sql, &[])?;
        Ok(())
    }
    pub fn get_role(&self, username: &str) -> Result<Option<Role>, DbError> {
        let result = self.0.get_roles(&self, username);
        match result {
            Ok(mut result) => match result.len() {
                0 => Ok(None),
                1 => Ok(Some(result.remove(0))),
                _ => Err(DbError::DataError(DataError::MoreThan1RecordReturned)),
            },
            Err(e) => Err(e),
        }
    }
    pub fn db(&self) -> &Database {
        &*self.0
    }
    /// get all the records of this table
    pub fn get_all<T>(&self) -> Result<Vec<T>, DbError>
    where
        T: ToTableName + ToColumnNames + FromDao,
    {
        let table = T::to_table_name();
        let columns = T::to_column_names();
        let enumerated_columns = columns
            .iter()
            .map(|c| c.name.to_owned())
            .collect::<Vec<_>>()
            .join(", ");
        let sql = format!(
            "SELECT {} FROM {}",
            enumerated_columns,
            table.complete_name()
        );
        let rows = self.0.execute_sql_with_return(&sql, &[])?;
        let mut entities = vec![];
        for dao in rows.iter() {
            let entity = T::from_dao(&dao);
            entities.push(entity)
        }
        Ok(entities)
    }

    /// get the table from database based on this column name
    pub fn get_table(&self, table_name: &TableName) -> Result<Table, DbError> {
        self.0.get_table(self, table_name)
    }

    /// get all the user table and views from the database
    pub fn get_all_tables(&self) -> Result<Vec<Table>, DbError> {
        info!("EXPENSIVE DB OPERATION: get_all_tables");
        self.0.get_all_tables(self)
    }

    pub fn get_users(&self) -> Result<Vec<User>, DbError> {
        self.0.get_users(self)
    }

    pub fn get_database_name(&self) -> Result<Option<DatabaseName>, DbError> {
        self.0.get_database_name(self)
    }

    /// get all table and views grouped per schema
    pub fn get_grouped_tables(&self) -> Result<Vec<SchemaContent>, DbError> {
        self.0.get_grouped_tables(self)
    }

    /// insert to table the values of this struct
    /// TODO: sqlite3 doesn't support the RETURNING keyword
    pub fn insert<T, R>(&self, entities: &[&T]) -> Result<Vec<R>, DbError>
    where
        T: ToTableName + ToColumnNames + ToDao,
        R: FromDao + ToColumnNames,
    {
        let table = T::to_table_name();
        let columns = T::to_column_names();
        let columns_len = columns.len();
        let mut sql = String::new();
        sql += &format!("INSERT INTO {} ", table.complete_name());
        sql += &format!(
            "({})\n",
            columns
                .iter()
                .map(|c| c.name.to_owned())
                .collect::<Vec<_>>()
                .join(", ")
        );
        sql += "VALUES ";
        sql += &entities
            .iter()
            .enumerate()
            .map(|(y, _)| {
                format!(
                    "\n\t({})",
                    columns
                        .iter()
                        .enumerate()
                        .map(|(x, _)| format!("${}", y * columns_len + x + 1))
                        .collect::<Vec<_>>()
                        .join(", ")
                )
            })
            .collect::<Vec<_>>()
            .join(", ");
        let return_columns = R::to_column_names();
        sql += &format!(
            "\nRETURNING \n{}",
            return_columns
                .iter()
                .map(|rc| rc.name.to_owned())
                .collect::<Vec<_>>()
                .join(", ")
        );

        let mut values: Vec<Value> = Vec::with_capacity(entities.len() * columns.len());
        for entity in entities {
            let mut dao = entity.to_dao();
            for col in columns.iter() {
                let value = dao.get_value(&col.name);
                match value {
                    Some(value) => values.push(value.clone()),
                    None => values.push(Value::Nil),
                }
            }
        }
        let bvalues:Vec<&Value> = values.iter().collect();
        let rows = self.0.execute_sql_with_return(&sql, &bvalues)?;
        let mut retrieved_entities = vec![];
        for dao in rows.iter() {
            let retrieved = R::from_dao(&dao);
            retrieved_entities.push(retrieved);
        }
        Ok(retrieved_entities)
    }

    pub fn execute_sql_with_return<'a, R>(
        &self,
        sql: &str,
        params: &[&'a ToValue],
    ) -> Result<Vec<R>, DbError>
    where
        R: FromDao,
    {
        let values:Vec<Value> = params.iter().map(|p|p.to_value()).collect();
        let bvalues: Vec<&Value> = values.iter().collect();
        let rows = self.0.execute_sql_with_return(sql, &bvalues)?;
        Ok(rows.iter().map(|dao| R::from_dao(&dao)).collect::<Vec<R>>())
    }

    pub fn execute_sql_with_one_return<'a, R>(
        &self,
        sql: &str,
        params: &[&'a ToValue],
    ) -> Result<R, DbError>
    where
        R: FromDao,
    {
        let result: Result<Vec<R>, DbError> = self.execute_sql_with_return(sql, &params);
        match result {
            Ok(mut result) => match result.len() {
                0 => Err(DbError::DataError(DataError::ZeroRecordReturned)),
                1 => Ok(result.remove(0)),
                _ => Err(DbError::DataError(DataError::MoreThan1RecordReturned)),
            },
            Err(e) => Err(e),
        }
    }

    pub fn execute_sql_with_maybe_one_return<'a, R>(
        &self,
        sql: &str,
        params: &[&'a ToValue],
    ) -> Result<Option<R>, DbError>
    where
        R: FromDao,
    {
        let result: Result<Vec<R>, DbError> = self.execute_sql_with_return(sql, &params);
        match result {
            Ok(mut result) => match result.len() {
                0 => Ok(None),
                1 => Ok(Some(result.remove(0))),
                _ => Err(DbError::DataError(DataError::MoreThan1RecordReturned)),
            },
            Err(e) => Err(e),
        }
    }
}

#[cfg(test)]
#[cfg(feature = "with-postgres")]
mod test_pg {
    extern crate rustorm_dao;
    use super::*;
    use chrono::offset::Utc;
    use chrono::{DateTime, NaiveDate};
    use rustorm_dao::TableName;
    use rustorm_dao::ToColumnNames;
    use rustorm_dao::ToTableName;
    use rustorm_dao::{FromDao, ToDao};
    use pool::Pool;
    use uuid::Uuid;

    #[test]
    fn use_em() {
        #[derive(Debug, FromDao, ToColumnNames, ToTableName)]
        struct Actor {
            actor_id: i32,
            first_name: String,
        }
        let db_url = "postgres://postgres:p0stgr3s@localhost/sakila";
        let mut pool = Pool::new();
        let em = pool.em(db_url).unwrap();
        let actors: Result<Vec<Actor>, DbError> = em.get_all();
        info!("Actor: {:#?}", actors);
        let actors = actors.unwrap();
        for actor in actors {
            info!("actor: {:?}", actor);
        }
    }

    #[test]
    fn various_data_types() {
        let db_url = "postgres://postgres:p0stgr3s@localhost/sakila";
        let mut pool = Pool::new();
        let em = pool.em(db_url).unwrap();
        #[derive(Debug, PartialEq, FromDao, ToDao, ToColumnNames, ToTableName)]
        struct Sample {
            vnil: Option<String>,
            vbool: bool,
            vsmallint: i16,
            vint: i32,
            vbigint: i64,
            vfloat: f32,
            vdouble: f64,
            vblob: Vec<u8>,
            vchar: char,
            vtext: String,
            vuuid: Uuid,
            vdate: NaiveDate,
            vtimestamp: DateTime<Utc>,
        }

        let sample: Result<Vec<Sample>, DbError> = em.execute_sql_with_return(
            r#"
            SELECT NULL::TEXT as vnil,
                    true::BOOL as vbool,
                    1000::INT2 as vsmallint,
                    32000::INT as vint,
                    123000::INT4 as vbigint,
                    1.0::FLOAT4 as vfloat,
                    2.0::FLOAT8 as vdouble,
                    E'\\000'::BYTEA as vblob,
                    'c'::CHAR as vchar,
                    'Hello'::TEXT as vtext,
                    'd25af116-fb30-4731-9cf9-2251c235e8fa'::UUID as vuuid,
                    now()::DATE as vdate,
                    now()::TIMESTAMP WITH TIME ZONE as vtimestamp

        "#,
            &[],
        );
        info!("{:#?}", sample);
        assert!(sample.is_ok());

        let sample = sample.unwrap();
        let sample = &sample[0];
        let now = Utc::now();
        let today = now.date();
        let naive_today = today.naive_utc();

        assert_eq!(None, sample.vnil);
        assert_eq!(true, sample.vbool);
        assert_eq!(1000, sample.vsmallint);
        assert_eq!(32000, sample.vint);
        assert_eq!(123000, sample.vbigint);
        assert_eq!(1.0, sample.vfloat);
        assert_eq!(2.0, sample.vdouble);
        assert_eq!(vec![0], sample.vblob);
        assert_eq!('c', sample.vchar);
        assert_eq!("Hello".to_string(), sample.vtext);
        assert_eq!(naive_today, sample.vdate);
        assert_eq!(today, sample.vtimestamp.date());
    }

    #[test]
    fn various_data_types_nulls() {
        let db_url = "postgres://postgres:p0stgr3s@localhost/sakila";
        let mut pool = Pool::new();
        let em = pool.em(db_url).unwrap();
        #[derive(Debug, PartialEq, FromDao, ToDao, ToColumnNames, ToTableName)]
        struct Sample {
            vnil: Option<String>,
            vbool: Option<bool>,
            vsmallint: Option<i16>,
            vint: Option<i32>,
            vbigint: Option<i64>,
            vfloat: Option<f32>,
            vdouble: Option<f64>,
            vblob: Option<Vec<u8>>,
            vchar: Option<char>,
            vtext: Option<String>,
            vuuid: Option<Uuid>,
            vdate: Option<NaiveDate>,
            vtimestamp: Option<DateTime<Utc>>,
        }

        let sample: Result<Vec<Sample>, DbError> = em.execute_sql_with_return(
            r#"
            SELECT NULL::TEXT as vnil,
                    NULL::BOOL as vbool,
                    NULL::INT2 as vsmallint,
                    NULL::INT as vint,
                    NULL::INT4 as vbigint,
                    NULL::FLOAT4 as vfloat,
                    NULL::FLOAT8 as vdouble,
                    NULL::BYTEA as vblob,
                    NULL::CHAR as vchar,
                    NULL::TEXT as vtext,
                    NULL::UUID as vuuid,
                    NULL::DATE as vdate,
                    NULL::TIMESTAMP WITH TIME ZONE as vtimestamp

        "#,
            &[],
        );
        info!("{:#?}", sample);
        assert!(sample.is_ok());

        let sample = sample.unwrap();
        let sample = &sample[0];

        assert_eq!(None, sample.vnil);
        assert_eq!(None, sample.vbool);
        assert_eq!(None, sample.vsmallint);
        assert_eq!(None, sample.vint);
        assert_eq!(None, sample.vbigint);
        assert_eq!(None, sample.vfloat);
        assert_eq!(None, sample.vdouble);
        assert_eq!(None, sample.vblob);
        assert_eq!(None, sample.vtext);
        assert_eq!(None, sample.vdate);
        assert_eq!(None, sample.vtimestamp);
    }

    #[test]
    fn edgecase_use_char_as_string() {
        let db_url = "postgres://postgres:p0stgr3s@localhost/sakila";
        let mut pool = Pool::new();
        let em = pool.em(db_url).unwrap();
        #[derive(Debug, PartialEq, FromDao, ToDao, ToColumnNames, ToTableName)]
        struct Sample {
            vchar: String,
        }

        let sample: Result<Vec<Sample>, DbError> = em.execute_sql_with_return(
            r#"
            SELECT
                'c'::CHAR as VCHAR
        "#,
            &[],
        );
        info!("{:#?}", sample);
        assert!(sample.is_ok());

        let sample = sample.unwrap();
        let sample = &sample[0];
        assert_eq!("c".to_string(), sample.vchar);
    }

    #[test]
    fn char1() {
        let db_url = "postgres://postgres:p0stgr3s@localhost/sakila";
        let mut pool = Pool::new();
        let em = pool.em(db_url).unwrap();
        #[derive(Debug, PartialEq, FromDao, ToDao, ToColumnNames, ToTableName)]
        struct Sample {
            vchar: char,
        }

        let sample: Result<Vec<Sample>, DbError> = em.execute_sql_with_return(
            r#"
            SELECT
                'c'::CHAR as VCHAR
        "#,
            &[],
        );
        info!("{:#?}", sample);
        assert!(sample.is_ok());

        let sample = sample.unwrap();
        let sample = &sample[0];
        assert_eq!('c', sample.vchar);
    }

    #[test]
    fn insert_some_data() {
        #[derive(Debug, PartialEq, FromDao, ToDao, ToColumnNames, ToTableName)]
        struct Actor {
            first_name: String,
            last_name: String,
        }
        let db_url = "postgres://postgres:p0stgr3s@localhost/sakila";
        let mut pool = Pool::new();
        let em = pool.em(db_url).unwrap();
        let tom_cruise = Actor {
            first_name: "TOM".into(),
            last_name: "CRUISE".to_string(),
        };
        let tom_hanks = Actor {
            first_name: "TOM".into(),
            last_name: "HANKS".to_string(),
        };

        let actors: Result<Vec<Actor>, DbError> = em.insert(&[&tom_cruise, &tom_hanks]);
        info!("Actor: {:#?}", actors);
        assert!(actors.is_ok());
        let actors = actors.unwrap();
        assert_eq!(tom_cruise, actors[0]);
        assert_eq!(tom_hanks, actors[1]);
    }

    #[test]
    fn insert_some_data_with_different_retrieve() {
        mod for_insert {
            use super::*;
            #[derive(Debug, PartialEq, ToDao, ToColumnNames, ToTableName)]
            pub struct Actor {
                pub first_name: String,
                pub last_name: String,
            }
        }

        mod for_retrieve {
            use super::*;
            #[derive(Debug, FromDao, ToColumnNames, ToTableName)]
            pub struct Actor {
                pub actor_id: i32,
                pub first_name: String,
                pub last_name: String,
                pub last_update: DateTime<Utc>,
            }
        }

        let db_url = "postgres://postgres:p0stgr3s@localhost/sakila";
        let mut pool = Pool::new();
        let em = pool.em(db_url).unwrap();
        let tom_cruise = for_insert::Actor {
            first_name: "TOM".into(),
            last_name: "CRUISE".to_string(),
        };
        let tom_hanks = for_insert::Actor {
            first_name: "TOM".into(),
            last_name: "HANKS".to_string(),
        };

        let actors: Result<Vec<for_retrieve::Actor>, DbError> =
            em.insert(&[&tom_cruise, &tom_hanks]);
        info!("Actor: {:#?}", actors);
        assert!(actors.is_ok());
        let actors = actors.unwrap();
        let today = Utc::now().date();
        assert_eq!(tom_cruise.first_name, actors[0].first_name);
        assert_eq!(tom_cruise.last_name, actors[0].last_name);
        assert_eq!(today, actors[0].last_update.date());
        assert_eq!(tom_hanks.first_name, actors[1].first_name);
        assert_eq!(tom_hanks.last_name, actors[1].last_name);
        assert_eq!(today, actors[1].last_update.date());
    }

    #[test]
    fn execute_sql_non_existing_table() {
        #[derive(Debug, FromDao)]
        struct Event {
            id: i32,
            name: String,
            created: DateTime<Utc>,
        }
        let db_url = "postgres://postgres:p0stgr3s@localhost/sakila";
        let mut pool = Pool::new();
        let em = pool.em(db_url).unwrap();
        let id = 1;
        let name = "dbus-notifications".to_string();
        let created = Utc::now();
        let events: Result<Vec<Event>, DbError> = em.execute_sql_with_return(
            "SELECT $1::INT as id, $2::TEXT as name, $3::TIMESTAMP WITH TIME ZONE as created",
            &[&id, &name, &created],
        );
        info!("events: {:#?}", events);
        assert!(events.is_ok());
        for event in events.unwrap().iter() {
            assert_eq!(event.id, id);
            assert_eq!(event.name, name);
            assert_eq!(event.created.date(), created.date());
        }
    }

    #[test]
    fn get_table() {
        let db_url = "postgres://postgres:p0stgr3s@localhost/sakila";
        let mut pool = Pool::new();
        let em = pool.em(db_url).unwrap();
        let actor = TableName::from("actor");
        let table = em.db().get_table(&em, &actor);
        assert!(table.is_ok());
    }

}