use of eu.esdihumboldt.hale.io.jdbc.JDBCProvider in project hale by halestudio.
the class SQLSchemaPage method updateState.
private boolean updateState(boolean runQuery) {
boolean typeValid = false;
boolean sqlValid = false;
String error = null;
String message = null;
if (typeName != null) {
// check type name
String type = typeName.getText();
typeValid = type != null && !type.isEmpty();
if (typeValid) {
// check if the name already exists in the source schema
SchemaService schemas = HaleUI.getServiceProvider().getService(SchemaService.class);
if (schemas != null) {
TypeDefinition existing = schemas.getSchemas(SchemaSpaceID.SOURCE).getType(new QName(SQLSchemaReader.NAMESPACE, type));
if (existing != null) {
typeValid = false;
error = "An SQL query with this name already exists";
}
}
// also test for specific characters?
}
}
if (sqlQuery != null) {
// check SQL query
String sql = sqlQuery.getText();
sqlValid = sql != null && !sql.isEmpty();
if (sqlValid) {
String processedQuery;
try {
processedQuery = JDBCUtil.replaceVariables(sql, HaleUI.getServiceProvider());
} catch (Exception e) {
error = e.getLocalizedMessage();
sqlValid = false;
processedQuery = null;
}
// check if processed SQL query can be executed
if (runQuery && processedQuery != null) {
ImportProvider provider = getWizard().getProvider();
if (provider != null && provider instanceof JDBCProvider) {
Connection connection = null;
try {
try {
connection = ((JDBCProvider) provider).getConnection();
} catch (SQLException e) {
sqlValid = false;
error = "Could not establish database connection: " + e.getLocalizedMessage();
}
if (connection != null) {
try {
Statement statement = JDBCUtil.createReadStatement(connection, 1);
try {
ResultSet result = statement.executeQuery(processedQuery);
int columnCount = result.getMetaData().getColumnCount();
if (columnCount <= 0) {
sqlValid = false;
error = "Query result does not have any columns";
} else {
if (columnCount == 1) {
message = "Successfully tested query. It yields a result with a single column.";
} else {
message = MessageFormat.format("Successfully tested query. It yields a result with {0} columns.", columnCount);
}
}
} catch (SQLException e) {
sqlValid = false;
error = "Error querying database: " + e.getMessage();
} finally {
statement.close();
}
} catch (SQLException e) {
sqlValid = false;
error = "Could not create database statement: " + e.getMessage();
}
}
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// ignore
}
}
}
}
}
}
}
boolean complete = typeValid && sqlValid;
if (complete) {
error = null;
} else if (!typeValid && error == null) {
error = "Please provide a name for the query";
} else if (error == null) {
error = "Please specify the SQL query to use";
}
setMessage(message);
setErrorMessage(error);
setPageComplete(complete);
return complete;
}
Aggregations