|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using NHibernate.Dialect.Function; |
| 4 | +using NHibernate.Engine; |
| 5 | +using NHibernate.SqlCommand; |
| 6 | +using NHibernate.Type; |
| 7 | + |
| 8 | +namespace NHibernate.Dialect |
| 9 | +{ |
| 10 | + [Serializable] |
| 11 | + public class BitwiseFunctionOperation : ISQLFunction |
| 12 | + { |
| 13 | + private readonly string _functionName; |
| 14 | + private SqlStringBuilder _sqlBuffer; |
| 15 | + private Queue _args; |
| 16 | + |
| 17 | + public BitwiseFunctionOperation(string functionName) |
| 18 | + { |
| 19 | + _functionName = functionName; |
| 20 | + } |
| 21 | + |
| 22 | + #region ISQLFunction Members |
| 23 | + |
| 24 | + public IType ReturnType(IType columnType, IMapping mapping) |
| 25 | + { |
| 26 | + return NHibernateUtil.Int64; |
| 27 | + } |
| 28 | + |
| 29 | + public bool HasArguments |
| 30 | + { |
| 31 | + get { return true; } |
| 32 | + } |
| 33 | + |
| 34 | + public bool HasParenthesesIfNoArguments |
| 35 | + { |
| 36 | + get { return true; } |
| 37 | + } |
| 38 | + |
| 39 | + public SqlString Render(IList args, ISessionFactoryImplementor factory) |
| 40 | + { |
| 41 | + Prepare(args); |
| 42 | + |
| 43 | + Function(); |
| 44 | + OpenParens(); |
| 45 | + Arguments(); |
| 46 | + CloseParens(); |
| 47 | + |
| 48 | + return _sqlBuffer.ToSqlString(); |
| 49 | + } |
| 50 | + |
| 51 | + #endregion |
| 52 | + |
| 53 | + private void Prepare(IList args) |
| 54 | + { |
| 55 | + _sqlBuffer = new SqlStringBuilder(); |
| 56 | + _args = new Queue(); |
| 57 | + foreach (var arg in args) |
| 58 | + { |
| 59 | + if (!IsParens(arg.ToString())) |
| 60 | + _args.Enqueue(arg); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private static bool IsParens(string candidate) |
| 65 | + { |
| 66 | + return candidate == "(" || candidate == ")"; |
| 67 | + } |
| 68 | + |
| 69 | + private void Function() |
| 70 | + { |
| 71 | + _sqlBuffer.Add(_functionName); |
| 72 | + } |
| 73 | + |
| 74 | + private void OpenParens() |
| 75 | + { |
| 76 | + _sqlBuffer.Add("("); |
| 77 | + } |
| 78 | + |
| 79 | + private void Arguments() |
| 80 | + { |
| 81 | + while (_args.Count > 0) |
| 82 | + { |
| 83 | + var arg = _args.Dequeue(); |
| 84 | + if (arg is Parameter || arg is SqlString) |
| 85 | + _sqlBuffer.AddObject(arg); |
| 86 | + else |
| 87 | + _sqlBuffer.Add(arg.ToString()); |
| 88 | + if (_args.Count > 0) |
| 89 | + _sqlBuffer.Add(", "); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private void CloseParens() |
| 94 | + { |
| 95 | + _sqlBuffer.Add(")"); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments