Search in sources :

Example 51 with JDBCStatement

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement in project dbeaver by serge-rider.

the class ExasolStructureAssistant method findProceduresByMask.

private void findProceduresByMask(JDBCSession session, ExasolSchema schema, String objectNameMask, int maxResults, List<DBSObjectReference> references) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    // don't use parameter marks because of performance
    String sql = "";
    if (schema == null) {
        sql = String.format(sqlProceduresAll, ExasolUtils.quoteString(objectNameMask));
    } else {
        sql = String.format(sqlProcedureSchema, ExasolUtils.quoteString(schema.getName()), ExasolUtils.quoteString(objectNameMask));
    }
    try (JDBCStatement dbstat = session.createStatement()) {
        try (JDBCResultSet dbResult = dbstat.executeQuery(sql)) {
            int num = maxResults;
            while (dbResult.next() && num-- > 0) {
                if (monitor.isCanceled()) {
                    break;
                }
                final String schemaName = JDBCUtils.safeGetString(dbResult, "SCRIPT_SCHEMA");
                final String scriptName = JDBCUtils.safeGetString(dbResult, "SCRIPT_NAME");
                references.add(new AbstractObjectReference(scriptName, dataSource.getSchema(monitor, schemaName), null, ExasolScript.class, RelationalObjectType.TYPE_PROCEDURE) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        ExasolSchema tableSchema = schema != null ? schema : dataSource.getSchema(monitor, schemaName);
                        if (tableSchema == null) {
                            throw new DBException("Table schema '" + schemaName + "' not found");
                        }
                        ExasolScript script = tableSchema.scriptCache.getObject(monitor, tableSchema, scriptName);
                        if (script == null) {
                            throw new DBException("Script '" + script + "'  not found in schema '" + schemaName + "'");
                        }
                        return script;
                    }
                });
            }
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) AbstractObjectReference(org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 52 with JDBCStatement

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement in project dbeaver by serge-rider.

the class ExasolStructureAssistant method findConstraintsByMask.

private void findConstraintsByMask(JDBCSession session, ExasolSchema schema, String objectNameMask, int maxResults, List<DBSObjectReference> references, String constType) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    // don't use parameter marks because of performance
    String sql = "";
    if (schema == null) {
        sql = String.format(sqlConstraintsAll, ExasolUtils.quoteString(objectNameMask), constType);
    } else {
        sql = String.format(sqlConstraintsSchema, ExasolUtils.quoteString(schema.getName()), constType, ExasolUtils.quoteString(objectNameMask));
    }
    try (JDBCStatement dbstat = session.createStatement()) {
        try (JDBCResultSet dbResult = dbstat.executeQuery(sql)) {
            int num = maxResults;
            while (dbResult.next() && num-- > 0) {
                if (monitor.isCanceled()) {
                    break;
                }
                final String schemaName = JDBCUtils.safeGetString(dbResult, "CONSTRAINT_SCHEMA");
                final String tableName = JDBCUtils.safeGetString(dbResult, "CONSTRAINT_TABLE");
                final String constName = JDBCUtils.safeGetString(dbResult, "CONSTRAINT_NAME");
                final Class<?> classType;
                if (constType.equals("PRIMARY KEY")) {
                    classType = ExasolTableUniqueKey.class;
                } else if (constType.equals("FOREIGN KEY")) {
                    classType = ExasolTableForeignKey.class;
                } else {
                    throw new DBException("Unkown constraint type" + constType);
                }
                references.add(new AbstractObjectReference(constName, dataSource.getSchema(monitor, schemaName), null, classType, RelationalObjectType.TYPE_CONSTRAINT) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        ExasolSchema tableSchema = schema != null ? schema : dataSource.getSchema(monitor, schemaName);
                        if (tableSchema == null) {
                            throw new DBException("Table schema '" + schemaName + "' not found");
                        }
                        ExasolTable table = tableSchema.getTable(monitor, tableName);
                        if (table == null) {
                            throw new DBException("Table '" + tableName + "' not found in schema  '" + schemaName + "' not found");
                        }
                        if (classType.equals(ExasolTableForeignKey.class)) {
                            ExasolTableForeignKey foreignKey = (ExasolTableForeignKey) table.getAssociation(monitor, constName);
                            if (foreignKey == null)
                                throw new DBException("Foreign Key  '" + constName + "' for Table '" + tableName + "' not found in schema '" + schemaName + "'");
                            return foreignKey;
                        } else {
                            ExasolTableUniqueKey primaryKey = table.getConstraint(monitor, constName);
                            if (primaryKey == null)
                                throw new DBException("Primary Key '" + constName + "' for Table '" + tableName + "' not found in schema '" + schemaName + "'");
                            return primaryKey;
                        }
                    }
                });
            }
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) AbstractObjectReference(org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 53 with JDBCStatement

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement in project dbeaver by serge-rider.

the class DB2DataSource method collectObjectStatistics.

@Override
public void collectObjectStatistics(DBRProgressMonitor monitor, boolean totalSizeOnly, boolean forceRefresh) throws DBException {
    if (hasStatistics && !forceRefresh) {
        return;
    }
    try (JDBCSession session = DBUtils.openMetaSession(monitor, this, "Load schema statistics")) {
        try (JDBCStatement dbStat = session.createStatement()) {
            try (JDBCResultSet dbResult = dbStat.executeQuery("SELECT\n" + "    TABSCHEMA,\n" + "    SUM(DATA_OBJECT_P_SIZE + INDEX_OBJECT_P_SIZE + LONG_OBJECT_P_SIZE + LOB_OBJECT_P_SIZE + XML_OBJECT_P_SIZE) AS TOTAL_SIZE_IN_KB\n" + "FROM SYSIBMADM.ADMINTABINFO\n" + "GROUP BY TABSCHEMA")) {
                while (dbResult.next()) {
                    String schemaName = JDBCUtils.safeGetStringTrimmed(dbResult, 1);
                    long bytes = dbResult.getLong(2) * 1024;
                    DB2Schema schema = getSchema(monitor, schemaName);
                    if (schema != null) {
                        schema.setSchemaTotalSize(bytes);
                    }
                }
                for (DB2Schema schema : getSchemas(monitor)) {
                    if (!schema.hasStatistics()) {
                        schema.setSchemaTotalSize(0);
                    }
                }
            }
        }
    } catch (SQLException e) {
        throw new DBCException("Error reading table statistics", e);
    } finally {
        hasStatistics = true;
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DB2XMLString(org.jkiss.dbeaver.ext.db2.info.DB2XMLString)

Example 54 with JDBCStatement

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement in project dbeaver by serge-rider.

the class ExasolUtils method readSessions.

public static Collection<ExasolServerSession> readSessions(DBRProgressMonitor progressMonitor, JDBCSession session) throws SQLException {
    LOG.debug("read sessions");
    List<ExasolServerSession> listSessions = new ArrayList<>();
    // check dba view
    try {
        try (JDBCStatement dbStat = session.createStatement()) {
            try (JDBCResultSet dbResult = dbStat.executeQuery(SESS_DBA_QUERY)) {
                while (dbResult.next()) {
                    listSessions.add(new ExasolServerSession(dbResult));
                }
            }
        }
    // now try all view
    } catch (SQLException e) {
        try (JDBCStatement dbStat = session.createStatement()) {
            try (JDBCResultSet dbResult = dbStat.executeQuery(SESS_ALL_QUERY)) {
                while (dbResult.next()) {
                    listSessions.add(new ExasolServerSession(dbResult));
                }
            }
        }
    }
    return listSessions;
}
Also used : JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) ArrayList(java.util.ArrayList) ExasolServerSession(org.jkiss.dbeaver.ext.exasol.model.app.ExasolServerSession)

Example 55 with JDBCStatement

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement in project dbeaver by serge-rider.

the class ExasolTableForeignKeyCache method prepareObjectsStatement.

@NotNull
@Override
protected JDBCStatement prepareObjectsStatement(JDBCSession session, ExasolSchema exasolSchema, ExasolTable forTable) throws SQLException {
    String sql;
    if (forTable != null) {
        sql = String.format(SQL_FK_TAB, ExasolUtils.quoteString(exasolSchema.getName()), ExasolUtils.quoteString(forTable.getName()), ExasolUtils.quoteString(exasolSchema.getName()), ExasolUtils.quoteString(forTable.getName()), ExasolUtils.quoteString(exasolSchema.getName()), ExasolUtils.quoteString(forTable.getName()));
    } else {
        sql = String.format(SQL_FK_ALL, ExasolUtils.quoteString(exasolSchema.getName()), ExasolUtils.quoteString(exasolSchema.getName()), ExasolUtils.quoteString(exasolSchema.getName()));
    }
    JDBCStatement dbStat = session.createStatement();
    dbStat.setQueryString(sql);
    return dbStat;
}
Also used : JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) NotNull(org.jkiss.code.NotNull)

Aggregations

JDBCStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement)70 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)40 SQLException (java.sql.SQLException)32 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)26 DBException (org.jkiss.dbeaver.DBException)24 NotNull (org.jkiss.code.NotNull)18 ArrayList (java.util.ArrayList)16 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)12 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)12 DBCException (org.jkiss.dbeaver.model.exec.DBCException)10 SQLWarning (java.sql.SQLWarning)8 AbstractObjectReference (org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference)8 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)8 JDBCStatementImpl (org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl)6 VoidProgressMonitor (org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)4 BigDecimal (java.math.BigDecimal)2 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)2 SQLXML (java.sql.SQLXML)2 Tree (org.eclipse.swt.widgets.Tree)2 TreeColumn (org.eclipse.swt.widgets.TreeColumn)2