| | 1 | | // Copyright (c) Microsoft. All rights reserved. |
| | 2 | | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.ServiceBus |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Globalization; |
| | 9 | | using Primitives; |
| | 10 | |
|
| | 11 | | /// <summary> |
| | 12 | | /// Represents set of actions written in SQL language-based syntax that is performed against a <see cref="Message" / |
| | 13 | | /// </summary> |
| | 14 | | public sealed class SqlRuleAction : RuleAction |
| | 15 | | { |
| | 16 | | internal PropertyDictionary parameters; |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Initializes a new instance of the <see cref="SqlRuleAction" /> class with the specified SQL expression. |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="sqlExpression">The SQL expression.</param> |
| | 22 | | /// <remarks>Max allowed length of sql expression is 1024 chars.</remarks> |
| 0 | 23 | | public SqlRuleAction(string sqlExpression) |
| | 24 | | { |
| 0 | 25 | | if (string.IsNullOrEmpty(sqlExpression)) |
| | 26 | | { |
| 0 | 27 | | throw Fx.Exception.ArgumentNullOrWhiteSpace(nameof(sqlExpression)); |
| | 28 | | } |
| | 29 | |
|
| 0 | 30 | | if (sqlExpression.Length > Constants.MaximumSqlRuleActionStatementLength) |
| | 31 | | { |
| 0 | 32 | | throw Fx.Exception.Argument( |
| 0 | 33 | | nameof(sqlExpression), |
| 0 | 34 | | Resources.SqlFilterActionStatmentTooLong.FormatForUser( |
| 0 | 35 | | sqlExpression.Length, |
| 0 | 36 | | Constants.MaximumSqlRuleActionStatementLength)); |
| | 37 | | } |
| | 38 | |
|
| 0 | 39 | | this.SqlExpression = sqlExpression; |
| 0 | 40 | | } |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Gets the SQL expression. |
| | 44 | | /// </summary> |
| | 45 | | /// <value>The SQL expression.</value> |
| | 46 | | /// <remarks>Max allowed length of sql expression is 1024 chars.</remarks> |
| 0 | 47 | | public string SqlExpression { get; } |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// Sets the value of a rule action. |
| | 51 | | /// </summary> |
| | 52 | | /// <value>The value of a rule action.</value> |
| 0 | 53 | | public IDictionary<string, object> Parameters => this.parameters ?? (this.parameters = new PropertyDictionary()) |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// Returns a string representation of <see cref="SqlRuleAction" />. |
| | 57 | | /// </summary> |
| | 58 | | /// <returns>The string representation of <see cref="SqlRuleAction" />.</returns> |
| | 59 | | public override string ToString() |
| | 60 | | { |
| 0 | 61 | | return string.Format(CultureInfo.InvariantCulture, "SqlRuleAction: {0}", this.SqlExpression); |
| | 62 | | } |
| | 63 | |
|
| | 64 | | public override int GetHashCode() |
| | 65 | | { |
| 0 | 66 | | return this.SqlExpression?.GetHashCode() ?? base.GetHashCode(); |
| | 67 | | } |
| | 68 | |
|
| | 69 | | public override bool Equals(object obj) |
| | 70 | | { |
| 0 | 71 | | var other = obj as RuleAction; |
| 0 | 72 | | return this.Equals(other); |
| | 73 | | } |
| | 74 | |
|
| | 75 | | public override bool Equals(RuleAction other) |
| | 76 | | { |
| 0 | 77 | | if (other is SqlRuleAction sqlAction) |
| | 78 | | { |
| 0 | 79 | | if (string.Equals(this.SqlExpression, sqlAction.SqlExpression, StringComparison.OrdinalIgnoreCase) |
| 0 | 80 | | && (this.parameters != null && sqlAction.parameters != null |
| 0 | 81 | | || this.parameters == null && sqlAction.parameters == null)) |
| | 82 | | { |
| 0 | 83 | | if (this.parameters != null) |
| | 84 | | { |
| 0 | 85 | | if (this.parameters.Count != sqlAction.parameters.Count) |
| | 86 | | { |
| 0 | 87 | | return false; |
| | 88 | | } |
| | 89 | |
|
| 0 | 90 | | foreach (var param in this.parameters) |
| | 91 | | { |
| 0 | 92 | | if (!sqlAction.parameters.TryGetValue(param.Key, out var otherParamValue) || |
| 0 | 93 | | (param.Value == null ^ otherParamValue == null) || |
| 0 | 94 | | (param.Value != null && !param.Value.Equals(otherParamValue))) |
| | 95 | | { |
| 0 | 96 | | return false; |
| | 97 | | } |
| | 98 | | } |
| | 99 | | } |
| | 100 | |
|
| 0 | 101 | | return true; |
| | 102 | | } |
| | 103 | | } |
| | 104 | |
|
| 0 | 105 | | return false; |
| 0 | 106 | | } |
| | 107 | |
|
| | 108 | | public static bool operator ==(SqlRuleAction o1, SqlRuleAction o2) |
| | 109 | | { |
| 0 | 110 | | if (ReferenceEquals(o1, o2)) |
| | 111 | | { |
| 0 | 112 | | return true; |
| | 113 | | } |
| | 114 | |
|
| 0 | 115 | | if (ReferenceEquals(o1, null) || ReferenceEquals(o2, null)) |
| | 116 | | { |
| 0 | 117 | | return false; |
| | 118 | | } |
| | 119 | |
|
| 0 | 120 | | return o1.Equals(o2); |
| | 121 | | } |
| | 122 | |
|
| | 123 | | public static bool operator !=(SqlRuleAction o1, SqlRuleAction o2) |
| | 124 | | { |
| 0 | 125 | | return !(o1 == o2); |
| | 126 | | } |
| | 127 | | } |
| | 128 | | } |