Author: vhalbert(a)redhat.com
Date: 2010-06-11 14:31:39 -0400 (Fri, 11 Jun 2010)
New Revision: 2225
Added:
trunk/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/modeshape/
trunk/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/modeshape/ModeShapeExecutionFactory.java
trunk/connectors/translator-jdbc/src/test/java/org/teiid/translator/jdbc/modeshape/
trunk/connectors/translator-jdbc/src/test/java/org/teiid/translator/jdbc/modeshape/TestModeShapeSqlTranslator.java
trunk/connectors/translator-jdbc/src/test/resources/ModeShape.vdb
Log:
Teiid-1106 : Adding the ModeShape translator, which uses the base JDBC translator. This
includes testcases validating support for simple queries. More semantics will be added
once the initial integration has been validated.
Added:
trunk/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/modeshape/ModeShapeExecutionFactory.java
===================================================================
---
trunk/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/modeshape/ModeShapeExecutionFactory.java
(rev 0)
+++
trunk/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/modeshape/ModeShapeExecutionFactory.java 2010-06-11
18:31:39 UTC (rev 2225)
@@ -0,0 +1,234 @@
+/*
+ * 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.translator.jdbc.modeshape;
+
+import java.sql.Date;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.teiid.language.ColumnReference;
+import org.teiid.language.Command;
+import org.teiid.language.Expression;
+import org.teiid.language.Function;
+import org.teiid.language.LanguageObject;
+import org.teiid.language.Literal;
+import org.teiid.language.NamedTable;
+import org.teiid.metadata.RuntimeMetadata;
+import org.teiid.translator.ExecutionContext;
+import org.teiid.translator.Translator;
+import org.teiid.translator.TranslatorException;
+import org.teiid.translator.TranslatorProperty;
+import org.teiid.translator.UpdateExecution;
+import org.teiid.translator.jdbc.ConvertModifier;
+import org.teiid.translator.jdbc.FunctionModifier;
+import org.teiid.translator.jdbc.JDBCExecutionFactory;
+import org.teiid.translator.jdbc.SQLConversionVisitor;
+
+
+
+/**
+ * Translator class for accessing the ModeShape JCR repository.
+ */
+@Translator(name="modeshape")
+public class ModeShapeExecutionFactory extends JDBCExecutionFactory {
+
+ private String version = "2.0";
+
+ @Override
+ public void start() throws TranslatorException {
+ super.start();
+
+ registerFunctionModifier("PATH", new FunctionModifier() {
+
+ @Override
+ public List<?> translate(Function function) {
+ List<Object> objs = new ArrayList<Object>();
+
+ List<Expression> parms = function.getParameters();
+
+ for (Expression s : parms)
+ {
+ String v = s.toString();
+ v.replace('\'', ' ');
+ objs.add(v);
+ }
+
+ return objs;
+ }
+ } );
+
+
+
+
+ //add in type conversion
+ ConvertModifier convertModifier = new ConvertModifier();
+
+
+ convertModifier.addTypeMapping("boolean", FunctionModifier.BOOLEAN);
//$NON-NLS-1$
+ convertModifier.addTypeMapping("smallint", FunctionModifier.BYTE,
FunctionModifier.SHORT); //$NON-NLS-1$
+ convertModifier.addTypeMapping("integer", FunctionModifier.INTEGER);
//$NON-NLS-1$
+ convertModifier.addTypeMapping("bigint", FunctionModifier.LONG);
//$NON-NLS-1$
+ convertModifier.addTypeMapping("real", FunctionModifier.FLOAT);
//$NON-NLS-1$
+ convertModifier.addTypeMapping("float8", FunctionModifier.DOUBLE);
//$NON-NLS-1$
+ convertModifier.addTypeMapping("numeric(38)",
FunctionModifier.BIGINTEGER); //$NON-NLS-1$
+ convertModifier.addTypeMapping("decimal", FunctionModifier.BIGDECIMAL);
//$NON-NLS-1$
+ convertModifier.addTypeMapping("char(1)", FunctionModifier.CHAR);
//$NON-NLS-1$
+ convertModifier.addTypeMapping("varchar(4000)", FunctionModifier.STRING);
//$NON-NLS-1$
+ convertModifier.addTypeMapping("date", FunctionModifier.DATE);
//$NON-NLS-1$
+ convertModifier.addTypeMapping("time", FunctionModifier.TIME);
//$NON-NLS-1$
+ convertModifier.addTypeMapping("timestamp", FunctionModifier.TIMESTAMP);
//$NON-NLS-1$
+ convertModifier.addConvert(FunctionModifier.TIME, FunctionModifier.TIMESTAMP, new
FunctionModifier() {
+ @Override
+ public List<?> translate(Function function) {
+ return Arrays.asList(function.getParameters().get(0), " + TIMESTAMP
'1970-01-01'"); //$NON-NLS-1$
+ }
+ });
+ convertModifier.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.TIME, new
FunctionModifier() {
+ @Override
+ public List<?> translate(Function function) {
+ return Arrays.asList("cast(date_trunc('second', ",
function.getParameters().get(0), ") AS time)"); //$NON-NLS-1$ //$NON-NLS-2$
+ }
+ });
+ convertModifier.addConvert(FunctionModifier.DATE, FunctionModifier.STRING, new
ConvertModifier.FormatModifier("to_char", "YYYY-MM-DD"));
//$NON-NLS-1$ //$NON-NLS-2$
+ convertModifier.addConvert(FunctionModifier.TIME, FunctionModifier.STRING, new
ConvertModifier.FormatModifier("to_char", "HH24:MI:SS"));
//$NON-NLS-1$ //$NON-NLS-2$
+ convertModifier.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.STRING, new
ConvertModifier.FormatModifier("to_char", "YYYY-MM-DD
HH24:MI:SS.UF")); //$NON-NLS-1$ //$NON-NLS-2$
+ convertModifier.addConvert(FunctionModifier.BOOLEAN, FunctionModifier.STRING, new
FunctionModifier() {
+ @Override
+ public List<?> translate(Function function) {
+ Expression stringValue = function.getParameters().get(0);
+ return Arrays.asList("CASE WHEN ", stringValue, " THEN 'true'
WHEN not(", stringValue, ") THEN 'false' END"); //$NON-NLS-1$
//$NON-NLS-2$ //$NON-NLS-3$
+ }
+ });
+ convertModifier.addSourceConversion(new FunctionModifier() {
+ @Override
+ public List<?> translate(Function function) {
+ ((Literal)function.getParameters().get(1)).setValue("integer");
//$NON-NLS-1$
+ return null;
+ }
+ }, FunctionModifier.BOOLEAN);
+ }
+
+
+
+ @Override
+ public SQLConversionVisitor getSQLConversionVisitor() {
+ return new ModeShapeSQLConversionVisitor(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() +
"]");
+ return ntlist;
+ }
+
+ if (obj instanceof ColumnReference) {
+ ColumnReference elem = (ColumnReference) obj;
+ List<String> ntlist = new ArrayList<String>(1);
+ ntlist.add("[" + elem.getMetadataObject().getNameInSource() +
"]");
+ return ntlist;
+
+ }
+
+ return super.translate(obj, context);
+ }
+
+ @Override
+ public String translateLiteralBoolean(Boolean booleanValue) {
+ if(booleanValue.booleanValue()) {
+ return "TRUE"; //$NON-NLS-1$
+ }
+ return "FALSE"; //$NON-NLS-1$
+ }
+
+
+
+ @Override
+ public List<?> translateCommand(Command command, ExecutionContext context) {
+ return super.translateCommand(command, context);
+ }
+
+
+
+ @Override
+ public String translateLiteralDate(Date dateValue) {
+ return "DATE '" + formatDateValue(dateValue) + "'";
//$NON-NLS-1$//$NON-NLS-2$
+ }
+
+ @Override
+ public String translateLiteralTime(Time timeValue) {
+ return "TIME '" + formatDateValue(timeValue) + "'";
//$NON-NLS-1$//$NON-NLS-2$
+ }
+
+ @Override
+ public String translateLiteralTimestamp(Timestamp timestampValue) {
+ return "TIMESTAMP '" + formatDateValue(timestampValue) +
"'"; //$NON-NLS-1$//$NON-NLS-2$
+ }
+
+ @Override
+ public int getTimestampNanoPrecision() {
+ return 6;
+ }
+
+ @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;
+
+ }
+
+ @TranslatorProperty(description= "ModeShape Repository Version")
+ public String getDatabaseVersion() {
+ return this.version;
+ }
+
+ public void setDatabaseVersion(String version) {
+ this.version = version;
+ }
+
+ @Override
+ public boolean useBindVariables() {
+ return false;
+ }
+
+ @Override
+ public UpdateExecution createUpdateExecution(Command command, ExecutionContext
executionContext, RuntimeMetadata metadata, Object connectionFactory) throws
TranslatorException {
+ throw new TranslatorException("Unsupported Execution");//$NON-NLS-1$
+ }
+
+}
Property changes on:
trunk/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/modeshape/ModeShapeExecutionFactory.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Added:
trunk/connectors/translator-jdbc/src/test/java/org/teiid/translator/jdbc/modeshape/TestModeShapeSqlTranslator.java
===================================================================
---
trunk/connectors/translator-jdbc/src/test/java/org/teiid/translator/jdbc/modeshape/TestModeShapeSqlTranslator.java
(rev 0)
+++
trunk/connectors/translator-jdbc/src/test/java/org/teiid/translator/jdbc/modeshape/TestModeShapeSqlTranslator.java 2010-06-11
18:31:39 UTC (rev 2225)
@@ -0,0 +1,127 @@
+/*
+ * 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.translator.jdbc.modeshape;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.teiid.core.util.UnitTestUtil;
+import org.teiid.language.Command;
+import org.teiid.translator.ExecutionContext;
+import org.teiid.translator.TranslatorException;
+import org.teiid.translator.jdbc.TranslatedCommand;
+
+import com.metamatrix.cdk.api.TranslationUtility;
+
+/**
+ */
+public class TestModeShapeSqlTranslator {
+
+ private static ModeShapeExecutionFactory TRANSLATOR;
+
+ private static String MODESHAPE_VDB = (UnitTestUtil.getTestDataPath() != null ?
UnitTestUtil.getTestDataPath() : "src/test/resources") +
"/ModeShape.vdb";
+
+ @BeforeClass
+ public static void setUp() throws TranslatorException {
+ TRANSLATOR = new ModeShapeExecutionFactory();
+ TRANSLATOR.start();
+
+ }
+
+
+ public void helpTestVisitor(TranslationUtility util, String input, String
expectedOutput) throws TranslatorException {
+ // Convert from sql to objects
+ Command obj = util.parseCommand(input);
+
+ TranslatedCommand tc = new
TranslatedCommand(Mockito.mock(ExecutionContext.class), TRANSLATOR);
+ tc.translateCommand(obj);
+
+ System.out.println("Input: "+ tc.getSql() + " Expected: " +
expectedOutput);
+ 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$
+
+ // 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);
+
+
+ }
+
+
+
+}
Property changes on:
trunk/connectors/translator-jdbc/src/test/java/org/teiid/translator/jdbc/modeshape/TestModeShapeSqlTranslator.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Added: trunk/connectors/translator-jdbc/src/test/resources/ModeShape.vdb
===================================================================
(Binary files differ)
Property changes on: trunk/connectors/translator-jdbc/src/test/resources/ModeShape.vdb
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream