1 /*
2 Copyright (c) 2008 Health Market Science, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 USA
18
19 You can contact Health Market Science at info@healthmarketscience.com
20 or at the following address:
21
22 Health Market Science
23 2700 Horizon Drive
24 Suite 200
25 King of Prussia, PA 19406
26 */
27
28 package com.healthmarketscience.sqlbuilder;
29
30 import java.io.IOException;
31 import com.healthmarketscience.common.util.AppendableExt;
32 import com.healthmarketscience.sqlbuilder.dbspec.Column;
33 import com.healthmarketscience.sqlbuilder.dbspec.Function;
34
35
36 /**
37 * Outputs a function call
38 * <code>"<name>([<param1>, ... <paramN>])"</code>.
39 *
40 * @author James Ahlborn
41 */
42 public class FunctionCall extends Expression
43 {
44 private boolean _isDistinct;
45 private SqlObject _functionName;
46 private SqlObjectList<SqlObject> _params = SqlObjectList.create();
47
48 public FunctionCall(Function function) {
49 this((Object)function);
50 }
51
52 /**
53 * {@code Object} -> {@code SqlObject} conversions handled by
54 * {@link Converter#toCustomFunctionSqlObject(Object)}.
55 */
56 public FunctionCall(Object functionNameStr) {
57 _functionName = Converter.toCustomFunctionSqlObject(functionNameStr);
58 }
59
60 @Override
61 public boolean hasParens() { return false; }
62
63 /** Iff isDistinct is <code>true</code>, adds the DISTINCT keyword to the
64 parameter clause */
65 public FunctionCall setIsDistinct(boolean isDistinct) {
66 _isDistinct = isDistinct;
67 return this;
68 }
69
70 /**
71 * Adds custom parameters to the function call.
72 * <p>
73 * {@code Object} -> {@code SqlObject} conversions handled by
74 * {@link Converter#COLUMN_VALUE_TO_OBJ}.
75 */
76 public FunctionCall addCustomParams(Object... params) {
77 _params.addObjects(Converter.COLUMN_VALUE_TO_OBJ, params);
78 return this;
79 }
80
81 /** Adds column parameters to the function call as
82 <code>"<alias>.<column>"</code>. */
83 public FunctionCall addColumnParams(Column... columns) {
84 return addCustomParams((Object[])columns);
85 }
86
87 /**
88 * Adds a numeric value parameter to the function call.
89 * <p>
90 * {@code Object} -> {@code SqlObject} conversions handled by
91 * {@link Converter#COLUMN_VALUE_TO_OBJ}.
92 */
93 public FunctionCall addNumericValueParam(Object obj) {
94 return addCustomParams(obj);
95 }
96
97 @Override
98 protected void collectSchemaObjects(ValidationContext vContext) {
99 _functionName.collectSchemaObjects(vContext);
100 _params.collectSchemaObjects(vContext);
101 }
102
103 @Override
104 public void appendTo(AppendableExt app) throws IOException {
105 app.append(_functionName).append("(");
106 if(_isDistinct) {
107 app.append("DISTINCT ");
108 }
109 app.append(_params).append(")");
110 }
111
112 /**
113 * Convenience method for generating a FunctionCall using the standard AVG
114 * function.
115 */
116 public static FunctionCall avg() {
117 return new FunctionCall(new CustomSql("AVG"));
118 }
119
120 /**
121 * Convenience method for generating a FunctionCall using the standard MIN
122 * function.
123 */
124 public static FunctionCall min() {
125 return new FunctionCall(new CustomSql("MIN"));
126 }
127
128 /**
129 * Convenience method for generating a FunctionCall using the standard MAX
130 * function.
131 */
132 public static FunctionCall max() {
133 return new FunctionCall(new CustomSql("MAX"));
134 }
135
136 /**
137 * Convenience method for generating a FunctionCall using the standard SUM
138 * function.
139 */
140 public static FunctionCall sum() {
141 return new FunctionCall(new CustomSql("SUM"));
142 }
143
144 /**
145 * Convenience method for generating a FunctionCall using the standard COUNT
146 * function.
147 */
148 public static FunctionCall count() {
149 return new FunctionCall(new CustomSql("COUNT"));
150 }
151
152 /**
153 * Convenience method for generating a FunctionCall using the standard COUNT
154 * function with the single parameter '*'.
155 */
156 public static FunctionCall countAll() {
157 return (new FunctionCall(new CustomSql("COUNT")))
158 .addCustomParams(ALL_SYMBOL);
159 }
160
161 }