1- package org .ngbsn .generator ;
2-
1+ package io .github .ngbsn .generator ;
2+
3+ import io .github .ngbsn .model .Column ;
4+ import io .github .ngbsn .model .EmbeddableClass ;
5+ import io .github .ngbsn .model .ForeignKeyConstraint ;
6+ import io .github .ngbsn .model .Table ;
7+ import io .github .ngbsn .model .annotations .entityAnnotations .EntityAnnotation ;
8+ import io .github .ngbsn .model .annotations .entityAnnotations .TableAnnotation ;
9+ import io .github .ngbsn .model .annotations .fieldAnnotations .ColumnAnnotation ;
10+ import io .github .ngbsn .model .annotations .fieldAnnotations .NotNullAnnotation ;
11+ import io .github .ngbsn .util .SQLToJavaMapping ;
12+ import io .github .ngbsn .util .Util ;
313import net .sf .jsqlparser .parser .CCJSqlParserUtil ;
414import net .sf .jsqlparser .statement .Statements ;
515import net .sf .jsqlparser .statement .alter .Alter ;
616import net .sf .jsqlparser .statement .create .table .CreateTable ;
717import net .sf .jsqlparser .statement .create .table .ForeignKeyIndex ;
818import net .sf .jsqlparser .statement .create .table .Index ;
9- import org .apache .commons .text .CaseUtils ;
10- import org .ngbsn .model .Column ;
11- import org .ngbsn .model .EmbeddableClass ;
12- import org .ngbsn .model .ForeignKeyConstraint ;
13- import org .ngbsn .model .Table ;
14- import org .ngbsn .model .annotations .entityAnnotations .EntityAnnotation ;
15- import org .ngbsn .model .annotations .entityAnnotations .TableAnnotation ;
16- import org .ngbsn .model .annotations .fieldAnnotations .ColumnAnnotation ;
17- import org .ngbsn .model .annotations .fieldAnnotations .NotNullAnnotation ;
18- import org .ngbsn .util .SQLToJavaMapping ;
19- import org .ngbsn .util .Util ;
2019import org .slf4j .Logger ;
2120import org .slf4j .LoggerFactory ;
2221
2322import java .util .*;
2423import java .util .stream .Collectors ;
2524
26- import static org .ngbsn .generator .AssociationMappingsGenerator .generateMappings ;
27-
2825/**
2926 * This class will parse the SQL script and generate the Table models for each table in the script
3027 * It will also extract all the columns from the script and add to the Table model
@@ -38,7 +35,7 @@ public static List<Table> parse(final String sqlScript) {
3835 Statements statements = CCJSqlParserUtil .parseStatements (sqlScript );
3936 processCreateTableStatements (statements );
4037 processAlterTableStatements (statements );
41- generateMappings ();
38+ AssociationMappingsGenerator . generateMappings ();
4239 return tablesMap .values ().stream ().toList ();
4340 } catch (Exception e ) {
4441 logger .error ("Error occurred" , e );
@@ -49,11 +46,13 @@ public static List<Table> parse(final String sqlScript) {
4946 /**
5047 * Clears the tables map. This is needed to have a clean start between junits
5148 */
52- public static void clearTablesMap (){
49+ public static void clearTablesMap () {
5350 tablesMap .clear ();
5451 }
52+
5553 /**
5654 * Iterate over all the Create Table statements and prepare the list of Table Model
55+ *
5756 * @param statements set of statements
5857 */
5958 private static void processCreateTableStatements (Statements statements ) {
@@ -91,20 +90,21 @@ private static void processCreateTableStatements(Statements statements) {
9190
9291 /**
9392 * Iterate over all the Alter Table statements and prepare the list of Table Model
93+ *
9494 * @param statements set of statements
9595 */
9696 private static void processAlterTableStatements (Statements statements ) {
9797 statements .getStatements ().forEach (statement -> {
9898 //Iterating over all Tables
9999 // Look for primary and foreign keys in ALTER TABLE constraints
100- if (statement instanceof Alter alterTable ){
100+ if (statement instanceof Alter alterTable ) {
101101 Table table = tablesMap .get (alterTable .getTable ().getName ().replaceAll ("[\" ']" , "" ));
102102 List <Index > foreignKeyIndexes = new ArrayList <>();
103103 alterTable .getAlterExpressions ().forEach (alterExpression -> {
104- if (alterExpression .getIndex () instanceof ForeignKeyIndex foreignKeyIndex ){
104+ if (alterExpression .getIndex () instanceof ForeignKeyIndex foreignKeyIndex ) {
105105 //case: ALTER TABLE FOREIGN KEY
106106 foreignKeyIndexes .add (alterExpression .getIndex ());
107- } else if (alterExpression .getIndex ().getType ().equals ("PRIMARY KEY" )){
107+ } else if (alterExpression .getIndex ().getType ().equals ("PRIMARY KEY" )) {
108108 //case: ALTER TABLE PRIMARY KEY
109109 extractPrimaryKeys (alterExpression .getIndex (), table );
110110 }
@@ -118,8 +118,8 @@ private static void processAlterTableStatements(Statements statements) {
118118 /**
119119 * Looking for all foreign keys in this table and adding it to our model
120120 *
121- * @param foreignKeyIndexes List of foreign key indexes
122- * @param table Table model
121+ * @param foreignKeyIndexes List of foreign key indexes
122+ * @param table Table model
123123 */
124124 private static void extractForeignKeys (List <Index > foreignKeyIndexes , Table table ) {
125125 if (!foreignKeyIndexes .isEmpty ()) {
@@ -141,11 +141,11 @@ private static void extractForeignKeys(List<Index> foreignKeyIndexes, Table tabl
141141 /**
142142 * Extracting primary key information from the parsed data and setting them into the Table model
143143 *
144- * @param primaryKeyIndex primaryKeyIndex
145- * @param table Table model
144+ * @param primaryKeyIndex primaryKeyIndex
145+ * @param table Table model
146146 */
147147 private static void extractPrimaryKeys (Index primaryKeyIndex , Table table ) {
148- List <Index .ColumnParams > columnParamsList = primaryKeyIndex != null ? primaryKeyIndex .getColumns (): null ;
148+ List <Index .ColumnParams > columnParamsList = primaryKeyIndex != null ? primaryKeyIndex .getColumns () : null ;
149149 if (columnParamsList != null ) {
150150 Set <Column > primaryKeyColumns = table .getColumns ().stream ().
151151 filter (column -> columnParamsList .stream ().anyMatch (columnParams -> columnParams .getColumnName ().replaceAll ("[\" ']" , "" ).equals (column .getColumnName ()))).collect (Collectors .toSet ());
0 commit comments