use of org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionComment in project dbeaver by serge-rider.
the class SQLTableManager method getTableDDL.
public DBEPersistAction[] getTableDDL(DBRProgressMonitor monitor, OBJECT_TYPE table, Map<String, Object> options) throws DBException {
List<DBEPersistAction> actions = new ArrayList<>();
final DBERegistry editorsRegistry = table.getDataSource().getContainer().getPlatform().getEditorsRegistry();
SQLObjectEditor<DBSEntityAttribute, OBJECT_TYPE> tcm = getObjectEditor(editorsRegistry, DBSEntityAttribute.class);
SQLObjectEditor<DBSEntityConstraint, OBJECT_TYPE> pkm = getObjectEditor(editorsRegistry, DBSEntityConstraint.class);
SQLObjectEditor<DBSTableForeignKey, OBJECT_TYPE> fkm = getObjectEditor(editorsRegistry, DBSTableForeignKey.class);
SQLObjectEditor<DBSTableIndex, OBJECT_TYPE> im = getObjectEditor(editorsRegistry, DBSTableIndex.class);
DBCExecutionContext executionContext = DBUtils.getDefaultContext(table, true);
if (CommonUtils.getOption(options, DBPScriptObject.OPTION_DDL_ONLY_FOREIGN_KEYS)) {
if (fkm != null) {
// Create only foreign keys
try {
for (DBSEntityAssociation foreignKey : CommonUtils.safeCollection(table.getAssociations(monitor))) {
if (!(foreignKey instanceof DBSTableForeignKey) || DBUtils.isHiddenObject(foreignKey) || DBUtils.isInheritedObject(foreignKey)) {
continue;
}
DBEPersistAction[] cmdActions = fkm.makeCreateCommand((DBSTableForeignKey) foreignKey, options).getPersistActions(monitor, executionContext, options);
if (cmdActions != null) {
Collections.addAll(actions, cmdActions);
}
}
} catch (DBException e) {
// Ignore primary keys
log.debug(e);
}
}
return actions.toArray(new DBEPersistAction[0]);
}
if (isIncludeDropInDDL()) {
actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Drop table"));
for (DBEPersistAction delAction : new ObjectDeleteCommand(table, ModelMessages.model_jdbc_delete_object).getPersistActions(monitor, executionContext, options)) {
String script = delAction.getScript();
String delimiter = SQLUtils.getScriptLineDelimiter(SQLUtils.getDialectFromObject(table));
if (!script.endsWith(delimiter)) {
script += delimiter;
}
actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), script));
}
}
StructCreateCommand command = makeCreateCommand(table, options);
if (tcm != null) {
// Aggregate nested column, constraint and index commands
for (DBSEntityAttribute column : CommonUtils.safeCollection(table.getAttributes(monitor))) {
if (DBUtils.isHiddenObject(column) || DBUtils.isInheritedObject(column)) {
// Do not include hidden (pseudo?) and inherited columns in DDL
continue;
}
command.aggregateCommand(tcm.makeCreateCommand(column, options));
}
}
if (pkm != null) {
try {
for (DBSEntityConstraint constraint : CommonUtils.safeCollection(table.getConstraints(monitor))) {
if (DBUtils.isHiddenObject(constraint) || DBUtils.isInheritedObject(constraint)) {
continue;
}
command.aggregateCommand(pkm.makeCreateCommand(constraint, options));
}
} catch (DBException e) {
// Ignore primary keys
log.debug(e);
}
}
if (fkm != null && !CommonUtils.getOption(options, DBPScriptObject.OPTION_DDL_SKIP_FOREIGN_KEYS)) {
try {
for (DBSEntityAssociation foreignKey : CommonUtils.safeCollection(table.getAssociations(monitor))) {
if (!(foreignKey instanceof DBSTableForeignKey) || DBUtils.isHiddenObject(foreignKey) || DBUtils.isInheritedObject(foreignKey)) {
continue;
}
command.aggregateCommand(fkm.makeCreateCommand((DBSTableForeignKey) foreignKey, options));
}
} catch (DBException e) {
// Ignore primary keys
log.debug(e);
}
}
if (im != null && table instanceof DBSTable) {
try {
for (DBSTableIndex index : CommonUtils.safeCollection(((DBSTable) table).getIndexes(monitor))) {
if (!isIncludeIndexInDDL(monitor, index)) {
continue;
}
command.aggregateCommand(im.makeCreateCommand(index, options));
}
} catch (DBException e) {
// Ignore indexes
log.debug(e);
}
}
addExtraDDLCommands(monitor, table, options, command);
Collections.addAll(actions, command.getPersistActions(monitor, executionContext, options));
return actions.toArray(new DBEPersistAction[0]);
}
use of org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionComment in project dbeaver by serge-rider.
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();
}
use of org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionComment in project dbeaver by dbeaver.
the class PostgreUtils method getObjectGrantPermissionActions.
public static void getObjectGrantPermissionActions(DBRProgressMonitor monitor, PostgrePrivilegeOwner object, List<DBEPersistAction> actions, Map<String, Object> options) throws DBException {
if (object.isPersisted() && CommonUtils.getOption(options, DBPScriptObject.OPTION_INCLUDE_PERMISSIONS)) {
DBCExecutionContext executionContext = DBUtils.getDefaultContext(object, true);
actions.add(new SQLDatabasePersistActionComment(object.getDataSource(), "Permissions"));
// Owner
PostgreRole owner = object.getOwner(monitor);
if (owner != null) {
String alterScript = object.generateChangeOwnerQuery(DBUtils.getQuotedIdentifier(owner));
if (!CommonUtils.isEmpty(alterScript)) {
actions.add(new SQLDatabasePersistAction("Owner change", alterScript));
}
}
// Permissions
Collection<PostgrePrivilege> permissions = object.getPrivileges(monitor, true);
if (!CommonUtils.isEmpty(permissions)) {
for (PostgrePrivilege permission : permissions) {
if (permission.hasAllPrivileges(object)) {
Collections.addAll(actions, new PostgreCommandGrantPrivilege(permission.getOwner(), true, object, permission, new PostgrePrivilegeType[] { PostgrePrivilegeType.ALL }).getPersistActions(monitor, executionContext, options));
} else {
PostgreCommandGrantPrivilege grant = new PostgreCommandGrantPrivilege(permission.getOwner(), true, object, permission, permission.getPrivileges());
Collections.addAll(actions, grant.getPersistActions(monitor, executionContext, options));
}
}
}
}
}
use of org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionComment in project dbeaver by dbeaver.
the class PostgreTableManagerBase method addObjectExtraActions.
@Override
protected void addObjectExtraActions(DBRProgressMonitor monitor, DBCExecutionContext executionContext, List<DBEPersistAction> actions, NestedObjectCommand<PostgreTableBase, PropertyHandler> command, Map<String, Object> options) {
boolean isDDL = CommonUtils.getOption(options, DBPScriptObject.OPTION_DDL_SOURCE);
PostgreTableBase table = command.getObject();
// Add comments
String comment;
if (!table.isPersisted()) {
Object descProp = command.getProperty(DBConstants.PROP_ID_DESCRIPTION);
comment = descProp != null ? descProp.toString() : null;
} else {
comment = table.getDescription();
}
boolean showComments = CommonUtils.getOption(options, DBPScriptObject.OPTION_INCLUDE_COMMENTS) || CommonUtils.getOption(options, DBPScriptObject.OPTION_OBJECT_SAVE);
if (showComments && !CommonUtils.isEmpty(comment)) {
actions.add(new SQLDatabasePersistAction("Comment table", "COMMENT ON " + (table.isView() ? ((PostgreViewBase) table).getViewType() : "TABLE") + " " + table.getFullyQualifiedName(DBPEvaluationContext.DDL) + " IS " + SQLUtils.quoteString(table, CommonUtils.notEmpty(comment))));
}
if (isDDL || !table.isPersisted()) {
// show comment commands for DDL and new objects
try {
if (showComments) {
// Column comments
boolean hasComments = false;
for (PostgreTableColumn column : CommonUtils.safeCollection(table.getAttributes(monitor))) {
if (!CommonUtils.isEmpty(column.getDescription())) {
if (!hasComments) {
actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Column comments"));
}
PostgreTableColumnManager.addColumnCommentAction(actions, column);
hasComments = true;
}
}
}
if (showComments) {
// Constraint comments
boolean hasComments = false;
for (PostgreTableConstraintBase constr : CommonUtils.safeCollection(table.getConstraints(monitor))) {
if (!CommonUtils.isEmpty(constr.getDescription())) {
if (!hasComments) {
actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Constraint comments"));
}
PostgreConstraintManager.addConstraintCommentAction(actions, constr);
hasComments = true;
}
}
for (DBSEntityAssociation fk : CommonUtils.safeCollection(table.getAssociations(monitor))) {
if (fk instanceof PostgreTableForeignKey && !CommonUtils.isEmpty(fk.getDescription())) {
if (!hasComments) {
actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Foreign key comments"));
}
PostgreConstraintManager.addConstraintCommentAction(actions, (PostgreTableForeignKey) fk);
hasComments = true;
}
}
}
// Triggers
if (table instanceof PostgreTableReal) {
Collection<PostgreTrigger> triggers = ((PostgreTableReal) table).getTriggers(monitor);
if (!CommonUtils.isEmpty(triggers)) {
actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Table Triggers"));
for (PostgreTrigger trigger : triggers) {
actions.add(new SQLDatabasePersistAction("Create trigger", trigger.getObjectDefinitionText(monitor, options)));
}
}
}
// Rules
if (table instanceof PostgreTableReal) {
Collection<PostgreRule> rules = ((PostgreTableReal) table).getRules(monitor);
if (!CommonUtils.isEmpty(rules)) {
actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Table Rules"));
for (PostgreRule rule : rules) {
actions.add(new SQLDatabasePersistAction("Create rule", rule.getObjectDefinitionText(monitor, options)));
}
}
}
if (isDDL) {
PostgreUtils.getObjectGrantPermissionActions(monitor, table, actions, options);
}
} catch (DBException e) {
log.error(e);
}
}
}
Aggregations