-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add copy command support to Npgsql (#72)
- Loading branch information
1 parent
077e21a
commit 3683023
Showing
21 changed files
with
250 additions
and
100 deletions.
There are no files selected for viewing
This file contains 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 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 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,83 @@ | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Plugin; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | ||
|
||
namespace SqlcGenCsharp.Drivers.Generators; | ||
|
||
public class CopyFromDeclareGen(DbDriver dbDriver) | ||
{ | ||
public MemberDeclarationSyntax Generate(string funcName, string queryTextConstant, string argInterface, | ||
IList<Parameter> parameters) | ||
{ | ||
return ParseMemberDeclaration($$""" | ||
public async Task {{funcName}}(List<{{argInterface}}> args) | ||
{ | ||
{{GetMethodBody(queryTextConstant, parameters)}} | ||
} | ||
""")!; | ||
} | ||
|
||
private string GetMethodBody(string queryTextConstant, IEnumerable<Parameter> parameters) | ||
{ | ||
var (establishConnection, connectionOpen) = dbDriver.EstablishConnection(true); | ||
var beginBinaryImport = $"{Variable.Connection.Name()}.BeginBinaryImportAsync({queryTextConstant}"; | ||
|
||
return dbDriver.DotnetFramework.LatestDotnetSupported() | ||
? GetAsModernDotnet() | ||
: GetAsLegacyDotnet(); | ||
|
||
string GetAsModernDotnet() | ||
{ | ||
var addRowsToCopyCommand = AddRowsToCopyCommand(); | ||
return $$""" | ||
{ | ||
await using {{establishConnection}}; | ||
{{connectionOpen.AppendSemicolonUnlessEmpty()}} | ||
await {{Variable.Connection.Name()}}.OpenAsync(); | ||
await using var {{Variable.Writer.Name()}} = await {{beginBinaryImport}}); | ||
{{addRowsToCopyCommand}} | ||
await {{Variable.Writer.Name()}}.CompleteAsync(); | ||
await {{Variable.Connection.Name()}}.CloseAsync(); | ||
} | ||
"""; | ||
} | ||
|
||
string GetAsLegacyDotnet() | ||
{ | ||
var addRowsToCopyCommand = AddRowsToCopyCommand(); | ||
return $$""" | ||
{ | ||
using ({{establishConnection}}) | ||
{ | ||
{{connectionOpen.AppendSemicolonUnlessEmpty()}} | ||
await {{Variable.Connection.Name()}}.OpenAsync(); | ||
using (var {{Variable.Writer.Name()}} = await {{beginBinaryImport}})) | ||
{ | ||
{{addRowsToCopyCommand}} | ||
await {{Variable.Writer.Name()}}.CompleteAsync(); | ||
} | ||
await {{Variable.Connection.Name()}}.CloseAsync(); | ||
} | ||
} | ||
"""; | ||
} | ||
|
||
string AddRowsToCopyCommand() | ||
{ | ||
var constructRow = new List<string>() | ||
.Append($"await {Variable.Writer.Name()}.StartRowAsync();") | ||
.Concat(parameters | ||
.Select(p => | ||
$"await {Variable.Writer.Name()}.WriteAsync({Variable.Row.Name()}.{p.Column.Name.FirstCharToUpper()});")) | ||
.JoinByNewLine(); | ||
return $$""" | ||
foreach (var {{Variable.Row.Name()}} in args) | ||
{ | ||
{{constructRow}} | ||
} | ||
"""; | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -5,6 +5,8 @@ public enum Variable | |
ConnectionString, | ||
Connection, | ||
Reader, | ||
Row, | ||
Writer, | ||
Command, | ||
Result | ||
} | ||
|
This file contains 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 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.