Search in sources :

Example 76 with JDBCPreparedStatement

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement 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 77 with JDBCPreparedStatement

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

the class OracleUtils method getDDL.

public static String getDDL(DBRProgressMonitor monitor, String objectType, DBSEntity object, OracleDDLFormat ddlFormat) throws DBException {
    String objectFullName = DBUtils.getObjectFullName(object, DBPEvaluationContext.DDL);
    OracleSchema schema = null;
    if (object instanceof OracleSchemaObject) {
        schema = ((OracleSchemaObject) object).getSchema();
    } else if (object instanceof OracleTableBase) {
        schema = ((OracleTableBase) object).getContainer();
    }
    final OracleDataSource dataSource = (OracleDataSource) object.getDataSource();
    monitor.beginTask("Load sources for " + objectType + " '" + objectFullName + "'...", 1);
    try (final JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Load source code for " + objectType + " '" + objectFullName + "'")) {
        if (dataSource.isAtLeastV9()) {
            JDBCUtils.executeProcedure(session, "begin DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'STORAGE'," + ddlFormat.isShowStorage() + "); end;");
            JDBCUtils.executeProcedure(session, "begin DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'TABLESPACE'," + ddlFormat.isShowTablespace() + ");  end;");
            JDBCUtils.executeProcedure(session, "begin DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'SEGMENT_ATTRIBUTES'," + ddlFormat.isShowSegments() + ");  end;");
        }
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT DBMS_METADATA.GET_DDL(?,?" + (schema == null ? "" : ",?") + ") TXT " + "FROM DUAL")) {
            dbStat.setString(1, objectType);
            dbStat.setString(2, object.getName());
            if (schema != null) {
                dbStat.setString(3, schema.getName());
            }
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                if (dbResult.next()) {
                    return dbResult.getString(1);
                } else {
                    log.warn("No DDL for " + objectType + " '" + objectFullName + "'");
                    return "-- EMPTY DDL";
                }
            }
        } finally {
        /*
                if (curSchema != null) {
                    setCurrentSchema(session, curSchema);
                }
*/
        }
    } catch (SQLException e) {
        if (object instanceof OracleTableBase) {
            log.error("Error generating Oracle DDL. Generate default.", e);
            return JDBCUtils.generateTableDDL(monitor, (OracleTableBase) object, true);
        } else {
            throw new DBException(e, dataSource);
        }
    } finally {
        monitor.done();
    }
}
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 JDBCPreparedStatement

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

the class OracleView method loadAdditionalInfo.

private void loadAdditionalInfo(DBRProgressMonitor monitor) throws DBException {
    if (!isPersisted()) {
        additionalInfo.loaded = true;
        return;
    }
    String viewText = null;
    try (JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Load table status")) {
        boolean isOracle9 = getDataSource().isAtLeastV9();
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT TEXT,TYPE_TEXT,OID_TEXT,VIEW_TYPE_OWNER,VIEW_TYPE" + (isOracle9 ? ",SUPERVIEW_NAME" : "") + "\n" + "FROM SYS.ALL_VIEWS WHERE OWNER=? AND VIEW_NAME=?")) {
            dbStat.setString(1, getContainer().getName());
            dbStat.setString(2, getName());
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                if (dbResult.next()) {
                    viewText = JDBCUtils.safeGetString(dbResult, "TEXT");
                    additionalInfo.setText(viewText);
                    additionalInfo.setTypeText(JDBCUtils.safeGetStringTrimmed(dbResult, "TYPE_TEXT"));
                    additionalInfo.setOidText(JDBCUtils.safeGetStringTrimmed(dbResult, "OID_TEXT"));
                    additionalInfo.typeOwner = JDBCUtils.safeGetStringTrimmed(dbResult, "VIEW_TYPE_OWNER");
                    additionalInfo.typeName = JDBCUtils.safeGetStringTrimmed(dbResult, "VIEW_TYPE");
                    if (isOracle9) {
                        String superViewName = JDBCUtils.safeGetString(dbResult, "SUPERVIEW_NAME");
                        if (!CommonUtils.isEmpty(superViewName)) {
                            additionalInfo.setSuperView(getContainer().getView(monitor, superViewName));
                        }
                    }
                } else {
                    log.warn("Cannot find view '" + getFullyQualifiedName(DBPEvaluationContext.UI) + "' metadata");
                }
                additionalInfo.loaded = true;
            }
        }
    } catch (SQLException e) {
        throw new DBCException(e, getDataSource());
    }
    if (viewText != null) {
        viewText = "CREATE OR REPLACE VIEW " + getFullyQualifiedName(DBPEvaluationContext.DDL) + " AS\n" + viewText;
    }
    additionalInfo.setText(viewText);
}
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) DBCException(org.jkiss.dbeaver.model.exec.DBCException)

Example 79 with JDBCPreparedStatement

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

the class OracleServerSessionManager method alterSession.

@Override
public void alterSession(DBCSession session, OracleServerSession sessionType, Map<String, Object> options) throws DBException {
    final boolean toKill = Boolean.TRUE.equals(options.get(PROP_KILL_SESSION));
    final boolean immediate = Boolean.TRUE.equals(options.get(PROP_IMMEDIATE));
    try {
        StringBuilder sql = new StringBuilder("ALTER SYSTEM ");
        if (toKill) {
            sql.append("KILL SESSION ");
        } else {
            sql.append("DISCONNECT SESSION ");
        }
        sql.append("'").append(sessionType.getSid()).append(',').append(sessionType.getSerial()).append("'");
        if (immediate) {
            sql.append(" IMMEDIATE");
        } else if (!toKill) {
            sql.append(" POST_TRANSACTION");
        }
        try (JDBCPreparedStatement dbStat = ((JDBCSession) session).prepareStatement(sql.toString())) {
            dbStat.execute();
        }
    } catch (SQLException e) {
        throw new DBException(e, session.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)

Example 80 with JDBCPreparedStatement

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

the class DB2Utils method readApplications.

// ---------------------
// DBA Data and Actions
// ---------------------
public static List<DB2ServerApplication> readApplications(DBRProgressMonitor monitor, JDBCSession session) throws SQLException {
    LOG.debug("readApplications");
    List<DB2ServerApplication> listApplications = new ArrayList<>();
    try (JDBCPreparedStatement dbStat = session.prepareStatement(SEL_APP)) {
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            while (dbResult.next()) {
                listApplications.add(new DB2ServerApplication(dbResult));
            }
        }
    }
    return listApplications;
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) ArrayList(java.util.ArrayList) DB2ServerApplication(org.jkiss.dbeaver.ext.db2.model.app.DB2ServerApplication)

Aggregations

JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)100 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)66 SQLException (java.sql.SQLException)49 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)46 DBException (org.jkiss.dbeaver.DBException)45 ArrayList (java.util.ArrayList)18 DBCException (org.jkiss.dbeaver.model.exec.DBCException)15 NotNull (org.jkiss.code.NotNull)12 AbstractObjectReference (org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference)10 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)10 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)8 DB2DataSource (org.jkiss.dbeaver.ext.db2.model.DB2DataSource)4 DB2XMLString (org.jkiss.dbeaver.ext.db2.info.DB2XMLString)3 GenericDataSource (org.jkiss.dbeaver.ext.generic.model.GenericDataSource)3 ResultSet (java.sql.ResultSet)2 DB2Parameter (org.jkiss.dbeaver.ext.db2.info.DB2Parameter)2 DB2Schema (org.jkiss.dbeaver.ext.db2.model.DB2Schema)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Clob (java.sql.Clob)1 SQLXML (java.sql.SQLXML)1