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