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;
}
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;
}
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();
}
}
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;
}
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;
}
Aggregations