From de43e0915f2b23fde2ac064006c8a70872a2ba1a Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Mon, 30 Dec 2013 20:15:08 +0400 Subject: [PATCH 1/2] make ReusableStatement public to allow adding types without library modification --- src/main/scala/net/noerd/prequel/ReusableStatement.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/scala/net/noerd/prequel/ReusableStatement.scala b/src/main/scala/net/noerd/prequel/ReusableStatement.scala index f6a669b..d2a2aec 100644 --- a/src/main/scala/net/noerd/prequel/ReusableStatement.scala +++ b/src/main/scala/net/noerd/prequel/ReusableStatement.scala @@ -18,7 +18,7 @@ import org.joda.time.DateTime * ## Set parameters and execute in on shot * statement.executeWith( param1, param2, param3 ) */ -private class ReusableStatement( val wrapped: PreparedStatement, formatter: SQLFormatter ) { +class ReusableStatement( val wrapped: PreparedStatement, formatter: SQLFormatter ) { private val StartIndex = 1 private var parameterIndex = StartIndex From f2755dd8be553413028cff1a74bad94a2ac5849e Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Mon, 30 Dec 2013 21:21:55 +0400 Subject: [PATCH 2/2] support BigDecimal --- .../scala/net/noerd/prequel/ColumnTypes.scala | 13 +++++++++++- .../net/noerd/prequel/Formattables.scala | 20 ++++++++++++++++++- .../net/noerd/prequel/ResultSetRow.scala | 5 ++++- .../net/noerd/prequel/ReusableStatement.scala | 7 ++++++- .../net/noerd/prequel/SQLFormatter.scala | 1 + 5 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/main/scala/net/noerd/prequel/ColumnTypes.scala b/src/main/scala/net/noerd/prequel/ColumnTypes.scala index adbee4b..c83ffb8 100644 --- a/src/main/scala/net/noerd/prequel/ColumnTypes.scala +++ b/src/main/scala/net/noerd/prequel/ColumnTypes.scala @@ -102,4 +102,15 @@ class BinaryColumnType( row: ResultSetRow ) extends ColumnType[ Array[Byte] ] { } object BinaryColumnType extends ColumnTypeFactory[ Array[Byte] ] { def apply( row: ResultSetRow ) = new BinaryColumnType( row ) -} \ No newline at end of file +} + +// +// BigDecimal +// + +class BigDecimalColumnType( row: ResultSetRow ) extends ColumnType[ BigDecimal ] { + override def nextValueOption: Option[ BigDecimal ] = row.nextBigDecimal +} +object BigDecimalColumnType extends ColumnTypeFactory[ BigDecimal ] { + def apply( row: ResultSetRow ) = new BigDecimalColumnType( row ) +} diff --git a/src/main/scala/net/noerd/prequel/Formattables.scala b/src/main/scala/net/noerd/prequel/Formattables.scala index 16968f2..5375398 100644 --- a/src/main/scala/net/noerd/prequel/Formattables.scala +++ b/src/main/scala/net/noerd/prequel/Formattables.scala @@ -184,4 +184,22 @@ class BinaryFormattable( val value: Array[Byte] ) extends Formattable { } object BinaryFormattable{ def apply( value: Array[Byte] ) = new BinaryFormattable( value ) -} \ No newline at end of file +} + +// +// BigDecimal +// +class BigDecimalFormattable( val value: BigDecimal ) + extends Formattable { + override def escaped( formatter: SQLFormatter ): String = { + formatter.toSQLString( value.toString() ) + } + override def addTo( statement: ReusableStatement ): Unit = { + statement.addBigDecimal( value ) + } +} +object BigDecimalFormattable{ + def apply( value: BigDecimal ) = { + new BigDecimalFormattable( value ) + } +} diff --git a/src/main/scala/net/noerd/prequel/ResultSetRow.scala b/src/main/scala/net/noerd/prequel/ResultSetRow.scala index 7d34e70..06ce6aa 100644 --- a/src/main/scala/net/noerd/prequel/ResultSetRow.scala +++ b/src/main/scala/net/noerd/prequel/ResultSetRow.scala @@ -29,7 +29,8 @@ class ResultSetRow( val rs: ResultSet ) { def nextDate: Option[ Date ] = nextValueOption( rs.getTimestamp ) def nextObject: Option[ AnyRef ] = nextValueOption( rs.getObject ) def nextBinary: Option[ Array[Byte] ] = nextValueOption( rs.getBytes ) - + def nextBigDecimal: Option[ BigDecimal ] = nextValueOption( i => BigDecimal(rs.getBigDecimal(i)) ) + def columnNames: Seq[ String ]= { val columnNames = ArrayBuffer.empty[ String ] val metaData = rs.getMetaData @@ -86,6 +87,7 @@ object ResultSetRowImplicits { implicit def row2DateTime( row: ResultSetRow ) = DateTimeColumnType( row ).nextValue implicit def row2Duration( row: ResultSetRow ) = DurationColumnType( row ).nextValue implicit def row2Binary( row: ResultSetRow ) = BinaryColumnType( row ).nextValue + implicit def row2BigDecimal( row: ResultSetRow ) = BigDecimalColumnType( row ).nextValue implicit def row2BooleanOption( row: ResultSetRow ) = BooleanColumnType( row ).nextValueOption implicit def row2IntOption( row: ResultSetRow ) = IntColumnType( row ).nextValueOption @@ -97,4 +99,5 @@ object ResultSetRowImplicits { implicit def row2DateTimeOption( row: ResultSetRow ) = DateTimeColumnType( row ).nextValueOption implicit def row2DurationOption( row: ResultSetRow ) = DurationColumnType( row ).nextValueOption implicit def row2BinaryOption( row: ResultSetRow ) = BinaryColumnType( row ).nextValueOption + implicit def row2BigDecimalOption( row: ResultSetRow ) = BigDecimalColumnType( row ).nextValueOption } \ No newline at end of file diff --git a/src/main/scala/net/noerd/prequel/ReusableStatement.scala b/src/main/scala/net/noerd/prequel/ReusableStatement.scala index d2a2aec..b4623d3 100644 --- a/src/main/scala/net/noerd/prequel/ReusableStatement.scala +++ b/src/main/scala/net/noerd/prequel/ReusableStatement.scala @@ -99,7 +99,12 @@ class ReusableStatement( val wrapped: PreparedStatement, formatter: SQLFormatter /** * Add a Double to the current parameter index */ - def addDouble( value: Double ): Unit = addValue( wrapped.setDouble( parameterIndex, value ) ) + def addDouble( value: Double ): Unit = addValue( wrapped.setDouble( parameterIndex, value ) ) + + /** + * Add a BigDecimal to the current parameter index + */ + def addBigDecimal( value: BigDecimal ): Unit = addValue( wrapped.setBigDecimal( parameterIndex, value.bigDecimal ) ) /** * Add Null to the current parameter index diff --git a/src/main/scala/net/noerd/prequel/SQLFormatter.scala b/src/main/scala/net/noerd/prequel/SQLFormatter.scala index e86a5d2..a5e865b 100644 --- a/src/main/scala/net/noerd/prequel/SQLFormatter.scala +++ b/src/main/scala/net/noerd/prequel/SQLFormatter.scala @@ -87,4 +87,5 @@ object SQLFormatterImplicits { implicit def date2Formattable( wrapped: Date ) = DateTimeFormattable( wrapped ) implicit def duration2Formattable( wrapped: Duration ) = new DurationFormattable( wrapped ) implicit def binary2Formattable( wrapped: Array[Byte] ) = new BinaryFormattable( wrapped ) + implicit def bigDecimalFormattable( wrapped: BigDecimal ) = new BigDecimalFormattable( wrapped ) } \ No newline at end of file