-
-
Notifications
You must be signed in to change notification settings - Fork 524
Expand file tree
/
Copy pathQuery.Update.cs
More file actions
75 lines (61 loc) · 2.08 KB
/
Query.Update.cs
File metadata and controls
75 lines (61 loc) · 2.08 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace SqlKata
{
public partial class Query
{
public Query AsUpdate(object data)
{
var dictionary = BuildKeyValuePairsFromObject(data, considerKeys: true, insert: false);
return AsUpdate(dictionary);
}
public Query AsUpdate(IEnumerable<string> columns, IEnumerable<object> values)
{
if ((columns?.Any() ?? false) == false || (values?.Any() ?? false) == false)
{
throw new InvalidOperationException($"{columns} and {values} cannot be null or empty");
}
if (columns.Count() != values.Count())
{
throw new InvalidOperationException($"{columns} count should be equal to {values} count");
}
Method = "update";
ClearComponent("update").AddComponent("update", new InsertClause
{
Columns = columns.ToList(),
Values = values.ToList()
});
return this;
}
public Query AsUpdate(IEnumerable<KeyValuePair<string, object>> values)
{
if (values == null || values.Any() == false)
{
throw new InvalidOperationException($"{values} cannot be null or empty");
}
Method = "update";
ClearComponent("update").AddComponent("update", new InsertClause
{
Columns = values.Select(x => x.Key).ToList(),
Values = values.Select(x => x.Value).ToList(),
});
return this;
}
public Query AsIncrement(string column, int value = 1)
{
Method = "update";
AddOrReplaceComponent("update", new IncrementClause
{
Column = column,
Value = value
});
return this;
}
public Query AsDecrement(string column, int value = 1)
{
return AsIncrement(column, -value);
}
}
}