}
private void createTable(VirtualDataWindowContext context) throws Exception {
Session session = null;
Exception error = null;
final String table = context.getNamedWindowName();
final String keyspace = context.getViewFactoryContext()
.getNamespaceName();
boolean tableExists = true;
try {
_log.info("Started checking whether table {} exists...", table);
session = _rootCluster.connect(keyspace);
session.execute(String.format("SELECT * FROM %s LIMIT 10", table));
_log.info("Finished checking whether table {} exists...", table);
_log.info("Table {} exists, nothing to do.", table);
}
// Keyspace does not exist, so we try to create it.
catch (InvalidQueryException ae) {
tableExists = false;
}
// Unknown exception -> error
catch (Exception e) {
tableExists = false;
_log.error("Unexcpected Exception when checking for table {}!",
table, e);
error = e;
}
if (error == null) {
_log.info("Finished checking whether table {} exists...", table);
try {
session = _rootCluster.connect(keyspace);
if (!tableExists) {
_log.info(
"Table does not yet {} exist, trying to create it",
table);
try {
_log.info("Started creating table {} ...", table);
final String createQuery = QueryStringBuilder
.createCreateTableQuery(context);
_log.debug("Executing create query: {}", createQuery);
session.execute(createQuery);
_log.info("Finished creating table {} .", table);
} catch (Exception e) {
_log.error("Error creating table {} .", table);
error = e;
}
}
if (_username != null && error == null) {
try {
_log.info("Started granting rights to table {} ...",
table);
final String grantQuery = String.format(
"GRANT ALL on %s TO %s", table, _username);
_log.debug("Executing grant query: {}", grantQuery);
session.execute(grantQuery);
_log.info("Finished granting rights to table {}.",
table);
} catch (Exception e) {
_log.error("Error granting rights to table {}.", table,
e);
error = e;
}
}
} catch (Exception e) {
_log.error("Error creating table {}", table, e);
error = e;
}
// In any case, shut down the session for the root user.
finally {
if (session != null) {
_log.info("Started shutting down session for creating table ...");
session.shutdown();
_log.info("Finished shutting down session for creating table.");
}
}
}
if (error != null) {