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
mod date_and_time;

use std::io::prelude::*;
use std::error::Error;

use super::Sqlite;
use super::connection::SqliteValue;
use types::{self, FromSql, ToSql, IsNull};

impl FromSql<types::VarChar, Sqlite> for String {
    fn from_sql(value: Option<&SqliteValue>) -> Result<Self, Box<Error+Send+Sync>> {
        let text = not_none!(value).read_text();
        Ok(text.into())
    }
}

impl FromSql<types::Binary, Sqlite> for Vec<u8> {
    fn from_sql(bytes: Option<&SqliteValue>) -> Result<Self, Box<Error+Send+Sync>> {
        let bytes = not_none!(bytes).read_blob();
        Ok(bytes.into())
    }
}

impl FromSql<types::SmallInt, Sqlite> for i16 {
    fn from_sql(value: Option<&SqliteValue>) -> Result<Self, Box<Error+Send+Sync>> {
        Ok(not_none!(value).read_integer() as i16)
    }
}

impl FromSql<types::Integer, Sqlite> for i32 {
    fn from_sql(value: Option<&SqliteValue>) -> Result<Self, Box<Error+Send+Sync>> {
        Ok(not_none!(value).read_integer())
    }
}

impl FromSql<types::Bool, Sqlite> for bool {
    fn from_sql(value: Option<&SqliteValue>) -> Result<Self, Box<Error+Send+Sync>> {
        Ok(not_none!(value).read_integer() != 0)
    }
}

impl FromSql<types::BigInt, Sqlite> for i64 {
    fn from_sql(value: Option<&SqliteValue>) -> Result<Self, Box<Error+Send+Sync>> {
        Ok(not_none!(value).read_long())
    }
}

impl FromSql<types::Float, Sqlite> for f32 {
    fn from_sql(value: Option<&SqliteValue>) -> Result<Self, Box<Error+Send+Sync>> {
        Ok(not_none!(value).read_double() as f32)
    }
}

impl FromSql<types::Double, Sqlite> for f64 {
    fn from_sql(value: Option<&SqliteValue>) -> Result<Self, Box<Error+Send+Sync>> {
        Ok(not_none!(value).read_double())
    }
}

impl ToSql<types::Bool, Sqlite> for bool {
    fn to_sql<W: Write>(&self, out: &mut W) -> Result<IsNull, Box<Error+Send+Sync>> {
        let int_value = if *self {
            1
        } else {
            0
        };
        <i32 as ToSql<types::Integer, Sqlite>>::to_sql(&int_value, out)
    }
}