* DeleteAllMatchingDocuments deletes all documents that match the query. */
| 813 | * DeleteAllMatchingDocuments deletes all documents that match the query. |
| 814 | */ |
| 815 | static uint64 |
| 816 | DeleteAllMatchingDocuments(MongoCollection *collection, pgbson *queryDoc, |
| 817 | const bson_value_t *variableSpec, |
| 818 | const char *collationString, bool hasShardKeyValueFilter, |
| 819 | int64 shardKeyHash) |
| 820 | { |
| 821 | uint64 collectionId = collection->collectionId; |
| 822 | |
| 823 | bool applyCollation = IsCollationApplicable(collationString); |
| 824 | |
| 825 | bool queryHasNonIdFilters = false; |
| 826 | bool isIdFilterCollationAware = false; |
| 827 | pgbson *objectIdFilter = GetObjectIdFilterFromQueryDocument(queryDoc, |
| 828 | &queryHasNonIdFilters, |
| 829 | &isIdFilterCollationAware); |
| 830 | bool applyObjectIdFilter = objectIdFilter != NULL; |
| 831 | if (applyObjectIdFilter && applyCollation) |
| 832 | { |
| 833 | /* if the _id filter value is collation-sensitive, we will omit */ |
| 834 | /* filtering by _id in the WHERE clause. */ |
| 835 | applyObjectIdFilter = !isIdFilterCollationAware; |
| 836 | } |
| 837 | |
| 838 | int argCount = 0; |
| 839 | int nextSqlArgIndex = 1; |
| 840 | uint64 rowsDeleted = 0; |
| 841 | StringInfoData deleteQuery; |
| 842 | |
| 843 | SPI_connect(); |
| 844 | |
| 845 | initStringInfo(&deleteQuery); |
| 846 | appendStringInfo(&deleteQuery, "DELETE FROM "); |
| 847 | |
| 848 | if (collection->shardTableName[0] != '\0') |
| 849 | { |
| 850 | appendStringInfo(&deleteQuery, " %s.%s", ApiDataSchemaName, |
| 851 | collection->shardTableName); |
| 852 | } |
| 853 | else |
| 854 | { |
| 855 | appendStringInfo(&deleteQuery, " %s.documents_" UINT64_FORMAT, ApiDataSchemaName, |
| 856 | collectionId); |
| 857 | } |
| 858 | |
| 859 | pgbson *variableSpecBson = NULL; |
| 860 | if (queryHasNonIdFilters) |
| 861 | { |
| 862 | variableSpecBson = variableSpec != NULL && |
| 863 | variableSpec->value_type == BSON_TYPE_DOCUMENT ? |
| 864 | PgbsonInitFromDocumentBsonValue(variableSpec) : NULL; |
| 865 | } |
| 866 | |
| 867 | bool applyVariableSpec = variableSpecBson != NULL; |
| 868 | if (applyVariableSpec || applyCollation) |
| 869 | { |
| 870 | /* utilize the collation and/or variables in matching the document */ |
| 871 | appendStringInfo(&deleteQuery, |
| 872 | " WHERE %s.bson_query_match(document, $1::%s.bson, $2::%s.bson, $3::text)", |
no test coverage detected