KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > expr > fun > ConcatFunExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free Software Foundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.amber.expr.fun;
30
31 import com.caucho.amber.expr.AmberExpr;
32 import com.caucho.amber.query.QueryParser;
33 import com.caucho.util.CharBuffer;
34 import com.caucho.util.L10N;
35
36 import java.util.ArrayList JavaDoc;
37
38
39 /**
40  * CONCAT function expression
41  */

42 public class ConcatFunExpr extends FunExpr {
43   private static final L10N L = new L10N(ConcatFunExpr.class);
44
45   /**
46    * Creates a new expression
47    */

48   protected ConcatFunExpr(QueryParser parser,
49                           ArrayList JavaDoc<AmberExpr> args)
50   {
51     super(parser, "concat", args, false);
52   }
53
54   public static FunExpr create(QueryParser parser,
55                                ArrayList JavaDoc<AmberExpr> args)
56   {
57     return new ConcatFunExpr(parser, args);
58   }
59
60   /**
61    * Generates the where expression.
62    */

63   public void generateWhere(CharBuffer cb)
64   {
65     generateInternalWhere(cb, true);
66   }
67
68   /**
69    * Generates the (update) where expression.
70    */

71   public void generateUpdateWhere(CharBuffer cb)
72   {
73     generateInternalWhere(cb, false);
74   }
75
76   //
77
// private/protected
78

79   void generateInternalWhere(CharBuffer cb,
80                              boolean select)
81   {
82     ArrayList JavaDoc<AmberExpr> args = getArgs();
83
84     int n = args.size();
85
86     // XXX: this validation should be moved to QueryParser
87
// if (n != 2)
88
// throw new QueryParseException(L.l("expected 2 string arguments for CONCAT"));
89

90     if (_parser.isDerbyDBMS()) {
91
92       // Derby does not accept two params in (? || ?).
93
// Translates to ((? || '') || ('' || ?)).
94

95       // XXX: there seems to be an issue with VARCHAR(32672)
96
// and a Derby driver patch should be released soon.
97

98       cb.append("VARCHAR(CAST(");
99
100       if (select)
101         args.get(0).generateWhere(cb);
102       else
103         args.get(0).generateUpdateWhere(cb);
104
105       cb.append("AS VARCHAR(2000)) || CAST(");
106
107       if (select)
108         args.get(1).generateWhere(cb);
109       else
110         args.get(1).generateUpdateWhere(cb);
111
112       cb.append(" AS VARCHAR(2000)))");
113
114       return;
115     }
116     else if (_parser.isPostgresDBMS()) {
117       // jpa/1230
118
generateInternalConcat(cb, true, true, null, select);
119       return;
120     }
121
122     cb.append("concat");
123
124     generateInternalConcat(cb, true, true, null, select);
125   }
126
127   private void generateInternalConcat(CharBuffer cb,
128                                       boolean arg0,
129                                       boolean arg1,
130                                       String JavaDoc str,
131                                       boolean select)
132   {
133     ArrayList JavaDoc<AmberExpr> args = getArgs();
134
135     boolean usesConcatOperator
136       = _parser.isDerbyDBMS() || _parser.isPostgresDBMS();
137
138     cb.append('(');
139
140     if (arg0) {
141       if (select)
142         args.get(0).generateWhere(cb);
143       else
144         args.get(0).generateUpdateWhere(cb);
145
146       if (usesConcatOperator)
147         cb.append(" || ");
148       else
149         cb.append(',');
150     }
151
152     if (arg1) {
153       if (select)
154         args.get(1).generateWhere(cb);
155       else
156         args.get(1).generateUpdateWhere(cb);
157
158       if (arg0) {
159         cb.append(')');
160         return;
161       }
162
163       if (usesConcatOperator)
164         cb.append(" || ");
165       else
166         cb.append(',');
167     }
168
169     cb.append('\'');
170
171     cb.append(str);
172
173     cb.append('\'');
174
175     cb.append(')');
176   }
177 }
178
Popular Tags