From 9bf0e73fa9ac74697cfed7fed297d7d3c64ab27d Mon Sep 17 00:00:00 2001 From: ParkSeolDev Date: Fri, 23 Feb 2024 23:14:18 +0900 Subject: [PATCH 1/6] =?UTF-8?q?refactor=20:=20step1=20=EC=BD=94=EB=93=9C?= =?UTF-8?q?=EB=A6=AC=EB=B7=B0=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../persistence/study/ReflectionTest.java | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/test/java/persistence/study/ReflectionTest.java b/src/test/java/persistence/study/ReflectionTest.java index 91acebd4d..f48526b17 100644 --- a/src/test/java/persistence/study/ReflectionTest.java +++ b/src/test/java/persistence/study/ReflectionTest.java @@ -11,7 +11,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; public class ReflectionTest { private static final Logger logger = LoggerFactory.getLogger(ReflectionTest.class); @@ -23,21 +23,21 @@ void showCarClass() { logger.debug(carClass.getName()); Field[] fields = carClass.getDeclaredFields(); - System.out.println("필드:"); + logger.debug("필드:"); for (Field field : fields) { - System.out.println(field); + logger.debug(String.valueOf(field)); } Constructor[] constructors = carClass.getDeclaredConstructors(); - System.out.println("생성자:"); + logger.debug("생성자:"); for (Constructor constructor : constructors) { - System.out.println(constructor); + logger.debug(String.valueOf(constructor)); } Method[] methods = carClass.getDeclaredMethods(); - System.out.println("메서드"); + logger.debug("메서드:"); for (Method method : methods) { - System.out.println(method); + logger.debug(String.valueOf(method)); } } @@ -46,7 +46,7 @@ void showCarClass() { Class carClass = Car.class; Car car = carClass.getConstructor().newInstance(); Arrays.stream(carClass.getDeclaredMethods()) - .filter(x -> x.getName().contains("test")) + .filter(x -> x.getName().startsWith("test")) .forEach(x -> { try { x.invoke(car); @@ -88,8 +88,12 @@ void showCarClass() { priceField.setAccessible(true); priceField.set(car, price); - assertThat(car.testGetName()).isEqualTo("test : " + name); - assertThat(car.testGetPrice()).isEqualTo("test : " + price); + assertAll( + () -> { + assertEquals(car.testGetName(), "test : " + name); + assertEquals(car.testGetPrice(), "test : " + price); + } + ); } @Test @@ -103,7 +107,11 @@ void showCarClass() { .findFirst().orElseThrow(); Car car = (Car) constructor.newInstance(name, price); - assertThat(car.testGetName()).isEqualTo("test : " + name); - assertThat(car.testGetPrice()).isEqualTo("test : " + price); + assertAll( + () -> { + assertEquals(car.testGetName(), "test : " + name); + assertEquals(car.testGetPrice(), "test : " + price); + } + ); } } From c50b16fcf5ea8c1d917612de46c80ea9cc929569 Mon Sep 17 00:00:00 2001 From: ParkSeolDev Date: Thu, 29 Feb 2024 22:40:15 +0900 Subject: [PATCH 2/6] =?UTF-8?q?=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD=201=20?= =?UTF-8?q?-=20create=20=EC=BF=BC=EB=A6=AC=20=EB=A7=8C=EB=93=A4=EC=96=B4?= =?UTF-8?q?=EB=B3=B4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Person.java | 16 +++++++++ src/main/java/persistence/Application.java | 7 ++++ .../sql/ddl/CreateQueryBuilder.java | 35 +++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 src/main/java/domain/Person.java create mode 100644 src/main/java/persistence/sql/ddl/CreateQueryBuilder.java diff --git a/src/main/java/domain/Person.java b/src/main/java/domain/Person.java new file mode 100644 index 000000000..3e76362b5 --- /dev/null +++ b/src/main/java/domain/Person.java @@ -0,0 +1,16 @@ +package domain; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; + +@Entity +public class Person { + + @Id + private Long id; + + private String name; + + private Integer age; + +} diff --git a/src/main/java/persistence/Application.java b/src/main/java/persistence/Application.java index 8809704f5..1726910a7 100644 --- a/src/main/java/persistence/Application.java +++ b/src/main/java/persistence/Application.java @@ -2,7 +2,10 @@ import database.DatabaseServer; import database.H2; +import domain.Person; import jdbc.JdbcTemplate; +import persistence.sql.ddl.CreateQueryBuilder; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -17,6 +20,10 @@ public static void main(String[] args) { final JdbcTemplate jdbcTemplate = new JdbcTemplate(server.getConnection()); + CreateQueryBuilder createQueryBuilder = new CreateQueryBuilder(); + + jdbcTemplate.execute(createQueryBuilder.getCreateTableSql(Person.class)); + server.stop(); } catch (Exception e) { logger.error("Error occurred", e); diff --git a/src/main/java/persistence/sql/ddl/CreateQueryBuilder.java b/src/main/java/persistence/sql/ddl/CreateQueryBuilder.java new file mode 100644 index 000000000..7b0bb38e4 --- /dev/null +++ b/src/main/java/persistence/sql/ddl/CreateQueryBuilder.java @@ -0,0 +1,35 @@ +package persistence.sql.ddl; + +import java.io.Serializable; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.lang.reflect.Field; + +import domain.Person; + +public class CreateQueryBuilder { + Map, String> javaClassTypeToDbTypes = Map.of( + Long.class, "BIGINT", + String.class, "VARCHAR", + Integer.class, "BIGINT" + ); + + public String getCreateTableSql(Class clazz) { + Class person = clazz; + String tableName = person.getSimpleName(); + + List> fieldTypes = Arrays.stream(person.getDeclaredFields()).map(Field::getType).toList(); + + List fieldNames = Arrays.stream(person.getDeclaredFields()).map(Field::getName).toList(); + + String createTableSql = String.format("create table %s(%s %s, %s %s, %s %s)", + tableName, fieldNames.get(0), javaClassTypeToDbTypes.get(fieldTypes.get(0)), + fieldNames.get(1), javaClassTypeToDbTypes.get(fieldTypes.get(1)), + fieldNames.get(2), javaClassTypeToDbTypes.get(fieldTypes.get(2)) + ); + + return createTableSql; + } +} From 9f407e49dd8b859d3bb729b3cd603f4ccdd9acf9 Mon Sep 17 00:00:00 2001 From: ParkSeolDev Date: Fri, 1 Mar 2024 22:38:06 +0900 Subject: [PATCH 3/6] =?UTF-8?q?=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD2=20-?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80=EB=90=9C=20=EC=A0=95=EB=B3=B4=EB=A5=BC=20?= =?UTF-8?q?=ED=86=B5=ED=95=B4=20create=20=EC=BF=BC=EB=A6=AC=20=EB=A7=8C?= =?UTF-8?q?=EB=93=A4=EC=96=B4=EB=B3=B4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Person.java | 8 +++ src/main/java/persistence/Application.java | 10 ++- .../sql/ddl/CreateQueryBuilder.java | 64 +++++++++++++++++-- 3 files changed, 73 insertions(+), 9 deletions(-) diff --git a/src/main/java/domain/Person.java b/src/main/java/domain/Person.java index 3e76362b5..608f42e56 100644 --- a/src/main/java/domain/Person.java +++ b/src/main/java/domain/Person.java @@ -1,16 +1,24 @@ package domain; +import jakarta.persistence.Column; import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; import jakarta.persistence.Id; @Entity public class Person { @Id + @GeneratedValue(strategy = GenerationType.AUTO) private Long id; + @Column(name = "nick_name") private String name; + @Column(name = "old") private Integer age; + @Column(nullable = false) + private String email; } diff --git a/src/main/java/persistence/Application.java b/src/main/java/persistence/Application.java index 1726910a7..814f7b63a 100644 --- a/src/main/java/persistence/Application.java +++ b/src/main/java/persistence/Application.java @@ -1,5 +1,11 @@ package persistence; +import java.io.Serializable; +import java.lang.reflect.Field; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + import database.DatabaseServer; import database.H2; import domain.Person; @@ -20,9 +26,7 @@ public static void main(String[] args) { final JdbcTemplate jdbcTemplate = new JdbcTemplate(server.getConnection()); - CreateQueryBuilder createQueryBuilder = new CreateQueryBuilder(); - - jdbcTemplate.execute(createQueryBuilder.getCreateTableSql(Person.class)); + jdbcTemplate.execute(new CreateQueryBuilder().getCreateTableSql()); server.stop(); } catch (Exception e) { diff --git a/src/main/java/persistence/sql/ddl/CreateQueryBuilder.java b/src/main/java/persistence/sql/ddl/CreateQueryBuilder.java index 7b0bb38e4..07ed6862a 100644 --- a/src/main/java/persistence/sql/ddl/CreateQueryBuilder.java +++ b/src/main/java/persistence/sql/ddl/CreateQueryBuilder.java @@ -1,13 +1,16 @@ package persistence.sql.ddl; import java.io.Serializable; +import java.lang.reflect.InvocationTargetException; import java.util.Arrays; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; import java.lang.reflect.Field; import domain.Person; +import jakarta.persistence.Column; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; public class CreateQueryBuilder { Map, String> javaClassTypeToDbTypes = Map.of( @@ -16,13 +19,62 @@ public class CreateQueryBuilder { Integer.class, "BIGINT" ); - public String getCreateTableSql(Class clazz) { - Class person = clazz; - String tableName = person.getSimpleName(); + public String getCreateTableSql() throws + NoSuchMethodException, + InvocationTargetException, + InstantiationException, + IllegalAccessException { - List> fieldTypes = Arrays.stream(person.getDeclaredFields()).map(Field::getType).toList(); + Class personClass = Person.class; - List fieldNames = Arrays.stream(person.getDeclaredFields()).map(Field::getName).toList(); + Person person = personClass.getConstructor().newInstance(); + + Arrays.stream(personClass.getDeclaredFields()) + .filter(x -> x.isAnnotationPresent(GeneratedValue.class)) + .forEach(x -> { + x.setAccessible(true); + try { + Long value = 0L; + if (x.getAnnotation(GeneratedValue.class).strategy().equals(GenerationType.AUTO)) { + value = (Long)x.get(person); + x.set(person, value + 1L); + } + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + }); + + Arrays.stream(personClass.getDeclaredFields()) + .filter(x -> x.isAnnotationPresent(Column.class)) + .forEach(x -> { + x.setAccessible(true); + try { + x.set(person, x.getAnnotation(Column.class).name()); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + }); + + Arrays.stream(personClass.getDeclaredFields()) + .filter(x -> x.isAnnotationPresent(Column.class)) + .forEach(x -> { + x.setAccessible(true); + try { + if (!x.getAnnotation(Column.class).nullable()) { + if (x.get(person) == null) { + throw new RuntimeException(); + } + } + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + }); + + String tableName = personClass.getSimpleName(); + + List> fieldTypes = Arrays.stream(personClass.getDeclaredFields()).map(Field::getType).toList(); + + List fieldNames = Arrays.stream(personClass.getDeclaredFields()).map(Field::getName).toList(); String createTableSql = String.format("create table %s(%s %s, %s %s, %s %s)", tableName, fieldNames.get(0), javaClassTypeToDbTypes.get(fieldTypes.get(0)), From a596a589cae52e0430a4409c7b57fb14d557c468 Mon Sep 17 00:00:00 2001 From: ParkSeolDev Date: Fri, 1 Mar 2024 22:41:50 +0900 Subject: [PATCH 4/6] =?UTF-8?q?=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD2=20-?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80=EB=90=9C=20=EC=A0=95=EB=B3=B4=EB=A5=BC=20?= =?UTF-8?q?=ED=86=B5=ED=95=B4=20create=20=EC=BF=BC=EB=A6=AC=20=EB=A7=8C?= =?UTF-8?q?=EB=93=A4=EC=96=B4=EB=B3=B4=EA=B8=B0=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/persistence/sql/ddl/CreateQueryBuilder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/persistence/sql/ddl/CreateQueryBuilder.java b/src/main/java/persistence/sql/ddl/CreateQueryBuilder.java index 07ed6862a..9dfdfa616 100644 --- a/src/main/java/persistence/sql/ddl/CreateQueryBuilder.java +++ b/src/main/java/persistence/sql/ddl/CreateQueryBuilder.java @@ -35,7 +35,7 @@ public String getCreateTableSql() throws x.setAccessible(true); try { Long value = 0L; - if (x.getAnnotation(GeneratedValue.class).strategy().equals(GenerationType.AUTO)) { + if (x.getAnnotation(GeneratedValue.class).strategy().equals(GenerationType.IDENTITY)) { value = (Long)x.get(person); x.set(person, value + 1L); } From d641d79a825bd8f358a87bb5154ab5ad014cf064 Mon Sep 17 00:00:00 2001 From: ParkSeolDev Date: Sat, 9 Mar 2024 22:19:56 +0900 Subject: [PATCH 5/6] =?UTF-8?q?feat=20:=20=EC=9A=94=EA=B5=AC=EC=82=AC?= =?UTF-8?q?=ED=95=AD=203=20-=20=EC=B6=94=EA=B0=80=EB=90=9C=20=EC=A0=95?= =?UTF-8?q?=EB=B3=B4=EB=A5=BC=20=ED=86=B5=ED=95=B4=20create=20=EC=BF=BC?= =?UTF-8?q?=EB=A6=AC=20=EB=A7=8C=EB=93=A4=EC=96=B4=EB=B3=B4=EA=B8=B02?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Person.java | 8 +- .../sql/ddl/CreateQueryBuilder.java | 138 ++++++++++++------ .../sql/ddl/CreateQueryBuilderTest.java | 21 +++ 3 files changed, 121 insertions(+), 46 deletions(-) create mode 100644 src/test/java/persistence/sql/ddl/CreateQueryBuilderTest.java diff --git a/src/main/java/domain/Person.java b/src/main/java/domain/Person.java index 608f42e56..0703d4703 100644 --- a/src/main/java/domain/Person.java +++ b/src/main/java/domain/Person.java @@ -5,12 +5,15 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; +import jakarta.persistence.Table; +import jakarta.persistence.Transient; +@Table(name = "users") @Entity public class Person { @Id - @GeneratedValue(strategy = GenerationType.AUTO) + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "nick_name") @@ -21,4 +24,7 @@ public class Person { @Column(nullable = false) private String email; + + @Transient + private Integer index; } diff --git a/src/main/java/persistence/sql/ddl/CreateQueryBuilder.java b/src/main/java/persistence/sql/ddl/CreateQueryBuilder.java index 9dfdfa616..6e579682f 100644 --- a/src/main/java/persistence/sql/ddl/CreateQueryBuilder.java +++ b/src/main/java/persistence/sql/ddl/CreateQueryBuilder.java @@ -10,7 +10,8 @@ import domain.Person; import jakarta.persistence.Column; import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; +import jakarta.persistence.Table; +import jakarta.persistence.Transient; public class CreateQueryBuilder { Map, String> javaClassTypeToDbTypes = Map.of( @@ -29,59 +30,106 @@ public String getCreateTableSql() throws Person person = personClass.getConstructor().newInstance(); - Arrays.stream(personClass.getDeclaredFields()) + // 목표 출력의 목표를 스트링으로 적는다 + /* + CREATE TABLE person ( + id INT AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(50), + age INT, + email VARCHAR(50) NOT NULL, + index INT + ); + */ + // 메서드 분리 (3) + Field idField = Arrays.stream(getDeclaredFields(personClass)) .filter(x -> x.isAnnotationPresent(GeneratedValue.class)) - .forEach(x -> { - x.setAccessible(true); - try { - Long value = 0L; - if (x.getAnnotation(GeneratedValue.class).strategy().equals(GenerationType.IDENTITY)) { - value = (Long)x.get(person); - x.set(person, value + 1L); - } - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } - }); - - Arrays.stream(personClass.getDeclaredFields()) + .findFirst().get(); + + String idFieldName = idField.getName(); + + String idType = javaClassTypeToDbTypes.get(idField.getType()); + + String generatedValue = generateValue(); + + String primaryKey = getPrimaryKey(); + + String idCombi = String.format("%s %s %s %s,", idFieldName, idType, generatedValue, primaryKey); + + // name VARCHAR(50), + Field nameField = Arrays.stream(getDeclaredFields(personClass)) + .filter(x -> x.isAnnotationPresent(Column.class)).findFirst().get(); + + String name = nameField.getName(); + + String nameType = javaClassTypeToDbTypes.get(nameField.getType()); + + String nameCombi = String.format("%s %s,", name, nameType); + + // age INT, + Field ageField = Arrays.stream(getDeclaredFields(personClass)) .filter(x -> x.isAnnotationPresent(Column.class)) - .forEach(x -> { - x.setAccessible(true); - try { - x.set(person, x.getAnnotation(Column.class).name()); - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } - }); - - Arrays.stream(personClass.getDeclaredFields()) + .filter(x -> x.getAnnotation(Column.class).name().equals("old")) + .findFirst().get(); + String age = ageField.getName(); + String ageType = javaClassTypeToDbTypes.get(ageField.getType()); + + String ageCombi = String.format("%s %s,", age, ageType); + + // email VARCHAR(50) NOT NULL, + Field emailField = Arrays.stream(getDeclaredFields(personClass)) .filter(x -> x.isAnnotationPresent(Column.class)) - .forEach(x -> { - x.setAccessible(true); - try { - if (!x.getAnnotation(Column.class).nullable()) { - if (x.get(person) == null) { - throw new RuntimeException(); - } - } - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } - }); + .filter(x -> !x.getAnnotation(Column.class).nullable()) + .findFirst().get(); + String email = emailField.getName(); + String emailType = javaClassTypeToDbTypes.get(emailField.getType()); + boolean isNullable = emailField.getAnnotation(Column.class).nullable(); - String tableName = personClass.getSimpleName(); + String emialNullable = isNull(isNullable); - List> fieldTypes = Arrays.stream(personClass.getDeclaredFields()).map(Field::getType).toList(); + String emailCombi = String.format("%s %s %s", email, emailType, emialNullable); - List fieldNames = Arrays.stream(personClass.getDeclaredFields()).map(Field::getName).toList(); + // index INT + Field indexField = Arrays.stream(getDeclaredFields(personClass)) + .filter(x -> x.isAnnotationPresent(Transient.class)) + .findFirst().get(); + String indexCombi = ""; - String createTableSql = String.format("create table %s(%s %s, %s %s, %s %s)", - tableName, fieldNames.get(0), javaClassTypeToDbTypes.get(fieldTypes.get(0)), - fieldNames.get(1), javaClassTypeToDbTypes.get(fieldTypes.get(1)), - fieldNames.get(2), javaClassTypeToDbTypes.get(fieldTypes.get(2)) + String tableName = personClass.getSimpleName(); + + // List> fieldTypes = Arrays.stream(getDeclaredFields(personClass)).map(Field::getType).toList(); + // + // List fieldNames = Arrays.stream(getDeclaredFields(personClass)).map(Field::getName).toList(); + + String createTableSql = String.format("create table %s(%s %s %s %s %s)", + tableName, + idCombi, + nameCombi, + ageCombi, + emailCombi, + indexCombi ); return createTableSql; } + + private String getPrimaryKey() { + + return "PRIMARY KEY"; + } + + private static Field[] getDeclaredFields(Class personClass) { + return personClass.getDeclaredFields(); + } + + // DB에서 가져와야겠다 ai를 // + private static String generateValue() { + return "AUTO_INCREMENT"; + } + + private static String isNull(boolean isNullable) { + if (!isNullable) { + return "NOT NULL"; + } + return ""; + } } diff --git a/src/test/java/persistence/sql/ddl/CreateQueryBuilderTest.java b/src/test/java/persistence/sql/ddl/CreateQueryBuilderTest.java new file mode 100644 index 000000000..92e05e946 --- /dev/null +++ b/src/test/java/persistence/sql/ddl/CreateQueryBuilderTest.java @@ -0,0 +1,21 @@ +package persistence.sql.ddl; + +import static org.junit.jupiter.api.Assertions.*; + +import java.lang.reflect.InvocationTargetException; + +import org.junit.jupiter.api.Test; + +class CreateQueryBuilderTest { + @Test + void createDDL() throws + InvocationTargetException, + NoSuchMethodException, + InstantiationException, + IllegalAccessException { + CreateQueryBuilder createQueryBuilder = new CreateQueryBuilder(); + String createTableSql = createQueryBuilder.getCreateTableSql(); + + assertEquals(createTableSql, "CREATE TABLE person (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) NOT NULL, age INT)"); + } +} From 41e45cefef745cc8db3229b93653fa8544337687 Mon Sep 17 00:00:00 2001 From: ParkSeolDev Date: Sat, 9 Mar 2024 22:42:59 +0900 Subject: [PATCH 6/6] =?UTF-8?q?feat=20:=20=EC=9A=94=EA=B5=AC=EC=82=AC?= =?UTF-8?q?=ED=95=AD4=20-=20=EC=A0=95=EB=B3=B4=EB=A5=BC=20=EB=B0=94?= =?UTF-8?q?=ED=83=95=EC=9C=BC=EB=A1=9C=20drop=20=EC=BF=BC=EB=A6=AC=20?= =?UTF-8?q?=EB=A7=8C=EB=93=A4=EC=96=B4=EB=B3=B4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/persistence/Application.java | 2 ++ .../persistence/sql/ddl/DropQueryBuilder.java | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 src/main/java/persistence/sql/ddl/DropQueryBuilder.java diff --git a/src/main/java/persistence/Application.java b/src/main/java/persistence/Application.java index 814f7b63a..c2b0ac2be 100644 --- a/src/main/java/persistence/Application.java +++ b/src/main/java/persistence/Application.java @@ -11,6 +11,7 @@ import domain.Person; import jdbc.JdbcTemplate; import persistence.sql.ddl.CreateQueryBuilder; +import persistence.sql.ddl.DropQueryBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -27,6 +28,7 @@ public static void main(String[] args) { final JdbcTemplate jdbcTemplate = new JdbcTemplate(server.getConnection()); jdbcTemplate.execute(new CreateQueryBuilder().getCreateTableSql()); + jdbcTemplate.execute(new DropQueryBuilder().getDropTableSql()); server.stop(); } catch (Exception e) { diff --git a/src/main/java/persistence/sql/ddl/DropQueryBuilder.java b/src/main/java/persistence/sql/ddl/DropQueryBuilder.java new file mode 100644 index 000000000..a116a13a1 --- /dev/null +++ b/src/main/java/persistence/sql/ddl/DropQueryBuilder.java @@ -0,0 +1,17 @@ +package persistence.sql.ddl; + +import domain.Person; + +public class DropQueryBuilder { + public String getDropTableSql() { + Class personClass = Person.class; + + String tableName = personClass.getSimpleName(); + // DROP TABLE Person + String dropTableSql = String.format("drop table %s", tableName); + + return dropTableSql; + } + + +}