Skip to content

Commit

Permalink
fix: pg types
Browse files Browse the repository at this point in the history
  • Loading branch information
invm authored and Michael Ionov committed Dec 30, 2023
1 parent f5a9db7 commit 01a0429
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 14 deletions.
1 change: 1 addition & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ tracing-subscriber = "0.3.17"
sqlparser = "0.38.0"
md-5 = "0.10.6"
chrono = "0.4.31"
postgres = { version = "0.19.7", features = ["array-impls", "with-serde_json-1"] }
postgres = { version = "0.19.7", features = ["array-impls", "with-serde_json-1", "with-chrono-0_4"] }
deadpool-postgres = "0.12.1"

[features]
Expand Down
12 changes: 11 additions & 1 deletion src-tauri/src/database/engine/postgresql/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ fn convert_value(row: &Row, column: &Column, column_i: usize) -> Result<Value> {
// for postgres types: https://www.postgresql.org/docs/7.4/datatype.html#DATATYPE-TABLE

// single types
Type::TIMESTAMP => {
get_basic(row, column, column_i, |a: chrono::NaiveDateTime| {
Ok(Value::String(a.to_string()))
})?
}
Type::TIMESTAMPTZ => {
get_basic(row, column, column_i, |a: chrono::DateTime<chrono::Utc>| {
Ok(Value::String(a.to_string()))
})?
}
Type::BOOL => get_basic(row, column, column_i, |a: bool| Ok(Value::Bool(a)))?,
Type::INT2 => get_basic(row, column, column_i, |a: i16| {
Ok(Value::Number(serde_json::Number::from(a)))
Expand All @@ -36,7 +46,7 @@ fn convert_value(row: &Row, column: &Column, column_i: usize) -> Result<Value> {
Type::INT8 => get_basic(row, column, column_i, |a: i64| {
Ok(Value::Number(serde_json::Number::from(a)))
})?,
Type::TEXT | Type::VARCHAR => {
Type::TEXT | Type::VARCHAR | Type::NAME | Type::CHAR | Type::UNKNOWN => {
get_basic(row, column, column_i, |a: String| Ok(Value::String(a)))?
}
Type::JSON | Type::JSONB => get_basic(row, column, column_i, |a: Value| Ok(a))?,
Expand Down
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function App() {

onMount(async () => {
await listen<QueryTaskResult>(Events.QueryFinished, async (event) => {
console.log({ event });
console.log(event);
await compareAndAssign(event.payload);
});
});
Expand Down
1 change: 0 additions & 1 deletion src/services/Connections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ export const ConnectionsService = () => {

const getSchemaEntity = <T extends keyof Schema>(entity: T): Schema[T] => {
const schema = getConnection().selectedSchema;
console.log({ schema, entity });
if (entity === 'tables') {
return getConnection().definition[schema]?.['tables'] ?? [];
}
Expand Down
17 changes: 7 additions & 10 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,19 @@ export const sortTableStructure = (

// TODO: handle all dialects
export const columnsToTable = (allColumns: Row[], dialect: DialectType) => {
if (dialect === Dialect.Mysql) {
if (dialect === Dialect.Mysql || dialect === Dialect.Postgresql) {
const schema = allColumns.reduce((acc, col) => {
return {
...acc,
[String(col.TABLE_NAME)]: {
// @ts-ignore
...acc[String(col.TABLE_NAME)],
[String(col.COLUMN_NAME)]: col,
},
};
const table_name = String(col.table_name ?? col.TABLE_NAME);
const column_name = String(col.column_name ?? col.COLUMN_NAME);
acc[table_name] = { ...(acc[table_name] as Record<string, string>), [column_name]: col };
return acc;
}, {});

return Object.keys(schema).map((name) => {
const columns = Object.values(schema[name]).map((col) => {
const name = String(col.column_name ?? col.COLUMN_NAME);
return {
name: String(col.COLUMN_NAME),
name,
props: col,
};
});
Expand Down

0 comments on commit 01a0429

Please sign in to comment.