From b83551e4de7f3e959e934772143ff50de6fff26b Mon Sep 17 00:00:00 2001 From: Rikard Pavelic Date: Wed, 30 Nov 2022 13:41:43 +0100 Subject: [PATCH] Revenj Scala v1.5 Allow use of optimized persist function. --- scala/build.sbt | 6 +- .../postgres/converters/ArrayTuple.scala | 109 ++++++++++++++++++ 2 files changed, 112 insertions(+), 3 deletions(-) diff --git a/scala/build.sbt b/scala/build.sbt index 0e96704b..7ec35cd0 100644 --- a/scala/build.sbt +++ b/scala/build.sbt @@ -6,7 +6,7 @@ lazy val core = (project in file("revenj-core") settings (commonSettings ++ publishSettings) enablePlugins(SbtDslPlatformPlugin) settings( - version := "1.4.0", + version := "1.5.0", libraryDependencies ++= Seq( "org.postgresql" % "postgresql" % "42.3.1", "joda-time" % "joda-time" % "2.10.13", // TODO: will be removed @@ -29,7 +29,7 @@ lazy val core = (project in file("revenj-core") lazy val akka = (project in file("revenj-akka") settings (commonSettings ++ publishSettings) settings( - version := "1.4.0", + version := "1.5.0", libraryDependencies ++= Seq( "com.typesafe" % "config" % "1.3.4", "com.typesafe.akka" %% "akka-http" % "10.2.7", @@ -42,7 +42,7 @@ lazy val akka = (project in file("revenj-akka") lazy val storage = (project in file("revenj-storage") settings (commonSettings ++ publishSettings) settings( - version := "1.4.0", + version := "1.5.0", libraryDependencies ++= Seq( "com.amazonaws" % "aws-java-sdk-s3" % "1.12.115", "org.specs2" %% "specs2-scalacheck" % "4.13.0" % Test, diff --git a/scala/revenj-core/src/main/scala/net/revenj/database/postgres/converters/ArrayTuple.scala b/scala/revenj-core/src/main/scala/net/revenj/database/postgres/converters/ArrayTuple.scala index 552b9035..8422c681 100644 --- a/scala/revenj-core/src/main/scala/net/revenj/database/postgres/converters/ArrayTuple.scala +++ b/scala/revenj-core/src/main/scala/net/revenj/database/postgres/converters/ArrayTuple.scala @@ -1,7 +1,10 @@ package net.revenj.database.postgres.converters +import net.revenj.database.postgres.PostgresWriter.{CollectionDiff, NewTuple} import net.revenj.database.postgres.converters.PostgresTuple.buildNextEscape import net.revenj.database.postgres.{PostgresReader, PostgresWriter} +import net.revenj.patterns.{Equality, Identifiable} +import org.postgresql.util.PGobject import scala.collection.concurrent.TrieMap import scala.collection.mutable @@ -346,4 +349,110 @@ object ArrayTuple { } } } + + def toParameter(sw: PostgresWriter, arrayType: String, data: Iterable[Array[PostgresTuple]]): PGobject = { + sw.reset() + val array = new ArrayTuple(data.map(a => RecordTuple(a)).toArray) + val pgo = new PGobject + pgo.setType(arrayType) + array.buildTuple(sw, false) + pgo.setValue(sw.bufferToString()) + sw.reset() + pgo + } + + private def emptyPGO(arrayType: String): PGobject = { + val pgo = new PGobject + pgo.setType(arrayType) + pgo.setValue("NULL") + pgo + } + + def toParameterSimple[T]( + sw: PostgresWriter, + collection: scala.collection.Seq[T], + arrayType: String, + toTuple: T => PostgresTuple + ): PGobject = { + if (collection.nonEmpty) { + toParameter( + sw, + arrayType, + collection.zipWithIndex.map { case (item, ind) => + Array[PostgresTuple]( + IntConverter.toTuple(ind), + toTuple(item)) + }) + } else { + emptyPGO(arrayType) + } + } + + def toParameterPair[T]( + sw: PostgresWriter, + collection: scala.collection.Seq[(T, T)], + arrayType: String, + toTupleUpdate: T => PostgresTuple, + toTupleTable: T => PostgresTuple + ): PGobject = { + if(collection.nonEmpty) { + toParameter( + sw, + arrayType, + collection.zipWithIndex.map { case ((oldValue, newValue), ind) => + Array[PostgresTuple]( + IntConverter.toTuple(ind), + if (oldValue == null) PostgresTuple.NULL else toTupleUpdate(oldValue), + if (newValue == null) PostgresTuple.NULL else toTupleTable(newValue)) + }) + } else { + emptyPGO(arrayType) + } + } + + def toParameterNew[T]( + sw: PostgresWriter, + collection: scala.collection.Seq[NewTuple[T]], + arrayType: String, + toTuple: T => PostgresTuple + ): PGobject = { + if(collection.nonEmpty) { + toParameter( + sw, + arrayType, + collection.map { tuple => + Array[PostgresTuple]( + IntConverter.toTuple(tuple.index), + IntConverter.toTuple(tuple.element), + toTuple(tuple.value)) + }) + } else { + emptyPGO(arrayType) + } + } + + def toParameterDiff[T <: Equality[T] with Identifiable]( + sw: PostgresWriter, + collection: scala.collection.Seq[CollectionDiff[T]], + arrayType: String, + toTuple: T => PostgresTuple + ): PGobject = { + if(collection.nonEmpty) { + toParameter( + sw, + arrayType, + collection.map { it => + Array[PostgresTuple]( + IntConverter.toTuple(it.index), + IntConverter.toTuple(it.element), + if (it.oldValue.isEmpty) PostgresTuple.NULL else toTuple(it.oldValue.get), + if (it.changedValue.isEmpty) PostgresTuple.NULL else toTuple(it.changedValue.get), + if (it.newValue.isEmpty) PostgresTuple.NULL else toTuple(it.newValue.get), + BoolConverter.toTuple(it.isNew)) + }) + } else { + emptyPGO(arrayType) + } + } + }