001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019
020package lucee.runtime.sql.old;
021
022import java.util.Hashtable;
023
024public final class ZUtils
025{
026    public static void init() {
027        if(fcts_ != null) return ;
028        fcts_ = new Hashtable();
029        //addCustomFunction("upper",1);
030        addCustomFunction("lower",1);
031        addCustomFunction("lcase",1);
032        addCustomFunction("ucase",1);
033        addCustomFunction("abs",1);
034        addCustomFunction("acos",1);
035        addCustomFunction("asin",1);
036        addCustomFunction("atan",1);
037        addCustomFunction("atan2",2);
038        addCustomFunction("bitand",2);
039        addCustomFunction("bitor",2);
040        addCustomFunction("ceiling",1);
041        addCustomFunction("cos",1);
042        addCustomFunction("cot",1);
043        addCustomFunction("degrees",1);
044        addCustomFunction("exp",1);
045        addCustomFunction("floor",1);
046        addCustomFunction("log",1);
047        addCustomFunction("log10",1);
048        addCustomFunction("mod",2);
049        addCustomFunction("pi",0);
050        addCustomFunction("power",2);
051        addCustomFunction("radians",1);
052        addCustomFunction("rand",0);
053        addCustomFunction("round",2);
054        addCustomFunction("roundmagic",1);
055        addCustomFunction("sign",1);
056        addCustomFunction("sin",1);
057        addCustomFunction("sqrt",1);
058        addCustomFunction("tan",1);
059        addCustomFunction("truncate",2);
060        addCustomFunction("ascii",1);
061        addCustomFunction("bit_length",1);
062        addCustomFunction("char",1);
063        addCustomFunction("char_length",1);
064        addCustomFunction("concat",2);
065        addCustomFunction("difference",2);
066        addCustomFunction("hextoraw",1);
067        addCustomFunction("insert",4);
068        addCustomFunction("lcase",1);
069        addCustomFunction("left",2);
070        addCustomFunction("length",1);
071        addCustomFunction("locate",3);
072        addCustomFunction("ltrim",1);
073        addCustomFunction("octet_length",1);
074        addCustomFunction("rawtohex",1);
075        addCustomFunction("repeat",2);
076        addCustomFunction("replace",3);
077        addCustomFunction("right",2);
078        addCustomFunction("rtrim",1);
079        addCustomFunction("soundex",1);
080        addCustomFunction("space",1);
081        addCustomFunction("substr",3);
082        addCustomFunction("substring",3);
083        addCustomFunction("ucase",1);
084        addCustomFunction("lower",1);
085        addCustomFunction("upper",1);
086        addCustomFunction("curdate",0);
087        addCustomFunction("curtime",0);
088        addCustomFunction("datediff",3);
089        addCustomFunction("dayname",1);
090        addCustomFunction("dayofmonth",1);
091        addCustomFunction("dayofweek",1);
092        addCustomFunction("dayofyear",1);
093        addCustomFunction("hour",1);
094        addCustomFunction("minute",1);
095        addCustomFunction("month",1);
096        addCustomFunction("monthname",1);
097        addCustomFunction("now",0);
098        addCustomFunction("quarter",1);
099        addCustomFunction("second",1);
100        addCustomFunction("week",1);
101        addCustomFunction("year",1);
102        addCustomFunction("current_date",1);
103        addCustomFunction("current_time",1);
104        addCustomFunction("current_timestamp",1);
105        addCustomFunction("database",0);
106        addCustomFunction("user",0);
107        addCustomFunction("current_user",0);
108        addCustomFunction("identity",0);
109        addCustomFunction("ifnull",2);
110        addCustomFunction("casewhen",3);
111        addCustomFunction("convert",2);
112        //addCustomFunction("cast",1);
113        addCustomFunction("coalesce",1000);
114        addCustomFunction("nullif",2);
115        addCustomFunction("extract",1);
116        addCustomFunction("position",1);
117        addCustomFunction("trim",1);
118        //LOCATE(search,s,[start])
119        //SUBSTR(s,start[,len])
120        //SUBSTRING(s,start[,len])
121        //COALESCE(expr1,expr2,expr3,...)[1]
122                
123    }
124
125    public ZUtils()
126    {
127    }
128
129    public static void addCustomFunction(String s, int i)
130    {
131        if(fcts_ == null)
132            fcts_ = new Hashtable();
133        if(i <= 0)
134            i = 1;
135        fcts_.put(s.toUpperCase(), Integer.valueOf(i));
136    }
137
138    public static int isCustomFunction(String s)        {
139        init();
140        Integer integer;
141        if(s == null || s.length() < 1 || fcts_ == null || (integer = (Integer)fcts_.get(s.toUpperCase())) == null) 
142            return -1;
143        
144        return integer.intValue();
145    }
146
147    public static boolean isAggregate(String s)
148    {
149        s = s.toUpperCase().trim();
150        return s.equals("SUM") || s.equals("AVG") || s.equals("MAX") || s.equals("MIN") || s.equals("COUNT") || fcts_ != null && fcts_.get(s) != null;
151    }
152
153    public static String getAggregateCall(String s)
154    {
155        int i = s.indexOf('(');
156        if(i <= 0)
157            return null;
158        String s1 = s.substring(0, i);
159        if(isAggregate(s1))
160            return s1.trim();
161        return null;
162    }
163
164    private static Hashtable fcts_ = null;
165
166}