Skip to content

Commit

Permalink
fix: handle edge cases in psql types
Browse files Browse the repository at this point in the history
  • Loading branch information
invm authored and Michael Ionov committed Jan 2, 2024
1 parent 088fdc5 commit 1d368a2
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 48 deletions.
44 changes: 32 additions & 12 deletions src-tauri/src/database/engine/postgresql/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ fn convert_value(row: &Row, column: &Column, column_i: usize) -> Result<Value> {
Ok(match *column.type_() {
// for rust-postgres <> postgres type-mappings: https://docs.rs/postgres/latest/postgres/types/trait.FromSql.html#types
// 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::OID => get_basic(row, column, column_i, |a: u32| {
Ok(Value::Number(serde_json::Number::from(a)))
})?,
Type::INET => get_basic(row, column, column_i, |a: std::net::IpAddr| {
Ok(Value::String(a.to_string()))
})?,
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()))
Expand Down Expand Up @@ -78,13 +80,31 @@ fn convert_value(row: &Row, column: &Column, column_i: usize) -> Result<Value> {
get_array(row, column, column_i, |a: f64| Ok(f64_to_json_number(a)?))?
}

_ => anyhow::bail!(
"Cannot convert pg-cell \"{}\" of type \"{}\" to a Value.",
column.name(),
column.type_().name()
),
_ => {
let val: Option<GenericEnum> = row.get(1);
if let Some(_i) = val {
return Ok(Value::String(_i.0));
}
Value::Null
}
})
}
#[derive(Debug)]
struct GenericEnum(String);

impl FromSql<'_> for GenericEnum {
fn from_sql(
_: &Type,
raw: &[u8],
) -> Result<GenericEnum, Box<dyn std::error::Error + Sync + Send>> {
let result = std::str::from_utf8(raw).unwrap();
let val = GenericEnum(result.to_owned());
Ok(val)
}
fn accepts(_ty: &Type) -> bool {
true
}
}

fn get_basic<'a, T: FromSql<'a>>(
row: &'a Row,
Expand Down
6 changes: 1 addition & 5 deletions src-tauri/src/database/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,12 @@ pub fn add_connection(db: &AppConnection, conn: &ConnectionConfig) -> Result<()>
)?;
let credentials = serde_json::to_string(&conn.credentials)?;
let credentials = encrypt_data(&credentials, &get_app_key()?);
let schema = match conn.dialect {
Dialect::Postgresql => "public",
_ => "",
};
statement.execute(named_params! {
":id": conn.id.to_string(),
":dialect": conn.dialect.to_string(),
":mode": conn.mode.to_string(),
":credentials": credentials,
":schema": schema,
":schema": conn.schema,
":name": conn.name,
":color": conn.color,
})?;
Expand Down
33 changes: 17 additions & 16 deletions src/components/Screens/Console/Sidebar/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { useAppSelector } from 'services/Context';
import { useContextMenu, Menu, animation, Item } from 'solid-contextmenu';
import { createSignal, For, Match, Show, Switch } from 'solid-js';
import { t } from 'utils/i18n';
import { Function, Refresh, Terminal } from 'components/UI/Icons';
import { Function, Refresh, ShareNodes, Terminal } from 'components/UI/Icons';
import { invoke } from '@tauri-apps/api';
import { ResultSet } from 'interfaces';
import { newContentTab } from 'services/Connections';
import MindShare from 'components/UI/Icons/MindShare';

export const Sidebar = () => {
const {
Expand Down Expand Up @@ -63,7 +62,11 @@ export const Sidebar = () => {

const showProcessList = async () => {
try {
const query = 'SHOW PROCESSLIST';
const conn = getConnection();
let query = 'SHOW PROCESSLIST';
if (conn.connection.dialect === 'Postgresql') {
query = 'SELECT * FROM pg_stat_activity';
}
const res = await invoke<ResultSet>('execute_query', {
connId: getConnection().id,
query,
Expand Down Expand Up @@ -186,7 +189,7 @@ export const Sidebar = () => {
<button
onClick={() => insertColumnName(column.name)}
class="flex btn-ghost w-full justify-between items-center w-full border-b-2 border-base-300">
<span class="text-xs font-semibold">{column.name}</span>
<span class="text-xs font-semibold ">{column.name}</span>
<span class="text-xs font-medium ml-2">
{column.props.COLUMN_TYPE ?? column.props.column_type}
</span>
Expand All @@ -202,7 +205,7 @@ export const Sidebar = () => {
<div class="text-xs font-bold py-1">{t('sidebar.routines')}</div>
<For each={getSchemaEntity('routines')}>
{(routine) => (
<div class="px-2 min-w-full w-fit">
<div class="px-2 w-fit truncate">
<div onContextMenu={(e) => show(e)}>
<Menu id={menu_id} animation={animation.fade} theme={'dark'}>
<Item onClick={() => showRoutine(routine.ROUTINE_NAME as string)}>{t('sidebar.show_routine')}</Item>
Expand All @@ -221,7 +224,7 @@ export const Sidebar = () => {
<Function />
</div>
</span>
<span class="text-xs font-semibold">
<span class="text-xs font-semibold truncate">
{String(routine['ROUTINE_NAME'] ?? routine['routine_name'])}
</span>
</div>
Expand All @@ -233,7 +236,7 @@ export const Sidebar = () => {
<div class="text-xs font-bold py-1">{t('sidebar.triggers')}</div>
<For each={getSchemaEntity('triggers')}>
{(trigger) => (
<div class="px-2 min-w-full w-fit">
<div class="px-2 w-fit truncate">
<div class="" onContextMenu={(e) => show(e)}>
<Menu id={menu_id} animation={animation.fade} theme={'dark'}>
<Item onClick={() => showTrigger(trigger.TRIGGER_NAME as string)}>{t('sidebar.show_trigger')}</Item>
Expand All @@ -248,17 +251,15 @@ export const Sidebar = () => {
<div
class="tooltip tooltip-info tooltip-right tooltip-xs"
data-tip={t(`sidebar.tooltips.triggers`)}>
<MindShare />
<ShareNodes />
</div>
</span>
{
<span class="text-xs font-semibold">
{String(trigger['TRIGGER_NAME'] ?? trigger['trigger_name']) +
' (' +
String(trigger['EVENT_OBJECT_TABLE'] ?? trigger['event_object_table']) +
')'}
</span>
}
<span class="text-xs font-semibold">
{String(trigger['TRIGGER_NAME'] ?? trigger['trigger_name']) +
' (' +
String(trigger['EVENT_OBJECT_TABLE'] ?? trigger['event_object_table']) +
')'}
</span>
</div>
</div>
)}
Expand Down
5 changes: 0 additions & 5 deletions src/components/Screens/Help/Help.tsx

This file was deleted.

7 changes: 5 additions & 2 deletions src/components/UI/Icons/Function.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
export const Function = () => {
return (
<svg class="w-4 h-4 text-primary" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<svg class="w-3 h-3 text-success" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path
fill-rule="evenodd"
stroke="currentColor"
clip-rule="evenodd"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M4.92789 6.39814C3.16565 4.88765 4.23386 2 6.55487 2H19C19.5523 2 20 2.44772 20 3C20 3.55228 19.5523 4 19 4H6.55487C6.09067 4 5.87703 4.57753 6.22948 4.87963L12.3221 10.1019C13.4861 11.0996 13.4861 12.9004 12.3221 13.8981L6.22948 19.1204C5.87703 19.4225 6.09067 20 6.55487 20H19C19.5523 20 20 20.4477 20 21C20 21.5523 19.5523 22 19 22H6.55487C4.23386 22 3.16565 19.1124 4.92789 17.6019L11.0205 12.3796C11.2533 12.1801 11.2533 11.8199 11.0205 11.6204L4.92789 6.39814Z"
fill="#0F0F0F"
/>
</svg>
);
Expand Down
7 changes: 0 additions & 7 deletions src/components/UI/Icons/MindShare.tsx

This file was deleted.

18 changes: 18 additions & 0 deletions src/components/UI/Icons/ShareNodes.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const ShareNodes = () => {
return (
<svg
class="w-3 h-3 text-error"
aria-hidden="true"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 18 18">
<path
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="m5.953 7.467 6.094-2.612m.096 8.114L5.857 9.676m.305-1.192a2.581 2.581 0 1 1-5.162 0 2.581 2.581 0 0 1 5.162 0ZM17 3.84a2.581 2.581 0 1 1-5.162 0 2.581 2.581 0 0 1 5.162 0Zm0 10.322a2.581 2.581 0 1 1-5.162 0 2.581 2.581 0 0 1 5.162 0Z"
/>
</svg>
);
};
2 changes: 1 addition & 1 deletion src/components/UI/Icons/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ export * from './Enter';
export * from './Fire';
export * from './Function';
export * from './Home';
export * from './MindShare';
export * from './Pen';
export * from './QuestionMark';
export * from './Refresh';
export * from './ShareNodes';
export * from './Table';
export * from './Terminal';
export * from './ThreeDots';
Expand Down
3 changes: 3 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,6 @@ dialog {
width: 6rem;
}

.tooltip::before {
@apply z-50;
}

0 comments on commit 1d368a2

Please sign in to comment.