Advertisement
Guest User

StringLength

a guest
Mar 10th, 2012
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.11 KB | None | 0 0
  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  * http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.    
  18.  */
  19. package org.apache.openjpa.jdbc.kernel.exps;
  20.  
  21. import org.apache.openjpa.jdbc.sql.DBDictionary;
  22. import org.apache.openjpa.jdbc.sql.SQLBuffer;
  23. import org.apache.openjpa.jdbc.sql.Select;
  24.  
  25. /**
  26.  * Returns the number of characters in a string.
  27.  *
  28.  * @author Marc Prud'hommeaux
  29.  */
  30. public class StringLength
  31.     extends StringFunction {
  32.  
  33.     private Class _cast = null;
  34.  
  35.     /**
  36.      * Constructor. Provide the string to operate on.
  37.      */
  38.     public StringLength(Val val) {
  39.         super(val);
  40.     }
  41.  
  42.     public Class getType() {
  43.         if (_cast != null)
  44.             return _cast;
  45.         return int.class;
  46.     }
  47.  
  48.     public void setImplicitType(Class type) {
  49.         _cast = type;
  50.     }
  51.  
  52.     public void appendTo(Select sel, ExpContext ctx, ExpState state,
  53.         SQLBuffer buf, int index) {
  54.         DBDictionary dict = ctx.store.getDBDictionary();
  55.         String func = dict.stringLengthFunction;
  56.         dict.assertSupport(func != null, "StringLengthFunction");
  57.         func = dict.getCastFunction(getValue(), func);
  58.        
  59.         int idx = func.indexOf("{0}");
  60.         buf.append(func.substring(0, idx));
  61.         getValue().appendTo(sel, ctx, state, buf, index);
  62.         buf.append(func.substring(idx + 3));
  63.     }
  64.  
  65.     public int getId() {
  66.         return Val.LENGTH_VAL;
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement