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 **/
019package lucee.commons.lang;
020
021
022
023
024
025public final class CFTypes {
026
027    /**
028     * Field <code>TYPE_ANY</code>
029     */
030    public static final short TYPE_ANY=0;
031    /**
032     * Field <code>TYPE_ARRAY</code>
033     */
034    public static final short TYPE_ARRAY=1;
035    /**
036     * Field <code>TYPE_BOOLEAN</code>
037     */
038    public static final short TYPE_BASE64=20;
039    /**
040     * Field <code>TYPE_BOOLEAN</code>
041     */
042    public static final short TYPE_BOOLEAN=2;
043    /**
044     * Field <code>TYPE_BINARY</code>
045     */
046    public static final short TYPE_BINARY=3;
047    /**
048     * Field <code>TYPE_DATETIME</code>
049     */
050    public static final short TYPE_DATETIME=4;
051    /**
052     * Field <code>TYPE_NUMERIC</code>
053     */
054    public static final short TYPE_NUMERIC=5;
055    /**
056     * Field <code>TYPE_QUERY</code>
057     */
058    public static final short TYPE_QUERY=6;
059    /**
060     * Field <code>TYPE_STRING</code>
061     */
062    public static final short TYPE_STRING=7;
063    /**
064     * Field <code>TYPE_STRUCT</code>
065     */
066    public static final short TYPE_STRUCT=8;
067    /**
068     * Field <code>TYPE_TIMESPAN</code>
069     */
070    public static final short TYPE_TIMESPAN=9;
071    /**
072     * Field <code>TYPE_UUID</code>
073     */
074    public static final short TYPE_UUID=10;
075    /**
076     * Field <code>TYPE_VARIABLE_NAME</code>
077     */
078    public static final short TYPE_VARIABLE_NAME=11;
079    /**
080     * Field <code>TYPE_VARIABLE_STRING</code>
081     */
082    public static final short TYPE_VARIABLE_STRING=12;
083    /**
084     * Field <code>TYPE_UNKNOW</code>
085     */
086    public static final short TYPE_UNKNOW=-1;
087    /**
088     * Field <code>TYPE_UNKNOW</code>
089     */
090    public static final short TYPE_UNDEFINED=14;
091    /**
092     * Field <code>TYPE_VOID</code>
093     */
094    public static final short TYPE_VOID=15;
095    
096    /**
097     * Field <code>TYPE_XML</code>
098     */
099    public static final short TYPE_XML = 16;
100    
101    //public static final short TYPE_SIZE = 21;
102
103    public static final short TYPE_GUID = 22;
104
105    public static final short TYPE_FUNCTION = 23;
106    public static final short TYPE_QUERY_COLUMN=24;
107    public static final short TYPE_IMAGE=25;
108    
109    /**
110         * Wandelt einen String Datentypen in ein CFML short Typ um.
111         * @param type
112         * @param defaultValue
113         * @return short Data Type
114         */
115        public static String toString(int type,String defaultValue) {
116            switch(type){
117                        case TYPE_ANY:return "any";
118                        case TYPE_ARRAY:return "array";
119                        case TYPE_BASE64:return "base64";
120                        case TYPE_BINARY:return "binary";
121                        case TYPE_BOOLEAN:return "boolean";
122                        case TYPE_DATETIME:return "datetime";
123                        case TYPE_GUID:return "guid";
124                        case TYPE_IMAGE:return "image";
125                        case TYPE_NUMERIC:return "numeric";
126                        case TYPE_QUERY:return "query";
127                        case TYPE_QUERY_COLUMN:return "querycolumn";
128                        case TYPE_STRING:return "string";
129                        case TYPE_STRUCT:return "struct";
130                        case TYPE_TIMESPAN:return "timespan";
131                        case TYPE_UNDEFINED:return "any";
132                        case TYPE_UNKNOW:return "any";
133                        case TYPE_UUID:return "uuid";
134                        case TYPE_VARIABLE_NAME:return "variablename";
135                        case TYPE_VARIABLE_STRING:return "variablestring";
136                        case TYPE_VOID:return "void";
137                        case TYPE_XML:return "xml";
138                        case TYPE_FUNCTION:return "function";
139                }
140                return defaultValue;
141    
142        }
143        
144        public static short toShortStrict(String type, short defaultValue) {
145                type=type.toLowerCase().trim();
146            if(type.length()>2) {
147                char first=type.charAt(0);
148                switch(first) {
149                        case 'a':
150                            if(type.equals("any")) return TYPE_ANY;
151                            if(type.equals("array")) return TYPE_ARRAY;
152                        break;
153                        case 'b':
154                            if(type.equals("boolean") || type.equals("bool")) return TYPE_BOOLEAN;
155                            if(type.equals("binary")) return TYPE_BINARY;
156                            
157                        break;                  
158                        case 'd':
159                        if(type.equals("date") || type.equals("datetime"))      return TYPE_DATETIME;
160                        case 'f':
161                        if(type.equals("function"))     return TYPE_FUNCTION;
162                        break; 
163                        case 'g':
164                        if("guid".equals(type))                                                         return TYPE_GUID;
165                    break;
166                        case 'i':
167                        if("image".equals(type))                                                        return TYPE_IMAGE;
168                    break;
169                    case 'n':
170                            if(type.equals("numeric")) return TYPE_NUMERIC;
171                            else if(type.equals("number")) return TYPE_NUMERIC;
172                            break;
173                        case 'o':
174                            if(type.equals("object")) return TYPE_ANY;
175                            break;
176                        case 'q':
177                            if(type.equals("query")) return TYPE_QUERY;
178                            if(type.equals("querycolumn")) return TYPE_QUERY_COLUMN;
179                            break;
180                        case 's':
181                            if(type.equals("string")) return TYPE_STRING;
182                            else if(type.equals("struct")) return TYPE_STRUCT;
183                        break;
184                        case 't':
185                            if(type.equals("timespan")) return TYPE_TIMESPAN;
186                            if(type.equals("time")) return TYPE_DATETIME;
187                            if(type.equals("timestamp")) return TYPE_DATETIME;
188                            break;
189                        case 'u':
190                            if(type.equals("uuid")) return TYPE_UUID;
191                        break;
192                        case 'v':
193                            if(type.equals("variablename")) return TYPE_VARIABLE_NAME;
194                            if(type.equals("variable_name")) return TYPE_VARIABLE_NAME;
195                            if(type.equals("variablestring")) return TYPE_VARIABLE_STRING;
196                            if(type.equals("variable_string")) return TYPE_VARIABLE_STRING;
197                            if(type.equals("void")) return TYPE_VOID;
198                            break;
199                case 'x':
200                    if(type.equals("xml")) return TYPE_XML;
201                    break;
202                   }
203                }
204                return defaultValue;
205        }
206        
207        public static short toShort(String type, boolean alsoAlias, short defaultValue) {
208                type=type.toLowerCase().trim();
209            if(type.length()>2) {
210                char first=type.charAt(0);
211                switch(first) {
212                        case 'a':
213                            if(type.equals("any")) return TYPE_ANY;
214                            if(type.equals("array")) return TYPE_ARRAY;
215                        break;
216                        case 'b':
217                            if(type.equals("boolean") || (alsoAlias && type.equals("bool"))) return TYPE_BOOLEAN;
218                            if(type.equals("binary")) return TYPE_BINARY;
219                            if(alsoAlias && type.equals("bigint")) return TYPE_NUMERIC;
220                            if("base64".equals(type))return TYPE_STRING;
221                            
222                        break;                  
223                        case 'c':
224                            if(alsoAlias && "char".equals(type))return TYPE_STRING;
225                            
226                        break;                  
227                    case 'd':
228                        if(alsoAlias && "double".equals(type))                                                  return TYPE_NUMERIC;
229                        if(alsoAlias && "decimal".equals(type))                                                 return TYPE_STRING;
230                            if(type.equals("date") || type.equals("datetime"))                          return TYPE_DATETIME;
231                        break; 
232
233                    case 'e':
234                        if("eurodate".equals(type))                                                     return TYPE_DATETIME;
235                    break;
236                    case 'f':
237                        if(alsoAlias && "float".equals(type))                                                   return TYPE_NUMERIC;
238                        if("function".equals(type))                                                     return TYPE_FUNCTION;
239                    break;
240                    case 'g':
241                        if("guid".equals(type))                                                         return TYPE_GUID;
242                    break;
243
244                    case 'i':
245                        if(alsoAlias && ("int".equals(type) || "integer".equals(type)))         return TYPE_NUMERIC;
246                        if("image".equals(type))        return TYPE_NUMERIC;
247                    break;
248
249                    case 'l':
250                        if(alsoAlias && "long".equals(type))                                                            return TYPE_NUMERIC;
251                    break;
252                        
253                        case 'n':
254                            if(type.equals("numeric")) return TYPE_NUMERIC;
255                            else if(type.equals("number")) return TYPE_NUMERIC;
256                            if(alsoAlias) {
257                                    if(type.equals("node")) return TYPE_XML;
258                                    else if(type.equals("nvarchar")) return TYPE_STRING;
259                                    else if(type.equals("nchar")) return TYPE_STRING;
260                            }
261                            break;
262                        case 'o':
263                            if(type.equals("object")) return TYPE_ANY;
264                            if(alsoAlias && type.equals("other")) return TYPE_ANY;
265                            break;
266                        case 'q':
267                            if(type.equals("query")) return TYPE_QUERY;
268                            if(type.equals("querycolumn")) return TYPE_QUERY_COLUMN;
269                            break;
270                        case 's':
271                            if(type.equals("string")) return TYPE_STRING;
272                            else if(type.equals("struct")) return TYPE_STRUCT;
273                        if(alsoAlias && "short".equals(type))return TYPE_NUMERIC;
274                        break;
275                        case 't':
276                            if(type.equals("timespan")) return TYPE_TIMESPAN;
277                            if(type.equals("time")) return TYPE_DATETIME;
278                            if(alsoAlias && type.equals("timestamp")) return TYPE_DATETIME;
279                            if(alsoAlias && type.equals("text")) return TYPE_STRING;
280                            break;
281                        case 'u':
282                            if(type.equals("uuid")) return TYPE_UUID;
283                        if(alsoAlias && "usdate".equals(type))return TYPE_DATETIME;
284                        if(alsoAlias && "udf".equals(type))return TYPE_FUNCTION;
285                            break;
286                        case 'v':
287                            if(type.equals("variablename")) return TYPE_VARIABLE_NAME;
288                            if(alsoAlias && type.equals("variable_name")) return TYPE_VARIABLE_NAME;
289                            
290                            if(type.equals("variablestring")) return TYPE_VARIABLE_STRING;
291                            if(alsoAlias && type.equals("variable_string")) return TYPE_VARIABLE_STRING;
292                            
293                            
294                            if(type.equals("void")) return TYPE_VOID;
295                            if(alsoAlias && type.equals("varchar")) return TYPE_STRING;
296                            break;
297                case 'x':
298                    if(type.equals("xml")) return TYPE_XML;
299                    break;
300                   }
301                }
302                return defaultValue;
303        }
304
305        public static boolean isSimpleType(short type) {
306                return  type==TYPE_BOOLEAN || type==TYPE_DATETIME || type==TYPE_NUMERIC || type==TYPE_STRING;
307        }
308}