|
| 1 | +using System.Collections.Generic; |
| 2 | + |
| 3 | +namespace NHibernate.Engine.Query |
| 4 | +{ |
| 5 | + /// <summary> |
| 6 | + /// Implements a parameter parser recognizer specifically for the purpose |
| 7 | + /// of journaling parameter locations. |
| 8 | + /// </summary> |
| 9 | + public class ParamLocationRecognizer : ParameterParser.IRecognizer |
| 10 | + { |
| 11 | + private readonly Dictionary<string, NamedParameterDescription> namedParameterDescriptions = |
| 12 | + new Dictionary<string, NamedParameterDescription>(); |
| 13 | + |
| 14 | + private readonly List<int> ordinalParameterLocationList = new List<int>(); |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Convenience method for creating a param location recognizer and |
| 18 | + /// initiating the parse. |
| 19 | + /// </summary> |
| 20 | + /// <param name="query">The query to be parsed for parameter locations. </param> |
| 21 | + /// <returns> The generated recognizer, with journaled location info. </returns> |
| 22 | + public static ParamLocationRecognizer parseLocations(string query) |
| 23 | + { |
| 24 | + ParamLocationRecognizer recognizer = new ParamLocationRecognizer(); |
| 25 | + ParameterParser.Parse(query, recognizer); |
| 26 | + return recognizer; |
| 27 | + } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// The dictionary of named parameter locations. |
| 31 | + /// The dictionary is keyed by parameter name. |
| 32 | + /// </summary> |
| 33 | + public IDictionary<string, NamedParameterDescription> NamedParameterDescriptionMap |
| 34 | + { |
| 35 | + get { return namedParameterDescriptions; } |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// The list of ordinal parameter locations. |
| 40 | + /// </summary> |
| 41 | + /// <remarks> |
| 42 | + /// The list elements are integers, representing the location for that given ordinal. |
| 43 | + /// Thus OrdinalParameterLocationList[n] represents the location for the nth parameter. |
| 44 | + /// </remarks> |
| 45 | + public List<int> OrdinalParameterLocationList |
| 46 | + { |
| 47 | + get { return ordinalParameterLocationList; } |
| 48 | + } |
| 49 | + |
| 50 | + #region IRecognizer Members |
| 51 | + |
| 52 | + public void OutParameter(int position) |
| 53 | + { |
| 54 | + // don't care... |
| 55 | + } |
| 56 | + |
| 57 | + public void OrdinalParameter(int position) |
| 58 | + { |
| 59 | + ordinalParameterLocationList.Add(position); |
| 60 | + } |
| 61 | + |
| 62 | + public void NamedParameter(string name, int position) |
| 63 | + { |
| 64 | + GetOrBuildNamedParameterDescription(name, false).Add(position); |
| 65 | + } |
| 66 | + |
| 67 | + public void Ejb3PositionalParameter(string name, int position) |
| 68 | + { |
| 69 | + GetOrBuildNamedParameterDescription(name, true).Add(position); |
| 70 | + } |
| 71 | + |
| 72 | + public void Other(char character) |
| 73 | + { |
| 74 | + // don't care... |
| 75 | + } |
| 76 | + |
| 77 | + private NamedParameterDescription GetOrBuildNamedParameterDescription(string name, bool jpa) |
| 78 | + { |
| 79 | + NamedParameterDescription desc = namedParameterDescriptions[name]; |
| 80 | + if (desc == null) |
| 81 | + { |
| 82 | + desc = new NamedParameterDescription(jpa); |
| 83 | + namedParameterDescriptions[name] = desc; |
| 84 | + } |
| 85 | + return desc; |
| 86 | + } |
| 87 | + #endregion |
| 88 | + |
| 89 | + public class NamedParameterDescription |
| 90 | + { |
| 91 | + private readonly bool jpaStyle; |
| 92 | + private readonly List<int> positions = new List<int>(); |
| 93 | + |
| 94 | + public NamedParameterDescription(bool jpaStyle) |
| 95 | + { |
| 96 | + this.jpaStyle = jpaStyle; |
| 97 | + } |
| 98 | + |
| 99 | + internal void Add(int position) |
| 100 | + { |
| 101 | + positions.Add(position); |
| 102 | + } |
| 103 | + |
| 104 | + public int[] BuildPositionsArray() |
| 105 | + { |
| 106 | + return positions.ToArray(); |
| 107 | + } |
| 108 | + |
| 109 | + public bool JpaStyle |
| 110 | + { |
| 111 | + get { return jpaStyle; } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | +} |
0 commit comments