All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.disl.pattern.ExecuteSQLScriptStep.groovy Maven / Gradle / Ivy

Go to download

disl = data integration specific language. Goal of this project is to implement groovy based domain specific language supporting modelling of data integration projects. Disl will support data modeling and data mapping including support for MDA transformations and unit testing.

There is a newer version: 1.1.2
Show newest version
/*
 * Copyright 2015 Karel H?bl .
 *
 * This file is part of disl.
 *
 * Disl is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Disl 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Disl.  If not, see .
 */
package org.disl.pattern;

import org.disl.meta.Table;

import groovy.sql.Sql

abstract class ExecuteSQLScriptStep extends Step {

	public static final String BACKSLASH_NEW_LINE = "\\\\\n"
	
	boolean ignoreErrors=false;
	String commandSeparator=";";

	abstract Sql getSql();

	public int executeInternal() {
		try {
			
		int processedRows=0
		getCommands().each {
			processedRows=processedRows+executeSqlStatement(it)
		}
		getSql().commit()
		return processedRows
		} catch (Exception e) {
			getSql().rollback()
			throw e
		}
	}
	
	protected Collection getCommands() {
		return code.split(getCommandSeparator())
	}

	protected int executeSqlStatement(String sqlCommand) {
		if (''.equals(sqlCommand.trim())) {
			return 0
		}
		try {
			return executeSqlStatementInternal(sqlCommand)
		} catch (Exception e) {
			if (!isIgnoreErrors()) {
				throw new RuntimeException("Error executing ${this}. SQL statement: $sqlCommand",e)
			}
			return 0
		}
	}

	protected int executeSqlStatementInternal(String sqlCommand) {
		return getSql().executeUpdate(sqlCommand)
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy