From de0d57c9a3d48433558ac5f71cdccfa0ca17300c Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sat, 11 Dec 2021 09:32:34 +0100 Subject: [PATCH] Fix JSON conversion of the table name which is also a keyword (#133) 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 a2a27bd..ab954b0 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()),