Search in sources :

Example 36 with Nullable

use of org.jkiss.code.Nullable in project dbeaver by serge-rider.

the class ExasolBaseTableToolDialog method getScriptListener.

@Override
protected SQLScriptProgressListener<ExasolTableBase> getScriptListener() {
    final int nbExtraColumns = getNumberExtraResultingColumns();
    return new SQLScriptStatusDialog<ExasolTableBase>(getShell(), getTitle() + " " + ExasolMessages.dialog_table_tools_progress, null) {

        @Override
        protected void createStatusColumns(Tree objectTree) {
            TreeColumn msgColumn = new TreeColumn(objectTree, SWT.NONE);
            msgColumn.setText(ExasolMessages.dialog_table_tools_result);
            for (int i = 0; i < nbExtraColumns; i++) {
                new TreeColumn(objectTree, SWT.NONE);
            }
        }

        @Override
        public void endObjectProcessing(@NotNull ExasolTableBase exasolTable, Exception exception) {
            TreeItem treeItem = getTreeItem(exasolTable);
            if (exception == null) {
                treeItem.setText(1, ExasolMessages.dialog_table_tools_success_title);
            } else {
                treeItem.setText(1, exception.getMessage());
            }
            UIUtils.packColumns(treeItem.getParent(), false, null);
        }

        // DF: This method is for tools that return resultsets
        @Override
        public void processObjectResults(@NotNull ExasolTableBase exasolTable, @Nullable DBCStatement statement, @Nullable DBCResultSet resultSet) throws DBCException {
            if (resultSet == null) {
                return;
            }
            // Retrieve column names
            DBCResultSetMetaData rsMetaData = resultSet.getMeta();
            try {
                TreeItem treeItem = getTreeItem(exasolTable);
                Font f = UIUtils.makeBoldFont(treeItem.getFont());
                if (treeItem != null) {
                    // Display the column names
                    TreeItem subItem = null;
                    subItem = new TreeItem(treeItem, SWT.NONE);
                    subItem.setFont(f);
                    for (DBCAttributeMetaData column : rsMetaData.getAttributes()) {
                        subItem.setText(column.getOrdinalPosition(), column.getName());
                        subItem.setGrayed(true);
                    }
                    // Display the data for each row
                    while (resultSet.nextRow()) {
                        subItem = new TreeItem(treeItem, SWT.NONE);
                        for (int i = 0; i < rsMetaData.getAttributes().size(); i++) {
                            subItem.setText(i, CommonUtils.toString(resultSet.getAttributeValue(i)));
                            i++;
                        }
                    }
                    treeItem.setExpanded(true);
                }
            } catch (Exception e) {
                throw new DBCException(e.getMessage());
            }
        }
    };
}
Also used : TreeItem(org.eclipse.swt.widgets.TreeItem) ExasolTableBase(org.jkiss.dbeaver.ext.exasol.model.ExasolTableBase) NotNull(org.jkiss.code.NotNull) SQLScriptStatusDialog(org.jkiss.dbeaver.ui.dialogs.sql.SQLScriptStatusDialog) Font(org.eclipse.swt.graphics.Font) TreeColumn(org.eclipse.swt.widgets.TreeColumn) Tree(org.eclipse.swt.widgets.Tree) Nullable(org.jkiss.code.Nullable)

Example 37 with Nullable

use of org.jkiss.code.Nullable in project dbeaver by serge-rider.

the class ForeignKeysCache method fetchObject.

@Nullable
@Override
protected GenericTableForeignKey fetchObject(JDBCSession session, GenericStructContainer owner, GenericTable parent, String fkName, JDBCResultSet dbResult) throws SQLException, DBException {
    String pkTableCatalog = GenericUtils.safeGetStringTrimmed(foreignKeyObject, dbResult, JDBCConstants.PKTABLE_CAT);
    String pkTableSchema = GenericUtils.safeGetStringTrimmed(foreignKeyObject, dbResult, JDBCConstants.PKTABLE_SCHEM);
    String pkTableName = GenericUtils.safeGetStringTrimmed(foreignKeyObject, dbResult, JDBCConstants.PKTABLE_NAME);
    String fkTableCatalog = GenericUtils.safeGetStringTrimmed(foreignKeyObject, dbResult, JDBCConstants.FKTABLE_CAT);
    String fkTableSchema = GenericUtils.safeGetStringTrimmed(foreignKeyObject, dbResult, JDBCConstants.FKTABLE_SCHEM);
    int keySeq = GenericUtils.safeGetInt(foreignKeyObject, dbResult, JDBCConstants.KEY_SEQ);
    int updateRuleNum = GenericUtils.safeGetInt(foreignKeyObject, dbResult, JDBCConstants.UPDATE_RULE);
    int deleteRuleNum = GenericUtils.safeGetInt(foreignKeyObject, dbResult, JDBCConstants.DELETE_RULE);
    String pkName = GenericUtils.safeGetStringTrimmed(foreignKeyObject, dbResult, JDBCConstants.PK_NAME);
    int defferabilityNum = GenericUtils.safeGetInt(foreignKeyObject, dbResult, JDBCConstants.DEFERRABILITY);
    DBSForeignKeyModifyRule deleteRule = JDBCUtils.getCascadeFromNum(deleteRuleNum);
    DBSForeignKeyModifyRule updateRule = JDBCUtils.getCascadeFromNum(updateRuleNum);
    DBSForeignKeyDefferability defferability;
    switch(defferabilityNum) {
        case DatabaseMetaData.importedKeyInitiallyDeferred:
            defferability = DBSForeignKeyDefferability.INITIALLY_DEFERRED;
            break;
        case DatabaseMetaData.importedKeyInitiallyImmediate:
            defferability = DBSForeignKeyDefferability.INITIALLY_IMMEDIATE;
            break;
        case DatabaseMetaData.importedKeyNotDeferrable:
            defferability = DBSForeignKeyDefferability.NOT_DEFERRABLE;
            break;
        default:
            defferability = DBSForeignKeyDefferability.UNKNOWN;
            break;
    }
    if (pkTableName == null) {
        log.debug("Null PK table name");
        return null;
    }
    String pkTableFullName = DBUtils.getSimpleQualifiedName(pkTableCatalog, pkTableSchema, pkTableName);
    GenericTable pkTable = parent.getDataSource().findTable(session.getProgressMonitor(), pkTableCatalog, pkTableSchema, pkTableName);
    if (pkTable == null) {
        // Try to use FK catalog/schema
        pkTable = parent.getDataSource().findTable(session.getProgressMonitor(), fkTableCatalog, fkTableSchema, pkTableName);
        if (pkTable == null) {
            log.warn("Can't find PK table " + pkTableName);
            return null;
        } else {
            log.debug("PK table " + pkTableFullName + " was taken from FK container.");
        }
    }
    // Find PK
    GenericPrimaryKey pk = null;
    if (!CommonUtils.isEmpty(pkName)) {
        pk = DBUtils.findObject(pkTable.getConstraints(session.getProgressMonitor()), pkName);
        if (pk == null) {
            log.debug("Unique key '" + pkName + "' not found in table " + pkTable.getFullyQualifiedName(DBPEvaluationContext.DDL));
        }
    }
    if (pk == null) {
        String pkColumnName = GenericUtils.safeGetStringTrimmed(foreignKeyObject, dbResult, JDBCConstants.PKCOLUMN_NAME);
        GenericTableColumn pkColumn = pkTable.getAttribute(session.getProgressMonitor(), pkColumnName);
        if (pkColumn == null) {
            log.warn("Can't find PK table " + pkTable.getFullyQualifiedName(DBPEvaluationContext.DDL) + " column " + pkColumnName);
            return null;
        }
        Collection<GenericPrimaryKey> uniqueKeys = pkTable.getConstraints(session.getProgressMonitor());
        if (uniqueKeys != null) {
            for (GenericPrimaryKey pkConstraint : uniqueKeys) {
                if (pkConstraint.getConstraintType().isUnique() && DBUtils.getConstraintAttribute(session.getProgressMonitor(), pkConstraint, pkColumn) != null) {
                    pk = pkConstraint;
                    break;
                }
            }
        }
        if (pk == null) {
            log.warn("Can't find unique key for table " + pkTable.getFullyQualifiedName(DBPEvaluationContext.DDL) + " column " + pkColumn.getName());
            // Too bad. But we have to create new fake PK for this FK
            String pkFullName = pkTable.getFullyQualifiedName(DBPEvaluationContext.DDL) + "." + pkName;
            pk = pkMap.get(pkFullName);
            if (pk == null) {
                pk = new GenericPrimaryKey(pkTable, pkName, null, DBSEntityConstraintType.PRIMARY_KEY, true);
                pkMap.put(pkFullName, pk);
                // Add this fake constraint to it's owner
                pk.getTable().addUniqueKey(pk);
            }
            pk.addColumn(new GenericTableConstraintColumn(pk, pkColumn, keySeq));
        }
    }
    if (CommonUtils.isEmpty(fkName)) {
        // [JDBC] Some drivers return empty foreign key names
        fkName = parent.getName().toUpperCase() + "_FK_" + pkTable.getName().toUpperCase(Locale.ENGLISH);
    }
    return new GenericTableForeignKey(parent, fkName, null, pk, deleteRule, updateRule, defferability, true);
}
Also used : DBSForeignKeyModifyRule(org.jkiss.dbeaver.model.struct.rdb.DBSForeignKeyModifyRule) DBSForeignKeyDefferability(org.jkiss.dbeaver.model.struct.rdb.DBSForeignKeyDefferability) Nullable(org.jkiss.code.Nullable)

Example 38 with Nullable

use of org.jkiss.code.Nullable in project dbeaver by serge-rider.

the class IndexCache method fetchObject.

@Nullable
@Override
protected GenericTableIndex fetchObject(JDBCSession session, GenericStructContainer owner, GenericTable parent, String indexName, JDBCResultSet dbResult) throws SQLException, DBException {
    boolean isNonUnique = GenericUtils.safeGetBoolean(indexObject, dbResult, JDBCConstants.NON_UNIQUE);
    String indexQualifier = GenericUtils.safeGetStringTrimmed(indexObject, dbResult, JDBCConstants.INDEX_QUALIFIER);
    long cardinality = GenericUtils.safeGetLong(indexObject, dbResult, JDBCConstants.INDEX_CARDINALITY);
    int indexTypeNum = GenericUtils.safeGetInt(indexObject, dbResult, JDBCConstants.TYPE);
    DBSIndexType indexType;
    switch(indexTypeNum) {
        case DatabaseMetaData.tableIndexStatistic:
            // Table index statistic. Not a real index.
            log.debug("Skip statistics index '" + indexName + "' in '" + DBUtils.getObjectFullName(parent, DBPEvaluationContext.DDL) + "'");
            return null;
        // indexType = DBSIndexType.STATISTIC; break;
        case DatabaseMetaData.tableIndexClustered:
            indexType = DBSIndexType.CLUSTERED;
            break;
        case DatabaseMetaData.tableIndexHashed:
            indexType = DBSIndexType.HASHED;
            break;
        case DatabaseMetaData.tableIndexOther:
            indexType = DBSIndexType.OTHER;
            break;
        default:
            indexType = DBSIndexType.UNKNOWN;
            break;
    }
    if (CommonUtils.isEmpty(indexName)) {
        // [JDBC] Some drivers return empty index names
        indexName = parent.getName().toUpperCase(Locale.ENGLISH) + "_INDEX";
    }
    return owner.getDataSource().getMetaModel().createIndexImpl(parent, isNonUnique, indexQualifier, cardinality, indexName, indexType, true);
}
Also used : DBSIndexType(org.jkiss.dbeaver.model.struct.rdb.DBSIndexType) Nullable(org.jkiss.code.Nullable)

Example 39 with Nullable

use of org.jkiss.code.Nullable in project dbeaver by serge-rider.

the class DBVUtils method findAttributeTransformers.

@Nullable
public static DBDAttributeTransformer[] findAttributeTransformers(@NotNull DBDAttributeBinding binding, @Nullable Boolean custom) {
    DBPDataSource dataSource = binding.getDataSource();
    DBPDataSourceContainer container = dataSource.getContainer();
    List<? extends DBDAttributeTransformerDescriptor> tdList = container.getPlatform().getValueHandlerRegistry().findTransformers(dataSource, binding.getAttribute(), custom);
    if (tdList == null || tdList.isEmpty()) {
        return null;
    }
    boolean filtered = false;
    DBSEntityAttribute entityAttribute = binding.getEntityAttribute();
    if (entityAttribute != null) {
        DBVEntity vEntity = findVirtualEntity(entityAttribute.getParentObject(), false);
        if (vEntity != null) {
            DBVEntityAttribute vAttr = vEntity.getVirtualAttribute(binding, false);
            if (vAttr != null) {
                final DBVTransformSettings transformSettings = getTransformSettings(vAttr, false);
                if (transformSettings != null) {
                    filtered = transformSettings.filterTransformers(tdList);
                }
            }
        }
    }
    if (!filtered) {
        // Leave only default transformers
        for (int i = 0; i < tdList.size(); ) {
            if (tdList.get(i).isCustom() || !tdList.get(i).isApplicableByDefault()) {
                tdList.remove(i);
            } else {
                i++;
            }
        }
    }
    if (tdList.isEmpty()) {
        return null;
    }
    DBDAttributeTransformer[] result = new DBDAttributeTransformer[tdList.size()];
    for (int i = 0; i < tdList.size(); i++) {
        result[i] = tdList.get(i).getInstance();
    }
    return result;
}
Also used : DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) Nullable(org.jkiss.code.Nullable)

Example 40 with Nullable

use of org.jkiss.code.Nullable in project dbeaver by serge-rider.

the class ContentUtils method convertPathToWorkspaceFile.

@Nullable
public static IFile convertPathToWorkspaceFile(IPath path) {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IFile file = root.getFileForLocation(path);
    if (file != null) {
        return file;
    }
    // Probably we have a path to some linked resource
    IPath folderPath = path.removeLastSegments(1);
    URI folderURI = folderPath.toFile().toURI();
    IContainer[] containers = root.findContainersForLocationURI(folderURI);
    if (!ArrayUtils.isEmpty(containers)) {
        IContainer container = containers[0];
        file = container.getFile(path.removeFirstSegments(path.segmentCount() - 1));
    }
    return file;
}
Also used : IPath(org.eclipse.core.runtime.IPath) URI(java.net.URI) Nullable(org.jkiss.code.Nullable)

Aggregations

Nullable (org.jkiss.code.Nullable)49 DBException (org.jkiss.dbeaver.DBException)6 Tree (org.eclipse.swt.widgets.Tree)5 TreeColumn (org.eclipse.swt.widgets.TreeColumn)5 TreeItem (org.eclipse.swt.widgets.TreeItem)5 NotNull (org.jkiss.code.NotNull)5 SQLException (java.sql.SQLException)4 Matcher (java.util.regex.Matcher)4 SQLScriptStatusDialog (org.jkiss.dbeaver.ui.dialogs.sql.SQLScriptStatusDialog)4 DB2TableColumn (org.jkiss.dbeaver.ext.db2.model.DB2TableColumn)3 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)3 DBCResultSet (org.jkiss.dbeaver.model.exec.DBCResultSet)3 DBCStatement (org.jkiss.dbeaver.model.exec.DBCStatement)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 IProject (org.eclipse.core.resources.IProject)2 IStatus (org.eclipse.core.runtime.IStatus)2 IFindReplaceTarget (org.eclipse.jface.text.IFindReplaceTarget)2 ISelectionProvider (org.eclipse.jface.viewers.ISelectionProvider)2 Font (org.eclipse.swt.graphics.Font)2