use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.
the class EntityEditor method saveCommandContext.
private boolean saveCommandContext(final DBRProgressMonitor monitor) {
int previewResult = IDialogConstants.PROCEED_ID;
if (DBeaverCore.getGlobalPreferenceStore().getBoolean(DBeaverPreferences.NAVIGATOR_SHOW_SQL_PREVIEW)) {
monitor.beginTask(CoreMessages.editors_entity_monitor_preview_changes, 1);
previewResult = showChanges(true);
monitor.done();
}
if (previewResult != IDialogConstants.PROCEED_ID) {
return true;
}
monitor.beginTask("Save entity", 1);
Throwable error = null;
final DBECommandContext commandContext = getCommandContext();
if (commandContext == null) {
log.warn("Null command context");
return true;
}
try {
commandContext.saveChanges(monitor);
} catch (DBException e) {
error = e;
}
if (getDatabaseObject() instanceof DBPStatefulObject) {
try {
((DBPStatefulObject) getDatabaseObject()).refreshObjectState(monitor);
} catch (DBCException e) {
// Just report an error
log.error(e);
}
}
if (error == null) {
// Refresh underlying node
// It'll refresh database object and all it's descendants
// So we'll get actual data from database
final DBNDatabaseNode treeNode = getEditorInput().getNavigatorNode();
try {
DBeaverUI.runInProgressService(new DBRRunnableWithProgress() {
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
treeNode.refreshNode(monitor, DBNEvent.FORCE_REFRESH);
} catch (DBException e) {
throw new InvocationTargetException(e);
}
}
});
} catch (InvocationTargetException e) {
error = e.getTargetException();
} catch (InterruptedException e) {
// ok
}
}
monitor.done();
if (error == null) {
return true;
} else {
// Try to handle error in nested editors
final Throwable vError = error;
DBeaverUI.syncExec(new Runnable() {
@Override
public void run() {
final IErrorVisualizer errorVisualizer = getAdapter(IErrorVisualizer.class);
if (errorVisualizer != null) {
errorVisualizer.visualizeError(monitor, vError);
}
}
});
// Show error dialog
DBeaverUI.asyncExec(new Runnable() {
@Override
public void run() {
UIUtils.showErrorDialog(getSite().getShell(), "Can't save '" + getDatabaseObject().getName() + "'", null, vError);
}
});
return false;
}
}
use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.
the class JDBCCollection method makeCollectionFromString.
@NotNull
public static DBDCollection makeCollectionFromString(@NotNull JDBCSession session, String value) throws DBCException {
String stringType = DBUtils.getDefaultDataTypeName(session.getDataSource(), DBPDataKind.STRING);
if (stringType == null) {
throw new DBCException("String data type not supported by database");
}
DBSDataType dataType = DBUtils.getLocalDataType(session.getDataSource(), stringType);
if (dataType == null) {
throw new DBCException("String data type '" + stringType + "' not supported by database");
}
DBDValueHandler valueHandler = DBUtils.findValueHandler(session, dataType);
// Try to divide on string elements
if (!CommonUtils.isEmpty(value)) {
if (value.startsWith("[") && value.endsWith("]")) {
// FIXME: use real parser (nested arrays, quotes escape, etc)
String arrayString = value.substring(1, value.length() - 1);
List<Object> items = new ArrayList<>();
StringTokenizer st = new StringTokenizer(arrayString, ",", false);
while (st.hasMoreTokens()) {
String token = st.nextToken().trim();
if (token.startsWith("\"") && token.endsWith("\"")) {
token = token.substring(1, token.length() - 1);
}
items.add(token);
}
return new JDBCCollectionString(dataType, valueHandler, value, items.toArray());
}
}
return new JDBCCollectionString(dataType, valueHandler, value);
}
use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.
the class JDBCCollection method makeCollectionFromArray.
/////////////////////////////////////////////////////////////////////////////////////
// Utilities
/////////////////////////////////////////////////////////////////////////////////////
@NotNull
public static JDBCCollection makeCollectionFromArray(@NotNull JDBCSession session, @NotNull DBSTypedObject column, Array array) throws DBCException {
DBRProgressMonitor monitor = session.getProgressMonitor();
DBSDataType elementType = null;
if (column instanceof DBSTypedObjectEx) {
DBSDataType arrayType = ((DBSTypedObjectEx) column).getDataType();
if (arrayType != null) {
elementType = arrayType.getComponentType(monitor);
}
}
if (elementType == null) {
try {
if (array == null) {
String arrayTypeName = column.getTypeName();
DBSDataType arrayType = session.getDataSource().resolveDataType(monitor, arrayTypeName);
if (arrayType != null) {
elementType = arrayType.getComponentType(monitor);
}
} else {
String baseTypeName = array.getBaseTypeName();
elementType = session.getDataSource().resolveDataType(monitor, baseTypeName);
}
} catch (Exception e) {
throw new DBCException("Error resolving data type", e);
}
}
try {
if (elementType == null) {
if (array == null) {
throw new DBCException("Can't resolve NULL array data type");
}
try {
return makeCollectionFromResultSet(session, array, null);
} catch (SQLException e) {
//$NON-NLS-1$
throw new DBCException(e, session.getDataSource());
}
}
try {
return makeCollectionFromArray(session, array, elementType);
} catch (SQLException e) {
if (array == null) {
//$NON-NLS-1$
throw new DBCException(e, session.getDataSource());
}
try {
return makeCollectionFromResultSet(session, array, elementType);
} catch (SQLException e1) {
//$NON-NLS-1$
throw new DBCException(e1, session.getDataSource());
}
}
} catch (DBException e) {
//$NON-NLS-1$
throw new DBCException("Can't extract array data from JDBC array", e);
}
}
use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.
the class JDBCCollection method getArrayValue.
public Array getArrayValue() throws DBCException {
Object[] attrs = new Object[contents.length];
for (int i = 0; i < contents.length; i++) {
Object attr = contents[i];
if (attr instanceof DBDValue) {
attr = ((DBDValue) attr).getRawValue();
}
attrs[i] = attr;
}
final DBSDataType dataType = getComponentType();
try (DBCSession session = DBUtils.openUtilSession(VoidProgressMonitor.INSTANCE, dataType.getDataSource(), "Create JDBC array")) {
if (session instanceof Connection) {
return ((Connection) session).createArrayOf(dataType.getTypeName(), attrs);
} else {
return new JDBCArrayImpl(dataType.getTypeName(), dataType.getTypeID(), attrs);
}
} catch (Throwable e) {
throw new DBCException("Error creating struct", e);
}
}
use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.
the class AbstractObjectManager method executePersistAction.
@Override
public void executePersistAction(DBCSession session, DBECommand<OBJECT_TYPE> command, DBEPersistAction action) throws DBException {
String script = action.getScript();
if (script == null) {
action.handleExecute(session, null);
} else {
DBCStatement dbStat = DBUtils.createStatement(session, script, false);
try {
dbStat.executeStatement();
action.handleExecute(session, null);
} catch (DBCException e) {
action.handleExecute(session, e);
throw e;
} finally {
dbStat.close();
}
}
}
Aggregations