001    package railo.runtime.sql.exp;
002    
003    import railo.runtime.exp.PageException;
004    import railo.runtime.type.Query;
005    import railo.runtime.type.QueryColumn;
006    import railo.runtime.type.util.QueryUtil;
007    
008    
009    public class ColumnExpression extends ExpressionSupport implements Column {
010    
011            private String table;
012            private String column;
013            private boolean hasBracked;
014            private int columnIndex;
015            private QueryColumn col;
016    
017            public ColumnExpression(String value, int columnIndex) {
018                    this.column=value;
019                    this.columnIndex=columnIndex;
020            }
021    
022            public void setSub(String sub) {
023                    if(table==null) {
024                            table=column;
025                            column=sub;
026                    }
027                    else column=(column+"."+sub);
028            }
029    
030            public String toString(boolean noAlias) {
031                    if(hasAlias() && !noAlias) return getFullName()+" as "+getAlias();
032                    return getFullName();
033            }
034    
035            @Override
036            public String getFullName() {
037                    if(table==null) return column;
038                    return table+"."+column;
039            }
040    
041            @Override
042            public String getAlias() {
043                    if(!hasAlias()) return getColumn();
044                    return super.getAlias();
045            }
046    
047            public String getColumn() {
048                    return column;
049            }
050    
051            public String getTable() {
052                    return table;
053            }
054    
055            public boolean hasBracked() {
056                    return hasBracked;
057            }
058    
059            public void hasBracked(boolean b) {
060                    this.hasBracked=b;
061            }
062    
063            /**
064             * @return the columnIndex
065             */
066            public int getColumnIndex() {
067                    return columnIndex;
068            }
069            
070            public Object getValue(Query qr, int row) throws PageException {
071                    if(col==null)col = qr.getColumn(getColumn());
072                    return QueryUtil.getValue(col,row);
073            }
074            
075            public Object getValue(Query qr, int row, Object defaultValue) {
076                    if(col==null){
077                            col = qr.getColumn(getColumn(),null);
078                            if(col==null) return defaultValue;
079                    }
080                    return col.get(row,defaultValue);
081            }
082    
083    }