11using Npgsql . Tests . Support ;
2+ using Npgsql . Util ;
23using NUnit . Framework ;
34using System ;
45using System . Collections . Generic ;
@@ -136,6 +137,52 @@ public async Task RecordsAffected_and_Rows()
136137 Assert . That ( command . Rows , Is . EqualTo ( 2 ) ) ;
137138 }
138139
140+ [ Test ]
141+ public async Task Merge_RecordsAffected_and_Rows ( )
142+ {
143+ await using var conn = await OpenConnectionAsync ( ) ;
144+
145+ MinimumPgVersion ( conn , "15.0" , "MERGE statement was introduced in PostgreSQL 15" ) ;
146+
147+ await using var _ = await CreateTempTable ( conn , "name TEXT" , out var table ) ;
148+
149+ await using var batch = new NpgsqlBatch ( conn )
150+ {
151+ BatchCommands =
152+ {
153+ new ( $ "INSERT INTO { table } (name) VALUES ('a'), ('b')") ,
154+ new ( $ "MERGE INTO { table } S USING (SELECT 'b' as name) T ON T.name = S.name WHEN MATCHED THEN UPDATE SET name = 'c'") ,
155+ new ( $ "MERGE INTO { table } S USING (SELECT 'b' as name) T ON T.name = S.name WHEN NOT MATCHED THEN INSERT (name) VALUES ('b')") ,
156+ new ( $ "MERGE INTO { table } S USING (SELECT 'b' as name) T ON T.name = S.name WHEN MATCHED THEN DELETE") ,
157+ new ( $ "MERGE INTO { table } S USING (SELECT 'b' as name) T ON T.name = S.name WHEN NOT MATCHED THEN DO NOTHING")
158+ }
159+ } ;
160+ await using var reader = await batch . ExecuteReaderAsync ( Behavior ) ;
161+
162+ // Consume MERGE result set to parse the CommandComplete
163+ await reader . CloseAsync ( ) ;
164+
165+ var command = batch . BatchCommands [ 0 ] ;
166+ Assert . That ( command . RecordsAffected , Is . EqualTo ( 2 ) ) ;
167+ Assert . That ( command . Rows , Is . EqualTo ( 2 ) ) ;
168+
169+ command = batch . BatchCommands [ 1 ] ;
170+ Assert . That ( command . RecordsAffected , Is . EqualTo ( 1 ) ) ;
171+ Assert . That ( command . Rows , Is . EqualTo ( 1 ) ) ;
172+
173+ command = batch . BatchCommands [ 2 ] ;
174+ Assert . That ( command . RecordsAffected , Is . EqualTo ( 1 ) ) ;
175+ Assert . That ( command . Rows , Is . EqualTo ( 1 ) ) ;
176+
177+ command = batch . BatchCommands [ 3 ] ;
178+ Assert . That ( command . RecordsAffected , Is . EqualTo ( 1 ) ) ;
179+ Assert . That ( command . Rows , Is . EqualTo ( 1 ) ) ;
180+
181+ command = batch . BatchCommands [ 4 ] ;
182+ Assert . That ( command . RecordsAffected , Is . EqualTo ( 0 ) ) ;
183+ Assert . That ( command . Rows , Is . EqualTo ( 0 ) ) ;
184+ }
185+
139186 [ Test ]
140187 public async Task NpgsqlBatchCommand_StatementType ( )
141188 {
@@ -155,6 +202,10 @@ public async Task NpgsqlBatchCommand_StatementType()
155202 new ( "COMMIT" )
156203 }
157204 } ;
205+
206+ if ( conn . PostgreSqlVersion . IsGreaterOrEqual ( 15 ) )
207+ batch . BatchCommands . Add ( new ( $ "MERGE INTO { table } S USING (SELECT 'b' as name) T ON T.name = S.name WHEN NOT MATCHED THEN DO NOTHING") ) ;
208+
158209 await using var reader = await batch . ExecuteReaderAsync ( Behavior ) ;
159210
160211 // Consume SELECT result set to parse the CommandComplete
@@ -167,6 +218,9 @@ public async Task NpgsqlBatchCommand_StatementType()
167218 Assert . That ( batch . BatchCommands [ 4 ] . StatementType , Is . EqualTo ( StatementType . Select ) ) ;
168219 Assert . That ( batch . BatchCommands [ 5 ] . StatementType , Is . EqualTo ( StatementType . Delete ) ) ;
169220 Assert . That ( batch . BatchCommands [ 6 ] . StatementType , Is . EqualTo ( StatementType . Other ) ) ;
221+
222+ if ( conn . PostgreSqlVersion . IsGreaterOrEqual ( 15 ) )
223+ Assert . That ( batch . BatchCommands [ 7 ] . StatementType , Is . EqualTo ( StatementType . Merge ) ) ;
170224 }
171225
172226 [ Test ]
0 commit comments