-
-
Notifications
You must be signed in to change notification settings - Fork 10
MySql support. #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
MySql support. #15
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
ad0d19c
MySql support.
SebastianStehle 23f5b4c
Fix conflicts.
SebastianStehle e703fb7
Clean up usings.
SebastianStehle 429f643
More tests.
SebastianStehle 340516c
Fix
SebastianStehle cc9e7f2
Merge branch 'master' of github.com:SebastianStehle/PhenX.EntityFrame…
SebastianStehle 9eb14c5
Added benchmark
SebastianStehle 009f6b1
Fix tests
SebastianStehle 37b32a7
Fix tests
SebastianStehle e9ef3e7
Fix.
SebastianStehle aa30f93
Fix tests
SebastianStehle 665ff8a
Test
SebastianStehle 94fa5ea
Dispose transaction.
SebastianStehle 37ca08d
Merge branch 'master' of github.com:SebastianStehle/PhenX.EntityFrame…
SebastianStehle 2b67eb0
Revert
SebastianStehle 7327c49
Random container name.
SebastianStehle 5f95d82
Revert changes.
SebastianStehle a29f9ee
Save file.
SebastianStehle 303e0b7
Implement comments
SebastianStehle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,3 +88,6 @@ fabric.properties | |
|
|
||
| # Nuget assets | ||
| /nupkgs | ||
|
|
||
| # Visual Studio Files | ||
| .vs | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/PhenX.EntityFrameworkCore.BulkInsert.MySql/MySqlBulkInsertProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.EntityFrameworkCore.Storage; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| using MySqlConnector; | ||
|
|
||
| using PhenX.EntityFrameworkCore.BulkInsert.Options; | ||
|
|
||
| namespace PhenX.EntityFrameworkCore.BulkInsert.MySql; | ||
|
|
||
| internal class MySqlBulkInsertProvider : BulkInsertProviderBase<MySqlServerDialectBuilder> | ||
| { | ||
| public MySqlBulkInsertProvider(ILogger<MySqlBulkInsertProvider>? logger = null) : base(logger) | ||
| { | ||
| } | ||
|
|
||
| //language=sql | ||
| /// <inheritdoc /> | ||
| protected override string CreateTableCopySql => "CREATE TEMPORARY TABLE {0} SELECT * FROM {1} WHERE 1 = 0;"; | ||
|
|
||
| //language=sql | ||
| /// <inheritdoc /> | ||
| protected override string AddTableCopyBulkInsertId => $"ALTER TABLE {{0}} ADD {BulkInsertId} INT AUTO_INCREMENT PRIMARY KEY;"; | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override string GetTempTableName(string tableName) => $"#_temp_bulk_insert_{tableName}"; | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override async Task BulkInsert<T>( | ||
| bool sync, | ||
| DbContext context, | ||
| IEnumerable<T> entities, | ||
| string tableName, | ||
| PropertyAccessor[] properties, | ||
| BulkInsertOptions options, | ||
| CancellationToken ctk | ||
| ) | ||
| { | ||
| var connection = (MySqlConnection)context.Database.GetDbConnection(); | ||
| var sqlTransaction = context.Database.CurrentTransaction!.GetDbTransaction() as MySqlTransaction; | ||
|
|
||
| var bulkCopy = new MySqlBulkCopy(connection, sqlTransaction); | ||
| bulkCopy.DestinationTableName = tableName; | ||
| bulkCopy.BulkCopyTimeout = 60; | ||
|
|
||
| var sourceOrdinal = 0; | ||
| foreach (var prop in properties) | ||
| { | ||
| bulkCopy.ColumnMappings.Add(new MySqlBulkCopyColumnMapping(sourceOrdinal, prop.ColumnName)); | ||
| sourceOrdinal++; | ||
| } | ||
|
|
||
| if (sync) | ||
| { | ||
| // ReSharper disable once MethodHasAsyncOverloadWithCancellation | ||
| bulkCopy.WriteToServer(new EnumerableDataReader<T>(entities, properties)); | ||
| } | ||
| else | ||
| { | ||
| await bulkCopy.WriteToServerAsync(new EnumerableDataReader<T>(entities, properties), ctk); | ||
| } | ||
| } | ||
| } | ||
22 changes: 22 additions & 0 deletions
22
src/PhenX.EntityFrameworkCore.BulkInsert.MySql/MySqlDbContextOptionsExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.EntityFrameworkCore.Infrastructure; | ||
|
|
||
| namespace PhenX.EntityFrameworkCore.BulkInsert.MySql; | ||
|
|
||
| /// <summary> | ||
| /// DbContext options extension for SQL Server. | ||
|
SebastianStehle marked this conversation as resolved.
Outdated
|
||
| /// </summary> | ||
| public static class MySqlDbContextOptionsExtensions | ||
| { | ||
| /// <summary> | ||
| /// Configures the DbContext to use the MySql bulk insert provider. | ||
| /// </summary> | ||
| public static DbContextOptionsBuilder UseBulkInsertMySql(this DbContextOptionsBuilder optionsBuilder) | ||
| { | ||
| var extension = optionsBuilder.Options.FindExtension<BulkInsertOptionsExtension<MySqlBulkInsertProvider>>() ?? new BulkInsertOptionsExtension<MySqlBulkInsertProvider>(); | ||
|
|
||
| ((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension); | ||
|
|
||
| return optionsBuilder; | ||
| } | ||
| } | ||
59 changes: 59 additions & 0 deletions
59
src/PhenX.EntityFrameworkCore.BulkInsert.MySql/MySqlDialectBuilder.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| using System.Text; | ||
|
|
||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.EntityFrameworkCore.Metadata; | ||
|
|
||
| using PhenX.EntityFrameworkCore.BulkInsert.Dialect; | ||
| using PhenX.EntityFrameworkCore.BulkInsert.Options; | ||
|
|
||
| namespace PhenX.EntityFrameworkCore.BulkInsert.MySql; | ||
|
|
||
| internal class MySqlServerDialectBuilder : SqlDialectBuilder | ||
| { | ||
| protected override string OpenDelimiter => "`"; | ||
|
|
||
| protected override string CloseDelimiter => "`"; | ||
|
|
||
| protected override bool SupportsMoveRows => false; | ||
|
|
||
| public override bool SupportsReturning => false; | ||
|
|
||
| protected override void AppendConflictCondition<T>(StringBuilder sql, OnConflictOptions<T> onConflictTyped) | ||
| { | ||
| throw new NotSupportedException("Conflict conditions are not supported in MYSQL"); | ||
| } | ||
|
|
||
| protected override void AppendOnConflictUpdate(StringBuilder sql, IEnumerable<string> updates) | ||
| { | ||
| sql.AppendLine("UPDATE"); | ||
|
|
||
| var i = 0; | ||
| foreach (var update in updates) | ||
| { | ||
| if (i > 0) | ||
| { | ||
| sql.Append(", "); | ||
| } | ||
|
|
||
| sql.Append(update); | ||
| i++; | ||
| } | ||
| } | ||
|
|
||
| protected override void AppendOnConflictStatement(StringBuilder sql) | ||
| { | ||
| sql.Append("ON DUPLICATE KEY"); | ||
| } | ||
|
|
||
| protected override void AppendDoNothing(StringBuilder sql, IProperty[] insertedProperties) | ||
| { | ||
| var columnName = insertedProperties[0].GetColumnName(); | ||
|
|
||
| sql.Append($"UPDATE {Quote(columnName)} = {GetExcludedColumnName(columnName)}"); | ||
| } | ||
|
|
||
| protected override string GetExcludedColumnName(string columnName) | ||
| { | ||
| return $"VALUES({Quote(columnName)})"; | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
...nX.EntityFrameworkCore.BulkInsert.MySql/PhenX.EntityFrameworkCore.BulkInsert.MySql.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <ItemGroup> | ||
|
SebastianStehle marked this conversation as resolved.
Outdated
|
||
| <None Include="..\..\images\icon.png" Link="icon.png"> | ||
| <PackagePath>\</PackagePath> | ||
| <Pack>true</Pack> | ||
| </None> | ||
| <None Include="..\..\README.md" Link="README.md"> | ||
| <PackagePath>\</PackagePath> | ||
| <Pack>true</Pack> | ||
| </None> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\PhenX.EntityFrameworkCore.BulkInsert\PhenX.EntityFrameworkCore.BulkInsert.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Condition="'$(TargetFramework)' == 'net8.0'" Version="8.0.3" /> | ||
| <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Condition="'$(TargetFramework)' == 'net9.0'" Version="9.0.0-preview.3.efcore.9.0.0" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.