use of herddb.model.TableDoesNotExistException in project herddb by diennea.
the class JSQLParserPlanner method buildCreateIndexStatement.
private Statement buildCreateIndexStatement(String defaultTableSpace, CreateIndex s) throws StatementExecutionException {
try {
String tableSpace = s.getTable().getSchemaName();
if (tableSpace == null) {
tableSpace = defaultTableSpace;
}
tableSpace = fixMySqlBackTicks(tableSpace);
String tableName = fixMySqlBackTicks(s.getTable().getName().toLowerCase());
String indexName = fixMySqlBackTicks(s.getIndex().getName().toLowerCase());
boolean unique = isUnique(s.getIndex().getType());
String indexType = convertIndexType(s.getIndex().getType());
herddb.model.Index.Builder builder = herddb.model.Index.builder().name(indexName).uuid(UUID.randomUUID().toString()).type(indexType).unique(unique).table(tableName).tablespace(tableSpace);
AbstractTableManager tableDefinition = manager.getTableSpaceManager(tableSpace).getTableManager(tableName);
if (tableDefinition == null) {
throw new TableDoesNotExistException("no such table " + tableName + " in tablespace " + tableSpace);
}
for (String columnName : s.getIndex().getColumnsNames()) {
columnName = fixMySqlBackTicks(columnName.toLowerCase());
Column column = tableDefinition.getTable().getColumn(columnName);
if (column == null) {
throw new StatementExecutionException("no such column " + columnName + " on table " + tableName + " in tablespace " + tableSpace);
}
builder.column(column.name, column.type);
}
CreateIndexStatement statement = new CreateIndexStatement(builder.build());
return statement;
} catch (IllegalArgumentException err) {
throw new StatementExecutionException("bad index definition: " + err.getMessage(), err);
}
}
use of herddb.model.TableDoesNotExistException in project herddb by diennea.
the class JSQLParserPlanner method getTable.
private Table getTable(String defaultTableSpace, net.sf.jsqlparser.schema.Table table) {
String tableSpace = table.getSchemaName();
if (tableSpace == null) {
tableSpace = defaultTableSpace;
}
tableSpace = fixMySqlBackTicks(tableSpace);
TableSpaceManager tableSpaceManager = getTableSpaceManager(tableSpace);
if (tableSpaceManager == null) {
clearCache();
throw new StatementExecutionException("tablespace " + defaultTableSpace + " is not available");
}
String tableName = fixMySqlBackTicks(table.getName().toLowerCase());
AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableName);
if (tableManager == null) {
throw new TableDoesNotExistException("no table " + tableName + " here for " + tableSpace);
}
return tableManager.getTable();
}
use of herddb.model.TableDoesNotExistException in project herddb by diennea.
the class ShowCreateTableCalculator method calculate.
public static String calculate(boolean showCreateIndex, String tableName, String tableSpace, AbstractTableManager tableManager) {
Table t = tableManager.getTable();
if (t == null) {
throw new TableDoesNotExistException(String.format("Table %s does not exist.", tableName));
}
StringBuilder sb = new StringBuilder("CREATE TABLE " + tableSpace + "." + tableName);
StringJoiner joiner = new StringJoiner(",", "(", ")");
for (Column c : t.getColumns()) {
joiner.add(c.name + " " + ColumnTypes.typeToString(c.type) + autoIncrementColumn(t, c) + defaultClause(c));
}
if (t.getPrimaryKey().length > 0) {
joiner.add("PRIMARY KEY(" + String.join(",", t.getPrimaryKey()) + ")");
}
if (showCreateIndex) {
List<Index> indexes = tableManager.getAvailableIndexes();
if (!indexes.isEmpty()) {
indexes.forEach(idx -> {
if (idx.unique) {
joiner.add("UNIQUE KEY " + idx.name + " (" + String.join(",", idx.columnNames) + ")");
} else {
joiner.add("INDEX " + idx.name + "(" + String.join(",", idx.columnNames) + ")");
}
});
}
}
sb.append(joiner.toString());
return sb.toString();
}
use of herddb.model.TableDoesNotExistException in project herddb by diennea.
the class DropTableSQLTest method dropTableWithTransaction.
@Test
public void dropTableWithTransaction() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
long tx = ((TransactionResult) execute(manager, "EXECUTE begintransaction 'tblspace1'", Collections.emptyList())).getTransactionId();
execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string)", Collections.emptyList(), new TransactionContext(tx));
execute(manager, "INSERT INTO tblspace1.tsql (k1) values('a')", Collections.emptyList(), new TransactionContext(tx));
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
fail();
} catch (TableDoesNotExistException ok) {
}
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where table_name='tsql'", Collections.emptyList())) {
List<DataAccessor> all = scan.consume();
assertEquals(0, all.size());
}
execute(manager, "EXECUTE committransaction 'tblspace1'," + tx, Collections.emptyList());
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
List<DataAccessor> all = scan.consume();
assertEquals(1, all.size());
}
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where table_name='tsql'", Collections.emptyList())) {
List<DataAccessor> all = scan.consume();
assertEquals(1, all.size());
}
long tx2 = ((TransactionResult) execute(manager, "EXECUTE begintransaction 'tblspace1'", Collections.emptyList())).getTransactionId();
// name is not case sensitive
execute(manager, "DROP TABLE tblspace1.tSQL", Collections.emptyList(), new TransactionContext(tx2));
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
List<DataAccessor> all = scan.consume();
assertEquals(1, all.size());
}
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where table_name='tsql'", Collections.emptyList())) {
List<DataAccessor> all = scan.consume();
assertEquals(1, all.size());
}
execute(manager, "EXECUTE committransaction 'tblspace1'," + tx2, Collections.emptyList());
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where table_name='tsql'", Collections.emptyList())) {
List<DataAccessor> all = scan.consume();
assertEquals(0, all.size());
}
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
fail();
} catch (TableDoesNotExistException ok) {
assertTrue(manager.getPlanner() instanceof JSQLParserPlanner);
} catch (StatementExecutionException ok) {
assertEquals("From line 1, column 15 to line 1, column 28: Object 'TSQL' not found within 'tblspace1'", ok.getMessage());
assertTrue(manager.getPlanner() instanceof CalcitePlanner);
}
}
}
use of herddb.model.TableDoesNotExistException in project herddb by diennea.
the class SimpleTransactionTest method rollbackCreateTable.
@Test
public void rollbackCreateTable() throws Exception {
Bytes key = Bytes.from_int(1234);
Bytes value = Bytes.from_long(8888);
Table transacted_table = Table.builder().tablespace(tableSpace).name("t2").column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("id").build();
long tx = beginTransaction();
CreateTableStatement st_create = new CreateTableStatement(transacted_table);
manager.executeStatement(st_create, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx));
InsertStatement insert = new InsertStatement(tableSpace, "t2", new Record(key, value));
assertEquals(1, manager.executeUpdate(insert, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx)).getUpdateCount());
GetStatement get = new GetStatement(tableSpace, "t2", key, null, false);
GetResult result = manager.get(get, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx));
assertTrue(result.found());
assertEquals(key, result.getRecord().key);
assertEquals(value, result.getRecord().value);
RollbackTransactionStatement rollback = new RollbackTransactionStatement(tableSpace, tx);
manager.executeStatement(rollback, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
try {
manager.get(new GetStatement(tableSpace, "t2", key, null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
fail();
} catch (TableDoesNotExistException error) {
}
}
Aggregations