8
8
9
9
namespace SqlcGenCsharp . Drivers ;
10
10
11
- public class NpgsqlDriver : DbDriver , IOne , IMany , IExec , ICopyFrom , IExecRows , IExecLastId
11
+ public class NpgsqlDriver : DbDriver , IOne , IMany , IExec , IExecRows , IExecLastId , ICopyFrom
12
12
{
13
13
public NpgsqlDriver ( Options options ) : base ( options )
14
14
{
@@ -145,11 +145,6 @@ public override MemberDeclarationSyntax ManyDeclare(string queryTextConstant, st
145
145
return new ManyDeclareGen ( this ) . Generate ( queryTextConstant , argInterface , returnInterface , query ) ;
146
146
}
147
147
148
- public MemberDeclarationSyntax CopyFromDeclare ( string queryTextConstant , string argInterface , Query query )
149
- {
150
- return new CopyFromDeclareGen ( this ) . Generate ( queryTextConstant , argInterface , query ) ;
151
- }
152
-
153
148
public MemberDeclarationSyntax ExecRowsDeclare ( string queryTextConstant , string argInterface , Query query )
154
149
{
155
150
return new ExecRowsDeclareGen ( this ) . Generate ( queryTextConstant , argInterface , query ) ;
@@ -159,4 +154,52 @@ public MemberDeclarationSyntax ExecLastIdDeclare(string queryTextConstant, strin
159
154
{
160
155
return new ExecLastIdDeclareGen ( this ) . Generate ( queryTextConstant , argInterface , query ) ;
161
156
}
157
+
158
+ public MemberDeclarationSyntax CopyFromDeclare ( string queryTextConstant , string argInterface , Query query )
159
+ {
160
+ return new CopyFromDeclareGen ( this ) . Generate ( queryTextConstant , argInterface , query ) ;
161
+ }
162
+
163
+ public string GetCopyFromImpl ( Query query , string queryTextConstant )
164
+ {
165
+ var ( establishConnection , connectionOpen ) = EstablishConnection ( query ) ;
166
+ var beginBinaryImport = $ "{ Variable . Connection . AsVarName ( ) } .BeginBinaryImportAsync({ queryTextConstant } ";
167
+ var addRowsToCopyCommand = AddRowsToCopyCommand ( ) ;
168
+ return $$ """
169
+ using ({{ establishConnection }} )
170
+ {
171
+ {{ connectionOpen . AppendSemicolonUnlessEmpty ( ) }}
172
+ await {{ Variable . Connection . AsVarName ( ) }} .OpenAsync();
173
+ using (var {{ Variable . Writer . AsVarName ( ) }} = await {{ beginBinaryImport }} ))
174
+ {
175
+ {{ addRowsToCopyCommand }}
176
+ await {{ Variable . Writer . AsVarName ( ) }} .CompleteAsync();
177
+ }
178
+ await {{ Variable . Connection . AsVarName ( ) }} .CloseAsync();
179
+ }
180
+ """ ;
181
+
182
+ string AddRowsToCopyCommand ( )
183
+ {
184
+ var constructRow = new List < string > ( )
185
+ . Append ( $ "await { Variable . Writer . AsVarName ( ) } .StartRowAsync();")
186
+ . Concat ( query . Params
187
+ . Select ( p =>
188
+ {
189
+ var typeOverride = GetColumnDbTypeOverride ( p . Column ) ;
190
+ var partialStmt =
191
+ $ "await { Variable . Writer . AsVarName ( ) } .WriteAsync({ Variable . Row . AsVarName ( ) } .{ p . Column . Name . ToPascalCase ( ) } ";
192
+ return typeOverride is null
193
+ ? $ "{ partialStmt } );"
194
+ : $ "{ partialStmt } , { typeOverride } );";
195
+ } ) )
196
+ . JoinByNewLine ( ) ;
197
+ return $$ """
198
+ foreach (var {{ Variable . Row . AsVarName ( ) }} in args)
199
+ {
200
+ {{ constructRow }}
201
+ }
202
+ """ ;
203
+ }
204
+ }
162
205
}
0 commit comments