Skip to content

Commit 0e3dfb2

Browse files
committed
store, relational: Refactor FromColumnValue
1 parent 49e9f4e commit 0e3dfb2

File tree

1 file changed

+96
-102
lines changed

1 file changed

+96
-102
lines changed

store/postgres/src/relational_queries.rs

Lines changed: 96 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -196,37 +196,37 @@ impl FromEntityData for BTreeMap<String, graphql_parser::query::Value> {
196196
pub trait FromColumnValue: Sized {
197197
fn is_null(&self) -> bool;
198198

199+
fn null() -> Self;
200+
199201
fn from_string(s: String) -> Self;
200202

201-
fn from_column_value(
202-
column_type: &ColumnType,
203-
json: serde_json::Value,
204-
) -> Result<Self, StoreError>;
205-
}
203+
fn from_bool(b: bool) -> Self;
206204

207-
impl FromColumnValue for graphql_parser::query::Value {
208-
fn is_null(&self) -> bool {
209-
self == &graphql_parser::query::Value::Null
210-
}
205+
fn from_i32(i: i32) -> Self;
211206

212-
fn from_string(s: String) -> Self {
213-
graphql_parser::query::Value::String(s)
214-
}
207+
fn from_big_decimal(d: scalar::BigDecimal) -> Self;
208+
209+
fn from_big_int(i: serde_json::Number) -> Result<Self, StoreError>;
210+
211+
// The string returned by the DB, without the leading '\x'
212+
fn from_bytes(i: &str) -> Result<Self, StoreError>;
213+
214+
fn from_vec(v: Vec<Self>) -> Self;
215215

216216
fn from_column_value(
217217
column_type: &ColumnType,
218218
json: serde_json::Value,
219-
) -> Result<graphql_parser::query::Value, StoreError> {
220-
use graphql_parser::query::Value as q;
219+
) -> Result<Self, StoreError> {
220+
//use graphql_parser::query::Value as q;
221221
use serde_json::Value as j;
222222
// Many possible conversion errors are already caught by how
223223
// we define the schema; for example, we can only get a NULL for
224224
// a column that is actually nullable
225225
match (json, column_type) {
226-
(j::Null, _) => Ok(q::Null),
227-
(j::Bool(b), _) => Ok(q::Boolean(b)),
226+
(j::Null, _) => Ok(Self::null()),
227+
(j::Bool(b), _) => Ok(Self::from_bool(b)),
228228
(j::Number(number), ColumnType::Int) => match number.as_i64() {
229-
Some(i) => i32::try_from(i).map(|i| q::Int(i.into())).map_err(|e| {
229+
Some(i) => i32::try_from(i).map(Self::from_i32).map_err(|e| {
230230
StoreError::Unknown(format_err!("failed to convert {} to Int: {}", number, e))
231231
}),
232232
None => Err(StoreError::Unknown(format_err!(
@@ -237,7 +237,7 @@ impl FromColumnValue for graphql_parser::query::Value {
237237
(j::Number(number), ColumnType::BigDecimal) => {
238238
let s = number.to_string();
239239
scalar::BigDecimal::from_str(s.as_str())
240-
.map(|d| q::String(d.to_string()))
240+
.map(Self::from_big_decimal)
241241
.map_err(|e| {
242242
StoreError::Unknown(format_err!(
243243
"failed to convert {} to BigDecimal: {}",
@@ -246,25 +246,23 @@ impl FromColumnValue for graphql_parser::query::Value {
246246
))
247247
})
248248
}
249-
(j::Number(number), ColumnType::BigInt) => Ok(q::String(number.to_string())),
249+
(j::Number(number), ColumnType::BigInt) => Self::from_big_int(number),
250250
(j::Number(number), column_type) => Err(StoreError::Unknown(format_err!(
251251
"can not convert number {} to {:?}",
252252
number,
253253
column_type
254254
))),
255255
(j::String(s), ColumnType::String) | (j::String(s), ColumnType::Enum(_)) => {
256-
Ok(q::String(s))
256+
Ok(Self::from_string(s))
257257
}
258-
(j::String(s), ColumnType::Bytes) => {
259-
Ok(q::String(format!("0x{}", s.trim_start_matches("\\x"))))
260-
}
261-
(j::String(s), ColumnType::BytesId) => Ok(q::String(bytes_as_str(&s))),
258+
(j::String(s), ColumnType::Bytes) => Self::from_bytes(s.trim_start_matches("\\x")),
259+
(j::String(s), ColumnType::BytesId) => Ok(Self::from_string(bytes_as_str(&s))),
262260
(j::String(s), column_type) => Err(StoreError::Unknown(format_err!(
263261
"can not convert string {} to {:?}",
264262
s,
265263
column_type
266264
))),
267-
(j::Array(values), _) => Ok(q::List(
265+
(j::Array(values), _) => Ok(Self::from_vec(
268266
values
269267
.into_iter()
270268
.map(|v| Self::from_column_value(column_type, v))
@@ -277,91 +275,87 @@ impl FromColumnValue for graphql_parser::query::Value {
277275
}
278276
}
279277

278+
impl FromColumnValue for graphql_parser::query::Value {
279+
fn is_null(&self) -> bool {
280+
self == &graphql_parser::query::Value::Null
281+
}
282+
283+
fn null() -> Self {
284+
Self::Null
285+
}
286+
287+
fn from_string(s: String) -> Self {
288+
graphql_parser::query::Value::String(s)
289+
}
290+
291+
fn from_bool(b: bool) -> Self {
292+
graphql_parser::query::Value::Boolean(b)
293+
}
294+
295+
fn from_i32(i: i32) -> Self {
296+
graphql_parser::query::Value::Int(i.into())
297+
}
298+
299+
fn from_big_decimal(d: scalar::BigDecimal) -> Self {
300+
graphql_parser::query::Value::String(d.to_string())
301+
}
302+
303+
fn from_big_int(i: serde_json::Number) -> Result<Self, StoreError> {
304+
Ok(graphql_parser::query::Value::String(i.to_string()))
305+
}
306+
307+
fn from_bytes(b: &str) -> Result<Self, StoreError> {
308+
Ok(graphql_parser::query::Value::String(format!("0x{}", b)))
309+
}
310+
311+
fn from_vec(v: Vec<Self>) -> Self {
312+
graphql_parser::query::Value::List(v)
313+
}
314+
}
315+
280316
impl FromColumnValue for graph::prelude::Value {
281317
fn is_null(&self) -> bool {
282318
self == &Value::Null
283319
}
284320

321+
fn null() -> Self {
322+
Self::Null
323+
}
324+
285325
fn from_string(s: String) -> Self {
286-
Value::String(s)
326+
graph::prelude::Value::String(s)
287327
}
288328

289-
fn from_column_value(
290-
column_type: &ColumnType,
291-
json: serde_json::Value,
292-
) -> Result<graph::prelude::Value, StoreError> {
293-
use graph::prelude::Value as g;
294-
use serde_json::Value as j;
295-
// Many possible conversion errors are already caught by how
296-
// we define the schema; for example, we can only get a NULL for
297-
// a column that is actually nullable
298-
match (json, column_type) {
299-
(j::Null, _) => Ok(g::Null),
300-
(j::Bool(b), _) => Ok(g::Bool(b)),
301-
(j::Number(number), ColumnType::Int) => match number.as_i64() {
302-
Some(i) => i32::try_from(i).map(|i| g::Int(i)).map_err(|e| {
303-
StoreError::Unknown(format_err!("failed to convert {} to Int: {}", number, e))
304-
}),
305-
None => Err(StoreError::Unknown(format_err!(
306-
"failed to convert {} to Int",
307-
number
308-
))),
309-
},
310-
(j::Number(number), ColumnType::BigDecimal) => {
311-
let s = number.to_string();
312-
scalar::BigDecimal::from_str(s.as_str())
313-
.map(|d| g::BigDecimal(d))
314-
.map_err(|e| {
315-
StoreError::Unknown(format_err!(
316-
"failed to convert {} to BigDecimal: {}",
317-
number,
318-
e
319-
))
320-
})
321-
}
322-
(j::Number(number), ColumnType::BigInt) => {
323-
let s = number.to_string();
324-
scalar::BigInt::from_str(s.as_str())
325-
.map(|d| g::BigInt(d))
326-
.map_err(|e| {
327-
StoreError::Unknown(format_err!(
328-
"failed to convert {} to BigInt: {}",
329-
number,
330-
e
331-
))
332-
})
333-
}
334-
(j::Number(number), column_type) => Err(StoreError::Unknown(format_err!(
335-
"can not convert number {} to {:?}",
336-
number,
337-
column_type
338-
))),
339-
(j::String(s), ColumnType::String) | (j::String(s), ColumnType::Enum(_)) => {
340-
Ok(g::String(s))
341-
}
342-
(j::String(s), ColumnType::Bytes) => {
343-
scalar::Bytes::from_str(s.trim_start_matches("\\x"))
344-
.map(|b| g::Bytes(b))
345-
.map_err(|e| {
346-
StoreError::Unknown(format_err!("failed to convert {} to Bytes: {}", s, e))
347-
})
348-
}
349-
(j::String(s), ColumnType::BytesId) => Ok(g::String(bytes_as_str(&s))),
350-
(j::String(s), column_type) => Err(StoreError::Unknown(format_err!(
351-
"can not convert string {} to {:?}",
352-
s,
353-
column_type
354-
))),
355-
(j::Array(values), _) => Ok(g::List(
356-
values
357-
.into_iter()
358-
.map(|v| Self::from_column_value(column_type, v))
359-
.collect::<Result<Vec<_>, _>>()?,
360-
)),
361-
(j::Object(_), _) => {
362-
unimplemented!("objects as entity attributes are not needed/supported")
363-
}
364-
}
329+
fn from_bool(b: bool) -> Self {
330+
graph::prelude::Value::Bool(b)
331+
}
332+
333+
fn from_i32(i: i32) -> Self {
334+
graph::prelude::Value::Int(i)
335+
}
336+
337+
fn from_big_decimal(d: scalar::BigDecimal) -> Self {
338+
graph::prelude::Value::BigDecimal(d)
339+
}
340+
341+
fn from_big_int(i: serde_json::Number) -> Result<Self, StoreError> {
342+
scalar::BigInt::from_str(&i.to_string())
343+
.map(graph::prelude::Value::BigInt)
344+
.map_err(|e| {
345+
StoreError::Unknown(format_err!("failed to convert {} to BigInt: {}", i, e))
346+
})
347+
}
348+
349+
fn from_bytes(b: &str) -> Result<Self, StoreError> {
350+
scalar::Bytes::from_str(b)
351+
.map(graph::prelude::Value::Bytes)
352+
.map_err(|e| {
353+
StoreError::Unknown(format_err!("failed to convert {} to Bytes: {}", b, e))
354+
})
355+
}
356+
357+
fn from_vec(v: Vec<Self>) -> Self {
358+
graph::prelude::Value::List(v)
365359
}
366360
}
367361

0 commit comments

Comments
 (0)