From e5ad15beb6df4b3ef092d92c538578ddf31d1a2e Mon Sep 17 00:00:00 2001 From: jczuchnowski Date: Sun, 24 Apr 2022 17:00:02 +0200 Subject: [PATCH] Render quoted table names and column names for PostgreSQL --- .../zio/sql/postgresql/PostgresModule.scala | 10 ++++++---- postgres/src/test/resources/db_schema.sql | 20 +++++++++---------- .../scala/zio/sql/postgresql/DbSchema.scala | 8 ++++---- .../sql/postgresql/PostgresModuleSpec.scala | 4 +--- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala b/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala index 5b90b15df..4ba61f60a 100644 --- a/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala +++ b/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala @@ -463,7 +463,7 @@ trait PostgresModule extends Jdbc { self => case SelectionSet.Empty => () // table is a collection of at least ONE column case SelectionSet.Cons(columnSelection, tail) => val _ = columnSelection.name.map { name => - render(name) + render(quoted(name)) } tail.asInstanceOf[SelectionSet[_]] match { case SelectionSet.Empty => () @@ -556,12 +556,14 @@ trait PostgresModule extends Jdbc { self => expr match { case Expr.Source(_, column) => column.name match { - case Some(columnName) => render(columnName) + case Some(columnName) => render(quoted(columnName)) case _ => () } case _ => () } + private[zio] def quoted(name: String): String = "\"" + name + "\"" + private[zio] def renderExpr[A, B](expr: self.Expr[_, A, B])(implicit render: Renderer): Unit = expr match { case Expr.Subselect(subselect) => render(" (") @@ -570,7 +572,7 @@ trait PostgresModule extends Jdbc { self => case Expr.Source(table, column) => (table, column.name) match { case (tableName: TableName, Some(columnName)) => - render(tableName, ".", columnName) + render(quoted(tableName), ".", quoted(columnName)) case _ => () } case Expr.Unary(base, op) => @@ -832,7 +834,7 @@ trait PostgresModule extends Jdbc { self => renderTable(derivedTable) } // The outer reference in this type test cannot be checked at run time?! - case sourceTable: self.Table.Source => render(sourceTable.name) + case sourceTable: self.Table.Source => render(quoted(sourceTable.name)) case Table.DerivedTable(read, name) => render(" ( ") render(renderRead(read.asInstanceOf[Read[_]])) diff --git a/postgres/src/test/resources/db_schema.sql b/postgres/src/test/resources/db_schema.sql index 9f324157d..85aad89a0 100644 --- a/postgres/src/test/resources/db_schema.sql +++ b/postgres/src/test/resources/db_schema.sql @@ -1,12 +1,12 @@ -create table customers +create table "Customers" ( - id uuid not null primary key, - first_name varchar not null, - last_name varchar not null, - verified boolean not null, - dob date not null, - created_timestamp_string varchar not null, - created_timestamp timestamp with time zone default now() + "Id" uuid not null primary key, + "First_name" varchar not null, + "Last_name" varchar not null, + "Verified" boolean not null, + "Dob" date not null, + "Created_timestamp_string" varchar not null, + "Created_timestamp" timestamp with time zone default now() ); create table orders @@ -78,8 +78,8 @@ ALTER TABLE metro_line ADD CONSTRAINT metro_line_id PRIMARY KEY(id); ALTER TABLE metro_line ADD CONSTRAINT metro_line_system_fk FOREIGN KEY(system_id) REFERENCES metro_system(id) ON DELETE CASCADE ON UPDATE CASCADE; -insert into customers - (id, first_name, last_name, verified, dob, created_timestamp_string, created_timestamp) +insert into "Customers" + ("Id", "First_name", "Last_name", "Verified", "Dob", "Created_timestamp_string", "Created_timestamp") values ('60b01fc9-c902-4468-8d49-3c0f989def37', 'Ronald', 'Russell', true, '1983-01-05', '2020-11-21T19:10:25+00:00', '2020-11-21 19:10:25+00'), ('f76c9ace-be07-4bf3-bd4c-4a9c62882e64', 'Terrence', 'Noel', true, '1999-11-02', '2020-11-21T15:10:25-04:00', '2020-11-21 15:10:25-04'), diff --git a/postgres/src/test/scala/zio/sql/postgresql/DbSchema.scala b/postgres/src/test/scala/zio/sql/postgresql/DbSchema.scala index c78b85b7f..e4ecdcbce 100644 --- a/postgres/src/test/scala/zio/sql/postgresql/DbSchema.scala +++ b/postgres/src/test/scala/zio/sql/postgresql/DbSchema.scala @@ -19,10 +19,10 @@ trait DbSchema extends Jdbc { self => object Customers { // https://github.com/zio/zio-sql/issues/320 Once Insert is supported, we can remove created_timestamp_string val customers = - (uuid("id") ++ localDate("dob") ++ string("first_name") ++ string("last_name") ++ boolean( - "verified" - ) ++ string("created_timestamp_string") ++ zonedDateTime("created_timestamp")) - .table("customers") + (uuid("Id") ++ localDate("Dob") ++ string("First_name") ++ string("Last_name") ++ boolean( + "Verified" + ) ++ string("Created_timestamp_string") ++ zonedDateTime("Created_timestamp")) + .table("Customers") val (customerId, dob, fName, lName, verified, createdString, createdTimestamp) = customers.columns diff --git a/postgres/src/test/scala/zio/sql/postgresql/PostgresModuleSpec.scala b/postgres/src/test/scala/zio/sql/postgresql/PostgresModuleSpec.scala index 3b0eb9af9..c8cd3fe5b 100644 --- a/postgres/src/test/scala/zio/sql/postgresql/PostgresModuleSpec.scala +++ b/postgres/src/test/scala/zio/sql/postgresql/PostgresModuleSpec.scala @@ -691,9 +691,7 @@ object PostgresModuleSpec extends PostgresRunnableSpec with DbSchema { assertM(result)(equalTo(expected)) }, test("update rows") { - import Persons._ - - val query = update(persons).set(fName, "Charlie").where(fName === "Charles") + val query = update(customers).set(fName, "Jaroslav").where(fName === "Jaro") assertM(execute(query))(equalTo(2)) }