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.runtime.db;
020
021import java.io.Serializable;
022import java.sql.Types;
023
024import lucee.commons.lang.SizeOf;
025import lucee.runtime.exp.PageException;
026import lucee.runtime.op.Caster;
027import lucee.runtime.type.Sizeable;
028
029
030/**
031 * 
032 */
033public class SQLItemImpl implements SQLItem,Serializable,Sizeable {
034
035        /** Yes or No. Indicates whether the parameter is passed as a null. If Yes, the tag ignores the 
036        **      value attribute. The default is No. */
037        private boolean nulls;
038
039        /** Specifies the actual value that Lucee passes to the right of the comparison operator in a 
040        **      where clause. */
041        private Object value;
042        private Object cfValue;
043
044
045        /** Number of decimal places of the parameter. The default value is zero. */
046        private int scale=0;
047
048        /** The SQL type that the parameter (any type) will be bound to. */
049        private int type=Types.CHAR;
050
051    private boolean isValueSet;
052    
053    /**
054     * constructor of the class
055     */
056    public SQLItemImpl() {}
057    
058    /**
059     *  constructor of the class
060     * @param value
061     */
062    public SQLItemImpl(Object value) {
063        this.value=value;
064    }
065    
066    /**
067     *  constructor of the class
068     * @param value
069     */
070    public SQLItemImpl(Object value, int type) {
071        this.value=value;
072        this.type=type;
073    }
074
075    @Override
076    public boolean isNulls() {
077        return nulls;
078    }
079    @Override
080    public void setNulls(boolean nulls) {
081        this.nulls = nulls;
082    }
083    @Override
084    public int getScale() {
085        return scale;
086    }
087    @Override
088    public void setScale(int scale) {
089        this.scale = scale;
090    }
091    @Override
092    public Object getValue() {
093        return value;
094    }
095    @Override
096    public void setValue(Object value) {
097        isValueSet=true;
098        this.value = value;
099    }
100    
101    @Override
102    public int getType() {
103        return type;
104    }
105    @Override
106    public void setType(int type) {
107        this.type = type;
108    }
109    
110    @Override
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    @Override
122    public Object getValueForCF() throws PageException {
123        if(cfValue==null) {
124            cfValue=SQLCaster.toCFTypex(this);
125        }
126        return cfValue;
127    }
128    
129    @Override
130    public boolean isValueSet() {
131        return isValueSet;
132    }
133    
134    public String toString() {
135        try {
136                        return Caster.toString(getValueForCF(),"");
137                } catch (PageException e) {
138                        return Caster.toString(getValue(),"");
139                }
140    }
141
142        public long sizeOf() {
143                return SizeOf.size(this.cfValue)+
144                SizeOf.size(this.isValueSet)+
145                SizeOf.size(this.nulls)+
146                SizeOf.size(this.value);
147        }
148
149        
150        public static SQLItem duplicate(SQLItem item) {
151                if(!(item instanceof SQLItemImpl)) return item;
152                return ((SQLItemImpl) item).duplicate();
153        }
154
155        public SQLItem duplicate() {
156                SQLItemImpl rtn = new SQLItemImpl(value,type);
157                rtn.nulls=nulls;
158                rtn.cfValue=cfValue;
159                rtn.isValueSet=isValueSet;
160                rtn.scale=scale;
161                return rtn;
162        }
163}