use of org.jkiss.dbeaver.model.exec.jdbc.JDBCSession 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.JDBCSession in project dbeaver by serge-rider.
the class NetezzaMetaModel method getViewDDL.
public String getViewDDL(DBRProgressMonitor monitor, GenericTable sourceObject) throws DBException {
GenericDataSource dataSource = sourceObject.getDataSource();
try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Read Netezza view source")) {
try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT v.definition " + "FROM _v_view v, _v_objs_owned o " + "WHERE v.objid = o.objid AND o.DATABASE=? AND v.VIEWNAME=?")) {
dbStat.setString(1, sourceObject.getContainer().getName());
dbStat.setString(2, sourceObject.getName());
try (JDBCResultSet dbResult = dbStat.executeQuery()) {
if (dbResult.nextRow()) {
return "CREATE OR REPLACE VIEW " + sourceObject.getName() + " AS\n" + dbResult.getString(1);
}
return "-- Netezza view definition not found";
}
}
} catch (SQLException e) {
throw new DBException(e, dataSource);
}
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCSession in project dbeaver by serge-rider.
the class CompileHandler method compileUnit.
public static boolean compileUnit(DBRProgressMonitor monitor, DBCCompileLog compileLog, OracleSourceObject unit) throws DBCException {
final DBEPersistAction[] compileActions = unit.getCompileActions();
if (ArrayUtils.isEmpty(compileActions)) {
return true;
}
try (JDBCSession session = DBUtils.openUtilSession(monitor, unit.getDataSource(), "Compile '" + unit.getName() + "'")) {
boolean success = true;
for (DBEPersistAction action : compileActions) {
final String script = action.getScript();
compileLog.trace(script);
if (monitor.isCanceled()) {
break;
}
try {
try (DBCStatement dbStat = session.prepareStatement(DBCStatementType.QUERY, script, false, false, false)) {
dbStat.executeStatement();
}
action.handleExecute(session, null);
} catch (DBCException e) {
action.handleExecute(session, e);
throw e;
}
if (action instanceof OracleObjectPersistAction) {
if (!logObjectErrors(session, compileLog, unit, ((OracleObjectPersistAction) action).getObjectType())) {
success = false;
}
}
}
final DBSObjectState oldState = unit.getObjectState();
unit.refreshObjectState(monitor);
if (unit.getObjectState() != oldState) {
unit.getDataSource().getContainer().fireEvent(new DBPEvent(DBPEvent.Action.OBJECT_UPDATE, unit));
}
return success;
}
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCSession in project dbeaver by serge-rider.
the class OracleDataType method getComponentType.
@Property(viewable = true, order = 8)
public OracleDataType getComponentType(@NotNull DBRProgressMonitor monitor) throws DBCException {
if (componentType != null) {
return componentType;
}
OracleSchema schema = getSchema();
if (schema == null || !TYPE_CODE_COLLECTION.equals(typeCode) || !getDataSource().isAtLeastV10()) {
return null;
}
try (JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Load collection types")) {
try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT ELEM_TYPE_OWNER,ELEM_TYPE_NAME,ELEM_TYPE_MOD FROM SYS.ALL_COLL_TYPES WHERE OWNER=? AND TYPE_NAME=?")) {
dbStat.setString(1, schema.getName());
dbStat.setString(2, getName());
try (JDBCResultSet dbResults = dbStat.executeQuery()) {
if (dbResults.next()) {
String compTypeSchema = JDBCUtils.safeGetString(dbResults, "ELEM_TYPE_OWNER");
String compTypeName = JDBCUtils.safeGetString(dbResults, "ELEM_TYPE_NAME");
//String compTypeMod = JDBCUtils.safeGetString(dbResults, "ELEM_TYPE_MOD");
componentType = OracleDataType.resolveDataType(monitor, getDataSource(), compTypeSchema, compTypeName);
} else {
log.warn("Can't resolve collection type [" + getName() + "]");
}
}
}
} catch (Exception e) {
log.warn("Error reading collection types", e);
}
return componentType;
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCSession in project dbeaver by serge-rider.
the class OracleStructureAssistant method findObjectsByMask.
@NotNull
@Override
public List<DBSObjectReference> findObjectsByMask(DBRProgressMonitor monitor, DBSObject parentObject, DBSObjectType[] objectTypes, String objectNameMask, boolean caseSensitive, boolean globalSearch, int maxResults) throws DBException {
OracleSchema schema = parentObject instanceof OracleSchema ? (OracleSchema) parentObject : null;
try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Find objects by name")) {
List<DBSObjectReference> objects = new ArrayList<>();
// Search all objects
searchAllObjects(session, schema, objectNameMask, objectTypes, caseSensitive, maxResults, objects);
if (ArrayUtils.contains(objectTypes, OracleObjectType.CONSTRAINT, OracleObjectType.FOREIGN_KEY) && objects.size() < maxResults) {
// Search constraints
findConstraintsByMask(session, schema, objectNameMask, objectTypes, maxResults, objects);
}
// Sort objects. Put ones in the current schema first
final OracleSchema activeSchema = dataSource.getDefaultObject();
Collections.sort(objects, new Comparator<DBSObjectReference>() {
@Override
public int compare(DBSObjectReference o1, DBSObjectReference o2) {
if (CommonUtils.equalObjects(o1.getContainer(), o2.getContainer())) {
return o1.getName().compareTo(o2.getName());
}
if (o1.getContainer() == null || o1.getContainer() == activeSchema) {
return -1;
}
if (o2.getContainer() == null || o2.getContainer() == activeSchema) {
return 1;
}
return o1.getContainer().getName().compareTo(o2.getContainer().getName());
}
});
return objects;
} catch (SQLException ex) {
throw new DBException(ex, dataSource);
}
}
Aggregations