001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.core.query.sql.model; 023 024/** 025 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 026 */ 027public class SelectClause extends Clause { 028 029 private static final long serialVersionUID = -3786932682733679665L; 030 031 public final SelectList elements; 032 033 public final boolean distinct; 034 035 public SelectClause() { 036 this(new SelectList(), false); 037 } 038 039 public SelectClause(boolean distinct) { 040 this(new SelectList(), distinct); 041 } 042 043 public SelectClause(SelectList elements) { 044 this(elements, false); 045 } 046 047 public SelectClause(SelectList elements, boolean distinct) { 048 super("SELECT"); 049 this.elements = elements; 050 this.distinct = distinct; 051 } 052 053 public void add(String alias, Operand element) { 054 elements.add(alias, element); 055 } 056 057 public void add(Operand element) { 058 elements.add(element.toString(), element); 059 } 060 061 public Operand get(String alias) { 062 return elements.get(alias); 063 } 064 065 public Reference getVariable(String alias) { 066 return (Reference) elements.get(alias); 067 } 068 069 public Literal getLiteral(String alias) { 070 return (Literal) elements.get(alias); 071 } 072 073 public Function getFunction(String alias) { 074 return (Function) elements.get(alias); 075 } 076 077 public Expression getExpression(String alias) { 078 return (Expression) elements.get(alias); 079 } 080 081 public Operand get(int i) { 082 return elements.get(i); 083 } 084 085 public String getAlias(int i) { 086 return elements.getKey(i); 087 } 088 089 public Reference getVariable(int i) { 090 return (Reference) elements.get(i); 091 } 092 093 public Literal getLiteral(int i) { 094 return (Literal) elements.get(i); 095 } 096 097 public Function getFunction(int i) { 098 return (Function) elements.get(i); 099 } 100 101 public Expression getExpression(int i) { 102 return (Expression) elements.get(i); 103 } 104 105 public boolean isDistinct() { 106 return distinct; 107 } 108 109 public SelectList getSelectList() { 110 return elements; 111 } 112 113 public int count() { 114 return elements.size(); 115 } 116 117 public boolean isEmpty() { 118 return elements.isEmpty(); 119 } 120 121 @Override 122 public void accept(IVisitor visitor) { 123 visitor.visitSelectClause(this); 124 } 125 126 @Override 127 public boolean equals(Object obj) { 128 if (this == obj) { 129 return true; 130 } 131 if (obj instanceof SelectClause) { 132 SelectClause sc = (SelectClause) obj; 133 return elements.equals(sc.elements) && (distinct == sc.distinct); 134 } 135 return false; 136 } 137 138 @Override 139 public int hashCode() { 140 int result = elements.hashCode(); 141 result = 31 * result + (distinct ? 1 : 0); 142 return result; 143 } 144 145 @Override 146 public String toString() { 147 return (distinct ? "DISTINCT " : "") + elements; 148 } 149 150}