Skip to content

Repository files navigation

Build Status

PgPartner (PostgreSQL Partner)

This .NET library extends Npgsql functions to simplify certain PostgreSQL tasks, including:

BulkAdd (NpgsqlConnection extension)

This command utilizes the PostgreSQL COPY function to perform an optimized insert of .NET objects into a PostgreSQL database table of your choice.

Example (sample .NET app can be found in src directory)

Assume you have a table such as:

public."Samples"

Column NameData Type
iduuid
namevarchar
sumint
amountdecimal

Using BulkAdd you can insert all domain model objects like so:

Sync

// Domain Model Objectsvarsamples=newList<Sample>(){newSample{Id=Guid.NewGuid(),Name="Test",ItemSum=200,ItemAmount=10},newSample{Id=Guid.NewGuid(),Name="Test 2",ItemSum=400,ItemAmount=20},newSample{Id=Guid.NewGuid(),Name="Test 3",ItemSum=800,ItemAmount=30},newSample{Id=Guid.NewGuid(),Name="Test 4",ItemSum=1200,ItemAmount=40},newSample{Id=Guid.NewGuid(),Name="Test 5",ItemSum=2400,ItemAmount=50}};// Instantiate and open PostgreSQL database connectionusingvarconn=newNpgsqlConnection("<connection string>");conn.Open();// Execute BulkAdd by passing objects to insert into table, single object mapping, database schema, and database tableconn.BulkAdd(samples,(mapper,sample)=>{mapper.Map("id",sample.Id,NpgsqlDbType.Uuid);mapper.Map("name",sample.Name,NpgsqlDbType.Text);mapper.Map("amount",sample.ItemAmount,NpgsqlDbType.Numeric);mapper.Map("sum",sample.ItemSum,NpgsqlDbType.Integer);},"public","\"Samples\"");

Async

// Domain Model Objectsvarsamples=newList<Sample>(){newSample{Id=Guid.NewGuid(),Name="Test",ItemSum=200,ItemAmount=10},newSample{Id=Guid.NewGuid(),Name="Test 2",ItemSum=400,ItemAmount=20},newSample{Id=Guid.NewGuid(),Name="Test 3",ItemSum=800,ItemAmount=30},newSample{Id=Guid.NewGuid(),Name="Test 4",ItemSum=1200,ItemAmount=40},newSample{Id=Guid.NewGuid(),Name="Test 5",ItemSum=2400,ItemAmount=50}};// Instantiate and open PostgreSQL database connectionusingvarconn=newNpgsqlConnection("<connection string>");awaitconn.OpenAsync();// Execute BulkAdd by passing objects to insert into table, single object mapping, database schema, and database tableawaitconn.BulkAddAsync(samples,(mapper,sample)=>{mapper.Map("id",sample.Id,NpgsqlDbType.Uuid);mapper.Map("name",sample.Name,NpgsqlDbType.Text);mapper.Map("amount",sample.ItemAmount,NpgsqlDbType.Numeric);mapper.Map("sum",sample.ItemSum,NpgsqlDbType.Integer);},"public","\"Samples\"");

Notes

  • To specify quote wrapped schema, table, or columns, simply pass the quotes in directly (see how this is done for "Samples" table in example above)
  • Make sure to correctly specify association of NpgsqlDbType to .NET CLR type, otherwise you will see various errors.

CopyTableAsTemp (NpgsqlConnection extension)

This command will create a temporary table that mirrors an existing table. This is useful for scenarios where you might need to synchronize old and new data for any table. In such a scenario you can leverage this command to create a temporary table that mirrors your existing table, use BulkAdd to add all records to the temporary table and then execute sync operations such as upsert/delete/etc.

Example (sample .NET app can be found in src directory)

Assume you have a table such as:

public."Samples"

Column NameData Type
iduuid
namevarchar
sumint
amountdecimal

Using CopyTableAsTemp you can mirror this table as a new temporary table like:

Sync

// Instantiate and open PostgreSQL database connectionusingvarconn=newNpgsqlConnection("<connection string>");conn.Open();// Execute a copy table as temporary table operationvartableDetails=conn.CopyTableAsTemp("public","\"Samples\"");// At this point a new table with the name of "tmp_samples" will exist. The tableDetails variable will contain all details of the newly created table.

If you'd like to create a temp table with your own name, simply pass in the name you'd like to create the temporary table with, like so:

...// Execute a copy table as temporary table operationvar tableDetails =conn.CopyTableAsTemp("public","\"Samples\"","temp_mytable");// At this point a new table with the name of "temp_mytable" will exist. The tableDetails variable will contain all details of the newly created table.

Async

// Instantiate and open PostgreSQL database connectionusingvarconn=newNpgsqlConnection("<connection string>");conn.Open();// Execute a copy table as temporary table operationvartableDetails=awaitconn.CopyTableAsTempAsync("public","\"Samples\"");// At this point a new table with the name of "tmp_samples" will exist. The tableDetails variable will contain all details of the newly created table.

About

PostgreSQL .NET Library that extends various Npgsql functions

Topics

Resources

Stars

8 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages