View Javadoc

1   package org.apache.torque.util.functions;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.lang.reflect.Constructor;
23  
24  import org.apache.torque.Torque;
25  import org.apache.torque.adapter.DB;
26  
27  /***
28   * <p>This class is a "Factory class" for creating SQLFunction implementation
29   * classes. It works with the DB adaptor classes to create functions that
30   * are appropriate for the specified (or default) db server type.</p>
31   * 
32   * <p>The generic method for doing this is the 
33   * functionInstance(String, FunctionEnum, Object[]) method.  However, there are
34   * a variety of convenience methods that can be used to quickly created 
35   * specific functions, e.g. avg( Object ) and the like.</p>
36   * 
37   * @see SQLFunction
38   * @see SummaryHelper
39   * 
40   * @author <a href="mailto:greg.monroe@dukece.com">Greg Monroe</a>
41   * @version $Id
42   */
43  public class FunctionFactory
44  {
45      /***
46       * This class only has static methods and shouldn't be created.
47       */
48      private FunctionFactory () 
49      {        
50      }
51      
52      /***
53       * Create an AVG SQL function implementation.
54       * 
55       * @param dbName The name of the Torque db 
56       * @param expr1 The expression to apply the function to.  Generally a 
57       *              column name in table.column format.  It is assumed
58       *              that the "toString()" method of this object will be
59       *              acceptable for an argument.
60       * @param distinct If true, the function expr will have be prefaced 
61       *                 with "DISTINCT", e.g. function(DISTINCT column).
62       * @return The function object to use.
63       * @throws Exception
64       */
65      public static final SQLFunction avg ( String dbName, Object expr1, 
66                                            boolean distinct )
67                                                           throws Exception
68      {
69          return functionInstance( dbName, FunctionEnum.AVG, 
70                                   new Object[]{ expr1, new Boolean(distinct) });
71      }
72      /***
73       * Convenience method for creating a non-distinct function that uses
74       * the default DB.  
75       *   
76       * @param expr1 The expression to apply the function to.  Generally a 
77       *              column name in table.column format.  It is assumed
78       *              that the "toString()" method of this object will be
79       *              acceptable for an argument.
80       * @return The function object to use.
81       * @throws Exception 
82       */
83      public static final SQLFunction avg ( Object expr1 ) 
84                                                  throws Exception
85      {
86          return avg ( null, expr1, false );
87      }
88      
89      /***
90       * Create a COUNT SQL function implementation.
91       * 
92       * @param dbName The name of the Torque db 
93       * @param expr1 The expression to apply the function to.  Generally a 
94       *              column name in table.column format.  It is assumed
95       *              that the "toString()" method of this object will be
96       *              acceptable for an argument.
97       * @param distinct If true, the function expr will have be prefaced 
98       *                 with "DISTINCT", e.g. function(DISTINCT column).
99       * @return The function object to use.
100      * @throws Exception
101      */
102     public static final SQLFunction count ( String dbName, Object expr1, 
103                                           boolean distinct )
104                                                          throws Exception
105     {
106         return functionInstance( dbName, FunctionEnum.COUNT,
107                                  new Object[]{ expr1, new Boolean(distinct) });
108     }
109     /***
110      * Convenience method for creating a non-distinct function that uses
111      * the default DB.  
112      *   
113      * @param expr1 The expression to apply the function to.  Generally a 
114      *              column name in table.column format.  It is assumed
115      *              that the "toString()" method of this object will be
116      *              acceptable for an argument.
117      * @return The function object to use.
118      * @throws Exception 
119      */
120     public static final SQLFunction count ( Object expr1 ) 
121                                                 throws Exception
122     {
123         return count ( null, expr1, false );
124     }
125     
126     /***
127      * Create a MAX SQL function implementation.
128      * 
129      * @param dbName The name of the Torque db 
130      * @param expr1 The expression to apply the function to.  Generally a 
131      *              column name in table.column format.  It is assumed
132      *              that the "toString()" method of this object will be
133      *              acceptable for an argument.
134      * @param distinct If true, the function expr will have be prefaced 
135      *                 with "DISTINCT", e.g. function(DISTINCT column).
136      * @return The function object to use.
137      * @throws Exception
138      */
139     public static final SQLFunction max ( String dbName, Object expr1, 
140                                           boolean distinct )
141                                                          throws Exception
142     {
143         return functionInstance( dbName, FunctionEnum.MAX,
144                                  new Object[]{ expr1, new Boolean(distinct) });
145     }
146     /***
147      * Convenience method for creating a non-distinct function that uses
148      * the default DB.  
149      *   
150      * @param expr1 The expression to apply the function to.  Generally a 
151      *              column name in table.column format.  It is assumed
152      *              that the "toString()" method of this object will be
153      *              acceptable for an argument.
154      * @return The function object to use.
155      * @throws Exception 
156      */
157     public static final SQLFunction max ( Object expr1 ) 
158                                                 throws Exception
159     {
160         return max ( null, expr1, false );
161     }
162     
163     /***
164      * Create a MIN SQL function implementation.
165      * 
166      * @param dbName The name of the Torque db 
167      * @param expr1 The expression to apply the function to.  Generally a 
168      *              column name in table.column format.  It is assumed
169      *              that the "toString()" method of this object will be
170      *              acceptable for an argument.
171      * @param distinct If true, the function expr will have be prefaced 
172      *                 with "DISTINCT", e.g. function(DISTINCT column).
173      * @return The function object to use.
174      * @throws Exception
175      */
176     public static final SQLFunction min ( String dbName, Object expr1, 
177                                           boolean distinct )
178                                                          throws Exception
179     {
180         return functionInstance( dbName, FunctionEnum.MIN,
181                                  new Object[]{ expr1, new Boolean(distinct) });
182     }
183     /***
184      * Convenience method for creating a non-distinct function that uses
185      * the default DB.  
186      *   
187      * @param expr1 The expression to apply the function to.  Generally a 
188      *              column name in table.column format.  It is assumed
189      *              that the "toString()" method of this object will be
190      *              acceptable for an argument.
191      * @return The function object to use.
192      * @throws Exception 
193      */
194     public static final SQLFunction min ( Object expr1 ) 
195                                                 throws Exception
196     {
197         return min ( null, expr1, false );
198     }
199     
200     /***
201      * Create a SUM SQL function implementation.
202      * 
203      * @param dbName The name of the Torque db 
204      * @param expr1 The expression to apply the function to.  Generally a 
205      *              column name in table.column format.  It is assumed
206      *              that the "toString()" method of this object will be
207      *              acceptable for an argument.
208      * @param distinct If true, the function expr will have be prefaced 
209      *                 with "DISTINCT", e.g. function(DISTINCT column).
210      * @return The function object to use.
211      * @throws Exception
212      */
213     public static final SQLFunction sum ( String dbName, Object expr1, 
214                                           boolean distinct )
215                                                          throws Exception
216     {
217         return functionInstance( dbName, FunctionEnum.SUM,
218                                   new Object[]{ expr1, new Boolean(distinct) });
219     }
220     /***
221      * Convenience method for creating a non-distinct function that uses
222      * the default DB.  
223      *   
224      * @param expr1 The expression to apply the function to.  Generally a 
225      *              column name in table.column format.  It is assumed
226      *              that the "toString()" method of this object will be
227      *              acceptable for an argument.
228      * @return The function object to use.
229      * @throws Exception 
230      */
231     public static final SQLFunction sum ( Object expr1 ) 
232                                                 throws Exception
233     {
234         return sum ( null, expr1, false );
235     }
236     
237     /***
238      * "Generic" function class creation method.
239      * 
240      * @param dbName  The name of the Torque DB this function applies to or 
241      *                the default Torque DB if null.
242      * @param function  The function to create an object for.
243      * @param arguments The arguments required by the function object.
244      * @return A SQLFunction implementation that for the specified DB server. 
245      * @throws Exception
246      */
247     public static final SQLFunction functionInstance ( String dbName, 
248                                                        FunctionEnum function,
249                                                        Object[] arguments )
250                                                            throws Exception 
251    {
252         DB dbAdaptor = null;
253         if ( dbName != null ) {
254            dbAdaptor = Torque.getDB(dbName);
255         } 
256         else 
257         {
258             dbAdaptor = Torque.getDB(Torque.getDefaultDB());
259         }
260         Class fClass = dbAdaptor.getFunctionClass( function );
261         if ( fClass == null ) 
262         {
263             throw new IllegalArgumentException(
264               "Could not find function class for '"+ function.toString() +"'");
265         }
266 
267         Object fInstance = null;
268         
269         // Check if class is an inner class 
270         Class outerClass = fClass.getDeclaringClass();
271         if ( outerClass != null ) 
272         {
273             Object oInstance = outerClass.newInstance();
274             Constructor iConstructor = 
275                 fClass.getDeclaredConstructor( new Class[] { outerClass } );
276             fInstance = iConstructor.newInstance( new Object[] { oInstance });
277         }
278         else
279         {
280             fInstance = fClass.newInstance();
281         }
282         SQLFunction func = (SQLFunction) fInstance;
283         func.setArguments( arguments );
284         func.setDBName( dbName );
285         
286         return func;
287    }
288 
289     public static final SQLFunction functionInstance ( FunctionEnum function,
290                                                        Object[] arguments )
291                                                            throws Exception 
292    {
293         return functionInstance ( null, function, arguments );
294    }
295 }