Skip to content

Commit

Permalink
TEIID-3548 omitting the consideration of complex type handling
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Jun 18, 2015
1 parent 65440df commit 1fdf0fd
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 54 deletions.
Expand Up @@ -46,11 +46,7 @@ static EdmDataServices buildMetadata(MetadataStore metadataStore) {
List<EdmSchema.Builder> edmSchemas = new ArrayList<EdmSchema.Builder>();
for (Schema schema:metadataStore.getSchemaList()) {
buildEntityTypes(schema, edmSchemas, true);
}
for (Schema schema:metadataStore.getSchemaList()) {
buildFunctionImports(schema, edmSchemas, true);
}
for (Schema schema:metadataStore.getSchemaList()) {
buildAssosiationSets(schema, edmSchemas, true);
}
return EdmDataServices.newBuilder().addSchemas(edmSchemas).build();
Expand Down Expand Up @@ -124,40 +120,42 @@ public static void buildEntityTypes(Schema schema, List<EdmSchema.Builder> edmSc
List<EdmEntityType.Builder> entityTypes = new ArrayList<EdmEntityType.Builder>();
LinkedHashMap<String, EdmComplexType.Builder> complexTypes = new LinkedHashMap<String, EdmComplexType.Builder>();

//first pass, build complex types
for (Table table: schema.getTables().values()) {
// skip if the table does not have the PK or unique
KeyRecord primaryKey = table.getPrimaryKey();
List<KeyRecord> uniques = table.getUniqueKeys();
if (primaryKey == null && uniques.isEmpty()) {
LogManager.logDetail(LogConstants.CTX_ODATA, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID17017, table.getFullName()));
continue;
}

for (Column c : table.getColumns()) {
String name = c.getSourceName();
String complexType = c.getProperty(ODataMetadataProcessor.COMPLEX_TYPE, false);
if (complexType == null) {
if (preserveEntityTypeName) {
//first pass, build complex types
for (Table table: schema.getTables().values()) {
// skip if the table does not have the PK or unique
KeyRecord primaryKey = table.getPrimaryKey();
List<KeyRecord> uniques = table.getUniqueKeys();
if (primaryKey == null && uniques.isEmpty()) {
LogManager.logDetail(LogConstants.CTX_ODATA, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID17017, table.getFullName()));
continue;
}
EdmComplexType.Builder complexTypeBuilder = complexTypes.get(complexType);
if (complexTypeBuilder == null) {
complexTypeBuilder = EdmComplexType.newBuilder();
complexTypes.put(complexType, complexTypeBuilder);
complexTypeBuilder.setName(complexType);
complexTypeBuilder.setNamespace(schema.getName());
} else if (complexTypeBuilder.findProperty(name) != null) {
continue; //already added
}
EdmProperty.Builder property = EdmProperty.newBuilder(c.getSourceName())
.setType(ODataTypeManager.odataType(c.getRuntimeType()))
.setNullable(isPartOfPrimaryKey(table, c.getName())?false:c.getNullType() == NullType.Nullable);
if (c.getRuntimeType().equals(DataTypeManager.DefaultDataTypes.STRING)) {
property.setFixedLength(c.isFixedLength())
.setMaxLength(c.getLength())
.setUnicode(true);

for (Column c : table.getColumns()) {
String name = c.getSourceName();
String complexType = c.getProperty(ODataMetadataProcessor.COMPLEX_TYPE, false);
if (complexType == null) {
continue;
}
EdmComplexType.Builder complexTypeBuilder = complexTypes.get(complexType);
if (complexTypeBuilder == null) {
complexTypeBuilder = EdmComplexType.newBuilder();
complexTypes.put(complexType, complexTypeBuilder);
complexTypeBuilder.setName(complexType);
complexTypeBuilder.setNamespace(schema.getName());
} else if (complexTypeBuilder.findProperty(name) != null) {
continue; //already added
}
EdmProperty.Builder property = EdmProperty.newBuilder(c.getSourceName())
.setType(ODataTypeManager.odataType(c.getRuntimeType()))
.setNullable(isPartOfPrimaryKey(table, c.getName())?false:c.getNullType() == NullType.Nullable);
if (c.getRuntimeType().equals(DataTypeManager.DefaultDataTypes.STRING)) {
property.setFixedLength(c.isFixedLength())
.setMaxLength(c.getLength())
.setUnicode(true);
}
complexTypeBuilder.addProperties(property);
}
complexTypeBuilder.addProperties(property);
}
}

Expand Down Expand Up @@ -196,7 +194,7 @@ public static void buildEntityTypes(Schema schema, List<EdmSchema.Builder> edmSc
// adding properties
for (Column c : table.getColumns()) {
String complexType = c.getProperty(ODataMetadataProcessor.COMPLEX_TYPE, false);
if (complexType != null) {
if (complexType != null && preserveEntityTypeName) {
String columnGroup = c.getProperty(ODataMetadataProcessor.COLUMN_GROUP, false);
if (!columnGroups.add(columnGroup)) {
continue;
Expand Down
65 changes: 47 additions & 18 deletions odata/src/test/java/org/teiid/odata/TestODataIntegration.java
Expand Up @@ -82,6 +82,7 @@
import org.teiid.json.simple.ContentHandler;
import org.teiid.json.simple.JSONParser;
import org.teiid.json.simple.ParseException;
import org.teiid.json.simple.SimpleContentHandler;
import org.teiid.language.QueryExpression;
import org.teiid.metadata.KeyRecord;
import org.teiid.metadata.MetadataStore;
Expand Down Expand Up @@ -436,9 +437,8 @@ public void testGetEntity() throws Exception {
try {
ModelMetaData mmd = new ModelMetaData();
mmd.setName("vw");
mmd.setSchemaSourceType("ddl");
mmd.addSourceMetadata("ddl", "create view x (a string primary key, b char, c string[], d integer) as select 'ab\u0000cd\u0001', char(22), ('a\u00021','b1'), 1;");
mmd.setModelType(Type.VIRTUAL);
mmd.setSchemaText("create view x (a string primary key, b char, c string[], d integer) as select 'ab\u0000cd\u0001', char(22), ('a\u00021','b1'), 1;");
es.deployVDB("northwind", mmd);

TeiidDriver td = es.getDriver();
Expand All @@ -464,9 +464,8 @@ public void testGetEntity() throws Exception {
try {
ModelMetaData mmd = new ModelMetaData();
mmd.setName("vw");
mmd.setSchemaSourceType("ddl");
mmd.addSourceMetadata("ddl", "create view x (a string primary key, b integer[], c string[][]) as select 'x', (1, 2, 3), (('a','b'),('c','d'));");
mmd.setModelType(Type.VIRTUAL);
mmd.setSchemaText("create view x (a string primary key, b integer[], c string[][]) as select 'x', (1, 2, 3), (('a','b'),('c','d'));");
es.deployVDB("northwind", mmd);

TeiidDriver td = es.getDriver();
Expand Down Expand Up @@ -494,9 +493,8 @@ public void testGetEntity() throws Exception {
try {
ModelMetaData mmd = new ModelMetaData();
mmd.setName("vw");
mmd.setSchemaSourceType("ddl");
mmd.addSourceMetadata("ddl", "create view x (a string primary key, b integer) as select 'xyz', 123 union all select 'abc', 456;");
mmd.setModelType(Type.VIRTUAL);
mmd.setSchemaText("create view x (a string primary key, b integer) as select 'xyz', 123 union all select 'abc', 456;");
es.deployVDB("northwind", mmd);

TeiidDriver td = es.getDriver();
Expand Down Expand Up @@ -560,9 +558,8 @@ public String getQueryParameter(String queryPath, String param) {
try {
ModelMetaData mmd = new ModelMetaData();
mmd.setName("vw");
mmd.setSchemaSourceType("ddl");
mmd.addSourceMetadata("ddl", "create view x (a string primary key, b integer) as select 'xyz', 123 union all select 'abc', 456;");
mmd.setModelType(Type.VIRTUAL);
mmd.setSchemaText("create view x (a string primary key, b integer) as select 'xyz', 123 union all select 'abc', 456;");
es.deployVDB("northwind", mmd);

TeiidDriver td = es.getDriver();
Expand Down Expand Up @@ -594,9 +591,8 @@ public String getQueryParameter(String queryPath, String param) {
try {
ModelMetaData mmd = new ModelMetaData();
mmd.setName("vw");
mmd.setSchemaSourceType("ddl");
mmd.addSourceMetadata("ddl", "create view x (a string primary key, b integer) as select 'xyz', 123 union all select 'abc', 456;");
mmd.setModelType(Type.VIRTUAL);
mmd.setSchemaText("create view x (a string primary key, b integer) as select 'xyz', 123 union all select 'abc', 456;");
es.deployVDB("northwind", mmd);

TeiidDriver td = es.getDriver();
Expand Down Expand Up @@ -660,8 +656,7 @@ public boolean supportsCompareCriteriaEquals() {
try {
ModelMetaData mmd = new ModelMetaData();
mmd.setName("m");
mmd.setSchemaSourceType("ddl");
mmd.setSchemaText("create foreign table x (a string, b string, c integer, primary key (a, b)) options (updatable true);");
mmd.addSourceMetadata("ddl", "create foreign table x (a string, b string, c integer, primary key (a, b)) options (updatable true);");
mmd.addSourceMapping("x", "x", null);
es.deployVDB("northwind", mmd);

Expand Down Expand Up @@ -713,8 +708,7 @@ public boolean supportsCompareCriteriaEquals() {
try {
ModelMetaData mmd = new ModelMetaData();
mmd.setName("m");
mmd.setSchemaSourceType("ddl");
mmd.setSchemaText("create foreign table x (a string, b string, c integer, primary key (a, b)) options (updatable true);");
mmd.addSourceMetadata("ddl", "create foreign table x (a string, b string, c integer, primary key (a, b)) options (updatable true);");
mmd.addSourceMapping("x", "x", null);
es.deployVDB("northwind", mmd);

Expand Down Expand Up @@ -749,8 +743,7 @@ public boolean supportsCompareCriteriaEquals() {
try {
ModelMetaData mmd = new ModelMetaData();
mmd.setName("m");
mmd.setSchemaSourceType("ddl");
mmd.setSchemaText("create foreign procedure x () returns table(y string);");
mmd.addSourceMetadata("ddl", "create foreign procedure x () returns table(y string);");
mmd.addSourceMapping("x", "x", null);
es.deployVDB("northwind", mmd);

Expand Down Expand Up @@ -825,8 +818,7 @@ protected List<? extends List<?>> getData(
ModelMetaData mmd = new ModelMetaData();
mmd.setName("m");
mmd.setModelType(Type.VIRTUAL);
mmd.setSchemaSourceType("ddl");
mmd.setSchemaText("create view v as select 1");
mmd.addSourceMetadata("ddl", "create view v as select 1");

Properties props = new Properties();
props.setProperty(ODBCServerRemoteImpl.CONNECTION_PROPERTY_PREFIX + ExecutionProperties.RESULT_SET_CACHE_MODE, "true");
Expand All @@ -845,6 +837,43 @@ protected List<? extends List<?>> getData(
}
}

@Test public void testEmbeddedComplexType() throws Exception {
EmbeddedServer es = new EmbeddedServer();
es.start(new EmbeddedConfiguration());
try {
ModelMetaData mmd = new ModelMetaData();
mmd.setName("m");
mmd.setModelType(Type.VIRTUAL);
mmd.addSourceMetadata("ddl", "CREATE VIEW Employees ( EmployeeID integer primary key, "
+ "LastName varchar(20), FirstName varchar(10), "
+ "Address varchar(60) options (\"teiid_odata:columngroup\" 'Address', \"teiid_odata:complextype\" 'NorthwindModel.Address'),"
+ "City varchar(15) options (\"teiid_odata:columngroup\" 'Address', \"teiid_odata:complextype\" 'NorthwindModel.Address')) as "
+ "select 1, 'wayne', 'john', '123 place', 'hollywood'");

es.deployVDB("northwind", mmd);

TeiidDriver td = es.getDriver();
Properties props = new Properties();
LocalClient lc = new LocalClient("northwind", 1, props);
lc.setDriver(td);
MockProvider.CLIENT = lc;

ClientRequest request = new ClientRequest(TestPortProvider.generateURL("/odata/northwind/Employees?$format=json&$select=Address"));
ClientResponse<String> response = request.get(String.class);
assertEquals(200, response.getStatus());
JSONParser parser = new JSONParser();
SimpleContentHandler sch = new SimpleContentHandler();
parser.parse(response.getEntity(), sch);
Map<String, ?> result = (Map<String, ?>)sch.getResult();
List<Object> results = (List<Object>)((Map<String, ?>)result.get("d")).get("results");
result = (Map<String, ?>)results.get(0);
assertEquals("123 place", result.get("Address"));
assertNull(result.get("City"));
} finally {
es.stop();
}
}

private OEntity createCustomersEntity(EdmDataServices metadata) {
EdmEntitySet entitySet = metadata.findEdmEntitySet("Customers");
OEntityKey entityKey = OEntityKey.parse("CustomerID='12'");
Expand Down

0 comments on commit 1fdf0fd

Please sign in to comment.