Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Recursive dao call ability was added #38

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
18 changes: 15 additions & 3 deletions jirm-orm/src/main/java/co/jirm/orm/OrmConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,29 @@

import co.jirm.core.execute.SqlExecutor;
import co.jirm.mapper.SqlObjectConfig;
import co.jirm.orm.dao.DaoHooks;
import co.jirm.orm.writer.SqlWriterStrategy;

import com.google.common.base.Optional;

public class OrmConfig {
private final SqlExecutor sqlExecutor;
private final SqlObjectConfig sqlObjectConfig;
private final SqlWriterStrategy sqlWriterStrategy;

private OrmConfig(SqlExecutor sqlExecutor, SqlObjectConfig sqlObjectConfig, SqlWriterStrategy sqlWriterStrategy) {
private final Optional<DaoHooks> daoHooks;

public OrmConfig(SqlExecutor sqlExecutor, SqlObjectConfig sqlObjectConfig, SqlWriterStrategy sqlWriterStrategy, Optional<DaoHooks> daoHooks) {
super();
this.sqlExecutor = sqlExecutor;
this.sqlObjectConfig = sqlObjectConfig;
this.sqlWriterStrategy = sqlWriterStrategy;
this.daoHooks = daoHooks;
}


public OrmConfig(SqlExecutor sqlExecutor, SqlObjectConfig sqlObjectConfig, SqlWriterStrategy sqlWriterStrategy) {
this(sqlExecutor, sqlObjectConfig, sqlWriterStrategy, Optional.<DaoHooks>absent());
}

public static OrmConfig newInstance(SqlExecutor sqlExecutor, SqlObjectConfig objectConfig) {
return new OrmConfig(sqlExecutor, SqlObjectConfig.DEFAULT, SqlWriterStrategy.newInstance("\n"));
}
Expand All @@ -51,4 +59,8 @@ public SqlObjectConfig getSqlObjectConfig() {
public SqlExecutor getSqlExecutor() {
return sqlExecutor;
}

public Optional<DaoHooks> getDaoHooks() {
return daoHooks;
}
}
11 changes: 11 additions & 0 deletions jirm-orm/src/main/java/co/jirm/orm/dao/DaoHooks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package co.jirm.orm.dao;

import co.jirm.mapper.definition.*;

import java.util.*;

public interface DaoHooks {
public void beforeInsert(SqlObjectDefinition<?> definition, Map<String, Object> values);

// TODO to add beforeInsertMaps() method and its usage to the JirmDao.
}
67 changes: 58 additions & 9 deletions jirm-orm/src/main/java/co/jirm/orm/dao/JirmDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import co.jirm.mapper.copy.CopyBuilder;
import co.jirm.mapper.definition.SqlObjectDefinition;
import co.jirm.mapper.definition.SqlParameterDefinition;
import co.jirm.orm.JirmFactory;
import co.jirm.orm.OrmConfig;
import co.jirm.orm.builder.delete.DeleteBuilderFactory;
import co.jirm.orm.builder.delete.DeleteRootClauseBuilder;
Expand Down Expand Up @@ -61,6 +62,8 @@ public final class JirmDao<T> {
private final UpdateBuilderFactory<T> updateBuilderFactory;
private final DeleteBuilderFactory<T> deleteBuilderFactory;
private final SqlWriterStrategy writerStrategy;
private final Optional<DaoHooks> daoHooks;
private final Optional<JirmFactory> jirmFactory;

private JirmDao(
SqlExecutor sqlExecutor,
Expand All @@ -69,7 +72,9 @@ private JirmDao(
SqlWriterStrategy writerStrategy,
SelectBuilderFactory<T> selectBuilderFactory,
UpdateBuilderFactory<T> updateBuilderFactory,
DeleteBuilderFactory<T> deleteBuilderFactory) {
DeleteBuilderFactory<T> deleteBuilderFactory,
Optional<DaoHooks> daoHooks,
Optional<JirmFactory> jirmFactory) {
super();
this.sqlExecutor = sqlExecutor;
this.config = config;
Expand All @@ -78,9 +83,15 @@ private JirmDao(
this.selectBuilderFactory = selectBuilderFactory;
this.updateBuilderFactory = updateBuilderFactory;
this.deleteBuilderFactory = deleteBuilderFactory;
this.daoHooks = daoHooks;
this.jirmFactory = jirmFactory;
}

public static <T> JirmDao<T> newInstance(Class<T> type, OrmConfig config) {
return newInstance(type, config, Optional.<JirmFactory>absent());
}

public static <T> JirmDao<T> newInstance(Class<T> type, OrmConfig config, Optional<JirmFactory> jirmFactory) {
SqlObjectDefinition<T> definition = config.getSqlObjectConfig().resolveObjectDefinition(type);
SelectBuilderFactory<T> selectBuilderFactory = SelectBuilderFactory.newInstance(definition, config);
UpdateBuilderFactory<T> updateBuilderFactory = UpdateBuilderFactory.newInstance(definition, config);
Expand All @@ -90,10 +101,12 @@ public static <T> JirmDao<T> newInstance(Class<T> type, OrmConfig config) {
config.getSqlExecutor(),
config.getSqlObjectConfig(),
definition, config.getSqlWriterStrategy(),
selectBuilderFactory, updateBuilderFactory, deleteBuilderFactory);
selectBuilderFactory, updateBuilderFactory, deleteBuilderFactory,
config.getDaoHooks(),
jirmFactory);
}

private LinkedHashMap<String, Object> toLinkedHashMap(T t, boolean bulkInsert) {
private LinkedHashMap<String, Object> toLinkedHashMap(T t, boolean bulkInsert, ForeignAct foreignAct) {
LinkedHashMap<String, Object> m = config.getObjectMapper().convertObjectToSqlMap(t);
/*
* Replace the complex objects with there ids.
Expand All @@ -108,6 +121,8 @@ private LinkedHashMap<String, Object> toLinkedHashMap(T t, boolean bulkInsert) {
/*
* TODO: We only set it if the object is actually present. ie do you really want to set null?
*/
actForeign(pd.getParameterType(), m.get(pd.getParameterName()), foreignAct);

m.put(pd.getParameterName(), idDef.convertToSql(nkv.object));
}
else if (bulkInsert) {
Expand Down Expand Up @@ -139,7 +154,35 @@ else if (bulkInsert) {
return m;

}


private void actForeign(final Class<?> clazz, final Object object, ForeignAct foreignAct) {
if (object != null && jirmFactory.isPresent()) {
@SuppressWarnings("unchecked")
final JirmDao<Object> foreignDao = ((JirmDao<Object>) jirmFactory.get().daoFor(clazz));

foreignAct.act(foreignDao, object);
}
}

private static interface ForeignAct {
public <T> void act(final JirmDao<T> dao, final T object);

public static final ForeignAct NO_ACT = new ForeignAct() {
@Override
public <T> void act(final JirmDao<T> dao, final T object) {
}
};
}

private static class ForeignInsert implements ForeignAct {
public static final ForeignInsert INSERT = new ForeignInsert();

@Override
public <T> void act(final JirmDao<T> dao, final T object) {
dao.insert(object);
}
}

public CopyBuilder<T> copyBuilder() {
return CopyBuilder.newInstance(definition.getObjectType(), config.getObjectMapper());
}
Expand Down Expand Up @@ -181,7 +224,7 @@ public T findById(Object id) {
}

public void insert(T t) {
LinkedHashMap<String, Object> m = toLinkedHashMap(t, false);
LinkedHashMap<String, Object> m = toLinkedHashMap(t, false, ForeignInsert.INSERT);
Iterator<Entry<String, Object>> it = m.entrySet().iterator();
/*
* Remove the null values that are to be generated.
Expand All @@ -207,19 +250,25 @@ public int deleteById(Object id) {
}

public UpdateObjectBuilder<T> update(T t) {
LinkedHashMap<String, Object> m = toLinkedHashMap(t, false);
// todo to probably change the foreign act (probably with the ForeignAct interface).
LinkedHashMap<String, Object> m = toLinkedHashMap(t, false, ForeignAct.NO_ACT);
return updateBuilderFactory.update(m);
}

public T reload(T t) {
LinkedHashMap<String, Object> m = toLinkedHashMap(t, false);
// todo to probably change the foreign act (probably with the ForeignAct interface).
LinkedHashMap<String, Object> m = toLinkedHashMap(t, false, ForeignAct.NO_ACT);
Optional<SqlParameterDefinition> id = definition.idParameter();
check.state(id.isPresent(), "No id definition");
Optional<Object> o = id.get().valueFrom(m);
return findById(o.get());
}

public void insert(Map<String,Object> values) {
if (daoHooks.isPresent()) {
daoHooks.get().beforeInsert(definition, values);
}

StringBuilder qb = new StringBuilder();
writerStrategy.insertStatement(qb, definition, values);
sqlExecutor.update(qb.toString(), writerStrategy.fillValues(definition, values).toArray());
Expand All @@ -229,7 +278,7 @@ public void insert(Iterator<T> values, int batchSize) {
Iterator<Map<String,Object>> t = Iterators.transform(values, new Function<T, Map<String,Object>>() {
@Override
public Map<String, Object> apply(T input) {
return toLinkedHashMap(input, true);
return toLinkedHashMap(input, true, ForeignInsert.INSERT);
}
});
insertMaps(t, batchSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ protected SqlWriterStrategy(Joiner commaJoiner, String clauseSpaceSeparator) {
this.clauseSpaceSeparator = clauseSpaceSeparator;
}

protected SqlWriterStrategy(String sep) {
this(Joiner.on("," + sep), sep);
}

public static SqlWriterStrategy newInstance(String sep) {
return new SqlWriterStrategy(Joiner.on("," + sep),sep);
return new SqlWriterStrategy(sep);
}

public StringBuilder insertStatement(StringBuilder qb, final SqlObjectDefinition<?> definition, Map<String, Object> m) {
Expand Down
11 changes: 9 additions & 2 deletions jirm-spring/src/main/java/co/jirm/spring/SpringJirmFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import com.google.common.base.Optional;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;

Expand All @@ -35,9 +36,14 @@ public class SpringJirmFactory implements JirmFactory {
private final JdbcTemplate jdbcTemplate;
private final Supplier<SqlExecutor> sqlExecutorSupplier;
private final Supplier<OrmConfig> ormConfigSupplier;

private final boolean recursive;

@Autowired
public SpringJirmFactory(DataSource dataSource) {
this(dataSource, false);
}

public SpringJirmFactory(DataSource dataSource, boolean recursive) {
jdbcTemplate = new JdbcTemplate(dataSource);
ormConfigSupplier = Suppliers.memoize(new Supplier<OrmConfig>() {
@Override
Expand All @@ -51,10 +57,11 @@ public SqlExecutor get() {
return SpringJirmFactory.this.createSqlExecutor();
}
});
this.recursive = recursive;
}

public <T> JirmDao<T> daoFor(Class<T> k) {
return JirmDao.newInstance(k, ormConfigSupplier.get());
return JirmDao.newInstance(k, ormConfigSupplier.get(), recursive ? Optional.<JirmFactory>of(this) : Optional.<JirmFactory>absent());
}

protected OrmConfig createOrmConfig() {
Expand Down