use of herddb.model.StatementExecutionException in project herddb by diennea.
the class JSQLParserPlanner method buildSelectStatement.
private ExecutionPlan buildSelectStatement(String defaultTableSpace, int maxRows, Select select, boolean forceScan) throws StatementExecutionException {
checkSupported(select.getWithItemsList() == null);
SelectBody selectBody = select.getSelectBody();
PlannerOp op = buildSelectBody(defaultTableSpace, maxRows, selectBody, forceScan).optimize();
// Simplify Scan to Get
if (!forceScan && op instanceof BindableTableScanOp) {
ScanStatement scanStatement = op.unwrap(ScanStatement.class);
if (scanStatement != null && scanStatement.getPredicate() != null) {
Table tableDef = scanStatement.getTableDef();
CompiledSQLExpression where = scanStatement.getPredicate().unwrap(CompiledSQLExpression.class);
SQLRecordKeyFunction keyFunction = IndexUtils.findIndexAccess(where, tableDef.getPrimaryKey(), tableDef, "=", tableDef);
if (keyFunction == null || !keyFunction.isFullPrimaryKey()) {
throw new StatementExecutionException("unsupported GET not on PK (" + keyFunction + ")");
}
GetStatement get = new GetStatement(scanStatement.getTableSpace(), scanStatement.getTable(), keyFunction, scanStatement.getPredicate(), true);
return ExecutionPlan.simple(get);
}
}
return ExecutionPlan.simple(new SQLPlannedOperationStatement(op), op);
}
use of herddb.model.StatementExecutionException in project herddb by diennea.
the class JSQLParserPlanner method queryConsistencyCheckStatement.
public Statement queryConsistencyCheckStatement(String defaultTablespace, String query, List<Object> parameters) {
if (query.startsWith(TABLE_CONSISTENCY_COMMAND)) {
query = query.substring(query.substring(0, 21).length());
String tableSpace = defaultTablespace;
String tableName;
if (query.contains(".")) {
String[] tokens = query.split("\\.");
tableSpace = tokens[0].trim().replaceAll("\'", "");
tableName = tokens[1].trim().replaceAll("\'", "");
} else {
tableName = query.trim();
}
TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSpace);
if (tableSpaceManager == null) {
throw new TableSpaceDoesNotExistException(String.format("Tablespace %s does not exist.", tableSpace));
}
AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableName);
if (tableManager == null || tableManager.getCreatedInTransaction() > 0) {
throw new TableDoesNotExistException(String.format("Table %s does not exist.", tableName));
}
return new TableConsistencyCheckStatement(tableName, tableSpace);
} else {
throw new StatementExecutionException(String.format("Incorrect Syntax for tableconsistencycheck"));
}
}
use of herddb.model.StatementExecutionException in project herddb by diennea.
the class JSQLParserPlanner method parseStatement.
private net.sf.jsqlparser.statement.Statement parseStatement(String query) throws StatementExecutionException {
net.sf.jsqlparser.statement.Statement stmt;
CCJSqlParser parser = new CCJSqlParser(new StringProvider(query));
// parser.setErrorRecovery(true);
try {
return parser.Statement();
} catch (ParseException err) {
throw new StatementExecutionException("unable to parse query " + query, err);
}
}
use of herddb.model.StatementExecutionException in project herddb by diennea.
the class JSQLParserPlanner method parseForeignKeyIndex.
private ForeignKeyDef parseForeignKeyIndex(ForeignKeyIndex fk, Table table, String tableName, String tableSpace) throws StatementExecutionException {
String indexName = fixMySqlBackTicks(fk.getName().toLowerCase());
if (indexName.equals("generate_unnamed")) {
indexName = "fk_" + tableName + "_" + System.nanoTime();
}
int onUpdateCascadeAction = parseForeignKeyAction(fk.getOnUpdateReferenceOption());
int onDeleteCascadeAction = parseForeignKeyAction(fk.getOnDeleteReferenceOption());
Table parentTableSchema = getTable(table.tablespace, fk.getTable());
herddb.model.ForeignKeyDef.Builder builder = herddb.model.ForeignKeyDef.builder().name(indexName).parentTableId(parentTableSchema.uuid).onUpdateAction(onUpdateCascadeAction).onDeleteAction(onDeleteCascadeAction);
for (String columnName : fk.getColumnsNames()) {
columnName = fixMySqlBackTicks(columnName.toLowerCase());
Column column = table.getColumn(columnName);
if (column == null) {
throw new StatementExecutionException("no such column " + columnName + " on table " + tableName + " in tablespace " + tableSpace);
}
builder.column(column.name);
}
for (String columnName : fk.getReferencedColumnNames()) {
columnName = fixMySqlBackTicks(columnName.toLowerCase());
Column column = parentTableSchema.getColumn(columnName);
if (column == null) {
throw new StatementExecutionException("no such column " + columnName + " on table " + parentTableSchema.name + " in tablespace " + parentTableSchema.tablespace);
}
builder.parentTableColumn(column.name);
}
ForeignKeyDef fkDef = builder.build();
return fkDef;
}
use of herddb.model.StatementExecutionException in project herddb by diennea.
the class JSQLParserPlanner method buildCreateTableStatement.
private Statement buildCreateTableStatement(String defaultTableSpace, CreateTable s) throws StatementExecutionException {
String tableSpace = fixMySqlBackTicks(s.getTable().getSchemaName());
String tableName = fixMySqlBackTicks(s.getTable().getName());
if (tableSpace == null) {
tableSpace = defaultTableSpace;
}
if (s.getColumnDefinitions() == null) {
throw new StatementExecutionException("A table must have at least 1 column");
}
final boolean isNotExsists = s.isIfNotExists();
try {
boolean foundPk = false;
Table.Builder tablebuilder = Table.builder().uuid(UUID.randomUUID().toString()).name(tableName).tablespace(tableSpace);
Set<String> primaryKey = new HashSet<>();
Set<String> simpleUniqueFields = new HashSet<>();
if (s.getIndexes() != null) {
for (Index index : s.getIndexes()) {
if (index.getType().equalsIgnoreCase("PRIMARY KEY")) {
for (String n : index.getColumnsNames()) {
n = fixMySqlBackTicks(n.toLowerCase());
tablebuilder.primaryKey(n);
primaryKey.add(n);
foundPk = true;
}
}
}
}
int position = 0;
for (ColumnDefinition cf : s.getColumnDefinitions()) {
String columnName = fixMySqlBackTicks(cf.getColumnName().toLowerCase());
int type;
String dataType = cf.getColDataType().getDataType();
List<String> columnSpecs = decodeColumnSpecs(cf.getColumnSpecs());
type = sqlDataTypeToColumnType(dataType, cf.getColDataType().getArgumentsStringList(), columnSpecs);
Bytes defaultValue = decodeDefaultValue(cf, type);
if (!columnSpecs.isEmpty()) {
boolean auto_increment = decodeAutoIncrement(columnSpecs);
if (columnSpecs.contains("PRIMARY")) {
foundPk = true;
tablebuilder.primaryKey(columnName, auto_increment);
}
if (auto_increment && primaryKey.contains(columnName)) {
tablebuilder.primaryKey(columnName, auto_increment);
}
boolean isUnique = columnSpecs.contains("UNIQUE");
if (isUnique) {
simpleUniqueFields.add(columnName);
}
}
tablebuilder.column(columnName, type, position++, defaultValue);
}
if (!foundPk) {
tablebuilder.column("_pk", ColumnTypes.LONG, position++, null);
tablebuilder.primaryKey("_pk", true);
}
Table table = tablebuilder.build();
List<herddb.model.Index> otherIndexes = new ArrayList<>();
List<herddb.model.ForeignKeyDef> foreignKeys = new ArrayList<>();
if (s.getIndexes() != null) {
for (Index index : s.getIndexes()) {
if (index.getType().equalsIgnoreCase("PRIMARY KEY")) {
} else if (index.getType().equalsIgnoreCase("INDEX") || index.getType().equalsIgnoreCase("KEY") || index.getType().equalsIgnoreCase("UNIQUE KEY")) {
String indexName = fixMySqlBackTicks(index.getName().toLowerCase());
String indexType = convertIndexType(null);
boolean unique = index.getType().equalsIgnoreCase("UNIQUE KEY");
herddb.model.Index.Builder builder = herddb.model.Index.builder().onTable(table).name(indexName).unique(unique).type(indexType).uuid(UUID.randomUUID().toString());
for (String columnName : index.getColumnsNames()) {
columnName = fixMySqlBackTicks(columnName.toLowerCase());
Column column = table.getColumn(columnName);
if (column == null) {
throw new StatementExecutionException("no such column " + columnName + " on table " + tableName + " in tablespace " + tableSpace);
}
builder.column(column.name, column.type);
}
otherIndexes.add(builder.build());
} else if (index.getType().equals("FOREIGN KEY")) {
ForeignKeyIndex fk = (ForeignKeyIndex) index;
ForeignKeyDef fkDef = parseForeignKeyIndex(fk, table, tableName, tableSpace);
foreignKeys.add(fkDef);
} else {
throw new StatementExecutionException("Unsupported index type " + index.getType());
}
}
}
for (String col : simpleUniqueFields) {
herddb.model.Index.Builder builder = herddb.model.Index.builder().onTable(table).name(table.name + "_unique_" + col).unique(true).type(herddb.model.Index.TYPE_BRIN).uuid(UUID.randomUUID().toString()).column(col, table.getColumn(col).type);
otherIndexes.add(builder.build());
}
if (!foreignKeys.isEmpty()) {
table = table.withForeignKeys(foreignKeys.toArray(new ForeignKeyDef[0]));
}
CreateTableStatement statement = new CreateTableStatement(table, otherIndexes, isNotExsists);
return statement;
} catch (IllegalArgumentException err) {
throw new StatementExecutionException("bad table definition: " + err.getMessage(), err);
}
}
Aggregations