Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Tentackle - https://tentackle.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.tentackle.sql;
import org.tentackle.common.StringHelper;
import org.tentackle.sql.backends.AbstractBackend;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
/**
* Default implementation of a script runner.
*/
public class DefaultScriptRunner implements ScriptRunner {
private final Backend backend;
private final Connection connection;
private final boolean posixEscapeSyntaxSupported;
private boolean escapeProcessing = true; // as per JDBC specs enabled by default
/**
* The next SQL code to execute as a statement.
*
* @param sql the SQL statement to execute
* @param nextOffset the offset where to start searching for the next statement within the script
*/
public record SQLCode(String sql, int nextOffset) {
public SQLCode {
// remove leading whitespaces
sql = sql.stripLeading();
}
}
/**
* Creates a script runner.
*
* @param backend the backend
* @param connection the SQL connection
* @see AbstractBackend#createScriptRunner(Connection)
*/
public DefaultScriptRunner(Backend backend, Connection connection) {
this.backend = backend;
this.connection = connection;
this.posixEscapeSyntaxSupported = determinePosixEscapeSyntaxSupported();
}
@Override
public Backend getBackend() {
return backend;
}
@Override
public Connection getConnection() {
return connection;
}
@Override
public void setEscapeProcessingEnabled(boolean enabled) {
escapeProcessing = enabled;
}
@Override
public boolean isEscapeProcessingEnabled() {
return escapeProcessing;
}
@Override
public List run(String script) {
SQLCode sqlCode = null;
int offset = 0;
try (Statement statement = connection.createStatement()) {
statement.setEscapeProcessing(escapeProcessing);
List scriptRunnerResults = new ArrayList<>();
while ((sqlCode = determineNextSqlCode(script, offset)) != null) {
if (!sqlCode.sql.isBlank()) {
List