-
Notifications
You must be signed in to change notification settings - Fork 2
/
SqlGeneratorDb2.scala
99 lines (82 loc) · 3.44 KB
/
SqlGeneratorDb2.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Copyright 2022 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package za.co.absa.pramen.core.sql
import za.co.absa.pramen.api.sql.{SqlColumnType, SqlConfig, SqlGeneratorBase}
import java.time.LocalDate
import java.time.format.DateTimeFormatter
class SqlGeneratorDb2(sqlConfig: SqlConfig) extends SqlGeneratorBase(sqlConfig) {
private val dateFormatterApp = DateTimeFormatter.ofPattern(sqlConfig.dateFormatApp)
override val beginEndEscapeChars: (Char, Char) = ('\"', '\"')
override def getDtable(sql: String): String = {
if (sql.exists(_ == ' ')) {
getAliasExpression(s"($sql)", "T")
} else {
sql
}
}
override def getCountQuery(tableName: String): String = {
s"SELECT ${getAliasExpression("COUNT(*)", "CNT")} FROM ${escape(tableName)}"
}
override def getCountQuery(tableName: String, infoDateBegin: LocalDate, infoDateEnd: LocalDate): String = {
val where = getWhere(infoDateBegin, infoDateEnd)
s"SELECT ${getAliasExpression("COUNT(*)", "CNT")} FROM ${escape(tableName)} WHERE $where"
}
override def getCountQueryForSql(filteredSql: String): String = {
s"SELECT COUNT(*) FROM ($filteredSql) AS query"
}
override def getDataQuery(tableName: String, columns: Seq[String], limit: Option[Int]): String = {
s"SELECT ${columnExpr(columns)} FROM ${escape(tableName)}${getLimit(limit)}"
}
override def getDataQuery(tableName: String, infoDateBegin: LocalDate, infoDateEnd: LocalDate, columns: Seq[String], limit: Option[Int]): String = {
val where = getWhere(infoDateBegin, infoDateEnd)
s"SELECT ${columnExpr(columns)} FROM ${escape(tableName)} WHERE $where${getLimit(limit)}"
}
override def getWhere(dateBegin: LocalDate, dateEnd: LocalDate): String = {
val dateBeginLit = getDateLiteral(dateBegin)
val dateEndLit = getDateLiteral(dateEnd)
val dateTypes: Array[SqlColumnType] = Array(SqlColumnType.DATETIME)
val infoDateColumnAdjusted =
if (dateTypes.contains(sqlConfig.infoDateType)) {
s"CAST($infoDateColumn AS DATE)"
} else {
infoDateColumn
}
if (dateBeginLit == dateEndLit) {
s"$infoDateColumnAdjusted = $dateBeginLit"
} else {
s"$infoDateColumnAdjusted >= $dateBeginLit AND $infoDateColumnAdjusted <= $dateEndLit"
}
}
override def getDateLiteral(date: LocalDate): String = {
sqlConfig.infoDateType match {
case SqlColumnType.DATE =>
val dateStr = DateTimeFormatter.ISO_LOCAL_DATE.format(date)
s"DATE '$dateStr'"
case SqlColumnType.DATETIME =>
val dateStr = DateTimeFormatter.ISO_LOCAL_DATE.format(date)
s"DATE '$dateStr'"
case SqlColumnType.STRING =>
val dateStr = dateFormatterApp.format(date)
s"'$dateStr'"
case SqlColumnType.NUMBER =>
val dateStr = dateFormatterApp.format(date)
s"$dateStr"
}
}
private def getLimit(limit: Option[Int]): String = {
limit.map(n => s" LIMIT $n").getOrElse("")
}
}