Skip to content

Commit ea581c3

Browse files
committed
feat: limit query
1 parent cdfd880 commit ea581c3

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

src-tauri/src/dialect/clickhouse.rs

+12
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ impl Connection for ClickhouseDialect {
6969
log::info!("show columns: {}", &sql);
7070
self.query(&sql, 0, 0).await
7171
}
72+
73+
#[allow(clippy::unused_async)]
74+
async fn query_count(&self, sql: &str) -> anyhow::Result<usize> {
75+
let mut client = self.client().await?;
76+
let block = client.query(sql).fetch_all().await?;
77+
if let Some(row) = block.rows().next() {
78+
let total = row.get::<u32, _>(0)?;
79+
Ok(total as usize)
80+
} else {
81+
Err(anyhow::anyhow!("null"))
82+
}
83+
}
7284
}
7385

7486
impl ClickhouseDialect {

src-tauri/src/dialect/mysql.rs

+10
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ impl Connection for MySqlDialect {
7070
log::info!("show columns: {}", &sql);
7171
self.query(&sql, 0, 0).await
7272
}
73+
74+
#[allow(clippy::unused_async)]
75+
async fn query_count(&self, sql: &str) -> anyhow::Result<usize> {
76+
let mut conn = self.get_conn()?;
77+
if let Some(total) = conn.query_first::<usize, _>(sql)? {
78+
Ok(total)
79+
} else {
80+
Err(anyhow::anyhow!("null"))
81+
}
82+
}
7383
}
7484

7585
impl MySqlDialect {

src-tauri/src/dialect/postgres.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ impl Connection for PostgresDialect {
7272
log::info!("show columns: {}", &sql);
7373
self.query(&sql, 0, 0).await
7474
}
75+
76+
async fn query_count(&self, sql: &str) -> anyhow::Result<usize> {
77+
let conn = self.get_conn(&self.database()).await?;
78+
let row = conn.query_one(sql, &[]).await?;
79+
let total: u32 = row.get(0);
80+
Ok(total as usize)
81+
}
7582
}
7683

7784
impl PostgresDialect {
@@ -109,15 +116,15 @@ impl PostgresDialect {
109116
pub async fn get_tables(&self, db: &str) -> anyhow::Result<Vec<Table>> {
110117
let client = self.get_conn(db).await?;
111118

112-
let sql = r#"
113-
select
114-
table_catalog as db_name,
115-
table_schema as table_schema,
116-
table_name as table_name,
117-
table_type as table_type,
118-
CASE WHEN table_type='BASE TABLE' THEN 'table' ELSE 'view' END as type
119-
from information_schema.tables WHERE table_schema='public'
120-
"#;
119+
let sql = "
120+
select
121+
table_catalog as db_name,
122+
table_schema as table_schema,
123+
table_name as table_name,
124+
table_type as table_type,
125+
CASE WHEN table_type='BASE TABLE' THEN 'table' ELSE 'view' END as type
126+
from information_schema.tables WHERE table_schema='public'
127+
";
121128
let mut tables = vec![];
122129
for row in client.query(sql, &[]).await? {
123130
tables.push(Table {

0 commit comments

Comments
 (0)