|
5 | 5 | using NHibernate.Engine; |
6 | 6 | using NHibernate.SqlCommand; |
7 | 7 | using NHibernate.Type; |
8 | | -using NHibernate.Util; |
9 | 8 | using System.Text.RegularExpressions; |
10 | 9 |
|
11 | 10 | namespace NHibernate.Dialect.Function |
@@ -41,6 +40,40 @@ public class AnsiTrimEmulationFunction : ISQLFunction, IFunctionGrammar |
41 | 40 | new SQLFunctionTemplate(NHibernateUtil.String, |
42 | 41 | "replace( replace( ltrim( rtrim( replace( replace( ?1, ' ', '${space}$' ), ?2, ' ' ) ) ), ' ', ?2 ), '${space}$', ' ' )"); |
43 | 42 |
|
| 43 | + private readonly ISQLFunction _leadingTrim = LeadingTrim; |
| 44 | + private readonly ISQLFunction _trailingTrim = TrailingTrim; |
| 45 | + private readonly ISQLFunction _bothTrim = BothTrim; |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Default constructor. The target database has to support the <c>replace</c> function. |
| 49 | + /// </summary> |
| 50 | + public AnsiTrimEmulationFunction() |
| 51 | + { |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Constructor for supplying the name of the replace function to use. |
| 56 | + /// </summary> |
| 57 | + /// <param name="replaceFunction">The replace function.</param> |
| 58 | + public AnsiTrimEmulationFunction(string replaceFunction) |
| 59 | + { |
| 60 | + _leadingTrim = |
| 61 | + new SQLFunctionTemplate( |
| 62 | + NHibernateUtil.String, |
| 63 | + $"{replaceFunction}( {replaceFunction}( ltrim( {replaceFunction}( {replaceFunction}( ?1, ' ', " + |
| 64 | + "'${space}$' ), ?2, ' ' ) ), ' ', ?2 ), '${space}$', ' ' )"); |
| 65 | + _trailingTrim = |
| 66 | + new SQLFunctionTemplate( |
| 67 | + NHibernateUtil.String, |
| 68 | + $"{replaceFunction}( {replaceFunction}( rtrim( {replaceFunction}( {replaceFunction}( ?1, ' ', " + |
| 69 | + "'${space}$' ), ?2, ' ' ) ), ' ', ?2 ), '${space}$', ' ' )"); |
| 70 | + _bothTrim = |
| 71 | + new SQLFunctionTemplate( |
| 72 | + NHibernateUtil.String, |
| 73 | + $"{replaceFunction}( {replaceFunction}( ltrim( rtrim( {replaceFunction}( {replaceFunction}( ?1, ' ', " + |
| 74 | + "'${space}$' ), ?2, ' ' ) ) ), ' ', ?2 ), '${space}$', ' ' )"); |
| 75 | + } |
| 76 | + |
44 | 77 | #region ISQLFunction Members |
45 | 78 |
|
46 | 79 | public IType ReturnType(IType columnType, IMapping mapping) |
@@ -174,18 +207,18 @@ public SqlString Render(IList args, ISessionFactoryImplementor factory) |
174 | 207 |
|
175 | 208 | return TrailingSpaceTrim.Render(argsToUse, factory); |
176 | 209 | } |
177 | | - |
| 210 | + |
178 | 211 | if (leading && trailing) |
179 | 212 | { |
180 | | - return BothTrim.Render(argsToUse, factory); |
| 213 | + return _bothTrim.Render(argsToUse, factory); |
181 | 214 | } |
182 | | - |
| 215 | + |
183 | 216 | if (leading) |
184 | 217 | { |
185 | | - return LeadingTrim.Render(argsToUse, factory); |
| 218 | + return _leadingTrim.Render(argsToUse, factory); |
186 | 219 | } |
187 | | - |
188 | | - return TrailingTrim.Render(argsToUse, factory); |
| 220 | + |
| 221 | + return _trailingTrim.Render(argsToUse, factory); |
189 | 222 | } |
190 | 223 |
|
191 | 224 | #endregion |
|
0 commit comments