Skip to content

Commit bbca48c

Browse files
Stefan Lancejenkins
authored andcommitted
finagle-mysql: Make it easier to use a ctu.Time with PreparedStatements
Problem To use a ctu.Time with a PreparedStatement, the Time must be converted to a java.sql.Timestamp. Solution Add an implicit so one does not need to explicitly do the conversion. JIRA Issues: CSL-6011 Differential Revision: https://phabricator.twitter.biz/D182973
1 parent b8523a4 commit bbca48c

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

CHANGES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Unreleased
1010
New Features
1111
~~~~~~~~~~~~
1212

13+
* finagle-mysql: `com.twitter.util.Time` can now be used with
14+
`PreparedStatement`s without converting the `ctu.Time` to a `java.sql.Timestamp`.
15+
``PHAB_ID=D182973``
16+
1317
* finagle-stats: Adds a lint rule to detect when metrics with colliding names are used.
1418
``PHAB_ID=D183494``
1519

finagle-mysql/src/main/scala/com/twitter/finagle/mysql/CanBeParameter.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,18 @@ object CanBeParameter {
300300
}
301301
}
302302

303+
// Note that Timestamp is UTC only and includes both Date and Time parts.
304+
// See https://dev.mysql.com/doc/refman/8.0/en/datetime.html.
305+
implicit val ctuTimeCanBeParameter: CanBeParameter[com.twitter.util.Time] = {
306+
new CanBeParameter[com.twitter.util.Time] {
307+
def sizeOf(param: com.twitter.util.Time): Int = 12
308+
def typeCode(param: com.twitter.util.Time): Short = Type.Timestamp
309+
def write(writer: MysqlBufWriter, param: com.twitter.util.Time): Unit = {
310+
valueCanBeParameter.write(writer, TimestampValue(new java.sql.Timestamp(param.inMillis)))
311+
}
312+
}
313+
}
314+
303315
implicit val nullCanBeParameter: CanBeParameter[Null] = {
304316
new CanBeParameter[Null] {
305317
def sizeOf(param: Null): Int = 0

finagle-mysql/src/test/scala/com/twitter/finagle/mysql/ParameterTest.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ class ParameterTest extends FunSuite {
135135
assert(param.value == value)
136136
}
137137

138+
test("com.twitter.util.Time") {
139+
val value = com.twitter.util.Time.fromMilliseconds(3L)
140+
val param: Parameter = value
141+
assert(param.value == value)
142+
}
143+
138144
test("null") {
139145
val value: String = null
140146
val param: Parameter = value

0 commit comments

Comments
 (0)