Search in sources :

Example 31 with DBSEntityAttribute

use of org.jkiss.dbeaver.model.struct.DBSEntityAttribute in project dbeaver by dbeaver.

the class SQLConstraintManager method appendConstraintDefinition.

protected void appendConstraintDefinition(StringBuilder decl, DBECommandAbstract<OBJECT_TYPE> command) {
    // $NON-NLS-1$
    decl.append(" (");
    // Get columns using void monitor
    try {
        List<? extends DBSEntityAttributeRef> attrs = command.getObject().getAttributeReferences(new VoidProgressMonitor());
        if (attrs != null) {
            boolean firstColumn = true;
            for (DBSEntityAttributeRef constraintColumn : attrs) {
                final DBSEntityAttribute attribute = constraintColumn.getAttribute();
                if (attribute == null) {
                    continue;
                }
                // $NON-NLS-1$
                if (!firstColumn)
                    decl.append(",");
                firstColumn = false;
                decl.append(DBUtils.getQuotedIdentifier(attribute));
            }
        }
    } catch (DBException e) {
        log.warn("Can't obtain attribute references", e);
    }
    // $NON-NLS-1$
    decl.append(")");
}
Also used : DBSEntityAttributeRef(org.jkiss.dbeaver.model.struct.DBSEntityAttributeRef) DBException(org.jkiss.dbeaver.DBException) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)

Example 32 with DBSEntityAttribute

use of org.jkiss.dbeaver.model.struct.DBSEntityAttribute in project dbeaver by dbeaver.

the class OracleIndexConfigurator method configureObject.

@Override
public OracleTableIndex configureObject(DBRProgressMonitor monitor, Object container, OracleTableIndex index) {
    return UITask.run(() -> {
        EditIndexPage editPage = new EditIndexPage(OracleUIMessages.edit_oracle_index_manager_dialog_title, index, Collections.singletonList(DBSIndexType.OTHER));
        if (!editPage.edit()) {
            return null;
        }
        StringBuilder idxName = new StringBuilder(64);
        // $NON-NLS-1$
        idxName.append(CommonUtils.escapeIdentifier(index.getTable().getName())).append("_").append(CommonUtils.escapeIdentifier(editPage.getSelectedAttributes().iterator().next().getName())).append(// $NON-NLS-1$
        "_IDX");
        index.setName(DBObjectNameCaseTransformer.transformName(index.getDataSource(), idxName.toString()));
        index.setUnique(editPage.isUnique());
        index.setIndexType(editPage.getIndexType());
        int colIndex = 1;
        for (DBSEntityAttribute tableColumn : editPage.getSelectedAttributes()) {
            index.addColumn(new OracleTableIndexColumn(index, (OracleTableColumn) tableColumn, colIndex++, !Boolean.TRUE.equals(editPage.getAttributeProperty(tableColumn, EditIndexPage.PROP_DESC)), null));
        }
        return index;
    });
}
Also used : DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) OracleTableColumn(org.jkiss.dbeaver.ext.oracle.model.OracleTableColumn) OracleTableIndexColumn(org.jkiss.dbeaver.ext.oracle.model.OracleTableIndexColumn) EditIndexPage(org.jkiss.dbeaver.ui.editors.object.struct.EditIndexPage)

Example 33 with DBSEntityAttribute

use of org.jkiss.dbeaver.model.struct.DBSEntityAttribute 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;
}
Also used : SQLDatabasePersistAction(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction) DBECommandContext(org.jkiss.dbeaver.model.edit.DBECommandContext) PostgreForeignTableManager(org.jkiss.dbeaver.ext.postgresql.edit.PostgreForeignTableManager) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) SQLDatabasePersistActionComment(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionComment) PostgreTableColumnManager(org.jkiss.dbeaver.ext.postgresql.edit.PostgreTableColumnManager) PropertySourceCustom(org.jkiss.dbeaver.runtime.properties.PropertySourceCustom) SimpleCommandContext(org.jkiss.dbeaver.ui.editors.SimpleCommandContext) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) DBSEntity(org.jkiss.dbeaver.model.struct.DBSEntity)

Example 34 with DBSEntityAttribute

use of org.jkiss.dbeaver.model.struct.DBSEntityAttribute in project dbeaver by dbeaver.

the class SQLSemanticProcessor method getConstraintTable.

/**
 * Extract alias (or source table name) for specified constraint from SQL select.
 * Searches in FROM and JOIN
 */
@Nullable
public static Table getConstraintTable(PlainSelect select, DBDAttributeConstraint constraint) {
    String constrTable;
    DBSAttributeBase ca = constraint.getAttribute();
    if (ca instanceof DBDAttributeBinding) {
        constrTable = ((DBDAttributeBinding) ca).getMetaAttribute().getEntityName();
    } else if (ca instanceof DBSEntityAttribute) {
        constrTable = ((DBSEntityAttribute) ca).getParentObject().getName();
    } else {
        return null;
    }
    if (constrTable == null) {
        return null;
    }
    FromItem fromItem = select.getFromItem();
    Table table = findTableInFrom(fromItem, constrTable);
    if (table == null) {
        // Maybe it is a join
        if (!CommonUtils.isEmpty(select.getJoins())) {
            for (Join join : select.getJoins()) {
                table = findTableInFrom(join.getRightItem(), constrTable);
                if (table != null) {
                    break;
                }
            }
        }
    }
    return table;
}
Also used : Table(net.sf.jsqlparser.schema.Table) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) DBSAttributeBase(org.jkiss.dbeaver.model.struct.DBSAttributeBase) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) Nullable(org.jkiss.code.Nullable)

Example 35 with DBSEntityAttribute

use of org.jkiss.dbeaver.model.struct.DBSEntityAttribute in project dbeaver by dbeaver.

the class GenericTableIndexConfigurator method configureObject.

@Override
public GenericTableIndex configureObject(DBRProgressMonitor monitor, Object table, GenericTableIndex index) {
    GenericTableBase tableBase = (GenericTableBase) table;
    boolean supportUniqueIndexes = tableBase.supportUniqueIndexes();
    Collection<DBSIndexType> tableIndexTypes = tableBase.getTableIndexTypes();
    return new UITask<GenericTableIndex>() {

        @Override
        protected GenericTableIndex runTask() {
            EditIndexPage editPage = new EditIndexPage("Create index", index, tableIndexTypes, supportUniqueIndexes);
            if (!editPage.edit()) {
                return null;
            }
            index.setIndexType(editPage.getIndexType());
            StringBuilder idxName = new StringBuilder(64);
            idxName.append(CommonUtils.escapeIdentifier(index.getTable().getName()));
            int colIndex = 1;
            for (DBSEntityAttribute tableColumn : editPage.getSelectedAttributes()) {
                if (colIndex == 1) {
                    idxName.append("_").append(CommonUtils.escapeIdentifier(tableColumn.getName()));
                }
                index.addColumn(new GenericTableIndexColumn(index, (GenericTableColumn) tableColumn, colIndex++, !Boolean.TRUE.equals(editPage.getAttributeProperty(tableColumn, EditIndexPage.PROP_DESC))));
            }
            idxName.append("_IDX");
            index.setName(DBObjectNameCaseTransformer.transformObjectName(index, idxName.toString()));
            index.setUnique(editPage.isUnique());
            return index;
        }
    }.execute();
}
Also used : DBSIndexType(org.jkiss.dbeaver.model.struct.rdb.DBSIndexType) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) GenericTableIndexColumn(org.jkiss.dbeaver.ext.generic.model.GenericTableIndexColumn) GenericTableBase(org.jkiss.dbeaver.ext.generic.model.GenericTableBase) GenericTableIndex(org.jkiss.dbeaver.ext.generic.model.GenericTableIndex) EditIndexPage(org.jkiss.dbeaver.ui.editors.object.struct.EditIndexPage)

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