1 package org.hibernate.dialect.function; 3 4 import java.util.List ; 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 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 render(List args, SessionFactoryImplementor factory) throws QueryException { 31 boolean threeArgs = args.size() > 2; 32 Object pattern = args.get(0); 33 Object string = args.get(1); 34 Object start = threeArgs ? args.get(2) : null; 35 36 StringBuffer buf = new StringBuffer (); 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 |