-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#378 Add support for MySQL databases in the JDBC source.
- Loading branch information
Showing
4 changed files
with
213 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
pramen/core/src/main/scala/za/co/absa/pramen/core/sql/SqlGeneratorMySQL.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* 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 SqlGeneratorMySQL(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(_ == ' ')) { | ||
s"($sql) t" | ||
} else { | ||
sql | ||
} | ||
} | ||
|
||
override def getCountQuery(tableName: String): String = { | ||
s"SELECT COUNT(*) FROM ${escape(tableName)}" | ||
} | ||
|
||
override def getCountQuery(tableName: String, infoDateBegin: LocalDate, infoDateEnd: LocalDate): String = { | ||
val where = getWhere(infoDateBegin, infoDateEnd) | ||
s"SELECT COUNT(*) FROM ${escape(tableName)} WHERE $where" | ||
} | ||
|
||
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"DATE($infoDateColumn)" | ||
} 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"'$dateStr'" | ||
case SqlColumnType.DATETIME => | ||
val dateStr = DateTimeFormatter.ISO_LOCAL_DATE.format(date) | ||
s"'$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("") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters