@@ -18,16 +18,18 @@ package za.co.absa.db.balta.classes
1818
1919import za .co .absa .db .balta .classes .inner .Params
2020import za .co .absa .db .balta .classes .inner .Params .NamedParams
21- import za .co .absa .db .balta .typeclasses .{QueryParamValue , QueryParamType }
21+ import za .co .absa .db .balta .typeclasses .{QueryParamType , QueryParamValue }
2222import za .co .absa .db .balta .classes .inner .Params .OrderedParams
23+ import za .co .absa .db .mag .core .SqlEntry
24+ import za .co .absa .db .mag .core .SqlEntryComposition ._
2325
2426/**
2527 * This class represents a database table. It allows to perform INSERT, SELECT and COUNT operations on the table easily.
2628 *
2729 * @param tableName The name of the table
2830 */
2931case class DBTable (tableName : String ) extends DBQuerySupport {
30-
32+ private val table : SqlEntry = SqlEntry (tableName)
3133 /**
3234 * Inserts a new row into the table.
3335 *
@@ -42,7 +44,7 @@ case class DBTable(tableName: String) extends DBQuerySupport{
4244 }
4345
4446 val paramStr = values.values.map(_.sqlEntry).mkString(" ," )
45- val sql = s " INSERT INTO $tableName $ columns VALUES( $ paramStr) RETURNING *; "
47+ val sql = INSERT INTO table( columns) VALUES (paramStr) RETURNING ALL
4648 runQuery(sql, values.values){_.next()}
4749 }
4850
@@ -78,7 +80,7 @@ case class DBTable(tableName: String) extends DBQuerySupport{
7880 * @return - the result of the verify function
7981 */
8082 def where [R ](params : NamedParams )(verify : QueryResult => R )(implicit connection : DBConnection ): R = {
81- composeSelectAndRun(strToOption( paramsToWhereCondition(params)) , None , params.values)(verify)
83+ composeSelectAndRun(paramsToWhereCondition(params).toOption , None , params.values)(verify)
8284 }
8385
8486 /**
@@ -91,7 +93,7 @@ case class DBTable(tableName: String) extends DBQuerySupport{
9193 * @return - the result of the verify function
9294 */
9395 def where [R ](params : NamedParams , orderBy : String )(verify : QueryResult => R )(implicit connection : DBConnection ): R = {
94- composeSelectAndRun(strToOption( paramsToWhereCondition(params)), strToOption (orderBy), params.values)(verify)
96+ composeSelectAndRun(paramsToWhereCondition(params).toOption, SqlEntry (orderBy).toOption , params.values)(verify)
9597 }
9698
9799 /**
@@ -103,7 +105,7 @@ case class DBTable(tableName: String) extends DBQuerySupport{
103105 * @return - the result of the verify function
104106 */
105107 def where [R ](condition : String )(verify : QueryResult => R )(implicit connection : DBConnection ): R = {
106- composeSelectAndRun(strToOption (condition), None )(verify)
108+ composeSelectAndRun(SqlEntry (condition).toOption , None )(verify)
107109 }
108110
109111 /**
@@ -116,7 +118,7 @@ case class DBTable(tableName: String) extends DBQuerySupport{
116118 * @return - the result of the verify function
117119 */
118120 def where [R ](condition : String , orderBy : String )(verify : QueryResult => R )(implicit connection : DBConnection ): R = {
119- composeSelectAndRun(strToOption (condition), strToOption (orderBy))(verify)
121+ composeSelectAndRun(SqlEntry (condition).toOption, SqlEntry (orderBy).toOption )(verify)
120122 }
121123
122124 /**
@@ -141,27 +143,27 @@ case class DBTable(tableName: String) extends DBQuerySupport{
141143 * @return - the result of the verify function
142144 */
143145 def all [R ](orderBy : String )(verify : QueryResult => R )(implicit connection : DBConnection ): R = {
144- composeSelectAndRun(None , strToOption (orderBy))(verify)
146+ composeSelectAndRun(None , SqlEntry (orderBy).toOption )(verify)
145147 }
146148
147149 def deleteWithCheck [R ](verify : QueryResult => R )(implicit connection : DBConnection ): R = {
148150 composeDeleteAndRun(None )(verify)
149151 }
150152
151153 def deleteWithCheck [R ](whereParams : NamedParams )(verify : QueryResult => R )(implicit connection : DBConnection ): R = {
152- composeDeleteAndRun(strToOption( paramsToWhereCondition(whereParams)) , whereParams.values)(verify)
154+ composeDeleteAndRun(paramsToWhereCondition(whereParams).toOption , whereParams.values)(verify)
153155 }
154156
155157 def deleteWithCheck [R ](whereCondition : String )(verify : QueryResult => R )(implicit connection : DBConnection ): R = {
156- composeDeleteAndRun(strToOption (whereCondition))(verify)
158+ composeDeleteAndRun(SqlEntry (whereCondition).toOption )(verify)
157159 }
158160
159161 def delete (whereParams : NamedParams )(implicit connection : DBConnection ): Unit = {
160- composeDeleteAndRun(strToOption( paramsToWhereCondition(whereParams)) , whereParams.values)(_ => ())
162+ composeDeleteAndRun(paramsToWhereCondition(whereParams).toOption , whereParams.values)(_ => ())
161163 }
162164
163165 def delete (whereCondition : String = " " )(implicit connection : DBConnection ): Unit = {
164- composeDeleteAndRun(strToOption (whereCondition))(_ => ())
166+ composeDeleteAndRun(SqlEntry (whereCondition).toOption )(_ => ())
165167 }
166168 /**
167169 * Counts the rows in the table.
@@ -180,7 +182,7 @@ case class DBTable(tableName: String) extends DBQuerySupport{
180182 */
181183 @ deprecated(" Use countOnCondition instead" , " 0.2.0" )
182184 def count (params : NamedParams )(implicit connection : DBConnection ): Long = {
183- composeCountAndRun(strToOption( paramsToWhereCondition(params)) , params.values)
185+ composeCountAndRun(paramsToWhereCondition(params).toOption , params.values)
184186 }
185187
186188 /**
@@ -191,7 +193,7 @@ case class DBTable(tableName: String) extends DBQuerySupport{
191193 */
192194 @ deprecated(" Use countOnCondition instead" , " 0.2.0" )
193195 def count (condition : String )(implicit connection : DBConnection ): Long = {
194- composeCountAndRun(strToOption (condition))
196+ composeCountAndRun(SqlEntry (condition).toOption )
195197 }
196198
197199 /**
@@ -201,7 +203,7 @@ case class DBTable(tableName: String) extends DBQuerySupport{
201203 * @return - the number of rows
202204 */
203205 def countOnCondition (params : NamedParams )(implicit connection : DBConnection ): Long = {
204- composeCountAndRun(strToOption( paramsToWhereCondition(params)) , params.values)
206+ composeCountAndRun(paramsToWhereCondition(params).toOption , params.values)
205207 }
206208
207209 /**
@@ -211,47 +213,36 @@ case class DBTable(tableName: String) extends DBQuerySupport{
211213 * @return - the number of rows
212214 */
213215 def countOnCondition (condition : String )(implicit connection : DBConnection ): Long = {
214- composeCountAndRun(strToOption (condition))
216+ composeCountAndRun(SqlEntry (condition).toOption )
215217 }
216218
217- private def composeSelectAndRun [R ](whereCondition : Option [String ], orderByExpr : Option [String ], values : Vector [QueryParamValue ] = Vector .empty)
219+ private def composeSelectAndRun [R ](whereCondition : Option [SqlEntry ], orderBy : Option [SqlEntry ], values : Vector [QueryParamValue ] = Vector .empty)
218220 (verify : QueryResult => R )
219221 (implicit connection : DBConnection ): R = {
220- val where = whereCondition.map(" WHERE " + _).getOrElse(" " )
221- val orderBy = orderByExpr.map(" ORDER BY " + _).getOrElse(" " )
222- val sql = s " SELECT * FROM $tableName $where $orderBy; "
222+ val sql = SELECT (ALL ) FROM table WHERE whereCondition ORDER BY (orderBy)
223223 runQuery(sql, values)(verify)
224224 }
225225
226- private def composeDeleteAndRun [R ](whereCondition : Option [String ], values : Vector [QueryParamValue ] = Vector .empty)
226+ private def composeDeleteAndRun [R ](whereCondition : Option [SqlEntry ], values : Vector [QueryParamValue ] = Vector .empty)
227227 (verify : QueryResult => R )
228228 (implicit connection : DBConnection ): R = {
229- val where = whereCondition.map(" WHERE " + _).getOrElse(" " )
230- val sql = s " DELETE FROM $tableName $where RETURNING *; "
229+ val sql = DELETE FROM table WHERE whereCondition RETURNING ALL
231230 runQuery(sql, values)(verify)
232231 }
233232
234- private def composeCountAndRun (whereCondition : Option [String ], values : Vector [QueryParamValue ] = Vector .empty)
233+ private def composeCountAndRun (whereCondition : Option [SqlEntry ], values : Vector [QueryParamValue ] = Vector .empty)
235234 (implicit connection : DBConnection ): Long = {
236- val where = whereCondition.map(" WHERE " + _).getOrElse(" " )
237- val sql = s " SELECT count(1) AS cnt FROM $tableName $where; "
235+ val sql = SELECT (COUNT_ALL ) FROM table WHERE whereCondition
238236 runQuery(sql, values) {resultSet =>
239237 resultSet.next().getLong(" cnt" ).getOrElse(0 )
240238 }
241239 }
242240
243- private def strToOption (str : String ): Option [String ] = {
244- if (str.isEmpty) {
245- None
246- } else {
247- Option (str)
248- }
249- }
250-
251- private def paramsToWhereCondition (params : NamedParams ): String = {
252- params.items.foldRight(List .empty[String ]) {case ((columnName, value), acc) =>
253- val condition = s " ${columnName.sqlEntry} ${value.equalityOperator} ${value.sqlEntry}"
241+ private def paramsToWhereCondition (params : NamedParams ): SqlEntry = {
242+ val resultList = params.items.foldRight(List .empty[SqlEntry ]) {case ((columnName, value), acc) =>
243+ val condition = columnName.sqlEntry + value.equalityOperator + value.sqlEntry
254244 condition :: acc
255- }.mkString(" AND " )
245+ }
246+ resultList.toSqlEntry(" AND " )
256247 }
257248}
0 commit comments