Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ cache:
script:
# - ./gradlew build -Pnetty_version=4.0.56.Final
# - ./gradlew clean build -x test
# - ./gradlew :mysq-async:test --tests com.github.jasync.sql.db.mysql.QueryTimeoutSpec --info
- ./gradlew :mysql-async:test --tests com.github.jasync.sql.db.mysql.PreparedStatementsSpec --info
- ./resources/run-docker-memsql.sh
- ./gradlew clean build
- ./resources/detect-leak.sh mysql-async/target/mysql-async-tests.log
Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ dependencies {
This project is a port of [mauricio/postgresql-async](https://github.com/mauricio/postgresql-async) to Kotlin.
Why? Because the original lib is not maintained anymore, We use it in [ob1k](https://github.com/outbrain/ob1k), and would like to remove the Scala dependency in ob1k.

This project always returns [JodaTime](http://joda-time.sourceforge.net/) when dealing with date types and not the
`java.util.Date` class. (We plan to move to jdk-8 dates).

If you want information specific to the drivers, check the [PostgreSQL README](postgresql-async/README.md) and the
[MySQL README](mysql-async/README.md).

Expand Down
2 changes: 0 additions & 2 deletions db-async-common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ dependencies {
api "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinx_coroutines_version"
implementation "org.slf4j:slf4j-api:$sl4j_version"
implementation "joda-time:joda-time:$joda_version"
implementation "org.joda:joda-convert:$joda_convert_version"
implementation "io.netty:netty-transport:$netty_version"
compileOnly "io.netty:netty-transport-native-epoll:$netty_version:linux-x86_64"
compileOnly "io.netty:netty-transport-native-kqueue:$netty_version:osx-x86_64"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.jasync.sql.db

import com.github.jasync.sql.db.util.XXX
import org.joda.time.LocalDateTime
import java.time.LocalDateTime

/**
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package com.github.jasync.sql.db.column

import com.github.jasync.sql.db.exceptions.DateEncoderNotAvailableException
import org.joda.time.LocalDate
import org.joda.time.ReadablePartial
import org.joda.time.format.DateTimeFormat
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.time.temporal.TemporalAccessor

object DateEncoderDecoder : ColumnEncoderDecoder {

private const val ZeroedDate = "0000-00-00"

private val formatter = DateTimeFormat.forPattern("yyyy-MM-dd")
private val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")

override fun decode(value: String): LocalDate? =
if (ZeroedDate == value) {
null
} else {
this.formatter.parseLocalDate(value)
LocalDate.parse(value, this.formatter)
}

override fun encode(value: Any): String {
return when (value) {
is java.sql.Date -> this.formatter.print(LocalDate(value))
is ReadablePartial -> this.formatter.print(value)
is java.sql.Date -> value.toLocalDate().format(this.formatter)
is TemporalAccessor -> this.formatter.format(value)
else -> throw DateEncoderNotAvailableException(value)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
package com.github.jasync.sql.db.column

import sun.net.util.IPAddressUtil.textToNumericFormatV4
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was not compiling for me using JDK 11 (AdoptOpenJDK, windows), and generally using sun.* packages is discouraged - decided to replace with standard java library calls. Let me know if there's a reason this textToNumeric() call was made in the first place - seems like .getByName() works OK

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I checked it and this method not exists in java 8

Copy link
Author

@bakatz bakatz Sep 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, the textToNumeric methods aren't public/don't exist or something. That's why I decided to remove them.

import sun.net.util.IPAddressUtil.textToNumericFormatV6
import java.net.InetAddress

object InetAddressEncoderDecoder : ColumnEncoderDecoder {

override fun decode(value: String): Any {
return if (value.contains(':')) {
InetAddress.getByAddress(textToNumericFormatV6(value))
} else {
InetAddress.getByAddress(textToNumericFormatV4(value))
}
}
override fun decode(value: String): Any = InetAddress.getByName(value)

override fun encode(value: Any): String {
return (value as InetAddress).hostAddress
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
package com.github.jasync.sql.db.column

import org.joda.time.LocalDateTime
import org.joda.time.format.DateTimeFormatterBuilder
import java.time.LocalDateTime
import java.time.format.DateTimeFormatterBuilder

object LocalDateTimeEncoderDecoder : ColumnEncoderDecoder {

private const val ZeroedTimestamp = "0000-00-00 00:00:00"

private val optional = DateTimeFormatterBuilder()
.appendPattern(".SSSSSS").toParser()
.appendPattern(".SSSSSS").toFormatter()

private val format = DateTimeFormatterBuilder()
.appendPattern("yyyy-MM-dd HH:mm:ss")
.appendOptional(optional)
.toFormatter()

override fun encode(value: Any): String =
format.print(value as LocalDateTime)
override fun encode(value: Any): String = (value as LocalDateTime).format(format)

override fun decode(value: String): LocalDateTime? =
if (ZeroedTimestamp == value) {
null
} else {
format.parseLocalDateTime(value)
LocalDateTime.parse(value, format)
}

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package com.github.jasync.sql.db.column

import org.joda.time.LocalTime
import org.joda.time.format.DateTimeFormatterBuilder
import java.time.LocalTime
import java.time.format.DateTimeFormatterBuilder

object SQLTimeEncoder : ColumnEncoder {

private val format = DateTimeFormatterBuilder()
.appendPattern("HH:mm:ss")
.toFormatter()

override fun encode(value: Any): String {
val time = value as java.sql.Time

return format.print(LocalTime(time.time))
}
override fun encode(value: Any): String = (value as java.sql.Time).toLocalTime().format(format)
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.github.jasync.sql.db.column

import org.joda.time.LocalTime
import org.joda.time.format.DateTimeFormatterBuilder
import java.time.LocalTime
import java.time.format.DateTimeFormatter
import java.time.format.DateTimeFormatterBuilder


open class TimeEncoderDecoder : ColumnEncoderDecoder {
companion object {
val Instance = TimeEncoderDecoder()
}

private val optional = DateTimeFormatterBuilder()
.appendPattern(".SSSSSS").toParser()
.appendPattern(".SSSSSS").toFormatter()

private val format = DateTimeFormatterBuilder()
.appendPattern("HH:mm:ss")
Expand All @@ -20,12 +22,10 @@ open class TimeEncoderDecoder : ColumnEncoderDecoder {
.appendPattern("HH:mm:ss.SSSSSS")
.toFormatter()

open fun formatter() = format
open fun formatter(): DateTimeFormatter = format

override fun decode(value: String): LocalTime =
format.parseLocalTime(value)
override fun decode(value: String): LocalTime = LocalTime.parse(value, format)

override fun encode(value: Any): String =
this.printer.print(value as LocalTime)
override fun encode(value: Any): String = (value as LocalTime).format(printer)

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.github.jasync.sql.db.column

import org.joda.time.format.DateTimeFormat
import org.joda.time.format.DateTimeFormatter
import java.time.format.DateTimeFormatter

object TimeWithTimezoneEncoderDecoder : TimeEncoderDecoder() {

private val format = DateTimeFormat.forPattern("HH:mm:ss.SSSSSSZ")
private val format = DateTimeFormatter.ofPattern("HH:mm:ss.SSSSSSZ")

override fun formatter(): DateTimeFormatter = format

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.github.jasync.sql.db.column

import com.github.jasync.sql.db.exceptions.DateEncoderNotAvailableException
import org.joda.time.DateTime
import org.joda.time.LocalDateTime
import org.joda.time.ReadableDateTime
import org.joda.time.format.DateTimeFormatterBuilder
import java.sql.Timestamp
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.ZoneOffset
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatterBuilder
import java.time.temporal.Temporal
import java.time.temporal.TemporalAccessor
import java.util.*


Expand All @@ -18,9 +21,9 @@ open class TimestampEncoderDecoder : ColumnEncoderDecoder {
}

private val optional = DateTimeFormatterBuilder()
.appendPattern(MillisFormat).toParser()
.appendPattern(MillisFormat).toFormatter()
private val optionalTimeZone = DateTimeFormatterBuilder()
.appendPattern("Z").toParser()
.appendPattern("Z").toFormatter()

private val builder = DateTimeFormatterBuilder()
.appendPattern(BaseFormat)
Expand All @@ -38,16 +41,19 @@ open class TimestampEncoderDecoder : ColumnEncoderDecoder {
open fun formatter() = format

override fun decode(value: String): Any {
return formatter().parseLocalDateTime(value)
return LocalDateTime.parse(value, formatter())
}

// java.util.Dates are constructed using the system default timezone, replicate this behavior when encoding a legacy date
private fun encodeLegacyDate(legacyDate: Date): String = ZonedDateTime.ofInstant(legacyDate.toInstant(), ZoneId.systemDefault()).format(this.timezonedPrinter)

override fun encode(value: Any): String {
return when (value) {
is Timestamp -> this.timezonedPrinter.print(DateTime(value))
is Date -> this.timezonedPrinter.print(DateTime(value))
is Calendar -> this.timezonedPrinter.print(DateTime(value))
is LocalDateTime -> this.nonTimezonedPrinter.print(value)
is ReadableDateTime -> this.timezonedPrinter.print(value)
is Timestamp -> encodeLegacyDate(value)
is Date -> encodeLegacyDate(value)
is Calendar -> encodeLegacyDate(value.time)
is LocalDateTime -> value.format(this.nonTimezonedPrinter)
is TemporalAccessor -> this.timezonedPrinter.format(value)
else -> throw DateEncoderNotAvailableException(value)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.github.jasync.sql.db.column

import org.joda.time.format.DateTimeFormat
import org.joda.time.format.DateTimeFormatter
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter

object TimestampWithTimezoneEncoderDecoder : TimestampEncoderDecoder() {

private val format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSSSSSZ")
private val format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSZ")

override fun formatter(): DateTimeFormatter = format

override fun decode(value: String): Any {
return formatter().parseDateTime(value)
return ZonedDateTime.parse(value, formatter())
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.github.jasync.sql.db

import org.assertj.core.api.Assertions.assertThat
import org.joda.time.LocalDateTime
import org.junit.Test
import java.time.LocalDateTime

class RowDataTest {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@

package com.github.jasync.sql.db.column

import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormatterBuilder
import org.junit.Test
import java.sql.Timestamp
import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatterBuilder
import java.util.*
import kotlin.test.assertEquals

class TimestampEncoderDecoderSpec {

val encoder = TimestampEncoderDecoder()
val dateTime = DateTime()
.withDate(2013, 12, 27)
.withTime(8, 40, 50, 800)
val dateTime = ZonedDateTime.of(2013, 12, 27,
8, 40, 50, 800 * 1000000,
ZoneId.systemDefault())

val result = "2013-12-27 08:40:50.800000"
val formatter = DateTimeFormatterBuilder().appendPattern("Z").toFormatter()
val resultWithTimezone = "2013-12-27 08:40:50.800000${formatter.print(dateTime)}"
val resultWithTimezone = "2013-12-27 08:40:50.800000${dateTime.format(formatter)}"

@Test
fun `should print a timestamp`() {
val timestamp = Timestamp(dateTime.toDate().time)

Timestamp.from(dateTime.toInstant())
val timestamp = Timestamp.from(dateTime.toInstant())
assertEquals(encoder.encode(timestamp), resultWithTimezone)
}

Expand All @@ -30,18 +35,18 @@ class TimestampEncoderDecoderSpec {

@Test
fun `should print a date`() {
assertEquals(encoder.encode(dateTime.toDate()), resultWithTimezone)
assertEquals(encoder.encode(Date.from(dateTime.toInstant())), resultWithTimezone)
}

@Test
fun `should print a calendar`() {
val calendar = java.util.Calendar.getInstance()
calendar.time = dateTime.toDate()
val calendar = Calendar.getInstance()
calendar.time = Date.from(calendar.toInstant())
encoder.encode(calendar) === resultWithTimezone
}

@Test
fun `should print a datetime`() {
encoder.encode(dateTime) === resultWithTimezone
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ActorBasedObjectPoolTest {
}

@Test
fun `basic take operation - when create is little stuck should not be timeout (create timeout is 5 sec)`() {
fun `basic take operation - when create is little stuck should not be timeout - create timeout is 5 sec`() {
tested = ActorBasedObjectPool(
factory, configuration.copy(
createTimeout = 5000
Expand Down Expand Up @@ -253,7 +253,7 @@ class ActorBasedObjectPoolTest {
}

@Test
fun `test for leaks detection - we are taking a widget but "lost" it so it should be cleaned up`() {
fun `test for leaks detection - we are taking a widget but lost it so it should be cleaned up`() {
tested = ActorBasedObjectPool(
ForTestingWeakMyFactory(), configuration.copy(
maxObjects = 1,
Expand Down
Loading