Search in sources :

Example 46 with DBRProgressMonitor

use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor in project dbeaver by serge-rider.

the class MySQLExportWizardPageObjects method loadTables.

private void loadTables(final MySQLCatalog catalog) {
    if (catalog != null) {
        curCatalog = catalog;
    }
    if (curCatalog == null) {
        return;
    }
    final boolean isCatalogChecked = isChecked(curCatalog);
    final Set<MySQLTableBase> checkedObjects = this.checkedObjects.get(curCatalog);
    new AbstractJob("Load '" + curCatalog.getName() + "' tables") {

        {
            setUser(true);
        }

        @Override
        protected IStatus run(DBRProgressMonitor monitor) {
            try {
                final List<MySQLTableBase> objects = new ArrayList<>();
                objects.addAll(curCatalog.getTables(monitor));
                if (wizard.showViews) {
                    objects.addAll(curCatalog.getViews(monitor));
                }
                Collections.sort(objects, DBUtils.nameComparator());
                DBeaverUI.syncExec(new Runnable() {

                    @Override
                    public void run() {
                        tablesTable.removeAll();
                        for (MySQLTableBase table : objects) {
                            TableItem item = new TableItem(tablesTable, SWT.NONE);
                            item.setImage(DBeaverIcons.getImage(table.isView() ? DBIcon.TREE_VIEW : DBIcon.TREE_TABLE));
                            item.setText(0, table.getName());
                            item.setData(table);
                            item.setChecked(isCatalogChecked && (checkedObjects == null || checkedObjects.contains(table)));
                        }
                    }
                });
            } catch (DBException e) {
                UIUtils.showErrorDialog(null, "Table list", "Can't read table list", e);
            }
            return Status.OK_STATUS;
        }
    }.schedule();
}
Also used : AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) DBException(org.jkiss.dbeaver.DBException) IStatus(org.eclipse.core.runtime.IStatus) MySQLTableBase(org.jkiss.dbeaver.ext.mysql.model.MySQLTableBase) List(java.util.List) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 47 with DBRProgressMonitor

use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor in project dbeaver by serge-rider.

the class MySQLStructureAssistant method findConstraintsByMask.

private void findConstraintsByMask(JDBCSession session, @Nullable final MySQLCatalog catalog, String constrNameMask, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    // Load constraints
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT " + MySQLConstants.COL_TABLE_SCHEMA + "," + MySQLConstants.COL_TABLE_NAME + "," + MySQLConstants.COL_CONSTRAINT_NAME + "," + MySQLConstants.COL_CONSTRAINT_TYPE + " FROM " + MySQLConstants.META_TABLE_TABLE_CONSTRAINTS + " WHERE " + MySQLConstants.COL_CONSTRAINT_NAME + " LIKE ? " + (catalog == null ? "" : " AND " + MySQLConstants.COL_TABLE_SCHEMA + "=?") + " ORDER BY " + MySQLConstants.COL_CONSTRAINT_NAME + " LIMIT " + maxResults)) {
        dbStat.setString(1, constrNameMask.toLowerCase(Locale.ENGLISH));
        if (catalog != null) {
            dbStat.setString(2, catalog.getName());
        }
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            int tableNum = maxResults;
            while (dbResult.next() && tableNum-- > 0) {
                if (monitor.isCanceled()) {
                    break;
                }
                final String catalogName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_TABLE_SCHEMA);
                final String tableName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_TABLE_NAME);
                final String constrName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_CONSTRAINT_NAME);
                final String constrType = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_CONSTRAINT_TYPE);
                final boolean isFK = MySQLConstants.CONSTRAINT_FOREIGN_KEY.equals(constrType);
                objects.add(new AbstractObjectReference(constrName, dataSource.getCatalog(catalogName), null, isFK ? MySQLTableForeignKey.class : MySQLTableConstraint.class, RelationalObjectType.TYPE_CONSTRAINT) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        MySQLCatalog tableCatalog = catalog != null ? catalog : dataSource.getCatalog(catalogName);
                        if (tableCatalog == null) {
                            throw new DBException("Constraint catalog '" + catalogName + "' not found");
                        }
                        MySQLTable table = tableCatalog.getTable(monitor, tableName);
                        if (table == null) {
                            throw new DBException("Constraint table '" + tableName + "' not found in catalog '" + tableCatalog.getName() + "'");
                        }
                        DBSObject constraint;
                        if (isFK) {
                            constraint = table.getAssociation(monitor, constrName);
                        } else {
                            constraint = table.getConstraint(monitor, constrName);
                        }
                        if (constraint == null) {
                            throw new DBException("Constraint '" + constrName + "' not found in table '" + table.getFullyQualifiedName(DBPEvaluationContext.DDL) + "'");
                        }
                        return constraint;
                    }
                });
            }
        }
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) DBException(org.jkiss.dbeaver.DBException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) AbstractObjectReference(org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 48 with DBRProgressMonitor

use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor in project dbeaver by serge-rider.

the class MySQLStructureAssistant method findProceduresByMask.

private void findProceduresByMask(JDBCSession session, @Nullable final MySQLCatalog catalog, String procNameMask, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    // Load procedures
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT " + MySQLConstants.COL_ROUTINE_SCHEMA + "," + MySQLConstants.COL_ROUTINE_NAME + " FROM " + MySQLConstants.META_TABLE_ROUTINES + " WHERE " + MySQLConstants.COL_ROUTINE_NAME + " LIKE ? " + (catalog == null ? "" : " AND " + MySQLConstants.COL_ROUTINE_SCHEMA + "=?") + " ORDER BY " + MySQLConstants.COL_ROUTINE_NAME + " LIMIT " + maxResults)) {
        dbStat.setString(1, procNameMask.toLowerCase(Locale.ENGLISH));
        if (catalog != null) {
            dbStat.setString(2, catalog.getName());
        }
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            int tableNum = maxResults;
            while (dbResult.next() && tableNum-- > 0) {
                if (monitor.isCanceled()) {
                    break;
                }
                final String catalogName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_ROUTINE_SCHEMA);
                final String procName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_ROUTINE_NAME);
                objects.add(new AbstractObjectReference(procName, dataSource.getCatalog(catalogName), null, MySQLProcedure.class, RelationalObjectType.TYPE_PROCEDURE) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        MySQLCatalog procCatalog = catalog != null ? catalog : dataSource.getCatalog(catalogName);
                        if (procCatalog == null) {
                            throw new DBException("Procedure catalog '" + catalogName + "' not found");
                        }
                        MySQLProcedure procedure = procCatalog.getProcedure(monitor, procName);
                        if (procedure == null) {
                            throw new DBException("Procedure '" + procName + "' not found in catalog '" + procCatalog.getName() + "'");
                        }
                        return procedure;
                    }
                });
            }
        }
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) DBException(org.jkiss.dbeaver.DBException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) AbstractObjectReference(org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 49 with DBRProgressMonitor

use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor in project dbeaver by serge-rider.

the class MySQLStructureAssistant method findTablesByMask.

private void findTablesByMask(JDBCSession session, @Nullable final MySQLCatalog catalog, String tableNameMask, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    // Load tables
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT " + MySQLConstants.COL_TABLE_SCHEMA + "," + MySQLConstants.COL_TABLE_NAME + " FROM " + MySQLConstants.META_TABLE_TABLES + " WHERE " + MySQLConstants.COL_TABLE_NAME + " LIKE ? " + (catalog == null ? "" : " AND " + MySQLConstants.COL_TABLE_SCHEMA + "=?") + " ORDER BY " + MySQLConstants.COL_TABLE_NAME + " LIMIT " + maxResults)) {
        dbStat.setString(1, tableNameMask.toLowerCase(Locale.ENGLISH));
        if (catalog != null) {
            dbStat.setString(2, catalog.getName());
        }
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            int tableNum = maxResults;
            while (dbResult.next() && tableNum-- > 0) {
                if (monitor.isCanceled()) {
                    break;
                }
                final String catalogName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_TABLE_SCHEMA);
                final String tableName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_TABLE_NAME);
                objects.add(new AbstractObjectReference(tableName, dataSource.getCatalog(catalogName), null, MySQLTableBase.class, RelationalObjectType.TYPE_TABLE) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        MySQLCatalog tableCatalog = catalog != null ? catalog : dataSource.getCatalog(catalogName);
                        if (tableCatalog == null) {
                            throw new DBException("Table catalog '" + catalogName + "' not found");
                        }
                        MySQLTableBase table = tableCatalog.getTableCache().getObject(monitor, tableCatalog, tableName);
                        if (table == null) {
                            throw new DBException("Table '" + tableName + "' not found in catalog '" + catalogName + "'");
                        }
                        return table;
                    }
                });
            }
        }
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) DBException(org.jkiss.dbeaver.DBException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) AbstractObjectReference(org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 50 with DBRProgressMonitor

use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor in project dbeaver by serge-rider.

the class NavigatorHandlerObjectCreateCopy method pasteResource.

private void pasteResource(final File file, DBNResource toFolder) {
    final IResource targetResource = toFolder.getResource();
    assert targetResource != null;
    final IContainer targetFolder = targetResource instanceof IContainer ? (IContainer) targetResource : targetResource.getParent();
    try {
        DBeaverUI.runInProgressService(new DBRRunnableWithProgress() {

            @Override
            public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    final IFile targetFile = targetFolder.getFile(new Path(file.getName()));
                    if (targetFile.exists()) {
                        throw new IOException("Target file '" + targetFile.getFullPath() + "' already exists");
                    }
                    try (InputStream is = new FileInputStream(file)) {
                        targetFile.create(is, true, monitor.getNestedMonitor());
                    }
                } catch (Exception e) {
                    throw new InvocationTargetException(e);
                }
            }
        });
    } catch (InvocationTargetException e) {
        UIUtils.showErrorDialog(null, "Copy error", "Error copying resource", e.getTargetException());
    } catch (InterruptedException e) {
    // ignore
    }
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) InvocationTargetException(java.lang.reflect.InvocationTargetException) CoreException(org.eclipse.core.runtime.CoreException) ExecutionException(org.eclipse.core.commands.ExecutionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) IContainer(org.eclipse.core.resources.IContainer) IResource(org.eclipse.core.resources.IResource)

Aggregations

DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)79 DBException (org.jkiss.dbeaver.DBException)45 InvocationTargetException (java.lang.reflect.InvocationTargetException)36 DBRRunnableWithProgress (org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress)30 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)14 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)12 AbstractJob (org.jkiss.dbeaver.model.runtime.AbstractJob)12 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)10 AbstractObjectReference (org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference)10 ArrayList (java.util.ArrayList)8 IStatus (org.eclipse.core.runtime.IStatus)8 IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)6 JobChangeAdapter (org.eclipse.core.runtime.jobs.JobChangeAdapter)6 GridData (org.eclipse.swt.layout.GridData)6 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)6 List (java.util.List)5 NotNull (org.jkiss.code.NotNull)5 File (java.io.File)4 IOException (java.io.IOException)4 IFile (org.eclipse.core.resources.IFile)4