From 999bc2077122c86abe6eb4e120a62355cb20b092 Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Fri, 20 Dec 2024 01:58:34 +0100 Subject: [PATCH] Npgsql: Harmonize DDL vs. data provisioning in `DemoTypes` --- by-language/csharp-npgsql/DemoProgram.cs | 12 ++- by-language/csharp-npgsql/DemoTypes.cs | 96 +++++++++---------- .../csharp-npgsql/tests/DemoProgramTest.cs | 10 +- 3 files changed, 57 insertions(+), 61 deletions(-) diff --git a/by-language/csharp-npgsql/DemoProgram.cs b/by-language/csharp-npgsql/DemoProgram.cs index 39582254..ec12a076 100644 --- a/by-language/csharp-npgsql/DemoProgram.cs +++ b/by-language/csharp-npgsql/DemoProgram.cs @@ -27,11 +27,13 @@ await Parser.Default.ParseArguments(args) await DatabaseWorkloads.SystemQueryExample(conn); await DatabaseWorkloads.BasicConversationExample(conn); await DatabaseWorkloads.UnnestExample(conn); - await DatabaseWorkloadsMore.AllTypesNativeExample(conn); - await DatabaseWorkloadsMore.ObjectJsonDocumentExample(conn); - // await DatabaseWorkloadsMore.ArrayJsonDocumentExample(conn); - await DatabaseWorkloadsMore.ObjectPocoExample(conn); - await DatabaseWorkloadsMore.ArrayPocoExample(conn); + + var dwt = new DatabaseWorkloadsTypes(conn); + await dwt.AllTypesNativeExample(); + await dwt.ObjectJsonDocumentExample(); + // await dwt.ArrayJsonDocumentExample(); + await dwt.ObjectPocoExample(); + await dwt.ArrayPocoExample(); conn.Close(); }); diff --git a/by-language/csharp-npgsql/DemoTypes.cs b/by-language/csharp-npgsql/DemoTypes.cs index fcbc637d..641a3521 100644 --- a/by-language/csharp-npgsql/DemoTypes.cs +++ b/by-language/csharp-npgsql/DemoTypes.cs @@ -53,12 +53,19 @@ public class AllTypesRecord public IList? FloatVector { get; set; } } - public class DatabaseWorkloadsMore + public class DatabaseWorkloadsTypes { - public static async Task ProvisionAllTypes(NpgsqlConnection conn) + public DatabaseWorkloadsTypes(NpgsqlConnection conn) { - Console.WriteLine("Running ProvisionAllTypes"); + this.conn = conn; + } + + private NpgsqlConnection conn; + + public async Task CreateTable() + { + Console.WriteLine("Running CreateTable"); // Submit DDL, create database schema. await using (var cmd = new NpgsqlCommand("DROP TABLE IF EXISTS testdrive.example", conn)) @@ -86,6 +93,7 @@ char CHARACTER(5), -- Container types "array" ARRAY(STRING), "object" OBJECT(DYNAMIC), + "array_object" ARRAY(OBJECT(DYNAMIC)), -- Geospatial types geopoint GEO_POINT, geoshape GEO_SHAPE, @@ -96,6 +104,11 @@ float_vector FLOAT_VECTOR(3) { cmd.ExecuteNonQuery(); } + } + + public async Task InsertRecord() + { + Console.WriteLine("Running InsertRecord"); // Insert single data point. await using (var cmd = new NpgsqlCommand(""" @@ -162,20 +175,26 @@ INSERT INTO testdrive.example ( cmd.ExecuteNonQuery(); } + await RefreshTable(); + + } + + public async Task RefreshTable() + { // Flush data. await using (var cmd = new NpgsqlCommand("REFRESH TABLE testdrive.example", conn)) { cmd.ExecuteNonQuery(); } - } - public static async Task AllTypesNativeExample(NpgsqlConnection conn) + public async Task AllTypesNativeExample() { Console.WriteLine("Running AllTypesNativeExample"); // Provision data. - await ProvisionAllTypes(conn); + await CreateTable(); + await InsertRecord(); // Query back data. await using (var cmd = new NpgsqlCommand("SELECT * FROM testdrive.example", conn)) @@ -190,18 +209,12 @@ public static async Task AllTypesNativeExample(NpgsqlConnection conn) } - public static async Task ObjectJsonDocumentExample(NpgsqlConnection conn) + public async Task ObjectJsonDocumentExample() { Console.WriteLine("Running ObjectJsonDocumentExample"); // Provision data. - await ProvisionAllTypes(conn); - - // This test uses the central DDL, but a blank slate to focus on the test case at hand. - await using (var cmd = new NpgsqlCommand("DELETE FROM testdrive.example", conn)) - { - cmd.ExecuteNonQuery(); - } + await CreateTable(); await using (var cmd = new NpgsqlCommand(""" INSERT INTO testdrive.example ( @@ -216,10 +229,7 @@ INSERT INTO testdrive.example ( } // Flush data. - await using (var cmd = new NpgsqlCommand("REFRESH TABLE testdrive.example", conn)) - { - cmd.ExecuteNonQuery(); - } + await RefreshTable(); // Query back data. await using (var cmd = new NpgsqlCommand("SELECT * FROM testdrive.example", conn)) @@ -232,12 +242,13 @@ INSERT INTO testdrive.example ( } } - public static async Task> ArrayJsonDocumentExample(NpgsqlConnection conn) + public async Task> ArrayJsonDocumentExample() { Console.WriteLine("Running ArrayJsonDocumentExample"); // Provision data. - await ProvisionAllTypes(conn); + await CreateTable(); + await InsertRecord(); // Query back data. await using (var cmd = new NpgsqlCommand("SELECT * FROM testdrive.example", conn)) @@ -253,34 +264,18 @@ public static async Task> ArrayJsonDocumentExample(NpgsqlConn } } - public static async Task ProvisionPoco(NpgsqlConnection conn) + public async Task InsertPoco() { /*** * Verify Npgsql POCO mapping with CrateDB. * https://www.npgsql.org/doc/types/json.html#poco-mapping */ - Console.WriteLine("Running ProvisionPoco"); - - // Submit DDL, create database schema. - await using (var cmd = new NpgsqlCommand("DROP TABLE IF EXISTS testdrive.poco", conn)) - { - cmd.ExecuteNonQuery(); - } - - await using (var cmd = new NpgsqlCommand(""" - CREATE TABLE testdrive.poco ( - "array" ARRAY(OBJECT(DYNAMIC)), - "object" OBJECT(DYNAMIC) - ); - """, conn)) - { - cmd.ExecuteNonQuery(); - } + Console.WriteLine("Running InsertPoco"); // Insert single data point. await using (var cmd = new NpgsqlCommand(""" - INSERT INTO testdrive.poco ( - "array", + INSERT INTO testdrive.example ( + "array_object", "object" ) VALUES ( @array, @@ -298,22 +293,20 @@ INSERT INTO testdrive.poco ( } // Flush data. - await using (var cmd = new NpgsqlCommand("REFRESH TABLE testdrive.poco", conn)) - { - cmd.ExecuteNonQuery(); - } + await RefreshTable(); } - public static async Task ObjectPocoExample(NpgsqlConnection conn) + public async Task ObjectPocoExample() { Console.WriteLine("Running ObjectPocoExample"); // Provision data. - await ProvisionPoco(conn); + await CreateTable(); + await InsertPoco(); // Query back data. - await using (var cmd = new NpgsqlCommand("SELECT * FROM testdrive.poco", conn)) + await using (var cmd = new NpgsqlCommand("SELECT * FROM testdrive.example", conn)) await using (var reader = cmd.ExecuteReader()) { reader.Read(); @@ -323,19 +316,20 @@ public static async Task ObjectPocoExample(NpgsqlConnection conn) } } - public static async Task> ArrayPocoExample(NpgsqlConnection conn) + public async Task> ArrayPocoExample() { Console.WriteLine("Running ArrayPocoExample"); // Provision data. - await ProvisionPoco(conn); + await CreateTable(); + await InsertPoco(); // Query back data. - await using (var cmd = new NpgsqlCommand("SELECT * FROM testdrive.poco", conn)) + await using (var cmd = new NpgsqlCommand("SELECT * FROM testdrive.example", conn)) await using (var reader = cmd.ExecuteReader()) { reader.Read(); - var obj = reader.GetFieldValue>("array"); + var obj = reader.GetFieldValue>("array_object"); Console.WriteLine(obj[0]); Console.WriteLine(obj[1]); return obj; diff --git a/by-language/csharp-npgsql/tests/DemoProgramTest.cs b/by-language/csharp-npgsql/tests/DemoProgramTest.cs index dafa5238..a84528e4 100644 --- a/by-language/csharp-npgsql/tests/DemoProgramTest.cs +++ b/by-language/csharp-npgsql/tests/DemoProgramTest.cs @@ -92,7 +92,7 @@ public async Task TestAllTypesNativeExample() var conn = fixture.Db; // Provision data. - var task = DatabaseWorkloadsMore.AllTypesNativeExample(conn); + var task = new DatabaseWorkloadsTypes(conn).AllTypesNativeExample(); var dt = await task.WaitAsync(TimeSpan.FromSeconds(0.5)); // Check results. @@ -146,7 +146,7 @@ public async Task TestContainerTypesExample() var conn = fixture.Db; // Provision data. - var task = DatabaseWorkloadsMore.AllTypesNativeExample(conn); + var task = new DatabaseWorkloadsTypes(conn).AllTypesNativeExample(); await task.WaitAsync(TimeSpan.FromSeconds(0.5)); // Run an SQL query indexing into ARRAY types. @@ -175,7 +175,7 @@ public async Task TestObjectJsonDocumentExample() var conn = fixture.Db; // Invoke database workload. - var task = DatabaseWorkloadsMore.ObjectJsonDocumentExample(conn); + var task = new DatabaseWorkloadsTypes(conn).ObjectJsonDocumentExample(); var obj = await task.WaitAsync(TimeSpan.FromSeconds(0.5)); // Validate the outcome. @@ -188,7 +188,7 @@ public async Task TestObjectPocoExample() var conn = fixture.Db; // Invoke database workload. - var task = DatabaseWorkloadsMore.ObjectPocoExample(conn); + var task = new DatabaseWorkloadsTypes(conn).ObjectPocoExample(); var obj = await task.WaitAsync(TimeSpan.FromSeconds(0.5)); // Validate the outcome. @@ -202,7 +202,7 @@ public async Task TestArrayPocoExample() var conn = fixture.Db; // Invoke database workload. - var task = DatabaseWorkloadsMore.ArrayPocoExample(conn); + var task = new DatabaseWorkloadsTypes(conn).ArrayPocoExample(); var obj = await task.WaitAsync(TimeSpan.FromSeconds(0.5)); // Validate the outcome.