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

merge "insert" and then "select" to a single "insert … returning *" statement #2021

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,20 @@ private Entity fetchEntity(EntityType entityType, PkValue id) {

@Override
public boolean insert(Entity entity, UpdateMode updateMode) throws NoSuchEntityException, IncompleteEntityException {
boolean result = doInsert(entity, updateMode);
if (result) {
Entity newEntity = fetchEntity(
entity.getEntityType(),
entity.getPrimaryKeyValues());
Entity newEntity = doInsert(entity, updateMode);
if (newEntity != null) {
newEntity.setQuery(getCoreSettings().getModelRegistry().getMessageQueryGenerator().getQueryFor(entity.getEntityType()));
changedEntities.add(
new EntityChangedMessage()
.setEventType(EntityChangedMessage.Type.CREATE)
.setEntity(newEntity));
return true;
} else {
return false;
}
return result;
}

public abstract boolean doInsert(Entity entity, UpdateMode updateMode) throws NoSuchEntityException, IncompleteEntityException;
public abstract Entity doInsert(Entity entity, UpdateMode updateMode) throws NoSuchEntityException, IncompleteEntityException;

@Override
public boolean delete(PathElementEntity pathElement) throws NoSuchEntityException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public interface JooqPersistenceManager extends LiquibaseUser, PersistenceManage

void doDelete(ResourcePath path, Query query);

boolean doInsert(Entity entity, UpdateMode updateMode) throws NoSuchEntityException, IncompleteEntityException;
Entity doInsert(Entity entity, UpdateMode updateMode) throws NoSuchEntityException, IncompleteEntityException;

EntityChangedMessage doUpdate(PathElementEntity pathElement, Entity entity, UpdateMode updateMode) throws NoSuchEntityException, IncompleteEntityException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,10 @@ public Object get(ResourcePath path, Query query) {
}

@Override
public boolean doInsert(Entity entity, UpdateMode updateMode) throws NoSuchEntityException, IncompleteEntityException {
public Entity doInsert(Entity entity, UpdateMode updateMode) throws NoSuchEntityException, IncompleteEntityException {
init();
StaMainTable<?> table = getTableCollection().getTableForType(entity.getEntityType());
return table.insertIntoDatabase(this, entity, updateMode);
return table.insertIntoDatabase(this, entity, updateMode, dataSize);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,10 @@ public Object get(ResourcePath path, Query query) {
}

@Override
public boolean doInsert(Entity entity, UpdateMode updateMode) throws NoSuchEntityException, IncompleteEntityException {
public Entity doInsert(Entity entity, UpdateMode updateMode) throws NoSuchEntityException, IncompleteEntityException {
init();
StaMainTable<?> table = getTableCollection().getTableForType(entity.getEntityType());
return table.insertIntoDatabase(this, entity, updateMode);
return table.insertIntoDatabase(this, entity, updateMode, dataSize);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public interface StaMainTable<T extends StaMainTable<T>> extends StaTable<T> {

public Entity entityFromQuery(Record tuple, QueryState<T> state, DataSize dataSize);

public boolean insertIntoDatabase(JooqPersistenceManager pm, Entity entity, UpdateMode updateMode) throws NoSuchEntityException, IncompleteEntityException;
public Entity insertIntoDatabase(JooqPersistenceManager pm, Entity entity, UpdateMode updateMode, DataSize dataSize) throws NoSuchEntityException, IncompleteEntityException;

public EntityChangedMessage updateInDatabase(JooqPersistenceManager pm, Entity entity, PkValue entityId, UpdateMode updateMode) throws NoSuchEntityException, IncompleteEntityException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@
import de.fraunhofer.iosb.ilt.frostserver.property.EntityPropertyMain;
import de.fraunhofer.iosb.ilt.frostserver.property.NavigationPropertyMain;
import de.fraunhofer.iosb.ilt.frostserver.property.NavigationPropertyMain.NavigationPropertyEntitySet;
import de.fraunhofer.iosb.ilt.frostserver.property.Property;
import de.fraunhofer.iosb.ilt.frostserver.service.UpdateMode;
import de.fraunhofer.iosb.ilt.frostserver.util.exception.IncompleteEntityException;
import de.fraunhofer.iosb.ilt.frostserver.util.exception.NoSuchEntityException;
import de.fraunhofer.iosb.ilt.frostserver.util.user.PrincipalExtended;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -256,16 +258,16 @@ public Entity entityFromQuery(Record tuple, QueryState<T> state, DataSize dataSi
}

@Override
public boolean insertIntoDatabase(JooqPersistenceManager pm, Entity entity, UpdateMode updateMode) throws NoSuchEntityException, IncompleteEntityException {
public Entity insertIntoDatabase(JooqPersistenceManager pm, Entity entity, UpdateMode updateMode, DataSize dataSize) throws NoSuchEntityException, IncompleteEntityException {
final T thisTable = getThis();
EntityFactories entityFactories = pm.getEntityFactories();
EntityType entityType = entity.getEntityType();
Map<Field, Object> insertFields = new HashMap<>();

// First, run the pre-insert hooks in the PRE_RELATION fase.
// First, run the pre-insert hooks in the PRE_RELATION phase.
for (SortingWrapper<Double, HookPreInsert> hookWrapper : hooksPreInsert) {
if (!hookWrapper.getObject().preInsertIntoDatabase(PRE_RELATIONS, pm, entity, insertFields)) {
return false;
return null;
}
}

Expand All @@ -281,10 +283,10 @@ public boolean insertIntoDatabase(JooqPersistenceManager pm, Entity entity, Upda
}
}

// Third, run the pre-insert hooks in POST_RELATION fase.
// Third, run the pre-insert hooks in POST_RELATION phase.
for (SortingWrapper<Double, HookPreInsert> hookWrapper : hooksPreInsert) {
if (!hookWrapper.getObject().preInsertIntoDatabase(POST_RELATIONS, pm, entity, insertFields)) {
return false;
return null;
}
}

Expand All @@ -305,13 +307,19 @@ public boolean insertIntoDatabase(JooqPersistenceManager pm, Entity entity, Upda
}

// Sixth, do the actual insert.
// This returns the entire inserted row. It may or may not be changed by databse rules or before-triggers.
DSLContext dslContext = pm.getDslContext();
Object[] entityPkValues = dslContext.insertInto(thisTable)
Record result = dslContext.insertInto(thisTable)
.set(insertFields)
.returningResult(thisTable.getPkFields())
.fetchOneArray();
LOGGER.debug("Inserted {} with id = {}.", entityType, entityPkValues);
entity.setPrimaryKeyValues(new PkValue(entityPkValues));
.returningResult(thisTable.fields())
.fetchAny();
LOGGER.debug("Inserted {} with id = {}.", entityType, result);
if (result == null) {
return null;
}
Set<Object> pks = new HashSet<>();
thisTable.getPkFields().stream().map(result::get).forEachOrdered(pks::add);
entity.setPrimaryKeyValues(new PkValue(pks.toArray()));

// Seventh, deal with set-navigation links.
// TODO: add Priority number, handle priorities >= 0 here
Expand All @@ -322,14 +330,21 @@ public boolean insertIntoDatabase(JooqPersistenceManager pm, Entity entity, Upda
}
}

// Finally, run the post-insert hooks in POST_INSERT fase.
// Eigth, run the post-insert hooks in POST_INSERT phase.
for (SortingWrapper<Double, HookPostInsert> hookWrapper : hooksPostInsert) {
if (!hookWrapper.getObject().postInsertIntoDatabase(pm, entity, insertFields)) {
return false;
return null;
}
}

return true;
// Finally, create a new Entity from the returned result and return it.
Set<Property> propertySet = entityType.getPropertySet();
Set<PropertyFields<T>> propertyFieldsSet = getPropertyFieldRegistry().getFieldsForProperties(propertySet);
Entity newEntity = new DefaultEntity(getEntityType(), entity.getPrimaryKeyValues());
for (PropertyFields<T> sp : propertyFieldsSet) {
sp.converter.convert(thisTable, result, newEntity, dataSize);
}
return newEntity;
}

/**
Expand Down
Loading