001    package railo.runtime.type;
002    
003    import java.util.Date;
004    import java.util.Iterator;
005    
006    import railo.runtime.PageContext;
007    import railo.runtime.dump.DumpData;
008    import railo.runtime.dump.DumpProperties;
009    import railo.runtime.dump.DumpUtil;
010    import railo.runtime.exp.DatabaseException;
011    import railo.runtime.exp.PageException;
012    import railo.runtime.op.Caster;
013    import railo.runtime.op.Operator;
014    import railo.runtime.op.date.DateCaster;
015    import railo.runtime.type.dt.DateTime;
016    import railo.runtime.type.it.KeyIterator;
017    import railo.runtime.type.util.QueryUtil;
018    
019    /**
020     * Recordcount Query Column
021     */
022    public final class QueryColumnRef implements QueryColumn,Sizeable {
023        
024        private Query query;
025        private Collection.Key columnName;
026        private int type;
027    
028        /**
029         * Constructor of the class
030         * @param query
031         * @param columnName 
032         * @param type 
033         */
034        public QueryColumnRef(Query query, Collection.Key columnName, int type) {
035            this.query=query;
036            this.columnName=columnName;
037            this.type=type;
038        }
039    
040        /**
041         * @see railo.runtime.type.QueryColumn#remove(int)
042         */
043        public Object remove(int row) throws DatabaseException {
044            throw new DatabaseException("can't remove "+columnName+" at row "+row+" value from Query",null,null,null);
045        }
046    
047        /**
048         * @see railo.runtime.type.QueryColumn#removeEL(int)
049         */
050        public Object removeEL(int row) {
051            return query.getAt(columnName,row,null);
052        }
053    
054        /**
055         * @see railo.runtime.type.QueryColumn#get(int)
056         */
057        public Object get(int row) throws PageException {
058            return query.getAt(columnName,row);
059        }
060        
061        /**
062         * touch a value, means if key dosent exist, it will created
063         * @param row
064         * @return matching value or created value
065         * @throws PageException
066         */
067        public Object touch(int row) throws PageException {
068            Object o= query.getAt(columnName,row,null);
069            if(o!=null) return o;
070            return query.setAt(columnName,row,new StructImpl());
071        }
072        
073        public Object touchEL(int row) {
074            Object o= query.getAt(columnName,row,null);
075            if(o!=null) return o;
076            return query.setAtEL(columnName,row,new StructImpl());
077        }
078    
079        /**
080         * @see railo.runtime.type.QueryColumn#get(int, java.lang.Object)
081         */
082        public Object get(int row, Object defaultValue) {
083            return query.getAt(columnName,row,defaultValue);
084        }
085    
086        /**
087         * @see railo.runtime.type.QueryColumn#set(int, java.lang.Object)
088         */
089        public Object set(int row, Object value) throws DatabaseException {
090            throw new DatabaseException("can't change "+columnName+" value from Query",null,null,null);
091        }
092    
093        /**
094         * @see railo.runtime.type.QueryColumn#setEL(int, java.lang.Object)
095         */
096        public Object setEL(int row, Object value) {
097            return query.getAt(columnName,row,null);
098        }
099    
100        /**
101         * @see railo.runtime.type.QueryColumn#add(java.lang.Object)
102         */
103        public void add(Object value) {}
104    
105        /**
106         * @see railo.runtime.type.QueryColumn#addRow(int)
107         */
108        public void addRow(int count) {}
109    
110        /**
111         * @see railo.runtime.type.QueryColumn#getType()
112         */
113        public int getType() {
114            return type;
115        }
116    
117        /**
118         * @see railo.runtime.type.QueryColumn#getTypeAsString()
119         */
120        public String getTypeAsString() {
121            return QueryImpl.getColumTypeName(getType());
122        }
123    
124        /**
125         * @see railo.runtime.type.QueryColumn#cutRowsTo(int)
126         */
127        public void cutRowsTo(int maxrows) {}
128    
129        /**
130         *
131         * @see railo.runtime.type.ContextCollection#get(railo.runtime.PageContext, java.lang.String, java.lang.Object)
132         */
133        public Object get(PageContext pc, String key, Object defaultValue) {
134            return get(key,defaultValue);
135        }
136    
137        /**
138         * @throws PageException 
139         * @see railo.runtime.type.ContextCollection#get(railo.runtime.PageContext, java.lang.String)
140         */
141        public Object get(PageContext pc, String key) throws PageException {
142            return get(key);
143        }
144    
145        /**
146         * @see railo.runtime.type.Collection#size()
147         */
148        public int size() {
149            return query.size();
150        }
151    
152        /**
153         * @see railo.runtime.type.Collection#keysAsString()
154         */
155        public String[] keysAsString() {
156            String[] k=new String[size()];
157            for(int i=1;i<=k.length;i++) {
158                k[i-1]=Caster.toString(i);
159            }
160            return k;
161        }
162        
163        public Collection.Key[] keys() {
164            Collection.Key[] k=new Collection.Key[size()];
165            for(int i=1;i<=k.length;i++) {
166                k[i-1]=KeyImpl.init(Caster.toString(i));
167            }
168            return k;
169        }
170    
171            /**
172             * @see railo.runtime.type.Collection#remove(railo.runtime.type.Collection.Key)
173             */
174            public Object remove(Collection.Key key) throws PageException {
175                    throw new DatabaseException("can't remove "+key+" from Query",null,null,null);
176            }
177    
178            /**
179             * @see railo.runtime.type.Collection#removeEL(railo.runtime.type.Collection.Key)
180             */
181            public Object removeEL(Collection.Key key) {
182                    return get(key,null);
183            }
184    
185        /**
186         * @see railo.runtime.type.Collection#clear()
187         */
188        public void clear() {}
189    
190        /**
191         * @see railo.runtime.type.Collection#get(java.lang.String)
192         */
193        public Object get(String key) throws PageException {
194            return get(Caster.toIntValue(key));
195        }
196    
197            /**
198             * @see railo.runtime.type.Collection#get(railo.runtime.type.Collection.Key)
199             */
200            public Object get(Collection.Key key) throws PageException {
201                    return get(Caster.toIntValue(key.getString()));
202            }
203    
204        /**
205         *
206         * @see railo.runtime.type.Collection#get(java.lang.String, java.lang.Object)
207         */
208        public Object get(String key, Object defaultValue) {
209            return get(Caster.toIntValue(key,query.getCurrentrow()),defaultValue);
210        }
211    
212            /**
213             *
214             * @see railo.runtime.type.Collection#get(railo.runtime.type.Collection.Key, java.lang.Object)
215             */
216            public Object get(Collection.Key key, Object defaultValue) {
217                    return get(Caster.toIntValue(key,query.getCurrentrow()),defaultValue);
218            }
219    
220        /**
221         * @see railo.runtime.type.Collection#set(java.lang.String, java.lang.Object)
222         */
223        public Object set(String key, Object value) throws PageException {
224            return set(Caster.toIntValue(key),value);
225        }
226    
227            /**
228             *
229             * @see railo.runtime.type.Collection#set(railo.runtime.type.Collection.Key, java.lang.Object)
230             */
231            public Object set(Collection.Key key, Object value) throws PageException {
232                    return set(Caster.toIntValue(key),value);
233            }
234    
235        /**
236         * @see railo.runtime.type.Collection#setEL(java.lang.String, java.lang.Object)
237         */
238        public Object setEL(String key, Object value) {
239            return setEL(Caster.toIntValue(key,query.getCurrentrow()),value);
240        }
241    
242            /**
243             *
244             * @see railo.runtime.type.Collection#setEL(railo.runtime.type.Collection.Key, java.lang.Object)
245             */
246            public Object setEL(Collection.Key key, Object value) {
247                    return setEL(Caster.toIntValue(key,query.getCurrentrow()),value);
248            }
249    
250        /**
251         * @see railo.runtime.type.Iteratorable#keyIterator()
252         */
253        public Iterator keyIterator() {
254            return new KeyIterator(keys());
255        }
256        
257            /**
258             * @see railo.runtime.type.Iteratorable#valueIterator()
259             */
260            public Iterator valueIterator() {
261                    return query.getColumn(columnName,null).valueIterator();
262            }
263        
264    
265            /**
266             *
267             * @see railo.runtime.type.Iteratorable#iterator()
268             */
269            public Iterator iterator() {
270                    return keyIterator();
271            }
272    
273        /**
274         * @see railo.runtime.type.Collection#containsKey(java.lang.String)
275         */
276        public boolean containsKey(String key) {
277            return get(key,null)!=null;
278        }
279    
280            /**
281             * @see railo.runtime.type.Collection#containsKey(railo.runtime.type.Collection.Key)
282             */
283            public boolean containsKey(Collection.Key key) {
284                    return get(key,null)!=null;
285            }
286    
287        /**
288             * @see railo.runtime.dump.Dumpable#toDumpData(railo.runtime.PageContext, int)
289             */
290            public DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties dp) {
291                return DumpUtil.toDumpData(get(query.getCurrentrow(),null), pageContext,maxlevel,dp);
292        }
293    
294        /**
295         * @see railo.runtime.op.Castable#castToString()
296         */
297        public String castToString() throws PageException {
298            return Caster.toString(get(query.getCurrentrow()));
299        }
300    
301            /**
302             * @see railo.runtime.op.Castable#castToString(java.lang.String)
303             */
304            public String castToString(String defaultValue) {
305                    Object value = get(query.getCurrentrow(),null);
306                    if(value==null)return defaultValue;
307                    return Caster.toString(value,defaultValue);
308            }
309    
310        /**
311         * @see railo.runtime.op.Castable#castToBooleanValue()
312         */
313        public boolean castToBooleanValue() throws PageException {
314            return Caster.toBooleanValue(get(query.getCurrentrow()));
315        }
316        
317        /**
318         * @see railo.runtime.op.Castable#castToBoolean(java.lang.Boolean)
319         */
320        public Boolean castToBoolean(Boolean defaultValue) {
321            Object value = get(query.getCurrentrow(),null);
322                    if(value==null)return defaultValue;
323                    return Caster.toBoolean(value,defaultValue);
324        }
325    
326        /**
327         * @see railo.runtime.op.Castable#castToDoubleValue()
328         */
329        public double castToDoubleValue() throws PageException {
330            return Caster.toDoubleValue(get(query.getCurrentrow()));
331        }
332        
333        /**
334         * @see railo.runtime.op.Castable#castToDoubleValue(double)
335         */
336        public double castToDoubleValue(double defaultValue) {
337            Object value = get(query.getCurrentrow(),null);
338                    if(value==null)return defaultValue;
339                    return Caster.toDoubleValue(value,defaultValue);
340        }
341    
342        /**
343         * @see railo.runtime.op.Castable#castToDateTime()
344         */
345        public DateTime castToDateTime() throws PageException {
346            return Caster.toDate(get(query.getCurrentrow()),null);
347        }
348        
349        /**
350         * @see railo.runtime.op.Castable#castToDateTime(railo.runtime.type.dt.DateTime)
351         */
352        public DateTime castToDateTime(DateTime defaultValue) {
353            Object value = get(query.getCurrentrow(),null);
354                    if(value==null)return defaultValue;
355                    return DateCaster.toDateAdvanced(value,true,null,defaultValue);
356        }
357    
358            /**
359             * @see railo.runtime.op.Castable#compare(boolean)
360             */
361            public int compareTo(boolean b) throws PageException {
362                    return Operator.compare(castToBooleanValue(), b);
363            }
364    
365            /**
366             * @see railo.runtime.op.Castable#compareTo(railo.runtime.type.dt.DateTime)
367             */
368            public int compareTo(DateTime dt) throws PageException {
369                    return Operator.compare((Date)castToDateTime(), (Date)dt);
370            }
371    
372            /**
373             * @see railo.runtime.op.Castable#compareTo(double)
374             */
375            public int compareTo(double d) throws PageException {
376                    return Operator.compare(castToDoubleValue(), d);
377            }
378    
379            /**
380             * @see railo.runtime.op.Castable#compareTo(java.lang.String)
381             */
382            public int compareTo(String str) throws PageException {
383                    return Operator.compare(castToString(), str);
384            }
385    
386        /**
387         * @see railo.runtime.type.ref.Reference#getKeyAsString()
388         */
389        public String getKeyAsString() throws PageException {
390            return columnName.toString();
391        }
392    
393        /**
394         * @see railo.runtime.type.ref.Reference#getKey()
395         */
396        public Collection.Key getKey() throws PageException {
397            return columnName;
398        }
399    
400        /**
401         * @see railo.runtime.type.ref.Reference#get(railo.runtime.PageContext)
402         */
403        public Object get(PageContext pc) throws PageException {
404            return get(query.getCurrentrow());
405        }
406        
407        /**
408         *
409         * @see railo.runtime.type.ref.Reference#get(railo.runtime.PageContext, java.lang.Object)
410         */
411        public Object get(PageContext pc, Object defaultValue) {
412            return get(query.getCurrentrow(),defaultValue);
413        }
414    
415        /**
416         *
417         * @see railo.runtime.type.QueryColumn#removeRow(int)
418         */
419        public Object removeRow(int row) throws DatabaseException {
420            throw new DatabaseException("can't remove row from Query",null,null,null);
421        }
422    
423        /**
424         * @see railo.runtime.type.ref.Reference#touch(railo.runtime.PageContext)
425         */
426        public Object touch(PageContext pc) throws PageException {
427            return touch(query.getCurrentrow());
428        }
429    
430        /**
431         * @see railo.runtime.type.ref.Reference#touchEL(railo.runtime.PageContext)
432         */
433        public Object touchEL(PageContext pc) {
434            return touchEL(query.getCurrentrow());
435        }
436    
437        /**
438         * @see railo.runtime.type.ref.Reference#set(railo.runtime.PageContext, java.lang.Object)
439         */
440        public Object set(PageContext pc, Object value) throws PageException {
441            return set(query.getCurrentrow(),value);
442        }
443        
444        /**
445         * @see railo.runtime.type.ref.Reference#setEL(railo.runtime.PageContext, java.lang.Object)
446         */
447        public Object setEL(PageContext pc, Object value) {
448            return setEL(query.getCurrentrow(),value);
449        }
450    
451        /**
452         * @see railo.runtime.type.ref.Reference#remove(railo.runtime.PageContext)
453         */
454        public Object remove(PageContext pc) throws PageException {
455            return remove(query.getCurrentrow());
456        }
457    
458        /**
459         * @see railo.runtime.type.ref.Reference#removeEL(railo.runtime.PageContext)
460         */
461        public Object removeEL(PageContext pc) {
462            return removeEL(query.getCurrentrow());
463        }
464    
465        /**
466         * @see railo.runtime.type.ref.Reference#getParent()
467         */
468        public Object getParent() {
469            return query;
470        }
471    
472        /**
473         * @see railo.runtime.type.Collection#clone()
474         */
475        public Object clone() {
476            QueryColumn clone=new QueryColumnRef(query,columnName,type);
477            return clone;
478        }
479    
480        /**
481         * @see railo.runtime.type.Collection#duplicate(boolean)
482         */
483        public Collection duplicate(boolean deepCopy) {
484    //               MUST muss deepCopy checken
485            QueryColumn clone=new QueryColumnRef(query,columnName,type);
486            return clone;
487        }
488            
489    
490            /**
491             * @see railo.runtime.type.Sizeable#sizeOf()
492             */
493            public long sizeOf() {
494                    return QueryUtil.sizeOf(this);
495            }
496    
497    }