Author: shawkins
Date: 2012-03-08 17:48:30 -0500 (Thu, 08 Mar 2012)
New Revision: 3924
Modified:
branches/7.7.x/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/oracle/OracleExecutionFactory.java
branches/7.7.x/connectors/translator-jdbc/src/test/java/org/teiid/translator/jdbc/oracle/TestOracleTranslator.java
Log:
TEIID-1952 adding detection of char types
Modified:
branches/7.7.x/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/oracle/OracleExecutionFactory.java
===================================================================
---
branches/7.7.x/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/oracle/OracleExecutionFactory.java 2012-03-08
20:05:47 UTC (rev 3923)
+++
branches/7.7.x/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/oracle/OracleExecutionFactory.java 2012-03-08
22:48:30 UTC (rev 3924)
@@ -87,6 +87,12 @@
*/
static final class RefCursorType {}
static int CURSOR_TYPE = -10;
+
+ /*
+ * handling for char bindings
+ */
+ static final class FixedCharType {}
+ static int FIXED_CHAR_TYPE = 999;
private boolean oracleSuppliedDriver = true;
@@ -434,6 +440,10 @@
stmt.setNull(i, Types.LONGVARBINARY);
return;
}
+ if (paramType == FixedCharType.class) {
+ stmt.setObject(i, param, FIXED_CHAR_TYPE);
+ return;
+ }
super.bindValue(stmt, param, paramType, i);
}
@@ -448,6 +458,45 @@
}
@Override
+ public SQLConversionVisitor getSQLConversionVisitor() {
+ if (!oracleSuppliedDriver) {
+ return super.getSQLConversionVisitor();
+ }
+ return new SQLConversionVisitor(this) {
+
+ @Override
+ public void visit(Comparison obj) {
+ if (isChar(obj.getLeftExpression()) && obj.getRightExpression() instanceof
Literal) {
+ Literal l = (Literal)obj.getRightExpression();
+ l.setType(FixedCharType.class);
+ }
+ super.visit(obj);
+ }
+
+ private boolean isChar(Expression obj) {
+ if (!(obj instanceof ColumnReference)) {
+ return false;
+ }
+ ColumnReference cr = (ColumnReference)obj;
+ return cr.getType() == TypeFacility.RUNTIME_TYPES.STRING &&
cr.getMetadataObject() != null &&
"CHAR".equalsIgnoreCase(cr.getMetadataObject().getNativeType()); //$NON-NLS-1$
+ }
+
+ public void visit(In obj) {
+ if (isChar(obj.getLeftExpression())) {
+ for (Expression exp : obj.getRightExpressions()) {
+ if (exp instanceof Literal) {
+ Literal l = (Literal)exp;
+ l.setType(FixedCharType.class);
+ }
+ }
+ }
+ super.visit(obj);
+ }
+
+ };
+ }
+
+ @Override
public List<String> getSupportedFunctions() {
List<String> supportedFunctions = new ArrayList<String>();
supportedFunctions.addAll(super.getSupportedFunctions());
Modified:
branches/7.7.x/connectors/translator-jdbc/src/test/java/org/teiid/translator/jdbc/oracle/TestOracleTranslator.java
===================================================================
---
branches/7.7.x/connectors/translator-jdbc/src/test/java/org/teiid/translator/jdbc/oracle/TestOracleTranslator.java 2012-03-08
20:05:47 UTC (rev 3923)
+++
branches/7.7.x/connectors/translator-jdbc/src/test/java/org/teiid/translator/jdbc/oracle/TestOracleTranslator.java 2012-03-08
22:48:30 UTC (rev 3924)
@@ -26,6 +26,7 @@
import java.sql.CallableStatement;
import java.sql.Connection;
+import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Types;
import java.util.Arrays;
@@ -41,6 +42,9 @@
import org.teiid.dqp.internal.datamgr.ExecutionContextImpl;
import org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl;
import org.teiid.language.Command;
+import org.teiid.language.Comparison;
+import org.teiid.language.Literal;
+import org.teiid.language.Select;
import org.teiid.metadata.Column;
import org.teiid.metadata.ColumnSet;
import org.teiid.metadata.MetadataStore;
@@ -56,6 +60,7 @@
import org.teiid.translator.ExecutionContext;
import org.teiid.translator.TranslatorException;
import org.teiid.translator.jdbc.JDBCProcedureExecution;
+import org.teiid.translator.jdbc.JDBCQueryExecution;
import org.teiid.translator.jdbc.SQLConversionVisitor;
import org.teiid.translator.jdbc.TranslatedCommand;
import org.teiid.translator.jdbc.TranslationHelper;
@@ -757,17 +762,20 @@
"DoubleNum", //$NON-NLS-1$
"ID", //$NON-NLS-1$
"timestampvalue", //$NON-NLS-1$
+ "description"
};
String[] elemTypes = new String[] {
DataTypeManager.DefaultDataTypes.DOUBLE,
DataTypeManager.DefaultDataTypes.INTEGER,
DataTypeManager.DefaultDataTypes.TIMESTAMP,
+ DataTypeManager.DefaultDataTypes.STRING,
};
RealMetadataFactory.createElements(x, elemNames, elemTypes);
List<Column> cols = RealMetadataFactory.createElements(table, elemNames,
elemTypes);
cols.get(1).setAutoIncremented(true);
cols.get(1).setNameInSource("ID:SEQUENCE=MYSEQUENCE.nextVal");
//$NON-NLS-1$
cols.get(2).setNativeType("date"); //$NON-NLS-1$
+ cols.get(3).setNativeType("CHAR");
RealMetadataFactory.createElements(dual, new String[] {"something"},
new String[] {DataTypeManager.DefaultDataTypes.STRING}); //$NON-NLS-1$
ProcedureParameter in1 = RealMetadataFactory.createParameter("in1",
SPParameter.IN, DataTypeManager.DefaultDataTypes.INTEGER); //$NON-NLS-1$
@@ -902,6 +910,20 @@
Mockito.verify(cs, Mockito.times(1)).setObject(1, 2, Types.INTEGER);
}
+ @Test public void testCharType() throws Exception {
+ CommandBuilder commandBuilder = new CommandBuilder(getOracleSpecificMetadata());
+ Command command = commandBuilder.getCommand("select id from smalla where
description = 'a'");
+
((Literal)((Comparison)((Select)command).getWhere()).getRightExpression()).setBindEligible(true);
+ Connection connection = Mockito.mock(Connection.class);
+ PreparedStatement ps = Mockito.mock(PreparedStatement.class);
+ Mockito.stub(connection.prepareStatement("SELECT SmallishA.ID FROM SmallishA WHERE
SmallishA.description = ?")).toReturn(ps); //$NON-NLS-1$
+ OracleExecutionFactory ef = new OracleExecutionFactory();
+
+ JDBCQueryExecution e = new JDBCQueryExecution(command, connection,
Mockito.mock(ExecutionContext.class), ef);
+ e.execute();
+ Mockito.verify(ps, Mockito.times(1)).setObject(1, "a", 999);
+ }
+
@Test public void testParseFormat() throws Exception {
String input = "select parsetimestamp(smalla.timestampvalue,
'yyyy.MM.dd'), formattimestamp(smalla.timestampvalue, 'yy.MM.dd') from
bqt1.smalla"; //$NON-NLS-1$
String output = "SELECT TO_TIMESTAMP(to_char(cast(g_0.TimestampValue AS
timestamp), 'YYYY-MM-DD HH24:MI:SS.FF'), YYYY.MM.DD), TO_CHAR(g_0.TimestampValue,
YY.MM.DD) FROM SmallA g_0"; //$NON-NLS-1$