| | 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 a filter which is a composition of an expression and an action that is executed in the pub/sub pipeli |
| | 14 | | /// </summary> |
| | 15 | | /// <remarks> |
| | 16 | | /// A <see cref="SqlRuleFilter"/> holds a SQL-like condition expression that is evaluated in the broker against the |
| | 17 | | /// user-defined properties and system properties. All system properties (which are all properties explicitly listed |
| | 18 | | /// on the <see cref="ServiceBusMessage"/> class) must be prefixed with <code>sys.</code> in the condition expressio |
| | 19 | | /// testing for existence of properties (EXISTS), testing for null-values (IS NULL), logical NOT/AND/OR, relational |
| | 20 | | /// numeric arithmetic, and simple text pattern matching with LIKE. |
| | 21 | | /// </remarks> |
| | 22 | | public class SqlRuleFilter : RuleFilter |
| | 23 | | { |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Initializes a new instance of the <see cref="SqlRuleFilter" /> class using the specified SQL expression. |
| | 27 | | /// </summary> |
| | 28 | | /// <remarks>Max allowed length of sql expression is 1024 chars.</remarks> |
| 64 | 29 | | public SqlRuleFilter(string sqlExpression) |
| | 30 | | { |
| 64 | 31 | | Argument.AssertNotNullOrEmpty(sqlExpression, nameof(sqlExpression)); |
| 60 | 32 | | Argument.AssertNotTooLong(sqlExpression, Constants.MaximumSqlRuleFilterStatementLength, nameof(sqlExpression |
| | 33 | |
|
| 58 | 34 | | SqlExpression = sqlExpression; |
| 58 | 35 | | } |
| | 36 | |
|
| | 37 | | internal override RuleFilter Clone() => |
| 8 | 38 | | new SqlRuleFilter(SqlExpression) |
| 8 | 39 | | { |
| 8 | 40 | | Parameters = (Parameters as PropertyDictionary).Clone() |
| 8 | 41 | | }; |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Gets the SQL expression. |
| | 45 | | /// </summary> |
| | 46 | | /// <value>The SQL expression.</value> |
| | 47 | | /// <remarks>Max allowed length of sql expression is 1024 chars.</remarks> |
| 68 | 48 | | public string SqlExpression { get; } |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Sets the value of a filter expression. |
| | 52 | | /// Allowed types: string, int, long, bool, double |
| | 53 | | /// </summary> |
| | 54 | | /// <value>The value of a filter expression.</value> |
| 380 | 55 | | public IDictionary<string, object> Parameters { get; internal set; } = new PropertyDictionary(); |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Returns a string representation of <see cref="SqlRuleFilter" />. |
| | 59 | | /// </summary> |
| | 60 | | /// <returns>The string representation of <see cref="SqlRuleFilter" />.</returns> |
| | 61 | | public override string ToString() |
| | 62 | | { |
| 0 | 63 | | return string.Format(CultureInfo.InvariantCulture, "SqlRuleFilter: {0}", SqlExpression); |
| | 64 | | } |
| | 65 | |
|
| | 66 | | /// <inheritdoc/> |
| | 67 | | public override int GetHashCode() |
| | 68 | | { |
| 0 | 69 | | return SqlExpression?.GetHashCode() ?? base.GetHashCode(); |
| | 70 | | } |
| | 71 | |
|
| | 72 | | /// <inheritdoc/> |
| | 73 | | public override bool Equals(object obj) |
| | 74 | | { |
| 0 | 75 | | var other = obj as RuleFilter; |
| 0 | 76 | | return Equals(other); |
| | 77 | | } |
| | 78 | |
|
| | 79 | | /// <inheritdoc/> |
| | 80 | | public override bool Equals(RuleFilter other) |
| | 81 | | { |
| 12 | 82 | | if (other is SqlRuleFilter sqlRuleFilter) |
| | 83 | | { |
| 12 | 84 | | if (string.Equals(SqlExpression, sqlRuleFilter.SqlExpression, StringComparison.OrdinalIgnoreCase)) |
| | 85 | | { |
| 12 | 86 | | if (Parameters.Count != sqlRuleFilter.Parameters.Count) |
| | 87 | | { |
| 0 | 88 | | return false; |
| | 89 | | } |
| | 90 | |
|
| 152 | 91 | | foreach (var param in Parameters) |
| | 92 | | { |
| 64 | 93 | | if (!sqlRuleFilter.Parameters.TryGetValue(param.Key, out var otherParamValue) || |
| 64 | 94 | | (param.Value == null ^ otherParamValue == null) || |
| 64 | 95 | | (param.Value != null && !param.Value.Equals(otherParamValue))) |
| | 96 | | { |
| 0 | 97 | | return false; |
| | 98 | | } |
| | 99 | | } |
| | 100 | |
|
| 12 | 101 | | return true; |
| | 102 | | } |
| | 103 | | } |
| | 104 | |
|
| 0 | 105 | | return false; |
| 0 | 106 | | } |
| | 107 | |
|
| | 108 | | /// <summary> |
| | 109 | | /// |
| | 110 | | /// </summary> |
| | 111 | | /// <param name="left"></param> |
| | 112 | | /// <param name="right"></param> |
| | 113 | | /// <returns></returns> |
| | 114 | | public static bool operator ==(SqlRuleFilter left, SqlRuleFilter right) |
| | 115 | | { |
| 0 | 116 | | if (ReferenceEquals(left, right)) |
| | 117 | | { |
| 0 | 118 | | return true; |
| | 119 | | } |
| | 120 | |
|
| 0 | 121 | | if (ReferenceEquals(left, null) || ReferenceEquals(right, null)) |
| | 122 | | { |
| 0 | 123 | | return false; |
| | 124 | | } |
| | 125 | |
|
| 0 | 126 | | return left.Equals(right); |
| | 127 | | } |
| | 128 | |
|
| | 129 | | /// <summary> |
| | 130 | | /// |
| | 131 | | /// </summary> |
| | 132 | | /// <param name="left"></param> |
| | 133 | | /// <param name="right"></param> |
| | 134 | | /// <returns></returns> |
| | 135 | | public static bool operator !=(SqlRuleFilter left, SqlRuleFilter right) |
| | 136 | | { |
| 0 | 137 | | return !(left == right); |
| | 138 | | } |
| | 139 | | } |
| | 140 | | } |