Skip to content

Commit 41a57a8

Browse files
authored
Merge pull request #69 from ashley-rose/jdk-instant-2.11
JDK8 Instant support for Scala 2.11
2 parents 6705f41 + b94cc1f commit 41a57a8

3 files changed

Lines changed: 104 additions & 60 deletions

File tree

src/main/scala/com/simple/jdub/Row.scala

Lines changed: 89 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
package com.simple.jdub
22

3+
import org.joda.time.DateTime
4+
import org.joda.time.DateTimeZone
5+
6+
import java.io.InputStream
7+
import java.io.Reader
8+
import java.net.URL
9+
import java.sql.Blob
10+
import java.sql.Clob
11+
import java.sql.Date
12+
import java.sql.NClob
13+
import java.sql.Ref
314
import java.sql.ResultSet
4-
import org.joda.time.{DateTime, DateTimeZone}
15+
import java.sql.SQLXML
16+
import java.sql.Time
17+
import java.sql.Timestamp
18+
import java.time.Instant
19+
import java.time.LocalDateTime
520
import java.util.UUID
621

722
/**
@@ -13,128 +28,128 @@ class Row(rs: ResultSet) {
1328
/**
1429
* Extract the value at the given offset as an Option[String].
1530
*/
16-
def string(index: Int) = extract(rs.getString(index + 1))
31+
def string(index: Int): Option[String] = extract(rs.getString(index + 1))
1732

1833
/**
1934
* Extract the value with the given name as an Option[String].
2035
*/
21-
def string(name: String) = extract(rs.getString(name))
36+
def string(name: String): Option[String] = extract(rs.getString(name))
2237

2338
/**
2439
* Extract the value at the given offset as an Option[UUID].
2540
*/
26-
def uuid(index: Int) = string(index).map { UUID.fromString }
41+
def uuid(index: Int): Option[UUID] = string(index).map { UUID.fromString }
2742

2843
/**
2944
* Extract the value with the given name as an Option[UUID].
3045
*/
31-
def uuid(name: String) = string(name).map { UUID.fromString }
46+
def uuid(name: String): Option[UUID] = string(name).map { UUID.fromString }
3247

3348
/**
3449
* Extract the value at the given offset as an Option[Boolean].
3550
*/
36-
def boolean(index: Int) = extract(rs.getBoolean(index + 1))
51+
def boolean(index: Int): Option[Boolean] = extract(rs.getBoolean(index + 1))
3752

3853
/**
3954
* Extract the value with the given name as an Option[Boolean].
4055
*/
41-
def boolean(name: String) = extract(rs.getBoolean(name))
56+
def boolean(name: String): Option[Boolean] = extract(rs.getBoolean(name))
4257

4358
/**
4459
* Extract the value at the given offset as an Option[Byte].
4560
*/
46-
def byte(index: Int) = extract(rs.getByte(index + 1))
61+
def byte(index: Int): Option[Byte] = extract(rs.getByte(index + 1))
4762

4863
/**
4964
* Extract the value with the given name as an Option[Byte].
5065
*/
51-
def byte(name: String) = extract(rs.getByte(name))
66+
def byte(name: String): Option[Byte] = extract(rs.getByte(name))
5267

5368
/**
5469
* Extract the value at the given offset as an Option[Short].
5570
*/
56-
def short(index: Int) = extract(rs.getShort(index + 1))
71+
def short(index: Int): Option[Short] = extract(rs.getShort(index + 1))
5772

5873
/**
5974
* Extract the value with the given name as an Option[Short].
6075
*/
61-
def short(name: String) = extract(rs.getShort(name))
76+
def short(name: String): Option[Short] = extract(rs.getShort(name))
6277

6378

6479
/**
6580
* Extract the value at the given offset as an Option[Int].
6681
*/
67-
def int(index: Int) = extract(rs.getInt(index + 1))
82+
def int(index: Int): Option[Int] = extract(rs.getInt(index + 1))
6883

6984
/**
7085
* Extract the value with the given name as an Option[Int].
7186
*/
72-
def int(name: String) = extract(rs.getInt(name))
87+
def int(name: String): Option[Int] = extract(rs.getInt(name))
7388

7489
/**
7590
* Extract the value at the given offset as an Option[Long].
7691
*/
77-
def long(index: Int) = extract(rs.getLong(index + 1))
92+
def long(index: Int): Option[Long] = extract(rs.getLong(index + 1))
7893

7994
/**
8095
* Extract the value with the given name as an Option[Long].
8196
*/
82-
def long(name: String) = extract(rs.getLong(name))
97+
def long(name: String): Option[Long] = extract(rs.getLong(name))
8398

8499
/**
85100
* Extract the value at the given offset as an Option[Float].
86101
*/
87-
def float(index: Int) = extract(rs.getFloat(index + 1))
102+
def float(index: Int): Option[Float] = extract(rs.getFloat(index + 1))
88103

89104
/**
90105
* Extract the value with the given name as an Option[Float].
91106
*/
92-
def float(name: String) = extract(rs.getFloat(name))
107+
def float(name: String): Option[Float] = extract(rs.getFloat(name))
93108

94109
/**
95110
* Extract the value at the given offset as an Option[Double].
96111
*/
97-
def double(index: Int) = extract(rs.getDouble(index + 1))
112+
def double(index: Int): Option[Double] = extract(rs.getDouble(index + 1))
98113

99114
/**
100115
* Extract the value with the given name as an Option[Double].
101116
*/
102-
def double(name: String) = extract(rs.getDouble(name))
117+
def double(name: String): Option[Double] = extract(rs.getDouble(name))
103118

104119
/**
105120
* Extract the value at the given offset as an Option[BigDecimal].
106121
*/
107-
def bigDecimal(index: Int) = extract(rs.getBigDecimal(index + 1)).map { scala.math.BigDecimal(_) }
122+
def bigDecimal(index: Int): Option[BigDecimal] = extract(rs.getBigDecimal(index + 1)).map { scala.math.BigDecimal(_) }
108123

109124
/**
110125
* Extract the value with the given name as an Option[BigDecimal].
111126
*/
112-
def bigDecimal(name: String) = extract(rs.getBigDecimal(name)).map { scala.math.BigDecimal(_) }
127+
def bigDecimal(name: String): Option[BigDecimal] = extract(rs.getBigDecimal(name)).map { scala.math.BigDecimal(_) }
113128

114129
/**
115130
* Extract the value at the given offset as an Option[Array[Byte]].
116131
*/
117-
def bytes(index: Int) = extract(rs.getBytes(index + 1))
132+
def bytes(index: Int): Option[Array[Byte]] = extract(rs.getBytes(index + 1))
118133

119134
/**
120135
* Extract the value with the given name as an Option[Array[Byte]].
121136
*/
122-
def bytes(name: String) = extract(rs.getBytes(name))
137+
def bytes(name: String): Option[Array[Byte]] = extract(rs.getBytes(name))
123138

124139
/**
125140
* Extract the value at the given offset as an Option[Date].
126141
*/
127-
def date(index: Int) = extract(rs.getDate(index + 1))
142+
def date(index: Int): Option[Date] = extract(rs.getDate(index + 1))
128143

129144
/**
130145
* Extract the value with the given name as an Option[Date].
131146
*/
132-
def date(name: String) = extract(rs.getDate(name))
147+
def date(name: String): Option[Date] = extract(rs.getDate(name))
133148

134149
/**
135150
* Extract the value at the given offset as an Option[Time].
136151
*/
137-
def time(index: Int) = extract(rs.getTime(index + 1))
152+
def time(index: Int): Option[Time] = extract(rs.getTime(index + 1))
138153

139154
/**
140155
* Extract the value with the given name as an Option[Time].
@@ -144,12 +159,32 @@ class Row(rs: ResultSet) {
144159
/**
145160
* Extract the value at the given offset as an Option[Timestamp].
146161
*/
147-
def timestamp(index: Int) = extract(rs.getTimestamp(index + 1))
162+
def timestamp(index: Int): Option[Timestamp] = extract(rs.getTimestamp(index + 1))
148163

149164
/**
150165
* Extract the value with the given name as an Option[Timestamp].
151166
*/
152-
def timestamp(name: String) = extract(rs.getTimestamp(name))
167+
def timestamp(name: String): Option[Timestamp] = extract(rs.getTimestamp(name))
168+
169+
/**
170+
* Extract the value at the given offset as an Option[Instant].
171+
*/
172+
def instant(index: Int): Option[Instant] = timestamp(index).map(_.toInstant)
173+
174+
/**
175+
* Extract the value with the given name as an Option[Instant].
176+
*/
177+
def instant(name: String): Option[Instant] = timestamp(name).map(_.toInstant)
178+
179+
/**
180+
* Extract the value at the given offset as an Option[LocalDateTime].
181+
*/
182+
def localDateTime(index: Int): Option[LocalDateTime] = timestamp(index).map(_.toLocalDateTime)
183+
184+
/**
185+
* Extract the value with the given name as an Option[LocalDateTime].
186+
*/
187+
def localDateTime(name: String): Option[LocalDateTime] = timestamp(name).map(_.toLocalDateTime)
153188

154189
/**
155190
* Extract the value with the given name as an Option[DateTime].
@@ -168,32 +203,32 @@ class Row(rs: ResultSet) {
168203
/**
169204
* Extract the value at the given offset as an Option[InputStream].
170205
*/
171-
def asciiStream(index: Int) = extract(rs.getAsciiStream(index + 1))
206+
def asciiStream(index: Int): Option[InputStream] = extract(rs.getAsciiStream(index + 1))
172207

173208
/**
174209
* Extract the value with the given name as an Option[InputStream].
175210
*/
176-
def asciiStream(name: String) = extract(rs.getAsciiStream(name))
211+
def asciiStream(name: String): Option[InputStream] = extract(rs.getAsciiStream(name))
177212

178213
/**
179-
* Extract the value at the given offset as an Option[InputStream].
214+
* Extract the value at the given offset as an Option[Reader].
180215
*/
181-
def characterStream(index: Int) = extract(rs.getCharacterStream(index + 1))
216+
def characterStream(index: Int): Option[Reader] = extract(rs.getCharacterStream(index + 1))
182217

183218
/**
184-
* Extract the value with the given name as an Option[InputStream].
219+
* Extract the value with the given name as an Option[Reader].
185220
*/
186-
def characterStream(name: String) = extract(rs.getCharacterStream(name))
221+
def characterStream(name: String): Option[Reader] = extract(rs.getCharacterStream(name))
187222

188223
/**
189224
* Extract the value at the given offset as an Option[InputStream].
190225
*/
191-
def binaryStream(index: Int) = extract(rs.getBinaryStream(index + 1))
226+
def binaryStream(index: Int): Option[InputStream] = extract(rs.getBinaryStream(index + 1))
192227

193228
/**
194229
* Extract the value with the given name as an Option[InputStream].
195230
*/
196-
def binaryStream(name: String) = extract(rs.getBinaryStream(name))
231+
def binaryStream(name: String): Option[InputStream] = extract(rs.getBinaryStream(name))
197232

198233
/**
199234
* Extract the value at the given offset as an Option[Any].
@@ -208,92 +243,92 @@ class Row(rs: ResultSet) {
208243
/**
209244
* Extract the value at the given offset as an Option[Array].
210245
*/
211-
def sqlArray(index: Int) = extract(rs.getArray(index + 1))
246+
def sqlArray(index: Int): Option[java.sql.Array] = extract(rs.getArray(index + 1))
212247

213248
/**
214249
* Extract the value with the given name as an Option[Array].
215250
*/
216-
def sqlArray(name: String) = extract(rs.getArray(name))
251+
def sqlArray(name: String): Option[java.sql.Array] = extract(rs.getArray(name))
217252

218253
/**
219254
* Extract the value at the given offset as an Option[Blob].
220255
*/
221-
def blob(index: Int) = extract(rs.getBlob(index + 1))
256+
def blob(index: Int): Option[Blob] = extract(rs.getBlob(index + 1))
222257

223258
/**
224259
* Extract the value with the given name as an Option[Blob].
225260
*/
226-
def blob(name: String) = extract(rs.getBlob(name))
261+
def blob(name: String): Option[Blob] = extract(rs.getBlob(name))
227262

228263
/**
229264
* Extract the value at the given offset as an Option[Clob].
230265
*/
231-
def clob(index: Int) = extract(rs.getClob(index + 1))
266+
def clob(index: Int): Option[Clob] = extract(rs.getClob(index + 1))
232267

233268
/**
234269
* Extract the value with the given name as an Option[Clob].
235270
*/
236-
def clob(name: String) = extract(rs.getClob(name))
271+
def clob(name: String): Option[Clob] = extract(rs.getClob(name))
237272

238273
/**
239274
* Extract the value at the given offset as an Option[Ref].
240275
*/
241-
def ref(index: Int) = extract(rs.getRef(index + 1))
276+
def ref(index: Int): Option[Ref] = extract(rs.getRef(index + 1))
242277

243278
/**
244279
* Extract the value with the given name as an Option[Ref].
245280
*/
246-
def ref(name: String) = extract(rs.getRef(name))
281+
def ref(name: String): Option[Ref] = extract(rs.getRef(name))
247282

248283
/**
249284
* Extract the value at the given offset as an Option[String].
250285
*/
251-
def nString(index: Int) = extract(rs.getNString(index + 1))
286+
def nString(index: Int): Option[String] = extract(rs.getNString(index + 1))
252287

253288
/**
254289
* Extract the value with the given name as an Option[String].
255290
*/
256-
def nString(name: String) = extract(rs.getNString(name))
291+
def nString(name: String): Option[String] = extract(rs.getNString(name))
257292

258293
/**
259294
* Extract the value at the given offset as an Option[Reader].
260295
*/
261-
def nCharacterStream(index: Int) = extract(rs.getNCharacterStream(index + 1))
296+
def nCharacterStream(index: Int): Option[Reader] = extract(rs.getNCharacterStream(index + 1))
262297

263298
/**
264299
* Extract the value with the given name as an Option[Reader].
265300
*/
266-
def nCharacterStream(name: String) = extract(rs.getNCharacterStream(name))
301+
def nCharacterStream(name: String): Option[Reader] = extract(rs.getNCharacterStream(name))
267302

268303
/**
269304
* Extract the value at the given offset as an Option[NClob].
270305
*/
271-
def nClob(index: Int) = extract(rs.getNClob(index + 1))
306+
def nClob(index: Int): Option[NClob] = extract(rs.getNClob(index + 1))
272307

273308
/**
274309
* Extract the value with the given name as an Option[NClob].
275310
*/
276-
def nClob(name: String) = extract(rs.getNClob(name))
311+
def nClob(name: String): Option[NClob] = extract(rs.getNClob(name))
277312

278313
/**
279314
* Extract the value at the given offset as an Option[SQLXML].
280315
*/
281-
def sqlXML(index: Int) = extract(rs.getSQLXML(index + 1))
316+
def sqlXML(index: Int): Option[SQLXML] = extract(rs.getSQLXML(index + 1))
282317

283318
/**
284319
* Extract the value with the given name as an Option[SQLXML].
285320
*/
286-
def sqlXML(name: String) = extract(rs.getSQLXML(name))
321+
def sqlXML(name: String): Option[SQLXML] = extract(rs.getSQLXML(name))
287322

288323
/**
289324
* Extract the value at the given offset as an Option[URL].
290325
*/
291-
def url(index: Int) = extract(rs.getURL(index + 1))
326+
def url(index: Int): Option[URL] = extract(rs.getURL(index + 1))
292327

293328
/**
294329
* Extract the value with the given name as an Option[URL].
295330
*/
296-
def url(name: String) = extract(rs.getURL(name))
331+
def url(name: String): Option[URL] = extract(rs.getURL(name))
297332

298333
/**
299334
* Transform the row into a Map[String, Any].

src/main/scala/com/simple/jdub/Utils.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ object Utils {
4242
// Convert JDK8 date/times
4343
case d: java.time.LocalDate => java.sql.Date.valueOf(d)
4444
case dt: java.time.LocalDateTime => java.sql.Timestamp.valueOf(dt)
45+
case i: java.time.Instant => java.sql.Timestamp.from(i)
4546

4647
// Pass everything else through.
4748
case _ => x

0 commit comments

Comments
 (0)