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.sql.exp;
020
021import lucee.runtime.exp.PageException;
022import lucee.runtime.type.Collection;
023import lucee.runtime.type.KeyImpl;
024import lucee.runtime.type.Query;
025import lucee.runtime.type.QueryColumn;
026import lucee.runtime.type.util.QueryUtil;
027
028
029public class ColumnExpression extends ExpressionSupport implements Column {
030
031        private String table;
032        private String column;
033        private boolean hasBracked;
034        private int columnIndex;
035        private QueryColumn col;
036        
037        public String toString(){
038                return "table:"+table+";column:"+column+";hasBracked:"+hasBracked+";columnIndex:"+columnIndex;
039                
040        }
041
042        public ColumnExpression(String value, int columnIndex) {
043                this.column=value;
044                this.columnIndex=columnIndex;
045        }
046
047        public void setSub(String sub) {
048                if(table==null) {
049                        table=column;
050                        column=sub;
051                }
052                else column=(column+"."+sub);
053        }
054
055        public String toString(boolean noAlias) {
056                if(hasAlias() && !noAlias) return getFullName()+" as "+getAlias();
057                return getFullName();
058        }
059
060        @Override
061        public String getFullName() {
062                if(table==null) return column;
063                return table+"."+column;
064        }
065
066        @Override
067        public String getAlias() {
068                if(!hasAlias()) return getColumn().getString();
069                return super.getAlias();
070        }
071
072        public Collection.Key getColumn() {
073                return KeyImpl.init(column);
074        }
075
076        public String getTable() {
077                return table;
078        }
079
080        public boolean hasBracked() {
081                return hasBracked;
082        }
083
084        public void hasBracked(boolean b) {
085                this.hasBracked=b;
086        }
087
088    public String getColumnName() {
089
090        return column;
091    }
092
093        /**
094         * @return the columnIndex
095         */
096        public int getColumnIndex() {
097                return columnIndex;
098        }
099        // MUST hanle null correctly
100        public Object getValue(Query qr, int row) throws PageException {
101                if(col==null)col = qr.getColumn(getColumn());
102                return QueryUtil.getValue(col,row);
103        }
104        
105        public Object getValue(Query qr, int row, Object defaultValue) {
106                if(col==null){
107                        col = qr.getColumn(getColumn(),null);
108                        if(col==null) return defaultValue;
109                }
110                return col.get(row,defaultValue);
111        }
112
113}