Search in sources :

Example 46 with Database

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

the class DynamicSQLRow method init.

public boolean init(StepMetaInterface smi, StepDataInterface sdi) {
    meta = (DynamicSQLRowMeta) smi;
    data = (DynamicSQLRowData) sdi;
    if (super.init(smi, sdi)) {
        if (meta.getDatabaseMeta() == null) {
            logError(BaseMessages.getString(PKG, "DynmaicSQLRow.Init.ConnectionMissing", getStepname()));
            return false;
        }
        data.db = new Database(this, meta.getDatabaseMeta());
        data.db.shareVariablesWith(this);
        try {
            if (getTransMeta().isUsingUniqueConnections()) {
                synchronized (getTrans()) {
                    data.db.connect(getTrans().getTransactionId(), getPartitionID());
                }
            } else {
                data.db.connect(getPartitionID());
            }
            // we never get a commit, but it just turns off auto-commit.
            data.db.setCommit(100);
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "DynamicSQLRow.Log.ConnectedToDB"));
            }
            data.db.setQueryLimit(meta.getRowLimit());
            return true;
        } catch (KettleException e) {
            logError(BaseMessages.getString(PKG, "DynamicSQLRow.Log.DatabaseError") + e.getMessage());
            if (data.db != null) {
                data.db.disconnect();
            }
        }
    }
    return false;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) Database(org.pentaho.di.core.database.Database)

Example 47 with Database

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

the class DynamicSQLRowMeta method check.

public void check(List<CheckResultInterface> remarks, TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev, String[] input, String[] output, RowMetaInterface info, VariableSpace space, Repository repository, IMetaStore metaStore) {
    CheckResult cr;
    String error_message = "";
    // See if we have input streams leading to this step!
    if (input.length > 0) {
        cr = new CheckResult(CheckResult.TYPE_RESULT_OK, BaseMessages.getString(PKG, "DynamicSQLRowMeta.CheckResult.ReceivingInfo"), stepMeta);
        remarks.add(cr);
    } else {
        cr = new CheckResult(CheckResult.TYPE_RESULT_ERROR, BaseMessages.getString(PKG, "DynamicSQLRowMeta.CheckResult.NoInputReceived"), stepMeta);
        remarks.add(cr);
    }
    // Check for SQL field
    if (Utils.isEmpty(sqlfieldname)) {
        cr = new CheckResult(CheckResult.TYPE_RESULT_ERROR, BaseMessages.getString(PKG, "DynamicSQLRowMeta.CheckResult.SQLFieldNameMissing"), stepMeta);
        remarks.add(cr);
    } else {
        ValueMetaInterface vfield = prev.searchValueMeta(sqlfieldname);
        if (vfield == null) {
            cr = new CheckResult(CheckResult.TYPE_RESULT_ERROR, BaseMessages.getString(PKG, "DynamicSQLRowMeta.CheckResult.SQLFieldNotFound", sqlfieldname), stepMeta);
        } else {
            cr = new CheckResult(CheckResult.TYPE_RESULT_OK, BaseMessages.getString(PKG, "DynamicSQLRowMeta.CheckResult.SQLFieldFound", sqlfieldname, vfield.getOrigin()), stepMeta);
        }
        remarks.add(cr);
    }
    if (databaseMeta != null) {
        Database db = new Database(loggingObject, databaseMeta);
        // Keep track of this one for cancelQuery
        databases = new Database[] { db };
        try {
            db.connect();
            if (sql != null && sql.length() != 0) {
                error_message = "";
                RowMetaInterface r = db.getQueryFields(sql, true);
                if (r != null) {
                    cr = new CheckResult(CheckResult.TYPE_RESULT_OK, BaseMessages.getString(PKG, "DynamicSQLRowMeta.CheckResult.QueryOK"), stepMeta);
                    remarks.add(cr);
                } else {
                    error_message = BaseMessages.getString(PKG, "DynamicSQLRowMeta.CheckResult.InvalidDBQuery");
                    cr = new CheckResult(CheckResult.TYPE_RESULT_ERROR, error_message, stepMeta);
                    remarks.add(cr);
                }
            }
        } catch (KettleException e) {
            error_message = BaseMessages.getString(PKG, "DynamicSQLRowMeta.CheckResult.ErrorOccurred") + e.getMessage();
            cr = new CheckResult(CheckResult.TYPE_RESULT_ERROR, error_message, stepMeta);
            remarks.add(cr);
        } finally {
            db.disconnect();
        }
    } else {
        error_message = BaseMessages.getString(PKG, "DynamicSQLRowMeta.CheckResult.InvalidConnection");
        cr = new CheckResult(CheckResult.TYPE_RESULT_ERROR, error_message, stepMeta);
        remarks.add(cr);
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) CheckResult(org.pentaho.di.core.CheckResult) Database(org.pentaho.di.core.database.Database) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Example 48 with Database

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

the class DeleteMeta method getSQLStatements.

public SQLStatement getSQLStatements(TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev, Repository repository, IMetaStore metaStore) {
    // default: nothing to do!
    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(schemaName, tableName);
                    String cr_table = db.getDDL(schemaTable, prev, null, false, null, true);
                    String cr_index = "";
                    String[] idx_fields = null;
                    if (keyLookup != null && keyLookup.length > 0) {
                        idx_fields = new String[keyLookup.length];
                        for (int i = 0; i < keyLookup.length; i++) {
                            idx_fields[i] = keyLookup[i];
                        }
                    } else {
                        retval.setError(BaseMessages.getString(PKG, "DeleteMeta.CheckResult.KeyFieldsRequired"));
                    }
                    // Key lookup dimensions...
                    if (idx_fields != null && idx_fields.length > 0 && !db.checkIndexExists(schemaTable, idx_fields)) {
                        String indexname = "idx_" + tableName + "_lookup";
                        cr_index = db.getCreateIndexStatement(schemaName, tableName, indexname, idx_fields, false, false, false, true);
                    }
                    String sql = cr_table + cr_index;
                    if (sql.length() == 0) {
                        retval.setSQL(null);
                    } else {
                        retval.setSQL(sql);
                    }
                } catch (KettleException e) {
                    retval.setError(BaseMessages.getString(PKG, "DeleteMeta.Returnvalue.ErrorOccurred") + e.getMessage());
                }
            } else {
                retval.setError(BaseMessages.getString(PKG, "DeleteMeta.Returnvalue.NoTableDefinedOnConnection"));
            }
        } else {
            retval.setError(BaseMessages.getString(PKG, "DeleteMeta.Returnvalue.NoReceivingAnyFields"));
        }
    } else {
        retval.setError(BaseMessages.getString(PKG, "DeleteMeta.Returnvalue.NoConnectionDefined"));
    }
    return retval;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) Database(org.pentaho.di.core.database.Database) SQLStatement(org.pentaho.di.core.SQLStatement)

Example 49 with Database

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

the class MonetDBBulkLoaderMeta method getMonetDBVersion.

/**
 * Returns the version of MonetDB that is used.
 *
 * @return The version of MonetDB
 * @throws KettleException
 *           if an error occurs
 */
private MonetDbVersion getMonetDBVersion() throws KettleException {
    Database db = null;
    db = new Database(loggingObject, databaseMeta);
    try {
        db.connect();
        return new MonetDbVersion(db.getDatabaseMetaData().getDatabaseProductVersion());
    } catch (Exception e) {
        throw new KettleException(e);
    } finally {
        if (db != null) {
            db.disconnect();
        }
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) Database(org.pentaho.di.core.database.Database) KettleException(org.pentaho.di.core.exception.KettleException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) KettleStepException(org.pentaho.di.core.exception.KettleStepException)

Example 50 with Database

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

the class MonetDBBulkLoaderMeta method getRequiredFields.

public RowMetaInterface getRequiredFields(VariableSpace space) throws KettleException {
    String realTableName = space.environmentSubstitute(tableName);
    String realSchemaName = space.environmentSubstitute(schemaName);
    if (databaseMeta != null) {
        Database db = new Database(loggingObject, databaseMeta);
        try {
            db.connect();
            if (!Utils.isEmpty(realTableName)) {
                String schemaTable = databaseMeta.getQuotedSchemaTableCombination(realSchemaName, realTableName);
                // Check if this table exists...
                if (db.checkTableExists(schemaTable)) {
                    return db.getTableFields(schemaTable);
                } else {
                    throw new KettleException(BaseMessages.getString(PKG, "MonetDBBulkLoaderMeta.Exception.TableNotFound"));
                }
            } else {
                throw new KettleException(BaseMessages.getString(PKG, "MonetDBBulkLoaderMeta.Exception.TableNotSpecified"));
            }
        } catch (Exception e) {
            throw new KettleException(BaseMessages.getString(PKG, "MonetDBBulkLoaderMeta.Exception.ErrorGettingFields"), e);
        } finally {
            db.disconnect();
        }
    } else {
        throw new KettleException(BaseMessages.getString(PKG, "MonetDBBulkLoaderMeta.Exception.ConnectionNotDefined"));
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) Database(org.pentaho.di.core.database.Database) KettleException(org.pentaho.di.core.exception.KettleException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) KettleStepException(org.pentaho.di.core.exception.KettleStepException)

Aggregations

Database (org.pentaho.di.core.database.Database)238 KettleException (org.pentaho.di.core.exception.KettleException)135 DatabaseMeta (org.pentaho.di.core.database.DatabaseMeta)90 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)82 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)62 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)46 ErrorDialog (org.pentaho.di.ui.core.dialog.ErrorDialog)32 KettleStepException (org.pentaho.di.core.exception.KettleStepException)30 MessageBox (org.eclipse.swt.widgets.MessageBox)28 CheckResult (org.pentaho.di.core.CheckResult)25 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)25 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)24 RowMeta (org.pentaho.di.core.row.RowMeta)22 SQLStatement (org.pentaho.di.core.SQLStatement)21 EnterSelectionDialog (org.pentaho.di.ui.core.dialog.EnterSelectionDialog)21 Test (org.junit.Test)20 ArrayList (java.util.ArrayList)18 KettleValueException (org.pentaho.di.core.exception.KettleValueException)17 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)16 ColumnInfo (org.pentaho.di.ui.core.widget.ColumnInfo)15