Search in sources :

Example 36 with JDBCSession

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

the class ExasolUtils method generateDDLforTable.

@SuppressWarnings("rawtypes")
public static String generateDDLforTable(DBRProgressMonitor monitor, ExasolDataSource dataSource, ExasolTable exasolTable) throws DBException {
    StringBuilder ddlOutput = new StringBuilder();
    ddlOutput.append("CREATE TABLE \"" + exasolTable.getSchema().getName() + "\".\"" + exasolTable.getName() + "\" (");
    try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Get Table DDL")) {
        try (JDBCStatement dbStat = session.createStatement()) {
            JDBCResultSet rs = dbStat.executeQuery(String.format(TABLE_QUERY_COLUMNS, quoteString(exasolTable.getSchema().getName()), quoteString(exasolTable.getName())));
            // column infos
            List<String> columns = new ArrayList<String>();
            // distribution key infos
            List<String> distKey = new ArrayList<String>();
            while (rs.next()) {
                StringBuilder columnString = new StringBuilder("");
                // double quotation mark for column as the name could be a
                // reserved word
                columnString.append("\n\t\t\"" + rs.getString("COLUMN_NAME") + "\" " + rs.getString("COLUMN_TYPE") + " ");
                // has default value?
                if (rs.getString("COLUMN_DEFAULT") != null)
                    columnString.append("DEFAULT " + rs.getString("COLUMN_DEFAULT") + " ");
                // has identity
                if (rs.getBigDecimal("COLUMN_IDENTITY") != null)
                    columnString.append("IDENTITY " + rs.getBigDecimal("COLUMN_IDENTITY").toString() + " ");
                // has identity
                if (!rs.getBoolean("COLUMN_IS_NULLABLE"))
                    columnString.append("NOT NULL ");
                // comment
                if (rs.getString("COLUMN_COMMENT") != null)
                    // replace ' to double ' -> escape for SQL
                    columnString.append("COMMENT IS '" + rs.getString("COLUMN_COMMENT").replaceAll("'", "''") + "'");
                // if distkey add column to distkey
                if (rs.getBoolean("COLUMN_IS_DISTRIBUTION_KEY"))
                    distKey.add(rs.getString("COLUMN_NAME"));
                columns.add(columnString.toString());
            }
            ddlOutput.append(CommonUtils.joinStrings(",", columns));
            // do we have a distkey?
            if (distKey.size() > 0) {
                ddlOutput.append(",\n\t\t DISTRIBUTE BY " + CommonUtils.joinStrings(",", distKey));
            }
            ddlOutput.append("\n);\n");
        }
        //primary key
        Collection<ExasolTableUniqueKey> pks = exasolTable.getConstraints(monitor);
        if (pks != null & pks.size() > 0) {
            //get only first as there is only 1 primary key
            ExasolTableUniqueKey pk = null;
            pk = pks.iterator().next();
            ArrayList<String> columns = new ArrayList<String>();
            for (DBSEntityAttributeRef c : pk.getAttributeReferences(monitor)) {
                columns.add("\"" + c.getAttribute().getName() + "\"");
            }
            ddlOutput.append("\nALTER TABLE \"" + exasolTable.getSchema().getName() + "\".\"" + exasolTable.getName() + "\" ADD CONSTRAINT " + pk.getName() + " PRIMARY KEY (" + CommonUtils.joinStrings(",", columns) + ") " + (pk.getEnabled() ? "ENABLE" : "") + " ;\n");
        }
        //foreign key
        Collection<ExasolTableForeignKey> fks = exasolTable.getAssociations(monitor);
        if (fks != null & fks.size() > 0) {
            //look keys
            for (ExasolTableForeignKey fk : fks) {
                ArrayList<String> columns = new ArrayList<String>();
                for (DBSEntityAttributeRef c : fk.getAttributeReferences(monitor)) {
                    columns.add("\"" + c.getAttribute().getName() + "\"");
                }
                ddlOutput.append("\nALTER TABLE \"" + exasolTable.getSchema().getName() + "\".\"" + exasolTable.getName() + "\" ADD CONSTRAINT " + fk.getName() + " FOREIGN KEY (" + CommonUtils.joinStrings(",", columns) + ") REFERENCES \"" + fk.getReferencedTable().getSchema().getName() + "\".\"" + fk.getReferencedTable().getName() + "\" " + (fk.getEnabled() ? "ENABLE" : "") + " ;\n");
            }
        }
        return ddlOutput.toString();
    } catch (SQLException e) {
        throw new DBException(e, dataSource);
    } finally {
        monitor.done();
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) DBSEntityAttributeRef(org.jkiss.dbeaver.model.struct.DBSEntityAttributeRef) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)

Example 37 with JDBCSession

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

the class ExasolTable method read.

private void read(DBRProgressMonitor monitor) throws DBCException {
    JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Read Table Details");
    try (JDBCStatement stmt = session.createStatement()) {
        String sql = String.format(readAdditionalInfo, ExasolUtils.quoteString(this.getSchema().getName()), ExasolUtils.quoteString(this.getName()), ExasolUtils.quoteString(this.getSchema().getName()), ExasolUtils.quoteString(this.getName()), ExasolUtils.quoteString(this.getSchema().getName()), ExasolUtils.quoteString(this.getName()));
        try (JDBCResultSet dbResult = stmt.executeQuery(sql)) {
            dbResult.next();
            this.hasDistKey = JDBCUtils.safeGetBoolean(dbResult, "TABLE_HAS_DISTRIBUTION_KEY");
            this.lastCommit = JDBCUtils.safeGetTimestamp(dbResult, "LAST_COMMIT");
            this.sizeRaw = JDBCUtils.safeGetLong(dbResult, "RAW_OBJECT_SIZE");
            this.sizeCompressed = JDBCUtils.safeGetLong(dbResult, "MEM_OBJECT_SIZE");
            this.deletePercentage = JDBCUtils.safeGetFloat(dbResult, "DELETE_PERCENTAGE");
            this.createTime = JDBCUtils.safeGetTimestamp(dbResult, "CREATED");
            this.hasRead = true;
        }
    } catch (SQLException e) {
        throw new DBCException(e, getDataSource());
    }
}
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)

Example 38 with JDBCSession

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

the class ExasolServerSessionManager method alterSession.

@Override
public void alterSession(DBCSession session, ExasolServerSession sessionType, Map<String, Object> options) throws DBException {
    try {
        String cmd = String.format(Boolean.TRUE.equals(options.get(PROP_KILL_QUERY)) ? KILL_STMT_CMD : KILL_APP_CMD, sessionType.getSessionID().toString());
        PreparedStatement dbStat = ((JDBCSession) session).prepareStatement(cmd);
        dbStat.execute();
    } catch (SQLException e) {
        throw new DBException(e, session.getDataSource());
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement)

Example 39 with JDBCSession

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

the class ExasolPlanAnalyser method explain.

public void explain(DBCSession session) throws DBCException {
    rootNodes = new ArrayList<>();
    JDBCSession connection = (JDBCSession) session;
    boolean oldAutoCommit = false;
    try {
        oldAutoCommit = connection.getAutoCommit();
        if (oldAutoCommit)
            connection.setAutoCommit(false);
        //alter session
        JDBCUtils.executeSQL(connection, "ALTER SESSION SET PROFILE = 'ON'");
        //execute query
        JDBCUtils.executeSQL(connection, query);
        //alter session
        JDBCUtils.executeSQL(connection, "ALTER SESSION SET PROFILE = 'OFF'");
        //rollback in case of DML
        connection.rollback();
        //alter session
        JDBCUtils.executeSQL(connection, "FLUSH STATISTICS");
        connection.commit();
        //retrieve execute info
        try (JDBCPreparedStatement stmt = connection.prepareStatement("SELECT * FROM EXA_USER_PROFILE_LAST_DAY WHERE SESSION_ID = CURRENT_SESSION AND STMT_ID = (select max(stmt_id) from EXA_USER_PROFILE_LAST_DAY where sql_text = ?)")) {
            stmt.setString(1, query);
            try (JDBCResultSet dbResult = stmt.executeQuery()) {
                while (dbResult.next()) {
                    ExasolPlanNode node = new ExasolPlanNode(null, dbResult);
                    rootNodes.add(node);
                }
            }
        }
    } catch (SQLException e) {
        throw new DBCException(e, session.getDataSource());
    } finally {
        //rollback changes because profile actually executes query and it could be INSERT/UPDATE
        try {
            connection.rollback();
            if (oldAutoCommit)
                connection.setAutoCommit(true);
        } catch (SQLException e) {
            LOG.error("Error closing plan analyser", e);
        }
    }
}
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 40 with JDBCSession

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

the class GenericCatalog method getSchemas.

public Collection<GenericSchema> getSchemas(DBRProgressMonitor monitor) throws DBException {
    if (schemas == null && !isInitialized) {
        try (JDBCSession session = DBUtils.openMetaSession(monitor, this.getDataSource(), "Load catalog schemas")) {
            this.schemas = this.getDataSource().loadSchemas(session, this);
            this.isInitialized = true;
        }
    }
    return schemas;
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)

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