Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 4단계 - Simple Entity Object #229

Open
wants to merge 4 commits into
base: rolroralra
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/jdbc/RowMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@

@FunctionalInterface
public interface RowMapper<T> {
T mapRow(final ResultSet resultSet) throws SQLException;
T mapRow(final ResultSet resultSet)
throws SQLException, InstantiationException, IllegalAccessException;
}
28 changes: 14 additions & 14 deletions src/main/java/persistence/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import jdbc.RowMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import persistence.sql.QueryTranslator;
import persistence.sql.QueryBuilder;
import persistence.sql.ddl.entity.Person;

public class Application {
Expand All @@ -28,19 +28,19 @@ public static void main(String[] args) {

final JdbcTemplate jdbcTemplate = new JdbcTemplate(server.getConnection());

QueryTranslator queryTranslator = new QueryTranslator();
QueryBuilder queryBuilder = new QueryBuilder();

jdbcTemplate.execute(queryTranslator.getCreateTableQuery(Person.class));
jdbcTemplate.execute(queryBuilder.getCreateTableQuery(Person.class));

executeInitializedQuery(jdbcTemplate, queryTranslator);
executeInitializedQuery(jdbcTemplate, queryBuilder);

querySelectAll(jdbcTemplate, queryTranslator);
querySelectAll(jdbcTemplate, queryBuilder);

querySelectById(jdbcTemplate, queryTranslator);
querySelectById(jdbcTemplate, queryBuilder);

jdbcTemplate.execute(queryTranslator.getDeleteByIdQuery(Person.class, 2L));
jdbcTemplate.execute(queryBuilder.getDeleteByIdQuery(Person.class, 2L));

querySelectAll(jdbcTemplate, queryTranslator);
querySelectAll(jdbcTemplate, queryBuilder);

server.stop();
} catch (Exception e) {
Expand All @@ -50,33 +50,33 @@ public static void main(String[] args) {
}
}

private static void querySelectById(JdbcTemplate jdbcTemplate, QueryTranslator queryTranslator) {
private static void querySelectById(JdbcTemplate jdbcTemplate, QueryBuilder queryBuilder) {
Person person = jdbcTemplate.queryForObject(
queryTranslator.getSelectByIdQuery(Person.class, 2L),
queryBuilder.getSelectByIdQuery(Person.class, 2L),
rowMapper
);

logger.info("Person: {}", person);
}

private static void querySelectAll(JdbcTemplate jdbcTemplate, QueryTranslator queryTranslator) {
private static void querySelectAll(JdbcTemplate jdbcTemplate, QueryBuilder queryBuilder) {
List<Person> persons = jdbcTemplate.query(
queryTranslator.getSelectAllQuery(Person.class),
queryBuilder.getSelectAllQuery(Person.class),
rowMapper
);

persons.forEach(person -> logger.info("Person: {}", person));
}

private static void executeInitializedQuery(JdbcTemplate jdbcTemplate, QueryTranslator queryTranslator) {
private static void executeInitializedQuery(JdbcTemplate jdbcTemplate, QueryBuilder queryBuilder) {
List<Person> persons = List.of(
new Person("John", 23, "[email protected]"),
new Person("Smith", 33, "[email protected]"),
new Person("rolroralra", 37, "[email protected]")
);

persons.stream()
.map(queryTranslator::getInsertQuery)
.map(queryBuilder::getInsertQuery)
.forEach(jdbcTemplate::execute);
}

Expand Down
29 changes: 29 additions & 0 deletions src/main/java/persistence/entity/EntityManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package persistence.entity;

public interface EntityManager {
/**
* Finds an entity by its id
*
* @param entityClass the entity class
* @param id the id of the entity
* @return the entity
*
* @param <T> the entity type
*/
<T> T find(Class<T> entityClass, Long id);
rolroralra marked this conversation as resolved.
Show resolved Hide resolved

/**
* Persists the entity object
*
* @param entity entity object to persist
* @return the persisted entity object
*/
Object persist(Object entity);

/**
* Removes the entity object
*
* @param entity entity object to remove
*/
void remove(Object entity);
}
54 changes: 54 additions & 0 deletions src/main/java/persistence/entity/EntityRowMapperFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package persistence.entity;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.stream.Collectors;
import jdbc.RowMapper;
import persistence.exception.ReflectionRuntimeException;
import persistence.sql.ddl.ColumnQueryBuilder;

public class EntityRowMapperFactory {

private EntityRowMapperFactory() {
// Do nothing
}

public static class CacheEntityRowMapperFactory {
private CacheEntityRowMapperFactory() {
// Do nothing
}

private static final EntityRowMapperFactory INSTANCE = new EntityRowMapperFactory();
}

public static EntityRowMapperFactory getInstance() {
return CacheEntityRowMapperFactory.INSTANCE;
}

public <T> RowMapper<T> getRowMapper(Class<T> entityClass) {
return resultSet -> {
try {
ColumnQueryBuilder columnQueryBuilder = new ColumnQueryBuilder();
Constructor<T> declaredConstructor = entityClass.getDeclaredConstructor();
declaredConstructor.setAccessible(true);
T entity = entityClass.getDeclaredConstructor().newInstance();

List<Field> columnFieldList = columnQueryBuilder.getColumnFieldStream(entityClass)
.collect(Collectors.toList());

for (Field field : columnFieldList) {
field.setAccessible(true);
String columnName = columnQueryBuilder.getColumnNameFrom(field);
field.set(entity, resultSet.getObject(columnName));
}

return entity;
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException |
InvocationTargetException e) {
throw new ReflectionRuntimeException(entityClass, e);
}
};
}
}
48 changes: 48 additions & 0 deletions src/main/java/persistence/entity/impl/EntityManagerImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package persistence.entity.impl;

import jdbc.JdbcTemplate;
import persistence.entity.EntityManager;
import persistence.entity.EntityRowMapperFactory;
import persistence.sql.QueryBuilder;

public class EntityManagerImpl implements EntityManager {
private final JdbcTemplate jdbcTemplate;

private final QueryBuilder queryBuilder;

public EntityManagerImpl(JdbcTemplate jdbcTemplate) {
this(jdbcTemplate, new QueryBuilder());
}

public EntityManagerImpl(JdbcTemplate jdbcTemplate, QueryBuilder queryBuilder) {
this.jdbcTemplate = jdbcTemplate;
this.queryBuilder = queryBuilder;
}


@Override
public <T> T find(Class<T> entityClass, Long id) {
String selectByIdQuery = queryBuilder.getSelectByIdQuery(entityClass, id);

return jdbcTemplate.queryForObject(
selectByIdQuery,
EntityRowMapperFactory.getInstance().getRowMapper(entityClass)
);
}

@Override
public Object persist(Object entity) {
String insertQuery = queryBuilder.getInsertQuery(entity);

jdbcTemplate.execute(insertQuery);

return entity;
}

@Override
public void remove(Object entity) {
String deleteQueryFromEntity = queryBuilder.getDeleteQueryFromEntity(entity);

jdbcTemplate.execute(deleteQueryFromEntity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package persistence.exception;

public class ReflectionRuntimeException extends RuntimeException {
public ReflectionRuntimeException(Class<?> clazz, Exception e) {
super("Reflection error on class: " + clazz.getName(), e);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package persistence.sql.exception;
package persistence.exception;

public class UnsupportedClassException extends RuntimeException {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package persistence.sql.exception.constraints;
package persistence.exception;

import java.lang.reflect.Field;

Expand Down
70 changes: 70 additions & 0 deletions src/main/java/persistence/sql/AbstractQueryBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package persistence.sql;

import jakarta.persistence.Column;
import jakarta.persistence.Id;
import jakarta.persistence.Transient;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.Stream;
import persistence.exception.UnsupportedClassException;

public abstract class AbstractQueryBuilder {

protected AbstractQueryBuilder() {

}

/**
* Get the stream of fields that are not annotated with <code>@Transient</code> from the entity class
* @param entityClass Entity class
* @return Stream of fields that are annotated with <code>@Column</code> or <code>@Id</code>
* @see Column
* @see Id
* @see Transient
*/
protected Stream<Field> getColumnFieldStream(Class<?> entityClass) {
return Arrays.stream(entityClass.getDeclaredFields())
.filter(field -> !field.isAnnotationPresent(Transient.class))
.sorted(Comparator.comparing(field -> field.isAnnotationPresent(Id.class) ? 0 : 1));
}
Comment on lines +26 to +30

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

추상 클래스를 이용하는 방식으로 수정해보셨네요 ! 👍 👍
다만 추상클래스를 사용하게되면 자식 클래스가 부모 클래스의 구조를 알아야하는 단점이 있는 것 같습니다.

그리고 책임에 대해 생각해보면 지금 쿼리 빌더는 아래와 같이 두 가지 책임을 가지고 있는데요.

  1. 엔티티 클래스의 정보(어노테이션, 필드 이름 등)를 조회한다.
  2. 엔티티 클래스의 정보들을 토대로 SQL을 만든다.

쿼리 빌더에서 SQL 만을 만드는 책임만 남기고,
클래스 정보를 얻어오는 책임은 협력을 통해 별도의 객체를 만들어 위임해보는건 어떨까요?


/**
* Get the column name from the field
* @param field Field
* @return Column name from the field
*/
protected String getColumnNameFrom(Field field) {
if (!field.isAnnotationPresent(Column.class)) {
return field.getName();
}

Column column = field.getAnnotation(Column.class);

if (column.name().isEmpty()) {
return field.getName();
}

return column.name();
}

/**
* Get the column value from the object
* @param columnValue Column value object
* @return Column value from the object
*/
protected String getColumnValueFromObject(Object columnValue) {
// TODO: remove this else-if statement
if (columnValue.getClass().equals(Boolean.class)) {
return columnValue == Boolean.TRUE ? "1" : "0";
} else if (columnValue.getClass().equals(String.class)) {
return String.format("'%s'", columnValue);
} else if (columnValue.getClass().equals(Integer.class)) {
return columnValue.toString();
} else if (columnValue.getClass().equals(Long.class)) {
return columnValue.toString();
}

throw new UnsupportedClassException(columnValue.getClass());
}
}
86 changes: 86 additions & 0 deletions src/main/java/persistence/sql/QueryBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package persistence.sql;


import java.lang.reflect.Field;
import persistence.sql.ddl.CreateQueryBuilder;
import persistence.sql.ddl.DropQueryBuilder;
import persistence.sql.ddl.TableQueryBuilder;
import persistence.sql.dml.DeleteQueryBuilder;
import persistence.sql.dml.InsertQueryBuilder;
import persistence.sql.dml.SelectQueryBuilder;

public class QueryBuilder extends AbstractQueryBuilder {

private final TableQueryBuilder tableQueryBuilder;

private final SelectQueryBuilder selectQueryBuilder;

private final DeleteQueryBuilder deleteQueryBuilder;

private final InsertQueryBuilder insertQueryTranslator;

private final DropQueryBuilder dropQueryBuilder;

private final CreateQueryBuilder createQueryBuilder;

public QueryBuilder() {
this(new TableQueryBuilder());
}

public QueryBuilder(TableQueryBuilder tableQueryBuilder) {
this.tableQueryBuilder = tableQueryBuilder;
this.selectQueryBuilder = new SelectQueryBuilder(tableQueryBuilder);
this.deleteQueryBuilder = new DeleteQueryBuilder(tableQueryBuilder);
this.insertQueryTranslator = new InsertQueryBuilder(tableQueryBuilder);
this.dropQueryBuilder = new DropQueryBuilder(tableQueryBuilder);
this.createQueryBuilder = new CreateQueryBuilder(tableQueryBuilder);
}

public String getCreateTableQuery(final Class<?> entityClass) {
return createQueryBuilder.getCreateTableQuery(entityClass);
}

public String getDropTableQuery(Class<?> entityClass) {
return dropQueryBuilder.getDropTableQuery(entityClass);
}

public String getInsertQuery(Object entity) {
return insertQueryTranslator.getInsertQuery(entity);
}

public String getSelectAllQuery(Class<?> entityClass) {
return selectQueryBuilder.getSelectAllQuery(entityClass);
}

public String getSelectByIdQuery(Class<?> entityClass, Object id) {
return selectQueryBuilder.getSelectByIdQuery(entityClass, id);
}

public String getSelectCountQuery(Class<?> entityClass) {
return selectQueryBuilder.getSelectCountQuery(entityClass);
}

public String getDeleteAllQuery(Class<?> entityClass) {
return deleteQueryBuilder.getDeleteAllQuery(entityClass);
}

public String getDeleteByIdQuery(Class<?> entityClass, Object id) {
return deleteQueryBuilder.getDeleteByIdQuery(entityClass, id);
}

public String getDeleteQueryFromEntity(Object entity) {
return deleteQueryBuilder.getDeleteQueryFromEntity(entity);
}

public String getTableNameFrom(Class<?> entityClass) {
return tableQueryBuilder.getTableNameFrom(entityClass);
}

public String getColumnDefinitionFrom(Field field) {
return createQueryBuilder.getColumnDefinitionFrom(field);
}

public String getColumnDefinitionsFrom(Class<?> entityClass) {
return createQueryBuilder.getColumnDefinitionsFrom(entityClass);
}
}
Loading