forked from PhenX/PhenX.EntityFrameworkCore.BulkInsert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqliteBulkInsertProvider.cs
More file actions
190 lines (159 loc) · 6.23 KB
/
SqliteBulkInsertProvider.cs
File metadata and controls
190 lines (159 loc) · 6.23 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System.Data.Common;
using JetBrains.Annotations;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using PhenX.EntityFrameworkCore.BulkInsert.Metadata;
using PhenX.EntityFrameworkCore.BulkInsert.Options;
namespace PhenX.EntityFrameworkCore.BulkInsert.Sqlite;
[UsedImplicitly]
internal class SqliteBulkInsertProvider : BulkInsertProviderBase<SqliteDialectBuilder>
{
public SqliteBulkInsertProvider(ILogger<SqliteBulkInsertProvider>? logger = null) : base(logger)
{
}
/// <inheritdoc />
protected override string BulkInsertId => "rowid";
//language=sql
/// <inheritdoc />
protected override string CreateTableCopySql => "CREATE TEMP TABLE {0} AS SELECT * FROM {1} WHERE 0;";
//language=sql
/// <inheritdoc />
protected override string AddTableCopyBulkInsertId => "--"; // No need to add an ID column in SQLite
/// <inheritdoc />
protected override Task AddBulkInsertIdColumn<T>(
bool sync,
DbContext context,
string tempTableName,
CancellationToken cancellationToken
) where T : class => Task.CompletedTask;
/// <summary>
/// Taken from https://github.com/dotnet/efcore/blob/667c569c49a1ab7e142621395d3f14f2af0508b4/src/Microsoft.Data.Sqlite.Core/SqliteValueBinder.cs#L231
/// As the method is not exposed in the public API, we need to copy it here.
/// </summary>
private static readonly Dictionary<Type, SqliteType> SqliteTypeMapping =
new()
{
{ typeof(bool), SqliteType.Integer },
{ typeof(byte), SqliteType.Integer },
{ typeof(byte[]), SqliteType.Blob },
{ typeof(char), SqliteType.Text },
{ typeof(DateTime), SqliteType.Text },
{ typeof(DateTimeOffset), SqliteType.Text },
{ typeof(DateOnly), SqliteType.Text },
{ typeof(TimeOnly), SqliteType.Text },
{ typeof(DBNull), SqliteType.Text },
{ typeof(decimal), SqliteType.Text },
{ typeof(double), SqliteType.Real },
{ typeof(float), SqliteType.Real },
{ typeof(Guid), SqliteType.Text },
{ typeof(int), SqliteType.Integer },
{ typeof(long), SqliteType.Integer },
{ typeof(sbyte), SqliteType.Integer },
{ typeof(short), SqliteType.Integer },
{ typeof(string), SqliteType.Text },
{ typeof(TimeSpan), SqliteType.Text },
{ typeof(uint), SqliteType.Integer },
{ typeof(ulong), SqliteType.Integer },
{ typeof(ushort), SqliteType.Integer }
};
private static SqliteType GetSqliteType(Type clrType)
{
var type = Nullable.GetUnderlyingType(clrType) ?? clrType;
type = type.IsEnum ? Enum.GetUnderlyingType(type) : type;
if (SqliteTypeMapping.TryGetValue(type, out var sqliteType))
{
return sqliteType;
}
throw new InvalidOperationException("Unknown Sqlite type for " + clrType);
}
private DbCommand GetInsertCommand(DbContext context, TableMetadata tableInfo, string tableName,
BulkInsertOptions options,
int batchSize)
{
var columns = tableInfo.GetProperties(options.CopyGeneratedColumns);
var cmd = context.Database.GetDbConnection().CreateCommand();
var sqliteColumns = columns
.Select(c => new
{
Name = c.ColumnName,
Type = GetSqliteType(c.ProviderClrType ?? c.ClrType)
})
.ToArray();
var i = 0;
var batches = Enumerable
.Repeat(0, batchSize)
.Select(_ =>
{
var cols = sqliteColumns.Select(column =>
{
var paramName = $"@p{i++}";
cmd.Parameters.Add(new SqliteParameter(paramName, column.Type));
return paramName;
});
return $"({string.Join(",", cols)})";
});
var sql = $"INSERT INTO {tableName} ({string.Join(",", sqliteColumns.Select(c => Quote(c.Name)))}) VALUES {string.Join(",", batches)}";
cmd.CommandText = sql;
cmd.Prepare();
return cmd;
}
/// <inheritdoc />
protected override async Task BulkInsert<T>(
bool sync,
DbContext context,
TableMetadata tableInfo,
IEnumerable<T> entities,
string tableName,
IReadOnlyList<PropertyMetadata> properties,
BulkInsertOptions options,
CancellationToken ctk
) where T : class
{
const int maxParams = 1000;
var batchSize = options.BatchSize ?? 5;
batchSize = Math.Min(batchSize, maxParams / properties.Count);
await using var insertCommand = GetInsertCommand(context, tableInfo, tableName, options, batchSize);
foreach (var chunk in entities.Chunk(batchSize))
{
// Full chunks
if (chunk.Length == batchSize)
{
FillValues(chunk, insertCommand.Parameters, properties);
await ExecuteCommand(sync, insertCommand, ctk);
}
// Last chunk
else
{
var partialInsertCommand = GetInsertCommand(context, tableInfo, tableName, options, chunk.Length);
FillValues(chunk, partialInsertCommand.Parameters, properties);
await ExecuteCommand(sync, partialInsertCommand, ctk);
}
}
}
private static async Task ExecuteCommand(bool sync, DbCommand insertCommand, CancellationToken ctk)
{
if (sync)
{
// ReSharper disable once MethodHasAsyncOverloadWithCancellation
insertCommand.ExecuteNonQuery();
}
else
{
await insertCommand.ExecuteNonQueryAsync(ctk);
}
}
private static void FillValues<T>(T[] chunk, DbParameterCollection parameters, IReadOnlyList<PropertyMetadata> properties) where T : class
{
var index = 0;
foreach (var entity in chunk)
{
foreach (var property in properties)
{
var value = property.GetValue(entity);
parameters[index].Value = value;
index++;
}
}
}
}