44//** Function Class
55//******************************************************************************
66/**
7- * Used to encapsulate SQL functions for database inserts and updates. This
8- * class is used in conjunction with the Recordset class. Example:
9- * <pre>rs.setValue("LAST_UPDATE", new javaxt.sql.Function("NOW()"));</pre>
7+ * The Function class is used to encapsulate SQL functions when inserting or
8+ * updating records via the javaxt.sql.Recordset class. Example:
9+ <pre>
10+ rs.setValue("LAST_UPDATE", new javaxt.sql.Function("NOW()"));
11+ rs.setValue("DATEDIFF_TEST", new javaxt.sql.Function("DATEDIFF(year, '2012/04/28', '2014/04/28')"));
12+ </pre>
1013 *
11- * Functions can be parameterized for more efficient inserts and updates. This
12- * is especially important for batch inserts. For example, instead of this:
13- * <pre>rs.setValue("DATEDIFF_TEST", new javaxt.sql.Function("DATEDIFF(year, '2012/04/28', '2014/04/28')"));</pre>
14- * The function can be parameterized like this:
15- * <pre>rs.setValue("DATEDIFF_TEST", new javaxt.sql.Function("DATEDIFF(year, ?, ?)", new Object[]{"2012/04/28", "2014/04/28"}));</pre>
14+ * Note that functions can be parameterized using standard JDBC syntax using
15+ * question marks (? characters) like this:
16+ <pre>
17+ JSONObject json = new JSONObject();
18+ rs.setValue("info", new javaxt.sql.Function("?::jsonb", json.toString()));
19+ </pre>
20+ *
21+ * Parameterizing values is especially useful when dealing with strings and
22+ * other values that may have invalid characters. It is also extremely useful
23+ * when performing batch inserts.
1624 *
1725 ******************************************************************************/
1826
@@ -22,30 +30,74 @@ public class Function {
2230 private Object [] values ;
2331
2432
25- public Function (String function , Object [] values ){
33+ //**************************************************************************
34+ //** Constructor
35+ //**************************************************************************
36+ /** Creates a new instance of this class using a function (e.g. "NOW()")
37+ */
38+ public Function (String function ){
2639 this .function = function ;
27- this .values = values ;
2840 }
2941
30- public Function (String function ){
42+
43+ //**************************************************************************
44+ //** Constructor
45+ //**************************************************************************
46+ /** Creates a new instance of this class using a parameterized function.
47+ */
48+ public Function (String function , Object ... values ){
3149 this .function = function ;
50+
51+ try {
52+ if (values [0 ] instanceof Object []){
53+ values = (Object []) values [0 ];
54+ }
55+ }
56+ catch (Exception e ){
57+ }
58+
59+ this .values = values ;
3260 }
3361
62+
63+ //**************************************************************************
64+ //** getFunction
65+ //**************************************************************************
66+ /** Returns the function supplied in the constructor
67+ */
3468 public String getFunction (){
3569 return function ;
3670 }
3771
72+
73+ //**************************************************************************
74+ //** hasValues
75+ //**************************************************************************
76+ /** Returns true if values were supplied to the constructor
77+ */
3878 public boolean hasValues (){
3979 if (values !=null ){
4080 return (values .length >0 );
4181 }
4282 return false ;
4383 }
4484
85+
86+ //**************************************************************************
87+ //** getValues
88+ //**************************************************************************
89+ /** Returns an array of values that were supplied in the constructor
90+ */
4591 public Object [] getValues (){
4692 return values ;
4793 }
94+
4895
96+ //**************************************************************************
97+ //** toString
98+ //**************************************************************************
99+ /** Returns the function supplied in the constructor
100+ */
49101 public String toString (){
50102 return function ;
51103 }
0 commit comments