Skip to content

Commit 93fbe85

Browse files
authored
Merge pull request #68 from ashley-rose/jdk-instant
Add support for JDK8 Instant class
2 parents 2421bfb + 6eb4499 commit 93fbe85

File tree

3 files changed

+93
-60
lines changed

3 files changed

+93
-60
lines changed

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

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

3-
import java.sql.ResultSet
43
import org.joda.time.{DateTime, DateTimeZone}
4+
5+
import java.io.{InputStream, Reader}
6+
import java.net.URL
7+
import java.sql.{Blob, Clob, Date, NClob, Ref, ResultSet, SQLXML, Time, Timestamp}
8+
import java.time.{Instant, LocalDateTime}
59
import java.util.UUID
610

711
/**
@@ -13,128 +17,128 @@ class Row(rs: ResultSet) {
1317
/**
1418
* Extract the value at the given offset as an Option[String].
1519
*/
16-
def string(index: Int) = extract(rs.getString(index + 1))
20+
def string(index: Int): Option[String] = extract(rs.getString(index + 1))
1721

1822
/**
1923
* Extract the value with the given name as an Option[String].
2024
*/
21-
def string(name: String) = extract(rs.getString(name))
25+
def string(name: String): Option[String] = extract(rs.getString(name))
2226

2327
/**
2428
* Extract the value at the given offset as an Option[UUID].
2529
*/
26-
def uuid(index: Int) = string(index).map { UUID.fromString }
30+
def uuid(index: Int): Option[UUID] = string(index).map { UUID.fromString }
2731

2832
/**
2933
* Extract the value with the given name as an Option[UUID].
3034
*/
31-
def uuid(name: String) = string(name).map { UUID.fromString }
35+
def uuid(name: String): Option[UUID] = string(name).map { UUID.fromString }
3236

3337
/**
3438
* Extract the value at the given offset as an Option[Boolean].
3539
*/
36-
def boolean(index: Int) = extract(rs.getBoolean(index + 1))
40+
def boolean(index: Int): Option[Boolean] = extract(rs.getBoolean(index + 1))
3741

3842
/**
3943
* Extract the value with the given name as an Option[Boolean].
4044
*/
41-
def boolean(name: String) = extract(rs.getBoolean(name))
45+
def boolean(name: String): Option[Boolean] = extract(rs.getBoolean(name))
4246

4347
/**
4448
* Extract the value at the given offset as an Option[Byte].
4549
*/
46-
def byte(index: Int) = extract(rs.getByte(index + 1))
50+
def byte(index: Int): Option[Byte] = extract(rs.getByte(index + 1))
4751

4852
/**
4953
* Extract the value with the given name as an Option[Byte].
5054
*/
51-
def byte(name: String) = extract(rs.getByte(name))
55+
def byte(name: String): Option[Byte] = extract(rs.getByte(name))
5256

5357
/**
5458
* Extract the value at the given offset as an Option[Short].
5559
*/
56-
def short(index: Int) = extract(rs.getShort(index + 1))
60+
def short(index: Int): Option[Short] = extract(rs.getShort(index + 1))
5761

5862
/**
5963
* Extract the value with the given name as an Option[Short].
6064
*/
61-
def short(name: String) = extract(rs.getShort(name))
65+
def short(name: String): Option[Short] = extract(rs.getShort(name))
6266

6367

6468
/**
6569
* Extract the value at the given offset as an Option[Int].
6670
*/
67-
def int(index: Int) = extract(rs.getInt(index + 1))
71+
def int(index: Int): Option[Int] = extract(rs.getInt(index + 1))
6872

6973
/**
7074
* Extract the value with the given name as an Option[Int].
7175
*/
72-
def int(name: String) = extract(rs.getInt(name))
76+
def int(name: String): Option[Int] = extract(rs.getInt(name))
7377

7478
/**
7579
* Extract the value at the given offset as an Option[Long].
7680
*/
77-
def long(index: Int) = extract(rs.getLong(index + 1))
81+
def long(index: Int): Option[Long] = extract(rs.getLong(index + 1))
7882

7983
/**
8084
* Extract the value with the given name as an Option[Long].
8185
*/
82-
def long(name: String) = extract(rs.getLong(name))
86+
def long(name: String): Option[Long] = extract(rs.getLong(name))
8387

8488
/**
8589
* Extract the value at the given offset as an Option[Float].
8690
*/
87-
def float(index: Int) = extract(rs.getFloat(index + 1))
91+
def float(index: Int): Option[Float] = extract(rs.getFloat(index + 1))
8892

8993
/**
9094
* Extract the value with the given name as an Option[Float].
9195
*/
92-
def float(name: String) = extract(rs.getFloat(name))
96+
def float(name: String): Option[Float] = extract(rs.getFloat(name))
9397

9498
/**
9599
* Extract the value at the given offset as an Option[Double].
96100
*/
97-
def double(index: Int) = extract(rs.getDouble(index + 1))
101+
def double(index: Int): Option[Double] = extract(rs.getDouble(index + 1))
98102

99103
/**
100104
* Extract the value with the given name as an Option[Double].
101105
*/
102-
def double(name: String) = extract(rs.getDouble(name))
106+
def double(name: String): Option[Double] = extract(rs.getDouble(name))
103107

104108
/**
105109
* Extract the value at the given offset as an Option[BigDecimal].
106110
*/
107-
def bigDecimal(index: Int) = extract(rs.getBigDecimal(index + 1)).map { scala.math.BigDecimal(_) }
111+
def bigDecimal(index: Int): Option[BigDecimal] = extract(rs.getBigDecimal(index + 1)).map { scala.math.BigDecimal(_) }
108112

109113
/**
110114
* Extract the value with the given name as an Option[BigDecimal].
111115
*/
112-
def bigDecimal(name: String) = extract(rs.getBigDecimal(name)).map { scala.math.BigDecimal(_) }
116+
def bigDecimal(name: String): Option[BigDecimal] = extract(rs.getBigDecimal(name)).map { scala.math.BigDecimal(_) }
113117

114118
/**
115119
* Extract the value at the given offset as an Option[Array[Byte]].
116120
*/
117-
def bytes(index: Int) = extract(rs.getBytes(index + 1))
121+
def bytes(index: Int): Option[Array[Byte]] = extract(rs.getBytes(index + 1))
118122

119123
/**
120124
* Extract the value with the given name as an Option[Array[Byte]].
121125
*/
122-
def bytes(name: String) = extract(rs.getBytes(name))
126+
def bytes(name: String): Option[Array[Byte]] = extract(rs.getBytes(name))
123127

124128
/**
125129
* Extract the value at the given offset as an Option[Date].
126130
*/
127-
def date(index: Int) = extract(rs.getDate(index + 1))
131+
def date(index: Int): Option[Date] = extract(rs.getDate(index + 1))
128132

129133
/**
130134
* Extract the value with the given name as an Option[Date].
131135
*/
132-
def date(name: String) = extract(rs.getDate(name))
136+
def date(name: String): Option[Date] = extract(rs.getDate(name))
133137

134138
/**
135139
* Extract the value at the given offset as an Option[Time].
136140
*/
137-
def time(index: Int) = extract(rs.getTime(index + 1))
141+
def time(index: Int): Option[Time] = extract(rs.getTime(index + 1))
138142

139143
/**
140144
* Extract the value with the given name as an Option[Time].
@@ -144,12 +148,32 @@ class Row(rs: ResultSet) {
144148
/**
145149
* Extract the value at the given offset as an Option[Timestamp].
146150
*/
147-
def timestamp(index: Int) = extract(rs.getTimestamp(index + 1))
151+
def timestamp(index: Int): Option[Timestamp] = extract(rs.getTimestamp(index + 1))
148152

149153
/**
150154
* Extract the value with the given name as an Option[Timestamp].
151155
*/
152-
def timestamp(name: String) = extract(rs.getTimestamp(name))
156+
def timestamp(name: String): Option[Timestamp] = extract(rs.getTimestamp(name))
157+
158+
/**
159+
* Extract the value at the given offset as an Option[Instant].
160+
*/
161+
def instant(index: Int): Option[Instant] = timestamp(index).map(_.toInstant)
162+
163+
/**
164+
* Extract the value with the given name as an Option[Instant].
165+
*/
166+
def instant(name: String): Option[Instant] = timestamp(name).map(_.toInstant)
167+
168+
/**
169+
* Extract the value at the given offset as an Option[LocalDateTime].
170+
*/
171+
def localDateTime(index: Int): Option[LocalDateTime] = timestamp(index).map(_.toLocalDateTime)
172+
173+
/**
174+
* Extract the value with the given name as an Option[LocalDateTime].
175+
*/
176+
def localDateTime(name: String): Option[LocalDateTime] = timestamp(name).map(_.toLocalDateTime)
153177

154178
/**
155179
* Extract the value with the given name as an Option[DateTime].
@@ -168,32 +192,32 @@ class Row(rs: ResultSet) {
168192
/**
169193
* Extract the value at the given offset as an Option[InputStream].
170194
*/
171-
def asciiStream(index: Int) = extract(rs.getAsciiStream(index + 1))
195+
def asciiStream(index: Int): Option[InputStream] = extract(rs.getAsciiStream(index + 1))
172196

173197
/**
174198
* Extract the value with the given name as an Option[InputStream].
175199
*/
176-
def asciiStream(name: String) = extract(rs.getAsciiStream(name))
200+
def asciiStream(name: String): Option[InputStream] = extract(rs.getAsciiStream(name))
177201

178202
/**
179-
* Extract the value at the given offset as an Option[InputStream].
203+
* Extract the value at the given offset as an Option[Reader].
180204
*/
181-
def characterStream(index: Int) = extract(rs.getCharacterStream(index + 1))
205+
def characterStream(index: Int): Option[Reader] = extract(rs.getCharacterStream(index + 1))
182206

183207
/**
184-
* Extract the value with the given name as an Option[InputStream].
208+
* Extract the value with the given name as an Option[Reader].
185209
*/
186-
def characterStream(name: String) = extract(rs.getCharacterStream(name))
210+
def characterStream(name: String): Option[Reader] = extract(rs.getCharacterStream(name))
187211

188212
/**
189213
* Extract the value at the given offset as an Option[InputStream].
190214
*/
191-
def binaryStream(index: Int) = extract(rs.getBinaryStream(index + 1))
215+
def binaryStream(index: Int): Option[InputStream] = extract(rs.getBinaryStream(index + 1))
192216

193217
/**
194218
* Extract the value with the given name as an Option[InputStream].
195219
*/
196-
def binaryStream(name: String) = extract(rs.getBinaryStream(name))
220+
def binaryStream(name: String): Option[InputStream] = extract(rs.getBinaryStream(name))
197221

198222
/**
199223
* Extract the value at the given offset as an Option[Any].
@@ -208,92 +232,92 @@ class Row(rs: ResultSet) {
208232
/**
209233
* Extract the value at the given offset as an Option[Array].
210234
*/
211-
def sqlArray(index: Int) = extract(rs.getArray(index + 1))
235+
def sqlArray(index: Int): Option[java.sql.Array] = extract(rs.getArray(index + 1))
212236

213237
/**
214238
* Extract the value with the given name as an Option[Array].
215239
*/
216-
def sqlArray(name: String) = extract(rs.getArray(name))
240+
def sqlArray(name: String): Option[java.sql.Array] = extract(rs.getArray(name))
217241

218242
/**
219243
* Extract the value at the given offset as an Option[Blob].
220244
*/
221-
def blob(index: Int) = extract(rs.getBlob(index + 1))
245+
def blob(index: Int): Option[Blob] = extract(rs.getBlob(index + 1))
222246

223247
/**
224248
* Extract the value with the given name as an Option[Blob].
225249
*/
226-
def blob(name: String) = extract(rs.getBlob(name))
250+
def blob(name: String): Option[Blob] = extract(rs.getBlob(name))
227251

228252
/**
229253
* Extract the value at the given offset as an Option[Clob].
230254
*/
231-
def clob(index: Int) = extract(rs.getClob(index + 1))
255+
def clob(index: Int): Option[Clob] = extract(rs.getClob(index + 1))
232256

233257
/**
234258
* Extract the value with the given name as an Option[Clob].
235259
*/
236-
def clob(name: String) = extract(rs.getClob(name))
260+
def clob(name: String): Option[Clob] = extract(rs.getClob(name))
237261

238262
/**
239263
* Extract the value at the given offset as an Option[Ref].
240264
*/
241-
def ref(index: Int) = extract(rs.getRef(index + 1))
265+
def ref(index: Int): Option[Ref] = extract(rs.getRef(index + 1))
242266

243267
/**
244268
* Extract the value with the given name as an Option[Ref].
245269
*/
246-
def ref(name: String) = extract(rs.getRef(name))
270+
def ref(name: String): Option[Ref] = extract(rs.getRef(name))
247271

248272
/**
249273
* Extract the value at the given offset as an Option[String].
250274
*/
251-
def nString(index: Int) = extract(rs.getNString(index + 1))
275+
def nString(index: Int): Option[String] = extract(rs.getNString(index + 1))
252276

253277
/**
254278
* Extract the value with the given name as an Option[String].
255279
*/
256-
def nString(name: String) = extract(rs.getNString(name))
280+
def nString(name: String): Option[String] = extract(rs.getNString(name))
257281

258282
/**
259283
* Extract the value at the given offset as an Option[Reader].
260284
*/
261-
def nCharacterStream(index: Int) = extract(rs.getNCharacterStream(index + 1))
285+
def nCharacterStream(index: Int): Option[Reader] = extract(rs.getNCharacterStream(index + 1))
262286

263287
/**
264288
* Extract the value with the given name as an Option[Reader].
265289
*/
266-
def nCharacterStream(name: String) = extract(rs.getNCharacterStream(name))
290+
def nCharacterStream(name: String): Option[Reader] = extract(rs.getNCharacterStream(name))
267291

268292
/**
269293
* Extract the value at the given offset as an Option[NClob].
270294
*/
271-
def nClob(index: Int) = extract(rs.getNClob(index + 1))
295+
def nClob(index: Int): Option[NClob] = extract(rs.getNClob(index + 1))
272296

273297
/**
274298
* Extract the value with the given name as an Option[NClob].
275299
*/
276-
def nClob(name: String) = extract(rs.getNClob(name))
300+
def nClob(name: String): Option[NClob] = extract(rs.getNClob(name))
277301

278302
/**
279303
* Extract the value at the given offset as an Option[SQLXML].
280304
*/
281-
def sqlXML(index: Int) = extract(rs.getSQLXML(index + 1))
305+
def sqlXML(index: Int): Option[SQLXML] = extract(rs.getSQLXML(index + 1))
282306

283307
/**
284308
* Extract the value with the given name as an Option[SQLXML].
285309
*/
286-
def sqlXML(name: String) = extract(rs.getSQLXML(name))
310+
def sqlXML(name: String): Option[SQLXML] = extract(rs.getSQLXML(name))
287311

288312
/**
289313
* Extract the value at the given offset as an Option[URL].
290314
*/
291-
def url(index: Int) = extract(rs.getURL(index + 1))
315+
def url(index: Int): Option[URL] = extract(rs.getURL(index + 1))
292316

293317
/**
294318
* Extract the value with the given name as an Option[URL].
295319
*/
296-
def url(name: String) = extract(rs.getURL(name))
320+
def url(name: String): Option[URL] = extract(rs.getURL(name))
297321

298322
/**
299323
* 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)