[teiid-commits] teiid SVN: r3885 - in branches/7.7.x: connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc and 5 other directories.

teiid-commits at lists.jboss.org teiid-commits at lists.jboss.org
Wed Feb 15 13:39:23 EST 2012


Author: shawkins
Date: 2012-02-15 13:39:23 -0500 (Wed, 15 Feb 2012)
New Revision: 3885

Added:
   branches/7.7.x/metadata/src/test/resources/keys.vdb
   branches/7.7.x/test-integration/common/src/test/java/org/teiid/jdbc/TestDynamicImportedMetaData.java
   branches/7.7.x/test-integration/common/src/test/resources/keys.vdb
Removed:
   branches/7.7.x/test-integration/common/src/test/java/org/teiid/jdbc/TestDymamicImportedMetaData.java
Modified:
   branches/7.7.x/api/src/main/java/org/teiid/metadata/ForeignKey.java
   branches/7.7.x/api/src/main/java/org/teiid/metadata/MetadataFactory.java
   branches/7.7.x/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/JDBCMetdataProcessor.java
   branches/7.7.x/metadata/src/main/java/org/teiid/metadata/index/IndexMetadataFactory.java
   branches/7.7.x/metadata/src/test/java/org/teiid/metadata/index/TestMultipleModelIndexes.java
Log:
TEIID-1946 allowing foreign keys to reference unique constraints

Modified: branches/7.7.x/api/src/main/java/org/teiid/metadata/ForeignKey.java
===================================================================
--- branches/7.7.x/api/src/main/java/org/teiid/metadata/ForeignKey.java	2012-02-15 15:58:55 UTC (rev 3884)
+++ branches/7.7.x/api/src/main/java/org/teiid/metadata/ForeignKey.java	2012-02-15 18:39:23 UTC (rev 3885)
@@ -47,10 +47,17 @@
         uniqueKeyID = keyID;
     }    
     
+    /**
+     * @return the primary key or unique key referenced by this foreign key
+     */
     public KeyRecord getPrimaryKey() {
     	return this.primaryKey;
     }
     
+    /**
+     * 
+     * @param primaryKey,  the primary key or unique key referenced by this foreign key
+     */
     public void setPrimaryKey(KeyRecord primaryKey) {
 		this.primaryKey = primaryKey;
 	}

Modified: branches/7.7.x/api/src/main/java/org/teiid/metadata/MetadataFactory.java
===================================================================
--- branches/7.7.x/api/src/main/java/org/teiid/metadata/MetadataFactory.java	2012-02-15 15:58:55 UTC (rev 3884)
+++ branches/7.7.x/api/src/main/java/org/teiid/metadata/MetadataFactory.java	2012-02-15 18:39:23 UTC (rev 3885)
@@ -185,7 +185,7 @@
 	}
 		
 	/**
-	 * Adds a foreign key to the given table.  The column names should be in key order.
+	 * Adds a foreign key to the given table.  The referenced primary key must already exist.  The column names should be in key order.
 	 * @param name
 	 * @param columnNames
 	 * @param pkTable
@@ -194,20 +194,65 @@
 	 * @throws TranslatorException
 	 */
 	public ForeignKey addForiegnKey(String name, List<String> columnNames, Table pkTable, Table table) throws TranslatorException {
+		return addForiegnKey(name, columnNames, null, pkTable, table, false);
+	}
+
+	/**
+	 * Adds a foreign key to the given table.  The referenced key may be automatically created if addUniqueConstraint is true. The column names should be in key order.
+	 * @param name
+	 * @param columnNames
+	 * @param referencedColumnNames, may be null to indicate that the primary key should be used.
+	 * @param pkTable
+	 * @param table
+	 * @param addUniqueConstraint
+	 * @return
+	 * @throws TranslatorException
+	 */	
+	public ForeignKey addForiegnKey(String name, List<String> columnNames, List<String> referencedColumnNames, Table pkTable, Table table, boolean addUniqueConstraint) throws TranslatorException {
 		ForeignKey foreignKey = new ForeignKey();
 		foreignKey.setParent(table);
+		KeyRecord uniqueKey = null;
 		foreignKey.setColumns(new ArrayList<Column>(columnNames.size()));
+		assignColumns(columnNames, table, foreignKey);
+		if (referencedColumnNames == null) {
+			uniqueKey = pkTable.getPrimaryKey();
+		} else {
+			for (KeyRecord record : pkTable.getUniqueKeys()) {
+				if (keyMatches(referencedColumnNames, record)) {
+					uniqueKey = record;
+					break;
+				}
+			}
+			if (uniqueKey == null && pkTable.getPrimaryKey() != null && keyMatches(referencedColumnNames, pkTable.getPrimaryKey())) {
+				uniqueKey = pkTable.getPrimaryKey();
+			}
+		}
+		if (uniqueKey == null) {
+			if (!addUniqueConstraint) {
+				throw new TranslatorException("No unique key defined for table " + pkTable + " for columns " + referencedColumnNames); //$NON-NLS-1$ //$NON-NLS-2$
+			}
+			uniqueKey = addIndex(name + "_unique", false, referencedColumnNames, pkTable); //$NON-NLS-1$
+		}
 		foreignKey.setName(name);
 		setUUID(foreignKey);
-		if (pkTable.getPrimaryKey() == null) {
-			throw new TranslatorException("No primary key defined for table " + pkTable); //$NON-NLS-1$
-		}
-		foreignKey.setPrimaryKey(pkTable.getPrimaryKey());
-		foreignKey.setUniqueKeyID(pkTable.getPrimaryKey().getUUID());
-		assignColumns(columnNames, table, foreignKey);
+		foreignKey.setPrimaryKey(uniqueKey);
+		foreignKey.setUniqueKeyID(uniqueKey.getUUID());
 		table.getForeignKeys().add(foreignKey);
 		return foreignKey;
 	}
+
+	private boolean keyMatches(List<String> names,
+			KeyRecord record) {
+		if (names.size() != record.getColumns().size()) {
+			return false;
+		}
+		for (int i = 0; i < names.size(); i++) {
+			if (!names.get(i).equals(record.getColumns().get(i))) {
+				return false;
+			}
+		}
+		return true;
+	}
 	
 	/**
 	 * Add a procedure with the given name to the model.  

Modified: branches/7.7.x/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/JDBCMetdataProcessor.java
===================================================================
--- branches/7.7.x/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/JDBCMetdataProcessor.java	2012-02-15 15:58:55 UTC (rev 3884)
+++ branches/7.7.x/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/JDBCMetdataProcessor.java	2012-02-15 18:39:23 UTC (rev 3885)
@@ -28,8 +28,10 @@
 import java.sql.SQLException;
 import java.sql.Types;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.LinkedHashSet;
 import java.util.Map;
 import java.util.Set;
 import java.util.TreeMap;
@@ -86,6 +88,7 @@
 	private boolean quoteNameInSource = true;
 	private boolean useProcedureSpecificName;
 	private boolean useCatalogName = true;
+	private boolean autoCreateUniqueConstraints = true;
 	
 	private Set<String> unsignedTypes = new HashSet<String>();
 	private String quoteString;
@@ -112,17 +115,15 @@
 		
 		try {
 			Map<String, TableInfo> tableMap = getTables(metadataFactory, metadata);
-			
+			HashSet<TableInfo> tables = new LinkedHashSet<TableInfo>(tableMap.values());
 			if (importKeys) {
-				getPrimaryKeys(metadataFactory, metadata, tableMap);
-				
-				getForeignKeys(metadataFactory, metadata, tableMap);
+				getPrimaryKeys(metadataFactory, metadata, tables);
+				getIndexes(metadataFactory, metadata, tables, !importIndexes);
+				getForeignKeys(metadataFactory, metadata, tables, tableMap);
+			} else if (importIndexes) {
+				getIndexes(metadataFactory, metadata, tables, false);
 			}
 			
-			if (importIndexes) {
-				getIndexes(metadataFactory, metadata, tableMap);
-			}
-			
 			if (importProcedures) {
 				getProcedures(metadataFactory, metadata);
 			}
@@ -214,8 +215,9 @@
 			table.setSupportsUpdate(true);
 			String remarks = tables.getString(5);
 			table.setAnnotation(remarks);
-			tableMap.put(fullName, new TableInfo(tableCatalog, tableSchema, tableName, table));
-			tableMap.put(tableName, new TableInfo(tableCatalog, tableSchema, tableName, table));
+			TableInfo ti = new TableInfo(tableCatalog, tableSchema, tableName, table);
+			tableMap.put(fullName, ti);
+			tableMap.put(tableName, ti);
 		}
 		tables.close();
 		
@@ -301,10 +303,10 @@
 	}
 
 	private void getPrimaryKeys(MetadataFactory metadataFactory,
-			DatabaseMetaData metadata, Map<String, TableInfo> tableMap)
+			DatabaseMetaData metadata, Collection<TableInfo> tables)
 			throws SQLException, TranslatorException {
 		LogManager.logDetail(LogConstants.CTX_CONNECTOR, "JDBCMetadataProcessor - Importing primary keys"); //$NON-NLS-1$
-		for (TableInfo tableInfo : tableMap.values()) {
+		for (TableInfo tableInfo : tables) {
 			ResultSet pks = metadata.getPrimaryKeys(tableInfo.catalog, tableInfo.schema, tableInfo.name);
 			TreeMap<Short, String> keyColumns = null;
 			String pkName = null;
@@ -330,26 +332,31 @@
 	}
 	
 	private void getForeignKeys(MetadataFactory metadataFactory,
-			DatabaseMetaData metadata, Map<String, TableInfo> tableMap) throws SQLException, TranslatorException {
+			DatabaseMetaData metadata, Collection<TableInfo> tables, Map<String, TableInfo> tableMap) throws SQLException, TranslatorException {
 		LogManager.logDetail(LogConstants.CTX_CONNECTOR, "JDBCMetadataProcessor - Importing foreign keys"); //$NON-NLS-1$
-		for (TableInfo tableInfo : tableMap.values()) {
+		for (TableInfo tableInfo : tables) {
 			ResultSet fks = metadata.getImportedKeys(tableInfo.catalog, tableInfo.schema, tableInfo.name);
 			TreeMap<Short, String> keyColumns = null;
+			TreeMap<Short, String> referencedKeyColumns = null;
 			String fkName = null;
 			TableInfo pkTable = null;
 			short savedSeqNum = Short.MAX_VALUE;
 			while (fks.next()) {
 				String columnName = fks.getString(8);
 				short seqNum = fks.getShort(9);
+				String pkColumnName = fks.getString(4);
+				
 				if (seqNum <= savedSeqNum) {
 					if (keyColumns != null) {
-						metadataFactory.addForiegnKey(fkName, new ArrayList<String>(keyColumns.values()), pkTable.table, tableInfo.table);
+						metadataFactory.addForiegnKey(fkName, new ArrayList<String>(keyColumns.values()), new ArrayList<String>(referencedKeyColumns.values()), pkTable.table, tableInfo.table, autoCreateUniqueConstraints);
 					}
 					keyColumns = new TreeMap<Short, String>();
+					referencedKeyColumns = new TreeMap<Short, String>();
 					fkName = null;
 				}
 				savedSeqNum = seqNum;
 				keyColumns.put(seqNum, columnName);
+				referencedKeyColumns.put(seqNum, pkColumnName);
 				if (fkName == null) {
 					String tableCatalog = fks.getString(1);
 					String tableSchema = fks.getString(2);
@@ -367,16 +374,16 @@
 				} 
 			}
 			if (keyColumns != null) {
-				metadataFactory.addForiegnKey(fkName, new ArrayList<String>(keyColumns.values()), pkTable.table, tableInfo.table);
+				metadataFactory.addForiegnKey(fkName, new ArrayList<String>(keyColumns.values()), new ArrayList<String>(referencedKeyColumns.values()), pkTable.table, tableInfo.table, autoCreateUniqueConstraints);
 			}
 			fks.close();
 		}
 	}
 
 	private void getIndexes(MetadataFactory metadataFactory,
-			DatabaseMetaData metadata, Map<String, TableInfo> tableMap) throws SQLException, TranslatorException {
+			DatabaseMetaData metadata, Collection<TableInfo> tables, boolean uniqueOnly) throws SQLException, TranslatorException {
 		LogManager.logDetail(LogConstants.CTX_CONNECTOR, "JDBCMetadataProcessor - Importing index info"); //$NON-NLS-1$
-		for (TableInfo tableInfo : tableMap.values()) {
+		for (TableInfo tableInfo : tables) {
 			ResultSet indexInfo = metadata.getIndexInfo(tableInfo.catalog, tableInfo.schema, tableInfo.name, false, importApproximateIndexes);
 			TreeMap<Short, String> indexColumns = null;
 			String indexName = null;
@@ -390,7 +397,7 @@
 				}
 				short ordinalPosition = indexInfo.getShort(8);
 				if (ordinalPosition <= savedOrdinalPosition) {
-					if (indexColumns != null) {
+					if (indexColumns != null && (!uniqueOnly || !nonUnique)) {
 						metadataFactory.addIndex(indexName, nonUnique, new ArrayList<String>(indexColumns.values()), tableInfo.table);
 					}
 					indexColumns = new TreeMap<Short, String>();
@@ -407,7 +414,7 @@
 					}
 				}
 			}
-			if (indexColumns != null) {
+			if (indexColumns != null && (!uniqueOnly || !nonUnique)) {
 				metadataFactory.addIndex(indexName, nonUnique, new ArrayList<String>(indexColumns.values()), tableInfo.table);
 			}
 			indexInfo.close();
@@ -486,4 +493,9 @@
 		this.useCatalogName = useCatalog;
 	}
 	
+	public void setAutoCreateUniqueConstraints(
+			boolean autoCreateUniqueConstraints) {
+		this.autoCreateUniqueConstraints = autoCreateUniqueConstraints;
+	}
+	
 }

Modified: branches/7.7.x/metadata/src/main/java/org/teiid/metadata/index/IndexMetadataFactory.java
===================================================================
--- branches/7.7.x/metadata/src/main/java/org/teiid/metadata/index/IndexMetadataFactory.java	2012-02-15 15:58:55 UTC (rev 3884)
+++ branches/7.7.x/metadata/src/main/java/org/teiid/metadata/index/IndexMetadataFactory.java	2012-02-15 18:39:23 UTC (rev 3885)
@@ -363,7 +363,11 @@
 				}
 		        tableRecord.setForiegnKeys(getByParent(tableRecord.getUUID(), MetadataConstants.RECORD_TYPE.FOREIGN_KEY, ForeignKey.class, false));
 		        for (ForeignKey foreignKeyRecord : tableRecord.getForeignKeys()) {
-		        	foreignKeyRecord.setPrimaryKey(getPrimaryKey(foreignKeyRecord.getUniqueKeyID()));
+		        	KeyRecord pk = (KeyRecord) getRecordByType(foreignKeyRecord.getUniqueKeyID(), MetadataConstants.RECORD_TYPE.PRIMARY_KEY, false);
+		        	if (pk == null) {
+		        		pk = (KeyRecord) getRecordByType(foreignKeyRecord.getUniqueKeyID(), MetadataConstants.RECORD_TYPE.UNIQUE_KEY);
+		        	}
+		        	foreignKeyRecord.setPrimaryKey(pk);
 		        	loadColumnSetRecords(foreignKeyRecord, uuidColumnMap);
 		        	foreignKeyRecord.setParent(tableRecord);
 				}
@@ -378,7 +382,7 @@
 					columnSetRecordImpl.setParent(tableRecord);
 				}
 		        if (tableRecord.getPrimaryKey() != null) {
-		        	KeyRecord primaryKey = getPrimaryKey(tableRecord.getPrimaryKey().getUUID());
+		        	KeyRecord primaryKey = (KeyRecord) getRecordByType(tableRecord.getPrimaryKey().getUUID(), MetadataConstants.RECORD_TYPE.PRIMARY_KEY);
 		        	loadColumnSetRecords(primaryKey, uuidColumnMap);
 		        	primaryKey.setParent(tableRecord);
 		        	tableRecord.setPrimaryKey(primaryKey);

Modified: branches/7.7.x/metadata/src/test/java/org/teiid/metadata/index/TestMultipleModelIndexes.java
===================================================================
--- branches/7.7.x/metadata/src/test/java/org/teiid/metadata/index/TestMultipleModelIndexes.java	2012-02-15 15:58:55 UTC (rev 3884)
+++ branches/7.7.x/metadata/src/test/java/org/teiid/metadata/index/TestMultipleModelIndexes.java	2012-02-15 18:39:23 UTC (rev 3885)
@@ -51,6 +51,14 @@
 		assertNotNull(t.getColumns().get(0).getDatatype());
 	}
 	
+	@Test public void testUniqueReferencedKey() throws Exception {
+		TransformationMetadata tm = VDBMetadataFactory.getVDBMetadata(UnitTestUtil.getTestDataPath() + "/keys.vdb");
+		Collection fks = tm.getForeignKeysInGroup(tm.getGroupID("x.a"));
+		assertEquals(1, fks.size());
+		Object pk = tm.getPrimaryKeyIDForForeignKeyID(fks.iterator().next());
+		assertNotNull(pk);
+	}
+	
 	@Test public void testSchemaLoad() throws Exception {
 		TransformationMetadata tm = VDBMetadataFactory.getVDBMetadata(UnitTestUtil.getTestDataPath() + "/Test.vdb");
 		

Added: branches/7.7.x/metadata/src/test/resources/keys.vdb
===================================================================
(Binary files differ)


Property changes on: branches/7.7.x/metadata/src/test/resources/keys.vdb
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Deleted: branches/7.7.x/test-integration/common/src/test/java/org/teiid/jdbc/TestDymamicImportedMetaData.java
===================================================================
--- branches/7.7.x/test-integration/common/src/test/java/org/teiid/jdbc/TestDymamicImportedMetaData.java	2012-02-15 15:58:55 UTC (rev 3884)
+++ branches/7.7.x/test-integration/common/src/test/java/org/teiid/jdbc/TestDymamicImportedMetaData.java	2012-02-15 18:39:23 UTC (rev 3885)
@@ -1,129 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * See the COPYRIGHT.txt file distributed with this work for information
- * regarding copyright ownership.  Some portions may be licensed
- * to Red Hat, Inc. under one or more contributor license agreements.
- * 
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- * 
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- * 
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301 USA.
- */
-
-package org.teiid.jdbc;
-
-import static org.junit.Assert.*;
-
-import java.sql.Connection;
-import java.util.LinkedHashMap;
-import java.util.Properties;
-
-import org.junit.Test;
-import org.teiid.core.types.DataTypeManager;
-import org.teiid.core.util.UnitTestUtil;
-import org.teiid.deployers.VDBRepository;
-import org.teiid.metadata.MetadataFactory;
-import org.teiid.metadata.MetadataStore;
-import org.teiid.metadata.Procedure;
-import org.teiid.metadata.Table;
-import org.teiid.metadata.index.VDBMetadataFactory;
-import org.teiid.query.metadata.TransformationMetadata.Resource;
-import org.teiid.translator.TranslatorException;
-import org.teiid.translator.jdbc.teiid.TeiidExecutionFactory;
-
-
-/**
- */
- at SuppressWarnings("nls")
-public class TestDymamicImportedMetaData {
-
-	private MetadataFactory getMetadata(Properties importProperties, Connection conn)
-			throws TranslatorException {
-		MetadataFactory mf = createMetadataFactory("test", importProperties);
-    	
-    	TeiidExecutionFactory tef = new TeiidExecutionFactory();
-    	tef.getMetadata(mf, conn);
-    	return mf;
-	}
-
-	private MetadataFactory createMetadataFactory(String schema, Properties importProperties) {
-		VDBRepository vdbRepository = new VDBRepository();
-    	vdbRepository.setSystemStore(VDBMetadataFactory.getSystem());
-    	return new MetadataFactory(schema, vdbRepository.getBuiltinDatatypes(), importProperties);
-	}
-	
-    @Test public void testProcImport() throws Exception {
-    	FakeServer server = new FakeServer();
-    	server.deployVDB("vdb", UnitTestUtil.getTestDataPath() + "/TestCase3473/test.vdb");
-    	Connection conn = server.createConnection("jdbc:teiid:vdb"); //$NON-NLS-1$
-    	
-    	Properties importProperties = new Properties();
-    	importProperties.setProperty("importer.importProcedures", Boolean.TRUE.toString());
-    	MetadataFactory mf = getMetadata(importProperties, conn);
-    	Procedure p = mf.getMetadataStore().getSchemas().get("TEST").getProcedures().get("VDB.SYS.GETXMLSCHEMAS");
-    	assertEquals(1, p.getResultSet().getColumns().size());
-    }
-    
-    @Test public void testDuplicateException() throws Exception {
-    	FakeServer server = new FakeServer();
-    	MetadataFactory mf = createMetadataFactory("x", new Properties());
-    	MetadataFactory mf1 = createMetadataFactory("y", new Properties());
-    	
-    	Table dup = mf.addTable("dup");
-    	Table dup1 = mf1.addTable("dup");
-    	
-    	mf.addColumn("x", DataTypeManager.DefaultDataTypes.STRING, dup);
-    	mf1.addColumn("x", DataTypeManager.DefaultDataTypes.STRING, dup1);
-    	
-    	MetadataStore ms = mf.getMetadataStore();
-    	ms.addSchema(mf1.getMetadataStore().getSchemas().values().iterator().next());
-    	
-    	server.deployVDB("test", ms, new LinkedHashMap<String, Resource>());
-    	Connection conn = server.createConnection("jdbc:teiid:test"); //$NON-NLS-1$
-    	
-    	Properties importProperties = new Properties();
-    	
-    	mf = getMetadata(importProperties, conn);
-    	Table t = mf.getMetadataStore().getSchemas().get("TEST").getTables().get("TEST.X.DUP");
-    	assertEquals("\"test\".\"x\".\"dup\"", t.getNameInSource());
-
-    	importProperties.setProperty("importer.useFullSchemaName", Boolean.FALSE.toString());
-    	try {
-    		getMetadata(importProperties, conn);
-    		fail();
-    	} catch (TranslatorException e) {
-    		
-    	}
-    }
-    
-    @Test public void testUseCatalog() throws Exception {
-    	FakeServer server = new FakeServer();
-    	MetadataFactory mf = createMetadataFactory("x", new Properties());
-    	
-    	Table dup = mf.addTable("dup");
-    	
-    	mf.addColumn("x", DataTypeManager.DefaultDataTypes.STRING, dup);
-    	
-    	MetadataStore ms = mf.getMetadataStore();
-    	
-    	server.deployVDB("test", ms, new LinkedHashMap<String, Resource>());
-    	Connection conn = server.createConnection("jdbc:teiid:test"); //$NON-NLS-1$
-    	
-    	Properties importProperties = new Properties();
-    	importProperties.setProperty("importer.useCatalogName", Boolean.FALSE.toString());
-    	mf = getMetadata(importProperties, conn);
-    	Table t = mf.getMetadataStore().getSchemas().get("TEST").getTables().get("X.DUP");
-    	assertEquals("\"x\".\"dup\"", t.getNameInSource());
-    }
-
-}

Copied: branches/7.7.x/test-integration/common/src/test/java/org/teiid/jdbc/TestDynamicImportedMetaData.java (from rev 3868, branches/7.7.x/test-integration/common/src/test/java/org/teiid/jdbc/TestDymamicImportedMetaData.java)
===================================================================
--- branches/7.7.x/test-integration/common/src/test/java/org/teiid/jdbc/TestDynamicImportedMetaData.java	                        (rev 0)
+++ branches/7.7.x/test-integration/common/src/test/java/org/teiid/jdbc/TestDynamicImportedMetaData.java	2012-02-15 18:39:23 UTC (rev 3885)
@@ -0,0 +1,154 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * See the COPYRIGHT.txt file distributed with this work for information
+ * regarding copyright ownership.  Some portions may be licensed
+ * to Red Hat, Inc. under one or more contributor license agreements.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA.
+ */
+
+package org.teiid.jdbc;
+
+import static org.junit.Assert.*;
+
+import java.sql.Connection;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Properties;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.teiid.core.types.DataTypeManager;
+import org.teiid.core.util.UnitTestUtil;
+import org.teiid.deployers.VDBRepository;
+import org.teiid.metadata.ForeignKey;
+import org.teiid.metadata.MetadataFactory;
+import org.teiid.metadata.MetadataStore;
+import org.teiid.metadata.Procedure;
+import org.teiid.metadata.Table;
+import org.teiid.metadata.index.VDBMetadataFactory;
+import org.teiid.query.metadata.TransformationMetadata.Resource;
+import org.teiid.translator.TranslatorException;
+import org.teiid.translator.jdbc.teiid.TeiidExecutionFactory;
+
+
+/**
+ */
+ at SuppressWarnings("nls")
+public class TestDynamicImportedMetaData {
+
+	private FakeServer server;
+	
+	@Before public void setup() {
+		this.server = new FakeServer();
+	}
+	
+	@After public void tearDown() {
+		this.server.stop();
+	}
+
+	private MetadataFactory getMetadata(Properties importProperties, Connection conn)
+			throws TranslatorException {
+		MetadataFactory mf = createMetadataFactory("test", importProperties);
+    	
+    	TeiidExecutionFactory tef = new TeiidExecutionFactory();
+    	tef.getMetadata(mf, conn);
+    	return mf;
+	}
+
+	private MetadataFactory createMetadataFactory(String schema, Properties importProperties) {
+		VDBRepository vdbRepository = new VDBRepository();
+    	vdbRepository.setSystemStore(VDBMetadataFactory.getSystem());
+    	return new MetadataFactory(schema, vdbRepository.getBuiltinDatatypes(), importProperties);
+	}
+	
+	@Test public void testUniqueReferencedKey() throws Exception {
+		server.deployVDB("vdb", UnitTestUtil.getTestDataPath() + "/keys.vdb");
+    	Connection conn = server.createConnection("jdbc:teiid:vdb"); //$NON-NLS-1$
+    	
+    	Properties importProperties = new Properties();
+    	importProperties.setProperty("importer.importKeys", "true");
+    	importProperties.setProperty("importer.schemaPattern", "x");
+    	MetadataFactory mf = getMetadata(importProperties, conn);
+    	Table t = mf.getMetadataStore().getSchemas().get("TEST").getTables().get("VDB.X.A");
+    	List<ForeignKey> fks = t.getForeignKeys();
+    	assertEquals(1, fks.size());
+    	assertNotNull(fks.get(0).getPrimaryKey());
+	}
+	
+    @Test public void testProcImport() throws Exception {
+    	server.deployVDB("vdb", UnitTestUtil.getTestDataPath() + "/TestCase3473/test.vdb");
+    	Connection conn = server.createConnection("jdbc:teiid:vdb"); //$NON-NLS-1$
+    	
+    	Properties importProperties = new Properties();
+    	importProperties.setProperty("importer.importProcedures", Boolean.TRUE.toString());
+    	MetadataFactory mf = getMetadata(importProperties, conn);
+    	Procedure p = mf.getMetadataStore().getSchemas().get("TEST").getProcedures().get("VDB.SYS.GETXMLSCHEMAS");
+    	assertEquals(1, p.getResultSet().getColumns().size());
+    }
+    
+    @Test public void testDuplicateException() throws Exception {
+    	MetadataFactory mf = createMetadataFactory("x", new Properties());
+    	MetadataFactory mf1 = createMetadataFactory("y", new Properties());
+    	
+    	Table dup = mf.addTable("dup");
+    	Table dup1 = mf1.addTable("dup");
+    	
+    	mf.addColumn("x", DataTypeManager.DefaultDataTypes.STRING, dup);
+    	mf1.addColumn("x", DataTypeManager.DefaultDataTypes.STRING, dup1);
+    	
+    	MetadataStore ms = mf.getMetadataStore();
+    	ms.addSchema(mf1.getMetadataStore().getSchemas().values().iterator().next());
+    	
+    	server.deployVDB("test", ms, new LinkedHashMap<String, Resource>());
+    	Connection conn = server.createConnection("jdbc:teiid:test"); //$NON-NLS-1$
+    	
+    	Properties importProperties = new Properties();
+    	
+    	mf = getMetadata(importProperties, conn);
+    	Table t = mf.getMetadataStore().getSchemas().get("TEST").getTables().get("TEST.X.DUP");
+    	assertEquals("\"test\".\"x\".\"dup\"", t.getNameInSource());
+
+    	importProperties.setProperty("importer.useFullSchemaName", Boolean.FALSE.toString());
+    	try {
+    		getMetadata(importProperties, conn);
+    		fail();
+    	} catch (TranslatorException e) {
+    		
+    	}
+    }
+    
+    @Test public void testUseCatalog() throws Exception {
+    	MetadataFactory mf = createMetadataFactory("x", new Properties());
+    	
+    	Table dup = mf.addTable("dup");
+    	
+    	mf.addColumn("x", DataTypeManager.DefaultDataTypes.STRING, dup);
+    	
+    	MetadataStore ms = mf.getMetadataStore();
+    	
+    	server.deployVDB("test", ms, new LinkedHashMap<String, Resource>());
+    	Connection conn = server.createConnection("jdbc:teiid:test"); //$NON-NLS-1$
+    	
+    	Properties importProperties = new Properties();
+    	importProperties.setProperty("importer.useCatalogName", Boolean.FALSE.toString());
+    	mf = getMetadata(importProperties, conn);
+    	Table t = mf.getMetadataStore().getSchemas().get("TEST").getTables().get("X.DUP");
+    	assertEquals("\"x\".\"dup\"", t.getNameInSource());
+    }
+
+}


Property changes on: branches/7.7.x/test-integration/common/src/test/java/org/teiid/jdbc/TestDynamicImportedMetaData.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain

Added: branches/7.7.x/test-integration/common/src/test/resources/keys.vdb
===================================================================
(Binary files differ)


Property changes on: branches/7.7.x/test-integration/common/src/test/resources/keys.vdb
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream



More information about the teiid-commits mailing list