Search in sources :

Example 26 with JDBCSession

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

the class OracleTablePhysical method getPartitionInfo.

@PropertyGroup
@LazyProperty(cacheValidator = PartitionInfoValidator.class)
public PartitionInfo getPartitionInfo(DBRProgressMonitor monitor) throws DBException {
    if (partitionInfo == null && partitioned) {
        try (final JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Load partitioning info")) {
            try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT * FROM ALL_PART_TABLES WHERE OWNER=? AND TABLE_NAME=?")) {
                dbStat.setString(1, getContainer().getName());
                dbStat.setString(2, getName());
                try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                    if (dbResult.next()) {
                        partitionInfo = new PartitionInfo(monitor, this.getDataSource(), dbResult);
                    }
                }
            }
        } catch (SQLException e) {
            throw new DBException(e, getDataSource());
        }
    }
    return partitionInfo;
}
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 27 with JDBCSession

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

the class HSQLMetaModel method getProcedureDDL.

@Override
public String getProcedureDDL(DBRProgressMonitor monitor, GenericProcedure sourceObject) throws DBException {
    GenericDataSource dataSource = sourceObject.getDataSource();
    try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Read HSQLDB procedure source")) {
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT ROUTINE_DEFINITION FROM INFORMATION_SCHEMA.ROUTINES " + "WHERE ROUTINE_SCHEMA=? AND ROUTINE_NAME=?")) {
            dbStat.setString(1, sourceObject.getContainer().getName());
            dbStat.setString(2, sourceObject.getName());
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                if (dbResult.nextRow()) {
                    String definition = dbResult.getString(1);
                    if (definition != null) {
                        definition = SQLUtils.formatSQL(dataSource, definition);
                    }
                    return definition;
                }
                return "-- HSQLDB procedure definition not found";
            }
        }
    } 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 28 with JDBCSession

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

the class NetezzaMetaModel method getProcedureDDL.

@Override
public String getProcedureDDL(DBRProgressMonitor monitor, GenericProcedure sourceObject) throws DBException {
    GenericDataSource dataSource = sourceObject.getDataSource();
    try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Read Netezza procedure source")) {
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT p.proceduresignature,p.returns,p.proceduresource " + "FROM _v_procedure p " + "WHERE p.owner=? AND p.procedure=?")) {
            dbStat.setString(1, sourceObject.getContainer().getName());
            dbStat.setString(2, sourceObject.getName());
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                if (dbResult.nextRow()) {
                    return "CREATE OR REPLACE PROCEDURE " + dbResult.getString(1) + " RETURNS " + dbResult.getString(2) + "LANGUAGE NZPLSQL AS BEGIN_PROC\n" + dbResult.getString(3) + "\nEND_PROC;";
                }
                return "-- Netezza procedure source not found";
            }
        }
    } 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) GenericDataSource(org.jkiss.dbeaver.ext.generic.model.GenericDataSource)

Example 29 with JDBCSession

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

the class MySQLHelpProvider method loadTopics.

private void loadTopics(DBRProgressMonitor monitor) {
    try (final JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Read MySQL help topicc")) {
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT name, description, example, url FROM mysql.help_topic")) {
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                while (dbResult.next()) {
                    String topicName = dbResult.getString(1);
                    SQLHelpTopic helpTopic = new SQLHelpTopic();
                    helpTopic.setContents("<pre>" + dbResult.getString(2) + "</pre>");
                    helpTopic.setExample(dbResult.getString(3));
                    helpTopic.setUrl(dbResult.getString(4));
                    if (topicName != null) {
                        synchronized (topicCache) {
                            topicCache.put(topicName.toUpperCase(Locale.ENGLISH), helpTopic);
                        }
                    }
                }
            }
        } catch (SQLException e) {
            log.error("Error reading help topics", e);
        }
    } finally {
        isLoaded = true;
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) SQLHelpTopic(org.jkiss.dbeaver.model.sql.SQLHelpTopic)

Example 30 with JDBCSession

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

the class MySQLUser method getGrants.

public List<MySQLGrant> getGrants(DBRProgressMonitor monitor) throws DBException {
    if (this.grants != null) {
        return this.grants;
    }
    if (!isPersisted()) {
        this.grants = new ArrayList<>();
        return this.grants;
    }
    try (JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Read catalog privileges")) {
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SHOW GRANTS FOR " + getFullName())) {
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                List<MySQLGrant> grants = new ArrayList<>();
                while (dbResult.next()) {
                    List<MySQLPrivilege> privileges = new ArrayList<>();
                    boolean allPrivilegesFlag = false;
                    boolean grantOption = false;
                    String catalog = null;
                    String table = null;
                    String grantString = CommonUtils.notEmpty(JDBCUtils.safeGetString(dbResult, 1)).trim().toUpperCase(Locale.ENGLISH);
                    if (grantString.endsWith(" WITH GRANT OPTION")) {
                        //privileges.add(getDataSource().getPrivilege(monitor, MySQLPrivilege.GRANT_PRIVILEGE));
                        grantOption = true;
                    }
                    String privString;
                    Matcher matcher = MySQLGrant.TABLE_GRANT_PATTERN.matcher(grantString);
                    if (matcher.find()) {
                        privString = matcher.group(1);
                        catalog = matcher.group(2);
                        table = matcher.group(3);
                    } else {
                        matcher = MySQLGrant.GLOBAL_GRANT_PATTERN.matcher(grantString);
                        if (matcher.find()) {
                            privString = matcher.group(1);
                        } else {
                            log.warn("Can't parse GRANT string: " + grantString);
                            continue;
                        }
                    }
                    StringTokenizer st = new StringTokenizer(privString, ",");
                    while (st.hasMoreTokens()) {
                        String privName = st.nextToken().trim();
                        if (privName.equalsIgnoreCase(MySQLPrivilege.ALL_PRIVILEGES)) {
                            allPrivilegesFlag = true;
                            continue;
                        }
                        MySQLPrivilege priv = getDataSource().getPrivilege(monitor, privName);
                        if (priv == null) {
                            log.warn("Can't find privilege '" + privName + "'");
                        } else {
                            privileges.add(priv);
                        }
                    }
                    grants.add(new MySQLGrant(this, privileges, catalog, table, allPrivilegesFlag, grantOption));
                }
                this.grants = grants;
                return this.grants;
            }
        }
    } catch (SQLException e) {
        throw new DBException(e, getDataSource());
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) Matcher(java.util.regex.Matcher) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) StringTokenizer(java.util.StringTokenizer) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)

Aggregations

JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)73 SQLException (java.sql.SQLException)67 DBException (org.jkiss.dbeaver.DBException)54 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)54 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)46 DBCException (org.jkiss.dbeaver.model.exec.DBCException)16 ArrayList (java.util.ArrayList)15 JDBCStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement)7 NotNull (org.jkiss.code.NotNull)5 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)5 DatabaseMetaData (java.sql.DatabaseMetaData)3 GenericDataSource (org.jkiss.dbeaver.ext.generic.model.GenericDataSource)3 JDBCCallableStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCCallableStatement)3 Matcher (java.util.regex.Matcher)2 DB2XMLString (org.jkiss.dbeaver.ext.db2.info.DB2XMLString)2 GenericMetaObject (org.jkiss.dbeaver.ext.generic.model.meta.GenericMetaObject)2 JDBCDatabaseMetaData (org.jkiss.dbeaver.model.exec.jdbc.JDBCDatabaseMetaData)2 Property (org.jkiss.dbeaver.model.meta.Property)2 DBSObjectType (org.jkiss.dbeaver.model.struct.DBSObjectType)2 DBSProcedureType (org.jkiss.dbeaver.model.struct.rdb.DBSProcedureType)2