Skip to content

Refactor: decompose conditional, extract SqlTransformerBuilder, and replace conditional with polymorphism in SqlTransformer#1778

Closed
akanksharaut2662-rgb wants to merge 2 commits intodatafaker-net:mainfrom
akanksharaut2662-rgb:refactor/sqltransformer-refactoring
Closed

Refactor: decompose conditional, extract SqlTransformerBuilder, and replace conditional with polymorphism in SqlTransformer#1778
akanksharaut2662-rgb wants to merge 2 commits intodatafaker-net:mainfrom
akanksharaut2662-rgb:refactor/sqltransformer-refactoring

Conversation

@akanksharaut2662-rgb
Copy link
Copy Markdown

@akanksharaut2662-rgb akanksharaut2662-rgb commented Mar 13, 2026

Summary

This PR introduces three structural refactoring improvements in the sql transformation package to improve readability, separation of responsibilities, and object-oriented design.

Changes Made

  1. SqlTransformer.java - Decompose Conditional
    Refactored the method isSqlQuoteIdentifierRequiredFor() by decomposing a complex conditional expression into smaller helper methods. This separates the logical concerns and makes the intent of each condition clearer.

  2. SqlTransformerBuilder - Extract Class
    Extracted the SqlTransformerBuilder nested class into its own dedicated file.

  3. SQLKeyWords.java - Replace Conditional with Polymorphism
    Refactored SQLKeyWords.getValue() to eliminate a switch conditional by moving behavior into the Case enum.

Testing

  1. No behavior change
  2. All existing tests pass

Notes

This PR introduces only structural refactoring improvements to improve maintainability and clarity without altering application behavior.

…and replace conditional with polymorphism in SQLKeyWords
@what-the-diff
Copy link
Copy Markdown

what-the-diff bot commented Mar 13, 2026

PR Summary

  • Refactored the SqlTransformer class
    The existing logic in the SqlTransformer class was a bit complex. In order to simplify it, the code for building (i.e., setting up) the SqlTransformer has now been moved to a new, dedicated class called SqlTransformerBuilder.

  • New way for instantiating SqlTransformer class
    A new, easier way for creating an instance of SqlTransformer is added. This method, named create, can use various input parameters including dialect and casing options which can be useful to handle different types of database systems.

  • Improved approach to determine if SQL quoting is required
    The code now includes a more efficient way to decide if SQL statements require quoting. This improvement is achieved through helper methods requiresQuotingForCasing and containsSpecialCharacter.

  • Enhanced string transformations with Case enum
    The enumeration Case which is responsible for string transformations, has been enhanced for better handling.

  • Added new features to the SqlTransformerBuilder class
    The new SqlTransformerBuilder class now has methods for setting parameters like quote, schemaName, and batchSize. You can also use the build method to generate an instance of SqlTransformer, providing it with the necessary configurations.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Mar 14, 2026

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 92.72727% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.49%. Comparing base (66aeb85) to head (725c054).

Files with missing lines Patch % Lines
.../datafaker/transformations/sql/SqlTransformer.java 76.47% 1 Missing and 3 partials ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #1778      +/-   ##
============================================
+ Coverage     92.37%   92.49%   +0.12%     
- Complexity     3448     3466      +18     
============================================
  Files           339      340       +1     
  Lines          6794     6798       +4     
  Branches        670      666       -4     
============================================
+ Hits           6276     6288      +12     
+ Misses          353      348       -5     
+ Partials        165      162       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the SQL transformation utilities by extracting SqlTransformer’s builder into a standalone class and doing small internal cleanups to SQL keyword casing and identifier-quoting logic.

Changes:

  • Extract SqlTransformerBuilder into src/main/java/net/datafaker/transformations/sql/SqlTransformerBuilder.java and route SqlTransformer.builder() through it.
  • Introduce a package-private SqlTransformer.create(...) factory used by the new builder.
  • Refactor identifier-quoting checks and simplify SQL keyword case selection via Case.apply(...).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
src/main/java/net/datafaker/transformations/sql/SqlTransformerBuilder.java New standalone builder that constructs SqlTransformer instances via a factory method.
src/main/java/net/datafaker/transformations/sql/SqlTransformer.java Updates builder entry point, adds factory method, refactors quoting checks, and adjusts keyword case handling.

Comment on lines +46 to +48
public static <IN> SqlTransformerBuilder<IN> builder() {
return new SqlTransformerBuilder<>();
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah the tests already show this is an issue

Comment on lines +52 to +53
return new SqlTransformer<>(schemaName, tableName, quote, dialect, sqlIdentifier,
casing, withBatchMode, batchSize, keywordCase, forceSqlQuoteIdentifierUsage);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use the spotless apply task to fix formatting

|| name.charAt(i) == DEFAULT_CATALOG_SEPARATOR) {
char currentChar = name.charAt(i);
if (requiresQuotingForCasing(currentChar)
||containsSpecialCharacter(currentChar)) {
Comment on lines +84 to +85
return casing == Casing.TO_UPPER && Character.isLowerCase(c)
|| casing == Casing.TO_LOWER && Character.isUpperCase(c);
Comment on lines +84 to +91
return casing == Casing.TO_UPPER && Character.isLowerCase(c)
|| casing == Casing.TO_LOWER && Character.isUpperCase(c);
}

private boolean containsSpecialCharacter(char c) {
return c == openSqlIdentifier
|| c == closeSqlIdentifier
|| c == DEFAULT_CATALOG_SEPARATOR;
Copy link
Copy Markdown
Author

@akanksharaut2662-rgb akanksharaut2662-rgb Mar 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review @kingthorin I've addressed all the feedback:

  • Reintroduced SqlTransformer.SqlTransformerBuilder as a deprecated wrapper extending the new top-level class to avoid breaking API changes
  • Fixed formatting and indentation in create(), requiresQuotingForCasing(), containsSpecialCharacter() and the || spacing issue
  • Added unit tests to improve coverage

…er alias, fix formatting, and added Test cases for SqlTransformer for better coverage
@bodiam
Copy link
Copy Markdown
Contributor

bodiam commented Mar 14, 2026

AI PR

@bodiam bodiam closed this Mar 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants