Search in sources :

Example 31 with JDBCStatement

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

the class SQLServerExecutionContext method refreshDefaults.

@Override
public boolean refreshDefaults(DBRProgressMonitor monitor, boolean useBootstrapSettings) throws DBException {
    // Check default active schema
    try (JDBCSession session = openSession(monitor, DBCExecutionPurpose.META, "Query active schema and database")) {
        String currentDatabase = null;
        try {
            try (JDBCStatement dbStat = session.createStatement()) {
                try (JDBCResultSet dbResult = dbStat.executeQuery("SELECT db_name(), schema_name(), original_login()")) {
                    dbResult.next();
                    currentDatabase = dbResult.getString(1);
                    activeSchemaName = dbResult.getString(2);
                    currentUser = dbResult.getString(3);
                }
            }
        } catch (Throwable e) {
            log.debug("Error getting current user: " + e.getMessage());
        }
        if (CommonUtils.isEmpty(activeSchemaName)) {
            activeSchemaName = SQLServerConstants.DEFAULT_SCHEMA_NAME;
        }
        if (useBootstrapSettings) {
            DBPConnectionBootstrap bootstrap = getBootstrapSettings();
            if (!CommonUtils.isEmpty(bootstrap.getDefaultCatalogName()) && supportsCatalogChange()) {
                setCurrentDatabase(monitor, bootstrap.getDefaultCatalogName());
            }
        /*
                if (!CommonUtils.isEmpty(bootstrap.getDefaultSchemaName()) && supportsSchemaChange()) {
                    setCurrentSchema(monitor, bootstrap.getDefaultSchemaName());
                }
*/
        }
        if (!CommonUtils.isEmpty(currentDatabase) && !CommonUtils.equalObjects(currentDatabase, activeDatabaseName)) {
            activeDatabaseName = currentDatabase;
            return true;
        }
    }
    return false;
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBPConnectionBootstrap(org.jkiss.dbeaver.model.connection.DBPConnectionBootstrap) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)

Example 32 with JDBCStatement

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

the class SQLServerToolWithStatus method getExecuteStatistics.

@Override
public List<ToolStatus> getExecuteStatistics(OBJECT_TYPE object, SETTINGS settings, DBEPersistAction action, DBCSession session, DBCStatement dbStat) throws DBCException {
    List<ToolStatus> statusList = new ArrayList<>();
    try {
        int warnNum = 0;
        SQLWarning warning = ((JDBCStatement) dbStat).getWarnings();
        while (warning != null) {
            statusList.add(new ToolStatus(object, warning.getMessage()));
            warnNum++;
            warning = warning.getNextWarning();
        }
        if (warnNum == 0) {
            statusList.add(new ToolStatus(object, "Done"));
        }
    } catch (SQLException e) {
    // ignore
    }
    return statusList;
}
Also used : SQLWarning(java.sql.SQLWarning) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList)

Example 33 with JDBCStatement

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

the class ExasolUtils method generateDDLforTable.

public static String generateDDLforTable(DBRProgressMonitor monitor, ExasolDataSource dataSource, ExasolTable exasolTable) throws DBException {
    StringBuilder ddlOutput = new StringBuilder();
    ddlOutput.append("CREATE TABLE ").append(exasolTable.getFullyQualifiedName(DBPEvaluationContext.DDL)).append(" (");
    try (JDBCSession session = DBUtils.openMetaSession(monitor, exasolTable, "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<>();
            // distribution key infos
            List<String> distKey = new ArrayList<>();
            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").append(DBUtils.getQuotedIdentifier(dataSource, CommonUtils.notEmpty(rs.getString("COLUMN_NAME")))).append(" ").append(rs.getString("COLUMN_TYPE"));
                // has default value?
                String columnDefault = rs.getString("COLUMN_DEFAULT");
                if (columnDefault != null)
                    columnString.append(" DEFAULT ").append(columnDefault);
                // has identity
                BigDecimal bigDecimal = rs.getBigDecimal("COLUMN_IDENTITY");
                if (bigDecimal != null)
                    columnString.append(" IDENTITY ").append(bigDecimal.toString());
                // has identity
                if (!rs.getBoolean("COLUMN_IS_NULLABLE"))
                    columnString.append(" NOT NULL");
                // comment
                String columnComment = rs.getString("COLUMN_COMMENT");
                if (columnComment != null)
                    // replace ' to double ' -> escape for SQL
                    columnString.append(" COMMENT IS '").append(columnComment.replaceAll("'", "''")).append("'");
                // 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 ").append(CommonUtils.joinStrings(",", distKey));
            }
            ddlOutput.append("\n);\n");
        }
        // partitioning
        ddlOutput.append(getPartitionDdl(exasolTable, monitor));
        // ddlOutput.append(";\n"); //partition expression has ; already
        // 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;
            pk = pks.iterator().next();
            ddlOutput.append("\n").append(getPKDdl(pk, monitor)).append(";\n");
        }
        // foreign key
        Collection<ExasolTableForeignKey> fks = exasolTable.getAssociations(monitor);
        if (fks != null && fks.size() > 0) {
            // look keys
            for (ExasolTableForeignKey fk : fks) {
                ddlOutput.append("\n").append(getFKDdl(fk, monitor)).append(";\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) BigDecimal(java.math.BigDecimal) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)

Example 34 with JDBCStatement

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

the class ExasolTableUniqueKeyCache method prepareObjectsStatement.

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

Example 35 with JDBCStatement

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

the class ExasolJDBCObjectSimpleCacheLiterals method prepareObjectsStatement.

@NotNull
@SuppressWarnings("rawtypes")
@Override
protected JDBCStatement prepareObjectsStatement(@NotNull JDBCSession session, @NotNull OWNER owner) throws SQLException {
    JDBCStatement dbStat = session.createStatement();
    // escape parameters
    for (int i = 0; i < queryParameters.length; i++) {
        queryParameters[i] = (Object) ExasolUtils.quoteString(queryParameters[i].toString());
    }
    executeQuery = String.format(query, queryParameters);
    ((JDBCStatementImpl) dbStat).setQueryString(executeQuery);
    return dbStat;
}
Also used : JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) JDBCStatementImpl(org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl) 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