|
1 | 1 | package lk.ijse.dep8.orm.annotations; |
2 | 2 |
|
| 3 | +import java.lang.annotation.Annotation; |
| 4 | +import java.lang.reflect.Field; |
3 | 5 | import java.sql.Connection; |
| 6 | +import java.sql.SQLException; |
| 7 | +import java.sql.Statement; |
4 | 8 | import java.util.ArrayList; |
5 | 9 | import java.util.List; |
6 | 10 |
|
7 | 11 |
|
8 | 12 | 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; |
23 | 15 |
|
| 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 | + } |
24 | 23 |
|
| 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 | + } |
25 | 66 | } |
0 commit comments