CommonNetFuncs.Sql.PostgreSql 5.0.0

dotnet add package CommonNetFuncs.Sql.PostgreSql --version 5.0.0
                    
NuGet\Install-Package CommonNetFuncs.Sql.PostgreSql -Version 5.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CommonNetFuncs.Sql.PostgreSql" Version="5.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CommonNetFuncs.Sql.PostgreSql" Version="5.0.0" />
                    
Directory.Packages.props
<PackageReference Include="CommonNetFuncs.Sql.PostgreSql" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CommonNetFuncs.Sql.PostgreSql --version 5.0.0
                    
#r "nuget: CommonNetFuncs.Sql.PostgreSql, 5.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CommonNetFuncs.Sql.PostgreSql@5.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CommonNetFuncs.Sql.PostgreSql&version=5.0.0
                    
Install as a Cake Addin
#tool nuget:?package=CommonNetFuncs.Sql.PostgreSql&version=5.0.0
                    
Install as a Cake Tool

CommonNetFuncs.Sql.PostgreSql

License NuGet Version NuGet Downloads

PostgreSQL/Npgsql implementation of IDirectQuery from CommonNetFuncs.Sql.Common. Targets net11.0.

Contents

Important behavior

  • Calls accept PostgreSQL SQL text, not a parameter collection. Protect the connection string and configure appropriate TLS and authentication.
  • Parameterize untrusted values with NpgsqlCommand and typed NpgsqlParameter values; do not interpolate them into the sql argument.
  • Use an explicit allow-list and PostgreSQL identifier quoting for dynamic identifiers.
  • commandTimeoutSeconds defaults to 30; maxRetry defaults to 3 total attempts. Retrying a non-idempotent statement can repeat its effect.
  • Table/update failures are logged through NLog. Table failures become an empty DataTable; update failures become the default UpdateResult.
  • Streaming and direct mapping require T : class, new() and exact, case-sensitive mapping to public writable property names. DBNull becomes the property's default value; unsupported conversions/missing columns can cause DataException.
  • The direct-query contract has no transaction parameter.

Construction

The constructor has one optional Func<string, NpgsqlConnection> factory:

using CommonNetFuncs.Sql.PostgreSql;
using Npgsql;

DirectQuery defaultFactory = new();
DirectQuery customFactory = new(connectionString =>
    new NpgsqlConnection(connectionString));

Each operation creates a provider connection and command; the wrapper owns and disposes them. Async-iterator setup is deferred until enumeration starts.

All DirectQuery operations

The examples demonstrate every public operation. They contain only trusted static SQL:

using System.Data;
using CommonNetFuncs.Sql.Common;
using CommonNetFuncs.Sql.PostgreSql;

const string connectionString =
    "Host=db.example;Database=app;Username=app;Password=from-secret-store;Ssl Mode=Require";

DirectQuery queries = new();
CancellationToken cancellationToken = CancellationToken.None;

// GetDataTableAsync
using DataTable asyncTable = await queries.GetDataTableAsync(
    """SELECT id, name FROM customers WHERE is_active = TRUE""",
    connectionString, 30, 3, cancellationToken);

// GetDataTable
using DataTable table = queries.GetDataTable(
    """SELECT id, name FROM customers WHERE is_active = TRUE""",
    connectionString, 30, 3);

// RunUpdateQueryAsync
UpdateResult asyncResult = await queries.RunUpdateQueryAsync(
    """UPDATE customers SET is_active = TRUE WHERE id = 42""",
    connectionString, 30, 1, cancellationToken);

// RunUpdateQuery
UpdateResult result = queries.RunUpdateQuery(
    """UPDATE customers SET is_active = TRUE WHERE id = 42""",
    connectionString, 30, 1);

// GetDataStreamAsync<T>
await foreach (Customer row in queries.GetDataStreamAsync<Customer>(
    """SELECT id AS "Id", name AS "Name" FROM customers ORDER BY id""",
    connectionString, 30, 3, useCache: true, cancellationToken))
{
    await ProcessAsync(row, cancellationToken);
}

// GetDataStream<T> (see synchronous-stream limitation below)
IEnumerable<Customer> stream = queries.GetDataStream<Customer>(
    """SELECT id AS "Id", name AS "Name" FROM customers ORDER BY id""",
    connectionString, 30, 3, useCache: true, cancellationToken);
foreach (Customer row in stream)
{
    Process(row);
}

// GetDataDirectAsync<T>
IEnumerable<Customer> buffered = await queries.GetDataDirectAsync<Customer>(
    """SELECT id AS "Id", name AS "Name" FROM customers ORDER BY id""",
    connectionString, 30, 3, useCache: true, cancellationToken);

Column aliases must match CLR property names exactly. The example aliases folded PostgreSQL identifiers to the exact CLR names:

public sealed class Customer
{
    public int Id { get; set; }     // SELECT id AS "Id"
    public string? Name { get; set; } // SELECT name AS "Name"
}

For updates, verify both fields:

UpdateResult update = await queries.RunUpdateQueryAsync(sql, connectionString);
if (!update.Success || update.RecordsChanged != 1)
{
    // Handle failure or an unexpected affected-row count.
}

An empty DataTable is ambiguous and can represent either zero matches or a logged failure.

Streaming and ownership

GetDataStreamAsync<T> owns its connection/command until async enumeration completes, is canceled, or is disposed. Fully enumerate or promptly dispose the enumerator. The maxRetry wrapper does not currently surround exceptions raised during MoveNextAsync, so runtime failures during enumeration may not be retried.

GetDataStream<T> currently creates a lazy enumerable and disposes its outer provider connection/command scope on return. Do not rely on deferred enumeration through this provider overload. Prefer the async method, or call CommonNetFuncs.Sql.Common.DirectQuery.GetDataStream<T> with a caller-owned NpgsqlConnection.

GetDataDirectAsync<T> materializes a list, closes its connection, and throws DataException after retries are exhausted. Cancellation and other execution errors are included in that retry path.

Parameterized PostgreSQL SQL

Use Npgsql directly when values come from any untrusted source:

using Npgsql;

await using NpgsqlConnection connection = new(connectionString);
await connection.OpenAsync(cancellationToken);
await using NpgsqlCommand command = new(
    """SELECT id AS "Id", name AS "Name" FROM customers WHERE tenant_id = $1""",
    connection);
command.Parameters.Add(new NpgsqlParameter<int> { Value = tenantId });

await using NpgsqlDataReader reader =
    await command.ExecuteReaderAsync(cancellationToken);
while (await reader.ReadAsync(cancellationToken))
{
    Console.WriteLine($"{reader.GetInt32(0)} {reader.GetString(1)}");
}

Transactions

The DirectQuery methods do not accept an existing transaction. Compose atomic operations on one provider connection:

await using NpgsqlConnection connection = new(connectionString);
await connection.OpenAsync(cancellationToken);
await using NpgsqlTransaction transaction =
    await connection.BeginTransactionAsync(cancellationToken);

try
{
    await using NpgsqlCommand command = connection.CreateCommand();
    command.Transaction = transaction;
    command.CommandText =
        "UPDATE accounts SET balance = balance - $2 WHERE id = $1";
    command.Parameters.Add(new NpgsqlParameter<int> { Value = sourceId });
    command.Parameters.Add(new NpgsqlParameter<decimal> { Value = amount });
    await command.ExecuteNonQueryAsync(cancellationToken);
    await transaction.CommitAsync(cancellationToken);
}
catch
{
    await transaction.RollbackAsync(cancellationToken);
    throw;
}

Diagnostic SQL rendering

CommonNetFuncs.Sql.PostgreSql.Debug.RenderCommandAsScript(DbCommand) is the package's additional public method. It produces a diagnostic script with parameter values inlined, including @name, :name, and $N forms:

using CommonNetFuncs.Sql.PostgreSql;
using Npgsql;
using NpgsqlTypes;

await using NpgsqlCommand command = new(
    "SELECT * FROM customers WHERE id = @id AND name = @name");
command.Parameters.Add(new NpgsqlParameter("id", NpgsqlDbType.Integer)
{
    Value = 42
});
command.Parameters.Add(new NpgsqlParameter("name", NpgsqlDbType.Text)
{
    Value = "O'Brian"
});

string diagnosticScript = Debug.RenderCommandAsScript(command);

The script is diagnostics only. It contains real values and is not safe to execute as a production parameterization mechanism. It supports nulls, numerics, dates/times, GUIDs, byte arrays, and enumerable parameters, but restrict access to the resulting logs.

Installation

dotnet add package CommonNetFuncs.Sql.PostgreSql

License

MIT. See LICENSE.

Product Compatible and additional computed target framework versions.
.NET net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
5.0.0 56 11/23/2026
4.5.5 114 11/11/2026
4.5.4 125 10/17/2026
4.5.3 119 10/15/2026
4.5.2 111 10/10/2026
4.5.0 110 10/9/2026
4.4.2 129 9/18/2026
4.4.1 130 9/15/2026
4.4.0 126 8/31/2026
4.3.2 135 8/24/2026
4.3.1 135 8/11/2026
4.3.0 141 8/3/2026
4.1.14 132 8/3/2026
4.1.12 134 7/31/2026
4.1.11 149 7/9/2026
4.1.0 148 6/5/2026
4.0.53 148 5/28/2026
4.0.43 137 5/14/2026
4.0.40 148 5/10/2026
4.0.39 142 5/10/2026
Loading failed