001package gudusoft.gsqlparser.compiler.builtinFunction;
002
003import gudusoft.gsqlparser.TBaseType;
004import gudusoft.gsqlparser.TLog;
005
006import java.lang.reflect.Method;
007import java.math.BigDecimal;
008import java.math.RoundingMode;
009
010public class TOracleBuiltFunction {
011
012    public final static String PACKAGE_DELIMITER = "___";
013
014    static TOracleBuiltFunction oracleBuiltFunction;
015
016    public static TOracleBuiltFunction getOracleBuiltFunctionInstance(){
017        if (oracleBuiltFunction == null) oracleBuiltFunction = new TOracleBuiltFunction();
018        return oracleBuiltFunction;
019    }
020    public Integer ABS(Integer n){
021        return Math.abs(n);
022    }
023
024    public Double ABS(Double n){
025        return Math.abs(n);
026    }
027
028    public Double ACOS(Double n){
029        return Math.acos(n);
030    }
031
032    public String CHR(Integer n){
033        // 目前只转化 ascii 字符
034        if (n>256) return "";
035        char c = (char) (n % 256);
036        return String.valueOf((char)c);
037    }
038
039    public String CONCAT(String char1, String char2){
040        return char1+char2;
041    }
042
043    public Double COS(Double n){
044        return Math.cos(n);
045    }
046
047    public Double COSH(Double n){
048        return Math.cosh(n);
049    }
050
051    public void DBMS_OUTPUT___PUT_LINE(String a){
052      //  System.out.println(a);
053        TBaseType.log(a, TLog.INTERPRETER_MSG);
054    }
055
056    public Double LOG(Double d1, Double d2){
057        return Math.log(d2) / Math.log(d1);
058    }
059    public String LOWER(String str){
060        if(str==null){
061            return "";
062        }
063        return str.toLowerCase();
064    }
065
066    public Integer MOD(Integer n2, Integer n1){
067        if(n1==0){
068            return n2;
069        }
070        return n2 % n1;
071    }
072
073    public String REPLACE(String str, String search, String replacement){
074        if(str==null){
075            return "";
076        }
077        return str.replace(search, replacement);
078    }
079
080    public Double ROUND(Double d, Integer newScale) {
081        if(newScale>=0){
082            BigDecimal bd = new BigDecimal(d).setScale(newScale, RoundingMode.HALF_UP);
083            return bd.doubleValue();
084        }
085        else{
086            int scale = -1 * newScale;
087            double f = 0.1;
088            int b = 10;
089            for(int i=0; i<scale; i++){
090                f = f * 0.1;
091                b = b * 10;
092            }
093            d = d * f;
094            BigDecimal bd = new BigDecimal(d).setScale(scale, RoundingMode.HALF_UP);
095            return bd.doubleValue() * b;
096        }
097    }
098
099    public Double ROUND(Double d) {
100        BigDecimal bd = new BigDecimal(d).setScale(0, RoundingMode.HALF_UP);
101        return bd.doubleValue();
102    }
103
104    public Integer SIGN(Double n){
105        if(n>0){
106            return 1;
107        }
108        else if(n==0){
109            return 0;
110        }
111        else{
112            return -1;
113        }
114    }
115
116    public Double SIN(Double n){
117        return Math.sin(n);
118    }
119
120    public String SUBSTR(String str,  Integer start, Integer size){
121        if(start==0){
122            start = 1;
123        }
124        if(start < 0){
125            start = str.length() + start + 1;
126        }
127        return str.substring(start-1, start + size -1);
128    }
129
130    public String SUBSTR(String str,  Integer start){
131        if(start==0){
132            start = 1;
133        }
134        if(start < 0){
135            start = str.length() + start + 1;
136        }
137        return str.substring(start-1);
138    }
139
140    public Double TAN(Double n){
141        BigDecimal bd = new BigDecimal(Math.tan(n)).setScale(10, RoundingMode.HALF_UP);
142        return bd.doubleValue();
143    }
144
145    public String TO_CHAR(Double n){
146        return Double.toString(n);
147    }
148
149    public String TO_CHAR(Integer n){
150        return Integer.toString(n);
151    }
152
153    public String TRIM(String str){
154        if(str==null){
155            return "";
156        }
157        return str.trim();
158    }
159
160    public String UPPER(String str){
161        if(str==null){
162            return "";
163        }
164        return str.toUpperCase();
165    }
166
167    public static Method searchMethod(String packageName, String functionName, Class[] parameterTypes, Class<?> returnType){
168        Method ret = null;
169
170
171        Class xxxClass = null;
172        try {
173            xxxClass = Class.forName("gudusoft.gsqlparser.compiler.builtinFunction.TOracleBuiltFunction");
174        } catch (ClassNotFoundException e) {
175            return null;
176        }
177
178        String fullName = functionName.toUpperCase();
179        if (packageName != null){
180            fullName = packageName.toUpperCase()+PACKAGE_DELIMITER+fullName;
181        }
182
183
184        Method[] allMethods = xxxClass.getDeclaredMethods();
185
186        for (Method m : allMethods) {
187           // System.out.println(m.getName());
188            if (m.getName().equals(fullName)){
189                if (returnType != m.getReturnType()){
190                    continue;
191                }
192                if (parameterTypes.length  != m.getParameterTypes().length){
193                    continue;
194                }
195
196                if ((parameterTypes.length == 0) && (m.getParameterTypes().length == 0)){
197                   // System.out.println("Find function: "+m.getName()+" with no argument");
198                    ret = m;
199                    break;
200                }
201
202                int i=0;
203                while (i<parameterTypes.length){
204                    if (m.getParameterTypes()[i] != parameterTypes[i]){
205                        break;
206                    }
207                    i++;
208                }
209
210                if (i == parameterTypes.length){
211                    //System.out.println("Find function: "+m.getName()+" with "+ parameterTypes.length +" arguments");
212                    ret =m;
213                    break;
214                }
215            }
216        }
217        return ret;
218    }
219
220}