@Test public void createTableFKSimple() throws Exception { makeSeparateAIS(); String sql = "CREATE TABLE t2 (c1 int not null primary key, c2 int not null, grouping foreign key (c2) references t1)"; createTableFKSimpleGenerateAIS(); StatementNode stmt = parser.parseStatement(sql); assertTrue (stmt instanceof CreateTableNode); TableDDL.createTable(ddlFunctions, null, DEFAULT_SCHEMA, (CreateTableNode)stmt, null); }
public static void createTable(DDLFunctions ddlFunctions, Session session, String defaultSchemaName, CreateTableNode createTable, QueryContext context) { if (createTable.getQueryExpression() != null) throw new UnsupportedCreateSelectException(); TableName fullName = convertName(defaultSchemaName, createTable.getObjectName()); String schemaName = fullName.getSchemaName(); String tableName = fullName.getTableName(); AkibanInformationSchema ais = ddlFunctions.getAIS(session); Table curTable = ais.getTable(fullName); if((curTable != null) && skipOrThrow(context, createTable.getExistenceCheck(), curTable, new DuplicateTableNameException(fullName))) { return; } TypesTranslator typesTranslator = ddlFunctions.getTypesTranslator(); AISBuilder builder = new AISBuilder(); builder.getNameGenerator().mergeAIS(ais); builder.table(schemaName, tableName); Table table = builder.akibanInformationSchema().getTable(schemaName, tableName); IndexNameGenerator namer = DefaultIndexNameGenerator.forTable(table); cloneReferencedTables(defaultSchemaName, ddlFunctions.getAISCloner(), ais, builder.akibanInformationSchema(), createTable.getTableElementList()); int colpos = 0; for (TableElementNode tableElement : createTable.getTableElementList()) { if (tableElement instanceof ColumnDefinitionNode) { addColumn (builder, typesTranslator, (ColumnDefinitionNode)tableElement, schemaName, tableName, colpos++); } } for (TableElementNode tableElement : createTable.getTableElementList()) { if (tableElement instanceof FKConstraintDefinitionNode) { FKConstraintDefinitionNode fkdn = (FKConstraintDefinitionNode)tableElement; if (fkdn.isGrouping()) { addJoin (builder, fkdn, defaultSchemaName, schemaName, tableName); } } else if (tableElement instanceof ConstraintDefinitionNode) { addIndex (namer, builder, (ConstraintDefinitionNode)tableElement, schemaName, tableName, context); } else if (tableElement instanceof IndexDefinitionNode) { addIndex (namer, builder, (IndexDefinitionNode)tableElement, schemaName, tableName, context, ddlFunctions); } else if (!(tableElement instanceof ColumnDefinitionNode)) { throw new UnsupportedSQLException("Unexpected TableElement", tableElement); } } for (TableElementNode tableElement : createTable.getTableElementList()) { if (tableElement instanceof FKConstraintDefinitionNode) { FKConstraintDefinitionNode fkdn = (FKConstraintDefinitionNode)tableElement; if (!fkdn.isGrouping()) { addForeignKey(builder, ddlFunctions.getAIS(session), fkdn, defaultSchemaName, schemaName, tableName); } } } setTableStorage(ddlFunctions, createTable, builder, tableName, table, schemaName); builder.basicSchemaIsComplete(); builder.groupingIsComplete(); ddlFunctions.createTable(session, table); }
TableDDL { public static void createTable(DDLFunctions ddlFunctions, Session session, String defaultSchemaName, CreateTableNode createTable, QueryContext context) { if (createTable.getQueryExpression() != null) throw new UnsupportedCreateSelectException(); TableName fullName = convertName(defaultSchemaName, createTable.getObjectName()); String schemaName = fullName.getSchemaName(); String tableName = fullName.getTableName(); AkibanInformationSchema ais = ddlFunctions.getAIS(session); Table curTable = ais.getTable(fullName); if((curTable != null) && skipOrThrow(context, createTable.getExistenceCheck(), curTable, new DuplicateTableNameException(fullName))) { return; } TypesTranslator typesTranslator = ddlFunctions.getTypesTranslator(); AISBuilder builder = new AISBuilder(); builder.getNameGenerator().mergeAIS(ais); builder.table(schemaName, tableName); Table table = builder.akibanInformationSchema().getTable(schemaName, tableName); IndexNameGenerator namer = DefaultIndexNameGenerator.forTable(table); cloneReferencedTables(defaultSchemaName, ddlFunctions.getAISCloner(), ais, builder.akibanInformationSchema(), createTable.getTableElementList()); int colpos = 0; for (TableElementNode tableElement : createTable.getTableElementList()) { if (tableElement instanceof ColumnDefinitionNode) { addColumn (builder, typesTranslator, (ColumnDefinitionNode)tableElement, schemaName, tableName, colpos++); } } for (TableElementNode tableElement : createTable.getTableElementList()) { if (tableElement instanceof FKConstraintDefinitionNode) { FKConstraintDefinitionNode fkdn = (FKConstraintDefinitionNode)tableElement; if (fkdn.isGrouping()) { addJoin (builder, fkdn, defaultSchemaName, schemaName, tableName); } } else if (tableElement instanceof ConstraintDefinitionNode) { addIndex (namer, builder, (ConstraintDefinitionNode)tableElement, schemaName, tableName, context); } else if (tableElement instanceof IndexDefinitionNode) { addIndex (namer, builder, (IndexDefinitionNode)tableElement, schemaName, tableName, context, ddlFunctions); } else if (!(tableElement instanceof ColumnDefinitionNode)) { throw new UnsupportedSQLException("Unexpected TableElement", tableElement); } } for (TableElementNode tableElement : createTable.getTableElementList()) { if (tableElement instanceof FKConstraintDefinitionNode) { FKConstraintDefinitionNode fkdn = (FKConstraintDefinitionNode)tableElement; if (!fkdn.isGrouping()) { addForeignKey(builder, ddlFunctions.getAIS(session), fkdn, defaultSchemaName, schemaName, tableName); } } } setTableStorage(ddlFunctions, createTable, builder, tableName, table, schemaName); builder.basicSchemaIsComplete(); builder.groupingIsComplete(); ddlFunctions.createTable(session, table); } }
TableDDL { public static void createTable(DDLFunctions ddlFunctions, Session session, String defaultSchemaName, CreateTableNode createTable, QueryContext context) { if (createTable.getQueryExpression() != null) throw new UnsupportedCreateSelectException(); TableName fullName = convertName(defaultSchemaName, createTable.getObjectName()); String schemaName = fullName.getSchemaName(); String tableName = fullName.getTableName(); AkibanInformationSchema ais = ddlFunctions.getAIS(session); Table curTable = ais.getTable(fullName); if((curTable != null) && skipOrThrow(context, createTable.getExistenceCheck(), curTable, new DuplicateTableNameException(fullName))) { return; } TypesTranslator typesTranslator = ddlFunctions.getTypesTranslator(); AISBuilder builder = new AISBuilder(); builder.getNameGenerator().mergeAIS(ais); builder.table(schemaName, tableName); Table table = builder.akibanInformationSchema().getTable(schemaName, tableName); IndexNameGenerator namer = DefaultIndexNameGenerator.forTable(table); cloneReferencedTables(defaultSchemaName, ddlFunctions.getAISCloner(), ais, builder.akibanInformationSchema(), createTable.getTableElementList()); int colpos = 0; for (TableElementNode tableElement : createTable.getTableElementList()) { if (tableElement instanceof ColumnDefinitionNode) { addColumn (builder, typesTranslator, (ColumnDefinitionNode)tableElement, schemaName, tableName, colpos++); } } for (TableElementNode tableElement : createTable.getTableElementList()) { if (tableElement instanceof FKConstraintDefinitionNode) { FKConstraintDefinitionNode fkdn = (FKConstraintDefinitionNode)tableElement; if (fkdn.isGrouping()) { addJoin (builder, fkdn, defaultSchemaName, schemaName, tableName); } } else if (tableElement instanceof ConstraintDefinitionNode) { addIndex (namer, builder, (ConstraintDefinitionNode)tableElement, schemaName, tableName, context); } else if (tableElement instanceof IndexDefinitionNode) { addIndex (namer, builder, (IndexDefinitionNode)tableElement, schemaName, tableName, context, ddlFunctions); } else if (!(tableElement instanceof ColumnDefinitionNode)) { throw new UnsupportedSQLException("Unexpected TableElement", tableElement); } } for (TableElementNode tableElement : createTable.getTableElementList()) { if (tableElement instanceof FKConstraintDefinitionNode) { FKConstraintDefinitionNode fkdn = (FKConstraintDefinitionNode)tableElement; if (!fkdn.isGrouping()) { addForeignKey(builder, ddlFunctions.getAIS(session), fkdn, defaultSchemaName, schemaName, tableName); } } } setTableStorage(ddlFunctions, createTable, builder, tableName, table, schemaName); builder.basicSchemaIsComplete(); builder.groupingIsComplete(); ddlFunctions.createTable(session, table); } private  TableDDL(); }
TableDDL { public static void createTable(DDLFunctions ddlFunctions, Session session, String defaultSchemaName, CreateTableNode createTable, QueryContext context) { if (createTable.getQueryExpression() != null) throw new UnsupportedCreateSelectException(); TableName fullName = convertName(defaultSchemaName, createTable.getObjectName()); String schemaName = fullName.getSchemaName(); String tableName = fullName.getTableName(); AkibanInformationSchema ais = ddlFunctions.getAIS(session); Table curTable = ais.getTable(fullName); if((curTable != null) && skipOrThrow(context, createTable.getExistenceCheck(), curTable, new DuplicateTableNameException(fullName))) { return; } TypesTranslator typesTranslator = ddlFunctions.getTypesTranslator(); AISBuilder builder = new AISBuilder(); builder.getNameGenerator().mergeAIS(ais); builder.table(schemaName, tableName); Table table = builder.akibanInformationSchema().getTable(schemaName, tableName); IndexNameGenerator namer = DefaultIndexNameGenerator.forTable(table); cloneReferencedTables(defaultSchemaName, ddlFunctions.getAISCloner(), ais, builder.akibanInformationSchema(), createTable.getTableElementList()); int colpos = 0; for (TableElementNode tableElement : createTable.getTableElementList()) { if (tableElement instanceof ColumnDefinitionNode) { addColumn (builder, typesTranslator, (ColumnDefinitionNode)tableElement, schemaName, tableName, colpos++); } } for (TableElementNode tableElement : createTable.getTableElementList()) { if (tableElement instanceof FKConstraintDefinitionNode) { FKConstraintDefinitionNode fkdn = (FKConstraintDefinitionNode)tableElement; if (fkdn.isGrouping()) { addJoin (builder, fkdn, defaultSchemaName, schemaName, tableName); } } else if (tableElement instanceof ConstraintDefinitionNode) { addIndex (namer, builder, (ConstraintDefinitionNode)tableElement, schemaName, tableName, context); } else if (tableElement instanceof IndexDefinitionNode) { addIndex (namer, builder, (IndexDefinitionNode)tableElement, schemaName, tableName, context, ddlFunctions); } else if (!(tableElement instanceof ColumnDefinitionNode)) { throw new UnsupportedSQLException("Unexpected TableElement", tableElement); } } for (TableElementNode tableElement : createTable.getTableElementList()) { if (tableElement instanceof FKConstraintDefinitionNode) { FKConstraintDefinitionNode fkdn = (FKConstraintDefinitionNode)tableElement; if (!fkdn.isGrouping()) { addForeignKey(builder, ddlFunctions.getAIS(session), fkdn, defaultSchemaName, schemaName, tableName); } } } setTableStorage(ddlFunctions, createTable, builder, tableName, table, schemaName); builder.basicSchemaIsComplete(); builder.groupingIsComplete(); ddlFunctions.createTable(session, table); } private  TableDDL(); static void dropTable(DDLFunctions ddlFunctions,
                                  Session session, 
                                  String defaultSchemaName,
                                  DropTableNode dropTable,
                                  QueryContext context); static void dropGroup(DDLFunctions ddlFunctions,
                                    Session session,
                                    String defaultSchemaName,
                                    DropGroupNode dropGroup,
                                    QueryContext context); static void renameTable(DDLFunctions ddlFunctions,
                                    Session session,
                                    String defaultSchemaName,
                                    RenameNode renameTable); static void createTable(DDLFunctions ddlFunctions,
                                   Session session,
                                   String defaultSchemaName,
                                   CreateTableNode createTable,
                                   QueryContext context); static void createTable(DDLFunctions ddlFunctions,
                                   Session session,
                                   String defaultSchemaName,
                                   CreateTableNode createTable,
                                   QueryContext context,
                                   List<DataTypeDescriptor>  descriptors,
                                   List<String> columnNames,
                                   ServerSession server); static void setStorage(DDLFunctions ddlFunctions,
                                  HasStorage object, 
                                  StorageFormatNode storage); static void setAutoIncrement(AISBuilder builder, String schema, String table, ColumnDefinitionNode cdn); static void setAutoIncrement(AISBuilder builder, String schemaName, String tableName, String columnName,
                                        boolean defaultIdentity, long start, long increment); static String addIndex(IndexNameGenerator namer, AISBuilder builder, ConstraintDefinitionNode cdn,
                                  String schemaName, String tableName, QueryContext context); static String addIndex(IndexNameGenerator namer,
                                  AISBuilder builder,
                                  IndexDefinitionNode idn,
                                  String schemaName,
                                  String tableName,
                                  QueryContext context,
                                  DDLFunctions ddl); static TableName getReferencedName(String schemaName, FKConstraintDefinitionNode fkdn); static void addJoin(final AISBuilder builder, final FKConstraintDefinitionNode fkdn,
                               final String defaultSchemaName, final String schemaName, final String tableName); }
TableDDL { public static void createTable(DDLFunctions ddlFunctions, Session session, String defaultSchemaName, CreateTableNode createTable, QueryContext context) { if (createTable.getQueryExpression() != null) throw new UnsupportedCreateSelectException(); TableName fullName = convertName(defaultSchemaName, createTable.getObjectName()); String schemaName = fullName.getSchemaName(); String tableName = fullName.getTableName(); AkibanInformationSchema ais = ddlFunctions.getAIS(session); Table curTable = ais.getTable(fullName); if((curTable != null) && skipOrThrow(context, createTable.getExistenceCheck(), curTable, new DuplicateTableNameException(fullName))) { return; } TypesTranslator typesTranslator = ddlFunctions.getTypesTranslator(); AISBuilder builder = new AISBuilder(); builder.getNameGenerator().mergeAIS(ais); builder.table(schemaName, tableName); Table table = builder.akibanInformationSchema().getTable(schemaName, tableName); IndexNameGenerator namer = DefaultIndexNameGenerator.forTable(table); cloneReferencedTables(defaultSchemaName, ddlFunctions.getAISCloner(), ais, builder.akibanInformationSchema(), createTable.getTableElementList()); int colpos = 0; for (TableElementNode tableElement : createTable.getTableElementList()) { if (tableElement instanceof ColumnDefinitionNode) { addColumn (builder, typesTranslator, (ColumnDefinitionNode)tableElement, schemaName, tableName, colpos++); } } for (TableElementNode tableElement : createTable.getTableElementList()) { if (tableElement instanceof FKConstraintDefinitionNode) { FKConstraintDefinitionNode fkdn = (FKConstraintDefinitionNode)tableElement; if (fkdn.isGrouping()) { addJoin (builder, fkdn, defaultSchemaName, schemaName, tableName); } } else if (tableElement instanceof ConstraintDefinitionNode) { addIndex (namer, builder, (ConstraintDefinitionNode)tableElement, schemaName, tableName, context); } else if (tableElement instanceof IndexDefinitionNode) { addIndex (namer, builder, (IndexDefinitionNode)tableElement, schemaName, tableName, context, ddlFunctions); } else if (!(tableElement instanceof ColumnDefinitionNode)) { throw new UnsupportedSQLException("Unexpected TableElement", tableElement); } } for (TableElementNode tableElement : createTable.getTableElementList()) { if (tableElement instanceof FKConstraintDefinitionNode) { FKConstraintDefinitionNode fkdn = (FKConstraintDefinitionNode)tableElement; if (!fkdn.isGrouping()) { addForeignKey(builder, ddlFunctions.getAIS(session), fkdn, defaultSchemaName, schemaName, tableName); } } } setTableStorage(ddlFunctions, createTable, builder, tableName, table, schemaName); builder.basicSchemaIsComplete(); builder.groupingIsComplete(); ddlFunctions.createTable(session, table); } private  TableDDL(); static void dropTable(DDLFunctions ddlFunctions,
                                  Session session, 
                                  String defaultSchemaName,
                                  DropTableNode dropTable,
                                  QueryContext context); static void dropGroup(DDLFunctions ddlFunctions,
                                    Session session,
                                    String defaultSchemaName,
                                    DropGroupNode dropGroup,
                                    QueryContext context); static void renameTable(DDLFunctions ddlFunctions,
                                    Session session,
                                    String defaultSchemaName,
                                    RenameNode renameTable); static void createTable(DDLFunctions ddlFunctions,
                                   Session session,
                                   String defaultSchemaName,
                                   CreateTableNode createTable,
                                   QueryContext context); static void createTable(DDLFunctions ddlFunctions,
                                   Session session,
                                   String defaultSchemaName,
                                   CreateTableNode createTable,
                                   QueryContext context,
                                   List<DataTypeDescriptor>  descriptors,
                                   List<String> columnNames,
                                   ServerSession server); static void setStorage(DDLFunctions ddlFunctions,
                                  HasStorage object, 
                                  StorageFormatNode storage); static void setAutoIncrement(AISBuilder builder, String schema, String table, ColumnDefinitionNode cdn); static void setAutoIncrement(AISBuilder builder, String schemaName, String tableName, String columnName,
                                        boolean defaultIdentity, long start, long increment); static String addIndex(IndexNameGenerator namer, AISBuilder builder, ConstraintDefinitionNode cdn,
                                  String schemaName, String tableName, QueryContext context); static String addIndex(IndexNameGenerator namer,
                                  AISBuilder builder,
                                  IndexDefinitionNode idn,
                                  String schemaName,
                                  String tableName,
                                  QueryContext context,
                                  DDLFunctions ddl); static TableName getReferencedName(String schemaName, FKConstraintDefinitionNode fkdn); static void addJoin(final AISBuilder builder, final FKConstraintDefinitionNode fkdn,
                               final String defaultSchemaName, final String schemaName, final String tableName);  }