use of org.jkiss.dbeaver.model.struct.DBSEntityAttribute in project dbeaver by dbeaver.
the class SQLGeneratorMerge method generateSQL.
@Override
public void generateSQL(DBRProgressMonitor monitor, StringBuilder sql, DBSEntity object) throws DBException {
boolean hasAttr = false;
sql.append("MERGE INTO ").append(getEntityName(object)).append(" AS tgt").append(getLineSeparator());
sql.append("USING SOURCE_TABLE AS src").append(getLineSeparator());
Collection<? extends DBSEntityAttribute> keyAttributes = getKeyAttributes(monitor, object);
if (!CommonUtils.isEmpty(keyAttributes)) {
sql.append("ON (");
for (DBSEntityAttribute attr : keyAttributes) {
if (hasAttr)
sql.append(" AND ");
sql.append("tgt.").append(DBUtils.getQuotedIdentifier(attr)).append("=src.").append(DBUtils.getQuotedIdentifier(attr));
hasAttr = true;
}
sql.append(")\n");
}
sql.append("WHEN MATCHED\nTHEN UPDATE SET").append(getLineSeparator());
hasAttr = false;
for (DBSAttributeBase attr : getValueAttributes(monitor, object, keyAttributes)) {
if (hasAttr)
sql.append(", ");
sql.append("tgt.").append(DBUtils.getQuotedIdentifier(object.getDataSource(), attr.getName())).append("=src.").append(DBUtils.getQuotedIdentifier(object.getDataSource(), attr.getName()));
hasAttr = true;
}
sql.append(getLineSeparator()).append("WHEN NOT MATCHED").append(getLineSeparator()).append("THEN INSERT (");
hasAttr = false;
for (DBSEntityAttribute attr : getAllAttributes(monitor, object)) {
if (hasAttr)
sql.append(", ");
sql.append(DBUtils.getQuotedIdentifier(attr));
hasAttr = true;
}
sql.append(")").append(getLineSeparator()).append("VALUES (");
hasAttr = false;
for (DBSEntityAttribute attr : getAllAttributes(monitor, object)) {
if (hasAttr)
sql.append(", ");
sql.append("src.").append(DBUtils.getQuotedIdentifier(attr));
hasAttr = true;
}
sql.append(");\n");
}
use of org.jkiss.dbeaver.model.struct.DBSEntityAttribute in project dbeaver by dbeaver.
the class SQLGeneratorInsert method generateSQL.
@Override
public void generateSQL(DBRProgressMonitor monitor, StringBuilder sql, DBSEntity object) throws DBException {
sql.append("INSERT INTO ").append(getEntityName(object)).append(getLineSeparator()).append("(");
boolean hasAttr = false;
for (DBSEntityAttribute attr : getAllAttributes(monitor, object)) {
if (DBUtils.isPseudoAttribute(attr) || DBUtils.isHiddenObject(attr) || attr.isAutoGenerated()) {
continue;
}
if (hasAttr)
sql.append(", ");
sql.append(DBUtils.getObjectFullName(attr, DBPEvaluationContext.DML));
hasAttr = true;
}
sql.append(")").append(getLineSeparator()).append("VALUES(");
hasAttr = false;
for (DBSEntityAttribute attr : getAllAttributes(monitor, object)) {
if (DBUtils.isPseudoAttribute(attr) || DBUtils.isHiddenObject(attr) || attr.isAutoGenerated()) {
continue;
}
if (hasAttr)
sql.append(", ");
appendDefaultValue(sql, attr);
hasAttr = true;
}
sql.append(");\n");
}
use of org.jkiss.dbeaver.model.struct.DBSEntityAttribute in project dbeaver by dbeaver.
the class SQLSemanticProcessor method isValidTableColumn.
private static boolean isValidTableColumn(DBRProgressMonitor monitor, DBPDataSource dataSource, Table table, DBDAttributeConstraint co) throws DBException {
DBSAttributeBase attribute = co.getAttribute();
if (attribute instanceof DBDAttributeBinding) {
attribute = ((DBDAttributeBinding) attribute).getMetaAttribute();
}
if (table != null && attribute instanceof DBCAttributeMetaData) {
DBSEntityAttribute entityAttribute = null;
DBCEntityMetaData entityMetaData = ((DBCAttributeMetaData) attribute).getEntityMetaData();
if (entityMetaData != null) {
DBSEntity entity = DBUtils.getEntityFromMetaData(monitor, DBUtils.getDefaultContext(dataSource, true), entityMetaData);
if (entity != null) {
entityAttribute = entity.getAttribute(monitor, co.getAttributeName());
}
}
// No such attribute in entity. Do not use table prefix (#6927)
return entityAttribute != null;
}
return true;
}
use of org.jkiss.dbeaver.model.struct.DBSEntityAttribute in project dbeaver by dbeaver.
the class ValidateUniqueKeyUsageDialog method useAllColumns.
private static boolean useAllColumns(ResultSetViewer viewer) {
// Use all columns
final DBDRowIdentifier identifier = viewer.getVirtualEntityIdentifier();
DBVEntityConstraint constraint = (DBVEntityConstraint) identifier.getUniqueKey();
List<DBSEntityAttribute> uniqueColumns = new ArrayList<>();
for (DBDAttributeBinding binding : viewer.getModel().getAttributes()) {
if (binding.getEntityAttribute() != null) {
uniqueColumns.add(binding.getEntityAttribute());
}
}
if (uniqueColumns.isEmpty()) {
DBWorkbench.getPlatformUI().showError("Use All Columns", "No valid columns found for unique key");
return false;
}
constraint.setAttributes(uniqueColumns);
constraint.setUseAllColumns(true);
try {
identifier.reloadAttributes(new VoidProgressMonitor(), viewer.getModel().getAttributes());
} catch (DBException e) {
DBWorkbench.getPlatformUI().showError("Use All Columns", "Can't reload unique columns", e);
return false;
}
return true;
}
use of org.jkiss.dbeaver.model.struct.DBSEntityAttribute in project dbeaver by dbeaver.
the class OracleConstraintConfigurator method configureObject.
@Override
public OracleTableConstraint configureObject(DBRProgressMonitor monitor, Object parent, OracleTableConstraint constraint) {
return UITask.run(() -> {
EditConstraintPage editPage = new EditConstraintPage(OracleUIMessages.edit_oracle_constraint_manager_dialog_title, constraint, new DBSEntityConstraintType[] { DBSEntityConstraintType.PRIMARY_KEY, DBSEntityConstraintType.UNIQUE_KEY, DBSEntityConstraintType.CHECK });
if (!editPage.edit()) {
return null;
}
constraint.setName(editPage.getConstraintName());
constraint.setConstraintType(editPage.getConstraintType());
constraint.setSearchCondition(editPage.getConstraintExpression());
int colIndex = 1;
for (DBSEntityAttribute tableColumn : editPage.getSelectedAttributes()) {
constraint.addColumn(new OracleTableConstraintColumn(constraint, (OracleTableColumn) tableColumn, colIndex++));
}
return constraint;
});
}
Aggregations