Skip to content

Commit 42a3879

Browse files
committed
prefix SQL keyword with double undescore
1 parent 9b6691a commit 42a3879

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/webserver/database/sql_to_json.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,23 @@ use sqlx::any::{AnyColumn, AnyRow, AnyTypeInfo, AnyTypeInfoKind};
77
use sqlx::Decode;
88
use sqlx::{Column, Row, TypeInfo, ValueRef};
99

10+
fn remove_double_underscore(value: &str) -> String {
11+
let mut chars = value.chars();
12+
chars.next();
13+
chars.next();
14+
chars.as_str().to_owned()
15+
}
16+
1017
pub fn row_to_json(row: &AnyRow) -> Value {
1118
use Value::Object;
1219

1320
let columns = row.columns();
1421
let mut map = Map::new();
1522
for col in columns {
16-
let key = canonical_col_name(col);
23+
let mut key = canonical_col_name(col);
24+
if key.starts_with("__") {
25+
key = remove_double_underscore(&key);
26+
}
1727
let value: Value = sql_to_json(row, col);
1828
map = add_value_to_map(map, (key, value));
1929
}
@@ -533,7 +543,8 @@ mod tests {
533543
42 as "col-with-dashes",
534544
42 as "col with spaces",
535545
42 as "_UNDERSCORE_PREFIX",
536-
42 as "123_NUMBER_PREFIX"
546+
42 as "123_NUMBER_PREFIX",
547+
42 as "__SQL_KEYWORD"
537548
"#,
538549
)
539550
.fetch_one(&mut c)
@@ -553,7 +564,8 @@ mod tests {
553564
"col-with-dashes": 42,
554565
"col with spaces": 42,
555566
"_underscore_prefix": 42,
556-
"123_NUMBER_PREFIX": 42
567+
"123_NUMBER_PREFIX": 42,
568+
"sql_keyword": 42
557569
})
558570
} else {
559571
// Non-ODBC database - names remain as-is
@@ -565,7 +577,8 @@ mod tests {
565577
"col-with-dashes": 42,
566578
"col with spaces": 42,
567579
"_UNDERSCORE_PREFIX": 42,
568-
"123_NUMBER_PREFIX": 42
580+
"123_NUMBER_PREFIX": 42,
581+
"SQL_KEYWORD": 42,
569582
})
570583
};
571584

0 commit comments

Comments
 (0)