use of org.apache.ignite.internal.sql.engine.prepare.ddl.CreateTableCommand in project ignite-3 by apache.
the class DdlCommandHandler method handleCreateTable.
/**
* Handles create table command.
*/
private void handleCreateTable(CreateTableCommand cmd) {
final PrimaryKeyDefinitionBuilder pkeyDef = SchemaBuilders.primaryKey();
pkeyDef.withColumns(IgniteObjectName.quoteNames(cmd.primaryKeyColumns()));
pkeyDef.withColocationColumns(IgniteObjectName.quoteNames(cmd.affColumns()));
final IgniteTypeFactory typeFactory = Commons.typeFactory();
final List<org.apache.ignite.schema.definition.ColumnDefinition> colsInner = new ArrayList<>();
for (ColumnDefinition col : cmd.columns()) {
ColumnDefinitionBuilder col0 = SchemaBuilders.column(IgniteObjectName.quote(col.name()), typeFactory.columnType(col.type())).asNullable(col.nullable()).withDefaultValueExpression(col.defaultValue());
colsInner.add(col0.build());
}
Consumer<TableChange> tblChanger = tblCh -> {
TableChange conv = convert(SchemaBuilders.tableBuilder(IgniteObjectName.quote(cmd.schemaName()), IgniteObjectName.quote(cmd.tableName())).columns(colsInner).withPrimaryKey(pkeyDef.build()).build(), tblCh);
if (cmd.partitions() != null) {
conv.changePartitions(cmd.partitions());
}
if (cmd.replicas() != null) {
conv.changeReplicas(cmd.replicas());
}
};
String fullName = TableDefinitionImpl.canonicalName(IgniteObjectName.quote(cmd.schemaName()), IgniteObjectName.quote(cmd.tableName()));
try {
tableManager.createTable(fullName, tblChanger);
} catch (TableAlreadyExistsException ex) {
if (!cmd.ifTableExists()) {
throw ex;
}
}
}
Aggregations