Search in sources :

Example 41 with DBEPersistAction

use of org.jkiss.dbeaver.model.edit.DBEPersistAction in project dbeaver by dbeaver.

the class ExasolTableColumnManager method addObjectModifyActions.

// -----
// Alter
// -----
@Override
protected void addObjectModifyActions(DBRProgressMonitor monitor, DBCExecutionContext executionContext, List<DBEPersistAction> actionList, ObjectChangeCommand command, Map<String, Object> options) {
    ExasolTableColumn exasolColumn = command.getObject();
    Map<Object, Object> props = command.getProperties();
    if (props.containsKey("defaultValue") || props.containsKey("dataType") || props.containsKey("scale") || props.containsKey("maxLength") || props.containsKey("autoGenerated") || props.containsKey("identityValue") || props.containsKey("required")) {
        // build nullability string
        String nullability = "";
        if (exasolColumn.isOriRequired() != null && exasolColumn.isOriRequired() != exasolColumn.isRequired())
            nullability = exasolColumn.isRequired() ? "NOT NULL" : "NULL";
        final String deltaSQL = DBUtils.getQuotedIdentifier(exasolColumn) + " " + exasolColumn.getFormatType() + " " + (exasolColumn.getDefaultValue() == null ? "" : " DEFAULT " + exasolColumn.getDefaultValue()) + " " + formatIdentiy(exasolColumn.isAutoGenerated(), exasolColumn.getIdentityValue()) + " " + nullability;
        if (!deltaSQL.isEmpty()) {
            String sqlAlterColumn = String.format(SQL_ALTER, exasolColumn.getTable().getFullyQualifiedName(DBPEvaluationContext.DDL), deltaSQL);
            actionList.add(new SQLDatabasePersistAction(CMD_ALTER, sqlAlterColumn));
        }
    }
    // Comment
    DBEPersistAction commentAction = buildCommentAction(exasolColumn);
    if (commentAction != null) {
        actionList.add(commentAction);
    }
    if (command.getProperties().containsKey("distKey")) {
        try {
            actionList.addAll(modifyDistKey(monitor, exasolColumn));
        } catch (DBException e) {
            log.error("Failed to modify distkey settings", e);
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) ExasolTableColumn(org.jkiss.dbeaver.ext.exasol.model.ExasolTableColumn) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) SQLDatabasePersistAction(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction)

Example 42 with DBEPersistAction

use of org.jkiss.dbeaver.model.edit.DBEPersistAction in project dbeaver by dbeaver.

the class ExasolTableManager method addStructObjectCreateActions.

@Override
public void addStructObjectCreateActions(DBRProgressMonitor monitor, DBCExecutionContext executionContext, List<DBEPersistAction> actions, StructCreateCommand command, Map<String, Object> options) throws DBException {
    super.addStructObjectCreateActions(monitor, executionContext, actions, command, options);
    // Eventually add Comment
    DBEPersistAction commentAction = buildCommentAction(command.getObject());
    if (commentAction != null) {
        actions.add(commentAction);
    }
}
Also used : DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction)

Example 43 with DBEPersistAction

use of org.jkiss.dbeaver.model.edit.DBEPersistAction in project dbeaver by dbeaver.

the class ExasolTableManager method addObjectModifyActions.

// ------
// Alter
// ------
@Override
public void addObjectModifyActions(DBRProgressMonitor monitor, DBCExecutionContext executionContext, List<DBEPersistAction> actionList, ObjectChangeCommand command, Map<String, Object> options) {
    ExasolTable exasolTable = command.getObject();
    if (command.getProperties().size() > 0) {
        if (command.getProperties().containsKey("hasPartitionKey") && ((command.getProperties().get("hasPartitionKey").toString()).equals("false"))) {
            actionList.add(new SQLDatabasePersistAction("ALTER TABLE " + exasolTable.getFullyQualifiedName(DBPEvaluationContext.DDL) + " DROP PARTITION KEYS"));
        } else if (command.getProperties().size() > 1) {
            StringBuilder sb = new StringBuilder(128);
            sb.append(SQL_ALTER);
            sb.append(exasolTable.getFullyQualifiedName(DBPEvaluationContext.DDL));
            sb.append(" ");
            appendTableModifiers(monitor, command.getObject(), command, sb, true);
            actionList.add(new SQLDatabasePersistAction(CMD_ALTER, sb.toString()));
        }
    }
    DBEPersistAction commentAction = buildCommentAction(exasolTable);
    if (commentAction != null) {
        actionList.add(commentAction);
    }
}
Also used : DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) SQLDatabasePersistAction(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction)

Example 44 with DBEPersistAction

use of org.jkiss.dbeaver.model.edit.DBEPersistAction in project dbeaver by dbeaver.

the class PostgreProcedure method getObjectDefinitionText.

@Override
@Property(hidden = true, editable = true, updatable = true, order = -1)
public String getObjectDefinitionText(DBRProgressMonitor monitor, Map<String, Object> options) throws DBException {
    String procDDL;
    boolean omitHeader = CommonUtils.getOption(options, OPTION_DEBUGGER_SOURCE);
    if (isPersisted() && (!getDataSource().getServerType().supportsFunctionDefRead() || omitHeader)) {
        if (procSrc == null) {
            try (JDBCSession session = DBUtils.openMetaSession(monitor, this, "Read procedure body")) {
                procSrc = JDBCUtils.queryString(session, "SELECT prosrc FROM pg_proc where oid = ?", getObjectId());
            } catch (SQLException e) {
                throw new DBException("Error reading procedure body", e);
            }
        }
        PostgreDataType returnType = getReturnType();
        String returnTypeName = returnType == null ? null : returnType.getFullTypeName();
        procDDL = omitHeader ? procSrc : generateFunctionDeclaration(getLanguage(monitor), returnTypeName, procSrc);
    } else {
        if (body == null) {
            if (!isPersisted()) {
                PostgreDataType returnType = getReturnType();
                String returnTypeName = returnType == null ? null : returnType.getFullTypeName();
                body = generateFunctionDeclaration(getLanguage(monitor), returnTypeName, "\n\t-- Enter function body here\n");
            } else if (oid == 0 || isAggregate) {
                // No OID so let's use old (bad) way
                body = this.procSrc;
            } else {
                if (isAggregate) {
                    body = "-- Aggregate function";
                } else {
                    try (JDBCSession session = DBUtils.openMetaSession(monitor, this, "Read procedure body")) {
                        body = JDBCUtils.queryString(session, "SELECT pg_get_functiondef(" + getObjectId() + ")");
                    } catch (SQLException e) {
                        if (!CommonUtils.isEmpty(this.procSrc)) {
                            log.debug("Error reading procedure body", e);
                            // At least we have it
                            body = this.procSrc;
                        } else {
                            throw new DBException("Error reading procedure body", e);
                        }
                    }
                }
            }
        }
        procDDL = body;
    }
    if (this.isPersisted() && !omitHeader) {
        procDDL += ";\n";
        if (CommonUtils.getOption(options, DBPScriptObject.OPTION_INCLUDE_COMMENTS) && !CommonUtils.isEmpty(getDescription())) {
            procDDL += "\nCOMMENT ON " + getProcedureTypeName() + " " + getFullQualifiedSignature() + " IS " + SQLUtils.quoteString(this, getDescription()) + ";\n";
        }
        if (CommonUtils.getOption(options, DBPScriptObject.OPTION_INCLUDE_PERMISSIONS)) {
            List<DBEPersistAction> actions = new ArrayList<>();
            PostgreUtils.getObjectGrantPermissionActions(monitor, this, actions, options);
            procDDL += "\n" + SQLUtils.generateScript(getDataSource(), actions.toArray(new DBEPersistAction[0]), false);
        }
    }
    return procDDL;
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) Property(org.jkiss.dbeaver.model.meta.Property)

Example 45 with DBEPersistAction

use of org.jkiss.dbeaver.model.edit.DBEPersistAction in project dbeaver by dbeaver.

the class SQLToolExecuteHandler method executeTool.

private void executeTool(DBRProgressMonitor monitor, DBTTask task, SETTINGS settings, Log log, PrintStream outLog, DBTTaskExecutionListener listener) throws DBException, IOException {
    List<OBJECT_TYPE> objectList = settings.getObjectList();
    Exception lastError = null;
    listener.taskStarted(settings);
    try {
        monitor.beginTask("Execute tool '" + task.getType().getName() + "'", objectList.size());
        List<Throwable> warnings = settings.getWarnings();
        if (!warnings.isEmpty()) {
            Throwable throwable = warnings.get(0);
            throw new DBCException("Tool execution error: " + throwable.getMessage(), throwable);
        }
        for (OBJECT_TYPE object : objectList) {
            monitor.subTask("Process [" + DBUtils.getObjectFullName(object, DBPEvaluationContext.UI) + "]");
            try (DBCSession session = DBUtils.openUtilSession(monitor, object, "Execute " + task.getType().getName())) {
                List<DBEPersistAction> queries = new ArrayList<>();
                generateObjectQueries(session, settings, queries, object);
                DBCExecutionContext context = session.getExecutionContext();
                DBCTransactionManager txnManager = DBUtils.getTransactionManager(context);
                boolean isAutoCommitModeSwitchedOn = true;
                try {
                    if (isRunInAutoCommit() && txnManager != null && !txnManager.isAutoCommit()) {
                        isAutoCommitModeSwitchedOn = false;
                        txnManager.setAutoCommit(monitor, true);
                    }
                    for (DBEPersistAction action : queries) {
                        if (monitor.isCanceled()) {
                            break;
                        }
                        if (!CommonUtils.isEmpty(action.getTitle())) {
                            monitor.subTask(action.getTitle());
                        }
                        try {
                            if (action instanceof SQLDatabasePersistActionComment) {
                                continue;
                            }
                            String script = action.getScript();
                            if (!CommonUtils.isEmpty(script)) {
                                long startTime = System.currentTimeMillis();
                                try (final DBCStatement statement = session.prepareStatement(DBCStatementType.SCRIPT, script, false, false, false)) {
                                    long execTime = System.currentTimeMillis() - startTime;
                                    statement.executeStatement();
                                    if (listener instanceof SQLToolRunListener) {
                                        if (action.getType() != DBEPersistAction.ActionType.INITIALIZER && action.getType() != DBEPersistAction.ActionType.FINALIZER) {
                                            SQLToolStatisticsSimple statisticsSimple = new SQLToolStatisticsSimple(object, false);
                                            if (SQLToolExecuteHandler.this instanceof SQLToolRunStatisticsGenerator) {
                                                List<? extends SQLToolStatistics> executeStatistics = ((SQLToolRunStatisticsGenerator) SQLToolExecuteHandler.this).getExecuteStatistics(object, settings, action, session, statement);
                                                monitor.subTask("\tFinished in " + RuntimeUtils.formatExecutionTime(execTime));
                                                if (!CommonUtils.isEmpty(executeStatistics)) {
                                                    for (SQLToolStatistics stat : executeStatistics) {
                                                        stat.setExecutionTime(execTime);
                                                    }
                                                    ((SQLToolRunListener) listener).handleActionStatistics(object, action, session, executeStatistics);
                                                } else {
                                                    ((SQLToolRunListener) listener).handleActionStatistics(object, action, session, Collections.singletonList(statisticsSimple));
                                                }
                                            } else {
                                                ((SQLToolRunListener) listener).handleActionStatistics(object, action, session, Collections.singletonList(statisticsSimple));
                                            }
                                        }
                                    }
                                }
                            }
                        } catch (Exception e) {
                            lastError = e;
                            log.debug("Error executing query", e);
                            outLog.println("Error executing query\n" + e.getMessage());
                            if (listener instanceof SQLToolRunListener) {
                                SQLToolStatisticsSimple errorStat = new SQLToolStatisticsSimple(object, true);
                                errorStat.setStatusMessage(e.getMessage());
                                ((SQLToolRunListener) listener).handleActionStatistics(object, action, session, Collections.singletonList(errorStat));
                            }
                        } finally {
                            monitor.worked(1);
                        }
                    }
                } finally {
                    if (!isAutoCommitModeSwitchedOn) {
                        try {
                            txnManager.setAutoCommit(monitor, false);
                        } catch (DBCException e) {
                            log.debug("Cannot set auto-commit status", e);
                        }
                    }
                }
            }
            monitor.worked(1);
        }
    } catch (Exception e) {
        lastError = e;
        outLog.println("Process error\n" + e.getMessage());
    } finally {
        monitor.done();
    }
    listener.taskFinished(settings, lastError);
    outLog.println("Tool execution finished");
    outLog.flush();
}
Also used : SQLDatabasePersistActionComment(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionComment) ArrayList(java.util.ArrayList) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction)

Aggregations

DBEPersistAction (org.jkiss.dbeaver.model.edit.DBEPersistAction)94 SQLDatabasePersistAction (org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction)44 DBException (org.jkiss.dbeaver.DBException)24 InvocationTargetException (java.lang.reflect.InvocationTargetException)16 ArrayList (java.util.ArrayList)14 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)10 UIServiceSQL (org.jkiss.dbeaver.runtime.ui.UIServiceSQL)10 DBERegistry (org.jkiss.dbeaver.model.edit.DBERegistry)8 OracleObjectPersistAction (org.jkiss.dbeaver.ext.oracle.model.OracleObjectPersistAction)6 DBPEvent (org.jkiss.dbeaver.model.DBPEvent)6 DBECommand (org.jkiss.dbeaver.model.edit.DBECommand)6 DBECommandContext (org.jkiss.dbeaver.model.edit.DBECommandContext)6 DBCException (org.jkiss.dbeaver.model.exec.DBCException)6 DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)6 DBCStatement (org.jkiss.dbeaver.model.exec.DBCStatement)6 SQLDatabasePersistActionComment (org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionComment)6 SQLObjectEditor (org.jkiss.dbeaver.model.impl.sql.edit.SQLObjectEditor)6 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)6 DBSObjectState (org.jkiss.dbeaver.model.struct.DBSObjectState)6 SQLException (java.sql.SQLException)4