44import io .github .ngbsn .model .EmbeddableClass ;
55import io .github .ngbsn .model .ForeignKeyConstraint ;
66import 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 ;
7+ import io .github .ngbsn .model .annotations .entity .EntityAnnotation ;
8+ import io .github .ngbsn .model .annotations .entity .TableAnnotation ;
9+ import io .github .ngbsn .model .annotations .field .ColumnAnnotation ;
10+ import io .github .ngbsn .model .annotations .field .NotNullAnnotation ;
1111import io .github .ngbsn .util .SQLToJavaMapping ;
1212import io .github .ngbsn .util .Util ;
1313import net .sf .jsqlparser .parser .CCJSqlParserUtil ;
2727 * It will also extract all the columns from the script and add to the Table model
2828 */
2929public class ModelGenerator {
30+
31+ public static final String REGEX_ALL_QUOTES = "[\" ']" ;
32+
33+ private ModelGenerator () {
34+ }
35+
3036 private static final Logger logger = LoggerFactory .getLogger (ModelGenerator .class );
3137 protected static Map <String , Table > tablesMap = new HashMap <>();
3238
39+ /**
40+ * Parse the DDL statements using JSQL parser
41+ * @param sqlScript The input DDL commands used for generating the models
42+ * @return List of Tables models
43+ */
3344 public static List <Table > parse (final String sqlScript ) {
3445 try {
3546 Statements statements = CCJSqlParserUtil .parseStatements (sqlScript );
@@ -40,7 +51,7 @@ public static List<Table> parse(final String sqlScript) {
4051 } catch (Exception e ) {
4152 logger .error ("Error occurred" , e );
4253 }
43- return null ;
54+ return new ArrayList <>() ;
4455 }
4556
4657 /**
@@ -51,16 +62,15 @@ public static void clearTablesMap() {
5162 }
5263
5364 /**
54- * Iterate over all the Create Table statements and prepare the list of Table Model
55- *
56- * @param statements set of statements
65+ * Iterate over all the JSQL Create Table statements and prepare the list of Table Model
66+ * @param statements Set of JSQL statements
5767 */
58- private static void processCreateTableStatements (Statements statements ) {
68+ private static void processCreateTableStatements (final Statements statements ) {
5969 statements .getStatements ().forEach (statement -> {
6070 //Iterating over all Tables
6171 if (statement instanceof CreateTable parsedTable ) {
6272 Table table = new Table ();
63- table .setTableName (parsedTable .getTable ().getName ().replaceAll ("[ \" ']" , "" ));
73+ table .setTableName (parsedTable .getTable ().getName ().replaceAll (REGEX_ALL_QUOTES , "" ));
6474 tablesMap .put (table .getTableName (), table );
6575 table .setClassName (Util .convertSnakeCaseToCamelCase (table .getTableName (), true ));
6676
@@ -82,26 +92,25 @@ private static void processCreateTableStatements(Statements statements) {
8292 extractPrimaryKeys (optionalIndex .orElse (null ), table );
8393
8494 //extract foreign keys
85- List <Index > foreignKeyIndexes = parsedTable .getIndexes ().stream ().filter (index -> index instanceof ForeignKeyIndex ).toList ();
95+ List <Index > foreignKeyIndexes = parsedTable .getIndexes ().stream ().filter (ForeignKeyIndex . class :: isInstance ).toList ();
8696 extractForeignKeys (foreignKeyIndexes , table );
8797 }
8898 });
8999 }
90100
91101 /**
92- * Iterate over all the Alter Table statements and prepare the list of Table Model
93- *
94- * @param statements set of statements
102+ * Iterate over all the JSQL Alter Table statements and prepare the list of Table Model
103+ * @param statements Set of JSQL statements
95104 */
96- private static void processAlterTableStatements (Statements statements ) {
105+ private static void processAlterTableStatements (final Statements statements ) {
97106 statements .getStatements ().forEach (statement -> {
98107 //Iterating over all Tables
99108 // Look for primary and foreign keys in ALTER TABLE constraints
100109 if (statement instanceof Alter alterTable ) {
101- Table table = tablesMap .get (alterTable .getTable ().getName ().replaceAll ("[ \" ']" , "" ));
110+ Table table = tablesMap .get (alterTable .getTable ().getName ().replaceAll (REGEX_ALL_QUOTES , "" ));
102111 List <Index > foreignKeyIndexes = new ArrayList <>();
103112 alterTable .getAlterExpressions ().forEach (alterExpression -> {
104- if (alterExpression .getIndex () instanceof ForeignKeyIndex foreignKeyIndex ) {
113+ if (alterExpression .getIndex () instanceof ForeignKeyIndex ) {
105114 //case: ALTER TABLE FOREIGN KEY
106115 foreignKeyIndexes .add (alterExpression .getIndex ());
107116 } else if (alterExpression .getIndex ().getType ().equals ("PRIMARY KEY" )) {
@@ -116,39 +125,39 @@ private static void processAlterTableStatements(Statements statements) {
116125
117126
118127 /**
119- * Looking for all foreign keys in this table and adding it to our model
120- *
121- * @param foreignKeyIndexes List of foreign key indexes
128+ * Looking for all JSQL foreign keys in this table and adding it to our model
129+ * @param foreignKeyIndexes List of JSQL foreign key indexes
122130 * @param table Table model
123131 */
124- private static void extractForeignKeys (List <Index > foreignKeyIndexes , Table table ) {
132+ private static void extractForeignKeys (final List <Index > foreignKeyIndexes , final Table table ) {
125133 if (!foreignKeyIndexes .isEmpty ()) {
126134 foreignKeyIndexes .forEach (index -> {
127135 if (index instanceof ForeignKeyIndex foreignKeyIndex ) {
128136 ForeignKeyConstraint foreignKeyConstraint = new ForeignKeyConstraint ();
129137 foreignKeyConstraint .setColumns (foreignKeyIndex .getColumnsNames ()
130- .stream ().map (s -> s .replaceAll ("[ \" ']" , "" )).collect ( Collectors . toList () ));
138+ .stream ().map (s -> s .replaceAll (REGEX_ALL_QUOTES , "" )).toList ());
131139 foreignKeyConstraint .setReferencedColumns (foreignKeyIndex .getReferencedColumnNames ()
132- .stream ().map (s -> s .replaceAll ("[ \" ']" , "" )).collect ( Collectors . toList () ));
140+ .stream ().map (s -> s .replaceAll (REGEX_ALL_QUOTES , "" )).toList ());
133141 foreignKeyConstraint .setReferencedTableName (foreignKeyIndex .getTable ().getName ()
134- .replaceAll ("[ \" ']" , "" ));
142+ .replaceAll (REGEX_ALL_QUOTES , "" ));
135143 table .getForeignKeyConstraints ().add (foreignKeyConstraint );
144+
136145 }
137146 });
138147 }
139148 }
140149
141150 /**
142- * Extracting primary key information from the parsed data and setting them into the Table model
151+ * Extracting primary key information from the JSQL parsed data and setting them into the Table model
143152 *
144- * @param primaryKeyIndex primaryKeyIndex
153+ * @param primaryKeyIndex JSQL primaryKeyIndex
145154 * @param table Table model
146155 */
147- private static void extractPrimaryKeys (Index primaryKeyIndex , Table table ) {
156+ private static void extractPrimaryKeys (final Index primaryKeyIndex , final Table table ) {
148157 List <Index .ColumnParams > columnParamsList = primaryKeyIndex != null ? primaryKeyIndex .getColumns () : null ;
149158 if (columnParamsList != null ) {
150159 Set <Column > primaryKeyColumns = table .getColumns ().stream ().
151- filter (column -> columnParamsList .stream ().anyMatch (columnParams -> columnParams .getColumnName ().replaceAll ("[ \" ']" , "" ).equals (column .getColumnName ()))).collect (Collectors .toSet ());
160+ filter (column -> columnParamsList .stream ().anyMatch (columnParams -> columnParams .getColumnName ().replaceAll (REGEX_ALL_QUOTES , "" ).equals (column .getColumnName ()))).collect (Collectors .toSet ());
152161
153162 if (columnParamsList .size () > 1 ) {
154163 table .setNumOfPrimaryKeyColumns (columnParamsList .size ());
@@ -170,22 +179,21 @@ private static void extractPrimaryKeys(Index primaryKeyIndex, Table table) {
170179 }
171180
172181 /**
173- * Generate column models for all the parsed table
174- *
175- * @param parsedTable Parsed table
176- * @param columns List of generated column models
182+ * Generate column models for the JSQL parsed table
183+ * @param parsedTable JSQL CreateTable
184+ * @param columns Set of generated column models
177185 */
178- private static void extractColumns (CreateTable parsedTable , Set <Column > columns ) {
186+ private static void extractColumns (final CreateTable parsedTable , final Set <Column > columns ) {
179187 parsedTable .getColumnDefinitions ().forEach (columnDefinition -> {
180188 Column column = new Column ();
181189 columns .add (column );
182190 Set <String > columnAnnotations = new HashSet <>();
183191 column .setAnnotations (columnAnnotations );
184- column .setColumnName (columnDefinition .getColumnName ().replaceAll ("[ \" ']" , "" ));
192+ column .setColumnName (columnDefinition .getColumnName ().replaceAll (REGEX_ALL_QUOTES , "" ));
185193 //Adding @Column
186194 columnAnnotations .add (ColumnAnnotation .builder ().columnName (column .getColumnName ()).build ().toString ());
187195 column .setFieldName (Util .convertSnakeCaseToCamelCase (column .getColumnName (), false ));
188- column .setType (SQLToJavaMapping .sqlToJavaMap .get (columnDefinition .getColDataType ().getDataType ()));
196+ column .setType (SQLToJavaMapping .getSqlToJavaMap () .get (columnDefinition .getColDataType ().getDataType ()));
189197
190198 //Check for NOT NULL
191199 if (columnDefinition .getColumnSpecs () != null ) {
0 commit comments