use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.
the class ConfigImportWizard method adaptConnectionUrl.
protected void adaptConnectionUrl(ImportConnectionInfo connectionInfo) throws DBException {
String sampleURL = connectionInfo.getDriverInfo().getSampleURL();
if (connectionInfo.getDriver() != null) {
sampleURL = connectionInfo.getDriver().getSampleURL();
}
//connectionInfo.getDriver()
String url = connectionInfo.getUrl();
if (url != null) {
// Parse url
final DriverDescriptor.MetaURL metaURL = DriverDescriptor.parseSampleURL(sampleURL);
int sourceOffset = 0;
List<String> urlComponents = metaURL.getUrlComponents();
for (int i = 0, urlComponentsSize = urlComponents.size(); i < urlComponentsSize; i++) {
String component = urlComponents.get(i);
if (component.length() > 2 && component.charAt(0) == '{' && component.charAt(component.length() - 1) == '}' && metaURL.getAvailableProperties().contains(component.substring(1, component.length() - 1))) {
// Property
int partEnd;
if (i < urlComponentsSize - 1) {
// Find next component
final String nextComponent = urlComponents.get(i + 1);
partEnd = url.indexOf(nextComponent, sourceOffset);
if (partEnd == -1) {
if (nextComponent.equals(":")) {
// Try to find another divider - dbvis sometimes contains bad sample URLs (e.g. for Oracle)
partEnd = url.indexOf("/", sourceOffset);
}
if (partEnd == -1) {
if (connectionInfo.getHost() == null) {
throw new DBException("Can't parse URL '" + url + "' with pattern '" + sampleURL + "'. String '" + nextComponent + "' not found after '" + component);
} else {
// We have connection properties anyway
url = null;
break;
}
}
}
} else {
partEnd = url.length();
}
String propertyValue = url.substring(sourceOffset, partEnd);
switch(component) {
case "{host}":
connectionInfo.setHost(propertyValue);
break;
case "{port}":
connectionInfo.setPort(propertyValue);
break;
case "{database}":
connectionInfo.setDatabase(propertyValue);
break;
default:
if (connectionInfo.getHost() == null) {
throw new DBException("Unsupported property " + component);
}
}
sourceOffset = partEnd;
} else {
// Static string
sourceOffset += component.length();
}
}
}
if (url == null) {
if (connectionInfo.getDriver() == null) {
throw new DBCException("Can't detect target driver for '" + connectionInfo.getAlias() + "'");
}
if (connectionInfo.getHost() == null) {
throw new DBCException("No URL and no host name - can't import connection '" + connectionInfo.getAlias() + "'");
}
// No URL - generate from props
DBPConnectionConfiguration conConfig = new DBPConnectionConfiguration();
conConfig.setHostName(connectionInfo.getHost());
conConfig.setHostPort(connectionInfo.getPort());
conConfig.setDatabaseName(connectionInfo.getDatabase());
url = connectionInfo.getDriver().getDataSourceProvider().getConnectionURL(connectionInfo.getDriver(), conConfig);
connectionInfo.setUrl(url);
}
}
use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.
the class MySQLView method loadAdditionalInfo.
private void loadAdditionalInfo(DBRProgressMonitor monitor) throws DBCException {
if (!isPersisted() || getContainer().isSystem()) {
additionalInfo.loaded = true;
return;
}
try (JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Load table status")) {
try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT * FROM " + MySQLConstants.META_TABLE_VIEWS + " WHERE " + MySQLConstants.COL_TABLE_SCHEMA + "=? AND " + MySQLConstants.COL_TABLE_NAME + "=?")) {
dbStat.setString(1, getContainer().getName());
dbStat.setString(2, getName());
try (JDBCResultSet dbResult = dbStat.executeQuery()) {
if (dbResult.next()) {
try {
additionalInfo.setCheckOption(CheckOption.valueOf(JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_CHECK_OPTION)));
} catch (IllegalArgumentException e) {
log.warn(e);
}
additionalInfo.setDefiner(JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_DEFINER));
additionalInfo.setDefinition(SQLUtils.formatSQL(getDataSource(), JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_VIEW_DEFINITION)));
additionalInfo.setUpdatable("YES".equals(JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_IS_UPDATABLE)));
}
additionalInfo.loaded = true;
}
}
} catch (SQLException e) {
throw new DBCException(e, getDataSource());
}
}
use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.
the class MySQLPlanAnalyser method explain.
public void explain(DBCSession session) throws DBCException {
String plainQuery = SQLUtils.stripComments(SQLUtils.getDialectFromObject(session.getDataSource()), query).toUpperCase();
if (!plainQuery.startsWith("SELECT")) {
throw new DBCException("Only SELECT statements could produce execution plan");
}
JDBCSession connection = (JDBCSession) session;
try {
try (JDBCPreparedStatement dbStat = connection.prepareStatement("EXPLAIN EXTENDED " + query)) {
try (JDBCResultSet dbResult = dbStat.executeQuery()) {
rootNodes = new ArrayList<>();
while (dbResult.next()) {
MySQLPlanNode node = new MySQLPlanNode(null, dbResult);
rootNodes.add(node);
}
}
}
} catch (SQLException e) {
throw new DBCException(e, session.getDataSource());
}
}
use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.
the class OracleContentBFILE method openFile.
private void openFile() throws DBCException {
if (opened) {
return;
}
try {
BeanUtils.invokeObjectMethod(bfile, "openFile");
opened = true;
} catch (Throwable e) {
throw new DBCException(e, dataSource);
}
}
use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.
the class OracleContentBFILE method getContents.
@Override
public DBDContentStorage getContents(DBRProgressMonitor monitor) throws DBCException {
if (storage == null && bfile != null) {
try {
openFile();
long contentLength = getContentLength();
DBPPlatform platform = dataSource.getContainer().getPlatform();
if (contentLength < platform.getPreferenceStore().getInt(ModelPreferences.MEMORY_CONTENT_MAX_SIZE)) {
try {
try (InputStream bs = getInputStream()) {
storage = BytesContentStorage.createFromStream(bs, contentLength, getDefaultEncoding());
}
} catch (IOException e) {
throw new DBCException("IO error while reading content", e);
}
} else {
// Create new local storage
File tempFile;
try {
tempFile = ContentUtils.createTempContentFile(monitor, platform, "blob" + bfile.hashCode());
} catch (IOException e) {
throw new DBCException("Can't create temporary file", e);
}
try (OutputStream os = new FileOutputStream(tempFile)) {
try (InputStream bs = getInputStream()) {
ContentUtils.copyStreams(bs, contentLength, os, monitor);
}
} catch (IOException e) {
ContentUtils.deleteTempFile(tempFile);
throw new DBCException("IO error while copying stream", e);
} catch (Throwable e) {
ContentUtils.deleteTempFile(tempFile);
throw new DBCException(e, dataSource);
}
this.storage = new TemporaryContentStorage(platform, tempFile, getDefaultEncoding());
}
// Free blob - we don't need it anymore
releaseBlob();
} finally {
closeFile();
}
}
return storage;
}
Aggregations