use of org.jkiss.dbeaver.model.edit.DBEPersistAction in project dbeaver by dbeaver.
the class PostgreFDWConfigWizard method generateScript.
List<DBEPersistAction> generateScript(DBRProgressMonitor monitor) throws DBException {
PostgreDatabase database = getDatabase();
PostgreDataSource curDataSource = database.getDataSource();
List<DBEPersistAction> actions = new ArrayList<>();
PostgreFDWConfigWizard.FDWInfo selectedFDW = getSelectedFDW();
PropertySourceCustom propertySource = getFdwPropertySource();
Map<String, Object> propValues = propertySource.getPropertiesWithDefaults();
String serverId = getFdwServerId();
actions.add(new SQLDatabasePersistActionComment(curDataSource, "CREATE EXTENSION " + selectedFDW.getId()));
{
StringBuilder script = new StringBuilder();
script.append("CREATE SERVER ").append(serverId).append("\n\tFOREIGN DATA WRAPPER ").append(selectedFDW.getId()).append("\n\tOPTIONS(");
boolean firstProp = true;
for (Map.Entry<String, Object> pe : propValues.entrySet()) {
String propName = CommonUtils.toString(pe.getKey());
String propValue = CommonUtils.toString(pe.getValue());
if (CommonUtils.isEmpty(propName) || CommonUtils.isEmpty(propValue)) {
continue;
}
if (!firstProp)
script.append(", ");
script.append(propName).append(" '").append(propValue).append("'");
firstProp = false;
}
script.append(")");
actions.add(new SQLDatabasePersistAction("Create extension", script.toString()));
}
actions.add(new SQLDatabasePersistAction("CREATE USER MAPPING FOR CURRENT_USER SERVER " + serverId));
// Now tables
DBECommandContext commandContext = new SimpleCommandContext(getExecutionContext(), false);
try {
PostgreFDWConfigWizard.FDWInfo fdwInfo = getSelectedFDW();
Map<String, Object> options = new HashMap<>();
options.put(SQLObjectEditor.OPTION_SKIP_CONFIGURATION, true);
PostgreForeignTableManager tableManager = new PostgreForeignTableManager();
PostgreTableColumnManager columnManager = new PostgreTableColumnManager();
for (DBNDatabaseNode tableNode : getSelectedEntities()) {
DBSEntity entity = (DBSEntity) tableNode.getObject();
PostgreTableForeign pgTable = (PostgreTableForeign) tableManager.createNewObject(monitor, commandContext, getSelectedSchema(), null, options);
if (pgTable == null) {
log.error("Internal error while creating new table");
continue;
}
pgTable.setName(entity.getName());
pgTable.setForeignServerName(serverId);
pgTable.setForeignOptions(new String[0]);
for (DBSEntityAttribute attr : CommonUtils.safeCollection(entity.getAttributes(monitor))) {
// Cache data types
PostgreSchema catalogSchema = database.getCatalogSchema(monitor);
if (catalogSchema != null) {
catalogSchema.getDataTypes(monitor);
}
String defTypeName = DBStructUtils.mapTargetDataType(database, attr, true);
String plainTargetTypeName = SQLUtils.stripColumnTypeModifiers(defTypeName);
PostgreDataType dataType = database.getDataType(monitor, plainTargetTypeName);
if (dataType == null) {
log.error("Data type '" + plainTargetTypeName + "' not found. Skip column mapping.");
continue;
}
PostgreTableColumn newColumn = columnManager.createNewObject(monitor, commandContext, pgTable, null, options);
assert newColumn != null;
newColumn.setName(attr.getName());
newColumn.setDataType(dataType);
}
DBEPersistAction[] tableDDL = tableManager.getTableDDL(monitor, pgTable, options);
Collections.addAll(actions, tableDDL);
}
} finally {
commandContext.resetChanges(true);
}
// CREATE SERVER clickhouse_svr FOREIGN DATA WRAPPER clickhousedb_fdw OPTIONS(dbname 'default', driver '/usr/local/lib/odbc/libclickhouseodbc.so', host '46.101.202.143');
return actions;
}
use of org.jkiss.dbeaver.model.edit.DBEPersistAction in project dbeaver by dbeaver.
the class ACLCommandChangePrivilege method getPersistActions.
@Override
public DBEPersistAction[] getPersistActions(DBRProgressMonitor monitor, DBCExecutionContext executionContext, Map<String, Object> options) {
DBAPrivilegeOwner object = getObject();
String grantScript = aclManager.generatePermissionChangeScript(monitor, object, grant, privilege, privilegeTypes, options);
return new DBEPersistAction[] { new SQLDatabasePersistAction(ACLMessages.edit_command_grant_privilege_action_grant_privilege, grantScript) };
}
use of org.jkiss.dbeaver.model.edit.DBEPersistAction in project dbeaver by dbeaver.
the class CompileHandler method compileUnit.
public static boolean compileUnit(DBRProgressMonitor monitor, DBCCompileLog compileLog, OracleSourceObject unit) throws DBCException {
final DBEPersistAction[] compileActions = unit.getCompileActions(monitor);
if (ArrayUtils.isEmpty(compileActions)) {
throw new DBCException("No compile actions associated with " + unit.getSourceType().name());
}
try (JDBCSession session = DBUtils.openUtilSession(monitor, unit, "Compile '" + unit.getName() + "'")) {
boolean success = true;
for (DBEPersistAction action : compileActions) {
final String script = action.getScript();
compileLog.trace(script);
if (monitor.isCanceled()) {
break;
}
try {
try (DBCStatement dbStat = session.prepareStatement(DBCStatementType.QUERY, script, false, false, false)) {
action.beforeExecute(session);
dbStat.executeStatement();
}
action.afterExecute(session, null);
} catch (DBCException e) {
action.afterExecute(session, e);
throw e;
}
if (action instanceof OracleObjectPersistAction) {
if (!logObjectErrors(session, compileLog, unit, ((OracleObjectPersistAction) action).getObjectType())) {
success = false;
}
}
}
final DBSObjectState oldState = unit.getObjectState();
unit.refreshObjectState(monitor);
if (unit.getObjectState() != oldState) {
unit.getDataSource().getContainer().fireEvent(new DBPEvent(DBPEvent.Action.OBJECT_UPDATE, unit));
}
return success;
}
}
use of org.jkiss.dbeaver.model.edit.DBEPersistAction in project dbeaver by dbeaver.
the class JobRunHandler method runJob.
public static boolean runJob(DBRProgressMonitor monitor, DBCCompileLog compileLog, OracleSchedulerJob job) throws DBCException {
final DBEPersistAction[] compileActions = job.getRunActions();
if (ArrayUtils.isEmpty(compileActions)) {
return true;
}
try (JDBCSession session = DBUtils.openUtilSession(monitor, job, "Run '" + job.getName() + "'")) {
boolean success = true;
for (DBEPersistAction action : compileActions) {
final String script = action.getScript();
compileLog.trace(script);
if (monitor.isCanceled()) {
break;
}
try {
try (DBCStatement dbStat = session.prepareStatement(DBCStatementType.SCRIPT, script, false, false, false)) {
action.beforeExecute(session);
dbStat.executeStatement();
}
action.afterExecute(session, null);
} catch (DBCException e) {
action.afterExecute(session, e);
throw e;
}
if (action instanceof OracleObjectPersistAction) {
if (!logObjectErrors(session, compileLog, job, ((OracleObjectPersistAction) action).getObjectType())) {
success = false;
}
}
}
final DBSObjectState oldState = job.getObjectState();
job.refreshObjectState(monitor);
if (job.getObjectState() != oldState) {
job.getDataSource().getContainer().fireEvent(new DBPEvent(DBPEvent.Action.OBJECT_UPDATE, job));
}
return success;
}
}
use of org.jkiss.dbeaver.model.edit.DBEPersistAction in project dbeaver by dbeaver.
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]);
}
Aggregations