-
Notifications
You must be signed in to change notification settings - Fork 33
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
Step1: EntityLoader 구현 #6
Changes from all commits
292be97
65986b1
0f0c26a
066d85c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package persistence.entity; | ||
|
||
import jdbc.RowMapper; | ||
import jdbc.exception.RowMapException; | ||
import persistence.sql.util.ColumnFields; | ||
import persistence.sql.util.ColumnName; | ||
|
||
import java.lang.reflect.Field; | ||
import java.sql.ResultSet; | ||
import java.sql.ResultSetMetaData; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public class EntityLoader<T> implements RowMapper<T> { | ||
private final Class<T> clazz; | ||
private final Map<String, Field> fieldsByName; | ||
|
||
public EntityLoader(Class<T> clazz) { | ||
this.clazz = clazz; | ||
this.fieldsByName = ColumnFields.forQuery(clazz) | ||
.stream().collect(Collectors.toMap( | ||
ColumnName::build, | ||
field -> { | ||
field.setAccessible(true); | ||
return field; | ||
} | ||
)); | ||
Comment on lines
+20
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 메서드 분리를 해보아도 좋을 것 같아요! |
||
} | ||
|
||
@Override | ||
public T mapRow(ResultSet resultSet) { | ||
try { | ||
final T object = clazz.getDeclaredConstructor().newInstance(); | ||
final ResultSetMetaData metaData = resultSet.getMetaData(); | ||
final int columnCount = metaData.getColumnCount(); | ||
for (int i = 1; i <= columnCount; i++) { | ||
fieldsByName.get( | ||
metaData.getColumnLabel(i) | ||
).set( | ||
object, | ||
resultSet.getObject(metaData.getColumnName(i)) | ||
); | ||
} | ||
return object; | ||
Comment on lines
+31
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ResultSetMetaData 위주로 EntityLoader 를 구현해보았습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 동적으로 구현 잘하셨군요! 크 |
||
} catch (Exception e) { | ||
throw new RowMapException(e); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
package persistence.entity; | ||
|
||
import jdbc.JdbcTemplate; | ||
import jdbc.RowMapperImpl; | ||
import persistence.sql.dml.DmlBuilder; | ||
|
||
import java.util.List; | ||
|
@@ -31,9 +30,9 @@ public <T> Optional<T> find(Class<T> clazz, Object id) { | |
|
||
@Override | ||
public void persist(Object entity) { | ||
final String query = hasEntity(entity) | ||
? dml.getUpdateQuery(entity) | ||
: dml.getInsertQuery(entity); | ||
final String query = isNew(entity) | ||
? dml.getInsertQuery(entity) | ||
: dml.getUpdateQuery(entity); | ||
Comment on lines
+33
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 삼항 연산자도 좋으나 코드의 가시성을 위해 분리해보아도 좋을 것 같습니다 |
||
jdbcTemplate.execute(query); | ||
context.addEntity(entity); | ||
context.getDatabaseSnapshot( | ||
|
@@ -70,17 +69,25 @@ public void detach(Object entity) { | |
|
||
@Override | ||
public boolean isDirty(Object entity) { | ||
return !hasEntity(entity) || !EntityHelper.equals( | ||
return !EntityHelper.equals( | ||
entity, | ||
context.getCachedDatabaseSnapshot(new EntityKey<>(entity)) | ||
); | ||
} | ||
|
||
@Override | ||
public boolean isNew(Object entity) { | ||
return !find( | ||
entity.getClass(), | ||
new EntityKey(entity).getEntityId() | ||
).isPresent(); | ||
} | ||
|
||
private <T> Optional<T> findFromDB(EntityKey<T> key) { | ||
Class<T> clazz = key.getEntityClass(); | ||
List<T> entities = jdbcTemplate.query( | ||
dml.getFindByIdQuery(clazz, key.getEntityId()), | ||
new RowMapperImpl<>(clazz) | ||
new EntityLoader<>(clazz) | ||
); | ||
if (entities.isEmpty()) { | ||
return Optional.empty(); | ||
|
@@ -91,11 +98,4 @@ private <T> Optional<T> findFromDB(EntityKey<T> key) { | |
new EntityKey<>(entity), entity | ||
)); | ||
} | ||
|
||
private boolean hasEntity(Object entity) { | ||
return find( | ||
entity.getClass(), | ||
new EntityKey(entity).getEntityId() | ||
).isPresent(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
else-if 를 사용하지 않고 구현할 수 있을 것 같은데요~
도전해보아도 좋을 것 같습니다 :)