@@ -18,6 +18,7 @@ package za.co.absa.db.balta.classes
1818
1919import QueryResultRow ._
2020import za .co .absa .db .balta .implicits .MapImplicits .MapEnhancements
21+ import za .co .absa .db .mag .core .ColumnName
2122
2223import java .sql
2324import java .sql .{Date , ResultSet , ResultSetMetaData , Time , Types }
@@ -29,26 +30,16 @@ import java.util.UUID
2930 *
3031 * @param rowNumber - the number of the row in the result set
3132 * @param fields - the values of the row
32- * @param columnLabels - the names of the columns; class uses `columnLabel(s)` to refer to the column names in accordance
33- * to `java.sql.ResultSet`, which is is build around and that despite that aliases are not expected
34- * to appear here
33+ * @param columnNames - the names of the columns
3534 */
3635class QueryResultRow private [classes](val rowNumber : Int ,
3736 private val fields : Vector [Option [Object ]],
38- private val columnLabels : ColumnNames ) {
37+ private val columnNames : ColumnNames ) {
3938
4039 def columnCount : Int = fields.length
41- def columnNumber (columnLabel : String ): Int = {
42- val quotedRegex = """ ^"(.+)"$""" .r
43- columnLabel match {
44- case quotedRegex(innerLabel) => // in case the column label is quoted, first try to find it as is, then without quotes
45- columnLabels.getOrElse(
46- columnLabel,
47- columnLabels.getOrThrow(innerLabel, new NoSuchElementException (s " Column ' $columnLabel' not found " ))
48- )
49- case _ => columnLabels.getOrThrow(columnLabel, new NoSuchElementException (s " Column ' $columnLabel' not found " ))
50- }
51-
40+ def columnNumber (columnName : String ): Int = {
41+ val columnNameQuoteless = ColumnName (columnName).quoteLess
42+ columnNames.getOrThrow(columnNameQuoteless, new NoSuchElementException (s " Column ' $columnNameQuoteless' not found " ))
5243 }
5344
5445 /**
@@ -59,22 +50,22 @@ class QueryResultRow private[classes](val rowNumber: Int,
5950 def apply (column : Int ): Option [Any ] = getObject(column - 1 )
6051 /**
6152 * Extracts a value from the row by column name.
62- * @param columnLabel - the name of the column
53+ * @param columnName - the name of the column
6354 * @return - the value stored in the column, type `Any` is for warningless comparison with any type
6455 */
65- def apply (columnLabel : String ): Option [Any ] = getObject(columnNumber(columnLabel ))
56+ def apply (columnName : String ): Option [Any ] = getObject(columnNumber(columnName ))
6657
6758 def getAs [T ](column : Int , transformer : TransformerFnc [T ]): Option [T ] = getObject(column).map(transformer)
6859 def getAs [T ](column : Int ): Option [T ] = getObject(column)map(_.asInstanceOf [T ])
6960
70- def getAs [T ](columnLabel : String , transformer : TransformerFnc [T ]): Option [T ] = getAs(columnNumber(columnLabel ), transformer)
71- def getAs [T ](columnLabel : String ): Option [T ] = getObject(columnNumber(columnLabel )).map(_.asInstanceOf [T ])
61+ def getAs [T ](columnName : String , transformer : TransformerFnc [T ]): Option [T ] = getAs(columnNumber(columnName ), transformer)
62+ def getAs [T ](columnName : String ): Option [T ] = getObject(columnNumber(columnName )).map(_.asInstanceOf [T ])
7263
7364 def getObject (column : Int ): Option [Object ] = fields(column - 1 )
74- def getObject (columnLabel : String ): Option [Object ] = getObject(columnNumber(columnLabel ))
65+ def getObject (columnName : String ): Option [Object ] = getObject(columnNumber(columnName ))
7566
7667 def getBoolean (column : Int ): Option [Boolean ] = getAs(column : Int , {item : Object => item.asInstanceOf [Boolean ]})
77- def getBoolean (columnLabel : String ): Option [Boolean ] = getBoolean(columnNumber(columnLabel ))
68+ def getBoolean (columnName : String ): Option [Boolean ] = getBoolean(columnNumber(columnName ))
7869
7970 def getChar (column : Int ): Option [Char ] = {
8071 getString(column) match {
@@ -84,45 +75,45 @@ class QueryResultRow private[classes](val rowNumber: Int,
8475 None
8576 }
8677 }
87- def getChar (columnLabel : String ): Option [Char ] = getChar(columnNumber(columnLabel ))
78+ def getChar (columnName : String ): Option [Char ] = getChar(columnNumber(columnName ))
8879
8980 def getString (column : Int ): Option [String ] = getAs(column : Int , {item : Object => item.asInstanceOf [String ]})
90- def getString (columnLabel : String ): Option [String ] = getString(columnNumber(columnLabel ))
81+ def getString (columnName : String ): Option [String ] = getString(columnNumber(columnName ))
9182
9283 def getInt (column : Int ): Option [Int ] = getAs(column : Int , {item : Object => item.asInstanceOf [Int ]})
93- def getInt (columnLabel : String ): Option [Int ] = getInt(columnNumber(columnLabel ))
84+ def getInt (columnName : String ): Option [Int ] = getInt(columnNumber(columnName ))
9485
9586 def getLong (column : Int ): Option [Long ] = getAs(column : Int , {item : Object => item.asInstanceOf [Long ]})
96- def getLong (columnLabel : String ): Option [Long ] = getLong(columnNumber(columnLabel ))
87+ def getLong (columnName : String ): Option [Long ] = getLong(columnNumber(columnName ))
9788
9889 def getDouble (column : Int ): Option [Double ] = getAs(column : Int , {item : Object => item.asInstanceOf [Double ]})
99- def getDouble (columnLabel : String ): Option [Double ] = getDouble(columnNumber(columnLabel ))
90+ def getDouble (columnName : String ): Option [Double ] = getDouble(columnNumber(columnName ))
10091
10192 def getFloat (column : Int ): Option [Float ] = getAs(column : Int , {item : Object => item.asInstanceOf [Float ]})
102- def getFloat (columnLabel : String ): Option [Float ] = getFloat(columnNumber(columnLabel ))
93+ def getFloat (columnName : String ): Option [Float ] = getFloat(columnNumber(columnName ))
10394
10495 def getBigDecimal (column : Int ): Option [BigDecimal ] =
10596 getAs(column : Int , {item : Object => item.asInstanceOf [java.math.BigDecimal ]})
10697 .map(scala.math.BigDecimal (_))
107- def getBigDecimal (columnLabel : String ): Option [BigDecimal ] = getBigDecimal(columnNumber(columnLabel ))
98+ def getBigDecimal (columnName : String ): Option [BigDecimal ] = getBigDecimal(columnNumber(columnName ))
10899
109100 def getTime (column : Int ): Option [Time ] = getAs(column : Int , {item : Object => item.asInstanceOf [Time ]})
110- def getTime (columnLabel : String ): Option [Time ] = getTime(columnNumber(columnLabel ))
101+ def getTime (columnName : String ): Option [Time ] = getTime(columnNumber(columnName ))
111102
112103 def getDate (column : Int ): Option [Date ] = getAs(column : Int , {item : Object => item.asInstanceOf [Date ]})
113- def getDate (columnLabel : String ): Option [Date ] = getDate(columnNumber(columnLabel ))
104+ def getDate (columnName : String ): Option [Date ] = getDate(columnNumber(columnName ))
114105
115106 def getLocalDateTime (column : Int ): Option [LocalDateTime ] = getAs(column : Int , {item : Object => item.asInstanceOf [LocalDateTime ]})
116- def getLocalDateTime (columnLabel : String ): Option [LocalDateTime ] = getLocalDateTime(columnNumber(columnLabel ))
107+ def getLocalDateTime (columnName : String ): Option [LocalDateTime ] = getLocalDateTime(columnNumber(columnName ))
117108
118109 def getOffsetDateTime (column : Int ): Option [OffsetDateTime ] = getAs(column : Int , {item : Object => item.asInstanceOf [OffsetDateTime ]})
119- def getOffsetDateTime (columnLabel : String ): Option [OffsetDateTime ] = getOffsetDateTime(columnNumber(columnLabel ))
110+ def getOffsetDateTime (columnName : String ): Option [OffsetDateTime ] = getOffsetDateTime(columnNumber(columnName ))
120111
121112 def getInstant (column : Int ): Option [Instant ] = getOffsetDateTime(column).map(_.toInstant)
122- def getInstant (columnLabel : String ): Option [Instant ] = getOffsetDateTime(columnLabel ).map(_.toInstant)
113+ def getInstant (columnName : String ): Option [Instant ] = getOffsetDateTime(columnName ).map(_.toInstant)
123114
124115 def getUUID (column : Int ): Option [UUID ] = getAs(column : Int , {item : Object => item.asInstanceOf [UUID ]})
125- def getUUID (columnLabel : String ): Option [UUID ] = getUUID(columnNumber(columnLabel ))
116+ def getUUID (columnName : String ): Option [UUID ] = getUUID(columnNumber(columnName ))
126117
127118 def getArray [T ](column : Int ): Option [Vector [T ]] = {
128119 def transformerFnc (obj : Object ): Vector [T ] = {
@@ -131,7 +122,7 @@ class QueryResultRow private[classes](val rowNumber: Int,
131122 getAs(column : Int , transformerFnc _)
132123 }
133124
134- def getArray [T ](columnLabel : String ): Option [Vector [T ]] = getArray[T ](columnNumber(columnLabel ))
125+ def getArray [T ](columnName : String ): Option [Vector [T ]] = getArray[T ](columnNumber(columnName ))
135126
136127 def getArray [T ](column : Int , itemTransformerFnc : TransformerFnc [T ]): Option [Vector [T ]] = {
137128 def transformerFnc (obj : Object ): Vector [T ] = {
@@ -159,7 +150,7 @@ object QueryResultRow {
159150 new QueryResultRow (resultSet.getRow, fields, columnNames)
160151 }
161152
162- def fieldNamesFromMetadata (metaData : ResultSetMetaData ): ColumnNames = {
153+ def columnNamesFromMetadata (metaData : ResultSetMetaData ): ColumnNames = {
163154 Range .inclusive(1 , metaData.getColumnCount).map(i => metaData.getColumnName(i) -> i).toMap
164155 }
165156
0 commit comments