use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.
the class BaseDatabaseMeta method getNextBatchIdUsingAutoIncSQL.
public Long getNextBatchIdUsingAutoIncSQL(String autoIncSQL, DatabaseMeta dbm, Database ldb) throws KettleDatabaseException {
Long rtn = null;
PreparedStatement stmt = ldb.prepareSQL(autoIncSQL, true);
try {
stmt.executeUpdate();
RowMetaAndData rmad = ldb.getGeneratedKeys(stmt);
if (rmad.getRowMeta().size() > 0) {
rtn = rmad.getRowMeta().getInteger(rmad.getData(), 0);
} else {
throw new KettleDatabaseException("Unable to retrieve value of auto-generated technical key : " + "no value found!");
}
} catch (KettleValueException kve) {
throw new KettleDatabaseException(kve);
} catch (SQLException sqlex) {
throw new KettleDatabaseException(sqlex);
} finally {
try {
stmt.close();
} catch (SQLException ignored) {
// Ignored
}
}
return rtn;
}
use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.
the class Database method execStatements.
/**
* Execute a series of SQL statements, separated by ;
* <p/>
* We are already connected...
* <p/>
* Multiple statements have to be split into parts We use the ";" to separate statements...
* <p/>
* We keep the results in Result object from Jobs
*
* @param script The SQL script to be execute
* @param params Parameters Meta
* @param data Parameters value
* @return A result with counts of the number or records updates, inserted, deleted or read.
* @throws KettleDatabaseException In case an error occurs
*/
public Result execStatements(String script, RowMetaInterface params, Object[] data) throws KettleDatabaseException {
Result result = new Result();
SqlScriptParser sqlScriptParser = databaseMeta.getDatabaseInterface().createSqlScriptParser();
List<String> statements = sqlScriptParser.split(script);
int nrstats = 0;
if (statements != null) {
for (String stat : statements) {
// Deleting all the single-line and multi-line comments from the string
stat = sqlScriptParser.removeComments(stat);
if (!Const.onlySpaces(stat)) {
String sql = Const.trim(stat);
if (sql.toUpperCase().startsWith("SELECT")) {
// A Query
if (log.isDetailed()) {
log.logDetailed("launch SELECT statement: " + Const.CR + sql);
}
nrstats++;
ResultSet rs = null;
try {
rs = openQuery(sql, params, data);
if (rs != null) {
Object[] row = getRow(rs);
while (row != null) {
result.setNrLinesRead(result.getNrLinesRead() + 1);
if (log.isDetailed()) {
log.logDetailed(rowMeta.getString(row));
}
row = getRow(rs);
}
} else {
if (log.isDebug()) {
log.logDebug("Error executing query: " + Const.CR + sql);
}
}
} catch (KettleValueException e) {
// just pass the error
throw new KettleDatabaseException(e);
// upwards.
} finally {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException ex) {
if (log.isDebug()) {
log.logDebug("Error closing query: " + Const.CR + sql);
}
}
}
} else {
// any kind of statement
if (log.isDetailed()) {
log.logDetailed("launch DDL statement: " + Const.CR + sql);
}
// A DDL statement
nrstats++;
Result res = execStatement(sql, params, data);
result.add(res);
}
}
}
}
if (log.isDetailed()) {
log.logDetailed(nrstats + " statement" + (nrstats == 1 ? "" : "s") + " executed");
}
return result;
}
use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.
the class Value method num2dat.
// implement the TO_DATE function, arguments in args[]
public Value num2dat() throws KettleValueException {
if (isNull()) {
setType(VALUE_TYPE_DATE);
} else {
if (isNumeric()) {
setValue(new Date(getInteger()));
setLength(-1, -1);
} else {
throw new KettleValueException("Function NUM2DAT only works on a number");
}
}
return this;
}
use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.
the class Value method num2str.
public Value num2str(String format, String decimalSymbol, String groupingSymbol, String currencySymbol) throws KettleValueException {
if (isNull()) {
setType(VALUE_TYPE_STRING);
} else {
// Number to String conversion...
if (getType() == VALUE_TYPE_NUMBER || getType() == VALUE_TYPE_INTEGER) {
NumberFormat nf = NumberFormat.getInstance();
DecimalFormat df = (DecimalFormat) nf;
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
if (currencySymbol != null && currencySymbol.length() > 0) {
dfs.setCurrencySymbol(currencySymbol);
}
if (groupingSymbol != null && groupingSymbol.length() > 0) {
dfs.setGroupingSeparator(groupingSymbol.charAt(0));
}
if (decimalSymbol != null && decimalSymbol.length() > 0) {
dfs.setDecimalSeparator(decimalSymbol.charAt(0));
}
// in case of 4, 3 or 2
df.setDecimalFormatSymbols(dfs);
if (format != null && format.length() > 0) {
df.applyPattern(format);
}
try {
setValue(nf.format(getNumber()));
} catch (Exception e) {
setType(VALUE_TYPE_STRING);
setNull();
throw new KettleValueException("Couldn't convert Number to String " + e.toString());
}
} else {
throw new KettleValueException("Function NUM2STR only works on Numbers and Integers");
}
}
return this;
}
use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.
the class Value method add_days.
/**
* Add a number of days to a Date value.
*
* @param days
* The number of days to add to the current date value
* @return The resulting value
* @throws KettleValueException
*/
public Value add_days(long days) throws KettleValueException {
if (getType() == VALUE_TYPE_DATE) {
if (!isNull() && getDate() != null) {
Calendar cal = Calendar.getInstance();
cal.setTime(getDate());
cal.add(Calendar.DAY_OF_YEAR, (int) days);
setValue(cal.getTime());
}
} else {
throw new KettleValueException("Function add_days only works on a date!");
}
return this;
}
Aggregations