org.jumpmind.symmetric.db.SqlScript Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of symmetric-ds Show documentation
Show all versions of symmetric-ds Show documentation
SymmetricDS is an open source database synchronization solution. It is platform-independent,
web-enabled, and database-agnostic. SymmetricDS was first built to replicate changes between 'retail store'
databases and ad centralized 'corporate' database.
The newest version!
/*
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU Lesser General Public License (the
* "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see
* .
*
* 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.jumpmind.symmetric.db;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URL;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.apache.commons.io.IOUtils;
import org.jumpmind.symmetric.common.logging.ILog;
import org.jumpmind.symmetric.common.logging.LogFactory;
import org.jumpmind.symmetric.util.AppUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* This class is for running SQL scripts against a DataSource.
*
*
*/
public class SqlScript {
static final String COMMENT_CHARS_1 = "--";
static final String COMMENT_CHARS_2 = "#";
static final ILog log = LogFactory.getLog(SqlScript.class);
public final static String QUERY_ENDS = ";";
private String delimiter = QUERY_ENDS;
private List script;
private DataSource dataSource;
private int commitRate = 10000;
private boolean failOnError = true;
private Map replacementTokens;
private String fileName = "memory";
public SqlScript(URL url, DataSource ds) {
this(url, ds, true, QUERY_ENDS, null);
}
public SqlScript(URL url, DataSource ds, boolean failOnError) {
this(url, ds, failOnError, QUERY_ENDS, null);
}
public SqlScript(URL url, DataSource ds, String delimiter) {
this(url, ds, true, delimiter, null);
}
@SuppressWarnings("unchecked")
public SqlScript(URL url, DataSource ds, boolean failOnError,
String delimiter, Map replacementTokens) {
try {
fileName = url.getFile();
fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
log.info("ScriptLoading", fileName);
init(IOUtils.readLines(new InputStreamReader(url.openStream(),
"UTF-8")), ds, failOnError, delimiter, replacementTokens);
} catch (IOException ex) {
log.error(ex);
throw new RuntimeException(ex);
}
}
@SuppressWarnings("unchecked")
public SqlScript(String sqlScript, DataSource ds, boolean failOnError,
String delimiter, Map replacementTokens) {
try {
init(IOUtils.readLines(new StringReader(sqlScript)), ds,
failOnError, delimiter, replacementTokens);
} catch (IOException ex) {
log.error(ex);
throw new RuntimeException(ex);
}
}
private void init(List sqlScript, DataSource ds,
boolean failOnError, String delimiter,
Map replacementTokens) {
this.script = sqlScript;
this.dataSource = ds;
this.failOnError = failOnError;
this.delimiter = delimiter;
this.replacementTokens = replacementTokens;
}
private void closeQuietly(Statement stmt) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
log.error(e);
}
}
}
public void execute() {
execute(false);
}
public void execute(final boolean autoCommit) {
JdbcTemplate template = new JdbcTemplate(this.dataSource);
template.execute(new ConnectionCallback