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);
}
}
}
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);
}
}
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);
}
}
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;
}
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();
}
Aggregations