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));
}
}
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;
}
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);
}
}
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"));
}
}
}
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();
}
}
Aggregations