KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > dialect > function > NvlFunction


1 //$Id: NvlFunction.java,v 1.2 2005/04/29 15:32:30 oneovthafew Exp $
2
package org.hibernate.dialect.function;
3
4 import java.util.List JavaDoc;
5
6 import org.hibernate.QueryException;
7 import org.hibernate.engine.Mapping;
8 import org.hibernate.engine.SessionFactoryImplementor;
9 import org.hibernate.type.Type;
10
11 /**
12  * Emulation of <tt>coalesce()</tt> on Oracle, using multiple
13  * <tt>nvl()</tt> calls
14  * @author Gavin King
15  */

16 public class NvlFunction implements SQLFunction {
17
18     public Type getReturnType(Type columnType, Mapping mapping) throws QueryException {
19         return columnType;
20     }
21
22     public boolean hasArguments() {
23         return true;
24     }
25
26     public boolean hasParenthesesIfNoArguments() {
27         return true;
28     }
29
30     public String JavaDoc render(List JavaDoc args, SessionFactoryImplementor factory) throws QueryException {
31         int lastIndex = args.size()-1;
32         Object JavaDoc last = args.remove(lastIndex);
33         if ( lastIndex==0 ) return last.toString();
34         Object JavaDoc secondLast = args.get(lastIndex-1);
35         String JavaDoc nvl = "nvl(" + secondLast + ", " + last + ")";
36         args.set(lastIndex-1, nvl);
37         return render(args, factory);
38     }
39
40     
41
42 }
43
Popular Tags