zio / zio-jdbc

A small, idiomatic ZIO interface to JDBC.
Apache License 2.0
82 stars 64 forks source link

feat!: Add RETURNING clause #157

Closed regiskuckaertz closed 1 year ago

regiskuckaertz commented 1 year ago

Most RDBMS support the ability of returning rows after an UPDATE/DELETE/INSERT statement.

This is quite useful to move computation inside the database and reduce the number of queries. It also completely subsumes the ability to return created IDs from after INSERT statements.

So that is why I changed the UpdateResult datatype to hold any value and implemented the insertWithKeys in terms of the new executeWithReturning.

Also created an integration spec as I did not know if you wanted to launch containers inside GH actions, current output is:

[zio-default-blocking-1] INFO org.testcontainers.utility.ImageNameSubstitutor - Image name substitution will be performed by: DefaultImageNameSubstitutor (composite of 'ConfigurationFileImageNameSubstitutor' and 'PrefixingImageNameSubstitutor')
[zio-default-blocking-1] INFO org.testcontainers.dockerclient.DockerClientProviderStrategy - Loaded org.testcontainers.dockerclient.UnixSocketClientProviderStrategy from ~/.testcontainers.properties, will try it first
[zio-default-blocking-1] INFO org.testcontainers.dockerclient.DockerClientProviderStrategy - Found Docker environment with Docker accessed via Unix socket (/Users/regiskuckaertz/.docker/run/docker.sock)
[zio-default-blocking-1] INFO org.testcontainers.DockerClientFactory - Docker host IP address is localhost
[zio-default-blocking-1] INFO org.testcontainers.DockerClientFactory - Connected to docker:
  Server Version: 24.0.5
  API Version: 1.43
  Operating System: Docker Desktop
  Total Memory: 7959 MB
[zio-default-blocking-1] INFO tc.testcontainers/ryuk:0.5.1 - Creating container for image: testcontainers/ryuk:0.5.1
[zio-default-blocking-1] INFO org.testcontainers.utility.RegistryAuthLocator - Credential helper/store (docker-credential-desktop) does not have credentials for https://index.docker.io/v1/
[zio-default-blocking-1] INFO tc.testcontainers/ryuk:0.5.1 - Container testcontainers/ryuk:0.5.1 is starting: f50814a76c487cd3f3a47f9c1a84f470c3f30a09705571a8bf80a2072bd53e59
[zio-default-blocking-1] INFO tc.testcontainers/ryuk:0.5.1 - Container testcontainers/ryuk:0.5.1 started in PT10.332311S
[zio-default-blocking-1] INFO org.testcontainers.utility.RyukResourceReaper - Ryuk started - will monitor and terminate Testcontainers containers on JVM exit
[zio-default-blocking-1] INFO org.testcontainers.DockerClientFactory - Checking the system...
[zio-default-blocking-1] INFO org.testcontainers.DockerClientFactory - ✔︎ Docker server version should be at least 1.6.0
[zio-default-blocking-1] INFO tc.postgres:9.6.12 - Creating container for image: postgres:9.6.12
[zio-default-blocking-1] INFO tc.postgres:9.6.12 - Container postgres:9.6.12 is starting: 19b6e4ae434f20f8853f9a20542b28b9be5b3b508052d777dd62fb4c1c803eb4
[zio-default-blocking-1] INFO tc.postgres:9.6.12 - Container postgres:9.6.12 started in PT4.605536S
[zio-default-blocking-1] INFO tc.postgres:9.6.12 - Container is started (JDBC URL: jdbc:postgresql://localhost:50131/test?loggerLevel=OFF)
+ Returning
  + Inserts returning rows
  + Updates returning rows
  + Deletes returning rows
timestamp=2023-09-09T12:04:35.174604Z level=ERROR thread=#zio-fiber-0 message="" cause="Exception in thread "zio-fiber-8" java.io.FileNotFoundException: target/test-reports-zio/output.json (No such file or directory)
        at java.base/java.io.FileInputStream.open0(Native Method)
        at java.base/java.io.FileInputStream.open(FileInputStream.java:216)
        at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
        at scala.io.Source$.fromFile(Source.scala:94)
        at scala.io.Source$.fromFile(Source.scala:79)
        at scala.io.Source$.fromFile(Source.scala:57)
        at zio.test.results.ResultFileOpsJson$Live.$anonfun$removeLastComma$1(ResultFileOpsJson.scala:50)
        at zio.test.results.ResultFileOpsJson.Live.removeLastComma(ResultFileOpsJson.scala:50)
        at zio.test.results.ResultFileOpsJson.Live.closeJson(ResultFileOpsJson.scala:38)
        at zio.test.results.ResultFileOpsJson.Live.apply(ResultFileOpsJson.scala:82)"

(Is there a way to disable the production of that JSON report?)

regiskuckaertz commented 1 year ago

Hello?

regiskuckaertz commented 1 year ago

@adamgfraser can you review please?

regiskuckaertz commented 1 year ago

@guizmaii Thank you 🙏