-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInsertIntoQuery.cs
74 lines (60 loc) · 2.38 KB
/
InsertIntoQuery.cs
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
using Queries.Core.Builders.Fluent;
using Queries.Core.Parts.Columns;
using System;
namespace Queries.Core.Builders;
/// <summary>
/// A query to insert data
/// </summary>
public class InsertIntoQuery : IInsertIntoQuery<InsertIntoQuery>, IBuild<InsertIntoQuery>, IEquatable<InsertIntoQuery>
{
/// <summary>
/// Values to insert
/// </summary>
public IInsertable InsertedValue { get; private set; }
/// <summary>
/// Name of the element where to insert <see cref="InsertedValue"/>
/// </summary>
public string TableName { get; }
/// <summary>
/// Creates a new <see cref="InsertIntoQuery"/>
/// </summary>
/// <param name="tableName">name of the table the INSERT INTO will be made for</param>
/// <exception cref="ArgumentNullException">if <paramref name="tableName"/> is <see langword="null" />.</exception>
public InsertIntoQuery(string tableName) => TableName = tableName ?? throw new ArgumentNullException(nameof(tableName));
/// <summary>
/// Defines values to insert as a <see cref="SelectQuery"/>.
/// </summary>
/// <param name="select">The query from whi</param>
/// <returns></returns>
public IBuild<InsertIntoQuery> Values(SelectQuery select)
{
InsertedValue = select;
return this;
}
/// <summary>
/// Defines the VALUES the current query will insert
/// </summary>
/// <param name="value">First value to insert</param>
/// <param name="values">Additional values to insert</param>
/// <returns></returns>
public IBuild<InsertIntoQuery> Values(InsertedValue value, params InsertedValue[] values)
{
InsertedValues insertedValues = new() { value };
foreach (InsertedValue insertedValue in values)
{
insertedValues.Add(insertedValue);
}
InsertedValue = insertedValues;
return this;
}
///<inheritdoc/>
public InsertIntoQuery Build() => this;
///<inheritdoc/>
public override bool Equals(object obj) => Equals(obj as InsertIntoQuery);
///<inheritdoc/>
public bool Equals(InsertIntoQuery other) => other is not null
&& TableName == other.TableName
&& InsertedValue.Equals(other.InsertedValue);
///<inheritdoc/>
public override int GetHashCode() => (InsertedValue, TableName).GetHashCode();
}