forked from graphprotocol/graph-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_value.rs
More file actions
55 lines (48 loc) · 1.88 KB
/
Copy pathsql_value.rs
File metadata and controls
55 lines (48 loc) · 1.88 KB
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
use diesel::pg::Pg;
use diesel::serialize::{self, Output, ToSql};
use diesel::sql_types::{Bool, Integer, Numeric, Text};
use std::io::Write;
use graph::data::store::Value;
#[derive(Clone, Debug, PartialEq, AsExpression)]
pub struct SqlValue(Value);
impl SqlValue {
pub fn new_array(values: Vec<Value>) -> Vec<Self> {
values.into_iter().map(SqlValue).collect()
}
}
impl ToSql<Bool, Pg> for SqlValue {
fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result {
match self.0 {
Value::Bool(ref b) => <bool as ToSql<Bool, Pg>>::to_sql(&b, out),
_ => panic!("Failed to convert non-boolean attribute value to boolean in SQL"),
}
}
}
impl ToSql<Integer, Pg> for SqlValue {
fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result {
match self.0 {
Value::Int(ref i) => <i32 as ToSql<Integer, Pg>>::to_sql(&i, out),
_ => panic!("Failed to convert non-int attribute value to int in SQL"),
}
}
}
impl ToSql<Numeric, Pg> for SqlValue {
fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result {
match &self.0 {
Value::BigDecimal(d) => <_ as ToSql<Numeric, Pg>>::to_sql(&d, out),
Value::BigInt(number) => {
<_ as ToSql<Numeric, Pg>>::to_sql(&number.clone().to_big_decimal(0.into()), out)
}
_ => panic!("Failed to convert attribute value to bigint in SQL"),
}
}
}
impl ToSql<Text, Pg> for SqlValue {
fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result {
match self.0 {
Value::String(ref s) => <String as ToSql<Text, Pg>>::to_sql(&s, out),
Value::Bytes(ref h) => <String as ToSql<Text, Pg>>::to_sql(&h.to_string(), out),
_ => panic!("Failed to convert attribute value to String or Bytes in SQL"),
}
}
}