Skip to content

Commit 6910ae4

Browse files
committed
Implement DepSessionFactory Part-
1 parent e2d5c23 commit 6910ae4

File tree

6 files changed

+193
-14
lines changed

6 files changed

+193
-14
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea/
2+
target/
3+
4+
*.iml

.idea/uiDesigner.xml

Lines changed: 124 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LICENSE.txt

Whitespace-only changes.

README.md

Whitespace-only changes.

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,14 @@
1313
<maven.compiler.target>8</maven.compiler.target>
1414
</properties>
1515

16+
<dependencies>
17+
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
18+
<dependency>
19+
<groupId>mysql</groupId>
20+
<artifactId>mysql-connector-java</artifactId>
21+
<version>8.0.29</version>
22+
</dependency>
23+
24+
</dependencies>
25+
1626
</project>
Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,66 @@
11
package lk.ijse.dep8.orm.annotations;
22

3+
import java.lang.annotation.Annotation;
4+
import java.lang.reflect.Field;
35
import java.sql.Connection;
6+
import java.sql.SQLException;
7+
import java.sql.Statement;
48
import java.util.ArrayList;
59
import java.util.List;
610

711

812
public class DepSessionFactory {
9-
private List<Class<?>> entityClassList=new ArrayList<>();
10-
private Connection connection;
11-
12-
public DepSessionFactory addAnnotatedClass(Class<?> entityClass){
13-
if(entityClass.getDeclaredAnnotations(Entity.class)==null){
14-
throw new RuntimeException("Invalid Entity class");
15-
}
16-
entityClassList.add(entityClassClass);
17-
return this;
18-
}
19-
public DepSessionFactory setConnection(Connection connection){
20-
this.connection=connection;
21-
return this;
22-
}
13+
private final List<Class<?>> entityClassList = new ArrayList<>();
14+
private Connection connection;
2315

16+
public DepSessionFactory addAnnotatedClass(Class<?> entityClass){
17+
if (entityClass.getDeclaredAnnotation(Entity.class) == null){
18+
throw new RuntimeException("Invalid entity class");
19+
}
20+
entityClassList.add(entityClass);
21+
return this;
22+
}
2423

24+
public DepSessionFactory setConnection(Connection connection){
25+
this.connection = connection;
26+
return this;
27+
}
28+
29+
public DepSessionFactory build(){
30+
if (this.connection == null){
31+
throw new RuntimeException("Failed to build without a connection");
32+
}
33+
return this;
34+
}
35+
36+
public void bootstrap() throws SQLException {
37+
for (Class<?> entity : entityClassList) {
38+
String tableName = entity.getDeclaredAnnotation(Entity.class).value();
39+
if (tableName.trim().isEmpty()) tableName = entity.getSimpleName();
40+
List<String> columns = new ArrayList<>();
41+
String primaryKey = null;
42+
43+
Field[] fields = entity.getDeclaredFields();
44+
for (Field field : fields) {
45+
Id primaryKeyField = field.getDeclaredAnnotation(Id.class);
46+
if (primaryKeyField != null) {
47+
primaryKey = field.getName();
48+
continue;
49+
}
50+
51+
String columnName = field.getName();
52+
columns.add(columnName);
53+
}
54+
if (primaryKey == null) throw new RuntimeException("Entity without a primary key");
55+
56+
StringBuilder sb = new StringBuilder();
57+
sb.append("CREATE TABLE ").append(tableName).append("(");
58+
for (String column : columns) {
59+
sb.append(column).append(" VARCHAR(255),");
60+
}
61+
sb.append(primaryKey).append("VARCHAR(255) PRIMARY KEY)");
62+
Statement stm = connection.createStatement();
63+
stm.executeQuery(sb.toString());
64+
}
65+
}
2566
}

0 commit comments

Comments
 (0)