Skip to content

Commit

Permalink
Revert "Changed responsibility for creating CrudHandler to Manager"
Browse files Browse the repository at this point in the history
This reverts commit 5bcdda1.
  • Loading branch information
Emil Forslund committed Oct 7, 2015
1 parent 28cdfcb commit ca023d0
Show file tree
Hide file tree
Showing 7 changed files with 350 additions and 91 deletions.
173 changes: 173 additions & 0 deletions src/main/java/com/speedment/component/CrudHandlerComponent.java
@@ -0,0 +1,173 @@
/**
*
* Copyright (c) 2006-2015, Speedment, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); You may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.speedment.component;

import com.speedment.annotation.Api;
import com.speedment.db.crud.Create;
import com.speedment.db.crud.Delete;
import com.speedment.db.crud.Read;
import com.speedment.db.crud.Result;
import com.speedment.db.crud.Update;
import com.speedment.exception.SpeedmentException;
import java.util.function.Function;
import java.util.stream.Stream;

/**
* This component is used to execute CRUD operations generated by Speedment.
* For each type of CRUD operation, there is a specific executor that will
* consume all events of that type. By setting the executor, you can control
* how Speedment stores and reads data.
* <p>
* The executor should be set by the persistance manager (like the database
* manager) before any operations are executed.
*
* @author Emil Forslund
* @since 2.2
*/
@Api(version = "2.2")
public interface CrudHandlerComponent extends Component {

/**
* Functional interface for something that can execute a {@link Create}
* operation and map the results into T using the specified mapper.
*/
@FunctionalInterface
interface CreateMethod {
<T> T apply(Create create, Function<Result, T> mapper) throws SpeedmentException;
}

/**
* Functional interface for something that can execute an {@link Update}
* operation and map the results into T using the specified mapper.
*/
@FunctionalInterface
interface UpdateMethod {
<T> T apply(Update update, Function<Result, T> mapper) throws SpeedmentException;
}

/**
* Functional interface for something that can execute a {@link Read}
* operation and map the results into T using the specified mapper.
*/
@FunctionalInterface
interface ReadMethod {
<T> Stream<T> apply(Read read, Function<Result, T> mapper) throws SpeedmentException;
}

/**
* Functional interface for something that can execute a {@link Delete}
* operation.
*/
@FunctionalInterface
interface DeleteMethod {
void apply(Delete delete) throws SpeedmentException;
}

/**
* Sets the method to use when
* {@link #create(com.speedment.db.crud.Create, java.util.function.Function)}
* is called.
*
* @param creator to use
* @return a reference to this
*/
CrudHandlerComponent setCreator(CreateMethod creator);

/**
* Sets the method to use when
* {@link #update(com.speedment.db.crud.Update, java.util.function.Function)}
* is called.
*
* @param updater to use
* @return a reference to this
*/
CrudHandlerComponent setUpdater(UpdateMethod updater);

/**
* Sets the method to use when
* {@link #delete(com.speedment.db.crud.Delete)}
* is called.
*
* @param deleter to use
* @return a reference to this
*/
CrudHandlerComponent setDeleter(DeleteMethod deleter);

/**
* Sets the method to use when
* {@link #read(com.speedment.db.crud.Read, java.util.function.Function)}
* is called.
*
* @param reader to use
* @return a reference to this
*/
CrudHandlerComponent setReader(ReadMethod reader);

/**
* Executes the specified {@link Create} operation in the database, mapping
* the result using the specified mapper.
*
* @param <T> the expected return type
* @param operation the operation to perform
* @param mapper for the result
* @return the mapped result
* @throws SpeedmentException if the operation failed
*/
<T> T create(final Create operation, final Function<Result, T> mapper) throws SpeedmentException;

/**
* Executes the specified {@link Update} operation in the database, mapping
* the result using the specified mapper.
*
* @param <T> the expected return type
* @param operation the operation to perform
* @param mapper for the result
* @return the mapped result
* @throws SpeedmentException if the operation failed
*/
<T> T update(final Update operation, final Function<Result, T> mapper) throws SpeedmentException;

/**
* Executes the specified {@link Delete} operation in the database, mapping
* the result using the specified mapper.
*
* @param <T> the expected return type
* @param operation the operation to perform
* @throws SpeedmentException if the operation failed
*/
<T> void delete(final Delete operation) throws SpeedmentException;

/**
* Executes the specified {@link Read} operation in the database, mapping
* the result using the specified mapper.
*
* @param <T> the expected return type
* @param operation the operation to perform
* @param mapper for the result
* @return the mapped result
* @throws SpeedmentException if the operation failed
*/
<T> Stream<T> read(final Read operation, final Function<Result, T> mapper) throws SpeedmentException;

/**
* {@inheritDoc}
*/
@Override
default Class<CrudHandlerComponent> getComponentClass() {
return CrudHandlerComponent.class;
}
}
58 changes: 1 addition & 57 deletions src/main/java/com/speedment/db/CrudHandler.java
Expand Up @@ -17,15 +17,6 @@
package com.speedment.db; package com.speedment.db;


import com.speedment.annotation.Api; import com.speedment.annotation.Api;
import com.speedment.config.Dbms;
import com.speedment.db.crud.Create;
import com.speedment.db.crud.Delete;
import com.speedment.db.crud.Read;
import com.speedment.db.crud.Result;
import com.speedment.db.crud.Update;
import com.speedment.exception.SpeedmentException;
import java.util.function.Function;
import java.util.stream.Stream;


/** /**
* *
Expand All @@ -34,52 +25,5 @@
*/ */
@Api(version = "2.2") @Api(version = "2.2")
public interface CrudHandler { public interface CrudHandler {

void setAsPrimary();
Dbms getDbms();

/**
* Executes the specified {@link Create} operation in the database, mapping
* the result using the specified mapper.
*
* @param <T> the expected return type
* @param operation the operation to perform
* @param mapper for the result
* @return the mapped result
* @throws SpeedmentException if the operation failed
*/
<T> T create(final Create operation, final Function<Result, T> mapper) throws SpeedmentException;

/**
* Executes the specified {@link Update} operation in the database, mapping
* the result using the specified mapper.
*
* @param <T> the expected return type
* @param operation the operation to perform
* @param mapper for the result
* @return the mapped result
* @throws SpeedmentException if the operation failed
*/
<T> T update(final Update operation, final Function<Result, T> mapper) throws SpeedmentException;

/**
* Executes the specified {@link Delete} operation in the database, mapping
* the result using the specified mapper.
*
* @param <T> the expected return type
* @param operation the operation to perform
* @throws SpeedmentException if the operation failed
*/
<T> void delete(final Delete operation) throws SpeedmentException;

/**
* Executes the specified {@link Read} operation in the database, mapping
* the result using the specified mapper.
*
* @param <T> the expected return type
* @param operation the operation to perform
* @param mapper for the result
* @return the mapped result
* @throws SpeedmentException if the operation failed
*/
<T> Stream<T> read(final Read operation, final Function<Result, T> mapper) throws SpeedmentException;
} }
25 changes: 14 additions & 11 deletions src/main/java/com/speedment/internal/core/db/MySqlCrudHandler.java
Expand Up @@ -41,6 +41,7 @@
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Stream; import java.util.stream.Stream;


import static java.util.Objects.requireNonNull;
import java.util.Spliterators; import java.util.Spliterators;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;


Expand All @@ -52,23 +53,25 @@
public final class MySqlCrudHandler extends AbstractCrudHandler { public final class MySqlCrudHandler extends AbstractCrudHandler {


private static final Logger LOGGER = LoggerManager.getLogger(MySqlCrudHandler.class); private static final Logger LOGGER = LoggerManager.getLogger(MySqlCrudHandler.class);
private final Dbms dbms;


public MySqlCrudHandler(Speedment speedment, Dbms dbms) { public MySqlCrudHandler(Speedment speedment, Dbms dbms) {
super(speedment, dbms); super(speedment);
this.dbms = requireNonNull(dbms);
} }


@Override @Override
public <T> T create(Create create, Function<Result, T> mapper) throws SpeedmentException { protected <T> T create(Create create, Function<Result, T> mapper) throws SpeedmentException {
return executeUpdate(create, mapper); return executeUpdate(create, mapper);
} }


@Override @Override
public <T> T update(Update update, Function<Result, T> mapper) throws SpeedmentException { protected <T> T update(Update update, Function<Result, T> mapper) throws SpeedmentException {
return executeUpdate(update, mapper); return executeUpdate(update, mapper);
} }


@Override @Override
public void delete(Delete delete) throws SpeedmentException { protected void delete(Delete delete) throws SpeedmentException {
final PreparedStatement statement = SqlWriter.prepare(getConnection(), delete); final PreparedStatement statement = SqlWriter.prepare(getConnection(), delete);


try { try {
Expand All @@ -81,7 +84,7 @@ public void delete(Delete delete) throws SpeedmentException {
} }


@Override @Override
public <T> Stream<T> read(Read read, Function<Result, T> mapper) throws SpeedmentException { protected <T> Stream<T> read(Read read, Function<Result, T> mapper) throws SpeedmentException {
final PreparedStatement statement = SqlWriter.prepare(getConnection(), read); final PreparedStatement statement = SqlWriter.prepare(getConnection(), read);
try (final ResultSet set = statement.executeQuery()) { try (final ResultSet set = statement.executeQuery()) {
return StreamSupport.stream( return StreamSupport.stream(
Expand Down Expand Up @@ -139,15 +142,15 @@ private Connection getConnection() {
final Connection conn; final Connection conn;


final String url = getUrl(); final String url = getUrl();
final String user = getDbms().getUsername().orElse(null); final String user = dbms.getUsername().orElse(null);
final String password = getDbms().getPassword().orElse(null); final String password = dbms.getPassword().orElse(null);


try { try {
conn = speedment().get(ConnectionPoolComponent.class) conn = speedment().get(ConnectionPoolComponent.class)
.getConnection(url, user, password); .getConnection(url, user, password);


} catch (SQLException sqle) { } catch (SQLException sqle) {
final String msg = "Unable to get connection for " + getDbms() + " using url \"" + url + "\", user = " + user + "."; final String msg = "Unable to get connection for " + dbms + " using url \"" + url + "\", user = " + user + ".";
LOGGER.error(sqle, msg); LOGGER.error(sqle, msg);
throw new SpeedmentException(msg, sqle); throw new SpeedmentException(msg, sqle);
} }
Expand All @@ -156,15 +159,15 @@ private Connection getConnection() {
} }


private String getUrl() { private String getUrl() {
final DbmsType dbmsType = getDbms().getType(); final DbmsType dbmsType = dbms.getType();
final StringBuilder result = new StringBuilder(); final StringBuilder result = new StringBuilder();


result.append("jdbc:"); result.append("jdbc:");
result.append(dbmsType.getJdbcConnectorName()); result.append(dbmsType.getJdbcConnectorName());
result.append("://"); result.append("://");


getDbms().getIpAddress().ifPresent(ip -> result.append(ip)); dbms.getIpAddress().ifPresent(ip -> result.append(ip));
getDbms().getPort().ifPresent(port -> result.append(":").append(port)); dbms.getPort().ifPresent(port -> result.append(":").append(port));


result.append("/"); result.append("/");


Expand Down
Expand Up @@ -17,11 +17,10 @@
package com.speedment.internal.core.manager; package com.speedment.internal.core.manager;


import com.speedment.Speedment; import com.speedment.Speedment;
import com.speedment.component.CrudHandlerComponent;
import com.speedment.config.Column; import com.speedment.config.Column;
import com.speedment.config.Dbms;
import com.speedment.config.PrimaryKeyColumn; import com.speedment.config.PrimaryKeyColumn;
import com.speedment.config.Table; import com.speedment.config.Table;
import com.speedment.db.CrudHandler;
import com.speedment.db.MetaResult; import com.speedment.db.MetaResult;
import com.speedment.db.crud.Result; import com.speedment.db.crud.Result;
import com.speedment.db.crud.Selector; import com.speedment.db.crud.Selector;
Expand All @@ -47,7 +46,7 @@
*/ */
public abstract class AbstractCrudManager<ENTITY> extends AbstractManager<ENTITY> { public abstract class AbstractCrudManager<ENTITY> extends AbstractManager<ENTITY> {


private final CrudHandler handler; private final CrudHandlerComponent handler;
private final Column primaryKeyColumn; private final Column primaryKeyColumn;


/** /**
Expand All @@ -58,12 +57,7 @@ public abstract class AbstractCrudManager<ENTITY> extends AbstractManager<ENTITY
*/ */
protected AbstractCrudManager(Speedment speedment) { protected AbstractCrudManager(Speedment speedment) {
super(speedment); super(speedment);

this.handler = speedment.get(CrudHandlerComponent.class);
final Dbms dbms = getTable().ancestor(Dbms.class).orElseThrow(
() -> new SpeedmentException("Can not instantiate an entity manager without a Dbms.")
);

this.handler = dbms.getType().makeCrudHandler(speedment, dbms);
this.primaryKeyColumn = findColumnOfPrimaryKey(getTable()); this.primaryKeyColumn = findColumnOfPrimaryKey(getTable());
} }


Expand Down

0 comments on commit ca023d0

Please sign in to comment.