-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#301 Make SQL generators extensible so that custom generators could be plugged in without Pramen recompilation #352
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c74eee3
#301 Move SqlGenerator interface along with SqlGeneratorBase to the A…
yruslan 05a6caa
#245 Add the ability to specify and load a custom SQL generator class.
yruslan b5425ac
#245 Update README to include the new feature.
yruslan db6db96
#353 Fix PR suggestion and make a couple of methods private.
yruslan dc6c028
Fix a redundant println().
yruslan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
81 changes: 81 additions & 0 deletions
81
pramen/api/src/main/scala/za/co/absa/pramen/api/sql/SqlGenerator.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,81 @@ | ||
/* | ||
* 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.api.sql | ||
|
||
import java.sql.Connection | ||
import java.time.LocalDate | ||
|
||
trait SqlGenerator { | ||
/** | ||
* Returns wrapped query that can be passed as .option("dtable", here) to the Spark JDBC reader. | ||
* For example, given "SELECT * FROM abc", returns "(SELECT * FROM abc) tbl" | ||
*/ | ||
def getDtable(sql: String): String | ||
|
||
/** | ||
* Generates a query that returns the record count of a table that does not have the information date field. | ||
*/ | ||
def getCountQuery(tableName: String): String | ||
|
||
/** | ||
* Generates a query that returns the record count of a table for the given period when the table does have the information date field. | ||
*/ | ||
def getCountQuery(tableName: String, infoDateBegin: LocalDate, infoDateEnd: LocalDate): String | ||
|
||
/** | ||
* Generates a query that returns data of a table that does not have the information date field. | ||
*/ | ||
def getDataQuery(tableName: String, columns: Seq[String], limit: Option[Int]): String | ||
|
||
/** | ||
* Generates a query that returns the data of a table for the given period when the table does have the information date field. | ||
*/ | ||
def getDataQuery(tableName: String, infoDateBegin: LocalDate, infoDateEnd: LocalDate, columns: Seq[String], limit: Option[Int]): String | ||
|
||
/** | ||
* Returns WHERE condition for table that has the information date field given the time period. | ||
*/ | ||
def getWhere(dateBegin: LocalDate, dateEnd: LocalDate): String | ||
|
||
/** Returns the date literal for the dialect of the SQL. */ | ||
def getDateLiteral(date: LocalDate): String | ||
|
||
/** | ||
* Validates and escapes an identifier (table or column name) if needed. | ||
* Escaping happens according to the quoting policy. | ||
*/ | ||
def escape(identifier: String): String | ||
|
||
/** | ||
* Quotes an identifier name with characters specific to SQL dialects. | ||
* If the identifier is already wrapped, it won't be double wrapped. | ||
* It supports partially quoted identifiers. E.g. '"my_catalog".my table' will be quoted as '"my_catalog"."my table"'. | ||
*/ | ||
def quote(identifier: String): String | ||
|
||
/** | ||
* Returns true if the SQL generator can only work if it has an active connection. | ||
* This can be for database engines that does not support "SELECT * FROM table" and require explicit list of columns. | ||
* The connection can be used to query the list of columns first. | ||
*/ | ||
def requiresConnection: Boolean = false | ||
|
||
/** | ||
* Sets the connection for the the SQL generator can only work if it has an active connection. | ||
*/ | ||
def setConnection(connection: Connection): Unit = {} | ||
} |
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
44 changes: 44 additions & 0 deletions
44
pramen/api/src/test/scala/za/co/absa/pramen/api/sql/QuotingPolicySuite.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,44 @@ | ||
/* | ||
* 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.api.sql | ||
|
||
import org.scalatest.wordspec.AnyWordSpec | ||
|
||
class QuotingPolicySuite extends AnyWordSpec { | ||
"QuotingPolicy.fromString" should { | ||
"return Never for 'never'" in { | ||
assert(QuotingPolicy.fromString("never") == QuotingPolicy.Never) | ||
assert(QuotingPolicy.fromString("never").name == "never") | ||
} | ||
|
||
"return Always for 'always'" in { | ||
assert(QuotingPolicy.fromString("always") == QuotingPolicy.Always) | ||
assert(QuotingPolicy.fromString("always").name == "always") | ||
} | ||
|
||
"return Auto for 'auto'" in { | ||
assert(QuotingPolicy.fromString("auto") == QuotingPolicy.Auto) | ||
assert(QuotingPolicy.fromString("auto").name == "auto") | ||
} | ||
|
||
"throw an exception for an unknown quoting policy" in { | ||
assertThrows[IllegalArgumentException] { | ||
QuotingPolicy.fromString("unknown") | ||
} | ||
} | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you leave the methods
fromDriverName
andfromClass
with public access on purpose?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, it indeed makes sense to make the private since the object has
getSqlGenerator()
method that can be used in both cases.