@@ -75,13 +75,16 @@ 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+
7881 // Values to be inserted into the entity.
7982 string values = dbPolicypredicates . Equals ( BASE_PREDICATE ) ?
8083 $ "VALUES ({ string . Join ( ", " , structure . Values ) } );" : $ "SELECT { insertColumns } FROM (VALUES({ string . Join ( ", " , structure . Values ) } )) T({ insertColumns } ) WHERE { dbPolicypredicates } ;";
8184
8285 // Final insert query to be executed against the database.
8386 StringBuilder insertQuery = new ( ) ;
84- if ( ! isInsertDMLTriggerEnabled )
87+ if ( structure . ApiRequestType is ApiType . GraphQL && ! isInsertDMLTriggerEnabled )
8588 {
8689 // When there is no DML trigger enabled on the table for insert operation, we can use OUTPUT clause to return the data.
8790 insertQuery . Append ( $ "INSERT INTO { tableName } ({ insertColumns } ) OUTPUT " +
@@ -142,7 +145,12 @@ public string Build(SqlInsertStructure structure)
142145 // If there is an autogenerated column in the PK, we will add an additional WHERE condition for it.
143146 // Using SCOPE_IDENTITY() method provided by sql server,
144147 // we can get the last generated value of the autogenerated column.
145- subsequentSelect . Append ( $ "WHERE { tableName } .{ QuoteIdentifier ( autoGenPKColumn ) } = SCOPE_IDENTITY()") ;
148+ subsequentSelect . Append ( $ "WHERE { tableName } .{ QuoteIdentifier ( autoGenPKColumn ) } = SCOPE_IDENTITY() ") ;
149+ subsequentSelect . Append ( $ "AND { selectQueryPredicates } ") ;
150+ }
151+ else
152+ {
153+ subsequentSelect . Append ( $ "WHERE { selectQueryPredicates } ") ;
146154 }
147155
148156 insertQuery . Append ( subsequentSelect . ToString ( ) ) ;
@@ -265,9 +273,11 @@ public string Build(SqlUpsertQueryStructure structure)
265273 // Predicates by virtue of PK + database policy.
266274 string updatePredicates = JoinPredicateStrings ( pkPredicates , structure . GetDbPolicyForOperation ( EntityActionOperation . Update ) ) ;
267275
276+ string selectPredicates = JoinPredicateStrings ( pkPredicates , structure . GetDbPolicyForOperation ( EntityActionOperation . Read ) ) ;
277+
268278 string updateOperations = Build ( structure . UpdateOperations , ", " ) ;
269279 string columnsToBeReturned =
270- MakeOutputColumns ( structure . OutputColumns , isUpdateTriggerEnabled ? string . Empty : OutputQualifier . Inserted . ToString ( ) ) ;
280+ MakeOutputColumns ( structure . OutputColumns , ( isUpdateTriggerEnabled || structure . ApiRequestType is ApiType . REST ) ? string . Empty : OutputQualifier . Inserted . ToString ( ) ) ;
271281 string queryToGetCountOfRecordWithPK = $ "SELECT COUNT(*) as { COUNT_ROWS_WITH_GIVEN_PK } FROM { tableName } WHERE { pkPredicates } ";
272282
273283 // Query to get the number of records with a given PK.
@@ -285,13 +295,13 @@ public string Build(SqlUpsertQueryStructure structure)
285295 $ "UPDATE { tableName } " +
286296 $ "SET { updateOperations } ") ;
287297
288- if ( isUpdateTriggerEnabled )
298+ if ( structure . ApiRequestType is ApiType . REST || isUpdateTriggerEnabled )
289299 {
290300 // If a trigger is enabled on the entity, we cannot use OUTPUT clause to return the record.
291301 // In such a case, we will use a subsequent select query to get the record. By the time the subsequent select executes,
292302 // the trigger would have already executed and we get the data as it is present in the table.
293303 updateQuery . Append ( $ "WHERE { updatePredicates } ;") ;
294- string subsequentSelect = $ "SELECT { columnsToBeReturned } FROM { tableName } WHERE { updatePredicates } ;";
304+ string subsequentSelect = $ "SELECT { columnsToBeReturned } FROM { tableName } WHERE { selectPredicates } ;";
295305 updateQuery . Append ( subsequentSelect ) ;
296306 }
297307 else
@@ -321,8 +331,9 @@ public string Build(SqlUpsertQueryStructure structure)
321331
322332 bool isInsertTriggerEnabled = sourceDefinition . IsInsertDMLTriggerEnabled ;
323333 // We can only use OUTPUT clause to return inserted data when there is no trigger enabled on the entity.
324- if ( ! isInsertTriggerEnabled )
334+ if ( ! isInsertTriggerEnabled && structure . ApiRequestType is ApiType . GraphQL )
325335 {
336+ // use output clause here
326337 if ( isUpdateTriggerEnabled )
327338 {
328339 // This is just an optimisation. If update trigger is enabled, then this build method had created
@@ -332,9 +343,10 @@ public string Build(SqlUpsertQueryStructure structure)
332343
333344 insertQuery . Append ( $ "OUTPUT { columnsToBeReturned } ") ;
334345 }
346+
335347 // If an insert trigger is enabled but there was no update trigger enabled,
336348 // we need to generate columnsToBeReturned without the 'Inserted' prefix on each column.
337- else if ( ! isUpdateTriggerEnabled )
349+ else if ( isInsertTriggerEnabled && ! isUpdateTriggerEnabled )
338350 {
339351 // This is again just an optimisation. If update trigger was enabled, then the columnsToBeReturned would
340352 // have already been created without any prefix.
@@ -349,11 +361,11 @@ public string Build(SqlUpsertQueryStructure structure)
349361 // Append the values to be inserted to the insertQuery.
350362 insertQuery . Append ( fetchColumnValuesQuery ) ;
351363
352- if ( isInsertTriggerEnabled )
364+ if ( isInsertTriggerEnabled || structure . ApiRequestType is ApiType . REST )
353365 {
354366 // Since a trigger is enabled, a subsequent select query is to be executed to get the inserted record.
355367 // 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.
356- string subsequentSelect = $ "SELECT { columnsToBeReturned } from { tableName } WHERE { pkPredicates } ;";
368+ string subsequentSelect = $ "SELECT { columnsToBeReturned } from { tableName } WHERE { pkPredicates } AND { selectPredicates } ;";
357369 insertQuery . Append ( subsequentSelect ) ;
358370 }
359371
0 commit comments