Skip to content

Commit 38b77ac

Browse files
committed
Doing Json or JsonB on String Update case
1 parent aa00a76 commit 38b77ac

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

my-postgres-core/src/sql_update/sql_update_model.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use super::SqlUpdateModelValue;
77

88
pub trait SqlUpdateModel {
99
fn get_column_name(no: usize) -> ColumnName;
10-
fn get_field_value(&self, no: usize) -> SqlUpdateModelValue;
10+
fn get_field_value<'s>(&'s self, no: usize) -> SqlUpdateModelValue<'s>;
1111
fn get_fields_amount() -> usize;
1212

1313
fn get_primary_key_as_single_string(&self) -> String;

my-postgres-core/src/sql_update/sql_update_value_provider.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,15 @@ impl SqlUpdateValueProvider for String {
2020
fn get_update_value(
2121
&self,
2222
params: &mut SqlValues,
23-
_metadata: &Option<SqlValueMetadata>,
23+
metadata: &Option<SqlValueMetadata>,
2424
) -> SqlUpdateValue {
25+
if let Some(metadata) = metadata {
26+
if metadata.is_json_or_jsonb() {
27+
let index = params.push(self.into());
28+
return SqlUpdateValue::Json(index);
29+
}
30+
}
31+
2532
let index = params.push(self.into());
2633
SqlUpdateValue::Index(index)
2734
}
@@ -31,8 +38,14 @@ impl<'s> SqlUpdateValueProvider for &'s str {
3138
fn get_update_value(
3239
&self,
3340
params: &mut SqlValues,
34-
_metadata: &Option<SqlValueMetadata>,
41+
metadata: &Option<SqlValueMetadata>,
3542
) -> SqlUpdateValue {
43+
if let Some(metadata) = metadata {
44+
if metadata.is_json_or_jsonb() {
45+
let index = params.push((*self).into());
46+
return SqlUpdateValue::Json(index);
47+
}
48+
}
3649
let index = params.push((*self).into());
3750
SqlUpdateValue::Index(index)
3851
}

my-postgres-core/src/sql_value/sql_value_metadata.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,13 @@ pub struct SqlValueMetadata {
44
pub operator: Option<&'static str>,
55
pub wrap_column_name: Option<&'static str>,
66
}
7+
8+
impl SqlValueMetadata {
9+
pub fn is_json_or_jsonb(&self) -> bool {
10+
if let Some(sql_type) = self.sql_type {
11+
return sql_type.eq_ignore_ascii_case("json") || sql_type.eq_ignore_ascii_case("jsonb");
12+
}
13+
14+
false
15+
}
16+
}

0 commit comments

Comments
 (0)