From 6fb0cd61c366341f2556f97356c0da1fa86766f4 Mon Sep 17 00:00:00 2001 From: Christopher Scott Date: Wed, 8 Feb 2023 11:07:09 -0600 Subject: [PATCH] fix tables samples so they build (#33998) --- sdk/tables/Azure.Data.Tables/CHANGELOG.md | 2 +- .../Azure.Data.Tables/MigrationGuide.md | 3 +-- sdk/tables/Azure.Data.Tables/README.md | 8 +++---- .../samples/Sample1CreateDeleteTables.md | 15 +++++------- .../samples/Sample2CreateDeleteEntities.md | 15 ++++++------ .../samples/Sample4QueryEntities.md | 9 ++++---- .../samples/Sample5UpdateUpsertEntities.md | 5 ++-- .../samples/Sample6TransactionalBatch.md | 15 ++++++------ .../samples/Sample1_CreateDeleteTable.cs | 23 ++++--------------- .../samples/Sample2_CreateDeleteEntities.cs | 10 ++++---- .../tests/samples/Sample4_QueryEntities.cs | 8 ++----- .../Sample6_TransactionalBatchAsync.cs | 12 ++++------ 12 files changed, 48 insertions(+), 77 deletions(-) diff --git a/sdk/tables/Azure.Data.Tables/CHANGELOG.md b/sdk/tables/Azure.Data.Tables/CHANGELOG.md index 65907c747d780..2313cb2e1a603 100644 --- a/sdk/tables/Azure.Data.Tables/CHANGELOG.md +++ b/sdk/tables/Azure.Data.Tables/CHANGELOG.md @@ -1,6 +1,6 @@ # Release History -## 12.8.0 (2023-02-07) +## 12.8.0 (2023-02-08) ### Bugs Fixed - Fixed an issue where LINQ predicates containing New expressions, such as `ent => ent.TimeStamp > new DateTimeOffset(...)`, threw an exception. diff --git a/sdk/tables/Azure.Data.Tables/MigrationGuide.md b/sdk/tables/Azure.Data.Tables/MigrationGuide.md index 093a43a852e19..c053c9da865d3 100644 --- a/sdk/tables/Azure.Data.Tables/MigrationGuide.md +++ b/sdk/tables/Azure.Data.Tables/MigrationGuide.md @@ -78,7 +78,6 @@ any one table, it is ideal for scenarios where you need to create, delete, or li ```C# Snippet:TablesSample1CreateTable // Create a new table. The TableItem class stores properties of the created table. -string tableName = "OfficeSupplies1p1"; TableItem table = serviceClient.CreateTableIfNotExists(tableName); Console.WriteLine($"The created table's name is {table.Name}."); ``` @@ -175,7 +174,7 @@ or not it already exists. ```C# Snippet:TablesMigrationUpsertEntity // Upsert the newly created entity. -tableClient.UpsertEntity(entity); +tableClient.UpsertEntity(tableEntity); ``` ### Fetching a single entity from the table diff --git a/sdk/tables/Azure.Data.Tables/README.md b/sdk/tables/Azure.Data.Tables/README.md index 7d7e2bd174ce1..217f3454b40be 100644 --- a/sdk/tables/Azure.Data.Tables/README.md +++ b/sdk/tables/Azure.Data.Tables/README.md @@ -108,7 +108,6 @@ Next, we can create a new table. ```C# Snippet:TablesSample1CreateTable // Create a new table. The TableItem class stores properties of the created table. -string tableName = "OfficeSupplies1p1"; TableItem table = serviceClient.CreateTableIfNotExists(tableName); Console.WriteLine($"The created table's name is {table.Name}."); ``` @@ -137,7 +136,6 @@ Individual tables can be deleted from the service. ```C# Snippet:TablesSample1DeleteTable // Deletes the table made previously. -string tableName = "OfficeSupplies1p1"; serviceClient.DeleteTable(tableName); ``` @@ -162,21 +160,21 @@ Let's define a new `TableEntity` so that we can add it to the table. ```C# Snippet:TablesSample2CreateDictionaryEntity // Make a dictionary entity by defining a . -var entity = new TableEntity(partitionKey, rowKey) +var tableEntity = new TableEntity(partitionKey, rowKey) { { "Product", "Marker Set" }, { "Price", 5.00 }, { "Quantity", 21 } }; -Console.WriteLine($"{entity.RowKey}: {entity["Product"]} costs ${entity.GetDouble("Price")}."); +Console.WriteLine($"{tableEntity.RowKey}: {tableEntity["Product"]} costs ${tableEntity.GetDouble("Price")}."); ``` Using the `TableClient` we can now add our new entity to the table. ```C# Snippet:TablesSample2AddEntity // Add the newly created entity. -tableClient.AddEntity(entity); +tableClient.AddEntity(tableEntity); ``` ### Query table entities diff --git a/sdk/tables/Azure.Data.Tables/samples/Sample1CreateDeleteTables.md b/sdk/tables/Azure.Data.Tables/samples/Sample1CreateDeleteTables.md index 380411ff9a1ba..1805b9591b607 100644 --- a/sdk/tables/Azure.Data.Tables/samples/Sample1CreateDeleteTables.md +++ b/sdk/tables/Azure.Data.Tables/samples/Sample1CreateDeleteTables.md @@ -22,14 +22,13 @@ A `TableClient` is needed to perform table-level operations like inserting and d - Call `GetTableClient` from the `TableServiceClient` with the table name. ```C# Snippet:TablesSample1GetTableClient -string tableName = "OfficeSupplies1p2"; -var tableClient = serviceClient.GetTableClient(tableName); +var tableClient2 = serviceClient.GetTableClient(tableName); ``` - Create a `TableClient` with a SAS URI, an endpoint and `TableSharedKeyCredential`, or a connection string. ```C# Snippet:TablesSample1CreateTableClient -var tableClient = new TableClient( +var tableClient3 = new TableClient( new Uri(storageUri), tableName, new TableSharedKeyCredential(accountName, storageAccountKey)); @@ -41,21 +40,20 @@ A table requires a [unique table name](https://docs.microsoft.com/rest/api/stora ### Using `TableServiceClient` -To create a table, invoke `CreateTable` with the table name. +To create a table, invoke `CreateTableIfNotExists` with the table name. ```C# Snippet:TablesSample1CreateTable // Create a new table. The TableItem class stores properties of the created table. -string tableName = "OfficeSupplies1p1"; TableItem table = serviceClient.CreateTableIfNotExists(tableName); Console.WriteLine($"The created table's name is {table.Name}."); ``` ### Using `TableClient` -To create a table, invoke `Create` with the table name. +To create a table, invoke `CreateIfNotExists` with the table name. ```C# Snippet:TablesSample1TableClientCreateTable -tableClient.CreateIfNotExists(); +tableClient3.CreateIfNotExists(); ``` ## Delete a table @@ -66,7 +64,6 @@ To delete the table, invoke `DeleteTable` with the table name. ```C# Snippet:TablesSample1DeleteTable // Deletes the table made previously. -string tableName = "OfficeSupplies1p1"; serviceClient.DeleteTable(tableName); ``` @@ -75,7 +72,7 @@ serviceClient.DeleteTable(tableName); To delete the table, invoke `Delete` with the table name. ```C# Snippet:TablesSample1TableClientDeleteTable -tableClient.Delete(); +tableClient3.Delete(); ``` ## Handle errors diff --git a/sdk/tables/Azure.Data.Tables/samples/Sample2CreateDeleteEntities.md b/sdk/tables/Azure.Data.Tables/samples/Sample2CreateDeleteEntities.md index 4b4a0b852d07b..4725ba7191c93 100644 --- a/sdk/tables/Azure.Data.Tables/samples/Sample2CreateDeleteEntities.md +++ b/sdk/tables/Azure.Data.Tables/samples/Sample2CreateDeleteEntities.md @@ -8,14 +8,13 @@ A `TableClient` is needed to perform table-level operations like inserting and d - Call `GetTableClient` from the `TableServiceClient` with the table name. ```C# Snippet:TablesSample1GetTableClient -string tableName = "OfficeSupplies1p2"; -var tableClient = serviceClient.GetTableClient(tableName); +var tableClient2 = serviceClient.GetTableClient(tableName); ``` - Create a `TableClient` with a SAS URI, an endpoint and `TableSharedKeyCredential`, or a connection string. ```C# Snippet:TablesSample1CreateTableClient -var tableClient = new TableClient( +var tableClient3 = new TableClient( new Uri(storageUri), tableName, new TableSharedKeyCredential(accountName, storageAccountKey)); @@ -58,7 +57,7 @@ var strongEntity = new OfficeSupplyEntity Quantity = 50 }; -Console.WriteLine($"{entity.RowKey}: {strongEntity.Product} costs ${strongEntity.Price}."); +Console.WriteLine($"{tableEntity.RowKey}: {strongEntity.Product} costs ${strongEntity.Price}."); ``` ### Dictionary entity @@ -69,23 +68,23 @@ Properties accessed using the indexer `[]` will be typed as an `object`, but `Ta ```C# Snippet:TablesSample2CreateDictionaryEntity // Make a dictionary entity by defining a . -var entity = new TableEntity(partitionKey, rowKey) +var tableEntity = new TableEntity(partitionKey, rowKey) { { "Product", "Marker Set" }, { "Price", 5.00 }, { "Quantity", 21 } }; -Console.WriteLine($"{entity.RowKey}: {entity["Product"]} costs ${entity.GetDouble("Price")}."); +Console.WriteLine($"{tableEntity.RowKey}: {tableEntity["Product"]} costs ${tableEntity.GetDouble("Price")}."); ``` ## Add an entity -To add the entity to the table, invoke `CreateEntity` and pass in the newly created entity. +To add the entity to the table, invoke `AddEntity` and pass in the newly created entity. ```C# Snippet:TablesSample2AddEntity // Add the newly created entity. -tableClient.AddEntity(entity); +tableClient.AddEntity(tableEntity); ``` ## Delete an entity diff --git a/sdk/tables/Azure.Data.Tables/samples/Sample4QueryEntities.md b/sdk/tables/Azure.Data.Tables/samples/Sample4QueryEntities.md index f1799866a23cc..deb1c49732eb4 100644 --- a/sdk/tables/Azure.Data.Tables/samples/Sample4QueryEntities.md +++ b/sdk/tables/Azure.Data.Tables/samples/Sample4QueryEntities.md @@ -8,14 +8,13 @@ A `TableClient` is needed to perform table-level operations like inserting and d - Call `GetTableClient` from the `TableServiceClient` with the table name. ```C# Snippet:TablesSample1GetTableClient -string tableName = "OfficeSupplies1p2"; -var tableClient = serviceClient.GetTableClient(tableName); +var tableClient2 = serviceClient.GetTableClient(tableName); ``` - Create a `TableClient` with a SAS URI, an endpoint and `TableSharedKeyCredential`, or a connection string. ```C# Snippet:TablesSample1CreateTableClient -var tableClient = new TableClient( +var tableClient3 = new TableClient( new Uri(storageUri), tableName, new TableSharedKeyCredential(accountName, storageAccountKey)); @@ -49,9 +48,9 @@ The `QueryFilter` class handles all the type escaping for you. ```C# Snippet:TablesSample4QueryEntitiesFilterWithQueryFilter // The CreateQueryFilter method is also available to assist with properly formatting and escaping OData queries. -Pageable queryResultsFilter = tableClient.Query(filter: TableClient.CreateQueryFilter($"PartitionKey eq {partitionKey}")); +var queryResultsFilter2 = tableClient.Query(filter: TableClient.CreateQueryFilter($"PartitionKey eq {partitionKey}")); // Iterate the to access all queried entities. -foreach (TableEntity qEntity in queryResultsFilter) +foreach (TableEntity qEntity in queryResultsFilter2) { Console.WriteLine($"{qEntity.GetString("Product")}: {qEntity.GetDouble("Price")}"); } diff --git a/sdk/tables/Azure.Data.Tables/samples/Sample5UpdateUpsertEntities.md b/sdk/tables/Azure.Data.Tables/samples/Sample5UpdateUpsertEntities.md index 1062202aecb11..0277286442f2b 100644 --- a/sdk/tables/Azure.Data.Tables/samples/Sample5UpdateUpsertEntities.md +++ b/sdk/tables/Azure.Data.Tables/samples/Sample5UpdateUpsertEntities.md @@ -8,14 +8,13 @@ A `TableClient` is needed to perform table-level operations like inserting and d - Call `GetTableClient` from the `TableServiceClient` with the table name. ```C# Snippet:TablesSample1GetTableClient -string tableName = "OfficeSupplies1p2"; -var tableClient = serviceClient.GetTableClient(tableName); +var tableClient2 = serviceClient.GetTableClient(tableName); ``` - Create a `TableClient` with a SAS URI, an endpoint and `TableSharedKeyCredential`, or a connection string. ```C# Snippet:TablesSample1CreateTableClient -var tableClient = new TableClient( +var tableClient3 = new TableClient( new Uri(storageUri), tableName, new TableSharedKeyCredential(accountName, storageAccountKey)); diff --git a/sdk/tables/Azure.Data.Tables/samples/Sample6TransactionalBatch.md b/sdk/tables/Azure.Data.Tables/samples/Sample6TransactionalBatch.md index 3433d27070fbc..0ebcda50a4f86 100644 --- a/sdk/tables/Azure.Data.Tables/samples/Sample6TransactionalBatch.md +++ b/sdk/tables/Azure.Data.Tables/samples/Sample6TransactionalBatch.md @@ -13,14 +13,13 @@ A `TableClient` is needed to perform table-level operations like inserting and d - Call `GetTableClient` from the `TableServiceClient` with the table name. ```C# Snippet:TablesSample1GetTableClient -string tableName = "OfficeSupplies1p2"; -var tableClient = serviceClient.GetTableClient(tableName); +var tableClient2 = serviceClient.GetTableClient(tableName); ``` - Create a `TableClient` with a SAS URI, an endpoint and `TableSharedKeyCredential`, or a connection string. ```C# Snippet:TablesSample1CreateTableClient -var tableClient = new TableClient( +var tableClient3 = new TableClient( new Uri(storageUri), tableName, new TableSharedKeyCredential(accountName, storageAccountKey)); @@ -34,28 +33,28 @@ A common use case for batch operations is to add many entities to a table in bul ```C# Snippet:BatchAdd // Create a list of 5 entities with the same partition key. -string partitionKey = "BatchInsertSample"; +string batchPartitionKey = "BatchInsertSample"; List entityList = new List { - new TableEntity(partitionKey, "01") + new TableEntity(batchPartitionKey, "01") { { "Product", "Marker" }, { "Price", 5.00 }, { "Brand", "Premium" } }, - new TableEntity(partitionKey, "02") + new TableEntity(batchPartitionKey, "02") { { "Product", "Pen" }, { "Price", 3.00 }, { "Brand", "Premium" } }, - new TableEntity(partitionKey, "03") + new TableEntity(batchPartitionKey, "03") { { "Product", "Paper" }, { "Price", 0.10 }, { "Brand", "Premium" } }, - new TableEntity(partitionKey, "04") + new TableEntity(batchPartitionKey, "04") { { "Product", "Glue" }, { "Price", 1.00 }, diff --git a/sdk/tables/Azure.Data.Tables/tests/samples/Sample1_CreateDeleteTable.cs b/sdk/tables/Azure.Data.Tables/tests/samples/Sample1_CreateDeleteTable.cs index 3aa56cab220f6..943eb93668998 100644 --- a/sdk/tables/Azure.Data.Tables/tests/samples/Sample1_CreateDeleteTable.cs +++ b/sdk/tables/Azure.Data.Tables/tests/samples/Sample1_CreateDeleteTable.cs @@ -29,9 +29,6 @@ public void CreateDeleteTable() #region Snippet:TablesSample1CreateTable // Create a new table. The TableItem class stores properties of the created table. -#if SNIPPET - string tableName = "OfficeSupplies1p1"; -#endif TableItem table = serviceClient.CreateTableIfNotExists(tableName); Console.WriteLine($"The created table's name is {table.Name}."); #endregion @@ -46,39 +43,29 @@ public void CreateDeleteTable() #region Snippet:TablesSample1DeleteTable // Deletes the table made previously. -#if SNIPPET - string tableName = "OfficeSupplies1p1"; -#endif serviceClient.DeleteTable(tableName); #endregion #region Snippet:TablesSample1GetTableClient -#if SNIPPET - string tableName = "OfficeSupplies1p2"; - var tableClient = serviceClient.GetTableClient(tableName); -#else +#if !SNIPPET tableName = "OfficeSupplies1p2" + _random.Next(); - tableClient = serviceClient.GetTableClient(tableName); #endif + var tableClient2 = serviceClient.GetTableClient(tableName); #endregion #region Snippet:TablesSample1CreateTableClient -#if SNIPPET - var tableClient = new TableClient( -#else - tableClient = new TableClient( -#endif + var tableClient3 = new TableClient( new Uri(storageUri), tableName, new TableSharedKeyCredential(accountName, storageAccountKey)); #endregion #region Snippet:TablesSample1TableClientCreateTable - tableClient.CreateIfNotExists(); + tableClient3.CreateIfNotExists(); #endregion #region Snippet:TablesSample1TableClientDeleteTable - tableClient.Delete(); + tableClient3.Delete(); #endregion } } diff --git a/sdk/tables/Azure.Data.Tables/tests/samples/Sample2_CreateDeleteEntities.cs b/sdk/tables/Azure.Data.Tables/tests/samples/Sample2_CreateDeleteEntities.cs index 756247dd11436..24467f0fa2cc7 100644 --- a/sdk/tables/Azure.Data.Tables/tests/samples/Sample2_CreateDeleteEntities.cs +++ b/sdk/tables/Azure.Data.Tables/tests/samples/Sample2_CreateDeleteEntities.cs @@ -34,24 +34,24 @@ public void CreateDeleteEntity() #region Snippet:TablesSample2CreateDictionaryEntity // Make a dictionary entity by defining a . - var entity = new TableEntity(partitionKey, rowKey) + var tableEntity = new TableEntity(partitionKey, rowKey) { { "Product", "Marker Set" }, { "Price", 5.00 }, { "Quantity", 21 } }; - Console.WriteLine($"{entity.RowKey}: {entity["Product"]} costs ${entity.GetDouble("Price")}."); + Console.WriteLine($"{tableEntity.RowKey}: {tableEntity["Product"]} costs ${tableEntity.GetDouble("Price")}."); #endregion #region Snippet:TablesSample2AddEntity // Add the newly created entity. - tableClient.AddEntity(entity); + tableClient.AddEntity(tableEntity); #endregion #region Snippet:TablesMigrationUpsertEntity // Upsert the newly created entity. - tableClient.UpsertEntity(entity); + tableClient.UpsertEntity(tableEntity); #endregion #region Snippet:TablesSample2CreateStronglyTypedEntity @@ -65,7 +65,7 @@ public void CreateDeleteEntity() Quantity = 50 }; - Console.WriteLine($"{entity.RowKey}: {strongEntity.Product} costs ${strongEntity.Price}."); + Console.WriteLine($"{tableEntity.RowKey}: {strongEntity.Product} costs ${strongEntity.Price}."); #endregion #region Snippet:TablesMigrationCreateEntity diff --git a/sdk/tables/Azure.Data.Tables/tests/samples/Sample4_QueryEntities.cs b/sdk/tables/Azure.Data.Tables/tests/samples/Sample4_QueryEntities.cs index 4863304f9a11c..282c6fe013d3e 100644 --- a/sdk/tables/Azure.Data.Tables/tests/samples/Sample4_QueryEntities.cs +++ b/sdk/tables/Azure.Data.Tables/tests/samples/Sample4_QueryEntities.cs @@ -60,13 +60,9 @@ public void QueryEntities() #region Snippet:TablesSample4QueryEntitiesFilterWithQueryFilter // The CreateQueryFilter method is also available to assist with properly formatting and escaping OData queries. -#if SNIPPET - Pageable queryResultsFilter = tableClient.Query(filter: TableClient.CreateQueryFilter($"PartitionKey eq {partitionKey}")); -#else - queryResultsFilter = tableClient.Query(filter: TableClient.CreateQueryFilter($"PartitionKey eq {partitionKey}")); -#endif + var queryResultsFilter2 = tableClient.Query(filter: TableClient.CreateQueryFilter($"PartitionKey eq {partitionKey}")); // Iterate the to access all queried entities. - foreach (TableEntity qEntity in queryResultsFilter) + foreach (TableEntity qEntity in queryResultsFilter2) { Console.WriteLine($"{qEntity.GetString("Product")}: {qEntity.GetDouble("Price")}"); } diff --git a/sdk/tables/Azure.Data.Tables/tests/samples/Sample6_TransactionalBatchAsync.cs b/sdk/tables/Azure.Data.Tables/tests/samples/Sample6_TransactionalBatchAsync.cs index 08a2f8af0f60f..90e72a0f52998 100644 --- a/sdk/tables/Azure.Data.Tables/tests/samples/Sample6_TransactionalBatchAsync.cs +++ b/sdk/tables/Azure.Data.Tables/tests/samples/Sample6_TransactionalBatchAsync.cs @@ -33,30 +33,28 @@ public async Task TransactionalBatchAsync() #region Snippet:BatchAdd // Create a list of 5 entities with the same partition key. -#if SNIPPET - string partitionKey = "BatchInsertSample"; -#endif + string batchPartitionKey = "BatchInsertSample"; List entityList = new List { - new TableEntity(partitionKey, "01") + new TableEntity(batchPartitionKey, "01") { { "Product", "Marker" }, { "Price", 5.00 }, { "Brand", "Premium" } }, - new TableEntity(partitionKey, "02") + new TableEntity(batchPartitionKey, "02") { { "Product", "Pen" }, { "Price", 3.00 }, { "Brand", "Premium" } }, - new TableEntity(partitionKey, "03") + new TableEntity(batchPartitionKey, "03") { { "Product", "Paper" }, { "Price", 0.10 }, { "Brand", "Premium" } }, - new TableEntity(partitionKey, "04") + new TableEntity(batchPartitionKey, "04") { { "Product", "Glue" }, { "Price", 1.00 },