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