make casser as a central entity cache
This commit is contained in:
parent
00a3e20a98
commit
6b50e86dde
4 changed files with 59 additions and 42 deletions
|
@ -26,7 +26,8 @@ import com.noorq.casser.config.CasserSettings;
|
||||||
import com.noorq.casser.config.DefaultCasserSettings;
|
import com.noorq.casser.config.DefaultCasserSettings;
|
||||||
import com.noorq.casser.core.reflect.CasserPropertyNode;
|
import com.noorq.casser.core.reflect.CasserPropertyNode;
|
||||||
import com.noorq.casser.core.reflect.DslExportable;
|
import com.noorq.casser.core.reflect.DslExportable;
|
||||||
import com.noorq.casser.mapping.MappingRepositoryBuilder;
|
import com.noorq.casser.mapping.CasserMappingEntity;
|
||||||
|
import com.noorq.casser.support.CasserMappingException;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -113,4 +114,41 @@ public final class Casser {
|
||||||
return settings.getMapperInstantiator().instantiate(iface, src, classLoader);
|
return settings.getMapperInstantiator().instantiate(iface, src, classLoader);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static CasserMappingEntity entity(Class<?> iface) {
|
||||||
|
|
||||||
|
Object dsl = dsl(iface);
|
||||||
|
|
||||||
|
DslExportable e = (DslExportable) dsl;
|
||||||
|
|
||||||
|
return e.getCasserMappingEntity();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CasserMappingEntity resolve(Object ifaceOrDsl) {
|
||||||
|
|
||||||
|
if (ifaceOrDsl == null) {
|
||||||
|
throw new CasserMappingException("ifaceOrDsl is null");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ifaceOrDsl instanceof DslExportable) {
|
||||||
|
|
||||||
|
DslExportable e = (DslExportable) ifaceOrDsl;
|
||||||
|
|
||||||
|
return e.getCasserMappingEntity();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ifaceOrDsl instanceof Class) {
|
||||||
|
|
||||||
|
Class<?> iface = (Class<?>) ifaceOrDsl;
|
||||||
|
|
||||||
|
if (!iface.isInterface()) {
|
||||||
|
throw new CasserMappingException("class is not an interface " + iface);
|
||||||
|
}
|
||||||
|
|
||||||
|
return entity(iface);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new CasserMappingException("unknown dsl object or mapping interface " + ifaceOrDsl);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -118,25 +118,18 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl
|
||||||
|
|
||||||
public <E> SelectOperation<E> select(Class<E> entityClass) {
|
public <E> SelectOperation<E> select(Class<E> entityClass) {
|
||||||
Objects.requireNonNull(entityClass, "entityClass is empty");
|
Objects.requireNonNull(entityClass, "entityClass is empty");
|
||||||
|
|
||||||
Class<?> iface = MappingUtil.getMappingInterface(entityClass);
|
|
||||||
CasserMappingEntity entity = mappingRepository.getEntity(iface);
|
|
||||||
ColumnValueProvider valueProvider = getValueProvider();
|
ColumnValueProvider valueProvider = getValueProvider();
|
||||||
|
CasserMappingEntity entity = Casser.entity(entityClass);
|
||||||
return new SelectOperation<E>(this, entity, (r) -> {
|
return new SelectOperation<E>(this, entity, (r) -> {
|
||||||
Map<String, Object> map = new RowProviderMap(r, valueProvider, entity);
|
Map<String, Object> map = new RowProviderMap(r, valueProvider, entity);
|
||||||
return (E) Casser.map(iface, map);
|
return (E) Casser.map(entityClass, map);
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public <E> SelectOperation<E> select(Class<E> entityClass, Function<Row, E> rowMapper) {
|
public <E> SelectOperation<E> select(Class<E> entityClass, Function<Row, E> rowMapper) {
|
||||||
Objects.requireNonNull(entityClass, "entityClass is empty");
|
Objects.requireNonNull(entityClass, "entityClass is empty");
|
||||||
|
return new SelectOperation<E>(this, Casser.entity(entityClass), rowMapper);
|
||||||
Class<?> iface = MappingUtil.getMappingInterface(entityClass);
|
|
||||||
CasserMappingEntity entity = mappingRepository.getEntity(iface);
|
|
||||||
|
|
||||||
return new SelectOperation<E>(this, entity, rowMapper);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public <V1> SelectOperation<Tuple1<V1>> select(Getter<V1> getter1) {
|
public <V1> SelectOperation<Tuple1<V1>> select(Getter<V1> getter1) {
|
||||||
|
@ -251,12 +244,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl
|
||||||
|
|
||||||
public CountOperation count(Object dsl) {
|
public CountOperation count(Object dsl) {
|
||||||
Objects.requireNonNull(dsl, "dsl is empty");
|
Objects.requireNonNull(dsl, "dsl is empty");
|
||||||
|
return new CountOperation(this, Casser.resolve(dsl));
|
||||||
Class<?> iface = MappingUtil.getMappingInterface(dsl);
|
|
||||||
|
|
||||||
CasserMappingEntity entity = mappingRepository.getEntity(iface);
|
|
||||||
|
|
||||||
return new CountOperation(this, entity);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public <V> UpdateOperation update() {
|
public <V> UpdateOperation update() {
|
||||||
|
@ -280,8 +268,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl
|
||||||
Objects.requireNonNull(pojo, "pojo is empty");
|
Objects.requireNonNull(pojo, "pojo is empty");
|
||||||
|
|
||||||
Class<?> iface = MappingUtil.getMappingInterface(pojo);
|
Class<?> iface = MappingUtil.getMappingInterface(pojo);
|
||||||
|
CasserMappingEntity entity = Casser.entity(iface);
|
||||||
CasserMappingEntity entity = mappingRepository.getEntity(iface);
|
|
||||||
|
|
||||||
return new InsertOperation(this, entity, pojo, true);
|
return new InsertOperation(this, entity, pojo, true);
|
||||||
}
|
}
|
||||||
|
@ -294,8 +281,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl
|
||||||
Objects.requireNonNull(pojo, "pojo is empty");
|
Objects.requireNonNull(pojo, "pojo is empty");
|
||||||
|
|
||||||
Class<?> iface = MappingUtil.getMappingInterface(pojo);
|
Class<?> iface = MappingUtil.getMappingInterface(pojo);
|
||||||
|
CasserMappingEntity entity = Casser.entity(iface);
|
||||||
CasserMappingEntity entity = mappingRepository.getEntity(iface);
|
|
||||||
|
|
||||||
return new InsertOperation(this, entity, pojo, false);
|
return new InsertOperation(this, entity, pojo, false);
|
||||||
}
|
}
|
||||||
|
@ -306,12 +292,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl
|
||||||
|
|
||||||
public DeleteOperation delete(Object dsl) {
|
public DeleteOperation delete(Object dsl) {
|
||||||
Objects.requireNonNull(dsl, "dsl is empty");
|
Objects.requireNonNull(dsl, "dsl is empty");
|
||||||
|
return new DeleteOperation(this, Casser.resolve(dsl));
|
||||||
Class<?> iface = MappingUtil.getMappingInterface(dsl);
|
|
||||||
|
|
||||||
CasserMappingEntity entity = mappingRepository.getEntity(iface);
|
|
||||||
|
|
||||||
return new DeleteOperation(this, entity);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Session getSession() {
|
public Session getSession() {
|
||||||
|
|
|
@ -80,18 +80,15 @@ public final class MappingRepositoryBuilder {
|
||||||
|
|
||||||
public CasserMappingEntity add(Object dsl, Optional<CasserEntityType> type) {
|
public CasserMappingEntity add(Object dsl, Optional<CasserEntityType> type) {
|
||||||
|
|
||||||
Class<?> iface = MappingUtil.getMappingInterface(dsl);
|
CasserMappingEntity casserEntity = Casser.resolve(dsl);
|
||||||
|
|
||||||
|
Class<?> iface = casserEntity.getMappingInterface();
|
||||||
|
|
||||||
CasserMappingEntity entity = entityMap.get(iface);
|
CasserMappingEntity entity = entityMap.get(iface);
|
||||||
|
|
||||||
if (entity == null) {
|
if (entity == null) {
|
||||||
|
|
||||||
if (!(dsl instanceof DslExportable)) {
|
entity = casserEntity;
|
||||||
dsl = Casser.dsl(iface);
|
|
||||||
}
|
|
||||||
|
|
||||||
DslExportable e = (DslExportable) dsl;
|
|
||||||
|
|
||||||
entity = e.getCasserMappingEntity();
|
|
||||||
|
|
||||||
if (type.isPresent() && entity.getType() != type.get()) {
|
if (type.isPresent() && entity.getType() != type.get()) {
|
||||||
throw new CasserMappingException("unexpected entity type " + entity.getType() + " for " + entity);
|
throw new CasserMappingException("unexpected entity type " + entity.getType() + " for " + entity);
|
||||||
|
|
|
@ -168,19 +168,19 @@ public final class MappingUtil {
|
||||||
.apply(iface.getSimpleName());
|
.apply(iface.getSimpleName());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Class<?> getMappingInterface(Object entity) {
|
public static Class<?> getMappingInterface(Object pojo) {
|
||||||
|
|
||||||
Class<?> iface = null;
|
Class<?> iface = null;
|
||||||
|
|
||||||
if (entity instanceof Class) {
|
if (pojo instanceof Class) {
|
||||||
iface = (Class<?>) entity;
|
iface = (Class<?>) pojo;
|
||||||
|
|
||||||
if (!iface.isInterface()) {
|
if (!iface.isInterface()) {
|
||||||
throw new CasserMappingException("expected interface " + iface);
|
throw new CasserMappingException("expected interface " + iface);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
Class<?>[] ifaces = entity.getClass().getInterfaces();
|
Class<?>[] ifaces = pojo.getClass().getInterfaces();
|
||||||
|
|
||||||
int len = ifaces.length;
|
int len = ifaces.length;
|
||||||
for (int i = 0; i != len; ++i) {
|
for (int i = 0; i != len; ++i) {
|
||||||
|
@ -192,7 +192,8 @@ public final class MappingUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (iface.getDeclaredAnnotation(Table.class) != null ||
|
if (iface.getDeclaredAnnotation(Table.class) != null ||
|
||||||
iface.getDeclaredAnnotation(UserDefinedType.class) != null) {
|
iface.getDeclaredAnnotation(UserDefinedType.class) != null ||
|
||||||
|
iface.getDeclaredAnnotation(Tuple.class) != null) {
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -204,7 +205,7 @@ public final class MappingUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (iface == null) {
|
if (iface == null) {
|
||||||
throw new CasserMappingException("dsl interface not found for " + entity);
|
throw new CasserMappingException("dsl interface not found for " + pojo);
|
||||||
}
|
}
|
||||||
|
|
||||||
return iface;
|
return iface;
|
||||||
|
|
Loading…
Reference in a new issue