Skip to content

Commit

Permalink
Merge branch 'jdk8'
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorndarri committed Sep 12, 2023
2 parents 9a90951 + 4b4d5f3 commit a755d2e
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 20 deletions.
4 changes: 3 additions & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9586,4 +9586,6 @@
LoadTestModel refactored, application initialization improved
DefaultRemoteClient.withDatabaseUser() bug fixed, copy constructor improved
ResultIterator no longer calls close() on exhaustion or error, related refactoring
Database.closeSilently() removed
Database.closeSilently() removed
EntityConnection.insertSelect() added
AbstractEntityEditModel.doInsert() now returns entities instead of keys
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ static void insert(EntityConnectionProvider connectionProvider) throws DatabaseE
.with(Artist.NAME, "My Band")
.build();

connection.insert(myBand);
myBand = connection.insertSelect(myBand);

Entity firstAlbum = entities.builder(Album.TYPE)
.with(Album.ARTIST_FK, myBand)
Expand All @@ -223,7 +223,7 @@ static void insert(EntityConnectionProvider connectionProvider) throws DatabaseE
.with(Album.TITLE, "Second album")
.build();

Collection<Entity.Key> keys = connection.insert(asList(firstAlbum, secondAlbum));
Collection<Entity.Key> albumKeys = connection.insert(asList(firstAlbum, secondAlbum));
// end::insert[]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ void addTotalsUpdatedListener(Consumer<Collection<Entity>> listener) {
}

@Override
protected Collection<Entity.Key> doInsert(Collection<? extends Entity> entities) throws DatabaseException {
protected Collection<Entity> doInsert(Collection<? extends Entity> entities) throws DatabaseException {
EntityConnection connection = connectionProvider().connection();
connection.beginTransaction();
try {
Collection<Entity.Key> keys = connection.insert(entities);
Collection<Entity> inserted = connection.insertSelect(entities);
updateTotals(entities, connection);
connection.commitTransaction();

return keys;
return inserted;
}
catch (DatabaseException e) {
connection.rollbackTransaction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ static void storeEntityConnection() throws DatabaseException {
.with(Customer.LAST_NAME, "Jackson")
.build();

Entity.Key customerKey = connection.insert(customer);
// select to get generated and default column values
customer = connection.select(customerKey);
customer = connection.insertSelect(customer);

Entity address = entities.builder(Address.TYPE)
.with(Address.CUSTOMER_FK, customer)
Expand All @@ -79,7 +77,7 @@ static void storeEntityConnection() throws DatabaseException {

customer = connection.updateSelect(customer);

connection.delete(asList(addressKey, customerKey));
connection.delete(asList(addressKey, customer.primaryKey()));

connection.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,12 @@ For inserting rows.
** {url-entity-connection}#insert{opar}is.codion.framework.domain.entity.Entity{cpar}[insert(Entity entity)]
** {url-entity-connection}#insertSelect{opar}is.codion.framework.domain.entity.Entity{cpar}[insertSelect(Entity entity)]
** {url-entity-connection}#insert{opar}java.util.Collection{cpar}[insert(Collection<? extends Entity> entities)]
** {url-entity-connection}#insertSelect{opar}java.util.Collection{cpar}[insertSelect(Collection<? extends Entity> entities)]
[source,java,indent=0]
----
include::{dir-chinook-source}/is/codion/framework/demos/chinook/manual/EntityConnectionDemo.java[tags=insert]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ public interface EntityConnection extends AutoCloseable {
*/
Entity.Key insert(Entity entity) throws DatabaseException;

/**
* Inserts the given entity, returning the inserted entity.
* Performs a commit unless a transaction is open.
* @param entity the entity to insert
* @return the inserted entity
* @throws DatabaseException in case of a database exception
*/
Entity insertSelect(Entity entity) throws DatabaseException;

/**
* Inserts the given entities, returning the primary keys in the same order as they were received.
* Performs a commit unless a transaction is open.
Expand All @@ -162,6 +171,15 @@ public interface EntityConnection extends AutoCloseable {
*/
Collection<Entity.Key> insert(Collection<? extends Entity> entities) throws DatabaseException;

/**
* Inserts the given entities, returning the inserted entities.
* Performs a commit unless a transaction is open.
* @param entities the entities to insert
* @return the inserted entities
* @throws DatabaseException in case of a database exception
*/
Collection<Entity> insertSelect(Collection<? extends Entity> entities) throws DatabaseException;

/**
* Updates the given entity based on its attribute values.
* Throws an exception if the given entity is unmodified.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ public Entity.Key insert(Entity entity) throws DatabaseException {
return insert(singletonList(entity)).iterator().next();
}

@Override
public Entity insertSelect(Entity entity) throws DatabaseException {
return insertSelect(singletonList(entity)).iterator().next();
}

@Override
public Collection<Entity.Key> insert(Collection<? extends Entity> entities) throws DatabaseException {
Objects.requireNonNull(entities);
Expand All @@ -216,6 +221,22 @@ public Collection<Entity.Key> insert(Collection<? extends Entity> entities) thro
}
}

@Override
public Collection<Entity> insertSelect(Collection<? extends Entity> entities) throws DatabaseException {
Objects.requireNonNull(entities);
try {
synchronized (this.entities) {
return onResponse(execute(createHttpPost("insertSelect", byteArrayEntity(entities))));
}
}
catch (DatabaseException e) {
throw e;
}
catch (Exception e) {
throw logAndWrap(e);
}
}

@Override
public void update(Entity entity) throws DatabaseException {
update(singletonList(requireNonNull(entity, "entity")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ public Entity.Key insert(Entity entity) throws DatabaseException {
return insert(singletonList(entity)).iterator().next();
}

@Override
public Entity insertSelect(Entity entity) throws DatabaseException {
return insertSelect(singletonList(entity)).iterator().next();
}

@Override
public Collection<Entity.Key> insert(Collection<? extends Entity> entities) throws DatabaseException {
Objects.requireNonNull(entities);
Expand All @@ -234,6 +239,24 @@ public Collection<Entity.Key> insert(Collection<? extends Entity> entities) thro
}
}

@Override
public Collection<Entity> insertSelect(Collection<? extends Entity> entities) throws DatabaseException {
Objects.requireNonNull(entities);
try {
synchronized (this.entities) {
return onJsonResponse(execute(createHttpPost("insertSelect",
stringEntity(entityObjectMapper.writeValueAsString(entities)))),
entityObjectMapper, EntityObjectMapper.ENTITY_LIST_REFERENCE);
}
}
catch (DatabaseException e) {
throw e;
}
catch (Exception e) {
throw logAndWrap(e);
}
}

@Override
public void update(Entity entity) throws DatabaseException {
update(singletonList(entity));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ public Entity.Key insert(Entity entity) throws DatabaseException {
return insert(singletonList(requireNonNull(entity, "entity"))).iterator().next();
}

@Override
public Entity insertSelect(Entity entity) throws DatabaseException {
return select(insert(entity));
}

@Override
public Collection<Entity.Key> insert(Collection<? extends Entity> entities) throws DatabaseException {
if (requireNonNull(entities, ENTITIES).isEmpty()) {
Expand Down Expand Up @@ -259,6 +264,11 @@ public Collection<Entity.Key> insert(Collection<? extends Entity> entities) thro
}
}

@Override
public Collection<Entity> insertSelect(Collection<? extends Entity> entities) throws DatabaseException {
return select(insert(entities));
}

@Override
public void update(Entity entity) throws DatabaseException {
update(singletonList(requireNonNull(entity, "entity")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ public interface RemoteEntityConnection extends Remote, AutoCloseable {
*/
Entity.Key insert(Entity entity) throws RemoteException, DatabaseException;

/**
* Inserts the given entity, returning the inserted etity.
* Performs a commit unless a transaction is open.
* @param entity the entity to insert
* @return the inserted entity
* @throws DatabaseException in case of a database exception
* @throws RemoteException in case of a remote exception
*/
Entity insertSelect(Entity entity) throws RemoteException, DatabaseException;

/**
* Inserts the given entities, returning the primary keys in the same order as they were received.
* Performs a commit unless a transaction is open.
Expand All @@ -163,6 +173,16 @@ public interface RemoteEntityConnection extends Remote, AutoCloseable {
*/
Collection<Entity.Key> insert(Collection<? extends Entity> entities) throws RemoteException, DatabaseException;

/**
* Inserts the given entities, returning the inserted entities.
* Performs a commit unless a transaction is open.
* @param entities the entities to insert
* @return the inserted entities
* @throws DatabaseException in case of a database exception
* @throws RemoteException in case of a remote exception
*/
Collection<Entity> insertSelect(Collection<? extends Entity> entities) throws RemoteException, DatabaseException;

/**
* Updates the given entity based on its attribute values. Returns the updated entity.
* Throws an exception if the given entity is unmodified.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -737,11 +737,11 @@ public final void removeRefreshListener(Runnable listener) {
/**
* Inserts the given entities into the database
* @param entities the entities to insert
* @return the primary keys of the inserted entities
* @return the inserted entities
* @throws DatabaseException in case of a database exception
*/
protected Collection<Entity.Key> doInsert(Collection<? extends Entity> entities) throws DatabaseException {
return connectionProvider.connection().insert(entities);
protected Collection<Entity> doInsert(Collection<? extends Entity> entities) throws DatabaseException {
return connectionProvider.connection().insertSelect(entities);
}

/**
Expand Down Expand Up @@ -890,7 +890,7 @@ private Collection<Entity> insertEntities(Collection<? extends Entity> entities)
//has not been performed, hence why this logging is performed after validation
LOG.debug("{} - insert {}", this, entities);

return connectionProvider.connection().select(doInsert(entities));
return doInsert(entities);
}

private boolean isSetEntityAllowed() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,27 @@ public Entity.Key insert(Entity entity) throws RemoteException, DatabaseExceptio
}
}

@Override
public Entity insertSelect(Entity entity) throws RemoteException, DatabaseException {
synchronized (connectionProxy) {
return connectionProxy.insertSelect(entity);
}
}

@Override
public Collection<Entity.Key> insert(Collection<? extends Entity> entities) throws DatabaseException {
synchronized (connectionProxy) {
return connectionProxy.insert(entities);
}
}

@Override
public Collection<Entity> insertSelect(Collection<? extends Entity> entities) throws RemoteException, DatabaseException {
synchronized (connectionProxy) {
return connectionProxy.insertSelect(entities);
}
}

@Override
public void update(Entity entity) throws RemoteException, DatabaseException {
synchronized (connectionProxy) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,9 @@ private void setupHandlers() {
javalin.post(URL_JAVA_SERIALIZATION + "select", new SelectHandler());
javalin.post(URL_JSON_SERIALIZATION + "select", new SelectJsonHandler());
javalin.post(URL_JAVA_SERIALIZATION + "insert", new InsertHandler());
javalin.post(URL_JAVA_SERIALIZATION + "insertSelect", new InsertSelectHandler());
javalin.post(URL_JSON_SERIALIZATION + "insert", new InsertJsonHandler());
javalin.post(URL_JSON_SERIALIZATION + "insertSelect", new InsertSelectJsonHandler());
javalin.post(URL_JAVA_SERIALIZATION + "update", new UpdateHandler());
javalin.post(URL_JAVA_SERIALIZATION + "updateSelect", new UpdateSelectHandler());
javalin.post(URL_JSON_SERIALIZATION + "update", new UpdateJsonHandler());
Expand Down Expand Up @@ -673,6 +675,23 @@ public void handle(Context context) {
}
}

private final class InsertSelectHandler implements Handler {

@Override
public void handle(Context context) {
try {
RemoteEntityConnection connection = authenticate(context);
Collection<Entity> entities = connection.insertSelect((Collection<Entity>) deserialize(context.req));
context.status(HttpStatus.OK_200)
.contentType(ContentType.APPLICATION_OCTET_STREAM)
.result(Serializer.serialize(entities));
}
catch (Exception e) {
handleException(context, e);
}
}
}

private final class InsertJsonHandler implements Handler {

@Override
Expand All @@ -692,6 +711,25 @@ public void handle(Context context) {
}
}

private final class InsertSelectJsonHandler implements Handler {

@Override
public void handle(Context context) {
try {
RemoteEntityConnection connection = authenticate(context);
EntityObjectMapper mapper = entityObjectMapper(connection.entities());
Collection<Entity> entities = mapper.deserializeEntities(context.req.getInputStream());
Collection<Entity> inserted = connection.insertSelect(entities);
context.status(HttpStatus.OK_200)
.contentType(ContentType.APPLICATION_JSON)
.result(mapper.writeValueAsString(inserted));
}
catch (Exception e) {
handleException(context, e);
}
}
}

private final class UpdateHandler implements Handler {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,11 @@ void insert() throws Exception {
assertEquals(2, EntityServiceTest.<List<Entity.Key>>deserialize(response.getEntity().getContent()).size());
}
entities.forEach(entity -> entity.put(Department.ID, entity.get(Department.ID) + 1));
post = new HttpPost(createJsonURI("insert"));
post = new HttpPost(createJsonURI("insertSelect"));
post.setEntity(new StringEntity(ENTITY_OBJECT_MAPPER.writeValueAsString(entities)));
try (CloseableHttpResponse response = client.execute(TARGET_HOST, post, context)) {
assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
assertEquals(2, ENTITY_OBJECT_MAPPER.deserializeKeys(response.getEntity().getContent()).size());
assertEquals(2, ENTITY_OBJECT_MAPPER.deserializeEntities(response.getEntity().getContent()).size());
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions readme.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,7 @@ Entity customer = entities.builder(Customer.TYPE)
.with(Customer.LAST_NAME, "Jackson")
.build();
Entity.Key customerKey = connection.insert(customer);
// select to get generated and default column values
customer = connection.select(customerKey);
customer = connection.insertSelect(customer);
Entity address = entities.builder(Address.TYPE)
.with(Address.CUSTOMER_FK, customer)
Expand All @@ -508,7 +506,7 @@ customer.put(Customer.EMAIL, "[email protected]");
customer = connection.updateSelect(customer);
connection.delete(asList(addressKey, customerKey));
connection.delete(asList(addressKey, customer.primaryKey()));
connection.close();
----
Expand Down

0 comments on commit a755d2e

Please sign in to comment.