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        /**
058         * @see railo.runtime.db.SQLItem#isNulls()
059         */
060        public boolean isNulls() {
061            return nulls;
062        }
063        /**
064         * @see railo.runtime.db.SQLItem#setNulls(boolean)
065         */
066        public void setNulls(boolean nulls) {
067            this.nulls = nulls;
068        }
069        /**
070         * @see railo.runtime.db.SQLItem#getScale()
071         */
072        public int getScale() {
073            return scale;
074        }
075        /**
076         * @see railo.runtime.db.SQLItem#setScale(int)
077         */
078        public void setScale(int scale) {
079            this.scale = scale;
080        }
081        /**
082         * @see railo.runtime.db.SQLItem#getValue()
083         */
084        public Object getValue() {
085            return value;
086        }
087        /**
088         * @see railo.runtime.db.SQLItem#setValue(java.lang.Object)
089         */
090        public void setValue(Object value) {
091            isValueSet=true;
092            this.value = value;
093        }
094        
095        /**
096         * @see railo.runtime.db.SQLItem#getType()
097         */
098        public int getType() {
099            return type;
100        }
101        /**
102         * @see railo.runtime.db.SQLItem#setType(int)
103         */
104        public void setType(int type) {
105            this.type = type;
106        }
107        
108        /**
109         * @see railo.runtime.db.SQLItem#clone(java.lang.Object)
110         */
111        public SQLItem clone(Object object) {
112           
113            SQLItemImpl item = new SQLItemImpl();
114            item.nulls=nulls;
115            item.scale=scale;
116            item.type=type;
117            item.value=object;
118            return item;
119        }
120        
121        /**
122         * @see railo.runtime.db.SQLItem#getValueForCF()
123         */
124        public Object getValueForCF() throws PageException {
125            if(cfValue==null) {
126                cfValue=SQLCaster.toCFTypex(this);
127            }
128            return cfValue;
129        }
130        
131        /**
132         * @see railo.runtime.db.SQLItem#isValueSet()
133         */
134        public boolean isValueSet() {
135            return isValueSet;
136        }
137        
138        public String toString() {
139            try {
140                            return Caster.toString(getValueForCF(),"");
141                    } catch (PageException e) {
142                            return Caster.toString(getValue(),"");
143                    }
144        }
145    
146            public long sizeOf() {
147                    return SizeOf.size(this.cfValue)+
148                    SizeOf.size(this.isValueSet)+
149                    SizeOf.size(this.nulls)+
150                    SizeOf.size(this.value);
151            }
152    
153            
154            public static SQLItem duplicate(SQLItem item) {
155                    if(!(item instanceof SQLItemImpl)) return item;
156                    return ((SQLItemImpl) item).duplicate();
157            }
158    
159            public SQLItem duplicate() {
160                    SQLItemImpl rtn = new SQLItemImpl(value,type);
161                    rtn.nulls=nulls;
162                    rtn.cfValue=cfValue;
163                    rtn.isValueSet=isValueSet;
164                    rtn.scale=scale;
165                    return rtn;
166            }
167    }