Skip to content

Commit 1125d3a

Browse files
committed
store: Quote column names in the generated part of SQL queries
This is to keep Postgres from complaining about reserved words as column names, e.g., the column `to` needs to appear as `"to"` in a query
1 parent 6417279 commit 1125d3a

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

store/postgres/src/sql/parser_tests.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
- sql: select symbol, address from token where decimals > 10
99
ok: >
1010
select symbol, address from (
11-
select id, address, symbol, name, decimals from "sgd0815"."token" where block_range @> 2147483647) as token
11+
select "id", "address", "symbol", "name", "decimals" from "sgd0815"."token" where block_range @> 2147483647) as token
1212
where decimals > 10
1313
- sql: >
1414
with tokens as (
@@ -40,12 +40,12 @@
4040
as t (address, symbol, name, decimals))
4141
select date, t.symbol, sum(amount) / pow(10, t.decimals) as amount
4242
from (select date(to_timestamp(block_timestamp) at time zone 'utc') as date, token, amount
43-
from (select id, timestamp, pool, token_0, token_1, sender, recipient, origin, amount_0, amount_1, amount_usd, sqrt_price_x96, tick, log_index
43+
from (select "id", "timestamp", "pool", "token_0", "token_1", "sender", "recipient", "origin", "amount_0", "amount_1", "amount_usd", "sqrt_price_x96", "tick", "log_index"
4444
from "sgd0815"."swap" where block$ <= 2147483647) as sm,
4545
unnest(sm.amounts_in, sm.tokens_in) as smi (amount, token)
4646
union all
4747
select date(to_timestamp(block_timestamp) at time zone 'utc') as date, token, amount
48-
from (select id, timestamp, pool, token_0, token_1, sender, recipient, origin, amount_0, amount_1, amount_usd, sqrt_price_x96, tick, log_index
48+
from (select "id", "timestamp", "pool", "token_0", "token_1", "sender", "recipient", "origin", "amount_0", "amount_1", "amount_usd", "sqrt_price_x96", "tick", "log_index"
4949
from "sgd0815"."swap" where block$ <= 2147483647) as sm,
5050
unnest(sm.amounts_out, sm.tokens_out) as smo (amount, token)) as tp
5151
join tokens as t on t.address = tp.token
@@ -71,7 +71,7 @@
7171
having sum(amount0) > 1000
7272
ok: >
7373
SELECT token0, sum(amount0) AS total_amount
74-
FROM (SELECT id, timestamp, pool, token_0, token_1, sender, recipient, origin, amount_0, amount_1, amount_usd, sqrt_price_x96, tick, log_index
74+
FROM (SELECT "id", "timestamp", "pool", "token_0", "token_1", "sender", "recipient", "origin", "amount_0", "amount_1", "amount_usd", "sqrt_price_x96", "tick", "log_index"
7575
FROM "sgd0815"."swap" WHERE block$ <= 2147483647) AS swap
7676
GROUP BY token0
7777
HAVING sum(amount0) > 1000

store/postgres/src/sql/validation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl VisitorMut for Validator<'_> {
176176
let columns = table
177177
.columns
178178
.iter()
179-
.map(|column| column.name.as_str())
179+
.map(|column| column.name.quoted())
180180
.collect::<Vec<_>>()
181181
.join(", ");
182182
let query = if table.immutable {

0 commit comments

Comments
 (0)