Skip to content

Commit

Permalink
TEIID-2799 When the metadata is imported for the cache, add the pkkey…
Browse files Browse the repository at this point in the history
… when its defined
  • Loading branch information
vhalbert committed Jan 14, 2014
1 parent 20364e2 commit f525645
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 24 deletions.
Expand Up @@ -38,6 +38,7 @@ public class ObjectPlugin {
*/
public static final BundleUtil Util = new BundleUtil(PLUGIN_ID, PLUGIN_ID + ".i18n", ResourceBundle.getBundle(PLUGIN_ID + ".i18n")); //$NON-NLS-1$ //$NON-NLS-2$

public static enum Event implements BundleUtil.Event {
public static enum Event implements BundleUtil.Event{
TEIID21000
}
}
Expand Up @@ -91,23 +91,13 @@ public List<Object> search(Select command, String cacheName,
ObjectConnection connection, ExecutionContext executionContext)
throws TranslatorException {

// ClassLoader cl = Thread.currentThread().getContextClassLoader();
// try {
// Thread.currentThread().setContextClassLoader(
//
// (executionContext.getCommandContext() != null ? executionContext.getCommandContext().getVDBClassLoader() : this.getClass().getClassLoader() ) );

if (supportsLuceneSearching()) {
Class<?> type = connection.getType(cacheName);
return LuceneSearch.performSearch(command, type, cacheName,
connection.getCacheContainer());
}

return super.search(command, cacheName, connection, executionContext);

// } finally {
// Thread.currentThread().setContextClassLoader(cl);
// }
}

}
Expand Up @@ -25,9 +25,12 @@
import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.ArrayList;

import javax.script.ScriptException;

import org.teiid.logging.LogConstants;
import org.teiid.logging.LogManager;
import org.teiid.metadata.BaseColumn.NullType;
import org.teiid.metadata.Column;
import org.teiid.metadata.Column.SearchType;
Expand All @@ -38,6 +41,7 @@
import org.teiid.translator.TypeFacility;
import org.teiid.translator.object.ObjectConnection;
import org.teiid.translator.object.ObjectExecutionFactory;
import org.teiid.translator.object.ObjectPlugin;


/**
Expand Down Expand Up @@ -81,29 +85,41 @@ private Table createSourceTable(MetadataFactory mf, Class<?> entity, String cach
table.setProperty(ENTITYCLASS, entity.getName());

String columnName = tableName + OBJECT_COL_SUFFIX;
addColumn(mf, entity, entity, columnName, "this", SearchType.Unsearchable, table); //$NON-NLS-1$
addColumn(mf, entity, entity, columnName, "this", SearchType.Unsearchable, table, true); //$NON-NLS-1$
Map<String, Method> methods;
try {
methods = engine.getMethodMap(entity);
} catch (ScriptException e) {
throw new MetadataException(e);
}

Method pkMethod = null;
if (pkField != null) {
pkMethod = methods.get(pkField);
if (pkMethod != null) {
addColumn(mf, entity, pkMethod.getReturnType(), pkField, pkField, SearchType.Searchable, table);
} else {
//TODO: warning/error?
}
}

Method pkMethod = null;
if (pkField != null) {
pkMethod = methods.get(pkField);
if (pkMethod != null) {
addColumn(mf, entity, pkMethod.getReturnType(), pkField, pkField, SearchType.Searchable, table, true);
} else {
// add a column so the PKey can be created, but make it not selectable
addColumn(mf, entity, java.lang.String.class, pkField, pkField, SearchType.Searchable, table, false);

}

String pkName = "PK_" + pkField.toUpperCase();
ArrayList<String> x = new ArrayList<String>(1) ;
x.add(pkField);
mf.addPrimaryKey(pkName, x , table);

} else {
// warn if no pk is defined
LogManager.logWarning(LogConstants.CTX_CONNECTOR, ObjectPlugin.Util.gs(ObjectPlugin.Event.TEIID21000, tableName));
}

//we have to filter the duplicate names, isFoo vs. foo
Map<Method, String> methodsToAdd = new LinkedHashMap<Method, String>();
for (Map.Entry<String, Method> entry : methods.entrySet()) {
String name = methodsToAdd.get(entry.getValue());
if (name == null || name.length() > entry.getKey().length()) {
// dont create a column for the already created PKkey
if (entry.getValue() == pkMethod
|| entry.getValue().getDeclaringClass() == Object.class
|| entry.getValue().getName().equals("toString") //$NON-NLS-1$
Expand All @@ -116,8 +132,9 @@ private Table createSourceTable(MetadataFactory mf, Class<?> entity, String cach
}

for (Map.Entry<Method, String> entry : methodsToAdd.entrySet()) {
addColumn(mf, entity, entry.getKey().getReturnType(), entry.getValue(), entry.getValue(), SearchType.Unsearchable, table);
addColumn(mf, entity, entry.getKey().getReturnType(), entry.getValue(), entry.getValue(), SearchType.Unsearchable, table, true);
}

return table;
}

Expand Down Expand Up @@ -145,7 +162,7 @@ protected boolean isUpdateable(Class<?> entity, String columnName) {
return this.isUpdatable;
}

protected Column addColumn(MetadataFactory mf, Class<?> entity, Class<?> type, String attributeName, String nis, SearchType searchType, Table entityTable) {
protected Column addColumn(MetadataFactory mf, Class<?> entity, Class<?> type, String attributeName, String nis, SearchType searchType, Table entityTable, boolean selectable) {
Column c = entityTable.getColumnByName(attributeName);
if (c != null) {
//TODO: there should be a log here
Expand All @@ -158,6 +175,7 @@ protected Column addColumn(MetadataFactory mf, Class<?> entity, Class<?> type, S
c.setUpdatable(isUpdateable(entity, attributeName));
c.setSearchType(searchType);
c.setNativeType(type.getName());
c.setSelectable(selectable);
if (type.isPrimitive()) {
c.setNullType(NullType.No_Nulls);
}
Expand Down
Expand Up @@ -24,3 +24,5 @@ LuceneSearch.unsupportedComparingByNull=Comparing using a NULL is not currently
LuceneSearch.Unsupported_expression=Unsupported expression of {0} when performing {1}

MapCacheConnection.unexpectedObjectTypeInCache=Unexpected root object type in the cache is {0}, expected {1}

TEIID21000=No primary key was defined for table {0}, consider defining one for better performance.
Expand Up @@ -102,6 +102,8 @@ public TestFactory() {
assertEquals(NullType.No_Nulls, physicalTable.getColumns().get(1).getNullType());
//name
assertEquals("string", physicalTable.getColumns().get(2).getRuntimeType());

assertEquals(1, physicalTable.getAllKeys().size());
}

}

0 comments on commit f525645

Please sign in to comment.