Search in sources :

Example 6 with SQLDatabasePersistAction

use of org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction in project dbeaver by serge-rider.

the class SQLIndexManager method addObjectCreateActions.

@Override
protected void addObjectCreateActions(List<DBEPersistAction> actions, ObjectCreateCommand command) {
    final TABLE_TYPE table = command.getObject().getTable();
    final OBJECT_TYPE index = command.getObject();
    // Create index
    final String indexName = DBUtils.getQuotedIdentifier(index.getDataSource(), index.getName());
    index.setName(indexName);
    StringBuilder decl = new StringBuilder(40);
    decl.append("CREATE ");
    if (index.isUnique()) {
        decl.append("UNIQUE ");
    }
    //$NON-NLS-1$
    decl.append("INDEX ").append(indexName).append(" ON ").append(//$NON-NLS-1$
    table.getFullyQualifiedName(DBPEvaluationContext.DDL)).append(//$NON-NLS-1$
    " (");
    try {
        // Get columns using void monitor
        boolean firstColumn = true;
        for (DBSTableIndexColumn indexColumn : CommonUtils.safeCollection(command.getObject().getAttributeReferences(VoidProgressMonitor.INSTANCE))) {
            //$NON-NLS-1$
            if (!firstColumn)
                decl.append(",");
            firstColumn = false;
            decl.append(indexColumn.getName());
            appendIndexColumnModifiers(decl, indexColumn);
        }
    } catch (DBException e) {
        log.error(e);
    }
    //$NON-NLS-1$
    decl.append(")");
    actions.add(new SQLDatabasePersistAction(ModelMessages.model_jdbc_create_new_index, decl.toString()));
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBSTableIndexColumn(org.jkiss.dbeaver.model.struct.rdb.DBSTableIndexColumn) SQLDatabasePersistAction(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction)

Example 7 with SQLDatabasePersistAction

use of org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction in project dbeaver by serge-rider.

the class SQLTableColumnManager method addObjectCreateActions.

@Override
protected void addObjectCreateActions(List<DBEPersistAction> actions, ObjectCreateCommand command) {
    final TABLE_TYPE table = command.getObject().getTable();
    actions.add(new SQLDatabasePersistAction(ModelMessages.model_jdbc_create_new_table_column, "ALTER TABLE " + table.getFullyQualifiedName(DBPEvaluationContext.DDL) + " ADD " + getNestedDeclaration(table, command)));
}
Also used : SQLDatabasePersistAction(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction)

Example 8 with SQLDatabasePersistAction

use of org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction in project dbeaver by serge-rider.

the class SQLiteTableManager method addObjectRenameActions.

@Override
protected void addObjectRenameActions(List<DBEPersistAction> actions, ObjectRenameCommand command) {
    final GenericDataSource dataSource = command.getObject().getDataSource();
    actions.add(new SQLDatabasePersistAction("Rename table", //$NON-NLS-1$
    "ALTER TABLE " + command.getObject().getFullyQualifiedName(DBPEvaluationContext.DDL) + " RENAME TO " + //$NON-NLS-1$
    DBUtils.getQuotedIdentifier(dataSource, command.getNewName())));
}
Also used : GenericDataSource(org.jkiss.dbeaver.ext.generic.model.GenericDataSource) SQLDatabasePersistAction(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction)

Example 9 with SQLDatabasePersistAction

use of org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction in project dbeaver by serge-rider.

the class ExecuteBatchImpl method processBatch.

/**
     * Execute batch OR generate batch script.
     * @param session    session
     * @param actions    script actions. If not null then no execution will be done
     * @return execution statistics
     * @throws DBCException
     */
@NotNull
private DBCStatistics processBatch(@NotNull DBCSession session, @Nullable List<DBEPersistAction> actions) throws DBCException {
    DBDValueHandler[] handlers = new DBDValueHandler[attributes.length];
    for (int i = 0; i < attributes.length; i++) {
        if (attributes[i] instanceof DBDAttributeBinding) {
            handlers[i] = ((DBDAttributeBinding) attributes[i]).getValueHandler();
        } else {
            handlers[i] = DBUtils.findValueHandler(session, attributes[i]);
        }
    }
    boolean useBatch = session.getDataSource().getInfo().supportsBatchUpdates() && reuseStatement;
    if (values.size() <= 1) {
        useBatch = false;
    }
    DBCStatistics statistics = new DBCStatistics();
    DBCStatement statement = null;
    try {
        // Here we'll try to reuse prepared statement.
        // It makes a great sense in case of data transfer where we need millions of inserts.
        // We must be aware of nulls because actual insert statements may differ depending on null values.
        // So if row nulls aren't the same as in previous row we need to prepare new statement and restart batch.
        // Quite complicated but works.
        boolean[] prevNulls = new boolean[attributes.length];
        boolean[] nulls = new boolean[attributes.length];
        int statementsInBatch = 0;
        for (Object[] rowValues : values) {
            boolean reuse = reuseStatement;
            if (reuse) {
                for (int i = 0; i < rowValues.length; i++) {
                    nulls[i] = DBUtils.isNullValue(rowValues[i]);
                }
                if (!Arrays.equals(prevNulls, nulls) && statementsInBatch > 0) {
                    reuse = false;
                }
                System.arraycopy(nulls, 0, prevNulls, 0, nulls.length);
                if (!reuse && statementsInBatch > 0) {
                    // Flush batch
                    if (actions == null) {
                        flushBatch(statistics, statement);
                    }
                    statement.close();
                    statement = null;
                    statementsInBatch = 0;
                    reuse = true;
                }
            }
            if (statement == null || !reuse) {
                statement = prepareStatement(session, rowValues);
                statistics.setQueryText(statement.getQueryString());
            }
            try {
                bindStatement(handlers, statement, rowValues);
                if (actions == null) {
                    if (useBatch) {
                        statement.addToBatch();
                        statementsInBatch++;
                    } else {
                        // Execute each row separately
                        long startTime = System.currentTimeMillis();
                        executeStatement(statement);
                        statistics.addExecuteTime(System.currentTimeMillis() - startTime);
                        long rowCount = statement.getUpdateRowCount();
                        if (rowCount > 0) {
                            statistics.addRowsUpdated(rowCount);
                        }
                        // Read keys
                        if (keysReceiver != null) {
                            readKeys(statement.getSession(), statement, keysReceiver);
                        }
                    }
                } else {
                    String queryString;
                    if (statement instanceof DBCParameterizedStatement) {
                        queryString = ((DBCParameterizedStatement) statement).getFormattedQuery();
                    } else {
                        queryString = statement.getQueryString();
                    }
                    actions.add(new SQLDatabasePersistAction("Execute statement", queryString));
                }
            } finally {
                if (!reuse) {
                    statement.close();
                }
            }
        }
        values.clear();
        if (statementsInBatch > 0) {
            if (actions == null) {
                flushBatch(statistics, statement);
            }
            statement.close();
            statement = null;
        }
    } finally {
        if (reuseStatement && statement != null) {
            statement.close();
        }
    }
    return statistics;
}
Also used : DBDValueHandler(org.jkiss.dbeaver.model.data.DBDValueHandler) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) SQLDatabasePersistAction(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction) NotNull(org.jkiss.code.NotNull)

Example 10 with SQLDatabasePersistAction

use of org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction in project dbeaver by serge-rider.

the class DB2TableColumnManager method addObjectRenameActions.

@Override
protected void addObjectRenameActions(List<DBEPersistAction> actions, ObjectRenameCommand command) {
    final DB2TableColumn column = command.getObject();
    actions.add(new SQLDatabasePersistAction("Rename column", "ALTER TABLE " + column.getTable().getFullyQualifiedName(DBPEvaluationContext.DDL) + " RENAME COLUMN " + DBUtils.getQuotedIdentifier(column.getDataSource(), command.getOldName()) + " TO " + DBUtils.getQuotedIdentifier(column.getDataSource(), command.getNewName())));
}
Also used : DB2TableColumn(org.jkiss.dbeaver.ext.db2.model.DB2TableColumn) SQLDatabasePersistAction(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction)

Aggregations

SQLDatabasePersistAction (org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction)55 DBEPersistAction (org.jkiss.dbeaver.model.edit.DBEPersistAction)10 DBException (org.jkiss.dbeaver.DBException)3 MySQLTableColumn (org.jkiss.dbeaver.ext.mysql.model.MySQLTableColumn)3 DB2TableColumn (org.jkiss.dbeaver.ext.db2.model.DB2TableColumn)2 ExasolTableColumn (org.jkiss.dbeaver.ext.exasol.model.ExasolTableColumn)2 OracleTableColumn (org.jkiss.dbeaver.ext.oracle.model.OracleTableColumn)2 ArrayList (java.util.ArrayList)1 NotNull (org.jkiss.code.NotNull)1 ExasolView (org.jkiss.dbeaver.ext.exasol.model.ExasolView)1 GenericDataSource (org.jkiss.dbeaver.ext.generic.model.GenericDataSource)1 MySQLCatalog (org.jkiss.dbeaver.ext.mysql.model.MySQLCatalog)1 MySQLDataSource (org.jkiss.dbeaver.ext.mysql.model.MySQLDataSource)1 MySQLView (org.jkiss.dbeaver.ext.mysql.model.MySQLView)1 OracleDataType (org.jkiss.dbeaver.ext.oracle.model.OracleDataType)1 OracleUser (org.jkiss.dbeaver.ext.oracle.model.OracleUser)1 OracleView (org.jkiss.dbeaver.ext.oracle.model.OracleView)1 PostgreDatabase (org.jkiss.dbeaver.ext.postgresql.model.PostgreDatabase)1 PostgreRole (org.jkiss.dbeaver.ext.postgresql.model.PostgreRole)1 PostgreSchema (org.jkiss.dbeaver.ext.postgresql.model.PostgreSchema)1