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