public async IAsyncEnumerable <TEntity> GetAllAsync() { await foreach (var item in _container.GetItemQueryIterator <TEntity>(new QueryDefinition("SELECT * FROM c"))) { yield return(item); } }
public async IAsyncEnumerable <Image> GetImagesAsync() { await foreach (var item in CosmosContainer.GetItemQueryIterator <Image>("SELECT * FROM c F ORDER BY F.createdDate DESC")) { yield return(item); } }
public async Task <Competition[]> GetCompetitionsByTitle(string title) { if (string.IsNullOrWhiteSpace(title)) { _logger.LogWarning("Try to get competitions with null or " + "empty string as title. Skip select..."); return(null); } var query = new QueryDefinition($"SELECT * FROM c WHERE c.{nameof(Competition.Title)} = '{title}'"); var requestedItemPages = await _container .GetItemQueryIterator <Competition>(query) .AsPages() .FirstOrDefaultAsync(); var items = requestedItemPages.Values.Where(x => x != null).ToArray(); _logger.LogInformation(items.Length == 0 ? $"Items with title '{title}' not found" : $"Item with title '{title}' receive successful. Count: {items.Length}"); var requestUnit = GetRequestUnitHeader(requestedItemPages.GetRawResponse().Headers); if (requestUnit != null) { _logger.LogInformation($"Charged request units: {requestUnit}"); } return(items.Length == 0 ? null : items); }
public async Task <Image> GetImageAsync(string id) { QueryDefinition queryDefinition = new QueryDefinition("SELECT * FROM c F WHERE F.id = @id").WithParameter("@id", id); await foreach (Image item in CosmosContainer.GetItemQueryIterator <Image>(queryDefinition)) { return(item); } return(null); }
public async Task <User> GetByEmailAsync(string email) { var queryDefinition = new QueryDefinition("SELECT * FROM Users C WHERE C.Email = @email") .WithParameter("@email", email); var pageable = _userContainer.GetItemQueryIterator <User>(queryDefinition, null); await foreach (var page in pageable?.AsPages()) { return(page?.Values.FirstOrDefault()); } return(null); }
public async Task <BikeReservation> GetExistingReservationByBikeIdAsync(string carId, DateTime rentFrom) { try { CosmosContainer container = GetContainer(); var entities = new List <BikeReservation>(); QueryDefinition queryDefinition = new QueryDefinition("select * from c where c.rentTo > @rentFrom AND c.carId = @carId") .WithParameter("@rentFrom", rentFrom) .WithParameter("@carId", carId); AsyncPageable <BikeReservation> queryResultSetIterator = container.GetItemQueryIterator <BikeReservation>(queryDefinition); await foreach (BikeReservation BikeReservation in queryResultSetIterator) { entities.Add(BikeReservation); } return(entities.FirstOrDefault()); } catch (CosmosException ex) { Log.Error($"Entity with ID: {carId} was not retrieved successfully - error details: {ex.Message}"); if (ex.ErrorCode != "404") { throw; } return(null); } }
public async Task <IReadOnlyList <T> > GetAllAsync() { try { CosmosContainer container = GetContainer(); AsyncPageable <T> queryResultSetIterator = container.GetItemQueryIterator <T>(); List <T> entities = new List <T>(); await foreach (var entity in queryResultSetIterator) { entities.Add(entity); } return(entities); } catch (CosmosException ex) { Log.Error($"Entities was not retrieved successfully - error details: {ex.Message}"); if (ex.Status != (int)HttpStatusCode.NotFound) { throw; } return(null); } }
public async IAsyncEnumerable <RobotInfo> GetAll() { await foreach (var robotInfo in _robotsContainer.GetItemQueryIterator <RobotInfo>()) { yield return(robotInfo); } }
public async Task <Flag> GetAsync(string projectId, string environmentId, string flagId) { var queryDefinition = new QueryDefinition("SELECT * FROM Flags C WHERE C.Environment.Project.id = @projectId AND C.Environment.id = @environmentId AND C.id = @flagId") .WithParameter("@projectId", projectId) .WithParameter("@environmentId", environmentId) .WithParameter("@flagId", flagId); var pageable = _flagContainer.GetItemQueryIterator <Flag>(queryDefinition, null, new QueryRequestOptions { PartitionKey = new PartitionKey(environmentId) }); await foreach (var page in pageable?.AsPages()) { return(page?.Values.FirstOrDefault()); } return(null); }
public async IAsyncEnumerable <LogMessage> GetAll(int limit = 100) { var query = new QueryDefinition($"SELECT * FROM a ORDER BY a.Timestamp DESC OFFSET 0 LIMIT {limit}"); await foreach (var log in _logsContainer.GetItemQueryIterator <LogMessage>(query)) { yield return(log); } }
public async IAsyncEnumerable <Alert> GetAll(int limit = 10) { var query = new QueryDefinition($"SELECT * FROM a ORDER BY a.Timestamp DESC OFFSET 0 LIMIT {limit}"); await foreach (var alert in _alertsContainer.GetItemQueryIterator <Alert>(query)) { yield return(alert); } }
public static async Task <T?> GetItemByQueryAsync <T>(CosmosContainer container, QueryDefinition query) { // Get first item await foreach (var item in container.GetItemQueryIterator <T>(query)) { return(item); } return(default(T)); }
public async Task <IEnumerable <TestData> > GetTestDataAsync(string queryString) { var results = new List <TestData>(); await foreach (var t in _container.GetItemQueryIterator <TestData>(new QueryDefinition(queryString))) { results.Add(t); } return(results); }
public static async Task <List <T> > GetItemsByQueryAsync <T>(CosmosContainer container, QueryDefinition query) { var list = new List <T>(); // Get items await foreach (var item in container.GetItemQueryIterator <T>(query)) { list.Add(item); } return(list); }
static async Task <List <SaleData> > GetSalesDataByLocationAsync(CosmosContainer container, string location) { var sqlQueryText = $"SELECT * FROM c WHERE c.location = '{location}'"; QueryDefinition queryDefinition = new QueryDefinition(sqlQueryText); List <SaleData> sales = new List <SaleData>(); var queryResult = container.GetItemQueryIterator <SaleData>(queryDefinition); await foreach (SaleData saleData in queryResult) { sales.Add(saleData); } return(sales); }
public async Task <Storage[]> GetStoragesByUserIdAsync(int UserId) { var sqlQueryText = $"SELECT * FROM c WHERE c.UserId = '{UserId}' and c.Type='storage'"; QueryDefinition queryDefinition = new QueryDefinition(sqlQueryText); var Storages = new List <Storage>(); await foreach (var storage in UserContainer.GetItemQueryIterator <Storage>(queryDefinition)) { Storages.Add(storage); } return(Storages.ToArray()); }
public async Task <IEnumerable <Item> > GetItemsAsync(string queryString) { QueryDefinition queryDefinition = new QueryDefinition(queryString); List <Item> results = new List <Item>(); await foreach (Item item in _cosmosContainer.GetItemQueryIterator <Item>(queryDefinition)) { results.Add(item); } return(results); }
private async Task QueryItemsAsync(string search) { var sqlQueryText = "SELECT * FROM C WHERE C.name = '" + search + "'"; CosmosContainer CContainer = await GetContainer(); QueryDefinition queryDefinition = new QueryDefinition(sqlQueryText); List <Project> projects = new List <Project>(); await foreach (Project project in CContainer.GetItemQueryIterator <Project>(queryDefinition)) { projects.Add(project); } }
public async Task <IEnumerable <Domain.Entities.Foo> > GetByNeighborhood(string neighborhood) { var result = new List <Domain.Entities.Foo> { }; await foreach (var item in _container.GetItemQueryIterator <Domain.Entities.Foo>( new QueryDefinition($"SELECT * FROM c WHERE c.Neighborhood = '{neighborhood}'"))) { result.Add(item); } return(result); }
public async Task <IEnumerable <T> > GetAllAsync(string queryString) { var query = _container.GetItemQueryIterator <T>(new QueryDefinition(queryString)); var entities = new List <T>(); var queryResponseAsync = query.GetAsyncEnumerator(); while (await queryResponseAsync.MoveNextAsync()) { entities.Add(queryResponseAsync.Current); } return(entities); }
public async Task <List <Project> > GetProjects() { var sqlQueryText = "SELECT * FROM C"; CosmosContainer CContainer = await GetContainer(); QueryDefinition queryDefinition = new QueryDefinition(sqlQueryText); List <Project> projects = new List <Project>(); await foreach (Project project in CContainer.GetItemQueryIterator <Project>(queryDefinition)) { projects.Add(project); } return(projects); }
public async Task <RefreshToken> GetByKeyUserIdAsync(string key, string userId) { var queryDefinition = new QueryDefinition("SELECT * FROM RefreshTokens C WHERE C.Key = @key") .WithParameter("@key", key); var pageable = _refreshTokenContainer.GetItemQueryIterator <RefreshToken>(queryDefinition, null, new QueryRequestOptions { PartitionKey = new PartitionKey(userId) }); await foreach (var page in pageable?.AsPages()) { return(page?.Values.FirstOrDefault()); } return(null); }
public async void LoadSessions() { this.Cursor = System.Windows.Forms.Cursors.WaitCursor; dtSessions.Clear(); //Azure Cosmos cosmosClient = new CosmosClient(_settings.EndpointUrl, _settings.AuthorizationKey); try { CosmosDatabase database = await cosmosClient.CreateDatabaseIfNotExistsAsync(_settings.DatabaseId); CosmosContainer container = await cosmosClient.GetDatabase(_settings.DatabaseId).CreateContainerIfNotExistsAsync(_settings.ContainerId, "/partition"); var sqlQueryText = "SELECT * FROM c"; QueryDefinition queryDefinition = new QueryDefinition(sqlQueryText); await foreach (Session s in container.GetItemQueryIterator <Session>(queryDefinition)) { dtSessions.Rows.Add(new object[] { s.id, s.ResponseStatusCode, s.UriHost, s.UriAbsolutePath, s.Note }); _allSessions.Add(s); } } catch (Exception ex) { MessageBox.Show(ex.Message); } dataGridView1.DataSource = dtSessions; dataGridView1.Columns[0].Visible = false; dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.None; dataGridView1.Columns[1].Width = 30; dataGridView1.Columns[1].MinimumWidth = 30; dataGridView1.Columns[1].ReadOnly = true; dataGridView1.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader; dataGridView1.Columns[2].ReadOnly = true; dataGridView1.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader; dataGridView1.Columns[3].ReadOnly = true; dataGridView1.Columns[4].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; this.Cursor = System.Windows.Forms.Cursors.Default; }
public async Task <PaginatedRecipesClientResponse <Recipe> > GetPaginatedRecipesAsync(string continuationToken) { var conToken = continuationToken != null ? continuationToken : null; try { var query = new QueryDefinition("SELECT * FROM c"); var queryResultSetIterator = _cosmosContainer.GetItemQueryIterator <Recipe>( query, conToken, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("recipe"), MaxItemCount = _recipesCosmosDbConfig.pageSize, } ).AsPages(); var result = await queryResultSetIterator.FirstOrDefaultAsync(); var sourceContinuationToken = result.ContinuationToken != null? JsonSerializer.Deserialize <ContinuationToken>(result.ContinuationToken).SourceContinuationToken : null; var response = new PaginatedRecipesClientResponse <Recipe>() { message = $"Found {result.Values.Count} items", status = HttpStatusCode.OK, continuationToken = sourceContinuationToken, recipe = result.Values.ToList() }; return(response); } catch (CosmosException e) { _logger.LogInformation("Status: {0} - Message: {1}", e.Status, e.Message); var response = new PaginatedRecipesClientResponse <Recipe>() { message = e.Message, status = (HttpStatusCode)e.Status }; return(response); } }
// </AddItemsToContainerAsync> // <QueryItemsAsync> /// <summary> /// Run a query (using Azure Cosmos DB SQL syntax) against the container /// </summary> private static async Task QueryItemsAsync(CosmosClient cosmosClient) { var sqlQueryText = "SELECT * FROM c WHERE c.LastName = 'Andersen'"; Console.WriteLine("Running query: {0}\n", sqlQueryText); CosmosContainer container = cosmosClient.GetContainer(Program.DatabaseId, Program.ContainerId); QueryDefinition queryDefinition = new QueryDefinition(sqlQueryText); List <Family> families = new List <Family>(); await foreach (Family family in container.GetItemQueryIterator <Family>(queryDefinition)) { families.Add(family); Console.WriteLine("\tRead {0}\n", family); } }
public async Task <IEnumerable <Environment> > GetByProjectAsync(string projectId) { var queryDefinition = new QueryDefinition("SELECT * FROM Environments C WHERE C.Project.id = @projectId").WithParameter("@projectId", projectId); var pageable = _flagContainer.GetItemQueryIterator <Environment>(queryDefinition, null, new QueryRequestOptions { PartitionKey = new PartitionKey(projectId) }); var result = new List <Environment> { }; await foreach (var page in pageable?.AsPages()) { foreach (var entity in page?.Values) { result.Add(entity); } } return(result); }
private async void button_settings_export_Click(object sender, EventArgs e) { cosmosClient = new CosmosClient(_settings.EndpointUrl, _settings.AuthorizationKey); try { string timestamp = DateTime.Now.ToString("yyyymmddHHmmss"); SQLiteAsyncConnection db = new SQLiteAsyncConnection("export-" + timestamp + ".db"); await db.CreateTableAsync <Session>(); CosmosDatabase database = await cosmosClient.CreateDatabaseIfNotExistsAsync(_settings.DatabaseId); CosmosContainer container = await cosmosClient.GetDatabase(_settings.DatabaseId).CreateContainerIfNotExistsAsync(_settings.ContainerId, "/partition"); var sqlQueryText = "SELECT * FROM c"; QueryDefinition queryDefinition = new QueryDefinition(sqlQueryText); await foreach (Session s in container.GetItemQueryIterator <Session>(queryDefinition)) { await db.InsertAsync(s); } string exportFilepath = new FileInfo("export-" + timestamp + ".db").DirectoryName; if (MessageBox.Show("Exported in Sqlite format to: " + exportFilepath + "export-" + timestamp + ".db", "Open export directory?", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes) { System.Diagnostics.Process.Start("explorer.exe", exportFilepath); } ; } catch (Exception ex) { MessageBox.Show(ex.Message); } }