Skip to content

Commit

Permalink
Move bindings for TableLoader and SqlScriptExecutorProvider to the gu…
Browse files Browse the repository at this point in the history
…ice support module.
  • Loading branch information
badgerwithagun committed Nov 22, 2017
1 parent 85cd1b2 commit 68bb774
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.sql.Connection;
import java.sql.PreparedStatement;

import org.alfasoftware.morf.dataset.TableLoaderBuilder.TableLoaderBuilderImpl;
import org.alfasoftware.morf.jdbc.RuntimeSqlException;
import org.alfasoftware.morf.jdbc.SqlDialect;
import org.alfasoftware.morf.jdbc.SqlScriptExecutor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,10 @@

import java.sql.Connection;

import org.alfasoftware.morf.dataset.TableLoaderBuilder.TableLoaderBuilderImpl;
import org.alfasoftware.morf.jdbc.SqlDialect;
import org.alfasoftware.morf.jdbc.SqlScriptExecutor;
import org.alfasoftware.morf.jdbc.SqlScriptExecutorProvider;
import org.alfasoftware.morf.metadata.Table;

import com.google.inject.ImplementedBy;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.util.Providers;

/**
* Builds a {@link TableLoader}.
*
Expand All @@ -39,7 +32,6 @@
*
* @author Copyright (c) Alfa Financial Software 2014
*/
@ImplementedBy(TableLoaderBuilderImpl.class)
public interface TableLoaderBuilder {

/**
Expand Down Expand Up @@ -128,109 +120,4 @@ public interface TableLoaderBuilder {
* @return A table loader.
*/
TableLoader forTable(Table table);

/**
* Internal implementation of {@link TableLoaderBuilder}.
*/
class TableLoaderBuilderImpl implements TableLoaderBuilder {
private Connection connection;
private final SqlScriptExecutorProvider sqlScriptExecutorProvider;
private SqlScriptExecutor sqlScriptExecutor;
private boolean explicitCommit;
private boolean truncateBeforeLoad;
private boolean insertingWithPresetAutonums;
private boolean insertingUnderAutonumLimit;
private Provider<SqlDialect> sqlDialect;
private int batchSize = 1000;

TableLoaderBuilderImpl() {
super();
// This will need to be provided in withSqlScriptExecutor
sqlScriptExecutorProvider = null;
}

@Inject
TableLoaderBuilderImpl(
SqlScriptExecutorProvider sqlScriptExecutorProvider,
Provider<SqlDialect> sqlDialect) {
super();
this.sqlScriptExecutorProvider = sqlScriptExecutorProvider;
this.sqlDialect = sqlDialect;
}

@Override
public TableLoaderBuilder withDialect(SqlDialect sqlDialect) {
this.sqlDialect = Providers.of(sqlDialect);
return this;
}

@Override
public TableLoaderBuilder withConnection(Connection connection) {
this.connection = connection;
return this;
}

@Override
public TableLoaderBuilder withSqlScriptExecutor(SqlScriptExecutor sqlScriptExecutor) {
this.sqlScriptExecutor = sqlScriptExecutor;
return this;
}

@Override
public TableLoaderBuilder explicitCommit() {
this.explicitCommit = true;
return this;
}

@Override
public TableLoaderBuilder explicitCommit(boolean explicitCommit) {
this.explicitCommit = explicitCommit;
return this;
}

@Override
public TableLoaderBuilder truncateBeforeLoad() {
this.truncateBeforeLoad = true;
return this;
}

@Override
public TableLoaderBuilder insertingWithPresetAutonums() {
this.insertingWithPresetAutonums = true;
return this;
}

@Override
public TableLoaderBuilder insertingUnderAutonumLimit() {
this.insertingUnderAutonumLimit = true;
return this;
}

@Override
public TableLoaderBuilder withBatchSize(int recordsPerBatch) {
this.batchSize = recordsPerBatch;
return this;
}

@Override
public TableLoader forTable(Table table) {
SqlScriptExecutor executor = sqlScriptExecutor;
if (executor == null) {
if (sqlScriptExecutorProvider == null) {
throw new IllegalArgumentException("No SqlScriptExecutor provided");
}
executor = sqlScriptExecutorProvider.get();
}
return new TableLoader(
connection,
executor,
sqlDialect.get(),
explicitCommit,
table,
insertingWithPresetAutonums,
insertingUnderAutonumLimit,
truncateBeforeLoad,
batchSize);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/* Copyright 2017 Alfa Financial Software
*
* 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 org.alfasoftware.morf.dataset;

import java.sql.Connection;

import org.alfasoftware.morf.jdbc.SqlDialect;
import org.alfasoftware.morf.jdbc.SqlScriptExecutor;
import org.alfasoftware.morf.jdbc.SqlScriptExecutorProvider;
import org.alfasoftware.morf.metadata.Table;

import com.google.inject.Provider;
import com.google.inject.util.Providers;

/**
* Internal implementation of {@link TableLoaderBuilder}.
*/
class TableLoaderBuilderImpl implements TableLoaderBuilder {
private Connection connection;
private final SqlScriptExecutorProvider sqlScriptExecutorProvider;
private SqlScriptExecutor sqlScriptExecutor;
private boolean explicitCommit;
private boolean truncateBeforeLoad;
private boolean insertingWithPresetAutonums;
private boolean insertingUnderAutonumLimit;
private Provider<SqlDialect> sqlDialect;
private int batchSize = 1000;

TableLoaderBuilderImpl() {
super();
// This will need to be provided in withSqlScriptExecutor
sqlScriptExecutorProvider = null;
}

@Override
public TableLoaderBuilder withDialect(SqlDialect sqlDialect) {
this.sqlDialect = Providers.of(sqlDialect);
return this;
}

@Override
public TableLoaderBuilder withConnection(Connection connection) {
this.connection = connection;
return this;
}

@Override
public TableLoaderBuilder withSqlScriptExecutor(SqlScriptExecutor sqlScriptExecutor) {
this.sqlScriptExecutor = sqlScriptExecutor;
return this;
}

@Override
public TableLoaderBuilder explicitCommit() {
this.explicitCommit = true;
return this;
}

@Override
public TableLoaderBuilder explicitCommit(boolean explicitCommit) {
this.explicitCommit = explicitCommit;
return this;
}

@Override
public TableLoaderBuilder truncateBeforeLoad() {
this.truncateBeforeLoad = true;
return this;
}

@Override
public TableLoaderBuilder insertingWithPresetAutonums() {
this.insertingWithPresetAutonums = true;
return this;
}

@Override
public TableLoaderBuilder insertingUnderAutonumLimit() {
this.insertingUnderAutonumLimit = true;
return this;
}

@Override
public TableLoaderBuilder withBatchSize(int recordsPerBatch) {
this.batchSize = recordsPerBatch;
return this;
}

@Override
public TableLoader forTable(Table table) {
SqlScriptExecutor executor = sqlScriptExecutor;
if (executor == null) {
if (sqlScriptExecutorProvider == null) {
throw new IllegalArgumentException("No SqlScriptExecutor provided");
}
executor = sqlScriptExecutorProvider.get();
}
return new TableLoader(
connection,
executor,
sqlDialect.get(),
explicitCommit,
table,
insertingWithPresetAutonums,
insertingUnderAutonumLimit,
truncateBeforeLoad,
batchSize);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import org.alfasoftware.morf.jdbc.SqlScriptExecutor.SqlScriptVisitor;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.util.Providers;

Expand All @@ -40,7 +39,6 @@ public class SqlScriptExecutorProvider implements Provider<SqlScriptExecutor> {
* {@link SqlScriptExecutorProvider} for
* @param sqlDialect The dialect to use
*/
@Inject
public SqlScriptExecutorProvider(final DataSource dataSource, Provider<SqlDialect> sqlDialect) {
super();
this.dataSource = dataSource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@
*/
package org.alfasoftware.morf.guicesupport;

import javax.sql.DataSource;

import org.alfasoftware.morf.dataset.TableLoader;
import org.alfasoftware.morf.dataset.TableLoaderBuilder;
import org.alfasoftware.morf.jdbc.SqlDialect;
import org.alfasoftware.morf.jdbc.SqlScriptExecutorProvider;
import org.alfasoftware.morf.upgrade.TableContribution;
import org.alfasoftware.morf.upgrade.additions.UpgradeScriptAddition;
import org.alfasoftware.morf.upgrade.db.DatabaseUpgradeTableContribution;

import com.google.common.base.Preconditions;
import com.google.inject.AbstractModule;
import com.google.inject.Inject;
import com.google.inject.Provides;
import com.google.inject.multibindings.Multibinder;

/**
Expand All @@ -37,5 +46,43 @@ protected void configure() {
Multibinder<TableContribution> tableMultibinder = Multibinder.newSetBinder(binder(), TableContribution.class);
tableMultibinder.addBinding().to(DatabaseUpgradeTableContribution.class);
}
}


/**
* Provides a {@link SqlScriptExecutorProvider}.
*
* @param param The optional bindings.
* @return The {@link SqlScriptExecutorProvider}.
*/
@Provides
public SqlScriptExecutorProvider sqlScriptExecutorProvider(SqlScriptExecutorProviderParam param) {
Preconditions.checkNotNull(param.sqlDialect, "Dialect is null");
Preconditions.checkNotNull(param.dataSource, "Data source is null");
return new SqlScriptExecutorProvider(param.dataSource, param.sqlDialect);
}

private static final class SqlScriptExecutorProviderParam {
@Inject(optional = true) SqlDialect sqlDialect;
@Inject(optional = true) DataSource dataSource;
}


/**
* Provides a {@link TableLoaderBuilder}.
*
* @param param The optional bindings.
* @param sqlScriptExecutorProvider The SQL script executor.
* @return The {@link TableLoaderBuilder}.
*/
@Provides()
public TableLoaderBuilder tableLoaderBuilder(TableLoaderBuilderParam param, SqlScriptExecutorProvider sqlScriptExecutorProvider) {
Preconditions.checkNotNull(param.sqlDialect, "Dialect is null");
return TableLoader.builder()
.withDialect(param.sqlDialect)
.withSqlScriptExecutor(sqlScriptExecutorProvider.get());
}

private static final class TableLoaderBuilderParam {
@Inject(optional = true) SqlDialect sqlDialect;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import javax.sql.DataSource;

import com.google.inject.Binder;
import org.alfasoftware.morf.guicesupport.MorfModule;
import org.alfasoftware.morf.jdbc.ConnectionResources;
import org.alfasoftware.morf.jdbc.ConnectionResourcesBean;
import org.alfasoftware.morf.jdbc.SqlDialect;
Expand All @@ -26,6 +26,7 @@

import com.google.common.io.Resources;
import com.google.inject.AbstractModule;
import com.google.inject.Binder;
import com.google.inject.Provides;

/**
Expand All @@ -45,6 +46,7 @@ public class TestingDataSourceModule extends AbstractModule {
*/
@Override
public void configure() {
install(new MorfModule());
bind(ConnectionResources.class).toInstance(new ConnectionResourcesBean(Resources.getResource("morf.properties")));
}

Expand Down

0 comments on commit 68bb774

Please sign in to comment.