Search in sources :

Example 1 with DatabaseConnectionPortObject

use of org.knime.core.node.port.database.DatabaseConnectionPortObject in project knime-core by knime.

the class SQLInjectNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    exec.setMessage("Retrieving metadata from database");
    final String varName = m_flowVariableName.getStringValue();
    if (varName == null) {
        throw new InvalidSettingsException("No flow variable for the SQL statement selected");
    }
    final String sql = peekFlowVariableString(varName);
    if (sql == null) {
        throw new InvalidSettingsException("The selected flow variable is not assigned");
    }
    DatabaseConnectionPortObject dbIn = (DatabaseConnectionPortObject) inData[0];
    DatabaseConnectionSettings inSettings = dbIn.getConnectionSettings(getCredentialsProvider());
    // Attach the SQL query
    DatabaseQueryConnectionSettings outSettings = new DatabaseQueryConnectionSettings(inSettings, sql);
    // Probe the database to see how the result table looks like
    DBReader load = outSettings.getUtility().getReader(outSettings);
    DataTableSpec tableSpec = load.getDataTableSpec(getCredentialsProvider());
    DatabasePortObjectSpec outSpec = new DatabasePortObjectSpec(tableSpec, outSettings.createConnectionModel());
    return new PortObject[] { new DatabasePortObject(outSpec) };
}
Also used : DatabasePortObject(org.knime.core.node.port.database.DatabasePortObject) DatabaseConnectionPortObject(org.knime.core.node.port.database.DatabaseConnectionPortObject) DatabaseQueryConnectionSettings(org.knime.core.node.port.database.DatabaseQueryConnectionSettings) DataTableSpec(org.knime.core.data.DataTableSpec) DBReader(org.knime.core.node.port.database.reader.DBReader) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DatabaseConnectionSettings(org.knime.core.node.port.database.DatabaseConnectionSettings) DatabasePortObjectSpec(org.knime.core.node.port.database.DatabasePortObjectSpec) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DatabaseConnectionPortObject(org.knime.core.node.port.database.DatabaseConnectionPortObject) FlowVariablePortObject(org.knime.core.node.port.flowvariable.FlowVariablePortObject) DatabasePortObject(org.knime.core.node.port.database.DatabasePortObject) PortObject(org.knime.core.node.port.PortObject)

Example 2 with DatabaseConnectionPortObject

use of org.knime.core.node.port.database.DatabaseConnectionPortObject in project knime-core by knime.

the class DBDropTableNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
    exec.setMessage("Droping table");
    final DatabaseConnectionPortObject incomingConnection = (DatabaseConnectionPortObject) inObjects[0];
    final CredentialsProvider cp = getCredentialsProvider();
    final DatabaseConnectionSettings connSettings = incomingConnection.getConnectionSettings(cp);
    final DatabaseUtility dbUtility = connSettings.getUtility();
    final StatementManipulator manipulator = dbUtility.getStatementManipulator();
    final String table2Drop = m_tableName.getStringValue();
    try {
        if (m_failIfNotExists.getBooleanValue() || connSettings.execute(cp, conn -> {
            return dbUtility.tableExists(conn, table2Drop);
        })) {
            connSettings.execute(manipulator.dropTable(table2Drop, m_cascade.getBooleanValue()), cp);
            exec.setMessage("Table " + table2Drop + " sucessful droped");
        } else {
            exec.setMessage("Table " + table2Drop + " does not exist in db");
        }
    } catch (SQLException ex) {
        Throwable cause = ExceptionUtils.getRootCause(ex);
        if (cause == null) {
            cause = ex;
        }
        throw new InvalidSettingsException("Error while validating drop statement: " + cause.getMessage(), ex);
    }
    return inObjects;
}
Also used : ExecutionMonitor(org.knime.core.node.ExecutionMonitor) DatabaseConnectionPortObject(org.knime.core.node.port.database.DatabaseConnectionPortObject) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) ExceptionUtils(org.apache.commons.lang.exception.ExceptionUtils) SettingsModelBoolean(org.knime.core.node.defaultnodesettings.SettingsModelBoolean) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) IOException(java.io.IOException) NodeModel(org.knime.core.node.NodeModel) File(java.io.File) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) NodeSettingsWO(org.knime.core.node.NodeSettingsWO) ExecutionContext(org.knime.core.node.ExecutionContext) SQLException(java.sql.SQLException) DatabaseConnectionSettings(org.knime.core.node.port.database.DatabaseConnectionSettings) CredentialsProvider(org.knime.core.node.workflow.CredentialsProvider) StatementManipulator(org.knime.core.node.port.database.StatementManipulator) PortObject(org.knime.core.node.port.PortObject) PortType(org.knime.core.node.port.PortType) DatabaseUtility(org.knime.core.node.port.database.DatabaseUtility) DatabaseConnectionPortObject(org.knime.core.node.port.database.DatabaseConnectionPortObject) DatabaseUtility(org.knime.core.node.port.database.DatabaseUtility) DatabaseConnectionSettings(org.knime.core.node.port.database.DatabaseConnectionSettings) SQLException(java.sql.SQLException) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CredentialsProvider(org.knime.core.node.workflow.CredentialsProvider) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) StatementManipulator(org.knime.core.node.port.database.StatementManipulator)

Example 3 with DatabaseConnectionPortObject

use of org.knime.core.node.port.database.DatabaseConnectionPortObject in project knime-core by knime.

the class DBDeleteRowsNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    final BufferedDataTable inTable = (BufferedDataTable) inData[0];
    final DataTableSpec inSpec = inTable.getSpec();
    final String[] whereIncludes = m_configWHERE.applyTo(inSpec).getIncludes();
    DatabaseConnectionSettings connSettings;
    if ((inData.length > 1) && (inData[1] instanceof DatabaseConnectionPortObject)) {
        connSettings = ((DatabaseConnectionPortObject) inData[1]).getConnectionSettings(getCredentialsProvider());
    } else {
        connSettings = m_loginConfig;
    }
    // DELETE rows
    final int[] deleteStatus = new int[inTable.getRowCount()];
    final DBWriter dbWriter = connSettings.getUtility().getWriter(connSettings);
    final String errMsg = dbWriter.deleteRows(null, m_tableName, inTable, whereIncludes, deleteStatus, exec, getCredentialsProvider(), m_batchSize);
    // set warning message generated during deleting rows
    if (errMsg != null) {
        setWarningMessage(errMsg);
    }
    // create out table with update column
    final ColumnRearranger colre = createColumnRearranger(inTable.getSpec(), deleteStatus);
    final BufferedDataTable outTable = exec.createColumnRearrangeTable(inTable, colre, exec.createSubProgress(1.0));
    return new BufferedDataTable[] { outTable };
}
Also used : DatabaseConnectionPortObject(org.knime.core.node.port.database.DatabaseConnectionPortObject) DataTableSpec(org.knime.core.data.DataTableSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DatabaseConnectionSettings(org.knime.core.node.port.database.DatabaseConnectionSettings) BufferedDataTable(org.knime.core.node.BufferedDataTable) DBWriter(org.knime.core.node.port.database.writer.DBWriter)

Example 4 with DatabaseConnectionPortObject

use of org.knime.core.node.port.database.DatabaseConnectionPortObject in project knime-core by knime.

the class DBWriterNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws CanceledExecutionException, Exception {
    exec.setProgress("Opening database connection to write data...");
    DatabaseConnectionSettings connSettings;
    if ((inData.length > 1) && (inData[1] instanceof DatabaseConnectionPortObject)) {
        connSettings = ((DatabaseConnectionPortObject) inData[1]).getConnectionSettings(getCredentialsProvider());
    } else {
        connSettings = m_conn;
    }
    DBWriter writer = connSettings.getUtility().getWriter(connSettings);
    BufferedDataTable inputTable = (BufferedDataTable) inData[0];
    DataTableRowInput rowInput = new DataTableRowInput(inputTable);
    // write entire data
    final String error = writer.writeData(m_tableName, rowInput, inputTable.size(), m_append, exec, m_types, getCredentialsProvider(), m_batchSize, m_insertNullForMissingCols, m_failOnError);
    // set error message generated during writing rows
    if (error != null) {
        super.setWarningMessage(error);
    }
    return new BufferedDataTable[0];
}
Also used : DatabaseConnectionPortObject(org.knime.core.node.port.database.DatabaseConnectionPortObject) DatabaseConnectionSettings(org.knime.core.node.port.database.DatabaseConnectionSettings) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataTableRowInput(org.knime.core.node.streamable.DataTableRowInput) DBWriter(org.knime.core.node.port.database.writer.DBWriter)

Example 5 with DatabaseConnectionPortObject

use of org.knime.core.node.port.database.DatabaseConnectionPortObject in project knime-core by knime.

the class DBWriterNodeModel method createStreamableOperator.

/**
 * {@inheritDoc}
 */
@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    return new StreamableOperator() {

        @Override
        public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
            exec.setProgress("Opening database connection to write data...");
            DatabaseConnectionSettings connSettings;
            PortObject portObj = ((PortObjectInput) inputs[1]).getPortObject();
            if (portObj != null && (portObj instanceof DatabaseConnectionPortObject)) {
                connSettings = ((DatabaseConnectionPortObject) portObj).getConnectionSettings(getCredentialsProvider());
            } else {
                connSettings = m_conn;
            }
            DBWriter writer = connSettings.getUtility().getWriter(connSettings);
            // write entire data
            m_errorMessage = writer.writeData(m_tableName, (RowInput) inputs[0], -1, m_append, exec, m_types, getCredentialsProvider(), m_batchSize, m_insertNullForMissingCols);
        }
    };
}
Also used : DatabaseConnectionPortObject(org.knime.core.node.port.database.DatabaseConnectionPortObject) ExecutionContext(org.knime.core.node.ExecutionContext) DatabaseConnectionSettings(org.knime.core.node.port.database.DatabaseConnectionSettings) StreamableOperator(org.knime.core.node.streamable.StreamableOperator) DBWriter(org.knime.core.node.port.database.writer.DBWriter) DatabaseConnectionPortObject(org.knime.core.node.port.database.DatabaseConnectionPortObject) PortObject(org.knime.core.node.port.PortObject) DataTableRowInput(org.knime.core.node.streamable.DataTableRowInput) RowInput(org.knime.core.node.streamable.RowInput) PortObjectInput(org.knime.core.node.streamable.PortObjectInput)

Aggregations

DatabaseConnectionPortObject (org.knime.core.node.port.database.DatabaseConnectionPortObject)12 DatabaseConnectionSettings (org.knime.core.node.port.database.DatabaseConnectionSettings)10 PortObject (org.knime.core.node.port.PortObject)6 DataTableSpec (org.knime.core.data.DataTableSpec)5 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)5 SQLException (java.sql.SQLException)4 BufferedDataTable (org.knime.core.node.BufferedDataTable)4 DBReader (org.knime.core.node.port.database.reader.DBReader)4 DBWriter (org.knime.core.node.port.database.writer.DBWriter)4 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)3 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)3 DatabaseQueryConnectionSettings (org.knime.core.node.port.database.DatabaseQueryConnectionSettings)3 IOException (java.io.IOException)2 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)2 ExecutionContext (org.knime.core.node.ExecutionContext)2 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)2 DatabaseConnectionPortObjectSpec (org.knime.core.node.port.database.DatabaseConnectionPortObjectSpec)2 DatabasePortObject (org.knime.core.node.port.database.DatabasePortObject)2 DatabasePortObjectSpec (org.knime.core.node.port.database.DatabasePortObjectSpec)2 DataTableRowInput (org.knime.core.node.streamable.DataTableRowInput)2