From f24cc820543f73cd5781aad94a08defa6b982380 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Fri, 12 Nov 2021 15:46:13 +0100 Subject: [PATCH] Fix JSON conversion of the table name which is also a keyword Similarly to escape the table name in the SELECT command query, here we escape the table name with apostrophes. In fact, `to_json()` raised a "Syntax error" for table that uses a keyword as its name. --- src/database/postgres.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/database/postgres.rs b/src/database/postgres.rs index b4e4535..0c2a187 100644 --- a/src/database/postgres.rs +++ b/src/database/postgres.rs @@ -482,7 +482,7 @@ impl PostgresPool { ) -> anyhow::Result> { let query = if let Some(filter) = filter { format!( - r#"SELECT to_json({table}.*) FROM "{database}"."{table_schema}"."{table}" WHERE {filter} LIMIT {limit} OFFSET {page}"#, + r#"SELECT to_json("{table}".*) FROM "{database}"."{table_schema}"."{table}" WHERE {filter} LIMIT {limit} OFFSET {page}"#, database = database.name, table = table.name, filter = filter, @@ -492,7 +492,7 @@ impl PostgresPool { ) } else { format!( - r#"SELECT to_json({table}.*) FROM "{database}"."{table_schema}"."{table}" LIMIT {limit} OFFSET {page}"#, + r#"SELECT to_json("{table}".*) FROM "{database}"."{table_schema}"."{table}" LIMIT {limit} OFFSET {page}"#, database = database.name, table = table.name, table_schema = table.schema.clone().unwrap_or_else(|| "public".to_string()),