Search in sources :

Example 31 with KettleValueException

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;
}
Also used : RowMetaAndData(org.pentaho.di.core.RowMetaAndData) SQLException(java.sql.SQLException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) PreparedStatement(java.sql.PreparedStatement) KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Example 32 with KettleValueException

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;
}
Also used : SQLException(java.sql.SQLException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) ResultSet(java.sql.ResultSet) FileObject(org.apache.commons.vfs2.FileObject) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) KettleValueException(org.pentaho.di.core.exception.KettleValueException) KettleExtensionPoint(org.pentaho.di.core.extension.KettleExtensionPoint) Savepoint(java.sql.Savepoint) Result(org.pentaho.di.core.Result)

Example 33 with KettleValueException

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;
}
Also used : KettleValueException(org.pentaho.di.core.exception.KettleValueException) Date(java.util.Date)

Example 34 with KettleValueException

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;
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) KettleValueException(org.pentaho.di.core.exception.KettleValueException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) KettleEOFException(org.pentaho.di.core.exception.KettleEOFException) IOException(java.io.IOException) EOFException(java.io.EOFException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) NumberFormat(java.text.NumberFormat)

Example 35 with KettleValueException

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;
}
Also used : Calendar(java.util.Calendar) KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Aggregations

KettleValueException (org.pentaho.di.core.exception.KettleValueException)127 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)35 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)35 KettleException (org.pentaho.di.core.exception.KettleException)25 KettleStepException (org.pentaho.di.core.exception.KettleStepException)17 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)16 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)13 IOException (java.io.IOException)12 Test (org.junit.Test)11 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)11 KettleFileException (org.pentaho.di.core.exception.KettleFileException)11 ParseException (java.text.ParseException)10 Date (java.util.Date)9 EOFException (java.io.EOFException)8 SQLException (java.sql.SQLException)8 ArrayList (java.util.ArrayList)8 Calendar (java.util.Calendar)8 KettleEOFException (org.pentaho.di.core.exception.KettleEOFException)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 BigDecimal (java.math.BigDecimal)6