Skip to content
This repository
Browse code

Closes #1180. Return types for DB columns that are primitives will co…

…me back as null rather than 0/false if the columns are null
  • Loading branch information...
commit d0baf2e1f0d6963ffa4b6f36c081e2badfe698e9 1 parent dff0b6a
David Pollak authored January 09, 2012
20  persistence/db/src/main/scala/net/liftweb/db/DB.scala
@@ -396,9 +396,9 @@ trait DB extends Loggable {
396 396
           case x => x.toString
397 397
         }
398 398
 
399  
-      case BIGINT | INTEGER | /* DECIMAL | NUMERIC | */ SMALLINT | TINYINT => rs.getLong(pos).toString
  399
+      case BIGINT | INTEGER | /* DECIMAL | NUMERIC | */ SMALLINT | TINYINT => checkNull(rs, pos, rs.getLong(pos).toString)
400 400
 
401  
-      case BIT | BOOLEAN => rs.getBoolean(pos).toString
  401
+      case BIT | BOOLEAN => checkNull(rs, pos, rs.getBoolean(pos).toString)
402 402
 
403 403
       case VARCHAR | CHAR | CLOB | LONGVARCHAR => rs.getString(pos)
404 404
 
@@ -407,10 +407,18 @@ trait DB extends Loggable {
407 407
         case x => x.toString
408 408
       }
409 409
 
410  
-      case DOUBLE | FLOAT | REAL => rs.getDouble(pos).toString
  410
+      case DOUBLE | FLOAT | REAL => checkNull(rs, pos, rs.getDouble(pos).toString)
411 411
     }
412 412
   }
413 413
 
  414
+  /*
  415
+   If the column is null, return null rather than the boxed primitive
  416
+   */
  417
+  def checkNull[T](rs: ResultSet, pos: Int, res: => T): T = {
  418
+    if (null eq rs.getObject(pos)) null.asInstanceOf[T]
  419
+    else res
  420
+  }
  421
+
414 422
   private def asAny(pos: Int, rs: ResultSet, md: ResultSetMetaData): Any = {
415 423
     import java.sql.Types._
416 424
     md.getColumnType(pos) match {
@@ -418,15 +426,15 @@ trait DB extends Loggable {
418 426
 
419 427
       case DECIMAL | NUMERIC => rs.getBigDecimal(pos)
420 428
 
421  
-      case BIGINT | INTEGER | /* DECIMAL | NUMERIC | */ SMALLINT | TINYINT => rs.getLong(pos)
  429
+      case BIGINT | INTEGER | /* DECIMAL | NUMERIC | */ SMALLINT | TINYINT => checkNull(rs, pos, rs.getLong(pos))
422 430
 
423  
-      case BIT | BOOLEAN => rs.getBoolean(pos)
  431
+      case BIT | BOOLEAN => checkNull(rs, pos, rs.getBoolean(pos))
424 432
 
425 433
       case VARCHAR | CHAR | CLOB | LONGVARCHAR => rs.getString(pos)
426 434
 
427 435
       case DATE | TIME | TIMESTAMP => rs.getTimestamp(pos)
428 436
 
429  
-      case DOUBLE | FLOAT | REAL => rs.getDouble(pos)
  437
+      case DOUBLE | FLOAT | REAL => checkNull(rs, pos, rs.getDouble(pos))
430 438
     }
431 439
   }
432 440
 

0 notes on commit d0baf2e

Please sign in to comment.
Something went wrong with that request. Please try again.