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
//! Deserialize postgres rows into a Rust data structure.
use serde::de::{
    self,
    Deserialize,
    Visitor,
    IntoDeserializer,
    value::SeqDeserializer
};

use postgres::rows::{Row, Rows};

use error::{Error, Result};

/// A structure that deserialize Postgres rows into Rust values.
pub struct Deserializer<'a> {
    input: Row<'a>,
    index: usize,
}

impl<'a> Deserializer<'a> {
    /// Create a `Row` deserializer from a `Row`.
    pub fn from_row(input: Row<'a>) -> Self {
        Self { index: 0, input }
    }
}

/// Attempt to deserialize from a single `Row`.
pub fn from_row<'a, T: Deserialize<'a>>(input: Row) -> Result<T> {
    let mut deserializer = Deserializer::from_row(input);
    Ok(T::deserialize(&mut deserializer)?)
}

/// Attempt to deserialize from `Rows`.
pub fn from_rows<'a, T: Deserialize<'a>>(input: &'a Rows) -> Result<Vec<T>> {
    input.into_iter().map(|row| {
        let mut deserializer = Deserializer::from_row(row);
        T::deserialize(&mut deserializer)
    }).collect()
}

macro_rules! unsupported_type {
    ($($fn_name:ident),*,) => {
        $(
            fn $fn_name<V: Visitor<'de>>(self, _: V) -> Result<V::Value> {
                Err(Error::UnsupportedType)
            }
        )*
    }
}

macro_rules! get_value {
    ($this:ident, $v:ident, $fn_call:ident, $ty:ty) => {{
        $v.$fn_call($this.input.get_opt::<_, $ty>($this.index)
            .unwrap()
            .map_err(|_| Error::InvalidType)?)
    }}
}

impl<'de, 'a, 'b> de::Deserializer<'de> for &'b mut Deserializer<'a> {
    type Error = Error;

    unsupported_type! {
        deserialize_any,
        deserialize_u8,
        deserialize_u16,
        deserialize_u64,
        deserialize_char,
        deserialize_str,
        deserialize_bytes,
        deserialize_unit,
        deserialize_map,
        deserialize_identifier,
        deserialize_ignored_any,
    }

    fn deserialize_bool<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        get_value!(self, visitor, visit_bool, bool)
    }

    fn deserialize_i8<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        get_value!(self, visitor, visit_i8, i8)
    }

    fn deserialize_i16<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        get_value!(self, visitor, visit_i16, i16)
    }

    fn deserialize_i32<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        get_value!(self, visitor, visit_i32, i32)
    }

    fn deserialize_i64<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        get_value!(self, visitor, visit_i64, i64)
    }

    fn deserialize_u32<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        get_value!(self, visitor, visit_u32, u32)
    }

    fn deserialize_f32<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        get_value!(self, visitor, visit_f32, f32)
    }

    fn deserialize_f64<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        get_value!(self, visitor, visit_f64, f64)
    }

    fn deserialize_string<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        get_value!(self, visitor, visit_string, String)
    }

    fn deserialize_byte_buf<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        get_value!(self, visitor, visit_byte_buf, Vec<u8>)
    }

    fn deserialize_option<V: Visitor<'de>>(self, visitor: V)
        -> Result<V::Value>
    {

        if self.input.get_bytes(self.index).is_some() {
            visitor.visit_some(self)
        } else {
            visitor.visit_none()
        }
    }

    fn deserialize_seq<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        let raw = self.input.get_opt::<_, Vec<u8>>(self.index)
            .unwrap()
            .map_err(|_| Error::InvalidType)?;

        visitor.visit_seq(SeqDeserializer::new(raw.into_iter()))
    }


    fn deserialize_enum<V: Visitor<'de>>(self,
                                         _: &str,
                                         _: &[&str],
                                         _visitor: V)
        -> Result<V::Value>
    {
        //visitor.visit_enum(self)
        Err(Error::UnsupportedType)
    }

    fn deserialize_unit_struct<V: Visitor<'de>>(self, _: &str, _: V)
        -> Result<V::Value>
    {
        Err(Error::UnsupportedType)
    }

    fn deserialize_newtype_struct<V: Visitor<'de>>(self, _: &str, _: V)
        -> Result<V::Value>
    {
        Err(Error::UnsupportedType)
    }

    fn deserialize_tuple<V: Visitor<'de>>(self, _: usize, _: V)
        -> Result<V::Value>
    {
        Err(Error::UnsupportedType)
    }

    fn deserialize_tuple_struct<V: Visitor<'de>>(self,
                                                 _: &str,
                                                 _: usize,
                                                 _: V)
        -> Result<V::Value>
    {
        Err(Error::UnsupportedType)
    }

    fn deserialize_struct<V: Visitor<'de>>(self, _name: &'static str, _fields: &'static [&'static str], visitor: V) -> Result<V::Value> {
        visitor.visit_map(self)
    }
}

impl<'de, 'a> de::MapAccess<'de> for Deserializer<'a> {
    type Error = Error;

    fn next_key_seed<T: de::DeserializeSeed<'de>>(&mut self, seed: T)
        -> Result<Option<T::Value>>
    {
        if self.index >= self.input.columns().len() {
            return Ok(None)
        }

        self.input.columns()
            .get(self.index)
            .ok_or(Error::UnknownField)
            .map(|c| c.name().to_owned().into_deserializer())
            .and_then(|n| seed.deserialize(n).map(Some))

    }

    fn next_value_seed<T: de::DeserializeSeed<'de>>(&mut self, seed: T)
        -> Result<T::Value>
    {
        let result = seed.deserialize(&mut *self);
        self.index += 1;
        result
    }
}

/*
impl<'de, 'a, 'b> de::EnumAccess<'de> for &'b mut Deserializer<'a> {
    type Error = Error;
    type Variant = Self;

    fn variant_seed<V: de::DeserializeSeed<'de>>(self, seed: V)
        -> Result<(V::Value, Self::Variant)>
    {
        let value = seed.deserialize(self);
    }
}

impl<'de, 'a, 'b> de::VariantAccess<'de> for &'b mut Deserializer<'a> {
    type Error = Error;

    fn unit_variant(self) -> Result<()> {
        Ok(())
    }

    fn newtype_variant_seed<T: de::DeserializeSeed<'de>>(self, seed: T)
        -> Result<T::Value>
    {
        self.input.get_opt::<_, T::Value>(self.index)
            .unwrap()
            .map_err(|_| Error::InvalidType)
    }

    fn tuple_variant<V: Visitor<'de>>(self, _: usize, _: V)
        -> Result<V::Value>
    {
        unimplemented!("tuple_variant")
    }

    fn struct_variant<V: Visitor<'de>>(self, _: &[&str], _: V)
        -> Result<V::Value>
    {
        unimplemented!("struct_variant")
    }
}
*/

#[cfg(test)]
mod tests {
    use std::env;

    use serde_derive::Deserialize;

    use postgres::Connection;

    fn setup_and_connect_to_db() -> Connection {
        let user = env::var("PGUSER").unwrap_or("postgres".into());
        let pass = env::var("PGPASSWORD").map(|p| format!("{}", p)).unwrap_or("postgres".into());
        let addr = env::var("PGADDR").unwrap_or("localhost".into());
        let port = env::var("PGPORT").unwrap_or("5432".into());
        let url = format!("postgres://{user}:{pass}@{addr}:{port}", user = user, pass = pass, addr = addr, port = port);
        Connection::connect(url, postgres::TlsMode::None).unwrap()
    }

    #[test]
    fn non_null() {
        #[derive(Debug, Deserialize, PartialEq)]
        struct Buu {
            wants_candy: bool,
            width: i16,
            amount_eaten: i32,
            amount_want_to_eat: i64,
            speed: f32,
            weight: f64,
            catchphrase: String,
            stomach_contents: Vec<u8>,
        }

        let connection = setup_and_connect_to_db();

        connection.execute("CREATE TABLE IF NOT EXISTS Buu (
                    wants_candy BOOL NOT NULL,
                    width SMALLINT NOT NULL,
                    amount_eaten INT NOT NULL,
                    amount_want_to_eat BIGINT NOT NULL,
                    speed REAL NOT NULL,
                    weight DOUBLE PRECISION NOT NULL,
                    catchphrase VARCHAR NOT NULL,
                    stomach_contents BYTEA NOT NULL
        )", &[]).unwrap();

        connection.execute("INSERT INTO Buu (
            wants_candy,
            width,
            amount_eaten,
            amount_want_to_eat,
            speed,
            weight,
            catchphrase,
            stomach_contents
        ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)",
        &[&true, &20i16, &1000i32, &1000_000i64, &99.99f32, &9999.9999f64, &String::from("Woo Woo"), &vec![1u8, 2, 3, 4, 5, 6]]).unwrap();

        let results = connection.query("SELECT wants_candy,
            width,
            amount_eaten,
            amount_want_to_eat,
            speed,
            weight,
            catchphrase,
            stomach_contents
 FROM Buu", &[]).unwrap();

        let row = results.get(0);

        let buu: Buu = super::from_row(row).unwrap();

        assert_eq!(true, buu.wants_candy);
        assert_eq!(20, buu.width);
        assert_eq!(1000, buu.amount_eaten);
        assert_eq!(1000_000, buu.amount_want_to_eat);
        assert_eq!(99.99, buu.speed);
        assert_eq!(9999.9999, buu.weight);
        assert_eq!("Woo Woo", buu.catchphrase);
        assert_eq!(vec![1,2,3,4,5,6], buu.stomach_contents);

        connection.execute("DROP TABLE Buu", &[]).unwrap();
    }

    #[test]
    fn nullable() {
        #[derive(Debug, Deserialize, PartialEq)]
        struct Buu {
            wants_candy: Option<bool>,
            width: Option<i16>,
            amount_eaten: Option<i32>,
            amount_want_to_eat: Option<i64>,
            speed: Option<f32>,
            weight: Option<f64>,
            catchphrase: Option<String>,
            stomach_contents: Option<Vec<u8>>,
        }

        let connection = setup_and_connect_to_db();

        connection.execute("CREATE TABLE IF NOT EXISTS NullBuu (
                    wants_candy BOOL,
                    width SMALLINT,
                    amount_eaten INT,
                    amount_want_to_eat BIGINT,
                    speed REAL,
                    weight DOUBLE PRECISION,
                    catchphrase VARCHAR,
                    stomach_contents BYTEA
        )", &[]).unwrap();

        connection.execute("INSERT INTO NullBuu (
            wants_candy,
            width,
            amount_eaten,
            amount_want_to_eat,
            speed,
            weight,
            catchphrase,
            stomach_contents
        ) VALUES (
            NULL,
            NULL,
            NULL,
            NULL,
            NULL,
            NULL,
            NULL,
            NULL)",
        &[]).unwrap();

        let results = connection.query("SELECT wants_candy,
            width,
            amount_eaten,
            amount_want_to_eat,
            speed,
            weight,
            catchphrase,
            stomach_contents
 FROM NullBuu", &[]).unwrap();

        let row = results.get(0);

        let buu: Buu = super::from_row(row).unwrap();

        assert_eq!(None, buu.wants_candy);
        assert_eq!(None, buu.width);
        assert_eq!(None, buu.amount_eaten);
        assert_eq!(None, buu.amount_want_to_eat);
        assert_eq!(None, buu.speed);
        assert_eq!(None, buu.weight);
        assert_eq!(None, buu.catchphrase);
        assert_eq!(None, buu.stomach_contents);

        connection.execute("DROP TABLE NullBuu", &[]).unwrap();
    }

    /*
    use postgres_derive::FromSql;
    #[test]
    fn enums() {
        #[derive(Debug, Deserialize, PartialEq)]
        struct Goku {
            hair: HairColour,
        }

        #[derive(Debug, Deserialize, FromSql, PartialEq)]
        #[postgres(name = "hair_colour")]
        enum HairColour {
            #[postgres(name = "black")]
            Black,
            #[postgres(name = "yellow")]
            Yellow,
            #[postgres(name = "blue")]
            Blue,
        }

        let connection = setup_and_connect_to_db();

        connection.execute("CREATE TYPE hair_colour as ENUM (
            'black',
            'yellow',
            'blue'
        )", &[]).unwrap();

        connection.execute("CREATE TABLE Gokus (hair hair_colour)",
        &[]).unwrap();

        connection.execute("INSERT INTO Gokus VALUES ('black')", &[])
            .unwrap();

        let results = connection.query("SELECT * FROM Gokus", &[])
            .unwrap();

        let row = results.get(0);

        let goku: Goku = super::from_row(row).unwrap();

        assert_eq!(HairColour::Black, goku.hair);

        connection.execute("DROP TABLE Gokus", &[]).unwrap();
        connection.execute("DROP TYPE hair_colour", &[]).unwrap();
    }
    */
}