Search in sources :

Example 76 with JDBCResultSet

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

the class InformixMetaModel method loadTriggers.

@Override
public List<? extends GenericTrigger> loadTriggers(DBRProgressMonitor monitor, @NotNull GenericStructContainer container, @Nullable GenericTable table) throws DBException {
    assert table != null;
    try (JDBCSession session = DBUtils.openMetaSession(monitor, container.getDataSource(), "Read triggers")) {
        String query = "SELECT T1.trigname \n" + "FROM informix.systriggers AS T1, informix.systables AS T2 \n" + "WHERE T2.tabid = T1.tabid AND T2.tabname = ?";
        try (JDBCPreparedStatement dbStat = session.prepareStatement(query)) {
            dbStat.setString(2, table.getName());
            List<GenericTrigger> result = new ArrayList<>();
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                while (dbResult.next()) {
                    String name = JDBCUtils.safeGetString(dbResult, 1);
                    if (name == null) {
                        continue;
                    }
                    name = name.trim();
                    GenericTrigger trigger = new GenericTrigger(container, table, name, null);
                    result.add(trigger);
                }
            }
            return result;
        }
    } catch (SQLException e) {
        throw new DBException(e, container.getDataSource());
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) ArrayList(java.util.ArrayList)

Example 77 with JDBCResultSet

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

the class SQLServerMetaModel method extractSource.

private String extractSource(DBRProgressMonitor monitor, GenericDataSource dataSource, String catalog, String schema, String name) throws DBException {
    ServerType serverType = getServerType(monitor, dataSource);
    String systemSchema = getSystemSchema(serverType);
    catalog = DBUtils.getQuotedIdentifier(dataSource, catalog);
    try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Read source code")) {
        String mdQuery = serverType == ServerType.SQL_SERVER ? catalog + "." + systemSchema + ".sp_helptext '" + schema + "." + name + "'" : "SELECT sc.text\n" + "FROM " + catalog + "." + systemSchema + ".sysobjects so\n" + "INNER JOIN " + catalog + "." + systemSchema + ".syscomments sc on sc.id = so.id\n" + "WHERE user_name(so.uid)=? AND so.name=?";
        try (JDBCPreparedStatement dbStat = session.prepareStatement(mdQuery)) {
            if (serverType == ServerType.SYBASE) {
                dbStat.setString(1, schema);
                dbStat.setString(2, name);
            }
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                StringBuilder sql = new StringBuilder();
                while (dbResult.nextRow()) {
                    sql.append(dbResult.getString(1));
                }
                return sql.toString();
            }
        }
    } catch (SQLException e) {
        throw new DBException(e, dataSource);
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)

Example 78 with JDBCResultSet

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

the class HSQLMetaModel method loadSequences.

@Override
public List<GenericSequence> loadSequences(@NotNull DBRProgressMonitor monitor, @NotNull GenericStructContainer container) throws DBException {
    try (JDBCSession session = DBUtils.openMetaSession(monitor, container.getDataSource(), "Read sequences")) {
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT * FROM INFORMATION_SCHEMA.SEQUENCES WHERE SEQUENCE_SCHEMA=?")) {
            dbStat.setString(1, container.getName());
            List<GenericSequence> result = new ArrayList<>();
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                while (dbResult.next()) {
                    String name = JDBCUtils.safeGetString(dbResult, "SEQUENCE_NAME");
                    if (name == null) {
                        continue;
                    }
                    GenericSequence sequence = new GenericSequence(container, name, null, JDBCUtils.safeGetLong(dbResult, "NEXT_VALUE"), JDBCUtils.safeGetLong(dbResult, "MINIMUM_VALUE"), JDBCUtils.safeGetLong(dbResult, "MAXIMUM_VALUE"), JDBCUtils.safeGetLong(dbResult, "INCREMENT"));
                    result.add(sequence);
                }
            }
            return result;
        }
    } catch (SQLException e) {
        throw new DBException(e, container.getDataSource());
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) ArrayList(java.util.ArrayList)

Example 79 with JDBCResultSet

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

the class ExasolStructureAssistant method searchTables.

// --------------
// Helper Classes
// --------------
private void searchTables(JDBCSession session, ExasolSchema schema, String searchObjectNameMask, List<ExasolObjectType> exasolObjectTypes, int maxResults, List<DBSObjectReference> objects, int nbResults) throws SQLException, DBException {
    String sql;
    if (schema != null) {
        sql = String.format(SQL_TABLES_SCHEMA, ExasolUtils.quoteString(schema.getName()), ExasolUtils.quoteString(searchObjectNameMask), buildTableTypes(exasolObjectTypes));
    } else {
        sql = String.format(SQL_TABLES_ALL, ExasolUtils.quoteString(searchObjectNameMask), buildTableTypes(exasolObjectTypes));
    }
    try (JDBCStatement dbStat = session.createStatement()) {
        dbStat.setFetchSize(DBConstants.METADATA_FETCH_SIZE);
        String schemaName;
        String objectName;
        ExasolSchema exasolSchema;
        ExasolObjectType objectType;
        try (JDBCResultSet dbResult = dbStat.executeQuery(sql)) {
            while (dbResult.next()) {
                if (session.getProgressMonitor().isCanceled()) {
                    break;
                }
                if (nbResults++ >= maxResults) {
                    break;
                }
                schemaName = JDBCUtils.safeGetStringTrimmed(dbResult, "TABLE_SCHEM");
                objectName = JDBCUtils.safeGetString(dbResult, "TABLE_NAME");
                exasolSchema = dataSource.getSchema(session.getProgressMonitor(), schemaName);
                if (exasolSchema == null) {
                    LOG.debug("Schema '" + schemaName + "' not found. Probably was filtered");
                    continue;
                }
                objectType = ExasolObjectType.TABLE;
                objects.add(new ExasolObjectReference(objectName, exasolSchema, objectType));
            }
        }
    }
}
Also used : ExasolObjectType(org.jkiss.dbeaver.ext.exasol.editors.ExasolObjectType) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)

Example 80 with JDBCResultSet

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

the class JDBCCompositeCache method loadObjects.

protected synchronized void loadObjects(DBRProgressMonitor monitor, OWNER owner, PARENT forParent) throws DBException {
    synchronized (objectCache) {
        if ((forParent == null && isFullyCached()) || (forParent != null && (!forParent.isPersisted() || objectCache.containsKey(forParent)))) {
            return;
        }
    }
    // Load tables and columns first
    if (forParent == null) {
        parentCache.loadObjects(monitor, owner);
        parentCache.loadChildren(monitor, owner, null);
    }
    Map<PARENT, Map<String, ObjectInfo>> parentObjectMap = new LinkedHashMap<>();
    // Load index columns
    DBPDataSource dataSource = owner.getDataSource();
    assert (dataSource != null);
    try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Load composite objects")) {
        JDBCStatement dbStat = prepareObjectsStatement(session, owner, forParent);
        dbStat.setFetchSize(DBConstants.METADATA_FETCH_SIZE);
        try {
            dbStat.executeStatement();
            JDBCResultSet dbResult = dbStat.getResultSet();
            if (dbResult != null)
                try {
                    while (dbResult.next()) {
                        if (monitor.isCanceled()) {
                            break;
                        }
                        String parentName = parentColumnName instanceof Number ? JDBCUtils.safeGetString(dbResult, ((Number) parentColumnName).intValue()) : JDBCUtils.safeGetString(dbResult, parentColumnName.toString());
                        String objectName = objectColumnName instanceof Number ? JDBCUtils.safeGetString(dbResult, ((Number) objectColumnName).intValue()) : JDBCUtils.safeGetString(dbResult, objectColumnName.toString());
                        if (CommonUtils.isEmpty(objectName)) {
                            // Use default name
                            objectName = getDefaultObjectName(dbResult, parentName);
                        }
                        if (forParent == null && CommonUtils.isEmpty(parentName)) {
                            // No parent - can't evaluate it
                            log.debug("Empty parent name in " + this);
                            continue;
                        }
                        PARENT parent = forParent;
                        if (parent == null) {
                            parent = parentCache.getObject(monitor, owner, parentName, parentType);
                            if (parent == null) {
                                log.debug("Object '" + objectName + "' owner '" + parentName + "' not found");
                                continue;
                            }
                        }
                        synchronized (objectCache) {
                            if (objectCache.containsKey(parent)) {
                                // Already cached
                                continue;
                            }
                        }
                        // Add to map
                        Map<String, ObjectInfo> objectMap = parentObjectMap.get(parent);
                        if (objectMap == null) {
                            objectMap = new TreeMap<>();
                            parentObjectMap.put(parent, objectMap);
                        }
                        ObjectInfo objectInfo = objectMap.get(objectName);
                        if (objectInfo == null) {
                            OBJECT object = fetchObject(session, owner, parent, objectName, dbResult);
                            if (object == null) {
                                // Can't fetch object
                                continue;
                            }
                            objectName = object.getName();
                            objectInfo = new ObjectInfo(object);
                            objectMap.put(objectName, objectInfo);
                        }
                        ROW_REF[] rowRef = fetchObjectRow(session, parent, objectInfo.object, dbResult);
                        if (rowRef == null || rowRef.length == 0) {
                            // At least one of rows is broken.
                            // So entire object is broken, let's just skip it.
                            objectInfo.broken = true;
                            //log.debug("Object '" + objectName + "' metadata corrupted - NULL child returned");
                            continue;
                        }
                        Collections.addAll(objectInfo.rows, rowRef);
                    }
                } finally {
                    dbResult.close();
                }
        } finally {
            dbStat.close();
        }
    } catch (SQLException ex) {
        throw new DBException(ex, dataSource);
    }
    if (monitor.isCanceled()) {
        return;
    }
    // Fill global cache
    synchronized (this) {
        synchronized (objectCache) {
            if (forParent != null || !parentObjectMap.isEmpty()) {
                if (forParent == null) {
                    // Cache global object list
                    List<OBJECT> globalCache = new ArrayList<>();
                    for (Map<String, ObjectInfo> objMap : parentObjectMap.values()) {
                        if (objMap != null) {
                            for (ObjectInfo info : objMap.values()) {
                                if (!info.broken) {
                                    globalCache.add(info.object);
                                }
                            }
                        }
                    }
                    // Save precached objects in global cache
                    for (List<OBJECT> objects : objectCache.values()) {
                        globalCache.addAll(objects);
                    }
                    // Add precached objects to global cache too
                    super.setCache(globalCache);
                    this.invalidateObjects(monitor, owner, new CacheIterator());
                }
            }
            // All objects are read. Now assign them to parents
            for (Map.Entry<PARENT, Map<String, ObjectInfo>> colEntry : parentObjectMap.entrySet()) {
                if (colEntry.getValue() == null || objectCache.containsKey(colEntry.getKey())) {
                    // Do not overwrite this object's cache
                    continue;
                }
                Collection<ObjectInfo> objectInfos = colEntry.getValue().values();
                ArrayList<OBJECT> objects = new ArrayList<>(objectInfos.size());
                for (ObjectInfo objectInfo : objectInfos) {
                    objectInfo.needsCaching = true;
                    objects.add(objectInfo.object);
                }
                objectCache.put(colEntry.getKey(), objects);
            }
            // Now set empty object list for other parents
            if (forParent == null) {
                for (PARENT tmpParent : parentCache.getTypedObjects(monitor, owner, parentType)) {
                    if (!parentObjectMap.containsKey(tmpParent) && !objectCache.containsKey(tmpParent)) {
                        objectCache.put(tmpParent, new ArrayList<OBJECT>());
                    }
                }
            } else if (!parentObjectMap.containsKey(forParent) && !objectCache.containsKey(forParent)) {
                objectCache.put(forParent, new ArrayList<OBJECT>());
            }
        }
        // Cache children lists (we do it in the end because children caching may operate with other model objects)
        for (Map.Entry<PARENT, Map<String, ObjectInfo>> colEntry : parentObjectMap.entrySet()) {
            for (ObjectInfo objectInfo : colEntry.getValue().values()) {
                if (objectInfo.needsCaching) {
                    cacheChildren(monitor, objectInfo.object, objectInfo.rows);
                }
            }
        }
        for (Map.Entry<PARENT, Map<String, ObjectInfo>> colEntry : parentObjectMap.entrySet()) {
            for (ObjectInfo objectInfo : colEntry.getValue().values()) {
                if (objectInfo.needsCaching) {
                    cacheChildren2(monitor, objectInfo.object, objectInfo.rows);
                }
            }
        }
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)

Aggregations

JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)82 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)66 SQLException (java.sql.SQLException)57 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)54 DBException (org.jkiss.dbeaver.DBException)53 ArrayList (java.util.ArrayList)20 DBCException (org.jkiss.dbeaver.model.exec.DBCException)15 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)12 JDBCStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement)10 AbstractObjectReference (org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference)10 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)8 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)5 GenericMetaObject (org.jkiss.dbeaver.ext.generic.model.meta.GenericMetaObject)4 NotNull (org.jkiss.code.NotNull)3 DB2XMLString (org.jkiss.dbeaver.ext.db2.info.DB2XMLString)3 DB2DataSource (org.jkiss.dbeaver.ext.db2.model.DB2DataSource)3 GenericDataSource (org.jkiss.dbeaver.ext.generic.model.GenericDataSource)3 Matcher (java.util.regex.Matcher)2 DB2Parameter (org.jkiss.dbeaver.ext.db2.info.DB2Parameter)2 DB2Schema (org.jkiss.dbeaver.ext.db2.model.DB2Schema)2