Search in sources :

Example 6 with CsvTransformGeneratorException

use of org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException in project data-access by pentaho.

the class StagingTransformGenerator method executeSql.

protected void executeSql(TableOutputMeta meta, StepMeta stepMeta, TransMeta transMeta) throws CsvTransformGeneratorException {
    try {
        RowMetaInterface prev = transMeta.getPrevStepFields(TABLE_OUTPUT);
        SQLStatement sqlStatement = meta.getSQLStatements(transMeta, stepMeta, prev, null, false, null);
        if (!sqlStatement.hasError()) {
            if (sqlStatement.hasSQL()) {
                // now we can execute the SQL
                String sqlScript = sqlStatement.getSQL();
                execSqlStatement(sqlScript, meta.getDatabaseMeta(), null);
            } else {
                // No SQL was generated
                // $NON-NLS-1$
                error("No SQL generated");
                // $NON-NLS-1$
                throw new CsvTransformGeneratorException("No SQL generated");
            }
        } else {
            error(sqlStatement.getError());
            throw new CsvTransformGeneratorException(sqlStatement.getError());
        }
    } catch (KettleException ke) {
        // $NON-NLS-1$
        error("Exception encountered", ke);
        throw new CsvTransformGeneratorException("Exception encountered", ke, // $NON-NLS-1$
        getStackTraceAsString(ke));
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) CsvTransformGeneratorException(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) SQLStatement(org.pentaho.di.core.SQLStatement)

Example 7 with CsvTransformGeneratorException

use of org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException in project data-access by pentaho.

the class StagingTransformGenerator method createIndices.

public int createIndices(IPentahoSession session) {
    if (transformStats != null) {
        transformStats.setIndexFinished(false);
        transformStats.setIndexStarted(true);
    }
    String tableName = getTableName();
    Database db = new Database(targetDatabaseMeta);
    String[] indexed = getIndexedColumnNames();
    List<String> commands = new ArrayList<String>();
    // TODO base this on the input rows meta for the table output step?
    for (String columnName : indexed) {
        String indexSql = // $NON-NLS-1$
        db.getCreateIndexStatement(// $NON-NLS-1$
        tableName, // $NON-NLS-1$
        columnName + "_idx", new String[] { columnName }, false, false, false, true);
        commands.add(indexSql);
    }
    if (transformStats != null) {
        transformStats.setIndexCount(commands.size());
    }
    int indexDone = 0;
    int indexSuccess = 0;
    for (String command : commands) {
        try {
            execSqlStatement(command, targetDatabaseMeta, null);
            indexSuccess++;
        } catch (CsvTransformGeneratorException e) {
        // failed to execute
        }
        indexDone++;
        if (transformStats != null) {
            transformStats.setIndexDone(indexDone);
        }
    }
    if (transformStats != null) {
        transformStats.setIndexFinished(true);
        transformStats.setIndexStarted(false);
    }
    return indexSuccess;
}
Also used : CsvTransformGeneratorException(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException) Database(org.pentaho.di.core.database.Database) ArrayList(java.util.ArrayList)

Example 8 with CsvTransformGeneratorException

use of org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException in project data-access by pentaho.

the class StagingTransformGenerator method loadTable.

/**
 * Stages the data from a CSV file into a database table. As the table is loading, a {@link TransformStats} monitors
 * the progress. This is placed in the supplied {@link IPentahoSession} to allow interrogation under the attribute key
 * <code>FileTransformStats_<em>fileName</em></code>
 *
 * @param truncate
 * @param session
 * @throws CsvTransformGeneratorException
 */
public void loadTable(boolean truncate, IPentahoSession session, boolean async) throws CsvTransformGeneratorException {
    if (session == null) {
        // $NON-NLS-1$
        throw new IllegalArgumentException("IPentahoSession cannot be null");
    }
    if (tableName == null) {
        // $NON-NLS-1$
        throw new IllegalArgumentException("Table name cannot be null");
    }
    if (transformStats != null) {
        transformStats.setRowsFinished(false);
        transformStats.setRowsStarted(true);
        transformStats.setTotalRecords(0);
        transformStats.setRowsRejected(0);
    }
    Trans trans = createTransform(true);
    // the table output is the last step
    StepMeta[] steps = trans.getTransMeta().getStepsArray();
    StepMeta tableStepMeta = steps[steps.length - 1];
    TableOutputMeta meta = (TableOutputMeta) tableStepMeta.getStepMetaInterface();
    meta.setDatabaseMeta(targetDatabaseMeta);
    meta.setTruncateTable(truncate);
    try {
        prepareTransform(trans, session);
    } catch (Exception e) {
        // $NON-NLS-1$
        error("Preview Failed: transformation preparation", e);
        Throwable e2 = e.getCause();
        e2 = e2 == null ? e : e2;
        throw new CsvTransformGeneratorException("Preview Failed: transformation preparation: loadTable", e2, // $NON-NLS-1$
        getStackTraceAsString(e2));
    }
    StepInterface step = trans.findRunThread(TABLE_OUTPUT);
    PdiTransListener listener = new PdiTransListener(trans, step, transformStats);
    // start the listener in a thread
    Thread listenerThread = new Thread(listener);
    listenerThread.start();
    session.setAttribute(TRANS_SESSION_ATTR, trans);
    if (async) {
        executeTransformAsync(trans);
    } else {
        executeTransformSync(trans, null, session);
    }
}
Also used : StepInterface(org.pentaho.di.trans.step.StepInterface) CsvTransformGeneratorException(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException) TableOutputMeta(org.pentaho.di.trans.steps.tableoutput.TableOutputMeta) Trans(org.pentaho.di.trans.Trans) StepMeta(org.pentaho.di.trans.step.StepMeta) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) CsvTransformGeneratorException(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException)

Example 9 with CsvTransformGeneratorException

use of org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException in project data-access by pentaho.

the class StagingTransformGenerator method createOrModifyTable.

public void createOrModifyTable(IPentahoSession session) throws CsvTransformGeneratorException, IllegalArgumentException {
    if (session == null) {
        // $NON-NLS-1$
        throw new IllegalArgumentException("IPentahoSession cannot be null");
    }
    if (tableName == null) {
        // $NON-NLS-1$
        throw new IllegalArgumentException("Table name cannot be null");
    }
    TransMeta transMeta = createTransMeta(true);
    // the table output is the last step
    StepMeta[] steps = transMeta.getStepsArray();
    StepMeta tableStepMeta = steps[steps.length - 1];
    TableOutputMeta meta = (TableOutputMeta) tableStepMeta.getStepMetaInterface();
    meta.setDatabaseMeta(targetDatabaseMeta);
    try {
        executeSql(meta, tableStepMeta, transMeta);
    } catch (CsvTransformGeneratorException e) {
        if (!e.getMessage().equalsIgnoreCase("No SQL generated")) {
            // $NON-NLS-1$
            error(e.getMessage());
            throw new // $NON-NLS-1$
            CsvTransformGeneratorException(// $NON-NLS-1$
            "Could not create or modify table", // $NON-NLS-1$
            e, getStackTraceAsString(e), null, // $NON-NLS-1$
            Messages.getString("StagingTransformGenerator.ERROR_0001_UNABLE_TO_CREATE_OR_MODIFY_TABLE"));
        }
    }
}
Also used : CsvTransformGeneratorException(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException) TransMeta(org.pentaho.di.trans.TransMeta) DummyTransMeta(org.pentaho.di.trans.steps.dummytrans.DummyTransMeta) TableOutputMeta(org.pentaho.di.trans.steps.tableoutput.TableOutputMeta) StepMeta(org.pentaho.di.trans.step.StepMeta)

Example 10 with CsvTransformGeneratorException

use of org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException in project data-access by pentaho.

the class CsvTransformGeneratorIT method testDropNonExistingTable.

/**
 * Given a name of a non-existing table to drop.
 * <br/>
 * When StagingTransformGenerator is called to drop this table,
 * then it shouldn't execute drop statement.
 */
public void testDropNonExistingTable() throws Exception {
    IPentahoSession session = new StandaloneSession("test");
    KettleSystemListener.environmentInit(session);
    DatabaseMeta dbMeta = getDatabaseMeta();
    ModelInfo info = createModel();
    CsvTransformGenerator gen = new CsvTransformGenerator(info, dbMeta);
    String tableName = info.getStageTableName();
    try {
        gen.execSqlStatement(getDropTableStatement(tableName), dbMeta, null);
    } catch (CsvTransformGeneratorException e) {
    // it is OK if the table doesn't exist previously
    }
    // now make sure we do not execute drop statement for non-existing table
    try {
        gen.dropTable(tableName);
    } catch (CsvTransformGeneratorException e) {
        // no need to forward exception, just fail the test
        fail();
    }
}
Also used : ModelInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) CsvTransformGeneratorException(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta)

Aggregations

CsvTransformGeneratorException (org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException)17 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)9 ModelInfo (org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo)9 StandaloneSession (org.pentaho.platform.engine.core.system.StandaloneSession)8 DatabaseMeta (org.pentaho.di.core.database.DatabaseMeta)4 KettleException (org.pentaho.di.core.exception.KettleException)4 Database (org.pentaho.di.core.database.Database)3 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)3 CsvTransformGenerator (org.pentaho.platform.dataaccess.datasource.wizard.service.agile.CsvTransformGenerator)3 XStream (com.thoughtworks.xstream.XStream)2 ArrayList (java.util.ArrayList)2 Trans (org.pentaho.di.trans.Trans)2 StepMeta (org.pentaho.di.trans.step.StepMeta)2 TableOutputMeta (org.pentaho.di.trans.steps.tableoutput.TableOutputMeta)2 Domain (org.pentaho.metadata.model.Domain)2 LogicalModel (org.pentaho.metadata.model.LogicalModel)2 DatasourceDTO (org.pentaho.platform.dataaccess.datasource.wizard.models.DatasourceDTO)2 DatasourceServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1