Adding generator app

This commit is contained in:
shafaat 2021-09-08 21:21:47 +08:00
commit 4b3d36f689
6 changed files with 373 additions and 0 deletions

135
.gitignore vendored Normal file
View File

@ -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

15
README.md Normal file
View File

@ -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

51
pom.xml Normal file
View File

@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>scripts-generator</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
</dependencies>
</project>

139
src/main/java/App.java Normal file
View File

@ -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<Row> 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 "";
}
}

View File

@ -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;
}
}

Binary file not shown.