diff --git a/src/main/scala/net/noerd/prequel/Formattables.scala b/src/main/scala/net/noerd/prequel/Formattables.scala index 16968f2..39de6a3 100644 --- a/src/main/scala/net/noerd/prequel/Formattables.scala +++ b/src/main/scala/net/noerd/prequel/Formattables.scala @@ -31,7 +31,7 @@ class Nullable( val value: Option[ Formattable ] ) extends Formattable { value.map( _.escaped( formatter ) ).getOrElse( "null" ) } override def addTo( statement: ReusableStatement ): Unit = { - statement.addNull + value.map(statement << _).getOrElse(statement.addNull) } } object Nullable { @@ -184,4 +184,4 @@ class BinaryFormattable( val value: Array[Byte] ) extends Formattable { } object BinaryFormattable{ def apply( value: Array[Byte] ) = new BinaryFormattable( value ) -} \ No newline at end of file +} diff --git a/src/test/scala/net/noerd/prequel/TransactionSpec.scala b/src/test/scala/net/noerd/prequel/TransactionSpec.scala index b5cf355..99fd06e 100644 --- a/src/test/scala/net/noerd/prequel/TransactionSpec.scala +++ b/src/test/scala/net/noerd/prequel/TransactionSpec.scala @@ -206,7 +206,31 @@ class TransactionSpec extends FunSpec with ShouldMatchers with BeforeAndAfterEac difference should be > (1.0) } } + + it( "should handle nullable columns correctly" ) { + case class Item( v1: Long, v2: Option[String] ) + + val items = List( Item( 1001, Some( "foo" ) ), Item( 1002, None ) ) + val strNullable = (x:Option[String]) => Nullable(x.map(StringFormattable(_))) + + database.transaction { tx => + tx.executeBatch( "insert into transactionspec values(?, ?)" ) { statement => + items.foreach { item => + statement << item.v1 << Nullable(item.v2.map(StringFormattable(_))) < + val rs = tx.select( "select id, name from transactionspec where id > 1000" ) { row => + Item( row, row ) + } + + rs should contain (items.head) + rs should contain (items.last) + } + } } } -} \ No newline at end of file +}