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.op;
020
021import java.util.Iterator;
022import java.util.List;
023
024import lucee.runtime.sql.exp.Expression;
025import lucee.runtime.sql.exp.ExpressionSupport;
026
027public class OperationN extends ExpressionSupport implements Operation {
028
029        private String operator;
030        private List operants;
031
032        public OperationN(String operator, List operants) {
033                this.operator=operator;
034                this.operants=operants;
035        }
036
037        public String toString(boolean noAlias) {
038                if(!hasIndex() || noAlias) {
039                        StringBuffer sb=new StringBuffer();
040                        sb.append(operator);
041                        sb.append('(');
042                        Iterator it = operants.iterator();
043                        boolean isFirst=true;
044                        while(it.hasNext()) {
045                                if(!isFirst)sb.append(',');
046                                Expression exp=(Expression) it.next();
047                                sb.append(exp.toString(!operator.equalsIgnoreCase("cast")));
048                                isFirst=false;
049                        }
050                        sb.append(')');
051                        return sb.toString();
052                }
053                return toString(true)+" as "+getAlias();
054        }
055
056        /**
057         * @return the operants
058         */
059        public Expression[] getOperants() {
060                if(operants==null) return new Expression[0];
061                return (Expression[]) operants.toArray(new Expression[operants.size()]);
062        }
063
064        /**
065         * @return the operator
066         */
067        public String getOperator() {
068                return operator;
069        }
070}