Skip to content

Commit f28e032

Browse files
committed
Refactor: Facotry And Builder Class
1 parent bc7548b commit f28e032

33 files changed

Lines changed: 799 additions & 536 deletions

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,13 @@
2929
<module>sqlschema2java-maven-plugin</module>
3030
<module>sqlschema2java-example</module>
3131
</modules>
32+
33+
<distributionManagement>
34+
<repository>
35+
<id>github</id>
36+
<name>GitHub Packages</name>
37+
<url>https://maven.pkg.github.com/Sassine/sqlschema2java</url>
38+
</repository>
39+
</distributionManagement>
3240

3341
</project>

sqlschema2java-core/pom.xml

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<version>1.0.0-SNAPSHOT</version>
1010

1111
<name>sqlschema2java</name>
12-
<description> Auto generate Class DTO, Entity and Repository from SQL Query. </description>
12+
<description>Auto generate Class DTO, Entity and Repository from SQL Query. </description>
1313
<url>https://sassine.dev/</url>
1414

1515
<properties>
@@ -96,6 +96,13 @@
9696
<version>4.3</version>
9797
</dependency>
9898

99+
<dependency>
100+
<groupId>ch.qos.logback</groupId>
101+
<artifactId>logback-classic</artifactId>
102+
<version>1.2.11</version>
103+
<scope>test</scope>
104+
</dependency>
105+
99106
<dependency>
100107
<groupId>org.burningwave</groupId>
101108
<artifactId>core</artifactId>
@@ -113,6 +120,40 @@
113120
<groupId>org.springframework.boot</groupId>
114121
<artifactId>spring-boot-starter-data-jpa</artifactId>
115122
<version>2.4.2</version>
123+
<exclusions>
124+
<exclusion>
125+
<groupId>org.springframework.boot</groupId>
126+
<artifactId>spring-boot-starter-logging</artifactId>
127+
</exclusion>
128+
<exclusion>
129+
<groupId>org.jboss.logging</groupId>
130+
<artifactId>jboss-logging</artifactId>
131+
</exclusion>
132+
<exclusion>
133+
<groupId>org.slf4j</groupId>
134+
<artifactId>slf4j-api</artifactId>
135+
</exclusion>
136+
<exclusion>
137+
<groupId>org.springframework.boot</groupId>
138+
<artifactId>spring-boot-autoconfigure</artifactId>
139+
</exclusion>
140+
<exclusion>
141+
<groupId>org.springframework.boot</groupId>
142+
<artifactId>spring-boot-starter</artifactId>
143+
</exclusion>
144+
<exclusion>
145+
<groupId>org.springframework.boot</groupId>
146+
<artifactId>spring-boot-starter-aop</artifactId>
147+
</exclusion>
148+
<exclusion>
149+
<groupId>org.springframework</groupId>
150+
<artifactId>spring-jdbc</artifactId>
151+
</exclusion>
152+
<exclusion>
153+
<groupId>org.dom4j</groupId>
154+
<artifactId>dom4j</artifactId>
155+
</exclusion>
156+
</exclusions>
116157
</dependency>
117158

118159
<dependency>
@@ -121,21 +162,18 @@
121162
<version>1.9</version>
122163
</dependency>
123164

124-
<dependency>
125-
<groupId>org.json</groupId>
126-
<artifactId>json</artifactId>
127-
<version>20140107</version>
128-
</dependency>
129165
<dependency>
130166
<groupId>com.fasterxml.jackson.core</groupId>
131167
<artifactId>jackson-core</artifactId>
132168
<version>2.4.2</version>
133169
</dependency>
170+
134171
<dependency>
135172
<groupId>com.fasterxml.jackson.core</groupId>
136173
<artifactId>jackson-annotations</artifactId>
137174
<version>2.4.2</version>
138175
</dependency>
176+
139177
<dependency>
140178
<groupId>com.fasterxml.jackson.core</groupId>
141179
<artifactId>jackson-databind</artifactId>
@@ -148,6 +186,21 @@
148186
<version>4.10</version>
149187
<scope>test</scope>
150188
</dependency>
189+
190+
191+
<dependency>
192+
<groupId>org.apache.logging.log4j</groupId>
193+
<artifactId>log4j-api</artifactId>
194+
<version>2.17.2</version>
195+
</dependency>
196+
197+
<dependency>
198+
<groupId>org.apache.logging.log4j</groupId>
199+
<artifactId>log4j-core</artifactId>
200+
<version>2.17.2</version>
201+
</dependency>
202+
203+
151204
</dependencies>
152205

153206
</project>

sqlschema2java-core/src/main/java/dev/sassine/api/structure/Main.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
public class Main {
66

77
public static void main(final String[] args) throws FileNotFoundException {
8-
new Sqlschema2Java().compile("",false, false);
8+
Sqlschema2Java.generate("C:\\poc\\poc.sql", false, false,"dev.sassine.api.structure.delete");
99
}
10-
10+
1111
}
1212

1313

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,44 @@
11
package dev.sassine.api.structure;
22

33
import static dev.sassine.api.structure.export.DatabaseConverter.convert;
4-
import static dev.sassine.api.structure.export.builder.BuilderEntity.build;
5-
import static dev.sassine.api.structure.type.TypeConverter.convertTypeFromSQLToEntityStore;
4+
import static dev.sassine.api.structure.type.TypeConverter.convertTypeFromSQLDataBaseToEntityStore;
65
import static dev.sassine.api.structure.util.Util.read;
76
import static dev.sassine.api.structure.validation.DatabaseValidator.validate;
7+
import static org.apache.logging.log4j.LogManager.getLogger;
88

99
import java.io.FileInputStream;
1010
import java.io.InputStream;
1111
import java.util.List;
1212

13+
import org.apache.logging.log4j.Logger;
14+
15+
import dev.sassine.api.structure.export.builder.ClassSourceBuilder;
1316
import dev.sassine.api.structure.model.java.EntityModel;
1417
import dev.sassine.api.structure.model.sql.Database;
1518
import dev.sassine.api.structure.parser.SqlImport;
16-
import lombok.extern.slf4j.Slf4j;
1719

18-
@Slf4j
1920
public class Sqlschema2Java {
2021

21-
public void compile(String inputFile,boolean isPostgress,boolean isAutoGenerated) {
22-
inputFile = "C:\\poc\\poc.sql";
22+
private static final Logger log = getLogger();
23+
24+
public static void generate(String inputFile, boolean isPostgress, boolean isAutoGenerated, String packageName) {
2325
log.debug("preparing to read SQL file('{}') ", inputFile);
2426
try (InputStream in = new FileInputStream(inputFile)) {
25-
log.debug("file read successfully");
26-
process(read(in),isPostgress,isAutoGenerated);
27+
log.debug("file read successfull");
28+
log.debug("Parameters ::: isPostgress: {} | isAutoGenerated: {} | packageName: {}", isPostgress, isAutoGenerated, packageName);
29+
process(read(in), isPostgress, isAutoGenerated, packageName);
30+
log.info("Congratulations build successful [√]");
2731
} catch (final Exception e) {
2832
log.error(e.getMessage(), e);
2933
}
3034
}
31-
32-
private void process(final String sqlContent, boolean isPostgress, boolean isAutoGenerated) {
33-
Database database = new SqlImport().getDatabase(sqlContent);
34-
convertTypeFromSQLToEntityStore(database,isPostgress);
35+
36+
private static void process(final String sqlContent, boolean isPostgress, boolean isAutoGenerated, String packageName) {
37+
final Database database = SqlImport.init().getDatabase(sqlContent);
38+
convertTypeFromSQLDataBaseToEntityStore(database, isPostgress);
3539
validate(database);
3640
List<EntityModel> entityModel = convert(database);
37-
build(entityModel,isAutoGenerated);
41+
ClassSourceBuilder.init().build(entityModel, isAutoGenerated, packageName);
3842
}
3943

4044
}

sqlschema2java-core/src/main/java/dev/sassine/api/structure/export/DatabaseConverter.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package dev.sassine.api.structure.export;
22

33
import static java.util.stream.Collectors.toList;
4+
import static org.apache.logging.log4j.LogManager.getLogger;
45

56
import java.util.List;
67

8+
import org.apache.logging.log4j.Logger;
9+
710
import dev.sassine.api.structure.model.java.EntityModel;
811
import dev.sassine.api.structure.model.java.FieldModel;
912
import dev.sassine.api.structure.model.sql.Column;
@@ -12,9 +15,13 @@
1215
import dev.sassine.api.structure.model.sql.TableModel;
1316

1417
public class DatabaseConverter {
15-
18+
19+
private static final Logger log = getLogger();
20+
1621
public static List<EntityModel> convert(final Database database) {
22+
log.debug("Converter Database :: Tables Size ({})",database.getTables().size());
1723
return database.getTables().stream().map(table -> {
24+
log.debug("Converter table ({}) ",table.getName());
1825
final EntityModel entity = new EntityModel(table.getName(),"user_generated_value");
1926
List<FieldModel> fields = entity.getFields();
2027
generateDefaultPK(table, fields);
@@ -26,16 +33,19 @@ public static List<EntityModel> convert(final Database database) {
2633
flagNullable(column, field);
2734
setDefaultValueColumn(column, field);
2835
fields.add(field);
36+
log.debug("Field ({}) added", field.getName());
2937
});
3038
return entity;
3139
}).collect(toList());
3240
}
3341

3442
private static void setDefaultValueColumn(final Column column, final FieldModel field) {
43+
log.debug("Set Default Value ({}) ",column.getDefaultValue());
3544
field.setDefaultValue(column.getDefaultValue());
3645
}
3746

3847
private static void flagNullable(final Column column, final FieldModel field) {
48+
log.debug("Set Nullable flag");
3949
field.setNullable(!(column.getIsNotNull() != null) && column.getIsNotNull());
4050
}
4151

@@ -45,8 +55,9 @@ private static void flagColumnPK(final TableModel table, final String columnName
4555
}
4656

4757
private static void generateFK(final Column column, final FieldModel field, final ForeignKey foreignKey) {
58+
log.debug("Generate FK");
4859
if(foreignKey == null && column.getConvertedType() != null) {
49-
field.setType(column.getConvertedType());
60+
field.setType(column.getConvertedType());
5061
} else {
5162
field.setType(foreignKey.getTableNameTarget());
5263
field.setMinOccurs(0);
@@ -55,7 +66,9 @@ private static void generateFK(final Column column, final FieldModel field, fina
5566
}
5667

5768
private static void generateDefaultPK(final TableModel table, final List<FieldModel> fields) {
69+
log.debug("Check exist Primary Key");
5870
if (table.getPrimaryKey().getColumnNames().size() != 1) {
71+
log.debug("PrimaryKey not found, adding default field PK");
5972
final FieldModel field = new FieldModel();
6073
field.setName("id");
6174
field.setType("Integer");

0 commit comments

Comments
 (0)