Skip to content

Commit

Permalink
Merge pull request #876 from tonykwok1992/null-primitive-type
Browse files Browse the repository at this point in the history
Make null primitive value return None
  • Loading branch information
sviezypan authored Jun 12, 2023
2 parents 37e49d2 + e594eba commit cccc040
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions jdbc/src/main/scala/zio/sql/JdbcInternalModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ trait JdbcInternalModule { self: Jdbc =>
else
try {
val value = decoder
if (resultSet.wasNull()) return Right(null.asInstanceOf[A])

value match {
case Some(value) => Right(value)
Expand Down
8 changes: 8 additions & 0 deletions postgres/src/test/resources/db_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ 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;

create table movies
(
id int not null primary key,
rating int
);

insert into "movies" ("id", "rating") values (1, null);

insert into "customers"
("id", "first_name", "last_name", "verified", "dob", "created_timestamp_string", "created_timestamp")
values
Expand Down
13 changes: 13 additions & 0 deletions postgres/src/test/scala/zio/sql/postgresql/DbSchema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,17 @@ trait DbSchema extends PostgresJdbcModule { self =>

val (personsId, personsName, birthDate) = persons.columns
}

object MoviesSchema {

case class Movies(id: Int, rating: Option[Int])

implicit val moviesSchema = DeriveSchema.gen[Movies]

val movies = Table.defineTableSmart[Movies]

val (id, rating) = movies.columns

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package zio.sql.postgresql
import java.math.BigDecimal
import java.time._
import java.util.UUID

import zio._
import zio.schema._
import zio.test._
Expand All @@ -12,6 +11,7 @@ import zio.test.TestAspect._
import zio.sql.expr.AggregationDef._
import zio.sql.expr._
import zio.sql.select._

import scala.language.postfixOps

object PostgresSqlModuleSpec extends PostgresRunnableSpec with DbSchema {
Expand Down Expand Up @@ -703,6 +703,24 @@ object PostgresSqlModuleSpec extends PostgresRunnableSpec with DbSchema {
orders <- execute(allOrders).runCollect
products <- execute(allProducts).runCollect
} yield assertTrue(customers.length == 2) && assertTrue(orders.length == 35) && assertTrue(products.length == 10)
},
test("Select null int value as None Option") {
val movieColumns = MoviesSchema.id ++ MoviesSchema.rating
val selectStmt = select(movieColumns).from(MoviesSchema.movies).where(MoviesSchema.id === 1)
val selectResult = execute(selectStmt.to((MoviesSchema.Movies.apply _).tupled)).runCollect

for {
movies <- selectResult
} yield assert(movies.toList)(
equalTo(
List(
MoviesSchema.Movies(
id = 1,
rating = None
)
)
)
)
}
) @@ sequential
}

0 comments on commit cccc040

Please sign in to comment.