Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
l1xnan committed Feb 26, 2024
1 parent c4efd80 commit f9a22d9
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 92 deletions.
4 changes: 2 additions & 2 deletions src-tauri/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub async fn get_db(
port: Option<String>,
cwd: Option<String>,
database: Option<String>,
) -> Result<Option<TreeNode>, String> {
) -> Result<TreeNode, String> {
let payload = DialectPayload {
dialect: dialect.to_string(),
path,
Expand All @@ -162,7 +162,7 @@ pub async fn get_db(
};

if let Some(d) = get_dialect(payload).await {
Ok(d.get_db().await)
d.get_db().await.map_err(|e| e.to_string())
} else {
Err("not support dialect".to_string())
}
Expand Down
23 changes: 10 additions & 13 deletions src-tauri/src/dialect/clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,15 @@ impl ClickhouseDialect {

#[async_trait]
impl Dialect for ClickhouseDialect {
async fn get_db(&self) -> Option<TreeNode> {
async fn get_db(&self) -> anyhow::Result<TreeNode> {
let url = self.get_url();
if let Ok(tables) = get_tables(&url).await {
Some(TreeNode {
name: self.host.clone(),
path: self.host.clone(),
node_type: "root".to_string(),
children: Some(build_tree(tables)),
})
} else {
None
}
let tables = get_tables(&url).await?;
Ok(TreeNode {
name: self.host.clone(),
path: self.host.clone(),
node_type: "root".to_string(),
children: Some(build_tree(tables)),
})
}

async fn query(&self, sql: &str, limit: usize, offset: usize) -> anyhow::Result<ArrowData> {
Expand Down Expand Up @@ -221,7 +218,7 @@ fn convert_type(col_type: &SqlType) -> DataType {
SqlType::Date => DataType::Date32,
SqlType::String => DataType::Utf8,
SqlType::DateTime(_) => DataType::Date64,
SqlType::Nullable(t) => convert_type(t.clone()),
SqlType::Nullable(t) => convert_type(*t),
SqlType::Decimal(d1, d2) => DataType::Utf8,
SqlType::Array(t) => DataType::List(Arc::new(Field::new("", convert_type(t), false))),
_ => DataType::Utf8,
Expand Down Expand Up @@ -267,7 +264,7 @@ fn convert_col(
) -> anyhow::Result<(Field, ArrayRef)> {
let nullable = matches!(col_type, SqlType::Nullable(_));
let typ = if let SqlType::Nullable(t) = col_type {
t.clone()
*t
} else {
col_type
};
Expand Down
30 changes: 12 additions & 18 deletions src-tauri/src/dialect/duckdb.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use std::env::{current_dir, set_current_dir};
use std::fs::File;
use async_trait::async_trait;
use duckdb::Connection;

use crate::api;
use crate::api::{serialize_preview, ArrowData};
use crate::api::ArrowData;
use crate::dialect::{Dialect, TreeNode};
use crate::utils::{build_tree, get_file_name, write_csv, Table};
use arrow::csv::{Writer, WriterBuilder};
use arrow::ipc::RecordBatch;
use async_trait::async_trait;
use duckdb::Connection;

#[derive(Debug, Default)]
pub struct DuckDbDialect {
pub path: String,
Expand All @@ -17,17 +14,14 @@ pub struct DuckDbDialect {

#[async_trait]
impl Dialect for DuckDbDialect {
async fn get_db(&self) -> Option<TreeNode> {
if let Ok(tables) = get_tables(&self.path) {
Some(TreeNode {
name: get_file_name(&self.path),
path: self.path.clone(),
node_type: "root".to_string(),
children: Some(build_tree(tables)),
})
} else {
None
}
async fn get_db(&self) -> anyhow::Result<TreeNode> {
let tables = get_tables(&self.path)?;
Ok(TreeNode {
name: get_file_name(&self.path),
path: self.path.clone(),
node_type: "root".to_string(),
children: Some(build_tree(tables)),
})
}

async fn query(&self, sql: &str, limit: usize, offset: usize) -> anyhow::Result<ArrowData> {
Expand Down
4 changes: 2 additions & 2 deletions src-tauri/src/dialect/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ pub struct FileDialect {

#[async_trait]
impl Dialect for FileDialect {
async fn get_db(&self) -> Option<TreeNode> {
async fn get_db(&self) -> anyhow::Result<TreeNode> {
let path = PathBuf::from(self.path.as_str());

Some(TreeNode {
Ok(TreeNode {
path: self.path.clone(),
name: get_file_name(&self.path),
node_type: path.extension().unwrap().to_string_lossy().to_string(),
Expand Down
14 changes: 5 additions & 9 deletions src-tauri/src/dialect/folder.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
use anyhow::anyhow;
use arrow::csv::WriterBuilder;
use async_trait::async_trait;
use std::env::current_dir;
use std::fs::File;
use std::fs;
use std::path::Path;
use std::{env::set_current_dir, fs};

use async_trait::async_trait;
use duckdb::Connection;

use crate::api;
use crate::utils::write_csv;
use crate::{
api::{serialize_preview, ArrowData},
api::ArrowData,
dialect::{Dialect, TreeNode},
};

Expand All @@ -23,8 +19,8 @@ pub struct FolderDialect {

#[async_trait]
impl Dialect for FolderDialect {
async fn get_db(&self) -> Option<TreeNode> {
directory_tree(self.path.as_str())
async fn get_db(&self) -> anyhow::Result<TreeNode> {
directory_tree(self.path.as_str()).ok_or(anyhow::bail!("null"))
}

async fn query(&self, sql: &str, limit: usize, offset: usize) -> anyhow::Result<ArrowData> {
Expand Down
6 changes: 3 additions & 3 deletions src-tauri/src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ pub struct Title {

#[async_trait]
pub trait Dialect: Sync + Send {
async fn get_db(&self) -> Option<TreeNode>;
async fn query(&self, sql: &str, limit: usize, offset: usize) -> anyhow::Result<ArrowData> {
async fn get_db(&self) -> anyhow::Result<TreeNode>;
async fn query(&self, _sql: &str, _limit: usize, _offset: usize) -> anyhow::Result<ArrowData> {
unimplemented!()
}
async fn export(&self, sql: &str, file: &str) {
async fn export(&self, _sql: &str, _file: &str) {
unimplemented!()
}
}
Expand Down
21 changes: 9 additions & 12 deletions src-tauri/src/dialect/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,20 @@ pub struct MySqlDialect {

#[async_trait]
impl Dialect for MySqlDialect {
async fn get_db(&self) -> Option<TreeNode> {
if let Ok(tables) = self.get_tables() {
Some(TreeNode {
name: self.host.clone(),
path: self.host.clone(),
node_type: "root".to_string(),
children: Some(build_tree(tables)),
})
} else {
None
}
async fn get_db(&self) -> anyhow::Result<TreeNode> {
let tables = self.get_tables()?;
Ok(TreeNode {
name: self.host.clone(),
path: self.host.clone(),
node_type: "root".to_string(),
children: Some(build_tree(tables)),
})
}

async fn query(&self, sql: &str, limit: usize, offset: usize) -> anyhow::Result<ArrowData> {
let mut conn = self.get_conn()?;

let mut stmt = conn.prep(sql)?;
let stmt = conn.prep(sql)?;
let mut fields = vec![];
let k = stmt.num_columns();
let mut titles = vec![];
Expand Down
27 changes: 12 additions & 15 deletions src-tauri/src/dialect/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,24 @@ pub struct PostgresDialect {

#[async_trait]
impl Dialect for PostgresDialect {
async fn get_db(&self) -> Option<TreeNode> {
if let Ok(tables) = self.get_all_tables().await {
Some(TreeNode {
name: self.host.clone(),
path: self.host.clone(),
node_type: "root".to_string(),
children: Some(build_tree(tables)),
})
} else {
None
}
async fn get_db(&self) -> anyhow::Result<TreeNode> {
let tables = self.get_all_tables().await?;
Ok(TreeNode {
name: self.host.clone(),
path: self.host.clone(),
node_type: "root".to_string(),
children: Some(build_tree(tables)),
})
}

async fn query(&self, sql: &str, limit: usize, offset: usize) -> anyhow::Result<ArrowData> {
let mut conn = self
let conn = self
.get_conn(&self.database.clone().unwrap_or("postgres".to_string()))
.await?;

let mut stmt = conn.prepare(sql).await?;
let stmt = conn.prepare(sql).await?;
let mut fields = vec![];
let k = stmt.columns().len();
let _k = stmt.columns().len();
let mut titles = vec![];
for col in stmt.columns() {
titles.push(Title {
Expand Down Expand Up @@ -110,7 +107,7 @@ impl PostgresDialect {
}

async fn get_schema(&self) -> Vec<Table> {
vec![]
unimplemented!()
}
pub async fn databases(&self) -> anyhow::Result<Vec<String>> {
let mut client = self.get_conn("postgres").await?;
Expand Down
39 changes: 21 additions & 18 deletions src-tauri/src/dialect/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,25 @@ pub struct SqliteDialect {

#[async_trait]
impl Dialect for SqliteDialect {
async fn get_db(&self) -> Option<TreeNode> {
async fn get_db(&self) -> anyhow::Result<TreeNode> {
let url = self.get_url();
if let Ok(tables) = get_tables(&url).await {
let mut tree = build_tree(tables);
let children = if tree.len() > 0 {
&tree[0].children
} else {
&None
};
Some(TreeNode {
name: get_file_name(&self.path),
path: self.path.clone(),
node_type: "root".to_string(),
children: children.clone(),
})
let tables = get_tables(&url).await?;
println!("tables={:?}", tables);
let tree = build_tree(tables);
let children = if tree.len() > 0 {
&tree[0].children
} else {
None
}
&None
};
Ok(TreeNode {
name: get_file_name(&self.path),
path: self.path.clone(),
node_type: "root".to_string(),
children: children.clone(),
})
}

async fn query(&self, sql: &str, limit: usize, offset: usize) -> anyhow::Result<ArrowData> {
async fn query(&self, sql: &str, _limit: usize, _offset: usize) -> anyhow::Result<ArrowData> {
let conn = Connection::open(&self.path)?;
let mut stmt = conn.prepare(sql)?;

Expand Down Expand Up @@ -104,7 +102,7 @@ impl SqliteDialect {
}

async fn get_schema(&self) -> Vec<Table> {
vec![]
unimplemented!()
}

fn fetch_all(&self, sql: &str) -> anyhow::Result<ArrowData> {
Expand Down Expand Up @@ -222,6 +220,7 @@ pub fn convert_arrow(value: &Value, typ: &str) -> ArrayRef {
}
}

#[allow(dead_code)]
pub fn convert_to_string(value: &Value) -> Option<String> {
match value {
Value::Integer(i) => Some(i.to_string()),
Expand All @@ -232,6 +231,7 @@ pub fn convert_to_string(value: &Value) -> Option<String> {
}
}

#[allow(dead_code)]
pub fn convert_to_i64(value: &Value) -> Option<i64> {
match value {
Value::Integer(i) => Some(*i),
Expand All @@ -250,14 +250,17 @@ pub fn convert_to_f64(value: &Value) -> Option<f64> {
}
}

#[allow(dead_code)]
pub fn convert_to_strings(values: &[Value]) -> Vec<Option<String>> {
values.iter().map(|v| convert_to_string(v)).collect()
}

#[allow(dead_code)]
pub fn convert_to_i64s(values: &[Value]) -> Vec<Option<i64>> {
values.iter().map(|v| convert_to_i64(v)).collect()
}

#[allow(dead_code)]
pub fn convert_to_f64s(values: &[Value]) -> Vec<Option<f64>> {
values.iter().map(|v| convert_to_f64(v)).collect()
}

0 comments on commit f9a22d9

Please sign in to comment.