Skip to content

11421 should be able to add all missing fields and missing tables in one go #11426

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

Conversation

buddhika75
Copy link
Member

@buddhika75 buddhika75 commented Mar 29, 2025

now all create statements can be pasted and run the query

Summary by CodeRabbit

  • New Features
    • Enhanced the administrative functionality to dynamically process SQL table creation and alteration commands, providing improved error feedback.
    • Revamped the user interface by organizing options into three distinct tabs for table creation, addressing missing fields, and executing SQL commands, offering a more intuitive user experience.

Signed-off-by: Dr M H B Ariyaratne <buddhika.ari@gmail.com>
Copy link
Contributor

coderabbitai bot commented Mar 29, 2025

Walkthrough

The pull request introduces functionality enhancements in the DataAdministrationController by adding new member variables and a method to process SQL CREATE statements. It also restructures a user interface file to group related SQL operations into three organized tabs using an accordion panel. The changes improve dynamic SQL handling in the backend and enhance navigation and accessibility in the frontend.

Changes

File(s) Change Summary
src/main/java/com/divudi/bean/common/DataAdministrationController.java - Added member variables: allCreateStetements (String) and tabIndexMissingFields (int).
- Updated navigateToCheckMissingFields to initialize the new variables.
- Introduced createTablesAndFieldsForAllCreateStatements method to process SQL CREATE TABLE statements and generate/execute corresponding ALTER commands with error handling.
- Added getter and setter methods for the new variables.
src/main/webapp/resources/ezcomp/midding_data_fields.xhtml - Redesigned UI structure by replacing the single form with a <p:accordionPanel> containing three tabs.
- First tab: Form for executing SQL commands from DDL file contents.
- Second tab: Section for handling missing fields with individual SQL statements and error display.
- Third tab: New interface for inputting and executing direct SQL commands, with execution feedback.

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant C as DataAdministrationController
    participant I as ItemFacade

    U->>C: Trigger createTablesAndFieldsForAllCreateStatements()
    C->>C: Parse allCreateStetements and split SQL statements
    C->>C: Generate ALTER commands based on table names
    C->>I: Execute ALTER statement(s)
    I-->>C: Return execution result or error
    C->>U: Display executionFeedback
Loading

Poem

Oh, how the code doth hop and change,
Fields and tables in neat arrange.
From SQL lines to shifting tabs,
My furry paws give joyous claps!
In the realm of code I leap with glee—
Hop on, my friends, to a bug-free spree!
🐇💻

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (6)
src/main/webapp/resources/ezcomp/midding_data_fields.xhtml (2)

21-23: Fix typo in tab title.

There is a spelling error in the tab title which should be corrected for better user experience.

-                title="Add All Tables using create ddl fiel contents"  >
+                title="Add All Tables using create ddl file contents"  >

30-35: Inconsistent label naming.

The label "All Create Statements in ddl file" is more descriptive than the header "Run SQL Commands". Consider aligning them for better user clarity.

-                            <h:outputLabel value="Run SQL Commands" class="mx-2"/>
+                            <h:outputLabel value="All Create Statements" class="mx-2"/>
src/main/java/com/divudi/bean/common/DataAdministrationController.java (4)

280-280: Fix spelling in variable name.

The variable name contains a spelling error that should be corrected for consistency.

-    private String allCreateStetements;
+    private String allCreateStatements;

This spelling error appears throughout the code and should be fixed in all occurrences.


874-934: Implementation for batch processing of CREATE statements.

The new method successfully implements the ability to process multiple CREATE statements at once, as required. However, there are several areas for improvement:

  1. The method is quite long and could be refactored into smaller, more focused methods
  2. Error handling could be improved with more specific exception handling
  3. SQL statement execution could benefit from transaction management

Consider refactoring this large method into smaller, more focused methods:

public void createTablesAndFieldsForAllCreateStatements() {
    StringBuilder executionResults = new StringBuilder();
    
    System.out.println("===== Starting CREATE TABLE parsing and ALTER execution =====");
    
+   String[] createStatements = splitCreateStatements(allCreateStetements);
+   processCreateStatements(createStatements, executionResults);
    
-   // Split by CREATE TABLE, keeping it in the output
-   String[] rawParts = allCreateStetements.split("(?i)CREATE TABLE");
-   int counter = 0;
-
-   for (String part : rawParts) {
-       part = part.trim();
-       if (part.isEmpty()) {
-           continue;
-       }
-
-       String createStatement = "CREATE TABLE " + part;
-       System.out.println("\n[INFO] Processing statement #" + (++counter));
-       System.out.println("Create SQL:\n" + createStatement);
-
-       try {
-           String tableName = extractTableName(createStatement);
-           if (tableName == null || tableName.isEmpty()) {
-               System.out.println("[WARNING] Skipping statement — table name not found.");
-               executionResults.append("<br/>Skipped malformed CREATE TABLE statement.");
-               continue;
-           }
-
-           System.out.println("[INFO] Extracted table name: " + tableName);
-
-           String alterSql = generateAlterStatements(createStatement);
-           System.out.println("[INFO] Generated ALTER statements:\n" + alterSql);
-
-           String[] sqlStatements = alterSql.split(";");
-           for (String sql : sqlStatements) {
-               sql = sql.trim();
-               if (sql.isEmpty()) {
-                   continue;
-               }
-
-               try {
-                   System.out.println("[EXEC] Running SQL: " + sql);
-                   itemFacade.executeNativeSql(sql);
-                   executionResults.append("<br/>Successfully executed: ").append(sql);
-               } catch (Exception e) {
-                   System.out.println("[ERROR] SQL failed: " + sql);
-                   System.out.println("[ERROR] Exception: " + e.getMessage());
-                   executionResults.append("<br/>Failed to execute: ").append(sql);
-                   executionResults.append("<br/>Error: ").append(e.getMessage());
-               }
-           }
-       } catch (Exception e) {
-           System.out.println("[ERROR] Unexpected error while processing create statement.");
-           e.printStackTrace();
-           executionResults.append("<br/>Error processing create statement: ").append(e.getMessage());
-       }
-   }

    executionFeedback = executionResults.toString();

    System.out.println("===== All CREATE TABLE processing complete =====");
}

+ private String[] splitCreateStatements(String allStatements) {
+     // Split by CREATE TABLE, keeping it in the output
+     return allStatements.split("(?i)CREATE TABLE");
+ }
+
+ private void processCreateStatements(String[] rawParts, StringBuilder executionResults) {
+     int counter = 0;
+     
+     for (String part : rawParts) {
+         part = part.trim();
+         if (part.isEmpty()) {
+             continue;
+         }
+         
+         String createStatement = "CREATE TABLE " + part;
+         System.out.println("\n[INFO] Processing statement #" + (++counter));
+         
+         try {
+             processCreateStatement(createStatement, executionResults);
+         } catch (Exception e) {
+             System.out.println("[ERROR] Unexpected error while processing create statement.");
+             e.printStackTrace();
+             executionResults.append("<br/>Error processing create statement: ").append(e.getMessage());
+         }
+     }
+ }
+
+ private void processCreateStatement(String createStatement, StringBuilder executionResults) {
+     String tableName = extractTableName(createStatement);
+     if (tableName == null || tableName.isEmpty()) {
+         System.out.println("[WARNING] Skipping statement — table name not found.");
+         executionResults.append("<br/>Skipped malformed CREATE TABLE statement.");
+         return;
+     }
+     
+     System.out.println("[INFO] Extracted table name: " + tableName);
+     
+     String alterSql = generateAlterStatements(createStatement);
+     executeAlterStatements(alterSql, executionResults);
+ }
+
+ private void executeAlterStatements(String alterSql, StringBuilder executionResults) {
+     String[] sqlStatements = alterSql.split(";");
+     for (String sql : sqlStatements) {
+         sql = sql.trim();
+         if (sql.isEmpty()) {
+             continue;
+         }
+         
+         try {
+             System.out.println("[EXEC] Running SQL: " + sql);
+             itemFacade.executeNativeSql(sql);
+             executionResults.append("<br/>Successfully executed: ").append(sql);
+         } catch (Exception e) {
+             System.out.println("[ERROR] SQL failed: " + sql);
+             System.out.println("[ERROR] Exception: " + e.getMessage());
+             executionResults.append("<br/>Failed to execute: ").append(sql);
+             executionResults.append("<br/>Error: ").append(e.getMessage());
+         }
+     }
+ }

880-880: Use a regex pattern for better CREATE TABLE splitting.

The current splitting approach has some limitations that could cause issues with certain SQL statements.

Consider using a more robust pattern matching approach:

-   // Split by CREATE TABLE, keeping it in the output
-   String[] rawParts = allCreateStetements.split("(?i)CREATE TABLE");
+   // Use a pattern that better handles SQL syntax
+   Pattern createTablePattern = Pattern.compile("(?i)(CREATE\\s+TABLE\\s+[^;]+;)");
+   Matcher matcher = createTablePattern.matcher(allCreateStetements);
+   List<String> createStatements = new ArrayList<>();
+   
+   while (matcher.find()) {
+       createStatements.add(matcher.group(1));
+   }

894-901: Improve table name extraction validation.

The current table name extraction could be more robust to handle edge cases.

Consider using a more precise regex pattern:

-   String tableName = extractTableName(createStatement);
-   if (tableName == null || tableName.isEmpty()) {
-       System.out.println("[WARNING] Skipping statement — table name not found.");
-       executionResults.append("<br/>Skipped malformed CREATE TABLE statement.");
-       continue;
-   }
+   String tableName;
+   try {
+       tableName = extractTableName(createStatement);
+       if (tableName == null || tableName.isEmpty()) {
+           throw new IllegalArgumentException("Table name not found");
+       }
+   } catch (Exception e) {
+       System.out.println("[WARNING] Skipping statement — " + e.getMessage());
+       executionResults.append("<br/>Skipped malformed CREATE TABLE statement: " + e.getMessage());
+       continue;
+   }

Also update the extractTableName method to be more robust:

private String extractTableName(String sql) {
    sql = sql.trim();
    String upperSql = sql.toUpperCase();
-   int createIndex = upperSql.indexOf("CREATE TABLE");
-   int startIndex = createIndex + "CREATE TABLE".length();
-   // Skip any whitespace after 'CREATE TABLE'
-   while (Character.isWhitespace(sql.charAt(startIndex))) {
-       startIndex++;
-   }
-   int endIndex = sql.indexOf("(", startIndex);
-   String tableName = sql.substring(startIndex, endIndex).trim();
-   // Remove backticks or quotes if present
-   tableName = tableName.replaceAll("[`\"']", "");
+   // Use regex to extract table name
+   Pattern pattern = Pattern.compile("(?i)CREATE\\s+TABLE\\s+[`\"']?([^`\"'(\\s]+)[`\"']?\\s*\\(");
+   Matcher matcher = pattern.matcher(sql);
+   if (matcher.find()) {
+       return matcher.group(1);
+   }
+   throw new IllegalArgumentException("Could not parse table name from SQL: " + sql);
-   return tableName;
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 396701e and c6d98aa.

📒 Files selected for processing (2)
  • src/main/java/com/divudi/bean/common/DataAdministrationController.java (5 hunks)
  • src/main/webapp/resources/ezcomp/midding_data_fields.xhtml (1 hunks)
🔇 Additional comments (8)
src/main/webapp/resources/ezcomp/midding_data_fields.xhtml (5)

17-19: Accordion panel initialized with controller state variable.

The accordion panel is correctly configured with the active index bound to a controller variable, allowing state preservation across requests.


37-40: SQL execution button correctly configured.

The command button is properly set up with ajax="false" to ensure complete page reload after the operation, which is important for database operations.


52-112: Second tab correctly migrated to accordion format.

The original functionality for handling individual create statements has been properly migrated to the new accordion panel structure with all the necessary inputs and commands preserved.


114-146: Third tab provides simplified SQL execution.

The third tab provides a streamlined interface for running any SQL commands. This enhances usability by separating generic SQL execution from the more specific table and field creation operations.


17-148: Good UI organization with accordion panel.

The restructuring of the UI into three distinct tabs using an accordion panel significantly improves the user experience by logically grouping related functionality and reducing visual clutter.

src/main/java/com/divudi/bean/common/DataAdministrationController.java (3)

292-292: Added tab index variable for state management.

The new tabIndexMissingFields variable properly supports the accordion panel state management in the UI.


651-656: Initialization of state variables in navigation method.

The navigation method now properly initializes all required variables including the new ones, ensuring a clean state when entering the screen.


2340-2354: Added getter and setter methods for new variables.

The getter and setter methods for the new class variables are properly implemented following JavaBean conventions.

…ler.java

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
src/main/java/com/divudi/bean/common/DataAdministrationController.java (3)

650-657: Initialization of fields when navigating to missing fields page.

The method now initializes the new allCreateStetements variable along with other related fields, ensuring a clean state when accessing the page.

Consider fixing the typo in the variable name allCreateStetements (should be allCreateStatements).


942-952: SQL validation method for security.

This method properly validates SQL statements for security, allowing only CREATE TABLE, ALTER TABLE, and foreign key check operations while blocking potentially harmful statements.

While this is a good start, consider enhancing the validation with more sophisticated checks:

private boolean isValidSqlStatement(String sql) {
    sql = sql.trim().toLowerCase();
+   // Remove comments that might be used to bypass validation
+   sql = sql.replaceAll("--.*?$|/\\*.*?\\*/", " ");
    
    // Only allow CREATE TABLE, ALTER TABLE statements, and setting foreign key checks
    return (sql.startsWith("create table") || 
            sql.startsWith("alter table") ||
            sql.startsWith("set foreign_key_checks")) &&
+          // Check for SQL injection attempts more thoroughly
           !sql.contains("drop") &&
           !sql.contains("truncate") &&
           !sql.contains("delete") &&
           !sql.contains("update") &&
+          !sql.contains("insert") &&
+          !sql.contains("exec") &&
+          !sql.contains("execute") &&
+          !sql.contains("union") &&
+          !sql.matches(".*?;\\s*\\w+.*");  // Detect multiple statements
}

2358-2364: Getter and setter for the allCreateStetements field.

These methods allow access to the field that stores CREATE SQL statements.

Fix the typo in the variable name and methods (allCreateStetements should be allCreateStatements).

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c6d98aa and a55bc61.

📒 Files selected for processing (1)
  • src/main/java/com/divudi/bean/common/DataAdministrationController.java (5 hunks)
🔇 Additional comments (5)
src/main/java/com/divudi/bean/common/DataAdministrationController.java (5)

280-280: New field for storing CREATE SQL statements.

This field will be used to store the SQL CREATE statements that will be processed for adding missing tables and columns.


292-292: New tab index for the missing fields UI.

This field will control which tab is active in the missing fields interface.


874-939: New method to process all CREATE statements in batch.

This method implements the core functionality of processing multiple SQL CREATE statements in one go, which aligns with the PR objective.

The implementation:

  1. Splits input by "CREATE TABLE"
  2. Processes each statement to extract table name and generate ALTER statements
  3. Executes the SQL with validation and error handling
  4. Provides feedback on execution results

While the implementation includes good error handling and validation, consider adding a check at the beginning of the method to ensure allCreateStetements is not empty or null before processing:

public void createTablesAndFieldsForAllCreateStatements() {
    StringBuilder executionResults = new StringBuilder();
+   
+   if (allCreateStetements == null || allCreateStetements.trim().isEmpty()) {
+       executionFeedback = "No SQL statements provided to execute.";
+       return;
+   }

    System.out.println("===== Starting CREATE TABLE parsing and ALTER execution =====");
    // Rest of the method remains the same
}

914-921: SQL validation check added for security.

Good implementation of security validation before executing SQL statements, addressing a potential security vulnerability.

The code now checks if the SQL statement is valid before execution, preventing potentially harmful operations like DROP, DELETE, etc.


2366-2372: Getter and setter for tabIndexMissingFields.

These methods allow control of which tab is active in the missing fields interface.

Copy link
Member Author

@buddhika75 buddhika75 left a comment

Choose a reason for hiding this comment

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

suggestions accepted

@GSMgeeth GSMgeeth merged commit d168342 into development Mar 29, 2025
3 checks passed
@GSMgeeth GSMgeeth deleted the 11421-should-be-able-to-add-all-missing-fields-and-missing-tables-in-one-go branch March 29, 2025 15:27
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.

Should be able to add all missing fields and missing tables in one go
2 participants