Search in sources :

Example 66 with Result

use of org.pentaho.di.core.Result in project pentaho-kettle by pentaho.

the class JobEntryColumnsExistTest method jobFail_tableNameIsEmpty.

@Test
public void jobFail_tableNameIsEmpty() throws KettleException {
    jobEntry.setTablename(null);
    final Result result = jobEntry.execute(new Result(), 0);
    assertEquals("Should be error", 1, result.getNrErrors());
    assertFalse("Result should be false", result.getResult());
}
Also used : Result(org.pentaho.di.core.Result) Test(org.junit.Test)

Example 67 with Result

use of org.pentaho.di.core.Result in project pentaho-kettle by pentaho.

the class JobEntryColumnsExistTest method jobFail_columnsArrayIsEmpty.

@Test
public void jobFail_columnsArrayIsEmpty() throws KettleException {
    jobEntry.setArguments(null);
    final Result result = jobEntry.execute(new Result(), 0);
    assertEquals("Should be error", 1, result.getNrErrors());
    assertFalse("Result should be false", result.getResult());
}
Also used : Result(org.pentaho.di.core.Result) Test(org.junit.Test)

Example 68 with Result

use of org.pentaho.di.core.Result in project pentaho-kettle by pentaho.

the class Database method execStatement.

public Result execStatement(String rawsql, RowMetaInterface params, Object[] data) throws KettleDatabaseException {
    Result result = new Result();
    // Replace existing code with a class that removes comments from the raw
    // SQL.
    // The SqlCommentScrubber respects single-quoted strings, so if a
    // double-dash or a multiline comment appears
    // in a single-quoted string, it will be treated as a string instead of
    // comments.
    String sql = databaseMeta.getDatabaseInterface().createSqlScriptParser().removeComments(rawsql).trim();
    try {
        boolean resultSet;
        int count;
        if (params != null) {
            PreparedStatement prep_stmt = connection.prepareStatement(databaseMeta.stripCR(sql));
            // set the parameters!
            setValues(params, data, prep_stmt);
            resultSet = prep_stmt.execute();
            count = prep_stmt.getUpdateCount();
            prep_stmt.close();
        } else {
            String sqlStripped = databaseMeta.stripCR(sql);
            // log.logDetailed("Executing SQL Statement: ["+sqlStripped+"]");
            Statement stmt = connection.createStatement();
            resultSet = stmt.execute(sqlStripped);
            count = stmt.getUpdateCount();
            stmt.close();
        }
        String upperSql = sql.toUpperCase();
        if (!resultSet) {
            // log.logDetailed("What to do with ResultSet??? (count="+count+")");
            if (count > 0) {
                if (upperSql.startsWith("INSERT")) {
                    result.setNrLinesOutput(count);
                } else if (upperSql.startsWith("UPDATE")) {
                    result.setNrLinesUpdated(count);
                } else if (upperSql.startsWith("DELETE")) {
                    result.setNrLinesDeleted(count);
                }
            }
        }
        // See if a cache needs to be cleared...
        if (upperSql.startsWith("ALTER TABLE") || upperSql.startsWith("DROP TABLE") || upperSql.startsWith("CREATE TABLE")) {
            DBCache.getInstance().clear(databaseMeta.getName());
        }
    } catch (SQLException ex) {
        throw new KettleDatabaseException("Couldn't execute SQL: " + sql + Const.CR, ex);
    } catch (Exception e) {
        throw new KettleDatabaseException("Unexpected error executing SQL: " + Const.CR, e);
    }
    return result;
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) CallableStatement(java.sql.CallableStatement) Statement(java.sql.Statement) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) PreparedStatement(java.sql.PreparedStatement) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) KettleExtensionPoint(org.pentaho.di.core.extension.KettleExtensionPoint) Savepoint(java.sql.Savepoint) KettleValueException(org.pentaho.di.core.exception.KettleValueException) BatchUpdateException(java.sql.BatchUpdateException) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) SQLException(java.sql.SQLException) KettleDatabaseBatchException(org.pentaho.di.core.exception.KettleDatabaseBatchException) Result(org.pentaho.di.core.Result)

Example 69 with Result

use of org.pentaho.di.core.Result 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 70 with Result

use of org.pentaho.di.core.Result in project pentaho-kettle by pentaho.

the class SimpleEvalNullFieldIT method testNullField.

@Test
public void testNullField() throws KettleXMLException, IOException, URISyntaxException {
    JobMeta jm = new JobMeta(new File(SimultaneousJobsAppenderIT.class.getClassLoader().getResource(PKG + jobPath).toURI()).getCanonicalPath(), null);
    Job job = new Job(null, jm);
    job.start();
    job.waitUntilFinished();
    Result result = job.getResult();
    Assert.assertTrue(result.getResult());
    if (result.getNrErrors() != 0) {
        Assert.fail(result.getLogText());
    }
}
Also used : File(java.io.File) Result(org.pentaho.di.core.Result) Test(org.junit.Test)

Aggregations

Result (org.pentaho.di.core.Result)192 Test (org.junit.Test)75 KettleException (org.pentaho.di.core.exception.KettleException)75 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)64 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)57 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)40 FileObject (org.apache.commons.vfs2.FileObject)34 Job (org.pentaho.di.job.Job)32 IOException (java.io.IOException)24 ResultFile (org.pentaho.di.core.ResultFile)20 File (java.io.File)17 ArrayList (java.util.ArrayList)16 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)15 KettleExtensionPoint (org.pentaho.di.core.extension.KettleExtensionPoint)12 Pattern (java.util.regex.Pattern)10 KettleFileException (org.pentaho.di.core.exception.KettleFileException)10 Database (org.pentaho.di.core.database.Database)9 Date (java.util.Date)8 Trans (org.pentaho.di.trans.Trans)8 Matcher (java.util.regex.Matcher)7