/// <summary> /// Gets the stored procedure, or creates a new one if one with the specified storedProcedure.id doesn't exist. /// </summary> /// <param name="dbId">The id of the Database to search for, or create.</param> /// <param name="collectionId">The id of the document db collection.</param> /// <param name="storedProcedure">The stored procedure to get or create.</param> /// <param name="deleteStoredProcedure">Indicator to delete the stored procedure before creating it.</param> /// <returns>The matched, or created, StoredProcedure object</returns> public async Task <StoredProcedure> GetOrCreateStoredProcedureAsync( string dbId, string collectionId, StoredProcedure storedProcedure, bool deleteStoredProcedure = false) { Code.ExpectsNotNullOrWhiteSpaceArgument(dbId, nameof(dbId), TaggingUtilities.ReserveTag(0x2381b14a /* tag_961fk */)); Code.ExpectsNotNullOrWhiteSpaceArgument(collectionId, nameof(collectionId), TaggingUtilities.ReserveTag(0x2381b14b /* tag_961fl */)); Code.ExpectsArgument(storedProcedure, nameof(storedProcedure), TaggingUtilities.ReserveTag(0x2381b14c /* tag_961fm */)); IDocumentClient client = await GetDocumentClientAsync().ConfigureAwait(false); StoredProcedure sproc = null; var colUri = UriFactory.CreateDocumentCollectionUri(dbId, collectionId); return(await DocumentDbAdapter.ExecuteAndLogAsync(TaggingUtilities.ReserveTag(0x2381b14d /* tag_961fn */), async() => { try { if (deleteStoredProcedure) { await DeleteStoredProcedureAsync(dbId, collectionId, storedProcedure.Id).ConfigureAwait(false); } sproc = client.CreateStoredProcedureQuery(colUri) .Where(p => p.Id == storedProcedure.Id).AsEnumerable().FirstOrDefault(); if (sproc != null) { return sproc; } sproc = await client.CreateStoredProcedureAsync(colUri, storedProcedure).ConfigureAwait(false); return sproc; } catch (DocumentClientException ex) { if (ex.StatusCode == HttpStatusCode.Conflict) { sproc = client.CreateStoredProcedureQuery(colUri) .Where(p => p.Id == storedProcedure.Id).AsEnumerable().FirstOrDefault(); return sproc; } throw; } }).ConfigureAwait(false)); }
private static async Task DeleteStoredProcedures(IDocumentClient client, Uri collectionUri) { Console.WriteLine("Deleting stored procedures"); IEnumerable <StoredProcedure> storedProcedures = client.CreateStoredProcedureQuery(collectionUri).AsEnumerable(); foreach (StoredProcedure storedProcedure in storedProcedures) { await client.DeleteStoredProcedureAsync(storedProcedure.SelfLink); } }
private async Task SetupStoredProcedureAsync() { var collectionName = typeof(NotificationRecord).Name; var documentCollectionUri = UriFactory.CreateDocumentCollectionUri(_configuration.DatabaseId, collectionName); var storedProcedure = _client.CreateStoredProcedureQuery(documentCollectionUri) .Where(db => db.Id == _configuration.BulkDeleteNotificationStoredProcedureId) .AsEnumerable() .FirstOrDefault(); if (storedProcedure == null) { storedProcedure = new StoredProcedure() { Id = _configuration.BulkDeleteNotificationStoredProcedureId, Body = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Scripts/bulkDeleteNotifications.js")) }; await _client.CreateStoredProcedureAsync(documentCollectionUri, storedProcedure); } }
public async Task <string> ExecuteStoredProcedureAsync(string storedProcedureName, params dynamic[] parameters) { var sproc = _client.CreateStoredProcedureQuery(_collection.SelfLink).Where(sp => sp.Id == storedProcedureName).AsEnumerable().First(); return(await _client.ExecuteStoredProcedureAsync <string>(sproc.SelfLink, parameters)); }