Search in sources :

Example 31 with SQLStatement

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

the class MySQLBulkLoaderMeta method getSQLStatements.

public SQLStatement getSQLStatements(TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev, Repository repository, IMetaStore metaStore) throws KettleStepException {
    // default: nothing to do!
    SQLStatement retval = new SQLStatement(stepMeta.getName(), databaseMeta, null);
    if (databaseMeta != null) {
        if (prev != null && prev.size() > 0) {
            // Copy the row
            RowMetaInterface tableFields = new RowMeta();
            // Now change the field names
            for (int i = 0; i < fieldTable.length; i++) {
                ValueMetaInterface v = prev.searchValueMeta(fieldStream[i]);
                if (v != null) {
                    ValueMetaInterface tableField = v.clone();
                    tableField.setName(fieldTable[i]);
                    tableFields.addValueMeta(tableField);
                } else {
                    throw new KettleStepException("Unable to find field [" + fieldStream[i] + "] in the input rows");
                }
            }
            if (!Utils.isEmpty(tableName)) {
                Database db = new Database(loggingObject, databaseMeta);
                db.shareVariablesWith(transMeta);
                try {
                    db.connect();
                    String schemaTable = databaseMeta.getQuotedSchemaTableCombination(transMeta.environmentSubstitute(schemaName), transMeta.environmentSubstitute(tableName));
                    String cr_table = db.getDDL(schemaTable, tableFields, null, false, null, true);
                    String sql = cr_table;
                    if (sql.length() == 0) {
                        retval.setSQL(null);
                    } else {
                        retval.setSQL(sql);
                    }
                } catch (KettleException e) {
                    retval.setError(BaseMessages.getString(PKG, "MySQLBulkLoaderMeta.GetSQL.ErrorOccurred") + e.getMessage());
                }
            } else {
                retval.setError(BaseMessages.getString(PKG, "MySQLBulkLoaderMeta.GetSQL.NoTableDefinedOnConnection"));
            }
        } else {
            retval.setError(BaseMessages.getString(PKG, "MySQLBulkLoaderMeta.GetSQL.NotReceivingAnyFields"));
        }
    } else {
        retval.setError(BaseMessages.getString(PKG, "MySQLBulkLoaderMeta.GetSQL.NoConnectionDefined"));
    }
    return retval;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) RowMeta(org.pentaho.di.core.row.RowMeta) Database(org.pentaho.di.core.database.Database) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) SQLStatement(org.pentaho.di.core.SQLStatement) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Example 32 with SQLStatement

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

the class IngresVectorwiseLoaderMeta method getSQLStatements.

@Override
public SQLStatement getSQLStatements(TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev, Repository repository, IMetaStore metaStore) {
    // default:
    SQLStatement retval = new SQLStatement(stepMeta.getName(), databaseMeta, null);
    if (databaseMeta != null) {
        if (prev != null && prev.size() > 0) {
            if (!Utils.isEmpty(tablename)) {
                Database db = new Database(loggingObject, databaseMeta);
                db.shareVariablesWith(transMeta);
                try {
                    db.connect();
                    String schemaTable = databaseMeta.getQuotedSchemaTableCombination(null, tablename);
                    String cr_table = db.getDDL(schemaTable, prev);
                    // Squeeze in the VECTORWISE col store clause...
                    // TODO: move this to the database dialog and make it user
                    // configurable.
                    // 
                    String VW_CLAUSE = "WITH STRUCTURE=VECTORWISE";
                    if (cr_table.toUpperCase().contains("CREATE TABLE")) {
                        int scIndex = cr_table.indexOf(';');
                        if (scIndex < 0) {
                            cr_table += VW_CLAUSE;
                        } else {
                            cr_table = cr_table.substring(0, scIndex) + VW_CLAUSE + cr_table.substring(scIndex);
                        }
                    }
                    // Empty string means: nothing to do: set it to null...
                    if (cr_table == null || cr_table.length() == 0) {
                        cr_table = null;
                    }
                    retval.setSQL(cr_table);
                } catch (KettleDatabaseException dbe) {
                    retval.setError(BaseMessages.getString(PKG, "IngresVectorWiseLoaderMeta.Error.ErrorConnecting", dbe.getMessage()));
                } finally {
                    db.disconnect();
                }
            } else {
                retval.setError(BaseMessages.getString(PKG, "IngresVectorWiseLoaderMeta.Error.NoTable"));
            }
        } else {
            retval.setError(BaseMessages.getString(PKG, "IngresVectorWiseLoaderMeta.Error.NoInput"));
        }
    } else {
        retval.setError(BaseMessages.getString(PKG, "IngresVectorWiseLoaderMeta.Error.NoConnection"));
    }
    return retval;
}
Also used : KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) Database(org.pentaho.di.core.database.Database) SQLStatement(org.pentaho.di.core.SQLStatement)

Example 33 with SQLStatement

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

the class TransMeta method getSQLStatementsString.

/**
 * Get the SQL statements (needed to run this transformation) as a single String.
 *
 * @return the SQL statements needed to run this transformation
 * @throws KettleStepException
 *           if any errors occur during SQL statement generation
 */
public String getSQLStatementsString() throws KettleStepException {
    String sql = "";
    List<SQLStatement> stats = getSQLStatements();
    for (int i = 0; i < stats.size(); i++) {
        SQLStatement stat = stats.get(i);
        if (!stat.hasError() && stat.hasSQL()) {
            sql += stat.getSQL();
        }
    }
    return sql;
}
Also used : SQLStatement(org.pentaho.di.core.SQLStatement) Point(org.pentaho.di.core.gui.Point) KettleExtensionPoint(org.pentaho.di.core.extension.KettleExtensionPoint)

Example 34 with SQLStatement

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

the class UpdateMetaTest method testUseDefaultSchemaName.

@Test
public void testUseDefaultSchemaName() throws Exception {
    String schemaName = "";
    String tableName = "tableName";
    String schemaTable = "default.tableName";
    DatabaseMeta databaseMeta = spy(new DatabaseMeta(databaseXML));
    doReturn("someValue").when(databaseMeta).getFieldDefinition(any(ValueMetaInterface.class), anyString(), anyString(), anyBoolean());
    doReturn(schemaTable).when(databaseMeta).getQuotedSchemaTableCombination(schemaName, tableName);
    ValueMetaInterface valueMeta = mock(ValueMetaInterface.class);
    when(valueMeta.clone()).thenReturn(mock(ValueMetaInterface.class));
    RowMetaInterface rowMetaInterface = mock(RowMetaInterface.class);
    when(rowMetaInterface.size()).thenReturn(1);
    when(rowMetaInterface.searchValueMeta(anyString())).thenReturn(valueMeta);
    UpdateMeta updateMeta = new UpdateMeta();
    updateMeta.setDatabaseMeta(databaseMeta);
    updateMeta.setTableName(tableName);
    updateMeta.setSchemaName(schemaName);
    updateMeta.setKeyLookup(new String[] { "KeyLookup1", "KeyLookup2" });
    updateMeta.setKeyStream(new String[] { "KeyStream1", "KeyStream2" });
    updateMeta.setUpdateLookup(new String[] { "updateLookup1", "updateLookup2" });
    updateMeta.setUpdateStream(new String[] { "UpdateStream1", "UpdateStream2" });
    SQLStatement sqlStatement = updateMeta.getSQLStatements(new TransMeta(), mock(StepMeta.class), rowMetaInterface, mock(Repository.class), mock(IMetaStore.class));
    String sql = sqlStatement.getSQL();
    assertTrue(StringUtils.countMatches(sql, schemaTable) == 2);
}
Also used : Repository(org.pentaho.di.repository.Repository) TransMeta(org.pentaho.di.trans.TransMeta) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) Matchers.anyString(org.mockito.Matchers.anyString) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) SQLStatement(org.pentaho.di.core.SQLStatement) StepMeta(org.pentaho.di.trans.step.StepMeta) IMetaStore(org.pentaho.metastore.api.IMetaStore) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) Test(org.junit.Test)

Example 35 with SQLStatement

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

the class SQLStatementsDialog method getSQL.

private String getSQL() {
    StringBuilder sql = new StringBuilder();
    int[] idx = wFields.table.getSelectionIndices();
    // None selected: don't waste users time: select them all!
    if (idx.length == 0) {
        idx = new int[stats.size()];
        for (int i = 0; i < stats.size(); i++) {
            idx[i] = i;
        }
    }
    for (int i = 0; i < idx.length; i++) {
        SQLStatement stat = stats.get(idx[i]);
        DatabaseMeta di = stat.getDatabase();
        if (i > 0) {
            sql.append("-------------------------------------------------------------------------------------------").append(Const.CR);
        }
        sql.append(BaseMessages.getString(PKG, "SQLStatementDialog.Log.Step", stat.getStepname()));
        sql.append(BaseMessages.getString(PKG, "SQLStatementDialog.Log.Connection", (di != null ? di.getName() : BaseMessages.getString(PKG, "SQLStatementDialog.Log.Undefined"))));
        if (stat.hasSQL()) {
            sql.append("-- SQL                  : ");
            sql.append(stat.getSQL()).append(Const.CR);
        }
        if (stat.hasError()) {
            sql.append(BaseMessages.getString(PKG, "SQLStatementDialog.Log.Error", stat.getError()));
        }
    }
    return sql.toString();
}
Also used : SQLStatement(org.pentaho.di.core.SQLStatement) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta)

Aggregations

SQLStatement (org.pentaho.di.core.SQLStatement)49 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)35 KettleException (org.pentaho.di.core.exception.KettleException)33 StepMeta (org.pentaho.di.trans.step.StepMeta)22 Database (org.pentaho.di.core.database.Database)21 MessageBox (org.eclipse.swt.widgets.MessageBox)18 BaseStepMeta (org.pentaho.di.trans.step.BaseStepMeta)16 SQLEditor (org.pentaho.di.ui.core.database.dialog.SQLEditor)16 ErrorDialog (org.pentaho.di.ui.core.dialog.ErrorDialog)16 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)15 RowMeta (org.pentaho.di.core.row.RowMeta)12 KettleStepException (org.pentaho.di.core.exception.KettleStepException)10 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)9 DatabaseMeta (org.pentaho.di.core.database.DatabaseMeta)6 TransMeta (org.pentaho.di.trans.TransMeta)4 KettleExtensionPoint (org.pentaho.di.core.extension.KettleExtensionPoint)3 Point (org.pentaho.di.core.gui.Point)3 ValueMetaInteger (org.pentaho.di.core.row.value.ValueMetaInteger)3 Repository (org.pentaho.di.repository.Repository)3 ArrayList (java.util.ArrayList)2