Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 31 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,8 +806,8 @@ mod sqlite {
let mut stmt = match conn.prepare(&format!(
r#"
SELECT *
FROM '{name}'
ORDER BY {first_column}
FROM "{name}"
ORDER BY "{first_column}"
LIMIT {ROWS_PER_PAGE}
OFFSET {offset}
"#
Expand Down Expand Up @@ -1392,8 +1392,8 @@ mod libsql {
&format!(
r#"
SELECT *
FROM '{name}'
ORDER BY {first_column}
FROM "{name}"
ORDER BY "{first_column}"
LIMIT {ROWS_PER_PAGE}
OFFSET {offset}
"#,
Expand Down Expand Up @@ -2233,6 +2233,10 @@ mod mysql {
query_timeout: Duration,
}

fn quote_ident(name: &str) -> String {
format!("`{}`", name.replace('`', "``"))
}

impl Db {
pub async fn open(url: String, query_timeout: Duration) -> color_eyre::Result<Self> {
let pool = Pool::from_url(&url)?;
Expand Down Expand Up @@ -2343,7 +2347,7 @@ mod mysql {
.await?;

for count in row_counts.iter_mut() {
count.count = format!("SELECT count(*) AS count FROM {}", count.name)
count.count = format!("SELECT count(*) AS count FROM {}", quote_ident(&count.name))
.with(())
.first(&mut conn)
.await?
Expand Down Expand Up @@ -2434,7 +2438,7 @@ mod mysql {
.await?;

for table in tables.iter_mut() {
table.count = format!("SELECT count(*) AS count FROM {}", table.name)
table.count = format!("SELECT count(*) AS count FROM {}", quote_ident(&table.name))
.with(())
.first(&mut conn)
.await?
Expand All @@ -2457,7 +2461,7 @@ mod mysql {
.map(|(_, sql): (String, String)| sql)
.ok_or_eyre("couldn't get table sql")?;

let row_count = format!("SELECT count(*) AS count FROM {name}")
let row_count = format!("SELECT count(*) AS count FROM {}", quote_ident(&name))
.with(())
.first(&mut conn)
.await?
Expand Down Expand Up @@ -2536,11 +2540,13 @@ mod mysql {
let offset = (page - 1) * ROWS_PER_PAGE;
let sql = format!(
r#"
SELECT * FROM {name}
ORDER BY {first_column}
SELECT * FROM {}
ORDER BY {}
LIMIT {ROWS_PER_PAGE}
OFFSET {offset}
"#
"#,
quote_ident(&name),
quote_ident(&first_column),
);

let stmt = conn.prep(&sql).await?;
Expand Down Expand Up @@ -4386,6 +4392,10 @@ mod mssql {
query_timeout: Duration,
}

fn quote_ident(name: &str) -> String {
format!("[{}]", name.replace(']', "]]"))
}

impl Db {
pub async fn open(connection: String, query_timeout: Duration) -> color_eyre::Result<Self> {
use tokio_util::compat::TokioAsyncWriteCompatExt;
Expand Down Expand Up @@ -4550,7 +4560,7 @@ mod mssql {
.await;

for count in row_counts.iter_mut() {
let sql = format!("SELECT count(*) AS count FROM {}", count.name);
let sql = format!("SELECT count(*) AS count FROM {}", quote_ident(&count.name));

count.count = client
.query(sql, &[])
Expand Down Expand Up @@ -4696,7 +4706,7 @@ mod mssql {
.await;

for count in tables.iter_mut() {
let sql = format!("SELECT count(*) AS count FROM {}", count.name);
let sql = format!("SELECT count(*) AS count FROM {}", quote_ident(&count.name));

count.count = client
.query(sql, &[])
Expand All @@ -4716,7 +4726,10 @@ mod mssql {
let mut client = self.client.lock().await;

let row_count: i32 = client
.query(format!("SELECT count(*) AS count FROM {name}"), &[])
.query(
format!("SELECT count(*) AS count FROM {}", quote_ident(&name)),
&[],
)
.await?
.into_row()
.await?
Expand Down Expand Up @@ -4812,10 +4825,12 @@ mod mssql {
let offset = (page - 1) * ROWS_PER_PAGE;
let sql = format!(
r#"
SELECT * FROM "{name}"
ORDER BY {first_column}
SELECT * FROM {}
ORDER BY {}
OFFSET {offset} ROWS FETCH NEXT {ROWS_PER_PAGE} ROWS ONLY;
"#
"#,
quote_ident(&name),
quote_ident(&first_column),
);

let mut query = client.query(sql, &[]).await?;
Expand Down