Skip to content

Commit aa00a76

Browse files
committed
Fixe jsonb on string
1 parent e7233fc commit aa00a76

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

my-postgres-core/src/sync_table_schema/sync_table_fields.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ use my_logger::LogEventCtx;
55
use my_telemetry::MyTelemetryContext;
66
use rust_extensions::StrOrString;
77

8-
use crate::{
9-
table_schema::{SchemaDifference, TableColumn, TableColumnType, TableSchema, DEFAULT_SCHEMA},
10-
ColumnName, MyPostgresError, PostgresConnection, RequestContext,
11-
};
8+
use crate::{table_schema::*, ColumnName, MyPostgresError, PostgresConnection, RequestContext};
129

1310
pub async fn sync_table_fields(
1411
conn_string: &PostgresConnection,

my-postgres-core/src/table_schema/sql_type_provider.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,19 @@ impl SqlTypeProvider for f64 {
9595
}
9696

9797
impl SqlTypeProvider for String {
98-
fn get_sql_type(_metadata: Option<SqlValueMetadata>) -> TableColumnType {
98+
fn get_sql_type(metadata: Option<SqlValueMetadata>) -> TableColumnType {
99+
if let Some(result) = TableColumnType::from_sql_metadata(&metadata) {
100+
return result;
101+
}
99102
TableColumnType::Text
100103
}
101104
}
102105

103106
impl SqlTypeProvider for Option<String> {
104-
fn get_sql_type(_metadata: Option<SqlValueMetadata>) -> TableColumnType {
107+
fn get_sql_type(metadata: Option<SqlValueMetadata>) -> TableColumnType {
108+
if let Some(result) = TableColumnType::from_sql_metadata(&metadata) {
109+
return result;
110+
}
105111
TableColumnType::Text
106112
}
107113
}

my-postgres-core/src/table_schema/table_column_type.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::SqlValueMetadata;
2+
13
#[derive(Debug, Clone)]
24
pub enum TableColumnType {
35
Text,
@@ -13,6 +15,31 @@ pub enum TableColumnType {
1315
}
1416

1517
impl TableColumnType {
18+
pub fn from_sql_metadata(sql_metadata: &Option<SqlValueMetadata>) -> Option<Self> {
19+
let sql_metadata = sql_metadata.as_ref()?;
20+
let sql_type = sql_metadata.sql_type.as_ref()?;
21+
22+
if sql_type.starts_with("timestamp") {
23+
return Some(TableColumnType::Timestamp);
24+
}
25+
26+
if sql_type.starts_with("double") {
27+
return Some(TableColumnType::Double);
28+
}
29+
30+
match *sql_type {
31+
"text" => Some(TableColumnType::Text),
32+
"smallint" => Some(TableColumnType::SmallInt),
33+
"bigint" => Some(TableColumnType::BigInt),
34+
"boolean" => Some(TableColumnType::Boolean),
35+
"real" => Some(TableColumnType::Real),
36+
"integer" => Some(TableColumnType::Integer),
37+
"json" => Some(TableColumnType::Json),
38+
"jsonb" => Some(TableColumnType::Jsonb),
39+
_ => None,
40+
}
41+
}
42+
1643
pub fn as_number(&self) -> i32 {
1744
match self {
1845
TableColumnType::Text => 0,

0 commit comments

Comments
 (0)