Skip to content

Commit

Permalink
Npgsql: Harmonize DDL vs. data provisioning in DemoTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Dec 20, 2024
1 parent 535be78 commit 999bc20
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 61 deletions.
12 changes: 7 additions & 5 deletions by-language/csharp-npgsql/DemoProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ await Parser.Default.ParseArguments<Options>(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();
});

Expand Down
96 changes: 45 additions & 51 deletions by-language/csharp-npgsql/DemoTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,19 @@ public class AllTypesRecord
public IList<string>? 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))
Expand Down Expand Up @@ -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,
Expand All @@ -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("""
Expand Down Expand Up @@ -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<DataTable> AllTypesNativeExample(NpgsqlConnection conn)
public async Task<DataTable> 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))
Expand All @@ -190,18 +209,12 @@ public static async Task<DataTable> AllTypesNativeExample(NpgsqlConnection conn)

}

public static async Task<JsonDocument> ObjectJsonDocumentExample(NpgsqlConnection conn)
public async Task<JsonDocument> 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 (
Expand All @@ -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))
Expand All @@ -232,12 +242,13 @@ INSERT INTO testdrive.example (
}
}

public static async Task<List<JsonDocument>> ArrayJsonDocumentExample(NpgsqlConnection conn)
public async Task<List<JsonDocument>> 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))
Expand All @@ -253,34 +264,18 @@ public static async Task<List<JsonDocument>> 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,
Expand All @@ -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<BasicPoco> ObjectPocoExample(NpgsqlConnection conn)
public async Task<BasicPoco> 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();
Expand All @@ -323,19 +316,20 @@ public static async Task<BasicPoco> ObjectPocoExample(NpgsqlConnection conn)
}
}

public static async Task<List<BasicPoco>> ArrayPocoExample(NpgsqlConnection conn)
public async Task<List<BasicPoco>> 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<List<BasicPoco>>("array");
var obj = reader.GetFieldValue<List<BasicPoco>>("array_object");
Console.WriteLine(obj[0]);
Console.WriteLine(obj[1]);
return obj;
Expand Down
10 changes: 5 additions & 5 deletions by-language/csharp-npgsql/tests/DemoProgramTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down

0 comments on commit 999bc20

Please sign in to comment.