1 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 ; 37 38 39 42 public class TrimFunExpr extends FunExpr { 43 private static final L10N L = new L10N(TrimFunExpr.class); 44 45 public enum TrimSemantics { LEADING, TRAILING, BOTH } 46 47 private TrimSemantics _trimSemantics = TrimSemantics.BOTH; 48 49 private AmberExpr _trimChar; 50 51 54 protected TrimFunExpr(QueryParser parser, 55 ArrayList <AmberExpr> args) 56 { 57 super(parser, "trim", args, false); 58 } 59 60 public static TrimFunExpr create(QueryParser parser, 61 ArrayList <AmberExpr> args) 62 { 63 return new TrimFunExpr(parser, args); 64 } 65 66 69 public void setTrimChar(AmberExpr trimChar) 70 { 71 _trimChar = trimChar; 72 } 73 74 77 public void setTrimSemantics(TrimSemantics trimSemantics) 78 { 79 _trimSemantics = trimSemantics; 80 } 81 82 85 public void generateWhere(CharBuffer cb) 86 { 87 generateInternalWhere(cb, true); 88 } 89 90 93 public void generateUpdateWhere(CharBuffer cb) 94 { 95 generateInternalWhere(cb, false); 96 } 97 98 101 void generateInternalWhere(CharBuffer cb, 102 boolean select) 103 { 104 ArrayList <AmberExpr> args = getArgs(); 105 106 int n = args.size(); 107 108 112 if (_parser.isDerbyDBMS()) { 113 114 116 switch (_trimSemantics) { 117 118 case LEADING: 119 cb.append("ltrim("); 120 break; 121 122 case TRAILING: 123 cb.append("rtrim("); 124 break; 125 126 default: 127 cb.append("ltrim(rtrim("); 128 } 129 130 if (_trimChar != null) { 131 if (select) 132 _trimChar.generateWhere(cb); 133 else 134 _trimChar.generateUpdateWhere(cb); 135 136 cb.append(" from "); 137 } 138 139 if (select) 140 args.get(0).generateWhere(cb); 141 else 142 args.get(0).generateUpdateWhere(cb); 143 144 cb.append(')'); 145 146 if (_trimSemantics == TrimSemantics.BOTH) 147 cb.append(')'); 148 149 return; 150 } 151 152 cb.append("trim("); 153 154 switch (_trimSemantics) { 155 156 case LEADING: 157 cb.append("leading "); 158 break; 159 160 case TRAILING: 161 cb.append("trailing "); 162 break; 163 164 default: 165 cb.append("both "); 166 } 167 168 if (_trimChar != null) { 169 if (select) 170 _trimChar.generateWhere(cb); 171 else 172 _trimChar.generateUpdateWhere(cb); 173 } 174 175 cb.append(" from "); 176 177 if (select) 178 args.get(0).generateWhere(cb); 179 else 180 args.get(0).generateUpdateWhere(cb); 181 182 cb.append(")"); 183 } 184 } 185 | Popular Tags |