-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUpdateQuery.cs
More file actions
114 lines (95 loc) · 3.51 KB
/
UpdateQuery.cs
File metadata and controls
114 lines (95 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System.Collections.Generic;
using Queries.Core.Parts;
using Queries.Core.Parts.Clauses;
using System;
using Queries.Core.Attributes;
using System.Linq;
#if !SYSTEM_TEXT_JSON
using Newtonsoft.Json;
#endif
namespace Queries.Core.Builders;
/// <summary>
/// A query to update a table
/// </summary>
#if !SYSTEM_TEXT_JSON
[JsonObject]
#endif
[DataManipulationLanguage]
public class UpdateQuery : IQuery, IEquatable<UpdateQuery>
{
/// <summary>
/// Table to update
/// </summary>
public Table Table { get; }
/// <summary>
/// Collection of values
/// </summary>
public IList<UpdateFieldValue> Values { get; private set; }
/// <summary>
/// Criteria associated with the current instance.
/// </summary>
public IWhereClause Criteria { get; set; }
/// <summary>
/// Builds a new <see cref="UpdateQuery"/> instance.
/// </summary>
/// <param name="tableName"></param>
/// <exception cref="ArgumentNullException"><paramref name="tableName"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="tableName"/> is empty or only contains whitespaces.</exception>
public UpdateQuery(string tableName)
{
if (tableName is null)
{
throw new ArgumentNullException(nameof(tableName), $"{nameof(tableName)} cannot be null");
}
if (string.IsNullOrWhiteSpace(tableName))
{
throw new ArgumentOutOfRangeException(nameof(tableName), tableName, $"{nameof(tableName)} cannot be empty or whitespace");
}
Table = tableName.Table();
Values = new List<UpdateFieldValue>();
}
/// <summary>
/// Builds a new <see cref="UpdateQuery"/> instance.
/// </summary>
/// <param name="table"></param>
public UpdateQuery(Table table) : this(table?.Name)
{
}
/// <summary>
/// Defines the <c>SET</c> part of the current instance.
/// </summary>
/// <param name="newValues"></param>
/// <returns>the current instance for further processing</returns>
public UpdateQuery Set(params UpdateFieldValue[] newValues)
{
Values = newValues;
return this;
}
/// <summary>
/// Adds the specified <see cref="IWhereClause"/> to the current instance
/// </summary>
/// <param name="clause"></param>
/// <returns>The current <see cref="UpdateQuery"/> for further processing</returns>
public UpdateQuery Where(IWhereClause clause)
{
Criteria = clause;
return this;
}
///<inheritdoc/>
public override bool Equals(object obj) => Equals(obj as UpdateQuery);
///<inheritdoc/>
public bool Equals(UpdateQuery other) => other is not null
&& ((Table == null && other.Table == null) || Table.Equals(other.Table))
&& Values.SequenceEqual(other.Values)
&& ((Criteria == null && other.Criteria == null) || Criteria.Equals(other.Criteria));
///<inheritdoc/>
public override int GetHashCode()
{
int hashCode = -1291674402;
hashCode = (hashCode * -1521134295) + EqualityComparer<Table>.Default.GetHashCode(Table);
hashCode = (hashCode * -1521134295) + EqualityComparer<IList<UpdateFieldValue>>.Default.GetHashCode(Values);
return (hashCode * -1521134295) + EqualityComparer<IWhereClause>.Default.GetHashCode(Criteria);
}
///<inheritdoc/>
public override string ToString() => this.Jsonify();
}