commit 4b3d36f6897fd64d4101416ab472924f23f63fe9 Author: shafaat Date: Wed Sep 8 21:21:47 2021 +0800 Adding generator app diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..54a6e95 --- /dev/null +++ b/.gitignore @@ -0,0 +1,135 @@ +###################### +# Project Specific +###################### +/src/main/webapp/content/css/main.css +/target/classes/static/** +/src/test/javascript/coverage/ + + + +###################### +# Eclipse +###################### +*.pydevproject +.project +.metadata +tmp/ +tmp/**/* +*.tmp +*.bak +*.swp +*~.nib +local.properties +.classpath +.settings/ +.loadpath +.factorypath +/src/main/resources/rebel.xml + +# External tool builders +.externalToolBuilders/** + +# Locally stored "Eclipse launch configurations" +*.launch + +# CDT-specific +.cproject + +# PDT-specific +.buildpath + +# STS-specific +/.sts4-cache/* + +###################### +# IntelliJ +###################### +.idea/ +*.iml +*.iws +*.ipr +*.ids +*.orig +classes/ +out/ + +###################### +# Visual Studio Code +###################### +.vscode/ + +###################### +# Maven +###################### +/log/ +/target/ + +###################### +# Gradle +###################### +.gradle/ +/build/ + +###################### +# Package Files +###################### +*.jar +*.war +*.ear +*.db + +###################### +# Windows +###################### +# Windows image file caches +Thumbs.db + +# Folder config file +Desktop.ini + +###################### +# Mac OSX +###################### +.DS_Store +.svn + +# Thumbnails +._* + +# Files that might appear on external disk +.Spotlight-V100 +.Trashes + +###################### +# Directories +###################### +/bin/ +/deploy/ + +###################### +# Logs +###################### +*.log* + +###################### +# Others +###################### +*.class +*.*~ +*~ +.merge_file* + +###################### +# Gradle Wrapper +###################### +!gradle/wrapper/gradle-wrapper.jar + +###################### +# Maven Wrapper +###################### +!.mvn/wrapper/maven-wrapper.jar + +###################### +# ESLint +###################### +.eslintcache diff --git a/README.md b/README.md new file mode 100644 index 0000000..85b47ef --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# Script Generator + +This small app helps to generate DDL statements from the excel file + +## Usage + +Before you start to generate the scripts, follow below +1. Java: The running machine should have at least java 8 installed +2. File: The sample xlxs file is located in resources folder. This can be modified as but the format should remain the same +3. Open the application in any IDE (eclipse / Intellij) +4. Right click on App.java and run +5. The scripts will be generated in the console log + + +> Note: `id` column will be auto generated for all tables \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..5970382 --- /dev/null +++ b/pom.xml @@ -0,0 +1,51 @@ + + + 4.0.0 + + org.example + scripts-generator + 1.0-SNAPSHOT + + + 8 + 8 + + + + + + org.apache.poi + poi + 4.1.2 + + + + + commons-io + commons-io + 2.4 + + + + + org.apache.poi + poi-ooxml + 4.1.2 + + + + + org.apache.poi + poi-ooxml-schemas + 4.1.2 + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/App.java b/src/main/java/App.java new file mode 100644 index 0000000..ced2ff5 --- /dev/null +++ b/src/main/java/App.java @@ -0,0 +1,139 @@ +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Iterator; + +public class App { + public static void main(String args[]) throws IOException + { + //obtaining input bytes from a file + App tc = new App(); + + InputStream fis = tc.getClass().getClassLoader().getResourceAsStream("Data_Dictionary.xlsx"); + + //creating workbook instance that refers to .xls file + XSSFWorkbook wb=new XSSFWorkbook(fis); + //creating a Sheet object to retrieve the object + XSSFSheet sheet = wb.getSheetAt(0); //creating a Sheet object to retrieve object + Iterator itr = sheet.iterator(); //iterating over excel file + + String prevTableName = ""; + + int rowCount = 0; + + StringBuilder sb = new StringBuilder(); + StringBuilder foreignKeyBuilder = new StringBuilder(); + + while (itr.hasNext()) + { + + Row row = itr.next(); + + // Skip first row. It's header + if(rowCount == 0){ + rowCount++; + continue; + } + + String tempTableName = ""; + + + + tempTableName = row.getCell(0).getStringCellValue(); + + if(tempTableName !=null && tempTableName.length() > 0){ + + if(!tempTableName.equals(prevTableName)){ + + + System.out.println(_finishOffCreate(sb.append(foreignKeyBuilder.toString()))); + sb = new StringBuilder(); + foreignKeyBuilder = new StringBuilder(); + + + sb.append(Constants.CREATE_TABLE + + tempTableName + + Constants.BRACKET_OPEN + + Constants.NEW_LINE); + + sb.append(Constants.constructColumn( + "id", + "bigint,\n" + )); + + + sb.append(Constants.constructColumn( + row.getCell(1).getStringCellValue(), + row.getCell(2).getStringCellValue() + )); + sb.append(Constants.COMMA + Constants.NEW_LINE); + + _buildForeignKeys(row, foreignKeyBuilder); + + prevTableName = tempTableName; + }else{ + sb.append(Constants.constructColumn( + row.getCell(1).getStringCellValue(), + row.getCell(2).getStringCellValue() + )); + + _buildForeignKeys(row, foreignKeyBuilder); + + sb.append(Constants.COMMA + Constants.NEW_LINE); + } + + } + rowCount++; + } + } + + private static void _buildForeignKeys(Row row, StringBuilder foreignKeyBuilder) { + // Check for any foreign keys + if( row.getCell(3) != null && "Y".equalsIgnoreCase(row.getCell(3).getStringCellValue()) ){ + String currentTableName = row.getCell(0).getStringCellValue(); + String parentTableName = row.getCell(4).getStringCellValue(); + + foreignKeyBuilder.append( + Constants.SPACE + Constants.SPACE + Constants.SPACE + + + Constants.CONSTRAINT + Constants.SPACE + + + (Constants.FK_PREFIX + Constants.UNDERSCORE + + currentTableName.substring(0,8) + Constants.UNDERSCORE + + parentTableName.substring(0,8) + + Constants.SPACE + Constants.NEW_LINE ) + + + Constants.SPACE + Constants.SPACE + Constants.SPACE + Constants.SPACE + + + Constants.FOREIGN_KEY+ + + (Constants.BRACKET_OPEN + row.getCell(1).getStringCellValue() + Constants.BRACKET_CLOSE)+ Constants.SPACE + + + Constants.REFERENCES + Constants.SPACE + + + (row.getCell(5).getStringCellValue() + Constants.BRACKET_OPEN + row.getCell(1).getStringCellValue() + Constants.BRACKET_CLOSE)+ + + Constants.COMMA + Constants.NEW_LINE); + } + } + + private static String _finishOffCreate(StringBuilder createStatement){ + + if(createStatement.length() > 0){ +// System.out.println(createStatement.toString()); + int lastIndexOfComma=createStatement.lastIndexOf(Constants.COMMA); + + createStatement.deleteCharAt(lastIndexOfComma); + + createStatement.append( Constants.BRACKET_CLOSE + Constants.SEMI_COLON + Constants.NEW_LINE); + + + createStatement.replace(lastIndexOfComma, lastIndexOfComma, ""); + return createStatement.toString(); + } + return ""; + } +} diff --git a/src/main/java/Constants.java b/src/main/java/Constants.java new file mode 100644 index 0000000..339b59f --- /dev/null +++ b/src/main/java/Constants.java @@ -0,0 +1,33 @@ +import com.sun.org.apache.bcel.internal.generic.NEW; + +public class Constants { + public static final String CREATE_TABLE="CREATE TABLE "; + public static final String BRACKET_OPEN="("; + public static final String BRACKET_CLOSE=")"; + public static final String SPACE="\t"; + public static final String NEW_LINE="\n"; + public static final String COMMA=","; + + + //Data types + public static final String TYPE_VARCHAR="VARCHAR"; + public static final String TYPE_INT="INT"; + public static final String TYPE_DECIMAL="DECIMAL"; + public static final String TYPE_DATETIME="DATETIME"; + public static final String TYPE_DATE="DATE"; + public static final String BIT="bit"; + public static final String SEMI_COLON = ";"; + + + //Foreign Key Constraint + public static final String REFERENCES = "REFERENCES"; + public static final String CONSTRAINT = "CONSTRAINT"; + public static final String FOREIGN_KEY = "FOREIGN KEY"; + public static final String FK_PREFIX = "fk"; + public static final String UNDERSCORE = "_"; + + + public static String constructColumn(String columnName, String dataType){ + return SPACE + SPACE + SPACE + columnName + SPACE + dataType; + } +} diff --git a/src/main/resources/Data_Dictionary.xlsx b/src/main/resources/Data_Dictionary.xlsx new file mode 100644 index 0000000..2093e6d Binary files /dev/null and b/src/main/resources/Data_Dictionary.xlsx differ