use of org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet 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());
}
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet 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);
}
}
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet in project dbeaver by serge-rider.
the class GenericProcedure method loadProcedureColumns.
private void loadProcedureColumns(DBRProgressMonitor monitor) throws DBException {
Collection<? extends GenericProcedure> procedures = getContainer().getProcedures(monitor, getName());
if (procedures == null || !procedures.contains(this)) {
throw new DBException("Internal error - cannot read columns for procedure '" + getName() + "' because its not found in container");
}
Iterator<? extends GenericProcedure> procIter = procedures.iterator();
GenericProcedure procedure = null;
final GenericMetaObject pcObject = getDataSource().getMetaObject(GenericConstants.OBJECT_PROCEDURE_COLUMN);
try (JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Load procedure columns")) {
final JDBCResultSet dbResult;
if (functionResultType == null) {
dbResult = session.getMetaData().getProcedureColumns(getCatalog() == null ? this.getPackage() == null || !this.getPackage().isNameFromCatalog() ? null : this.getPackage().getName() : getCatalog().getName(), getSchema() == null ? null : getSchema().getName(), getName(), getDataSource().getAllObjectsPattern());
} else {
dbResult = session.getMetaData().getFunctionColumns(getCatalog() == null ? null : getCatalog().getName(), getSchema() == null ? null : getSchema().getName(), getName(), getDataSource().getAllObjectsPattern());
}
try {
int previousPosition = -1;
while (dbResult.next()) {
String columnName = GenericUtils.safeGetString(pcObject, dbResult, JDBCConstants.COLUMN_NAME);
int columnTypeNum = GenericUtils.safeGetInt(pcObject, dbResult, JDBCConstants.COLUMN_TYPE);
int valueType = GenericUtils.safeGetInt(pcObject, dbResult, JDBCConstants.DATA_TYPE);
String typeName = GenericUtils.safeGetString(pcObject, dbResult, JDBCConstants.TYPE_NAME);
int columnSize = GenericUtils.safeGetInt(pcObject, dbResult, JDBCConstants.LENGTH);
boolean notNull = GenericUtils.safeGetInt(pcObject, dbResult, JDBCConstants.NULLABLE) == DatabaseMetaData.procedureNoNulls;
int scale = GenericUtils.safeGetInt(pcObject, dbResult, JDBCConstants.SCALE);
int precision = GenericUtils.safeGetInt(pcObject, dbResult, JDBCConstants.PRECISION);
//int radix = GenericUtils.safeGetInt(dbResult, JDBCConstants.RADIX);
String remarks = GenericUtils.safeGetString(pcObject, dbResult, JDBCConstants.REMARKS);
int position = GenericUtils.safeGetInt(pcObject, dbResult, JDBCConstants.ORDINAL_POSITION);
DBSProcedureParameterKind parameterType;
if (functionResultType == null) {
switch(columnTypeNum) {
case DatabaseMetaData.procedureColumnIn:
parameterType = DBSProcedureParameterKind.IN;
break;
case DatabaseMetaData.procedureColumnInOut:
parameterType = DBSProcedureParameterKind.INOUT;
break;
case DatabaseMetaData.procedureColumnOut:
parameterType = DBSProcedureParameterKind.OUT;
break;
case DatabaseMetaData.procedureColumnReturn:
parameterType = DBSProcedureParameterKind.RETURN;
break;
case DatabaseMetaData.procedureColumnResult:
parameterType = DBSProcedureParameterKind.RESULTSET;
break;
default:
parameterType = DBSProcedureParameterKind.UNKNOWN;
break;
}
} else {
switch(columnTypeNum) {
case DatabaseMetaData.functionColumnIn:
parameterType = DBSProcedureParameterKind.IN;
break;
case DatabaseMetaData.functionColumnInOut:
parameterType = DBSProcedureParameterKind.INOUT;
break;
case DatabaseMetaData.functionColumnOut:
parameterType = DBSProcedureParameterKind.OUT;
break;
case DatabaseMetaData.functionReturn:
parameterType = DBSProcedureParameterKind.RETURN;
break;
case DatabaseMetaData.functionColumnResult:
parameterType = DBSProcedureParameterKind.RESULTSET;
break;
default:
parameterType = DBSProcedureParameterKind.UNKNOWN;
break;
}
}
if (CommonUtils.isEmpty(columnName) && parameterType == DBSProcedureParameterKind.RETURN) {
columnName = "RETURN";
}
if (position == 0) {
// Some drivers do not return ordinal position (PostgreSQL) but
// position is contained in column name
Matcher numberMatcher = PATTERN_COL_NAME_NUMERIC.matcher(columnName);
if (numberMatcher.matches()) {
position = Integer.parseInt(numberMatcher.group(1));
}
}
if (procedure == null || (previousPosition >= 0 && position <= previousPosition && procIter.hasNext())) {
procedure = procIter.next();
}
GenericProcedureParameter column = new GenericProcedureParameter(procedure, columnName, typeName, valueType, position, columnSize, scale, precision, notNull, remarks, parameterType);
procedure.addColumn(column);
previousPosition = position;
}
} finally {
dbResult.close();
}
} catch (SQLException e) {
throw new DBException(e, getDataSource());
}
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet in project dbeaver by serge-rider.
the class GenericStructureAssistant method findProceduresByMask.
private void findProceduresByMask(JDBCSession session, GenericCatalog catalog, GenericSchema schema, String procNameMask, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
final GenericMetaObject procObject = getDataSource().getMetaObject(GenericConstants.OBJECT_PROCEDURE);
DBRProgressMonitor monitor = session.getProgressMonitor();
try (JDBCResultSet dbResult = session.getMetaData().getProcedures(catalog == null ? null : catalog.getName(), schema == null ? null : schema.getName(), procNameMask)) {
while (dbResult.next()) {
if (monitor.isCanceled()) {
break;
}
String catalogName = GenericUtils.safeGetStringTrimmed(procObject, dbResult, JDBCConstants.PROCEDURE_CAT);
String schemaName = GenericUtils.safeGetStringTrimmed(procObject, dbResult, JDBCConstants.PROCEDURE_SCHEM);
String procName = GenericUtils.safeGetStringTrimmed(procObject, dbResult, JDBCConstants.PROCEDURE_NAME);
String uniqueName = GenericUtils.safeGetStringTrimmed(procObject, dbResult, JDBCConstants.SPECIFIC_NAME);
if (CommonUtils.isEmpty(procName)) {
continue;
}
if (CommonUtils.isEmpty(uniqueName)) {
uniqueName = procName;
}
objects.add(new ProcedureReference(findContainer(session.getProgressMonitor(), catalog, schema, catalogName, schemaName), catalogName, procName, uniqueName));
if (objects.size() >= maxResults) {
break;
}
}
}
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet in project dbeaver by serge-rider.
the class GenericStructureAssistant method findTablesByMask.
private void findTablesByMask(JDBCSession session, GenericCatalog catalog, GenericSchema schema, String tableNameMask, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
final GenericMetaObject tableObject = getDataSource().getMetaObject(GenericConstants.OBJECT_TABLE);
final DBRProgressMonitor monitor = session.getProgressMonitor();
try (JDBCResultSet dbResult = session.getMetaData().getTables(catalog == null ? null : catalog.getName(), schema == null ? null : schema.getName(), tableNameMask, null)) {
while (dbResult.next()) {
if (monitor.isCanceled()) {
break;
}
String catalogName = GenericUtils.safeGetStringTrimmed(tableObject, dbResult, JDBCConstants.TABLE_CAT);
String schemaName = GenericUtils.safeGetStringTrimmed(tableObject, dbResult, JDBCConstants.TABLE_SCHEM);
String tableName = GenericUtils.safeGetStringTrimmed(tableObject, dbResult, JDBCConstants.TABLE_NAME);
if (CommonUtils.isEmpty(tableName)) {
continue;
}
objects.add(new TableReference(findContainer(session.getProgressMonitor(), catalog, schema, catalogName, schemaName), tableName, GenericUtils.safeGetString(tableObject, dbResult, JDBCConstants.REMARKS)));
if (objects.size() >= maxResults) {
break;
}
}
}
}
Aggregations