Search in sources :

Example 56 with DatabaseMeta

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

the class InsertUpdate method prepareUpdate.

// Lookup certain fields in a table
public void prepareUpdate(RowMetaInterface rowMeta) throws KettleDatabaseException {
    DatabaseMeta databaseMeta = meta.getDatabaseMeta();
    data.updateParameterRowMeta = new RowMeta();
    String sql = "UPDATE " + data.schemaTable + Const.CR;
    sql += "SET ";
    boolean comma = false;
    for (int i = 0; i < meta.getUpdateLookup().length; i++) {
        if (meta.getUpdate()[i].booleanValue()) {
            if (comma) {
                sql += ",   ";
            } else {
                comma = true;
            }
            sql += databaseMeta.quoteField(meta.getUpdateLookup()[i]);
            sql += " = ?" + Const.CR;
            data.updateParameterRowMeta.addValueMeta(rowMeta.searchValueMeta(meta.getUpdateStream()[i]).clone());
        }
    }
    sql += "WHERE ";
    for (int i = 0; i < meta.getKeyLookup().length; i++) {
        if (i != 0) {
            sql += "AND   ";
        }
        sql += " ( ( ";
        sql += databaseMeta.quoteField(meta.getKeyLookup()[i]);
        if ("BETWEEN".equalsIgnoreCase(meta.getKeyCondition()[i])) {
            sql += " BETWEEN ? AND ? ";
            data.updateParameterRowMeta.addValueMeta(rowMeta.searchValueMeta(meta.getKeyStream()[i]));
            data.updateParameterRowMeta.addValueMeta(rowMeta.searchValueMeta(meta.getKeyStream2()[i]));
        } else if ("IS NULL".equalsIgnoreCase(meta.getKeyCondition()[i]) || "IS NOT NULL".equalsIgnoreCase(meta.getKeyCondition()[i])) {
            sql += " " + meta.getKeyCondition()[i] + " ";
        } else if ("= ~NULL".equalsIgnoreCase(meta.getKeyCondition()[i])) {
            sql += " IS NULL AND ";
            if (databaseMeta.requiresCastToVariousForIsNull()) {
                sql += "CAST(? AS VARCHAR(256)) IS NULL";
            } else {
                sql += "? IS NULL";
            }
            // null check
            data.updateParameterRowMeta.addValueMeta(rowMeta.searchValueMeta(meta.getKeyStream()[i]));
            sql += " ) OR ( " + databaseMeta.quoteField(meta.getKeyLookup()[i]) + " = ?";
            // equality check, cloning so auto-rename because of adding same fieldname does not cause problems
            data.updateParameterRowMeta.addValueMeta(rowMeta.searchValueMeta(meta.getKeyStream()[i]).clone());
        } else {
            sql += " " + meta.getKeyCondition()[i] + " ? ";
            data.updateParameterRowMeta.addValueMeta(rowMeta.searchValueMeta(meta.getKeyStream()[i]).clone());
        }
        sql += " ) ) ";
    }
    try {
        if (log.isDetailed()) {
            logDetailed("Setting update preparedStatement to [" + sql + "]");
        }
        data.prepStatementUpdate = data.db.getConnection().prepareStatement(databaseMeta.stripCR(sql));
    } catch (SQLException ex) {
        throw new KettleDatabaseException("Unable to prepare statement for SQL statement [" + sql + "]", ex);
    }
}
Also used : RowMeta(org.pentaho.di.core.row.RowMeta) SQLException(java.sql.SQLException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta)

Example 57 with DatabaseMeta

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

the class InsertUpdate method setLookup.

public void setLookup(RowMetaInterface rowMeta) throws KettleDatabaseException {
    data.lookupParameterRowMeta = new RowMeta();
    data.lookupReturnRowMeta = new RowMeta();
    DatabaseMeta databaseMeta = meta.getDatabaseMeta();
    String sql = "SELECT ";
    for (int i = 0; i < meta.getUpdateLookup().length; i++) {
        if (i != 0) {
            sql += ", ";
        }
        sql += databaseMeta.quoteField(meta.getUpdateLookup()[i]);
        data.lookupReturnRowMeta.addValueMeta(rowMeta.searchValueMeta(meta.getUpdateStream()[i]).clone());
    }
    sql += " FROM " + data.schemaTable + " WHERE ";
    for (int i = 0; i < meta.getKeyLookup().length; i++) {
        if (i != 0) {
            sql += " AND ";
        }
        sql += " ( ( ";
        sql += databaseMeta.quoteField(meta.getKeyLookup()[i]);
        if ("BETWEEN".equalsIgnoreCase(meta.getKeyCondition()[i])) {
            sql += " BETWEEN ? AND ? ";
            data.lookupParameterRowMeta.addValueMeta(rowMeta.searchValueMeta(meta.getKeyStream()[i]));
            data.lookupParameterRowMeta.addValueMeta(rowMeta.searchValueMeta(meta.getKeyStream2()[i]));
        } else {
            if ("IS NULL".equalsIgnoreCase(meta.getKeyCondition()[i]) || "IS NOT NULL".equalsIgnoreCase(meta.getKeyCondition()[i])) {
                sql += " " + meta.getKeyCondition()[i] + " ";
            } else if ("= ~NULL".equalsIgnoreCase(meta.getKeyCondition()[i])) {
                sql += " IS NULL AND ";
                if (databaseMeta.requiresCastToVariousForIsNull()) {
                    sql += " CAST(? AS VARCHAR(256)) IS NULL ";
                } else {
                    sql += " ? IS NULL ";
                }
                // null check
                data.lookupParameterRowMeta.addValueMeta(rowMeta.searchValueMeta(meta.getKeyStream()[i]));
                sql += " ) OR ( " + databaseMeta.quoteField(meta.getKeyLookup()[i]) + " = ? ";
                // equality check, cloning so auto-rename because of adding same fieldname does not cause problems
                data.lookupParameterRowMeta.addValueMeta(rowMeta.searchValueMeta(meta.getKeyStream()[i]).clone());
            } else {
                sql += " " + meta.getKeyCondition()[i] + " ? ";
                data.lookupParameterRowMeta.addValueMeta(rowMeta.searchValueMeta(meta.getKeyStream()[i]));
            }
        }
        sql += " ) ) ";
    }
    try {
        if (log.isDetailed()) {
            logDetailed("Setting preparedStatement to [" + sql + "]");
        }
        data.prepStatementLookup = data.db.getConnection().prepareStatement(databaseMeta.stripCR(sql));
    } catch (SQLException ex) {
        throw new KettleDatabaseException("Unable to prepare statement for SQL statement [" + sql + "]", ex);
    }
}
Also used : RowMeta(org.pentaho.di.core.row.RowMeta) SQLException(java.sql.SQLException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta)

Example 58 with DatabaseMeta

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

the class DatabaseMetaLoadSaveValidator method getTestObject.

@Override
public DatabaseMeta getTestObject() {
    DatabaseMeta db = new DatabaseMeta();
    db.setObjectId(new LongObjectId(rand.nextInt(Integer.MAX_VALUE)));
    db.setName(UUID.randomUUID().toString());
    db.setHostname(UUID.randomUUID().toString());
    db.setUsername(UUID.randomUUID().toString());
    db.setPassword(UUID.randomUUID().toString());
    return db;
}
Also used : LongObjectId(org.pentaho.di.repository.LongObjectId) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta)

Example 59 with DatabaseMeta

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

the class XulDatabaseExplorerControllerIT method testPostActionStatusForReflectCalls.

/**
 * PDI-11394 - Empty database browser dialog appears after unsuccessful connect to DB
 */
@Test
public void testPostActionStatusForReflectCalls() {
    Shell shell = new Shell();
    DatabaseMeta meta = new DatabaseMeta();
    List<DatabaseMeta> list = Collections.emptyList();
    XulDatabaseExplorerController dialog = new XulDatabaseExplorerController(shell, meta, list, false);
    UiPostActionStatus actual = dialog.getActionStatus();
    Assert.assertEquals("By default action status is none", UiPostActionStatus.NONE, actual);
    try {
        dialog.createDatabaseNodes();
    } catch (Exception e) {
    // do nothing as it usually used for ui functionality
    }
    actual = dialog.getActionStatus();
    // this error is caused runtime exception on error dialog show
    Assert.assertEquals("For reflective calls we have ability to ask for status directly", UiPostActionStatus.ERROR, actual);
}
Also used : Shell(org.eclipse.swt.widgets.Shell) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) Test(org.junit.Test)

Example 60 with DatabaseMeta

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

the class JobEntryCheckDbConnections method getResourceDependencies.

public List<ResourceReference> getResourceDependencies(JobMeta jobMeta) {
    List<ResourceReference> references = super.getResourceDependencies(jobMeta);
    if (connections != null) {
        for (int i = 0; i < connections.length; i++) {
            DatabaseMeta connection = connections[i];
            ResourceReference reference = new ResourceReference(this);
            reference.getEntries().add(new ResourceEntry(connection.getHostname(), ResourceType.SERVER));
            reference.getEntries().add(new ResourceEntry(connection.getDatabaseName(), ResourceType.DATABASENAME));
            references.add(reference);
        }
    }
    return references;
}
Also used : ResourceEntry(org.pentaho.di.resource.ResourceEntry) ResourceReference(org.pentaho.di.resource.ResourceReference) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta)

Aggregations

DatabaseMeta (org.pentaho.di.core.database.DatabaseMeta)522 Test (org.junit.Test)133 KettleException (org.pentaho.di.core.exception.KettleException)131 Database (org.pentaho.di.core.database.Database)88 MessageBox (org.eclipse.swt.widgets.MessageBox)66 ErrorDialog (org.pentaho.di.ui.core.dialog.ErrorDialog)63 TransMeta (org.pentaho.di.trans.TransMeta)57 StepMeta (org.pentaho.di.trans.step.StepMeta)54 ArrayList (java.util.ArrayList)53 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)48 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)44 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)42 SlaveServer (org.pentaho.di.cluster.SlaveServer)33 IMetaStore (org.pentaho.metastore.api.IMetaStore)30 ObjectId (org.pentaho.di.repository.ObjectId)29 DatabaseExplorerDialog (org.pentaho.di.ui.core.database.dialog.DatabaseExplorerDialog)29 JobMeta (org.pentaho.di.job.JobMeta)26 TransHopMeta (org.pentaho.di.trans.TransHopMeta)26 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)24 PluginRegistry (org.pentaho.di.core.plugins.PluginRegistry)24