use of herddb.model.StatementExecutionException in project herddb by diennea.
the class SQLPlanner method buildCreateIndexStatement.
private Statement buildCreateIndexStatement(String defaultTableSpace, CreateIndex s) throws StatementExecutionException {
try {
String tableSpace = s.getTable().getSchemaName();
if (tableSpace == null) {
tableSpace = defaultTableSpace;
}
String tableName = s.getTable().getName();
String indexName = s.getIndex().getName().toLowerCase();
String indexType = convertIndexType(s.getIndex().getType());
herddb.model.Index.Builder builder = herddb.model.Index.builder().name(indexName).uuid(UUID.randomUUID().toString()).type(indexType).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 = 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.StatementExecutionException in project herddb by diennea.
the class SQLPlanner method buildUpdateStatement.
private ExecutionPlan buildUpdateStatement(String defaultTableSpace, Update s, boolean returnValues) throws StatementExecutionException {
if (s.getTables().size() != 1) {
throw new StatementExecutionException("unsupported multi-table update " + s);
}
net.sf.jsqlparser.schema.Table fromTable = s.getTables().get(0);
String tableSpace = fromTable.getSchemaName();
String tableName = fromTable.getName();
if (tableSpace == null) {
tableSpace = defaultTableSpace;
}
TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSpace);
if (tableSpaceManager == null) {
throw new StatementExecutionException("no such tablespace " + tableSpace + " here at " + manager.getNodeId());
}
AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableName);
if (tableManager == null) {
throw new StatementExecutionException("no such table " + tableName + " in tablespace " + tableSpace);
}
Table table = tableManager.getTable();
for (net.sf.jsqlparser.schema.Column c : s.getColumns()) {
Column column = table.getColumn(c.getColumnName());
if (column == null) {
throw new StatementExecutionException("no such column " + c.getColumnName() + " in table " + tableName + " in tablespace " + tableSpace);
}
if (table.isPrimaryKeyColumn(c.getColumnName())) {
throw new StatementExecutionException("updates of fields on the PK (" + Arrays.toString(table.primaryKey) + ") are not supported. Please perform a DELETE and than an INSERT");
}
}
List<CompiledSQLExpression> compiledSQLExpressions = new ArrayList<>();
for (Expression e : s.getExpressions()) {
compiledSQLExpressions.add(SQLExpressionCompiler.compileExpression(null, e));
}
RecordFunction function = new SQLRecordFunction(table, s.getColumns(), compiledSQLExpressions);
// Perform a scan and then update each row
SQLRecordPredicate where = s.getWhere() != null ? new SQLRecordPredicate(table, table.name, s.getWhere()) : null;
if (where != null) {
Expression expressionWhere = s.getWhere();
discoverIndexOperations(expressionWhere, table, table.name, where, tableSpaceManager);
}
DMLStatement st = new UpdateStatement(tableSpace, tableName, null, function, where).setReturnValues(returnValues);
return ExecutionPlan.simple(st);
}
use of herddb.model.StatementExecutionException in project herddb by diennea.
the class SQLPlanner method buildDeleteStatement.
private ExecutionPlan buildDeleteStatement(String defaultTableSpace, Delete s, boolean returnValues) throws StatementExecutionException {
net.sf.jsqlparser.schema.Table fromTable = s.getTable();
String tableSpace = fromTable.getSchemaName();
String tableName = fromTable.getName();
if (tableSpace == null) {
tableSpace = defaultTableSpace;
}
TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSpace);
if (tableSpaceManager == null) {
throw new StatementExecutionException("no such tablespace " + tableSpace + " here at " + manager.getNodeId());
}
AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableName);
if (tableManager == null) {
throw new StatementExecutionException("no such table " + tableName + " in tablespace " + tableSpace);
}
Table table = tableManager.getTable();
// Perform a scan and then delete each row
SQLRecordPredicate where = s.getWhere() != null ? new SQLRecordPredicate(table, table.name, s.getWhere()) : null;
if (where != null) {
Expression expressionWhere = s.getWhere();
discoverIndexOperations(expressionWhere, table, table.name, where, tableSpaceManager);
}
DMLStatement st = new DeleteStatement(tableSpace, tableName, null, where).setReturnValues(returnValues);
return ExecutionPlan.simple(st);
}
use of herddb.model.StatementExecutionException in project herddb by diennea.
the class SQLProjection method addExpressionsForFunctionArguments.
private static void addExpressionsForFunctionArguments(ColumnReferencesDiscovery discovery, List<OutputColumn> output, Table table) throws StatementExecutionException {
List<net.sf.jsqlparser.schema.Column> columns = discovery.getColumnsByTable().get(table.name);
if (columns != null) {
for (Expression e : columns) {
net.sf.jsqlparser.schema.Column c = (net.sf.jsqlparser.schema.Column) e;
String columnName = c.getColumnName();
boolean found = false;
for (OutputColumn outputColumn : output) {
if (columnName.equalsIgnoreCase(outputColumn.column.name)) {
found = true;
break;
} else if (outputColumn.directColumnReference != null && outputColumn.directColumnReference.getColumnName().equalsIgnoreCase(columnName)) {
found = true;
break;
}
}
if (!found) {
Column column = table.getColumn(c.getColumnName());
if (column == null) {
throw new StatementExecutionException("invalid column name " + c.getColumnName());
}
output.add(new OutputColumn(null, Column.column(columnName, column.type), c, null));
}
}
}
}
use of herddb.model.StatementExecutionException in project herddb by diennea.
the class AlterTableSQLTest method modifyColumn.
@Test
public void modifyColumn() 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);
execute(manager, "CREATE TABLE tblspace1.tsql (k1 int primary key auto_increment,n1 int,s1 string)", Collections.emptyList());
assertTrue(manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable().auto_increment);
Table table = manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable();
assertEquals(0, table.getColumn("k1").serialPosition);
assertEquals(1, table.getColumn("n1").serialPosition);
assertEquals(2, table.getColumn("s1").serialPosition);
execute(manager, "INSERT INTO tblspace1.tsql (n1,s1) values(1,'b')", Collections.emptyList());
{
List<DataAccessor> tuples = scan(manager, "SELECT * FROM tblspace1.tsql where k1=1", Collections.emptyList()).consume();
assertEquals(1, tuples.size());
assertEquals(3, tuples.get(0).getFieldNames().length);
}
execute(manager, "ALTER TABLE tblspace1.tsql modify column k1 int", Collections.emptyList());
execute(manager, "INSERT INTO tblspace1.tsql (k1,n1,s1) values(2, 2,'b')", Collections.emptyList());
{
List<DataAccessor> tuples = scan(manager, "SELECT * FROM tblspace1.tsql", Collections.emptyList()).consume();
assertEquals(2, tuples.size());
assertEquals(3, tuples.get(0).getFieldNames().length);
}
{
List<DataAccessor> tuples = scan(manager, "SELECT * FROM tblspace1.tsql where k1=2", Collections.emptyList()).consume();
assertEquals(1, tuples.size());
assertEquals(3, tuples.get(0).getFieldNames().length);
}
assertFalse(manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable().auto_increment);
execute(manager, "ALTER TABLE tblspace1.tsql modify column k1 int auto_increment", Collections.emptyList());
assertTrue(manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable().auto_increment);
execute(manager, "INSERT INTO tblspace1.tsql (n1,s1) values(1,'b')", Collections.emptyList());
{
List<DataAccessor> tuples = scan(manager, "SELECT * FROM tblspace1.tsql", Collections.emptyList()).consume();
assertEquals(3, tuples.size());
assertEquals(3, tuples.get(0).getFieldNames().length);
}
try {
execute(manager, "ALTER TABLE tblspace1.tsql modify column k1 string", Collections.emptyList());
fail();
} catch (StatementExecutionException error) {
assertTrue(error.getMessage().contains("cannot change datatype"));
}
try {
execute(manager, "ALTER TABLE tblspace1.tsql modify column badcol string", Collections.emptyList());
fail();
} catch (StatementExecutionException error) {
assertTrue(error.getMessage().contains("bad column badcol in table tsql"));
}
execute(manager, "ALTER TABLE tblspace1.tsql MODIFY COLUMN k1 int RENAME TO l2", Collections.emptyList());
assertFalse(manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable().auto_increment);
{
List<DataAccessor> tuples = scan(manager, "SELECT * FROM tblspace1.tsql " + "where l2=1", Collections.emptyList()).consume();
assertEquals(1, tuples.size());
assertEquals(Arrays.toString(tuples.get(0).getFieldNames()), 3, tuples.get(0).getFieldNames().length);
assertEquals("l2", tuples.get(0).getFieldNames()[0]);
assertEquals("n1", tuples.get(0).getFieldNames()[1]);
assertEquals("s1", tuples.get(0).getFieldNames()[2]);
}
}
}
Aggregations