Skip to content

Commit

Permalink
implement postgres timestamptz, int[], text[], jsonb columns
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan Liska authored and Juan Liska committed Nov 4, 2018
1 parent c287546 commit b2caebc
Show file tree
Hide file tree
Showing 13 changed files with 235 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import java.time.*
import kotlin.reflect.*

open class JDBCDataConversion {
open fun convertValueToDatabase(value: Any?): Any? {
open fun convertValueToDatabase(value: Any?, connection: Connection): Any? {
if (value == null)
return null
return when (value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ open class JDBCTransaction(override val connection: JDBCConnection) : Transactio
prepareStatement(statementSQL.sql, arrayOf(connection.dialect.idSQL(returnColumn.name)))

statementSQL.arguments.forEach { arg ->
preparedStatement.setObject(arg.index + 1, connection.conversion.convertValueToDatabase(arg.value))
preparedStatement.setObject(arg.index + 1, connection.conversion.convertValueToDatabase(arg.value, preparedStatement.connection))
}
return preparedStatement
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ package org.jetbrains.squash.dialects.mysql
import org.jetbrains.squash.connection.*
import org.jetbrains.squash.drivers.*
import java.nio.*
import java.sql.Connection
import java.util.*
import kotlin.reflect.*


class MySqlDataConversion : JDBCDataConversion() {
override fun convertValueToDatabase(value: Any?): Any? {
override fun convertValueToDatabase(value: Any?, connection: Connection): Any? {
if (value is UUID) {
val bb = ByteBuffer.wrap(ByteArray(16))
bb.putLong(value.mostSignificantBits)
bb.putLong(value.leastSignificantBits)
return bb.array()
}
return super.convertValueToDatabase(value)
return super.convertValueToDatabase(value, connection)
}

override fun convertValueFromDatabase(value: Any?, type: KClass<*>): Any? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@ import java.time.OffsetDateTime
/**
* Creates a [OffsetDateTime] column
*/
fun TableDefinition.offsetdatetime(name: String): ColumnDefinition<OffsetDateTime> {
fun TableDefinition.offsetDatetime(name: String): ColumnDefinition<OffsetDateTime> {
return createColumn(name, OffsetDateTimeColumnType)
}

/**
* Creates a [Int[]] column
*/
fun TableDefinition.intArray(name: String): ColumnDefinition<Array<Int>> {
return createColumn(name, IntArrayColumnType)
}

/**
* Creates a [String[]] column
*/
fun TableDefinition.textArray(name: String): ColumnDefinition<Array<String>> {
return createColumn(name, TextArrayColumnType)
}

/**
* Creates a [Json] column
*/
fun TableDefinition.jsonb(name: String): ColumnDefinition<Json> {
return createColumn(name, JsonbColumnType)
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ package org.jetbrains.squash.definition

import java.time.OffsetDateTime

object OffsetDateTimeColumnType : ColumnType(OffsetDateTime::class)
object OffsetDateTimeColumnType : ColumnType(OffsetDateTime::class)
object IntArrayColumnType : ColumnType(Array<Int>::class)
object TextArrayColumnType : ColumnType(Array<String>::class)
object JsonbColumnType : ColumnType(Json::class)
3 changes: 3 additions & 0 deletions squash-postgres/src/org/jetbrains/squash/definition/Json.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package org.jetbrains.squash.definition

data class Json(val json: String?)
Original file line number Diff line number Diff line change
@@ -1,21 +1,63 @@
package org.jetbrains.squash.dialects.postgres

import org.jetbrains.squash.drivers.*
import org.jetbrains.squash.definition.Json
import org.jetbrains.squash.drivers.JDBCDataConversion
import org.jetbrains.squash.drivers.JDBCResponseColumn
import org.postgresql.util.PGobject
import java.sql.Connection
import java.sql.ResultSet
import java.time.OffsetDateTime
import kotlin.reflect.KClass

class PgDataConversion : JDBCDataConversion() {
override fun convertValueToDatabase(value: Any?): Any? {
return when (value) {
value is OffsetDateTime -> value
else -> super.convertValueToDatabase(value)
override fun convertValueToDatabase(value: Any?, connection: Connection): Any? {
return when {
value is OffsetDateTime -> { value }
Array<Int>::class.isInstance(value) -> {
val typedArray = value as Array<Int>
connection.createArrayOf("int4", typedArray)
}
Array<String>::class.isInstance(value) -> {
val typedArray = value as Array<String>
connection.createArrayOf("text", typedArray)
}
Json::class.isInstance(value) -> {
val json = value as Json
PGobject().apply {
this.type = "json"
this.value = json.json
}
}
else -> super.convertValueToDatabase(value, connection)
}
}

override fun convertValueFromDatabase(value: Any?, type: KClass<*>): Any? {
return when (value) {
value is OffsetDateTime -> value
type == java.time.OffsetDateTime::class && value is java.time.OffsetDateTime -> { value }
type == kotlin.Array<Int>::class && value is java.sql.Array -> {
val array = value as java.sql.Array
array.array as Array<Int>
}
type == kotlin.Array<String>::class && value is java.sql.Array -> {
val array = value as java.sql.Array
array.array as Array<String>
}
type == Json::class && value is Json -> {
val json = value as Json
json.json
}
else -> super.convertValueFromDatabase(value, type)
}
}

override fun fetch(resultSet: ResultSet, dbColumnIndex: Int, column: JDBCResponseColumn): Any? {
return when (column.databaseType) {
"_int4" -> resultSet.getArray(dbColumnIndex)?.array as Array<Int>?
"_text" -> resultSet.getArray(dbColumnIndex)?.array as Array<String>?
"timestamptz" -> resultSet.getObject(dbColumnIndex, OffsetDateTime::class.java)
"json" -> Json(resultSet.getString(dbColumnIndex))
else -> super.fetch(resultSet, dbColumnIndex, column)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package org.jetbrains.squash.dialects.postgres

import org.jetbrains.squash.definition.*
import org.jetbrains.squash.dialect.*
import org.jetbrains.squash.dialect.BaseDefinitionSQLDialect
import org.jetbrains.squash.dialect.BaseSQLDialect
import org.jetbrains.squash.dialect.DefinitionSQLDialect
import org.jetbrains.squash.dialect.SQLStatementBuilder
import org.jetbrains.squash.expressions.Expression
import org.jetbrains.squash.expressions.ArrayInExpression
import org.jetbrains.squash.expressions.ArrayOverlapExpression

object PgDialect : BaseSQLDialect("Postgres") {
override val definition: DefinitionSQLDialect = object : BaseDefinitionSQLDialect(this) {
Expand Down Expand Up @@ -33,8 +39,42 @@ object PgDialect : BaseSQLDialect("Postgres") {
is BinaryColumnType -> builder.append("BYTEA")
is DateTimeColumnType -> builder.append("TIMESTAMP")
is OffsetDateTimeColumnType -> builder.append("TIMESTAMP WITH TIME ZONE")
is IntArrayColumnType -> builder.append("INT[]")
is TextArrayColumnType -> builder.append("TEXT[]")
is JsonbColumnType -> builder.append("JSONB")
else -> super.columnTypeSQL(builder, type)
}
}
}

override fun <T> appendExpression(builder: SQLStatementBuilder, expression: Expression<T>): Unit = with(builder) {
when (expression) {
is ArrayInExpression<*> -> {
appendExpression(this, expression.value)
append(" @> ARRAY[")
expression.values.forEachIndexed { index, value ->
if (index > 0)
append(", ")
appendLiteralSQL(this, value)
}
append("]")
}
is ArrayOverlapExpression<*> -> {
appendExpression(this, expression.value)
append(" && ARRAY[")
var anyValueIsString = false
expression.values.forEachIndexed { index, value ->
if (index > 0)
append(", ")
appendLiteralSQL(this, value)
if (value is String)
anyValueIsString = true
}
append("]")
if (anyValueIsString)
append("::text[]")
}
else -> super.appendExpression(builder, expression)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.jetbrains.squash.expressions

class ArrayInExpression<out V>(val value: Expression<V>, val values: Collection<V>) : Expression<Boolean>
class ArrayOverlapExpression<out V>(val value: Expression<V>, val values: Collection<V>) : Expression<Boolean>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.jetbrains.squash.expressions

infix fun <V> Expression<V>.contains(values: Collection<V>): ArrayInExpression<V> = ArrayInExpression(this, values)
infix fun <V> Expression<V>.containsAny(values: Collection<V>): ArrayOverlapExpression<V> = ArrayOverlapExpression(this, values)
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ class PgAllColumnTypesTests : AllColumnTypesTests(), DatabaseTests by PgDatabase
"\"binary\" BYTEA NOT NULL, " +
"\"blob\" BYTEA NOT NULL, " +
"uuid UUID NOT NULL, " +
"offsetdatetime TIMESTAMP WITH TIME ZONE NOT NULL, " +
"CONSTRAINT PK_AllColumnTypes PRIMARY KEY (id))"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.jetbrains.squash.dialects.postgres.tests

import org.jetbrains.squash.definition.*

object PgDialectColumnTypes : TableDefinition() {
val id = integer("id").autoIncrement().primaryKey()
val offsetdatetime = offsetDatetime("offsetdatetime")
val notnullIntarray = intArray("intarray")
val nullableIntarray = intArray("nullable_intarray").nullable()
val notnullTextarray = textArray("textarray")
val nullableTextarray = textArray("nullable_textarray").nullable()
val notnullJsonb = jsonb("notnull_jsonb")
val nullableJsonb = jsonb("nullable_jsonb").nullable()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.jetbrains.squash.dialects.postgres.tests

import org.jetbrains.squash.connection.Transaction
import org.jetbrains.squash.definition.IntColumnType
import org.jetbrains.squash.definition.Json
import org.jetbrains.squash.expressions.contains
import org.jetbrains.squash.expressions.containsAny
import org.jetbrains.squash.query.from
import org.jetbrains.squash.query.where
import org.jetbrains.squash.results.ResultRow
import org.jetbrains.squash.results.get
import org.jetbrains.squash.statements.insertInto
import org.jetbrains.squash.statements.values
import org.jetbrains.squash.tests.DatabaseTests
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util.*
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
import kotlin.test.assertTrue

class PgDialectColumnTypesTests: DatabaseTests by PgDatabaseTests() {
private val dialectColumnsTableSQL: String get() = "CREATE TABLE IF NOT EXISTS PgDialectColumnTypes (" +
"id ${getIdColumnType(IntColumnType)}, " +
"offsetdatetime TIMESTAMP WITH TIME ZONE NOT NULL, " +
"intarray INT[] NOT NULL, " +
"nullable_intarray INT[] NULL, " +
"textarray TEXT[] NOT NULL, " +
"nullable_textarray TEXT[] NULL, " +
"notnull_jsonb JSONB NOT NULL, " +
"nullable_jsonb JSONB NULL, " +
"CONSTRAINT PK_PgDialectColumnTypes PRIMARY KEY (id))"

@Test fun sql() {
withTransaction {
connection.dialect.definition.tableSQL(PgDialectColumnTypes).assertSQL { dialectColumnsTableSQL }
}
}

@Test fun insert() {
withTables(PgDialectColumnTypes) {
insertData()
}
}

private val offsetDate = OffsetDateTime.of(1976, 11, 24, 12, 1, 1, 0, ZoneOffset.ofHours(-6))
private val arrayOfInt = arrayOf(1, 2, 3, 4)
private val arrayOfString = arrayOf("see", "spot", "run")
private val jsonb = Json("{}")

@Test fun query() {
withTables(PgDialectColumnTypes) {
insertData()

fun checkRow(row: ResultRow) {
assertEquals(OffsetDateTime::class.java, row[PgDialectColumnTypes.offsetdatetime].javaClass)
assertEquals(offsetDate.toEpochSecond(), row[PgDialectColumnTypes.offsetdatetime].toEpochSecond())

assertEquals(Array<Int>::class.java, row[PgDialectColumnTypes.notnullIntarray].javaClass)
assertTrue { Arrays.equals(arrayOfInt, row[PgDialectColumnTypes.notnullIntarray]) }

assertEquals(Array<String>::class.java, row[PgDialectColumnTypes.notnullTextarray].javaClass)
assertTrue { Arrays.equals(arrayOfString, row[PgDialectColumnTypes.notnullTextarray]) }
}

val containsRow = from(PgDialectColumnTypes).where { PgDialectColumnTypes.notnullIntarray contains arrayOfInt.take(2) }.execute().single()
checkRow(containsRow)

val containsAnyRow = from(PgDialectColumnTypes).where { PgDialectColumnTypes.notnullTextarray containsAny listOf(arrayOfString.first(), "other")}.execute().single()
checkRow(containsAnyRow)

val noRow = from(PgDialectColumnTypes).where { PgDialectColumnTypes.notnullIntarray contains listOf(9) }.execute().singleOrNull()
assertNull(noRow)
}
}

private fun Transaction.insertData() {
insertInto(PgDialectColumnTypes).values {
it[offsetdatetime] = offsetDate
it[notnullIntarray] = arrayOfInt
it[nullableIntarray] = null
it[notnullTextarray] = arrayOfString
it[nullableTextarray] = arrayOfString
it[notnullJsonb] = jsonb
it[nullableJsonb] = null
}.execute()
}
}

0 comments on commit b2caebc

Please sign in to comment.