Search in sources :

Example 16 with SQLStatement

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

the class LucidDBBulkLoaderMeta 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 sql = db.getDDL(schemaTable, tableFields, null, false, null, true);
                    if (Utils.isEmpty(sql)) {
                        retval.setSQL(null);
                    } else {
                        retval.setSQL(sql);
                    }
                } catch (KettleException e) {
                    retval.setError(BaseMessages.getString(PKG, "LucidDBBulkLoaderMeta.GetSQL.ErrorOccurred") + e.getMessage());
                }
            } else {
                retval.setError(BaseMessages.getString(PKG, "LucidDBBulkLoaderMeta.GetSQL.NoTableDefinedOnConnection"));
            }
        } else {
            retval.setError(BaseMessages.getString(PKG, "LucidDBBulkLoaderMeta.GetSQL.NotReceivingAnyFields"));
        }
    } else {
        retval.setError(BaseMessages.getString(PKG, "LucidDBBulkLoaderMeta.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 17 with SQLStatement

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

the class LucidDBStreamingLoaderDialog method create.

// Generate code for create table...
// Conversions done by Database
private void create() {
    try {
        LucidDBStreamingLoaderMeta info = new LucidDBStreamingLoaderMeta();
        getInfo(info);
        // new name might not yet be linked to other steps!
        String name = stepname;
        StepMeta stepMeta = new StepMeta(BaseMessages.getString(PKG, "LucidDBStreamingLoaderDialog.StepMeta.Title"), name, info);
        RowMetaInterface prev = transMeta.getPrevStepFields(stepname);
        SQLStatement sql = info.getSQLStatements(transMeta, stepMeta, prev, repository, metaStore);
        if (!sql.hasError()) {
            if (sql.hasSQL()) {
                SQLEditor sqledit = new SQLEditor(transMeta, shell, SWT.NONE, info.getDatabaseMeta(), transMeta.getDbCache(), sql.getSQL());
                sqledit.open();
            } else {
                MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_INFORMATION);
                mb.setMessage(BaseMessages.getString(PKG, "LucidDBStreamingLoaderDialog.NoSQLNeeds.DialogMessage"));
                mb.setText(BaseMessages.getString(PKG, "LucidDBStreamingLoaderDialog.NoSQLNeeds.DialogTitle"));
                mb.open();
            }
        } else {
            MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
            mb.setMessage(sql.getError());
            mb.setText(BaseMessages.getString(PKG, "LucidDBStreamingLoaderDialog.SQLError.DialogTitle"));
            mb.open();
        }
    } catch (KettleException ke) {
        new ErrorDialog(shell, BaseMessages.getString(PKG, "LucidDBStreamingLoaderDialog.CouldNotBuildSQL.DialogTitle"), BaseMessages.getString(PKG, "LucidDBStreamingLoaderDialog.CouldNotBuildSQL.DialogMessage"), ke);
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) SQLEditor(org.pentaho.di.ui.core.database.dialog.SQLEditor) LucidDBStreamingLoaderMeta(org.pentaho.di.trans.steps.luciddbstreamingloader.LucidDBStreamingLoaderMeta) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) StepMeta(org.pentaho.di.trans.step.StepMeta) BaseStepMeta(org.pentaho.di.trans.step.BaseStepMeta) SQLStatement(org.pentaho.di.core.SQLStatement) MessageBox(org.eclipse.swt.widgets.MessageBox)

Example 18 with SQLStatement

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

the class DimensionLookupMetaTest method testUseDefaultSchemaName.

@Test
public void testUseDefaultSchemaName() throws Exception {
    String schemaName = "";
    String tableName = "tableName";
    String schemaTable = "default.tableName";
    String keyField = "keyField";
    DatabaseMeta databaseMeta = spy(new DatabaseMeta(databaseXML) {

        @Override
        public String getFieldDefinition(ValueMetaInterface v, String tk, String pk, boolean use_autoinc) {
            return "someValue";
        }
    });
    when(databaseMeta.getQuotedSchemaTableCombination(schemaName, tableName)).thenReturn(schemaTable);
    DimensionLookupMeta dlm = new DimensionLookupMeta();
    dlm.setUpdate(true);
    dlm.setDatabaseMeta(databaseMeta);
    dlm.setTableName(tableName);
    dlm.setSchemaName(schemaName);
    dlm.setKeyLookup(new String[] { "keyLookup1", "keyLookup2" });
    dlm.setKeyStream(new String[] { "keyStream1", "keyStream2" });
    dlm.setFieldLookup(new String[] { "fieldLookup1", "fieldLookup2" });
    dlm.setFieldStream(new String[] { "FieldStream1", "FieldStream2" });
    dlm.setFieldUpdate(new int[] { 1, 2 });
    dlm.setKeyField(keyField);
    StepMeta stepMeta = mock(StepMeta.class);
    RowMetaInterface rowMetaInterface = mock(RowMetaInterface.class);
    when(rowMetaInterface.size()).thenReturn(1);
    Repository repository = mock(Repository.class);
    IMetaStore metaStore = mock(IMetaStore.class);
    SQLStatement sqlStatement = dlm.getSQLStatements(new TransMeta(), stepMeta, rowMetaInterface, repository, metaStore);
    String sql = sqlStatement.getSQL();
    assertEquals(3, StringUtils.countMatches(sql, schemaTable));
}
Also used : Repository(org.pentaho.di.repository.Repository) TransMeta(org.pentaho.di.trans.TransMeta) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Matchers.anyString(org.mockito.Matchers.anyString) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) StepMeta(org.pentaho.di.trans.step.StepMeta) IMetaStore(org.pentaho.metastore.api.IMetaStore) SQLStatement(org.pentaho.di.core.SQLStatement) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) Test(org.junit.Test)

Example 19 with SQLStatement

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

the class CombinationLookupDialog method create.

/**
 * Generate code for create table. Conversions done by database.
 */
private void create() {
    try {
        // Gather info...
        CombinationLookupMeta info = new CombinationLookupMeta();
        getInfo(info);
        // new name might not yet be linked to other steps!
        String name = stepname;
        StepMeta stepMeta = new StepMeta(BaseMessages.getString(PKG, "CombinationLookupDialog.StepMeta.Title"), name, info);
        RowMetaInterface prev = transMeta.getPrevStepFields(stepname);
        SQLStatement sql = info.getSQLStatements(transMeta, stepMeta, prev, repository, metaStore);
        if (!sql.hasError()) {
            if (sql.hasSQL()) {
                SQLEditor sqledit = new SQLEditor(transMeta, shell, SWT.NONE, info.getDatabaseMeta(), transMeta.getDbCache(), sql.getSQL());
                sqledit.open();
            } else {
                MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_INFORMATION);
                mb.setMessage(BaseMessages.getString(PKG, "CombinationLookupDialog.NoSQLNeeds.DialogMessage"));
                mb.setText(BaseMessages.getString(PKG, "CombinationLookupDialog.NoSQLNeeds.DialogTitle"));
                mb.open();
            }
        } else {
            MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
            mb.setMessage(sql.getError());
            mb.setText(BaseMessages.getString(PKG, "CombinationLookupDialog.SQLError.DialogTitle"));
            mb.open();
        }
    } catch (KettleException ke) {
        new ErrorDialog(shell, BaseMessages.getString(PKG, "CombinationLookupDialog.UnableToCreateSQL.DialogTitle"), BaseMessages.getString(PKG, "CombinationLookupDialog.UnableToCreateSQL.DialogMessage"), ke);
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) SQLEditor(org.pentaho.di.ui.core.database.dialog.SQLEditor) CombinationLookupMeta(org.pentaho.di.trans.steps.combinationlookup.CombinationLookupMeta) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) StepMeta(org.pentaho.di.trans.step.StepMeta) BaseStepMeta(org.pentaho.di.trans.step.BaseStepMeta) SQLStatement(org.pentaho.di.core.SQLStatement) MessageBox(org.eclipse.swt.widgets.MessageBox)

Example 20 with SQLStatement

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

the class PGBulkLoaderMeta 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 sql = db.getDDL(schemaTable, tableFields, null, false, null, true);
                    if (sql.length() == 0) {
                        retval.setSQL(null);
                    } else {
                        retval.setSQL(sql);
                    }
                } catch (KettleException e) {
                    retval.setError(BaseMessages.getString(PKG, "GPBulkLoaderMeta.GetSQL.ErrorOccurred") + e.getMessage());
                }
            } else {
                retval.setError(BaseMessages.getString(PKG, "GPBulkLoaderMeta.GetSQL.NoTableDefinedOnConnection"));
            }
        } else {
            retval.setError(BaseMessages.getString(PKG, "GPBulkLoaderMeta.GetSQL.NotReceivingAnyFields"));
        }
    } else {
        retval.setError(BaseMessages.getString(PKG, "GPBulkLoaderMeta.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)

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