Skip to content

Commit e2bb11c

Browse files
authored
fix: quote table and column identifiers in mssql, mysql, sqlite and libsql queries (#77)
1 parent f75e221 commit e2bb11c

1 file changed

Lines changed: 31 additions & 16 deletions

File tree

‎src/main.rs‎

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -806,8 +806,8 @@ mod sqlite {
806806
let mut stmt = match conn.prepare(&format!(
807807
r#"
808808
SELECT *
809-
FROM '{name}'
810-
ORDER BY {first_column}
809+
FROM "{name}"
810+
ORDER BY "{first_column}"
811811
LIMIT {ROWS_PER_PAGE}
812812
OFFSET {offset}
813813
"#
@@ -1392,8 +1392,8 @@ mod libsql {
13921392
&format!(
13931393
r#"
13941394
SELECT *
1395-
FROM '{name}'
1396-
ORDER BY {first_column}
1395+
FROM "{name}"
1396+
ORDER BY "{first_column}"
13971397
LIMIT {ROWS_PER_PAGE}
13981398
OFFSET {offset}
13991399
"#,
@@ -2233,6 +2233,10 @@ mod mysql {
22332233
query_timeout: Duration,
22342234
}
22352235

2236+
fn quote_ident(name: &str) -> String {
2237+
format!("`{}`", name.replace('`', "``"))
2238+
}
2239+
22362240
impl Db {
22372241
pub async fn open(url: String, query_timeout: Duration) -> color_eyre::Result<Self> {
22382242
let pool = Pool::from_url(&url)?;
@@ -2343,7 +2347,7 @@ mod mysql {
23432347
.await?;
23442348

23452349
for count in row_counts.iter_mut() {
2346-
count.count = format!("SELECT count(*) AS count FROM {}", count.name)
2350+
count.count = format!("SELECT count(*) AS count FROM {}", quote_ident(&count.name))
23472351
.with(())
23482352
.first(&mut conn)
23492353
.await?
@@ -2434,7 +2438,7 @@ mod mysql {
24342438
.await?;
24352439

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

2460-
let row_count = format!("SELECT count(*) AS count FROM {name}")
2464+
let row_count = format!("SELECT count(*) AS count FROM {}", quote_ident(&name))
24612465
.with(())
24622466
.first(&mut conn)
24632467
.await?
@@ -2536,11 +2540,13 @@ mod mysql {
25362540
let offset = (page - 1) * ROWS_PER_PAGE;
25372541
let sql = format!(
25382542
r#"
2539-
SELECT * FROM {name}
2540-
ORDER BY {first_column}
2543+
SELECT * FROM {}
2544+
ORDER BY {}
25412545
LIMIT {ROWS_PER_PAGE}
25422546
OFFSET {offset}
2543-
"#
2547+
"#,
2548+
quote_ident(&name),
2549+
quote_ident(&first_column),
25442550
);
25452551

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

4395+
fn quote_ident(name: &str) -> String {
4396+
format!("[{}]", name.replace(']', "]]"))
4397+
}
4398+
43894399
impl Db {
43904400
pub async fn open(connection: String, query_timeout: Duration) -> color_eyre::Result<Self> {
43914401
use tokio_util::compat::TokioAsyncWriteCompatExt;
@@ -4550,7 +4560,7 @@ mod mssql {
45504560
.await;
45514561

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

45554565
count.count = client
45564566
.query(sql, &[])
@@ -4696,7 +4706,7 @@ mod mssql {
46964706
.await;
46974707

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

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

47184728
let row_count: i32 = client
4719-
.query(format!("SELECT count(*) AS count FROM {name}"), &[])
4729+
.query(
4730+
format!("SELECT count(*) AS count FROM {}", quote_ident(&name)),
4731+
&[],
4732+
)
47204733
.await?
47214734
.into_row()
47224735
.await?
@@ -4812,10 +4825,12 @@ mod mssql {
48124825
let offset = (page - 1) * ROWS_PER_PAGE;
48134826
let sql = format!(
48144827
r#"
4815-
SELECT * FROM "{name}"
4816-
ORDER BY {first_column}
4828+
SELECT * FROM {}
4829+
ORDER BY {}
48174830
OFFSET {offset} ROWS FETCH NEXT {ROWS_PER_PAGE} ROWS ONLY;
4818-
"#
4831+
"#,
4832+
quote_ident(&name),
4833+
quote_ident(&first_column),
48194834
);
48204835

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

0 commit comments

Comments
 (0)