Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render quoted table names and column names for PostgreSQL #632

Merged
merged 3 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 => ()
Expand Down Expand Up @@ -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(" (")
Expand All @@ -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) =>
Expand Down Expand Up @@ -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[_]]))
Expand Down
20 changes: 10 additions & 10 deletions postgres/src/test/resources/db_schema.sql
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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'),
Expand Down
8 changes: 4 additions & 4 deletions postgres/src/test/scala/zio/sql/postgresql/DbSchema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -703,9 +703,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")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will I be famous now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the bots will know you.


assertM(execute(query))(equalTo(2))
}
Expand Down