use of org.teiid.metadata.ForeignKey in project teiid by teiid.
the class CoherenceVisitor method getNameFromElement.
/**
* Method to get name from the supplied Element
* @param e the supplied Element
* @return the name
*/
// GHH 20080326 - found that code to fall back on Name if NameInSource
// was null wasn't working properly, so replaced with tried and true
// code from another custom connector.
public String getNameFromElement(Column e) {
String attributeName = null;
Object p = e.getParent();
if (p instanceof Table) {
Table t = (Table) p;
if (t.getForeignKeys() != null && !t.getForeignKeys().isEmpty()) {
ForeignKey fk = (ForeignKey) t.getForeignKeys().get(0);
String fk_nis = fk.getNameInSource();
attributeName = fk_nis + "." + e.getNameInSource();
}
}
if (attributeName == null) {
attributeName = e.getNameInSource();
}
if (attributeName == null || attributeName.equals("")) {
// $NON-NLS-1$
attributeName = e.getName();
// If name in source is not set, then fall back to the column name.
}
return attributeName;
}
use of org.teiid.metadata.ForeignKey in project teiid by teiid.
the class TestCoherenceTranslator method getTradeTranslationUtility.
/**
* this is used as an alternative to loading the vdb
* @return
*/
private TranslationUtility getTradeTranslationUtility() {
MetadataStore metadataStore = new MetadataStore();
// Create TRADE
// $NON-NLS-1$
Schema trading = RealMetadataFactory.createPhysicalModel("Trade_Object", metadataStore);
// Create physical groups
// $NON-NLS-1$
Table trade = RealMetadataFactory.createPhysicalGroup("TRADE", trading);
trade.setNameInSource("org.teiid.translator.coherence.Trade");
// Create physical elements
String[] elemNames = new String[] { // $NON-NLS-1$ //$NON-NLS-2$
"NAME", // $NON-NLS-1$ //$NON-NLS-2$
"TRADEID" };
String[] elemTypes = new String[] { DataTypeManager.DefaultDataTypes.STRING, DataTypeManager.DefaultDataTypes.LONG };
List<Column> cols = RealMetadataFactory.createElements(trade, elemNames, elemTypes);
// Set name in source on each column
String[] nameInSource = new String[] { "Name", "TradeId" };
for (int i = 0; i < nameInSource.length; i++) {
cols.get(i).setNameInSource(nameInSource[i]);
}
List<Column> keys = new ArrayList(1);
keys.add(cols.get(1));
KeyRecord trade_pk = RealMetadataFactory.createKey(Type.Primary, "TradeID_PK", trade, keys);
// LEG
// $NON-NLS-1$
Table leg = RealMetadataFactory.createPhysicalGroup("LEG", trading);
leg.setNameInSource("org.teiid.translator.coherence.Leg");
// Create physical elements
String[] legNames = new String[] { // $NON-NLS-1$ //$NON-NLS-2$
"LEGID", // $NON-NLS-1$ //$NON-NLS-2$
"NOTATIONAL", // $NON-NLS-1$ //$NON-NLS-2$
"NAME", // $NON-NLS-1$ //$NON-NLS-2$
"TRADEID" };
String[] legTypes = new String[] { DataTypeManager.DefaultDataTypes.LONG, DataTypeManager.DefaultDataTypes.DOUBLE, DataTypeManager.DefaultDataTypes.STRING, DataTypeManager.DefaultDataTypes.LONG };
List<Column> legcols = RealMetadataFactory.createElements(leg, legNames, legTypes);
// Set name in source on each column
String[] legnameInSource = new String[] { "LegId", "Notational", "Name", "TradeId" };
for (int i = 0; i < legnameInSource.length; i++) {
legcols.get(i).setNameInSource(legnameInSource[i]);
}
legcols.get(3).setSelectable(false);
legcols.get(3).setUpdatable(false);
List<Column> legkeys = new ArrayList(1);
keys.add(legcols.get(0));
RealMetadataFactory.createKey(Type.Primary, "Leg_ID_PK", leg, legkeys);
List<Column> foreignkey = new ArrayList(1);
foreignkey.add(legcols.get(3));
ForeignKey fk = RealMetadataFactory.createForeignKey("TRADE_FK", leg, foreignkey, trade_pk);
fk.setNameInSource("Legs");
fk.setParent(trade);
// Set column-specific properties
// cols.get(0).setSelectable(false);
// cols.get(0).setSearchType(SearchType.Unsearchable);
Schema tradeview = RealMetadataFactory.createVirtualModel("Trade_View", metadataStore);
QueryNode qn = new QueryNode("SELECT Trade_Object.Trade.TradeID, Trade_Object.Trade.Name, Trade_Object.Leg.LegID, Trade_Object.Leg.Notational, Trade_Object.Leg.Name AS LegName " + " FROM Trade_Object.Trade, Trade_Object.Leg " + "WHERE Trade_Object.Trade.TradeID = Trade_Object.Leg.TradeID " + "SELECT * FROM Trade.Trad " + " WHERE Trade.Trade.TradeID = Trade.Trade.TradeID");
Table trades = RealMetadataFactory.createVirtualGroup("Trades", tradeview, qn);
return new TranslationUtility(RealMetadataFactory.createTransformationMetadata(metadataStore, "Trade"));
}
use of org.teiid.metadata.ForeignKey in project teiid by teiid.
the class TestDynamicImportedMetaData method testDroppedFk.
@Test
public void testDroppedFk() throws Exception {
ModelMetaData mmd = new ModelMetaData();
mmd.addSourceMetadata("ddl", "create foreign table x (y integer primary key);" + "create foreign table z (y integer, foreign key (y) references x)");
mmd.setName("foo");
mmd.addSourceMapping("x", "x", "x");
server.addTranslator("x", new ExecutionFactory());
server.deployVDB("vdb", mmd);
// $NON-NLS-1$
Connection conn = server.createConnection("jdbc:teiid:vdb");
Properties importProperties = new Properties();
importProperties.setProperty("importer.importKeys", "true");
// only import z and not the referenced table x
importProperties.setProperty("importer.tableNamePattern", "z");
MetadataFactory mf = getMetadata(importProperties, conn);
Table t = mf.asMetadataStore().getSchemas().get("test").getTables().get("vdb.foo.z");
List<ForeignKey> fks = t.getForeignKeys();
assertEquals(0, fks.size());
}
use of org.teiid.metadata.ForeignKey in project teiid by teiid.
the class TestDynamicImportedMetaData method testUniqueReferencedKey.
@Test
public void testUniqueReferencedKey() throws Exception {
server.deployVDB("vdb", UnitTestUtil.getTestDataPath() + "/keys.vdb");
// $NON-NLS-1$
Connection conn = server.createConnection("jdbc:teiid:vdb");
Properties importProperties = new Properties();
importProperties.setProperty("importer.importKeys", "true");
importProperties.setProperty("importer.schemaPattern", "x");
MetadataFactory mf = getMetadata(importProperties, conn);
Table t = mf.asMetadataStore().getSchemas().get("TEST").getTables().get("VDB.X.A");
List<ForeignKey> fks = t.getForeignKeys();
assertEquals(1, fks.size());
assertNotNull(fks.get(0).getPrimaryKey());
}
Aggregations