Skip to content

Commit

Permalink
After merge fixes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
Tapac committed Jun 17, 2018
1 parent e269796 commit 4cb3dc7
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 18 deletions.
2 changes: 1 addition & 1 deletion extensions/data-types/java8-time/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ repositories {

dependencies {
compile(rootProject)
testCompile(rootProject)
testCompile(rootProject.files("/out/test/classes"))
}


Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,18 @@ import java.time.format.DateTimeFormatter
import java.util.*


/**
* A date column to store a date.
*
* @param name The column name
*/
fun Table.date(name: String): Column<LocalDateTime> = registerColumn(name, JavaLocalDateColumnType(DateType.DATE))

/**
* A datetime column to store both a date and a time.
*
* @param name The column name
*/
fun Table.datetime(name: String): Column<LocalDateTime> = registerColumn(name, JavaLocalDateColumnType(DateType.DATETIME))

private val DEFAULT_DATE_STRING_FORMATTER = DateTimeFormatter.ofPattern("YYYY-MM-dd", Locale.ROOT)
Expand All @@ -30,7 +41,7 @@ class JavaLocalDateColumnType(type: DateType): DateColumnType(type) {
is LocalDateTime -> value
is java.sql.Date -> value.time.millisToLocalDateTimeUTC()
is java.sql.Timestamp -> value.time.millisToLocalDateTimeUTC()
else -> error("Unexpected value: $value")
else -> error("Unexpected value: $value of ${value::class.qualifiedName}")
}

return if (type == DateType.DATETIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.junit.Test
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import kotlin.test.assertEquals

object JavaTimeTable : Table() {
val dateColumn = date("dateColumn")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
package org.jetbrains.exposed.extensions.dataTypes.joda

import org.jetbrains.exposed.sql.*
import org.joda.time.format.DateTimeFormat
import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.DateColumnType
import org.jetbrains.exposed.sql.DateType
import org.jetbrains.exposed.sql.Table
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
import org.joda.time.format.DateTimeFormat
import org.joda.time.format.ISODateTimeFormat
import java.util.*


/**
* A date column to store a date.
*
* @param name The column name
*/
fun Table.date(name: String): Column<DateTime> = registerColumn(name, JodaDateColumnType(DateType.DATE))

/**
* A datetime column to store both a date and a time.
*
* @param name The column name
*/
fun Table.datetime(name: String): Column<DateTime> = registerColumn(name, JodaDateColumnType(DateType.DATETIME))


Expand All @@ -26,7 +39,7 @@ class JodaDateColumnType(type: DateType): DateColumnType(type) {
is DateTime -> value
is java.sql.Date -> DateTime(value.time)
is java.sql.Timestamp -> DateTime(value.time)
else -> error("Unexpected value: $value")
else -> error("Unexpected value: $value of ${value::class.qualifiedName}")
}

return if (type == DateType.DATETIME)
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/org/jetbrains/exposed/sql/DateApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ object DefaultDateSPI : DateApi<java.util.Date>() {
is java.util.Date -> value
is java.sql.Date -> java.util.Date(value.time)
is java.sql.Timestamp -> java.util.Date(value.time)
else -> error("Unexpected value: $value")
else -> error("Unexpected value: $value of ${value::class.qualifiedName}")
}

return if (type == DateType.DATETIME)
Expand Down
9 changes: 5 additions & 4 deletions src/main/kotlin/org/jetbrains/exposed/sql/vendors/H2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.jetbrains.exposed.exceptions.throwUnsupportedException
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.TransactionManager
import java.sql.Wrapper
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*

internal object H2DataTypeProvider : DataTypeProvider() {
Expand All @@ -20,11 +20,12 @@ internal object H2FunctionProvider : FunctionProvider() {

private val isMySQLMode: Boolean get() = currentMode() == "MySQL"

private val RELEASE_DATE_1_4_197 = DateFormat.getDateInstance().parse("2018-03-18")

private val dateFormat = SimpleDateFormat("YYYY-MM-dd")
private val RELEASE_DATE_1_4_197 = dateFormat.parse("2018-03-18")

private fun dbReleaseDate(transaction: Transaction) : Date {
val releaseDate = transaction.db.metadata.databaseProductVersion.substringAfterLast('(').substringBeforeLast(')')
return DateFormat.getDateInstance().parse(releaseDate)
return dateFormat.parse(releaseDate)
}

override fun replace(table: Table, data: List<Pair<Column<*>, Any?>>, transaction: Transaction): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ class DDLTests : DatabaseTestsBase() {

withTables(TestTable) {
val dtType = currentDialect.dataTypeProvider.dateTimeType(DateType.DATETIME)
val dType = currentDialect.dataTypeProvider.dateTimeType(DateType.DATE)
assertEquals("CREATE TABLE " + if (currentDialect.supportsIfNotExists) { "IF NOT EXISTS " } else { "" } +
"${"t".inProperCase()} (" +
"${"s".inProperCase()} VARCHAR(100) DEFAULT 'test' NOT NULL, " +
Expand All @@ -228,7 +229,7 @@ class DDLTests : DatabaseTestsBase() {
"${"t1".inProperCase()} $dtType ${currentDT.itOrNull()}, " +
"${"t2".inProperCase()} $dtType ${nowExpression.itOrNull()}, " +
"${"t3".inProperCase()} $dtType ${dtLiteral.itOrNull()}, " +
"${"t4".inProperCase()} DATE ${dtLiteral.itOrNull()}" +
"${"t4".inProperCase()} $dType ${dtLiteral.itOrNull()}" +
")", TestTable.ddl)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import org.jetbrains.exposed.sql.Function
import org.jetbrains.exposed.sql.Random
import org.jetbrains.exposed.sql.statements.BatchDataInconsistentException
import org.jetbrains.exposed.sql.statements.BatchInsertStatement
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.vendors.*
import org.jetbrains.exposed.sql.tests.*
import org.joda.time.DateTime
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.vendors.OracleDialect
import org.jetbrains.exposed.sql.vendors.PostgreSQLDialect
import org.jetbrains.exposed.sql.vendors.SQLServerDialect
import org.jetbrains.exposed.sql.vendors.currentDialect
import org.junit.Assert.assertThat
import org.junit.Test
import java.math.BigDecimal
Expand Down Expand Up @@ -849,7 +851,7 @@ class DMLTests : DatabaseTestsBase() {
}
}

private fun DMLTestsData.Misc.checkRow(row: ResultRow, n: Int, nn: Int?, d: Date, dn: Date?, t: Date, tn: Date?, e: DMLTestsData.E, en: DMLTestsData.E?, es: DMLTestsData.E, esn: DMLTestsData.E?, s: String, sn: String?, dc: BigDecimal, dcn: BigDecimal?) {
private fun DMLTestsData.Misc.checkRow(row: ResultRow, n: Int, nn: Int?, d: Date, dn: Date?, t: Date, tn: Date?, e: DMLTestsData.E, en: DMLTestsData.E?, es: DMLTestsData.E, esn: DMLTestsData.E?, s: String, sn: String?, dc: BigDecimal, dcn: BigDecimal?, fcn: Float?) {
assertEquals(row[this.n], n)
assertEquals(row[this.nn], nn)
assertEqualDateTime(row[this.d], d)
Expand Down Expand Up @@ -1419,10 +1421,10 @@ class DMLTests : DatabaseTestsBase() {
fun testDefaultExpressions02() {
val foo = object : IntIdTable("foo") {
val name = text("name")
val defaultDateTime = datetime("defaultDateTime").defaultExpression(CurrentDateTime())
val defaultDateTime = datetime<Date>("defaultDateTime").defaultExpression(DefaultDateSPI.CurrentDateTime())
}

val nonDefaultDate = DateTime.parse("2000-01-01")
val nonDefaultDate = Date(2000,1,1)

withTables(foo) {
val id = foo.insertAndGetId {
Expand Down

0 comments on commit 4cb3dc7

Please sign in to comment.