-
-
Notifications
You must be signed in to change notification settings - Fork 10
Add oracle support #52
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
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9241bd2
Begin support for Oracle
795e03b
Merge branch 'main' into feature/oracle
6e2b8d7
Fix backmerge
df2e124
Merge branch 'main' into feature/oracle
8606f69
Merge branch 'main' into feature/oracle
4ce4dfa
Add basic support for Oracle
479c41f
Merge branch 'main' into feature/oracle
259c75a
Fix back merge
b7b0503
Mark test as unstable on oracle
0b30e4f
Little fixes
5058480
Update src/PhenX.EntityFrameworkCore.BulkInsert.Oracle/OracleBulkInse…
PhenX 773658f
Fix drop table method
f6c9803
Merge branch 'main' into feature/oracle
PhenX-Pro e43d86c
Put strin enum column back in string
PhenX-Pro 42aec43
Add comments and fix obsolete code
PhenX-Pro 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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions
14
src/PhenX.EntityFrameworkCore.BulkInsert.Oracle/OracleBulkInsertOptions.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,14 @@ | ||
| using Oracle.ManagedDataAccess.Client; | ||
|
|
||
| using PhenX.EntityFrameworkCore.BulkInsert.Options; | ||
|
|
||
| namespace PhenX.EntityFrameworkCore.BulkInsert.Oracle; | ||
|
|
||
| /// <summary> | ||
| /// Options specific to Oracle bulk insert. | ||
| /// </summary> | ||
| public class OracleBulkInsertOptions : BulkInsertOptions | ||
| { | ||
| /// <inheritdoc cref="OracleBulkCopyOptions"/> | ||
| public OracleBulkCopyOptions CopyOptions { get; set; } = OracleBulkCopyOptions.Default; | ||
| } |
94 changes: 94 additions & 0 deletions
94
src/PhenX.EntityFrameworkCore.BulkInsert.Oracle/OracleBulkInsertProvider.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,94 @@ | ||
| using JetBrains.Annotations; | ||
|
|
||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| using Oracle.ManagedDataAccess.Client; | ||
|
|
||
| using PhenX.EntityFrameworkCore.BulkInsert.Metadata; | ||
| using PhenX.EntityFrameworkCore.BulkInsert.Options; | ||
|
|
||
| namespace PhenX.EntityFrameworkCore.BulkInsert.Oracle; | ||
|
|
||
| [UsedImplicitly] | ||
| internal class OracleBulkInsertProvider(ILogger<OracleBulkInsertProvider>? logger) : BulkInsertProviderBase<OracleDialectBuilder, OracleBulkInsertOptions>(logger) | ||
| { | ||
| /// <inheritdoc /> | ||
| protected override string BulkInsertId => "ROWID"; | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override string AddTableCopyBulkInsertId => ""; // No need to add an ID column in Oracle | ||
|
|
||
| /// <inheritdoc /> | ||
| /// <summary> | ||
| /// The temporary table name is generated with a GUID to ensure uniqueness, but limited to less than 30 characters, | ||
| /// because Oracle prior 12.2 has a limit of 30 characters for identifiers. | ||
| /// </summary> | ||
| protected override string GetTempTableName(string tableName) => $"#temp_bulk_insert_{Guid.NewGuid().ToString("N")[..8]}"; | ||
|
|
||
| protected override OracleBulkInsertOptions CreateDefaultOptions() => new() | ||
| { | ||
| BatchSize = 50_000, | ||
| }; | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override IAsyncEnumerable<T> BulkInsertReturnEntities<T>( | ||
| bool sync, | ||
| DbContext context, | ||
| TableMetadata tableInfo, | ||
| IEnumerable<T> entities, | ||
| OracleBulkInsertOptions options, | ||
| OnConflictOptions<T>? onConflict, | ||
| CancellationToken ctk) | ||
| { | ||
| throw new NotSupportedException("Provider does not support returning entities."); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override Task BulkInsert<T>( | ||
| bool sync, | ||
| DbContext context, | ||
| TableMetadata tableInfo, | ||
| IEnumerable<T> entities, | ||
| string tableName, | ||
| IReadOnlyList<ColumnMetadata> columns, | ||
| OracleBulkInsertOptions options, | ||
| CancellationToken ctk) | ||
| { | ||
| var connection = (OracleConnection) context.Database.GetDbConnection(); | ||
|
|
||
| using var bulkCopy = new OracleBulkCopy(connection, options.CopyOptions); | ||
|
|
||
| bulkCopy.DestinationTableName = tableInfo.QuotedTableName; | ||
| bulkCopy.BatchSize = options.BatchSize; | ||
| bulkCopy.BulkCopyTimeout = options.GetCopyTimeoutInSeconds(); | ||
|
|
||
| foreach (var column in columns) | ||
| { | ||
| bulkCopy.ColumnMappings.Add(column.PropertyName, column.QuotedColumName); | ||
| } | ||
|
|
||
| var dataReader = new EnumerableDataReader<T>(entities, columns, options); | ||
|
|
||
| bulkCopy.WriteToServer(dataReader); | ||
|
|
||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override async Task DropTempTableAsync(bool sync, DbContext dbContext, string tableName) | ||
| { | ||
| var commandText = $""" | ||
| BEGIN | ||
| EXECUTE IMMEDIATE 'DROP TABLE {tableName}'; | ||
| EXCEPTION | ||
| WHEN OTHERS THEN | ||
| IF SQLCODE != -942 THEN -- ORA-00942: table or view does not exist | ||
| RAISE; | ||
| END IF; | ||
| END; | ||
| """; | ||
|
|
||
| await ExecuteAsync(sync, dbContext, commandText, CancellationToken.None); | ||
| } | ||
| } | ||
19 changes: 19 additions & 0 deletions
19
src/PhenX.EntityFrameworkCore.BulkInsert.Oracle/OracleDbContextOptionsExtensions.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,19 @@ | ||
| using Microsoft.EntityFrameworkCore; | ||
|
|
||
| using PhenX.EntityFrameworkCore.BulkInsert.Extensions; | ||
|
|
||
| namespace PhenX.EntityFrameworkCore.BulkInsert.Oracle; | ||
|
|
||
| /// <summary> | ||
| /// DbContext options extension for Oracle. | ||
| /// </summary> | ||
| public static class OracleDbContextOptionsExtensions | ||
| { | ||
| /// <summary> | ||
| /// Configures the DbContext to use the Oracle bulk insert provider. | ||
| /// </summary> | ||
| public static DbContextOptionsBuilder UseBulkInsertOracle(this DbContextOptionsBuilder optionsBuilder) | ||
| { | ||
| return optionsBuilder.UseProvider<OracleBulkInsertProvider>(); | ||
| } | ||
| } |
115 changes: 115 additions & 0 deletions
115
src/PhenX.EntityFrameworkCore.BulkInsert.Oracle/OracleDialectBuilder.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,115 @@ | ||
| using System.Text; | ||
|
|
||
| using Microsoft.EntityFrameworkCore; | ||
|
|
||
| using PhenX.EntityFrameworkCore.BulkInsert.Dialect; | ||
| using PhenX.EntityFrameworkCore.BulkInsert.Metadata; | ||
| using PhenX.EntityFrameworkCore.BulkInsert.Options; | ||
|
|
||
| namespace PhenX.EntityFrameworkCore.BulkInsert.Oracle; | ||
|
|
||
| internal class OracleDialectBuilder : SqlDialectBuilder | ||
| { | ||
| protected override string OpenDelimiter => "\""; | ||
| protected override string CloseDelimiter => "\""; | ||
| protected override string ConcatOperator => "||"; | ||
|
|
||
| protected override bool SupportsMoveRows => false; | ||
|
|
||
| public override string CreateTableCopySql(string tempTableName, TableMetadata tableInfo, IReadOnlyList<ColumnMetadata> columns) | ||
| { | ||
| return CreateTableCopySqlBase(tempTableName, columns); | ||
| } | ||
|
|
||
| public override string BuildMoveDataSql<T>( | ||
| DbContext context, | ||
| TableMetadata target, | ||
| string source, | ||
| IReadOnlyList<ColumnMetadata> insertedColumns, | ||
| IReadOnlyList<ColumnMetadata> returnedColumns, | ||
| BulkInsertOptions options, | ||
| OnConflictOptions? onConflict = null) | ||
| { | ||
| var q = new StringBuilder(); | ||
|
|
||
| // Merge handling | ||
| if (onConflict is OnConflictOptions<T> onConflictTyped) | ||
| { | ||
| IEnumerable<string> matchColumns; | ||
| if (onConflictTyped.Match != null) | ||
| { | ||
| matchColumns = GetColumns(target, onConflictTyped.Match); | ||
| } | ||
| else if (target.PrimaryKey.Count > 0) | ||
| { | ||
| matchColumns = target.PrimaryKey.Select(x => x.QuotedColumName); | ||
| } | ||
| else | ||
| { | ||
| throw new InvalidOperationException("Table has no primary key that can be used for conflict detection."); | ||
| } | ||
|
|
||
| q.AppendLine($"MERGE INTO {target.QuotedTableName} AS {PseudoTableInserted}"); | ||
|
|
||
| q.Append("USING (SELECT "); | ||
| q.AppendColumns(insertedColumns); | ||
| q.Append($" FROM {source}) AS {PseudoTableExcluded} ("); | ||
| q.AppendColumns(insertedColumns); | ||
| q.AppendLine(")"); | ||
|
|
||
| q.Append("ON "); | ||
| q.AppendJoin(" AND ", matchColumns, (b, col) => b.Append($"{PseudoTableInserted}.{col} = {PseudoTableExcluded}.{col}")); | ||
| q.AppendLine(); | ||
|
|
||
| if (onConflictTyped.Update != null) | ||
| { | ||
| var columns = target.GetColumns(false); | ||
|
|
||
| q.AppendLine("WHEN MATCHED THEN UPDATE SET "); | ||
| q.AppendJoin(", ", GetUpdates(context, target, columns, onConflictTyped.Update)); | ||
| q.AppendLine(); | ||
| } | ||
|
|
||
| q.Append("WHEN NOT MATCHED THEN INSERT ("); | ||
| q.AppendColumns(insertedColumns); | ||
| q.AppendLine(")"); | ||
|
|
||
| q.Append("VALUES ("); | ||
| q.AppendJoin(", ", insertedColumns, (b, col) => b.Append($"{PseudoTableExcluded}.{col.QuotedColumName}")); | ||
| q.AppendLine(")"); | ||
|
|
||
| if (returnedColumns.Count != 0) | ||
| { | ||
| q.Append("OUTPUT "); | ||
| q.AppendJoin(", ", returnedColumns, (b, col) => b.Append($"{PseudoTableInserted}.{col.QuotedColumName} AS {col.QuotedColumName}")); | ||
| q.AppendLine(); | ||
| } | ||
| } | ||
|
|
||
| // No conflict handling | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps two methods so that the comments are not necessary? |
||
| else | ||
| { | ||
| q.Append($"INSERT INTO {target.QuotedTableName} ("); | ||
| q.AppendColumns(insertedColumns); | ||
| q.AppendLine(")"); | ||
| q.Append("SELECT "); | ||
| q.AppendColumns(insertedColumns); | ||
| q.AppendLine(); | ||
| q.Append($"FROM {source}"); | ||
| q.AppendLine(); | ||
|
|
||
| if (returnedColumns.Count != 0) | ||
| { | ||
| q.Append("RETURNING "); | ||
| q.AppendJoin(", ", returnedColumns, (b, col) => b.Append(col.QuotedColumName)); | ||
| q.Append(" INTO "); | ||
| q.AppendJoin(", ", returnedColumns, (b, col) => b.Append($":{col.ColumnName}")); | ||
| q.AppendLine(); | ||
| } | ||
| } | ||
|
|
||
| q.AppendLine(";"); | ||
|
|
||
| return q.ToString(); | ||
| } | ||
| } | ||
12 changes: 12 additions & 0 deletions
12
....EntityFrameworkCore.BulkInsert.Oracle/PhenX.EntityFrameworkCore.BulkInsert.Oracle.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,12 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\PhenX.EntityFrameworkCore.BulkInsert\PhenX.EntityFrameworkCore.BulkInsert.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Oracle.EntityFrameworkCore" Condition="'$(TargetFramework)' == 'net8.0'" Version="8.23.80" /> | ||
| <PackageReference Include="Oracle.EntityFrameworkCore" Condition="'$(TargetFramework)' == 'net9.0'" Version="9.23.80" /> | ||
| </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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you not take the whole guid?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oracle under a certain version (12 I think) does not allow indentifiers longer than 30, yay
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would probably comment that, because I would have forgotten that next week.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh I will remember 😅 but yeah I'll add a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ahve said this very often to myself :D