Package org.teiid.translator.jdbc

Examples of org.teiid.translator.jdbc.FunctionModifier


public class MySQL5ExecutionFactory extends MySQLExecutionFactory {
 
  @Override
    public void start() throws TranslatorException {
        super.start();
        registerFunctionModifier(SourceSystemFunctions.CHAR, new FunctionModifier() {
     
      @Override
      public List<?> translate(Function function) {
        return Arrays.asList("char(", function.getParameters().get(0), " USING ASCII)"); //$NON-NLS-1$ //$NON-NLS-2$
      }
View Full Code Here


      convertModifier.addConvert(FunctionModifier.STRING, FunctionModifier.TIME, new ConvertModifier.FormatModifier("TIME")); //$NON-NLS-1$
      convertModifier.addConvert(FunctionModifier.STRING, FunctionModifier.TIMESTAMP, new ConvertModifier.FormatModifier("TIMESTAMP")); //$NON-NLS-1$
      convertModifier.addConvert(FunctionModifier.DATE, FunctionModifier.STRING, new ConvertModifier.FormatModifier("date_format", "%Y-%m-%d")); //$NON-NLS-1$ //$NON-NLS-2$
      convertModifier.addConvert(FunctionModifier.TIME, FunctionModifier.STRING, new ConvertModifier.FormatModifier("date_format", "%H:%i:%S")); //$NON-NLS-1$ //$NON-NLS-2$
      convertModifier.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.STRING, new ConvertModifier.FormatModifier("date_format", "%Y-%m-%d %H:%i:%S.%f")); //$NON-NLS-1$ //$NON-NLS-2$
      convertModifier.addTypeConversion(new FunctionModifier() {
      @Override
      public List<?> translate(Function function) {
        return Arrays.asList(function.getParameters().get(0), " + 0.0"); //$NON-NLS-1$
      }
    }, FunctionModifier.BIGDECIMAL, FunctionModifier.BIGINTEGER, FunctionModifier.FLOAT, FunctionModifier.DOUBLE);
View Full Code Here

      convertModifier.addTypeMapping("numeric(38, 0)", FunctionModifier.BIGINTEGER); //$NON-NLS-1$
      convertModifier.addTypeMapping("numeric(38, 19)", FunctionModifier.BIGDECIMAL); //$NON-NLS-1$
      convertModifier.addTypeMapping("char(1)", FunctionModifier.CHAR); //$NON-NLS-1$
      convertModifier.addTypeMapping("varchar(40)", FunctionModifier.STRING); //$NON-NLS-1$
      convertModifier.addTypeMapping("datetime", FunctionModifier.DATE, FunctionModifier.TIME, FunctionModifier.TIMESTAMP); //$NON-NLS-1$
      convertModifier.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.TIME, new FunctionModifier() {
      @Override
      public List<?> translate(Function function) {
        List<Object> result = new ArrayList<Object>();
        result.add("cast("); //$NON-NLS-1$
        boolean needsEnd = false;
        if (!nullPlusNonNullIsNull() && !ConcatFunctionModifier.isNotNull(function.getParameters().get(0))) {
          result.add("CASE WHEN "); //$NON-NLS-1$
          result.add(function.getParameters().get(0));
          result.add(" IS NOT NULL THEN "); //$NON-NLS-1$
          needsEnd = true;
        }
        result.add("'1970-01-01 ' + "); //$NON-NLS-1$
        result.addAll(convertTimeToString(function));
        if (needsEnd) {
          result.add(" END"); //$NON-NLS-1$
        }
        result.add(" AS datetime)"); //$NON-NLS-1$
        return result;
      }
    });
      convertModifier.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.DATE, new FunctionModifier() {
      @Override
      public List<?> translate(Function function) {
        List<Object> result = new ArrayList<Object>();
        result.add("cast("); //$NON-NLS-1$
        result.addAll(convertDateToString(function));
        result.add(" AS datetime)"); //$NON-NLS-1$
        return result;
      }
    });
      convertModifier.addConvert(FunctionModifier.TIME, FunctionModifier.STRING, new FunctionModifier() {
      @Override
      public List<?> translate(Function function) {
        return convertTimeToString(function);
      }
    });
      convertModifier.addConvert(FunctionModifier.DATE, FunctionModifier.STRING, new FunctionModifier() {
      @Override
      public List<?> translate(Function function) {
        return convertDateToString(function);
      }
    });
      convertModifier.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.STRING, new FunctionModifier() {
      @Override
      public List<?> translate(Function function) {
        return convertTimestampToString(function);
      }
    });
View Full Code Here

        registerFunctionModifier(SourceSystemFunctions.TAN, new EscapeSyntaxModifier());
        registerFunctionModifier(SourceSystemFunctions.TIMESTAMPADD, new EscapeSyntaxModifier());  
        registerFunctionModifier(SourceSystemFunctions.TIMESTAMPDIFF, new EscapeSyntaxModifier());   
        registerFunctionModifier(SourceSystemFunctions.TRUNCATE, new EscapeSyntaxModifier());  
        registerFunctionModifier(SourceSystemFunctions.WEEK, new EscapeSyntaxModifier());
        registerFunctionModifier(SourceSystemFunctions.DIVIDE_OP, new FunctionModifier() {
     
      @Override
      public List<?> translate(Function function) {
        if (function.getType() == TypeFacility.RUNTIME_TYPES.INTEGER || function.getType() == TypeFacility.RUNTIME_TYPES.LONG) {
          Function result = ConvertModifier.createConvertFunction(getLanguageFactory(), function, TypeFacility.getDataTypeName(function.getType()));
View Full Code Here

    registerFunctionModifier(SourceSystemFunctions.MINUTE, new ExtractModifier("MINUTE")); //$NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.SECOND, new ExtractModifier("SECOND")); //$NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.LOCATE, new LocateModifier(this.convert));
        registerFunctionModifier(SourceSystemFunctions.LEFT, new LeftOrRightFunctionModifier(getLanguageFactory(), this.convert));
        registerFunctionModifier(SourceSystemFunctions.RIGHT, new LeftOrRightFunctionModifier(getLanguageFactory(), this.convert));
        registerFunctionModifier(SourceSystemFunctions.COT, new FunctionModifier() {
      @Override
      public List<?> translate(Function function) {
        function.setName(SourceSystemFunctions.TAN);
        return Arrays.asList(getLanguageFactory().createFunction(SourceSystemFunctions.DIVIDE_OP, new Expression[] {new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER), function}, TypeFacility.RUNTIME_TYPES.DOUBLE));
      }
    });       
        registerFunctionModifier(SourceSystemFunctions.LTRIM, new FunctionModifier() {
      @Override
      public List<?> translate(Function function) {
        return Arrays.asList("TRIM(LEADING FROM ", expressionToString(function.getParameters().get(0), convert), ")"); //$NON-NLS-1$ //$NON-NLS-2$
      }
    });
        registerFunctionModifier(SourceSystemFunctions.RTRIM, new FunctionModifier() {
      @Override
      public List<?> translate(Function function) {
        return Arrays.asList("TRIM(TRAILING FROM ", expressionToString(function.getParameters().get(0), convert), ")"); //$NON-NLS-1$ //$NON-NLS-2$
      }
    });
        registerFunctionModifier(SourceSystemFunctions.MOD, new FunctionModifier() {
      @Override
      public List<?> translate(Function function) {
        return Arrays.asList(function.getParameters().get(0), " MOD ", function.getParameters().get(1)); //$NON-NLS-1$
      }
    });       
View Full Code Here

        //specific to 8.2 client or later
        registerFunctionModifier(SourceSystemFunctions.TIMESTAMPADD, new EscapeSyntaxModifier());
        registerFunctionModifier(SourceSystemFunctions.TIMESTAMPDIFF, new EscapeSyntaxModifier());
       
        registerFunctionModifier(SourceSystemFunctions.ARRAY_GET, new FunctionModifier() {
     
      @Override
      public List<?> translate(Function function) {
        return Arrays.asList(function.getParameters().get(0), '[', function.getParameters().get(1), ']');
      }
    });
        registerFunctionModifier(SourceSystemFunctions.ARRAY_LENGTH, new FunctionModifier() {
     
      @Override
      public List<?> translate(Function function) {
        if (function.getParameters().size() == 1) {
          function.getParameters().add(new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER));
        }
        return null;
      }
    });
               
        //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.US")); //$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;
      }
View Full Code Here

      convertModifier.addTypeMapping("numeric(31,0)", FunctionModifier.BIGINTEGER); //$NON-NLS-1$
      convertModifier.addTypeMapping("numeric(31,12)", FunctionModifier.BIGDECIMAL); //$NON-NLS-1$
      convertModifier.addTypeMapping("char(1)", FunctionModifier.CHAR); //$NON-NLS-1$
      convertModifier.addTypeMapping("blob", FunctionModifier.BLOB, FunctionModifier.OBJECT); //$NON-NLS-1$
      convertModifier.addTypeMapping("clob", FunctionModifier.CLOB, FunctionModifier.XML); //$NON-NLS-1$
      convertModifier.addConvert(FunctionModifier.TIME, FunctionModifier.TIMESTAMP, new FunctionModifier() {
      @Override
      public List<?> translate(Function function) {
        return Arrays.asList("timestamp('1970-01-01', ", function.getParameters().get(0), ")"); //$NON-NLS-1$ //$NON-NLS-2$
      }
    });
      convertModifier.addConvert(FunctionModifier.DATE, FunctionModifier.TIMESTAMP, new FunctionModifier() {
      @Override
      public List<?> translate(Function function) {
        return Arrays.asList("timestamp(",function.getParameters().get(0), ", '00:00:00')"); //$NON-NLS-1$ //$NON-NLS-2$
      }
    });
      //the next convert is not strictly necessary for db2, but it also works for derby
      convertModifier.addConvert(FunctionModifier.STRING, FunctionModifier.FLOAT, new FunctionModifier() {
      @Override
      public List<?> translate(Function function) {
        return Arrays.asList("cast(double(", function.getParameters().get(0), ") as real)"); //$NON-NLS-1$ //$NON-NLS-2$
      }
    });
View Full Code Here

        registerFunctionModifier(SourceSystemFunctions.LOCATE, new LocateFunctionModifier(getLanguageFactory(), "INSTR", true)); //$NON-NLS-1$
        registerFunctionModifier(SourceSystemFunctions.SUBSTRING, new AliasModifier("substr"));//$NON-NLS-1$
        registerFunctionModifier(SourceSystemFunctions.LEFT, new LeftOrRightFunctionModifier(getLanguageFactory()));
        registerFunctionModifier(SourceSystemFunctions.RIGHT, new LeftOrRightFunctionModifier(getLanguageFactory()));
        registerFunctionModifier(SourceSystemFunctions.CONCAT, new ConcatFunctionModifier(getLanguageFactory()));
        registerFunctionModifier(SourceSystemFunctions.COT, new FunctionModifier() {
      @Override
      public List<?> translate(Function function) {
        function.setName(SourceSystemFunctions.TAN);
        return Arrays.asList(getLanguageFactory().createFunction(SourceSystemFunctions.DIVIDE_OP, new Expression[] {new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER), function}, TypeFacility.RUNTIME_TYPES.DOUBLE));
      }
    });
       
        //spatial functions
        registerFunctionModifier(OracleSpatialFunctions.RELATE, new OracleSpatialFunctionModifier());
        registerFunctionModifier(OracleSpatialFunctions.NEAREST_NEIGHBOR, new OracleSpatialFunctionModifier());
        registerFunctionModifier(OracleSpatialFunctions.FILTER, new OracleSpatialFunctionModifier());
        registerFunctionModifier(OracleSpatialFunctions.WITHIN_DISTANCE, new OracleSpatialFunctionModifier());
       
        //add in type conversion
        ConvertModifier convertModifier = new ConvertModifier();
      convertModifier.addTypeMapping("char(1)", FunctionModifier.CHAR); //$NON-NLS-1$
      convertModifier.addTypeMapping("date", FunctionModifier.DATE, FunctionModifier.TIME); //$NON-NLS-1$
      convertModifier.addTypeMapping("timestamp", FunctionModifier.TIMESTAMP); //$NON-NLS-1$
      convertModifier.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.TIME, new FunctionModifier() {
        @Override
        public List<?> translate(Function function) {
          return Arrays.asList("case when ", function.getParameters().get(0), " is null then null else to_date('1970-01-01 ' || to_char(",function.getParameters().get(0),", 'HH24:MI:SS'), 'YYYY-MM-DD HH24:MI:SS') end"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        }
      });
      convertModifier.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.DATE, new FunctionModifier() {
      @Override
      public List<?> translate(Function function) {
        return Arrays.asList("trunc(cast(",function.getParameters().get(0)," AS date))"); //$NON-NLS-1$ //$NON-NLS-2$
      }
    });
      convertModifier.addConvert(FunctionModifier.DATE, FunctionModifier.STRING, new ConvertModifier.FormatModifier("to_char", DATE_FORMAT)); //$NON-NLS-1$
      convertModifier.addConvert(FunctionModifier.TIME, FunctionModifier.STRING, new ConvertModifier.FormatModifier("to_char", TIME_FORMAT)); //$NON-NLS-1$
      convertModifier.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.STRING, new FunctionModifier() {
      @Override
      public List<?> translate(Function function) {
        //if column and type is date, just use date format
        Expression ex = function.getParameters().get(0);
        String format = TIMESTAMP_FORMAT;
        if (ex instanceof ColumnReference && "date".equalsIgnoreCase(((ColumnReference)ex).getMetadataObject().getNativeType())) { //$NON-NLS-1$
          format = DATETIME_FORMAT;
        } else if (!(ex instanceof Literal) && !(ex instanceof Function)) {
          //this isn't needed in every case, but it's simpler than inspecting the expression more
          ex = ConvertModifier.createConvertFunction(getLanguageFactory(), function.getParameters().get(0), TypeFacility.RUNTIME_NAMES.TIMESTAMP);
        }
        return Arrays.asList("to_char(", ex, ", '", format, "')"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
      }
    });
      convertModifier.addConvert(FunctionModifier.STRING, FunctionModifier.DATE, new ConvertModifier.FormatModifier("to_date", DATE_FORMAT)); //$NON-NLS-1$
      convertModifier.addConvert(FunctionModifier.STRING, FunctionModifier.TIME, new ConvertModifier.FormatModifier("to_date", TIME_FORMAT)); //$NON-NLS-1$
      convertModifier.addConvert(FunctionModifier.STRING, FunctionModifier.TIMESTAMP, new ConvertModifier.FormatModifier("to_timestamp", TIMESTAMP_FORMAT)); //$NON-NLS-1$
      convertModifier.addTypeConversion(new ConvertModifier.FormatModifier("to_char"), FunctionModifier.STRING); //$NON-NLS-1$
      //NOTE: numeric handling in Oracle is split only between integral vs. floating/decimal types
      convertModifier.addTypeConversion(new ConvertModifier.FormatModifier("to_number"), //$NON-NLS-1$
          FunctionModifier.FLOAT, FunctionModifier.DOUBLE, FunctionModifier.BIGDECIMAL);
      convertModifier.addTypeConversion(new FunctionModifier() {
      @Override
      public List<?> translate(Function function) {
        if (Number.class.isAssignableFrom(function.getParameters().get(0).getType())) {
          return Arrays.asList("trunc(", function.getParameters().get(0), ")"); //$NON-NLS-1$ //$NON-NLS-2$
        }
View Full Code Here

TOP

Related Classes of org.teiid.translator.jdbc.FunctionModifier

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.