@@ -4,8 +4,12 @@ import com.github.navikt.tbd_libs.test_support.DatabaseContainers
44import java.sql.Connection
55import java.sql.ResultSet
66import java.time.Instant
7+ import java.time.LocalDate
8+ import java.time.LocalDateTime
79import java.time.OffsetDateTime
10+ import java.time.ZoneOffset
811import java.time.temporal.ChronoUnit.MILLIS
12+ import java.time.temporal.Temporal
913import java.util.UUID
1014import javax.sql.DataSource
1115import org.intellij.lang.annotations.Language
@@ -183,22 +187,64 @@ class QueryTest {
183187 }
184188
185189 @Test
186- fun `setter tidspunkt` () = setupTest { connection ->
190+ fun `parameter - localdate` () = setupTest { connection ->
191+ @Language(" PostgreSQL" )
192+ val sql = """ create table datotest ( dato date )"""
193+ connection.createStatement().execute(sql)
194+
195+ val dato = LocalDate .now()
196+ connection.prepareStatementWithNamedParameters(" insert into datotest (dato) values (:dato)" ) {
197+ withParameter(" dato" , dato)
198+ }.use { it.execute() }
199+
200+ val result = connection.prepareStatement(" select dato from datotest" ).use {
201+ it.executeQuery().single { rs -> rs.getObject(" dato" , LocalDate ::class .java) }
202+ }
203+
204+ assertEquals(dato, result)
205+ }
206+
207+ @Test
208+ fun `parameter - instant - timestamptz` () = setupTest { connection ->
187209 val instant = Instant .now()
188- connection.prepareStatementWithNamedParameters(" insert into name (name, created ) values (:navn, :tidspunkt)" ) {
210+ connection.prepareStatementWithNamedParameters(" insert into name (name, created_tz ) values (:navn, :tidspunkt)" ) {
189211 withParameter(" navn" , " trude" )
190212 withParameter(" tidspunkt" , instant)
191213 }.execute()
192214
193- val tidspunkt = connection.prepareStatementWithNamedParameters(" select created from name where name = :navn" ) {
215+ val tidspunkt = connection.prepareStatementWithNamedParameters(" select created_tz from name where name = :navn" ) {
194216 withParameter(" navn" , " trude" )
195217 }.use {
196- it.executeQuery().single { rs -> rs.getObject(" created " , OffsetDateTime ::class .java) }
218+ it.executeQuery().single { rs -> rs.getObject(" created_tz " , OffsetDateTime ::class .java) }
197219 }.toInstant()
198220
199221 assertEquals(instant.truncatedTo(MILLIS ), tidspunkt.truncatedTo(MILLIS ))
200222 }
201223
224+ @Test
225+ fun `parameter - instant - timestamp` () = setupTest { connection ->
226+ val instant = Instant .now()
227+ connection.prepareStatementWithNamedParameters(" insert into name (name, created_ts) values (:navn, :tidspunkt)" ) {
228+ withParameter(" navn" , " trude" )
229+ withParameter(" tidspunkt" , instant)
230+ }.execute()
231+
232+ fun <T : Temporal > hentTidspunkt (mapper : (ResultSet ) -> T ): T {
233+ return connection.prepareStatementWithNamedParameters(" select created_ts from name where name = :navn" ) {
234+ withParameter(" navn" , " trude" )
235+ }.use {
236+ it.executeQuery().single(mapper)
237+ }
238+ }
239+
240+ hentTidspunkt { rs -> rs.getObject(" created_ts" , OffsetDateTime ::class .java) }
241+ .toInstant()
242+ .also { tidspunkt -> assertEquals(instant.truncatedTo(MILLIS ), tidspunkt.truncatedTo(MILLIS )) }
243+
244+ hentTidspunkt { rs -> rs.getObject(" created_ts" , LocalDateTime ::class .java) }
245+ .toInstant(ZoneOffset .UTC )
246+ .also { tidspunkt -> assertEquals(instant.truncatedTo(MILLIS ), tidspunkt.truncatedTo(MILLIS )) }
247+ }
202248
203249 @Test
204250 fun `named parameters` () {
@@ -231,7 +277,8 @@ class QueryTest {
231277 val sql = """ create table name (
232278 id bigint primary key generated always as identity,
233279 name text,
234- created timestamptz not null default now()
280+ created_tz timestamptz not null default now(),
281+ created_ts timestamp not null default now()
235282 )"""
236283 createStatement().execute(sql)
237284 }
0 commit comments