Skip to content

Commit

Permalink
revised arrayPutUsing string implicit conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
TebaleloS committed Jun 26, 2024
1 parent 3c2ca00 commit 0f2b220
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ object DoobieImplicits {
.tcontramap { a =>
val o = new PGobject
o.setType("json[]")
o.setValue(a.map(x => s"\"${x.noSpaces.replaceAll("\"", """\"""")}\"").mkString("{", ",", "}"))
val arrayElements = a.map { x =>
// Convert to compact JSON string and escape inner quotes
val escapedJsonString = x.noSpaces.replace("\"", "\\\"")
// Wrap in double quotes for the array element
s"\"$escapedJsonString\""
}
// Join all elements into a single string wrapped in curly braces
o.setValue(arrayElements.mkString("{", ",", "}"))
o
}
}
Expand Down Expand Up @@ -109,7 +116,14 @@ object DoobieImplicits {
.tcontramap { a =>
val o = new PGobject
o.setType("jsonb[]")
o.setValue(a.map(x => s"\"${x.noSpaces.replaceAll("\"", """\"""")}\"").mkString("{", ",", "}"))
val arrayElements = a.map { x =>
// Convert to compact JSON string and escape inner quotes
val escapedJsonString = x.noSpaces.replace("\"", "\\\"")
// Wrap in double quotes for the array element
s"\"$escapedJsonString\""
}
// Join all elements into a single string wrapped in curly braces
o.setValue(arrayElements.mkString("{", ",", "}"))
o
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class WriteCheckpoint(implicit schema: DBSchema, dbEngine: DoobieEngine[Task])
values.measurements.toList.map(_.asJson)
}

val sqlDebug = sql"""SELECT ${Fragment.const(selectEntry)} FROM ${Fragment.const(functionName)}(
sql"""SELECT ${Fragment.const(selectEntry)} FROM ${Fragment.const(functionName)}(
${
import za.co.absa.atum.server.api.database.DoobieImplicits.Jsonb.jsonbPutUsingString
partitioningNormalized
Expand All @@ -68,8 +68,6 @@ class WriteCheckpoint(implicit schema: DBSchema, dbEngine: DoobieEngine[Task])
${values.measuredByAtumAgent},
${values.author}
) ${Fragment.const(alias)};"""
println(sqlDebug)
sqlDebug
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ object WriteCheckpointIntegrationTests extends ConfigProviderTest {
id = UUID.randomUUID(),
name = "name",
author = "author",
partitioning = Seq(PartitionDTO("key2", "value2")),
partitioning = Seq(PartitionDTO("key4", "value4")),
processStartTime = ZonedDateTime.now(),
processEndTime = Option(ZonedDateTime.now()),
measurements =
Expand Down

0 comments on commit 0f2b220

Please sign in to comment.