001    package railo.runtime.db;
002    
003    import java.io.Serializable;
004    import java.sql.Types;
005    
006    import railo.commons.lang.SizeOf;
007    import railo.runtime.exp.PageException;
008    import railo.runtime.op.Caster;
009    import railo.runtime.type.Sizeable;
010    
011    
012    /**
013     * 
014     */
015    public final class SQLItemImpl implements SQLItem,Serializable,Sizeable {
016    
017            /** Yes or No. Indicates whether the parameter is passed as a null. If Yes, the tag ignores the 
018            **      value attribute. The default is No. */
019            private boolean nulls;
020    
021            /** Specifies the actual value that Railo passes to the right of the comparison operator in a 
022            **      where clause. */
023            private Object value;
024            private Object cfValue;
025    
026    
027            /** Number of decimal places of the parameter. The default value is zero. */
028            private int scale=0;
029    
030            /** The SQL type that the parameter (any type) will be bound to. */
031            private int type=Types.CHAR;
032    
033        private boolean isValueSet;
034        
035        /**
036         * constructor of the class
037         */
038        public SQLItemImpl() {}
039        
040        /**
041         *  constructor of the class
042         * @param value
043         */
044        public SQLItemImpl(Object value) {
045            this.value=value;
046        }
047        
048        /**
049         *  constructor of the class
050         * @param value
051         */
052        public SQLItemImpl(Object value, int type) {
053            this.value=value;
054            this.type=type;
055        }
056    
057        @Override
058        public boolean isNulls() {
059            return nulls;
060        }
061        @Override
062        public void setNulls(boolean nulls) {
063            this.nulls = nulls;
064        }
065        @Override
066        public int getScale() {
067            return scale;
068        }
069        @Override
070        public void setScale(int scale) {
071            this.scale = scale;
072        }
073        @Override
074        public Object getValue() {
075            return value;
076        }
077        @Override
078        public void setValue(Object value) {
079            isValueSet=true;
080            this.value = value;
081        }
082        
083        @Override
084        public int getType() {
085            return type;
086        }
087        @Override
088        public void setType(int type) {
089            this.type = type;
090        }
091        
092        @Override
093        public SQLItem clone(Object object) {
094           
095            SQLItemImpl item = new SQLItemImpl();
096            item.nulls=nulls;
097            item.scale=scale;
098            item.type=type;
099            item.value=object;
100            return item;
101        }
102        
103        @Override
104        public Object getValueForCF() throws PageException {
105            if(cfValue==null) {
106                cfValue=SQLCaster.toCFTypex(this);
107            }
108            return cfValue;
109        }
110        
111        @Override
112        public boolean isValueSet() {
113            return isValueSet;
114        }
115        
116        public String toString() {
117            try {
118                            return Caster.toString(getValueForCF(),"");
119                    } catch (PageException e) {
120                            return Caster.toString(getValue(),"");
121                    }
122        }
123    
124            public long sizeOf() {
125                    return SizeOf.size(this.cfValue)+
126                    SizeOf.size(this.isValueSet)+
127                    SizeOf.size(this.nulls)+
128                    SizeOf.size(this.value);
129            }
130    
131            
132            public static SQLItem duplicate(SQLItem item) {
133                    if(!(item instanceof SQLItemImpl)) return item;
134                    return ((SQLItemImpl) item).duplicate();
135            }
136    
137            public SQLItem duplicate() {
138                    SQLItemImpl rtn = new SQLItemImpl(value,type);
139                    rtn.nulls=nulls;
140                    rtn.cfValue=cfValue;
141                    rtn.isValueSet=isValueSet;
142                    rtn.scale=scale;
143                    return rtn;
144            }
145    }