@@ -99,6 +99,7 @@ trait Dialect extends DialectTypeMappers {
99
99
r.getObject(idx) match {
100
100
case u : UUID => u
101
101
case s : String => UUID .fromString(s)
102
+ case null => null
102
103
}
103
104
}
104
105
@@ -110,16 +111,29 @@ trait Dialect extends DialectTypeMappers {
110
111
implicit def BytesType : TypeMapper [geny.Bytes ] = new BytesType
111
112
class BytesType extends TypeMapper [geny.Bytes ] {
112
113
def jdbcType = JDBCType .VARBINARY
113
- def get (r : ResultSet , idx : Int ) = new geny.Bytes (r.getBytes(idx))
114
- def put (r : PreparedStatement , idx : Int , v : geny.Bytes ) = r.setBytes(idx, v.array)
114
+ def get (r : ResultSet , idx : Int ) = {
115
+ val bytes = r.getBytes(idx)
116
+ if (bytes == null ) null
117
+ else new geny.Bytes (bytes)
118
+ }
119
+ def put (r : PreparedStatement , idx : Int , v : geny.Bytes ) = {
120
+ val byteArray = if (v == null ) null else v.array
121
+ r.setBytes(idx, byteArray)
122
+ }
115
123
}
116
124
117
125
implicit def UtilDateType : TypeMapper [java.util.Date ] = new UtilDateType
118
126
class UtilDateType extends TypeMapper [java.util.Date ] {
119
127
def jdbcType = JDBCType .TIMESTAMP
120
- def get (r : ResultSet , idx : Int ) = new java.util.Date (r.getTimestamp(idx).getTime)
121
- def put (r : PreparedStatement , idx : Int , v : java.util.Date ) =
122
- r.setTimestamp(idx, new java.sql.Timestamp (v.getTime))
128
+ def get (r : ResultSet , idx : Int ) = {
129
+ val ts = r.getTimestamp(idx)
130
+ if (ts == null ) null
131
+ else new java.util.Date (ts.getTime)
132
+ }
133
+ def put (r : PreparedStatement , idx : Int , v : java.util.Date ) = {
134
+ val time = if (v == null ) null else new java.sql.Timestamp (v.getTime)
135
+ r.setTimestamp(idx, time)
136
+ }
123
137
}
124
138
125
139
implicit def LocalDateType : TypeMapper [LocalDate ] = new LocalDateType
@@ -147,9 +161,15 @@ trait Dialect extends DialectTypeMappers {
147
161
class ZonedDateTimeType extends TypeMapper [ZonedDateTime ] {
148
162
def jdbcType = JDBCType .TIMESTAMP_WITH_TIMEZONE
149
163
override def castTypeString = " TIMESTAMP WITH TIME ZONE"
150
- def get (r : ResultSet , idx : Int ) = r.getTimestamp(idx).toInstant.atZone(ZoneId .systemDefault())
151
- def put (r : PreparedStatement , idx : Int , v : ZonedDateTime ) = r
152
- .setTimestamp(idx, java.sql.Timestamp .from(v.toInstant))
164
+ def get (r : ResultSet , idx : Int ) = {
165
+ val ts = r.getTimestamp(idx)
166
+ if (ts == null ) null
167
+ else ts.toInstant.atZone(ZoneId .systemDefault())
168
+ }
169
+ def put (r : PreparedStatement , idx : Int , v : ZonedDateTime ) = {
170
+ val ts = if (v == null ) null else java.sql.Timestamp .from(v.toInstant)
171
+ r.setTimestamp(idx, ts)
172
+ }
153
173
}
154
174
155
175
implicit def InstantType : TypeMapper [Instant ] = new InstantType
@@ -173,8 +193,10 @@ trait Dialect extends DialectTypeMappers {
173
193
}
174
194
}
175
195
176
- def put (r : PreparedStatement , idx : Int , v : Instant ) = r
177
- .setTimestamp(idx, java.sql.Timestamp .from(v))
196
+ def put (r : PreparedStatement , idx : Int , v : Instant ) = {
197
+ val ts = if (v == null ) null else java.sql.Timestamp .from(v)
198
+ r.setTimestamp(idx, ts)
199
+ }
178
200
}
179
201
180
202
implicit def OffsetTimeType : TypeMapper [OffsetTime ] = new OffsetTimeType
@@ -190,19 +212,27 @@ trait Dialect extends DialectTypeMappers {
190
212
def jdbcType = JDBCType .TIMESTAMP_WITH_TIMEZONE
191
213
override def castTypeString = " TIMESTAMP WITH TIME ZONE"
192
214
def get (r : ResultSet , idx : Int ) = {
193
- r.getTimestamp(idx).toInstant.atOffset(OffsetDateTime .now().getOffset)
215
+ val ts = r.getTimestamp(idx)
216
+ if (ts == null ) null
217
+ else ts.toInstant.atOffset(OffsetDateTime .now().getOffset)
194
218
}
195
219
def put (r : PreparedStatement , idx : Int , v : OffsetDateTime ) = {
196
- r.setTimestamp(idx, java.sql.Timestamp .from(v.toInstant))
220
+ val ts = if (v == null ) null else java.sql.Timestamp .from(v.toInstant)
221
+ r.setTimestamp(idx, ts)
197
222
}
198
223
}
199
224
200
225
implicit def EnumType [T <: Enumeration # Value ](implicit constructor : String => T ): TypeMapper [T ] =
201
226
new EnumType [T ]
202
227
class EnumType [T ](implicit constructor : String => T ) extends TypeMapper [T ] {
203
228
def jdbcType : JDBCType = JDBCType .VARCHAR
204
- def get (r : ResultSet , idx : Int ): T = constructor(r.getString(idx))
205
- def put (r : PreparedStatement , idx : Int , v : T ) = r.setObject(idx, v, java.sql.Types .OTHER )
229
+ def get (r : ResultSet , idx : Int ): T = {
230
+ val str = r.getString(idx)
231
+ if (str == null ) null .asInstanceOf [T ]
232
+ else constructor(str)
233
+ }
234
+ def put (r : PreparedStatement , idx : Int , v : T ) =
235
+ r.setObject(idx, v, java.sql.Types .OTHER )
206
236
}
207
237
208
238
implicit def from (x : Byte ): Expr [Byte ] = Expr (x)
0 commit comments