@@ -75,16 +75,13 @@ public string Build(SqlInsertStructure structure)
7575 // Columns whose values are provided in the request body - to be inserted into the record.
7676 string insertColumns = Build ( structure . InsertColumns ) ;
7777
78- // add appropriate comments here
79- string selectQueryPredicates = JoinPredicateStrings ( structure . GetDbPolicyForOperation ( EntityActionOperation . Read ) ) ;
80-
8178 // Values to be inserted into the entity.
8279 string values = dbPolicypredicates . Equals ( BASE_PREDICATE ) ?
8380 $ "VALUES ({ string . Join ( ", " , structure . Values ) } );" : $ "SELECT { insertColumns } FROM (VALUES({ string . Join ( ", " , structure . Values ) } )) T({ insertColumns } ) WHERE { dbPolicypredicates } ;";
8481
8582 // Final insert query to be executed against the database.
8683 StringBuilder insertQuery = new ( ) ;
87- if ( structure . ApiRequestType is ApiType . GraphQL && ! isInsertDMLTriggerEnabled )
84+ if ( ! isInsertDMLTriggerEnabled )
8885 {
8986 // When there is no DML trigger enabled on the table for insert operation, we can use OUTPUT clause to return the data.
9087 insertQuery . Append ( $ "INSERT INTO { tableName } ({ insertColumns } ) OUTPUT " +
@@ -145,12 +142,7 @@ public string Build(SqlInsertStructure structure)
145142 // If there is an autogenerated column in the PK, we will add an additional WHERE condition for it.
146143 // Using SCOPE_IDENTITY() method provided by sql server,
147144 // we can get the last generated value of the autogenerated column.
148- subsequentSelect . Append ( $ "WHERE { tableName } .{ QuoteIdentifier ( autoGenPKColumn ) } = SCOPE_IDENTITY() ") ;
149- subsequentSelect . Append ( $ "AND { selectQueryPredicates } ") ;
150- }
151- else
152- {
153- subsequentSelect . Append ( $ "WHERE { selectQueryPredicates } ") ;
145+ subsequentSelect . Append ( $ "WHERE { tableName } .{ QuoteIdentifier ( autoGenPKColumn ) } = SCOPE_IDENTITY()") ;
154146 }
155147
156148 insertQuery . Append ( subsequentSelect . ToString ( ) ) ;
@@ -273,11 +265,9 @@ public string Build(SqlUpsertQueryStructure structure)
273265 // Predicates by virtue of PK + database policy.
274266 string updatePredicates = JoinPredicateStrings ( pkPredicates , structure . GetDbPolicyForOperation ( EntityActionOperation . Update ) ) ;
275267
276- string selectPredicates = JoinPredicateStrings ( pkPredicates , structure . GetDbPolicyForOperation ( EntityActionOperation . Read ) ) ;
277-
278268 string updateOperations = Build ( structure . UpdateOperations , ", " ) ;
279269 string columnsToBeReturned =
280- MakeOutputColumns ( structure . OutputColumns , ( isUpdateTriggerEnabled || structure . ApiRequestType is ApiType . REST ) ? string . Empty : OutputQualifier . Inserted . ToString ( ) ) ;
270+ MakeOutputColumns ( structure . OutputColumns , isUpdateTriggerEnabled ? string . Empty : OutputQualifier . Inserted . ToString ( ) ) ;
281271 string queryToGetCountOfRecordWithPK = $ "SELECT COUNT(*) as { COUNT_ROWS_WITH_GIVEN_PK } FROM { tableName } WHERE { pkPredicates } ";
282272
283273 // Query to get the number of records with a given PK.
@@ -295,13 +285,13 @@ public string Build(SqlUpsertQueryStructure structure)
295285 $ "UPDATE { tableName } " +
296286 $ "SET { updateOperations } ") ;
297287
298- if ( structure . ApiRequestType is ApiType . REST || isUpdateTriggerEnabled )
288+ if ( isUpdateTriggerEnabled )
299289 {
300290 // If a trigger is enabled on the entity, we cannot use OUTPUT clause to return the record.
301291 // In such a case, we will use a subsequent select query to get the record. By the time the subsequent select executes,
302292 // the trigger would have already executed and we get the data as it is present in the table.
303293 updateQuery . Append ( $ "WHERE { updatePredicates } ;") ;
304- string subsequentSelect = $ "SELECT { columnsToBeReturned } FROM { tableName } WHERE { selectPredicates } ;";
294+ string subsequentSelect = $ "SELECT { columnsToBeReturned } FROM { tableName } WHERE { updatePredicates } ;";
305295 updateQuery . Append ( subsequentSelect ) ;
306296 }
307297 else
@@ -331,9 +321,8 @@ public string Build(SqlUpsertQueryStructure structure)
331321
332322 bool isInsertTriggerEnabled = sourceDefinition . IsInsertDMLTriggerEnabled ;
333323 // We can only use OUTPUT clause to return inserted data when there is no trigger enabled on the entity.
334- if ( ! isInsertTriggerEnabled && structure . ApiRequestType is ApiType . GraphQL )
324+ if ( ! isInsertTriggerEnabled )
335325 {
336- // use output clause here
337326 if ( isUpdateTriggerEnabled )
338327 {
339328 // This is just an optimisation. If update trigger is enabled, then this build method had created
@@ -343,10 +332,9 @@ public string Build(SqlUpsertQueryStructure structure)
343332
344333 insertQuery . Append ( $ "OUTPUT { columnsToBeReturned } ") ;
345334 }
346-
347335 // If an insert trigger is enabled but there was no update trigger enabled,
348336 // we need to generate columnsToBeReturned without the 'Inserted' prefix on each column.
349- else if ( isInsertTriggerEnabled && ! isUpdateTriggerEnabled )
337+ else if ( ! isUpdateTriggerEnabled )
350338 {
351339 // This is again just an optimisation. If update trigger was enabled, then the columnsToBeReturned would
352340 // have already been created without any prefix.
@@ -361,11 +349,11 @@ public string Build(SqlUpsertQueryStructure structure)
361349 // Append the values to be inserted to the insertQuery.
362350 insertQuery . Append ( fetchColumnValuesQuery ) ;
363351
364- if ( isInsertTriggerEnabled || structure . ApiRequestType is ApiType . REST )
352+ if ( isInsertTriggerEnabled )
365353 {
366354 // Since a trigger is enabled, a subsequent select query is to be executed to get the inserted record.
367355 // By the time the subsequent select executes, the trigger would have already executed and we get the data as it is present in the table.
368- string subsequentSelect = $ "SELECT { columnsToBeReturned } from { tableName } WHERE { pkPredicates } AND { selectPredicates } ;";
356+ string subsequentSelect = $ "SELECT { columnsToBeReturned } from { tableName } WHERE { pkPredicates } ;";
369357 insertQuery . Append ( subsequentSelect ) ;
370358 }
371359
0 commit comments