1 package org.apache.torque.util.functions;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.List;
23 import java.util.Vector;
24
25 import org.apache.torque.Torque;
26
27 /***
28 * A default framework that implements the core SQLFunction interface
29 * requirements that can be used to build specific functions on.
30 *
31 * @author <a href="mailto:greg.monroe@dukece.com">Greg Monroe</a>
32 * @version $Id
33 */
34 public abstract class AbstractFunction implements SQLFunction
35 {
36 /*** The arguments being used by this function */
37 private List argumentList;
38 /*** The Torque DB connector name this function is associated with */
39 private String dbName;
40
41 /***
42 * Functions should only be created via the FunctionFactory class.
43 */
44 protected AbstractFunction() {
45 super();
46 }
47
48 /***
49 * This should return the SQL string that can be used
50 * when constructing the query. E.g. "AVG( table.column )" or
51 * CONCAT(table.column, " foobar");
52 *
53 * @return The SQL String.
54 */
55 public abstract String toSQL();
56
57 /***
58 * Return a string representation of the function parameter
59 * at the specified index. Should be null if parameter does not exist.
60 *
61 * @param index The 0 based index of the parameter to get.
62 * @return A String representation of the parameter. Null if one does not
63 * exist.
64 */
65 public abstract String getArgument( int i );
66
67 /***
68 * Return all the parameters as an object array. This allow for
69 * processing of the parameters in their original format rather
70 * than just in String format. E.g. a parameter might be specified
71 * as a Date object, or a Column object.
72 *
73 * @return Should return a valid Object array and not null. E.g.
74 * implementors should return new Object[0] if there are no parameters.
75 */
76 public Object[] getArguments()
77 {
78 Object[] args = getArgumentList().toArray();
79 if ( args == null )
80 {
81 args = new Object[0];
82 }
83 return args;
84 }
85
86 /***
87 * Get the name of the Torque Database associated with this function.
88 *
89 * @return The DB name. Should not be null (use default DB in this case).
90 * @exception IllegalStateException If Torque has not been initialized and
91 * the default DB name can not be determined.
92 */
93 public String getDBName() throws IllegalStateException
94 {
95 if ( this.dbName == null ) {
96 this.dbName = Torque.getDefaultDB();
97 }
98 if ( this.dbName == null ) {
99 throw new IllegalStateException(
100 "Could not get DB Name! Torque may not be initialized yet!");
101 }
102 return this.dbName;
103 }
104
105 /***
106 * Return the object representation of the function parameter
107 * at the specified index. Will be null if parameter does not exist.
108 *
109 * @param index The 0 based index of the parameter to get.
110 * @return The parameter object. Null if one does not
111 * exist.
112 */
113 protected Object getArgumentObject( int index )
114 {
115 List argList = getArgumentList();
116 if ( index >= argList.size() )
117 {
118 return null;
119 }
120 return argList.get(index);
121 }
122
123 /***
124 * Add an argument to the function argument list
125 *
126 * @param arg The argument object.
127 */
128 protected void addArgument( Object arg ) {
129 getArgumentList().add( arg );
130 }
131
132 /***
133 * Set the full function argument list.
134 *
135 * @param args The new argument list
136 */
137 protected void setArgumentList( List args ) {
138 this.argumentList = args;
139 }
140
141 /***
142 * Get the full list of function arguments
143 *
144 * @return The argument list
145 */
146 protected List getArgumentList( ) {
147 if ( this.argumentList == null ) {
148 this.argumentList = new Vector();
149 }
150 return this.argumentList;
151 }
152
153 /***
154 * @param dbName the dbName to set
155 */
156 public void setDBName(String dbName)
157 {
158 this.dbName = dbName;
159 }
160
161 }