Search in sources :

Example 6 with Database

use of org.apache.hop.core.database.Database in project hop by apache.

the class PGBulkLoaderMeta method getRequiredFields.

@Override
public IRowMeta getRequiredFields(IVariables variables) throws HopException {
    String realTableName = variables.resolve(tableName);
    String realSchemaName = variables.resolve(schemaName);
    if (databaseMeta != null) {
        Database db = new Database(loggingObject, variables, databaseMeta);
        try {
            db.connect();
            if (!Utils.isEmpty(realTableName)) {
                String schemaTable = databaseMeta.getQuotedSchemaTableCombination(variables, realSchemaName, realTableName);
                // Check if this table exists...
                if (db.checkTableExists(realSchemaName, realTableName)) {
                    return db.getTableFields(schemaTable);
                } else {
                    throw new HopException(BaseMessages.getString(PKG, "GPBulkLoaderMeta.Exception.TableNotFound"));
                }
            } else {
                throw new HopException(BaseMessages.getString(PKG, "GPBulkLoaderMeta.Exception.TableNotSpecified"));
            }
        } catch (Exception e) {
            throw new HopException(BaseMessages.getString(PKG, "GPBulkLoaderMeta.Exception.ErrorGettingFields"), e);
        } finally {
            db.disconnect();
        }
    } else {
        throw new HopException(BaseMessages.getString(PKG, "GPBulkLoaderMeta.Exception.ConnectionNotDefined"));
    }
}
Also used : HopException(org.apache.hop.core.exception.HopException) Database(org.apache.hop.core.database.Database) HopException(org.apache.hop.core.exception.HopException) HopTransformException(org.apache.hop.core.exception.HopTransformException)

Example 7 with Database

use of org.apache.hop.core.database.Database in project hop by apache.

the class PGBulkLoaderTest method testDBNameNOTOverridden_IfDbNameOverrideEmpty.

@Test
public void testDBNameNOTOverridden_IfDbNameOverrideEmpty() throws Exception {
    // Db Name Override is empty
    PGBulkLoaderMeta pgBulkLoaderMock = getPgBulkLoaderMock(DB_NAME_EMPTY);
    Database database = pgBulkLoader.getDatabase(pgBulkLoader, pgBulkLoaderMock);
    assertNotNull(database);
    // Verify DB name is NOT overridden
    assertEquals(CONNECTION_DB_NAME, database.getDatabaseMeta().getDatabaseName());
    // Check additionally other connection information
    assertEquals(CONNECTION_NAME, database.getDatabaseMeta().getName());
    assertEquals(CONNECTION_DB_HOST, database.getDatabaseMeta().getHostname());
    assertEquals(CONNECTION_DB_PORT, database.getDatabaseMeta().getPort());
    assertEquals(CONNECTION_DB_USERNAME, database.getDatabaseMeta().getUsername());
    assertEquals(CONNECTION_DB_PASSWORD, database.getDatabaseMeta().getPassword());
}
Also used : Database(org.apache.hop.core.database.Database)

Example 8 with Database

use of org.apache.hop.core.database.Database in project hop by apache.

the class PGBulkLoaderTest method testDBNameNOTOverridden_IfDbNameOverrideNull.

@Test
public void testDBNameNOTOverridden_IfDbNameOverrideNull() throws Exception {
    // Db Name Override is null
    PGBulkLoaderMeta pgBulkLoaderMock = getPgBulkLoaderMock(null);
    Database database = pgBulkLoader.getDatabase(pgBulkLoader, pgBulkLoaderMock);
    assertNotNull(database);
    // Verify DB name is NOT overridden
    assertEquals(CONNECTION_DB_NAME, database.getDatabaseMeta().getDatabaseName());
    // Check additionally other connection information
    assertEquals(CONNECTION_NAME, database.getDatabaseMeta().getName());
    assertEquals(CONNECTION_DB_HOST, database.getDatabaseMeta().getHostname());
    assertEquals(CONNECTION_DB_PORT, database.getDatabaseMeta().getPort());
    assertEquals(CONNECTION_DB_USERNAME, database.getDatabaseMeta().getUsername());
    assertEquals(CONNECTION_DB_PASSWORD, database.getDatabaseMeta().getPassword());
}
Also used : Database(org.apache.hop.core.database.Database)

Example 9 with Database

use of org.apache.hop.core.database.Database in project hop by apache.

the class LocalPipelineEngine method prepareExecution.

@Override
public void prepareExecution() throws HopException {
    if (!(pipelineRunConfiguration.getEngineRunConfiguration() instanceof LocalPipelineRunConfiguration)) {
        throw new HopException("A local pipeline execution expects a local pipeline configuration, not an instance of class " + pipelineRunConfiguration.getEngineRunConfiguration().getClass().getName());
    }
    LocalPipelineRunConfiguration config = (LocalPipelineRunConfiguration) pipelineRunConfiguration.getEngineRunConfiguration();
    int sizeRowsSet = Const.toInt(resolve(config.getRowSetSize()), Const.ROWS_IN_ROWSET);
    setRowSetSize(sizeRowsSet);
    setSafeModeEnabled(config.isSafeModeEnabled());
    setSortingTransformsTopologically(config.isSortingTransformsTopologically());
    setGatheringMetrics(config.isGatheringMetrics());
    setFeedbackShown(config.isFeedbackShown());
    setFeedbackSize(Const.toInt(resolve(config.getFeedbackSize()), Const.ROWS_UPDATE));
    // See if we need to enable transactions...
    // 
    IExtensionData parentExtensionData = getParentPipeline();
    if (parentExtensionData == null) {
        parentExtensionData = getParentWorkflow();
    }
    String connectionGroup = null;
    if (parentExtensionData != null) {
        connectionGroup = (String) parentExtensionData.getExtensionDataMap().get(Const.CONNECTION_GROUP);
    }
    // 
    if (config.isTransactional() && connectionGroup == null) {
        // Store a value in the parent...
        // 
        connectionGroup = getPipelineMeta().getName() + " - " + UUID.randomUUID();
        // We also need to commit/rollback at the end of this pipeline...
        // We only do this when we created a new group.  Never in a child
        // 
        addExecutionFinishedListener((IExecutionFinishedListener<IPipelineEngine>) pipeline -> {
            String group = (String) pipeline.getExtensionDataMap().get(Const.CONNECTION_GROUP);
            List<Database> databases = DatabaseConnectionMap.getInstance().getDatabases(group);
            Result result = pipeline.getResult();
            for (Database database : databases) {
                try {
                    if (result.getResult() && !result.isStopped() && result.getNrErrors() == 0) {
                        try {
                            database.commit(true);
                            pipeline.getLogChannel().logBasic("All transactions of database connection '" + database.getDatabaseMeta().getName() + "' were committed at the end of the pipeline!");
                        } catch (HopDatabaseException e) {
                            throw new HopException("Error committing database connection " + database.getDatabaseMeta().getName(), e);
                        }
                    } else {
                        try {
                            database.rollback(true);
                            pipeline.getLogChannel().logBasic("All transactions of database connection '" + database.getDatabaseMeta().getName() + "' were rolled back at the end of the pipeline!");
                        } catch (HopDatabaseException e) {
                            throw new HopException("Error rolling back database connection " + database.getDatabaseMeta().getName(), e);
                        }
                    }
                } finally {
                    try {
                        database.closeConnectionOnly();
                        pipeline.getLogChannel().logDebug("Database connection '" + database.getDatabaseMeta().getName() + "' closed successfully!");
                    } catch (HopDatabaseException hde) {
                        pipeline.getLogChannel().logError("Error disconnecting from database - closeConnectionOnly failed:" + Const.CR + hde.getMessage());
                        pipeline.getLogChannel().logError(Const.getStackTracker(hde));
                    }
                }
                DatabaseConnectionMap.getInstance().removeConnection(group, null, database);
            }
        });
    }
    // 
    if (connectionGroup != null && getExtensionDataMap() != null) {
        // Set the connection group for this pipeline
        // 
        getExtensionDataMap().put(Const.CONNECTION_GROUP, connectionGroup);
    }
    super.prepareExecution();
}
Also used : IPipelineEngineRunConfiguration(org.apache.hop.pipeline.config.IPipelineEngineRunConfiguration) Database(org.apache.hop.core.database.Database) HopDatabaseException(org.apache.hop.core.exception.HopDatabaseException) ILoggingObject(org.apache.hop.core.logging.ILoggingObject) IVariables(org.apache.hop.core.variables.IVariables) HopException(org.apache.hop.core.exception.HopException) IHopMetadataProvider(org.apache.hop.metadata.api.IHopMetadataProvider) UUID(java.util.UUID) Const(org.apache.hop.core.Const) IPipelineEngine(org.apache.hop.pipeline.engine.IPipelineEngine) ArrayList(java.util.ArrayList) PipelineEnginePlugin(org.apache.hop.pipeline.engine.PipelineEnginePlugin) INamedParameters(org.apache.hop.core.parameters.INamedParameters) List(java.util.List) Pipeline(org.apache.hop.pipeline.Pipeline) DatabaseConnectionMap(org.apache.hop.core.database.map.DatabaseConnectionMap) Result(org.apache.hop.core.Result) IExtensionData(org.apache.hop.core.IExtensionData) PipelineRunConfiguration(org.apache.hop.pipeline.config.PipelineRunConfiguration) PipelineMeta(org.apache.hop.pipeline.PipelineMeta) PipelineEngineCapabilities(org.apache.hop.pipeline.engine.PipelineEngineCapabilities) IExecutionFinishedListener(org.apache.hop.pipeline.IExecutionFinishedListener) IExtensionData(org.apache.hop.core.IExtensionData) HopException(org.apache.hop.core.exception.HopException) Database(org.apache.hop.core.database.Database) ArrayList(java.util.ArrayList) List(java.util.List) HopDatabaseException(org.apache.hop.core.exception.HopDatabaseException) IPipelineEngine(org.apache.hop.pipeline.engine.IPipelineEngine) Result(org.apache.hop.core.Result)

Example 10 with Database

use of org.apache.hop.core.database.Database in project hop by apache.

the class CombinationLookup method init.

@Override
public boolean init() {
    if (super.init()) {
        data.realSchemaName = resolve(meta.getSchemaName());
        data.realTableName = resolve(meta.getTableName());
        if (meta.getCacheSize() > 0) {
            data.cache = new HashMap<>((int) (meta.getCacheSize() * 1.5));
        } else {
            data.cache = new HashMap<>();
        }
        if (meta.getDatabaseMeta() == null) {
            logError(BaseMessages.getString(PKG, "CombinationLookup.Init.ConnectionMissing", getTransformName()));
            return false;
        }
        data.db = new Database(this, this, meta.getDatabaseMeta());
        try {
            data.db.connect();
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "CombinationLookup.Log.ConnectedToDB"));
            }
            data.db.setCommit(meta.getCommitSize());
            return true;
        } catch (HopDatabaseException dbe) {
            logError(BaseMessages.getString(PKG, "CombinationLookup.Log.UnableToConnectDB") + dbe.getMessage());
        }
    }
    return false;
}
Also used : Database(org.apache.hop.core.database.Database)

Aggregations

Database (org.apache.hop.core.database.Database)142 HopException (org.apache.hop.core.exception.HopException)95 DatabaseMeta (org.apache.hop.core.database.DatabaseMeta)79 IRowMeta (org.apache.hop.core.row.IRowMeta)65 IValueMeta (org.apache.hop.core.row.IValueMeta)31 HopDatabaseException (org.apache.hop.core.exception.HopDatabaseException)28 ErrorDialog (org.apache.hop.ui.core.dialog.ErrorDialog)25 ICheckResult (org.apache.hop.core.ICheckResult)20 EnterSelectionDialog (org.apache.hop.ui.core.dialog.EnterSelectionDialog)19 HopTransformException (org.apache.hop.core.exception.HopTransformException)17 RowMeta (org.apache.hop.core.row.RowMeta)14 HopXmlException (org.apache.hop.core.exception.HopXmlException)13 CheckResult (org.apache.hop.core.CheckResult)11 Result (org.apache.hop.core.Result)11 IVariables (org.apache.hop.core.variables.IVariables)11 ColumnInfo (org.apache.hop.ui.core.widget.ColumnInfo)11 SqlStatement (org.apache.hop.core.SqlStatement)7 ILoggingObject (org.apache.hop.core.logging.ILoggingObject)6 ValueMetaInteger (org.apache.hop.core.row.value.ValueMetaInteger)6 Test (org.junit.Test)6