diff --git a/extensions/data-types/java8-time/build.gradle.kts b/extensions/data-types/java8-time/build.gradle.kts index 6cb3359306..4e0a07c20a 100644 --- a/extensions/data-types/java8-time/build.gradle.kts +++ b/extensions/data-types/java8-time/build.gradle.kts @@ -35,7 +35,7 @@ repositories { dependencies { compile(rootProject) - testCompile(rootProject) + testCompile(rootProject.files("/out/test/classes")) } diff --git a/extensions/data-types/java8-time/src/main/kotlin/org/jetbrains/exposed/extensions/dataTypes/javaTime/JavaLocalDateColumnType.kt b/extensions/data-types/java8-time/src/main/kotlin/org/jetbrains/exposed/extensions/dataTypes/javaTime/JavaLocalDateColumnType.kt index 4d5cfb34b2..1da5589b29 100644 --- a/extensions/data-types/java8-time/src/main/kotlin/org/jetbrains/exposed/extensions/dataTypes/javaTime/JavaLocalDateColumnType.kt +++ b/extensions/data-types/java8-time/src/main/kotlin/org/jetbrains/exposed/extensions/dataTypes/javaTime/JavaLocalDateColumnType.kt @@ -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 = 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 = registerColumn(name, JavaLocalDateColumnType(DateType.DATETIME)) private val DEFAULT_DATE_STRING_FORMATTER = DateTimeFormatter.ofPattern("YYYY-MM-dd", Locale.ROOT) @@ -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) diff --git a/extensions/data-types/java8-time/src/test/kotlin/org/jetbrains/exposed/extensions/dataTypes/javaTime/MappingTest.kt b/extensions/data-types/java8-time/src/test/kotlin/org/jetbrains/exposed/extensions/dataTypes/javaTime/MappingTest.kt index 041d6c9683..8b5fb0e60c 100644 --- a/extensions/data-types/java8-time/src/test/kotlin/org/jetbrains/exposed/extensions/dataTypes/javaTime/MappingTest.kt +++ b/extensions/data-types/java8-time/src/test/kotlin/org/jetbrains/exposed/extensions/dataTypes/javaTime/MappingTest.kt @@ -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") diff --git a/extensions/data-types/joda-time/src/main/kotlin/org/jetbrains/exposed/extensions/dataTypes/joda/JodaDateTimeColumnType.kt b/extensions/data-types/joda-time/src/main/kotlin/org/jetbrains/exposed/extensions/dataTypes/joda/JodaDateTimeColumnType.kt index 40475df1d8..4ea49a0da6 100644 --- a/extensions/data-types/joda-time/src/main/kotlin/org/jetbrains/exposed/extensions/dataTypes/joda/JodaDateTimeColumnType.kt +++ b/extensions/data-types/joda-time/src/main/kotlin/org/jetbrains/exposed/extensions/dataTypes/joda/JodaDateTimeColumnType.kt @@ -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 = 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 = registerColumn(name, JodaDateColumnType(DateType.DATETIME)) @@ -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) diff --git a/src/main/kotlin/org/jetbrains/exposed/sql/DateApi.kt b/src/main/kotlin/org/jetbrains/exposed/sql/DateApi.kt index b06e725c8a..6a95262475 100644 --- a/src/main/kotlin/org/jetbrains/exposed/sql/DateApi.kt +++ b/src/main/kotlin/org/jetbrains/exposed/sql/DateApi.kt @@ -55,7 +55,7 @@ object DefaultDateSPI : DateApi() { 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) diff --git a/src/main/kotlin/org/jetbrains/exposed/sql/vendors/H2.kt b/src/main/kotlin/org/jetbrains/exposed/sql/vendors/H2.kt index 06abd8e1d8..ea271ad533 100644 --- a/src/main/kotlin/org/jetbrains/exposed/sql/vendors/H2.kt +++ b/src/main/kotlin/org/jetbrains/exposed/sql/vendors/H2.kt @@ -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() { @@ -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, Any?>>, transaction: Transaction): String { diff --git a/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/DDLTests.kt b/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/DDLTests.kt index 684aa0aa10..28d09ebb33 100644 --- a/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/DDLTests.kt +++ b/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/DDLTests.kt @@ -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, " + @@ -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) } } diff --git a/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/DMLTests.kt b/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/DMLTests.kt index 6ec79beabb..4316f7319c 100644 --- a/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/DMLTests.kt +++ b/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/DMLTests.kt @@ -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 @@ -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) @@ -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("defaultDateTime").defaultExpression(DefaultDateSPI.CurrentDateTime()) } - val nonDefaultDate = DateTime.parse("2000-01-01") + val nonDefaultDate = Date(2000,1,1) withTables(foo) { val id = foo.insertAndGetId {