[teiid-commits] teiid SVN: r2563 - in branches/7.1.x/connectors/translator-jdbc/src: test/java/org/teiid/translator/jdbc/modeshape and 1 other directories.

teiid-commits at lists.jboss.org teiid-commits at lists.jboss.org
Mon Sep 13 17:30:51 EDT 2010


Author: vhalbert at redhat.com
Date: 2010-09-13 17:30:51 -0400 (Mon, 13 Sep 2010)
New Revision: 2563

Modified:
   branches/7.1.x/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/modeshape/ModeShapeExecutionFactory.java
   branches/7.1.x/connectors/translator-jdbc/src/test/java/org/teiid/translator/jdbc/modeshape/TestModeShapeSqlTranslator.java
   branches/7.1.x/connectors/translator-jdbc/src/test/resources/ModeShape.vdb
Log:
TEIID-1106 - When importing via Designer using the ModeShape jdbc driver, the name-in-source value will result in having tic marks surround it because the value contains a special character (i.e.,  nt:base becomes 'nt:base').  Therefore, these changes remove the tic marks before pushing the query to ModeShape.

Modified: branches/7.1.x/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/modeshape/ModeShapeExecutionFactory.java
===================================================================
--- branches/7.1.x/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/modeshape/ModeShapeExecutionFactory.java	2010-09-13 19:14:47 UTC (rev 2562)
+++ branches/7.1.x/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/modeshape/ModeShapeExecutionFactory.java	2010-09-13 21:30:51 UTC (rev 2563)
@@ -35,12 +35,14 @@
 import org.teiid.language.LanguageObject;
 import org.teiid.language.Literal;
 import org.teiid.language.NamedTable;
+import org.teiid.translator.SourceSystemFunctions;
 import org.teiid.translator.ExecutionContext;
 import org.teiid.translator.Translator;
 import org.teiid.translator.TranslatorException;
 import org.teiid.translator.jdbc.ConvertModifier;
 import org.teiid.translator.jdbc.FunctionModifier;
 import org.teiid.translator.jdbc.JDBCExecutionFactory;
+import org.teiid.translator.jdbc.SQLConversionVisitor;
 
 
 
@@ -76,10 +78,7 @@
                 return objs; 
             	}
         }   );
-        	
-
-
-           
+       
         //add in type conversion
         ConvertModifier convertModifier = new ConvertModifier();
    	 
@@ -126,31 +125,58 @@
 				return null;
 			}
 		}, FunctionModifier.BOOLEAN);
+    	
+    	registerFunctionModifier(SourceSystemFunctions.CONVERT, convertModifier);
      }    
     
+    /**
+     * Create the {@link SQLConversionVisitor} that will perform translation.  Typical custom
+     * JDBC connectors will not need to create custom conversion visitors, rather implementors 
+     * should override existing {@link JDBCExecutionFactory} methods.
+     * @return the {@link SQLConversionVisitor}
+     */
+    public SQLConversionVisitor getSQLConversionVisitor() {
+    	return new SQLConversionVisitor(this);
+    }
+    
 
 	@Override
     public List<?> translate(LanguageObject obj, ExecutionContext context) {
 
-	if (obj instanceof NamedTable) {
-
-	    NamedTable nt = (NamedTable) obj;
-	    List<String> ntlist = new ArrayList<String>(1);
-
-	    ntlist.add("[" + nt.getMetadataObject().getNameInSource() + "]"); //$NON-NLS-1$ //$NON-NLS-2$
-	    return ntlist;
-	}
-
-	if (obj instanceof ColumnReference) {
-	    ColumnReference elem = (ColumnReference) obj;
-	    List<String> ntlist = new ArrayList<String>(1);
-	    ntlist.add("[" + elem.getMetadataObject().getNameInSource() + "]"); //$NON-NLS-1$ //$NON-NLS-2$
-	    return ntlist;
-
-	}
-
-	return super.translate(obj, context);
-    }
+		if (obj instanceof NamedTable) {	
+		    NamedTable nt = (NamedTable) obj;
+		    List<String> ntlist = new ArrayList<String>(1);
+		    ntlist.add("[" + trimTics(nt.getMetadataObject().getNameInSource()) + "]"); //$NON-NLS-1$ //$NON-NLS-2$
+		    return ntlist;
+		}
+	
+		if (obj instanceof ColumnReference) {
+		    ColumnReference elem = (ColumnReference) obj;
+		    List<String> ntlist = new ArrayList<String>(1);
+		    ntlist.add("[" + trimTics(elem.getMetadataObject().getNameInSource()) + "]"); //$NON-NLS-1$ //$NON-NLS-2$
+		    return ntlist;	
+		}
+	
+		return super.translate(obj, context);
+    }
+	
+	/**
+	 * Because the Teiid Designer Import from JDBC adds tic's to a nameInSource that has special characters,
+	 * they have to be removed when building the sql syntax
+	 * @param name
+	 * @return
+	 */
+	private String trimTics(String name) {
+		String rtn = name;
+		if (rtn.startsWith("'")) {
+			rtn = rtn.substring(1);	
+		}
+		
+		if (rtn.endsWith("'")) {
+			rtn = rtn.substring(0, rtn.indexOf("'"));
+		}
+		return rtn;
+	}
     
     @Override
     public String translateLiteralBoolean(Boolean booleanValue) {
@@ -182,13 +208,13 @@
     
     @Override
     public List<String> getSupportedFunctions() {
-	List<String> supportedFunctions = new ArrayList<String>();
-	supportedFunctions.addAll(super.getSupportedFunctions());
-	supportedFunctions.add("PATH"); //$NON-NLS-1$
-	supportedFunctions.add("NAME"); //$NON-NLS-1$
-	supportedFunctions.add("ISCHILDNODE"); //$NON-NLS-1$
-	
-	return supportedFunctions;
+		List<String> supportedFunctions = new ArrayList<String>();
+		supportedFunctions.addAll(super.getSupportedFunctions());
+		supportedFunctions.add("PATH"); //$NON-NLS-1$
+		supportedFunctions.add("NAME"); //$NON-NLS-1$
+		supportedFunctions.add("ISCHILDNODE"); //$NON-NLS-1$
+		
+		return supportedFunctions;
 
     }
         

Modified: branches/7.1.x/connectors/translator-jdbc/src/test/java/org/teiid/translator/jdbc/modeshape/TestModeShapeSqlTranslator.java
===================================================================
--- branches/7.1.x/connectors/translator-jdbc/src/test/java/org/teiid/translator/jdbc/modeshape/TestModeShapeSqlTranslator.java	2010-09-13 19:14:47 UTC (rev 2562)
+++ branches/7.1.x/connectors/translator-jdbc/src/test/java/org/teiid/translator/jdbc/modeshape/TestModeShapeSqlTranslator.java	2010-09-13 21:30:51 UTC (rev 2563)
@@ -64,61 +64,81 @@
 		tc.translateCommand(obj);
 		assertEquals("Did not get correct sql", expectedOutput, tc.getSql()); //$NON-NLS-1$
 	}
-
+	
 	@Test
-	public void testSimpleSelect() throws Exception {
-		String input = "select Model from Car"; //$NON-NLS-1$
-		String output = "SELECT [car:Model] FROM [car:Car]"; //$NON-NLS-1$
+	public void testSelectAllFromBase() throws Exception {
+		String input = "select * from nt_base"; //$NON-NLS-1$
+		String output = "SELECT [jcr:mixinTypes] FROM [nt:base]"; //$NON-NLS-1$
 
 		// FakeTranslationFactory.getInstance().getExampleTranslationUtility(),
 		helpTestVisitor(new TranslationUtility(MODESHAPE_VDB), input, output);
 
 	}
-
+	
 	@Test
-	public void testWhereClause() throws Exception {
+	public void testSelectColumnFromBase() throws Exception {
+		String input = "select jcr_mixinTypes from nt_base"; //$NON-NLS-1$
+		String output = "SELECT [jcr:mixinTypes] FROM [nt:base]"; //$NON-NLS-1$
 
-		String input = "select Model from Car WHERE Make = 'Honda'"; //$NON-NLS-1$
-		String output = "SELECT [car:Model] FROM [car:Car] WHERE [car:Make] = 'Honda'"; //$NON-NLS-1$
-
 		// FakeTranslationFactory.getInstance().getExampleTranslationUtility(),
 		helpTestVisitor(new TranslationUtility(MODESHAPE_VDB), input, output);
 
-	}
+	}	
+//
+//	@Test
+//	public void testSimpleSelect() throws Exception {
+//		String input = "select Model from Car"; //$NON-NLS-1$
+//		String output = "SELECT [car:Model] FROM [car:Car]"; //$NON-NLS-1$
+//
+//		// FakeTranslationFactory.getInstance().getExampleTranslationUtility(),
+//		helpTestVisitor(new TranslationUtility(MODESHAPE_VDB), input, output);
+//
+//	}
+//
+//	@Test
+//	public void testWhereClause() throws Exception {
+//
+//		String input = "select Model from Car WHERE Make = 'Honda'"; //$NON-NLS-1$
+//		String output = "SELECT [car:Model] FROM [car:Car] WHERE [car:Make] = 'Honda'"; //$NON-NLS-1$
+//
+//		// FakeTranslationFactory.getInstance().getExampleTranslationUtility(),
+//		helpTestVisitor(new TranslationUtility(MODESHAPE_VDB), input, output);
+//
+//	}
+//
+//	@Test
+//	public void testOrderBy() throws Exception {
+//
+//		String input = "select Model from Car ORDER BY Make"; //$NON-NLS-1$
+//		String output = "SELECT [car:Model] FROM [car:Car] ORDER BY [car:Make]"; //$NON-NLS-1$
+//
+//		// FakeTranslationFactory.getInstance().getExampleTranslationUtility(),
+//		helpTestVisitor(new TranslationUtility(MODESHAPE_VDB), input, output);
+//
+//	}
+//
+//	@Ignore
+//	@Test
+//	public void testUsingAlias() throws Exception {
+//
+//		String input = "select c.Model from Car As c"; //$NON-NLS-1$
+//		String output = "SELECT c.[car:Model] FROM [car:Car] As c"; //$NON-NLS-1$
+//
+//		// FakeTranslationFactory.getInstance().getExampleTranslationUtility(),
+//		helpTestVisitor(new TranslationUtility(MODESHAPE_VDB), input, output);
+//
+//	}
+//
+//	@Ignore
+//	@Test
+//	public void testUsingNameFunction() throws Exception {
+//
+//		String input = "select Model from Car as car WHERE PATH('car') LIKE '%/Hybrid/%'"; //$NON-NLS-1$
+//		String output = "SELECT [car:Model] FROM [car:Car] WHERE PATH(car:Car) LIKE '%/Hybrid/%'"; //$NON-NLS-1$
+//
+//		// FakeTranslationFactory.getInstance().getExampleTranslationUtility(),
+//		helpTestVisitor(new TranslationUtility(MODESHAPE_VDB), input, output);
+//
+//	}
 
-	@Test
-	public void testOrderBy() throws Exception {
-
-		String input = "select Model from Car ORDER BY Make"; //$NON-NLS-1$
-		String output = "SELECT [car:Model] FROM [car:Car] ORDER BY [car:Make]"; //$NON-NLS-1$
-
-		// FakeTranslationFactory.getInstance().getExampleTranslationUtility(),
-		helpTestVisitor(new TranslationUtility(MODESHAPE_VDB), input, output);
-
-	}
-
-	@Ignore
-	@Test
-	public void testUsingAlias() throws Exception {
-
-		String input = "select c.Model from Car As c"; //$NON-NLS-1$
-		String output = "SELECT c.[car:Model] FROM [car:Car] As c"; //$NON-NLS-1$
-
-		// FakeTranslationFactory.getInstance().getExampleTranslationUtility(),
-		helpTestVisitor(new TranslationUtility(MODESHAPE_VDB), input, output);
-
-	}
-
-	@Ignore
-	@Test
-	public void testUsingNameFunction() throws Exception {
-
-		String input = "select Model from Car as car WHERE PATH('car') LIKE '%/Hybrid/%'"; //$NON-NLS-1$
-		String output = "SELECT [car:Model] FROM [car:Car] WHERE PATH(car:Car) LIKE '%/Hybrid/%'"; //$NON-NLS-1$
-
-		// FakeTranslationFactory.getInstance().getExampleTranslationUtility(),
-		helpTestVisitor(new TranslationUtility(MODESHAPE_VDB), input, output);
-
-	}
-
 }

Modified: branches/7.1.x/connectors/translator-jdbc/src/test/resources/ModeShape.vdb
===================================================================
(Binary files differ)



More information about the teiid-commits mailing list