예제 #1
0
  @Test
  public void testOptionalFKFail() throws Exception {
    String ddl =
        "CREATE FOREIGN TABLE G1(g1e1 integer, g1e2 varchar);\n"
            + "CREATE FOREIGN TABLE G2( g2e1 integer, g2e2 varchar, PRIMARY KEY(g2e1, g2e2),"
            + "FOREIGN KEY (g2e1, g2e2) REFERENCES G1)";

    MetadataFactory s = helpParse(ddl, "model");
    Map<String, Table> tableMap = s.getSchema().getTables();
    assertEquals(2, tableMap.size());

    assertTrue("Table not found", tableMap.containsKey("G1"));
    assertTrue("Table not found", tableMap.containsKey("G2"));

    Table table = tableMap.get("G2");
    ForeignKey fk = table.getForeignKeys().get(0);
    assertEquals(fk.getColumns(), table.getColumns());
    assertEquals("G1", fk.getReferenceTableName());

    VDBMetaData vdb = new VDBMetaData();
    vdb.setName("myVDB"); // $NON-NLS-1$
    ModelMetaData modelOne = new ModelMetaData();
    modelOne.setName("model"); // $NON-NLS-1$
    vdb.addModel(modelOne);

    ValidatorReport report = new MetadataValidator().validate(vdb, s.asMetadataStore());

    assertTrue(report.hasItems());
  }
예제 #2
0
  @Test
  public void testFKAccrossSchemas() throws Exception {
    String ddl = "CREATE FOREIGN TABLE G1(g1e1 integer, g1e2 varchar, PRIMARY KEY(g1e1, g1e2));\n";

    String ddl2 =
        "CREATE FOREIGN TABLE G2( g2e1 integer, g2e2 varchar, PRIMARY KEY(g2e1, g2e2),"
            + "FOREIGN KEY (g2e1, g2e2) REFERENCES model.G1)";

    MetadataFactory f1 = helpParse(ddl, "model");
    MetadataFactory f2 = helpParse(ddl2, "model2");

    VDBMetaData vdb = new VDBMetaData();
    vdb.setName("myVDB"); // $NON-NLS-1$
    ModelMetaData modelOne = new ModelMetaData();
    modelOne.setName("model"); // $NON-NLS-1$
    vdb.addModel(modelOne);

    ModelMetaData modelTwo = new ModelMetaData();
    modelTwo.setName("model2"); // $NON-NLS-1$
    vdb.addModel(modelTwo);

    MetadataStore s = f1.asMetadataStore();
    f2.mergeInto(s);

    ValidatorReport report = new MetadataValidator().validate(vdb, s);

    assertFalse(report.hasItems());

    Table table = s.getSchema("model2").getTable("G2");
    ForeignKey fk = table.getForeignKeys().get(0);
    assertEquals(fk.getColumns(), table.getColumns());
    assertEquals("G1", fk.getReferenceTableName());

    assertEquals(fk.getPrimaryKey().getColumns(), s.getSchema("model").getTable("G1").getColumns());
  }
예제 #3
0
  @Test
  public void testFK() throws Exception {
    String ddl =
        "CREATE FOREIGN TABLE G1(g1e1 integer, g1e2 varchar, PRIMARY KEY(g1e1, g1e2));\n"
            + "CREATE FOREIGN TABLE G2( g2e1 integer, g2e2 varchar, "
            + "FOREIGN KEY (g2e1, g2e2) REFERENCES G1 (g1e1, g1e2) options (\"teiid_rel:allow-join\" true))";

    Schema s = helpParse(ddl, "model").getSchema();
    Map<String, Table> tableMap = s.getTables();
    assertEquals(2, tableMap.size());

    assertTrue("Table not found", tableMap.containsKey("G1"));
    assertTrue("Table not found", tableMap.containsKey("G2"));

    Table table = tableMap.get("G2");
    ForeignKey fk = table.getForeignKeys().get(0);
    assertEquals(Boolean.TRUE.toString(), fk.getProperty(ForeignKey.ALLOW_JOIN, false));
    assertEquals(fk.getColumns(), table.getColumns());
    assertEquals("G1", fk.getReferenceTableName());
  }
  private static void buildAssosiationSets(MetadataStore metadataStore, List<Builder> edmSchemas) {
    for (Schema schema : metadataStore.getSchemaList()) {

      EdmSchema.Builder odataSchema = findSchema(edmSchemas, schema.getName());
      EdmEntityContainer.Builder entityContainer =
          findEntityContainer(edmSchemas, schema.getName());
      List<EdmAssociationSet.Builder> assosiationSets = new ArrayList<EdmAssociationSet.Builder>();
      List<EdmAssociation.Builder> assosiations = new ArrayList<EdmAssociation.Builder>();

      for (Table table : schema.getTables().values()) {
        // build Associations
        for (ForeignKey fk : table.getForeignKeys()) {

          EdmEntitySet.Builder entitySet =
              findEntitySet(edmSchemas, schema.getName(), table.getName());
          EdmEntitySet.Builder refEntitySet =
              findEntitySet(edmSchemas, schema.getName(), fk.getReferenceTableName());
          EdmEntityType.Builder entityType =
              findEntityType(edmSchemas, schema.getName(), table.getName());
          EdmEntityType.Builder refEntityType =
              findEntityType(edmSchemas, schema.getName(), fk.getReferenceTableName());

          // check to see if fk is part of this table's pk, then it is 1 to 1 relation
          boolean onetoone = sameColumnSet(table.getPrimaryKey(), fk);

          // Build Association Ends
          EdmAssociationEnd.Builder endSelf =
              EdmAssociationEnd.newBuilder()
                  .setRole(table.getName())
                  .setType(entityType)
                  .setMultiplicity(onetoone ? EdmMultiplicity.ZERO_TO_ONE : EdmMultiplicity.MANY);

          EdmAssociationEnd.Builder endRef =
              EdmAssociationEnd.newBuilder()
                  .setRole(fk.getReferenceTableName())
                  .setType(refEntityType)
                  .setMultiplicity(EdmMultiplicity.ZERO_TO_ONE);

          // Build Association
          EdmAssociation.Builder association = EdmAssociation.newBuilder();
          association.setName(table.getName() + "_" + fk.getName());
          association.setEnds(endSelf, endRef);
          association.setNamespace(
              refEntityType
                  .getFullyQualifiedTypeName()
                  .substring(0, refEntityType.getFullyQualifiedTypeName().indexOf('.')));
          assosiations.add(association);

          // Build ReferentialConstraint
          if (fk.getReferenceColumns() != null) {
            EdmReferentialConstraint.Builder erc = EdmReferentialConstraint.newBuilder();
            erc.setPrincipalRole(fk.getReferenceTableName());
            erc.addPrincipalReferences(fk.getReferenceColumns());
            erc.setDependentRole(table.getName());
            erc.addDependentReferences(getColumnNames(fk.getColumns()));
            association.setRefConstraint(erc);
          }

          // Add EdmNavigationProperty to entity type
          EdmNavigationProperty.Builder nav =
              EdmNavigationProperty.newBuilder(fk.getReferenceTableName());
          nav.setRelationshipName(fk.getName());
          nav.setFromToName(table.getName(), fk.getReferenceTableName());
          nav.setRelationship(association);
          nav.setFromTo(endSelf, endRef);
          entityType.addNavigationProperties(nav);

          // Add EdmNavigationProperty to Reference entity type
          EdmNavigationProperty.Builder refNav = EdmNavigationProperty.newBuilder(table.getName());
          refNav.setRelationshipName(fk.getName());
          refNav.setFromToName(fk.getReferenceTableName(), table.getName());
          refNav.setRelationship(association);
          refNav.setFromTo(endRef, endSelf);
          refEntityType.addNavigationProperties(refNav);

          // build AssosiationSet
          EdmAssociationSet.Builder assosiationSet =
              EdmAssociationSet.newBuilder()
                  .setName(table.getName() + "_" + fk.getName())
                  .setAssociationName(fk.getName());

          // Build AssosiationSet Ends
          EdmAssociationSetEnd.Builder endOne =
              EdmAssociationSetEnd.newBuilder()
                  .setEntitySet(entitySet)
                  .setRoleName(table.getName())
                  .setRole(
                      EdmAssociationEnd.newBuilder()
                          .setType(entityType)
                          .setRole(entityType.getName()));

          EdmAssociationSetEnd.Builder endTwo =
              EdmAssociationSetEnd.newBuilder()
                  .setEntitySet(refEntitySet)
                  .setRoleName(fk.getReferenceTableName())
                  .setRole(
                      EdmAssociationEnd.newBuilder()
                          .setType(refEntityType)
                          .setRole(refEntityType.getName()));
          assosiationSet.setEnds(endOne, endTwo);

          assosiationSet.setAssociation(association);
          assosiationSets.add(assosiationSet);
        }
      }
      entityContainer.addAssociationSets(assosiationSets);
      odataSchema.addAssociations(assosiations);
    }
  }