Search in sources :

Example 71 with DBSEntityAttribute

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");
}
Also used : DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) DBSAttributeBase(org.jkiss.dbeaver.model.struct.DBSAttributeBase)

Example 72 with DBSEntityAttribute

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");
}
Also used : DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute)

Example 73 with DBSEntityAttribute

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;
}
Also used : DBCAttributeMetaData(org.jkiss.dbeaver.model.exec.DBCAttributeMetaData) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) DBCEntityMetaData(org.jkiss.dbeaver.model.exec.DBCEntityMetaData) DBSAttributeBase(org.jkiss.dbeaver.model.struct.DBSAttributeBase) DBSEntity(org.jkiss.dbeaver.model.struct.DBSEntity) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding)

Example 74 with DBSEntityAttribute

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;
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) ArrayList(java.util.ArrayList) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) DBVEntityConstraint(org.jkiss.dbeaver.model.virtual.DBVEntityConstraint) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) DBDRowIdentifier(org.jkiss.dbeaver.model.data.DBDRowIdentifier)

Example 75 with DBSEntityAttribute

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;
    });
}
Also used : DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) OracleTableColumn(org.jkiss.dbeaver.ext.oracle.model.OracleTableColumn) OracleTableConstraintColumn(org.jkiss.dbeaver.ext.oracle.model.OracleTableConstraintColumn) EditConstraintPage(org.jkiss.dbeaver.ui.editors.object.struct.EditConstraintPage) OracleTableConstraint(org.jkiss.dbeaver.ext.oracle.model.OracleTableConstraint)

Aggregations

DBSEntityAttribute (org.jkiss.dbeaver.model.struct.DBSEntityAttribute)77 DBException (org.jkiss.dbeaver.DBException)25 DBSEntity (org.jkiss.dbeaver.model.struct.DBSEntity)16 VoidProgressMonitor (org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)14 ArrayList (java.util.ArrayList)11 DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)10 DBSAttributeBase (org.jkiss.dbeaver.model.struct.DBSAttributeBase)10 EditIndexPage (org.jkiss.dbeaver.ui.editors.object.struct.EditIndexPage)10 DBSEntityAttributeRef (org.jkiss.dbeaver.model.struct.DBSEntityAttributeRef)9 MySQLTableColumn (org.jkiss.dbeaver.ext.mysql.model.MySQLTableColumn)6 DBDRowIdentifier (org.jkiss.dbeaver.model.data.DBDRowIdentifier)6 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)6 DBSDataType (org.jkiss.dbeaver.model.struct.DBSDataType)6 EditConstraintPage (org.jkiss.dbeaver.ui.editors.object.struct.EditConstraintPage)6 TableItem (org.eclipse.swt.widgets.TableItem)5 BigDecimal (java.math.BigDecimal)4 SQLServerTableColumn (org.jkiss.dbeaver.ext.mssql.model.SQLServerTableColumn)4 KeyAdapter (org.eclipse.swt.events.KeyAdapter)3 KeyEvent (org.eclipse.swt.events.KeyEvent)3 GridData (org.eclipse.swt.layout.GridData)3