KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: PositionSubstringFunction.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.Hibernate;
7 import org.hibernate.QueryException;
8 import org.hibernate.engine.Mapping;
9 import org.hibernate.engine.SessionFactoryImplementor;
10 import org.hibernate.type.Type;
11
12 /**
13  * Emulation of <tt>locate()</tt> on PostgreSQL
14  * @author Gavin King
15  */

16 public class PositionSubstringFunction implements SQLFunction {
17
18     public Type getReturnType(Type columnType, Mapping mapping) throws QueryException {
19         return Hibernate.INTEGER;
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         boolean threeArgs = args.size() > 2;
32         Object JavaDoc pattern = args.get(0);
33         Object JavaDoc string = args.get(1);
34         Object JavaDoc start = threeArgs ? args.get(2) : null;
35
36         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
37         if (threeArgs) buf.append('(');
38         buf.append("position(").append( pattern ).append(" in ");
39         if (threeArgs) buf.append( "substring(");
40         buf.append( string );
41         if (threeArgs) buf.append( ", " ).append( start ).append(')');
42         buf.append(')');
43         if (threeArgs) buf.append('+').append( start ).append("-1)");
44         return buf.toString();
45     }
46
47
48 }
49
Popular Tags