Skip to content

Commit 1fc5e22

Browse files
committed
Implementer get/setPeriodeArray
1 parent da5eaa2 commit 1fc5e22

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

dbconnect/src/main/kotlin/no/nav/aap/komponenter/dbconnect/Params.kt

+5
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,9 @@ public class Params internal constructor(
105105
val array = connection.createArrayOf("BIGINT", longs.toTypedArray())
106106
preparedStatement.setArray(index, array)
107107
}
108+
109+
public fun setPeriodeArray(index: Int, perioder: List<Periode>) {
110+
val array = connection.createArrayOf("daterange", perioder.map(DaterangeParser::toSQL).toTypedArray())
111+
preparedStatement.setArray(index, array)
112+
}
108113
}

dbconnect/src/main/kotlin/no/nav/aap/komponenter/dbconnect/Row.kt

+5
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ public class Row internal constructor(private val resultSet: ResultSet) {
162162
return DaterangeParser.fromSQL(dateRange)
163163
}
164164

165+
public fun getPeriodeArray(columnLabel: String): List<Periode> {
166+
return (resultSet.getArray(columnLabel).array as Array<*>)
167+
.map { DaterangeParser.fromSQL(it.toString()) }
168+
}
169+
165170
/**
166171
* Feltet [elementType] må være av en type JDBC returnerer.
167172
* Example:

dbconnect/src/test/kotlin/no/nav/aap/komponenter/dbconnect/ParamsOgRowTest.kt

+29
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,35 @@ internal class ParamsOgRowTest {
402402
}
403403
}
404404

405+
@Test
406+
fun `Skriver og leser med periode-array`() {
407+
InitTestDatabase.dataSource.transaction { connection ->
408+
val perioderInn = listOf(
409+
Periode(LocalDate.of(1990, 6, 1), LocalDate.of(2200, 1, 1)),
410+
Periode(LocalDate.of(2020, 1, 5), LocalDate.of(2020, 5, 3)),
411+
)
412+
connection.execute(
413+
"""
414+
INSERT INTO TEST_PERIODE_ARRAY(TEST)
415+
VALUES (?::daterange[])
416+
""",
417+
) {
418+
setParams {
419+
setPeriodeArray(1, perioderInn)
420+
}
421+
}
422+
423+
424+
val perioderUt = connection.queryFirst("SELECT TEST FROM TEST_PERIODE_ARRAY") {
425+
setRowMapper { row ->
426+
row.getPeriodeArray("TEST")
427+
}
428+
}
429+
assertThat(perioderUt)
430+
.isEqualTo(perioderInn)
431+
}
432+
}
433+
405434
@Test
406435
fun `CURRENT_TIMESTAMP i postgres (UTC) matcher LocalDateTime now() i pod (Europe Oslo)`() {
407436
InitTestDatabase.dataSource.transaction { connection ->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE TEST_PERIODE_ARRAY
2+
(
3+
TEST daterange[] NOT NULL
4+
);
5+

0 commit comments

Comments
 (0)