use of org.jkiss.dbeaver.model.struct.rdb.DBSTableForeignKey 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<DBSTableConstraint, OBJECT_TYPE> pkm = getObjectEditor(editorsRegistry, DBSTableConstraint.class);
SQLObjectEditor<DBSTableForeignKey, OBJECT_TYPE> fkm = getObjectEditor(editorsRegistry, DBSTableForeignKey.class);
SQLObjectEditor<DBSTableIndex, OBJECT_TYPE> im = getObjectEditor(editorsRegistry, DBSTableIndex.class);
if (isIncludeDropInDDL()) {
actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Drop table"));
for (DBEPersistAction delAction : new ObjectDeleteCommand(table, ModelMessages.model_jdbc_delete_object).getPersistActions(options)) {
actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), delAction.getScript()));
}
}
StructCreateCommand command = makeCreateCommand(table);
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));
}
}
if (pkm != null) {
try {
for (DBSTableConstraint constraint : CommonUtils.safeCollection(table.getConstraints(monitor))) {
if (DBUtils.isHiddenObject(constraint) || DBUtils.isInheritedObject(constraint)) {
continue;
}
command.aggregateCommand(pkm.makeCreateCommand(constraint));
}
} catch (DBException e) {
// Ignore primary keys
log.debug(e);
}
}
if (fkm != null) {
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));
}
} catch (DBException e) {
// Ignore primary keys
log.debug(e);
}
}
if (im != null) {
try {
for (DBSTableIndex index : CommonUtils.safeCollection(table.getIndexes(monitor))) {
if (DBUtils.isHiddenObject(index) || DBUtils.isInheritedObject(index)) {
continue;
}
command.aggregateCommand(im.makeCreateCommand(index));
}
} catch (DBException e) {
// Ignore indexes
log.debug(e);
}
}
Collections.addAll(actions, command.getPersistActions(options));
return actions.toArray(new DBEPersistAction[actions.size()]);
}
use of org.jkiss.dbeaver.model.struct.rdb.DBSTableForeignKey in project dbeaver by serge-rider.
the class SQLTableManager method getTableDDL.
public DBEPersistAction[] getTableDDL(DBRProgressMonitor monitor, OBJECT_TYPE table) throws DBException {
final DBERegistry editorsRegistry = table.getDataSource().getContainer().getPlatform().getEditorsRegistry();
SQLObjectEditor<DBSEntityAttribute, OBJECT_TYPE> tcm = getObjectEditor(editorsRegistry, DBSEntityAttribute.class);
SQLObjectEditor<DBSTableConstraint, OBJECT_TYPE> pkm = getObjectEditor(editorsRegistry, DBSTableConstraint.class);
SQLObjectEditor<DBSTableForeignKey, OBJECT_TYPE> fkm = getObjectEditor(editorsRegistry, DBSTableForeignKey.class);
SQLObjectEditor<DBSTableIndex, OBJECT_TYPE> im = getObjectEditor(editorsRegistry, DBSTableIndex.class);
StructCreateCommand command = makeCreateCommand(table);
if (tcm != null) {
// Aggregate nested column, constraint and index commands
for (DBSEntityAttribute column : CommonUtils.safeCollection(table.getAttributes(monitor))) {
if (DBUtils.isHiddenObject(column)) {
continue;
}
command.aggregateCommand(tcm.makeCreateCommand(column));
}
}
if (pkm != null) {
try {
for (DBSTableConstraint constraint : CommonUtils.safeCollection(table.getConstraints(monitor))) {
if (DBUtils.isHiddenObject(constraint)) {
continue;
}
command.aggregateCommand(pkm.makeCreateCommand(constraint));
}
} catch (DBException e) {
// Ignore primary keys
log.debug(e);
}
}
if (fkm != null) {
try {
for (DBSEntityAssociation foreignKey : CommonUtils.safeCollection(table.getAssociations(monitor))) {
if (!(foreignKey instanceof DBSTableForeignKey) || DBUtils.isHiddenObject(foreignKey)) {
continue;
}
command.aggregateCommand(fkm.makeCreateCommand((DBSTableForeignKey) foreignKey));
}
} catch (DBException e) {
// Ignore primary keys
log.debug(e);
}
}
if (im != null) {
try {
for (DBSTableIndex index : CommonUtils.safeCollection(table.getIndexes(monitor))) {
if (DBUtils.isHiddenObject(index)) {
continue;
}
command.aggregateCommand(im.makeCreateCommand(index));
}
} catch (DBException e) {
// Ignore indexes
log.debug(e);
}
}
return command.getPersistActions();
}
use of org.jkiss.dbeaver.model.struct.rdb.DBSTableForeignKey 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]);
}
Aggregations