-
-
Notifications
You must be signed in to change notification settings - Fork 512
/
Copy pathQuery.Insert.cs
119 lines (97 loc) · 3.69 KB
/
Query.Insert.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
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
115
116
117
118
119
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace SqlKata
{
public partial class Query
{
public Query AsInsert(object data, bool returnId = false)
{
var propertiesKeyValues = BuildKeyValuePairsFromObject(data);
return AsInsert(propertiesKeyValues, returnId);
}
public Query AsInsert(IEnumerable<string> columns, IEnumerable<object> values)
{
var columnsList = columns?.ToList();
var valuesList = values?.ToList();
if ((columnsList?.Count ?? 0) == 0 || (valuesList?.Count ?? 0) == 0)
{
throw new InvalidOperationException($"{nameof(columns)} and {nameof(values)} cannot be null or empty");
}
if (columnsList.Count != valuesList.Count)
{
throw new InvalidOperationException($"{nameof(columns)} and {nameof(values)} cannot be null or empty");
}
Method = "insert";
ClearComponent("insert").AddComponent("insert", new InsertClause
{
Columns = columnsList,
Values = valuesList
});
return this;
}
public Query AsInsert(IEnumerable<KeyValuePair<string, object>> values, bool returnId = false)
{
if (values == null || values.Any() == false)
{
throw new InvalidOperationException($"{values} argument cannot be null or empty");
}
Method = "insert";
ClearComponent("insert").AddComponent("insert", new InsertClause
{
Columns = values.Select(x => x.Key).ToList(),
Values = values.Select(x => x.Value).ToList(),
ReturnId = returnId,
});
return this;
}
/// <summary>
/// Produces insert multi records
/// </summary>
/// <param name="columns"></param>
/// <param name="rowsValues"></param>
/// <returns></returns>
public Query AsInsert(IEnumerable<string> columns, IEnumerable<IEnumerable<object>> rowsValues)
{
var columnsList = columns?.ToList();
var valuesCollectionList = rowsValues?.ToList();
if ((columnsList?.Count ?? 0) == 0 || (valuesCollectionList?.Count ?? 0) == 0)
{
throw new InvalidOperationException($"{nameof(columns)} and {nameof(rowsValues)} cannot be null or empty");
}
Method = "insert";
ClearComponent("insert");
foreach (var values in valuesCollectionList)
{
var valuesList = values.ToList();
if (columnsList.Count != valuesList.Count)
{
throw new InvalidOperationException($"{nameof(columns)} count should be equal to each {nameof(rowsValues)} entry count");
}
AddComponent("insert", new InsertClause
{
Columns = columnsList,
Values = valuesList
});
}
return this;
}
/// <summary>
/// Produces insert from subquery
/// </summary>
/// <param name="columns"></param>
/// <param name="query"></param>
/// <returns></returns>
public Query AsInsert(IEnumerable<string> columns, Query query)
{
Method = "insert";
ClearComponent("insert").AddComponent("insert", new InsertQueryClause
{
Columns = columns.ToList(),
Query = query.Clone(),
});
return this;
}
}
}