Table of Contents

Class PostgresDataContext

Namespace
NextORM.Postgres
Assembly
nextorm.postgres.dll

Data context for PostgreSQL. Wraps the Npgsql driver for connections and parameters and renders SQL through PostgresDialect.

public class PostgresDataContext : DataContext, IDataContext, IQueryExecutor, IQueryMaterializer, IQueryPlanner, IRowReaderFactory, IQueryCache, IContextEnvironment, IRawCommandExecutor, IAsyncDisposable, IDisposable, IConnectionManager, ITransactionManager
Inheritance
PostgresDataContext
Implements
Inherited Members
Extension Methods

Constructors

PostgresDataContext(DbConnection, DataContextBuilder)

Creates a PostgreSQL context over a caller-supplied connection, which the context does not dispose. The context is unversioned: every supported form is emitted.

public PostgresDataContext(DbConnection connection, DataContextBuilder optionsBuilder)

Parameters

connection DbConnection

An already-created DbConnection owned by the caller.

optionsBuilder DataContextBuilder

The options collected from DataContextBuilder.

PostgresDataContext(DbConnection, DataContextBuilder, Version?)

Creates a version-aware PostgreSQL context over a caller-supplied connection, which the context does not dispose.

public PostgresDataContext(DbConnection connection, DataContextBuilder optionsBuilder, Version? serverVersion)

Parameters

connection DbConnection

An already-created DbConnection owned by the caller.

optionsBuilder DataContextBuilder

The options collected from DataContextBuilder.

serverVersion Version

The PostgreSQL server version, or null for unset. Immutable per context type.

Exceptions

InvalidOperationException

A different version was already registered for this context type.

PostgresDataContext(string, DataContextBuilder)

Creates a PostgreSQL context that owns a connection built lazily from connectionString. The context is unversioned: every supported form is emitted.

public PostgresDataContext(string connectionString, DataContextBuilder optionsBuilder)

Parameters

connectionString string

The connection string used to create the NpgsqlConnection.

optionsBuilder DataContextBuilder

The options collected from DataContextBuilder.

PostgresDataContext(string, DataContextBuilder, Version?)

Creates a version-aware PostgreSQL context that owns a connection built lazily from connectionString.

public PostgresDataContext(string connectionString, DataContextBuilder optionsBuilder, Version? serverVersion)

Parameters

connectionString string

The connection string used to create the NpgsqlConnection.

optionsBuilder DataContextBuilder

The options collected from DataContextBuilder.

serverVersion Version

The PostgreSQL server version, or null for unset. Immutable per context type.

Exceptions

InvalidOperationException

A different version was already registered for this context type.

Properties

Dialect

SQL dialect used to render queries. Supplied by the provider; SQL generation depends on this interface rather than on the context itself.

public override ISqlDialect Dialect { get; }

Property Value

ISqlDialect

SupportsRawRowColumns

True: PostgreSQL can surface an anonymous ROW(...) record column or a caller-registered named composite as a single raw result-set column (#194).

protected override bool SupportsRawRowColumns { get; }

Property Value

bool

TimeOnlyProjectionRefusal

PostgreSQL time is a true time of day and the driver surfaces it as TimeSpan with microsecond precision, so the projection is supported.

protected override string? TimeOnlyProjectionRefusal { get; }

Property Value

string

Methods

BulkInsertRows(string, IReadOnlyList<string>, IReadOnlyList<IPropertyMetadata>, IEnumerable<object?[]>, int?, int?, Action<int>?, int, BulkCopyFlags)

Writes rows through COPY <table> (<cols>) FROM STDIN (FORMAT BINARY) with NpgsqlBinaryImporter. A finite MaxBatchSize starts one importer per bounded chunk (each completed on success and disposed on cancellation/error); an unbounded request streams every row into one importer without buffering the set.

protected override int BulkInsertRows(string tableName, IReadOnlyList<string> columnNames, IReadOnlyList<IPropertyMetadata> columns, IEnumerable<object?[]> rows, int? commandTimeoutSeconds, int? maxBatchSize, Action<int>? progress, int notifyEvery, BulkCopyFlags bulkCopy)

Parameters

tableName string

The rendered target table reference (schema-qualified, quoted when configured).

columnNames IReadOnlyList<string>

The convention-resolved (unquoted) written column names, in row order.

columns IReadOnlyList<IPropertyMetadata>

The mapped columns, in row order.

rows IEnumerable<object[]>

The rows to write; each array matches columnNames by ordinal.

commandTimeoutSeconds int?

The Timeout budget in whole seconds, or null for the provider default.

maxBatchSize int?

The maximum rows per importer, or null to stream the whole set into one importer.

progress Action<int>

Called with the cumulative written-row count after each completed chunk, or null.

notifyEvery int

The progress reporting interval in rows; the callback wrapper applies it.

bulkCopy BulkCopyFlags

The bulk-copy flags requested by the caller; COPY cannot express them.

Returns

int

The number of rows written.

BulkInsertRowsAsync(string, IReadOnlyList<string>, IReadOnlyList<IPropertyMetadata>, IAsyncEnumerable<object?[]>, int?, int?, Action<int>?, int, BulkCopyFlags, CancellationToken)

Asynchronously writes rows through a binary COPY.

protected override Task<int> BulkInsertRowsAsync(string tableName, IReadOnlyList<string> columnNames, IReadOnlyList<IPropertyMetadata> columns, IAsyncEnumerable<object?[]> rows, int? commandTimeoutSeconds, int? maxBatchSize, Action<int>? progress, int notifyEvery, BulkCopyFlags bulkCopy, CancellationToken cancellationToken)

Parameters

tableName string

The rendered target table reference (schema-qualified, quoted when configured).

columnNames IReadOnlyList<string>

The convention-resolved (unquoted) written column names, in row order.

columns IReadOnlyList<IPropertyMetadata>

The mapped columns, in row order.

rows IAsyncEnumerable<object[]>

The rows to write; each array matches columnNames by ordinal.

commandTimeoutSeconds int?

The Timeout budget in whole seconds, or null for the provider default.

maxBatchSize int?

The maximum rows per importer, or null to stream the whole set into one importer.

progress Action<int>

Called with the cumulative written-row count after each completed chunk, or null.

notifyEvery int

The progress reporting interval in rows; the callback wrapper applies it.

bulkCopy BulkCopyFlags

The bulk-copy flags requested by the caller; COPY cannot express them.

cancellationToken CancellationToken

Cancels execution.

Returns

Task<int>

A task producing the number of rows written.

CreateDbConnection(string?)

Creates a new NpgsqlConnection for connectionString.

protected override DbConnection CreateDbConnection(string? connectionString)

Parameters

connectionString string

The PostgreSQL connection string.

Returns

DbConnection

A new, unopened PostgreSQL connection.

CreateParam(string, object?)

Creates an NpgsqlParameter, mapping a null value to DBNull.Value and binding JSON documents, elements and nodes as jsonb so that the JSON operators accept them without an explicit cast.

public override DbParameter CreateParam(string name, object? value)

Parameters

name string

The parameter name, without the provider prefix.

value object

The parameter value, or null.

Returns

DbParameter

A new PostgreSQL parameter.

CreateProcedureParameter(ProcedureParameter)

Creates an NpgsqlParameter for a descriptor. A TableParameterValue is emulated: a scalar row type becomes a typed array (unnest(@p)/= ANY(@p)), an entity row type becomes a jsonb document (jsonb_to_recordset(@p)). A TypeName has no meaning on PostgreSQL and is rejected.

protected override DbParameter CreateProcedureParameter(ProcedureParameter parameter)

Parameters

parameter ProcedureParameter

The parameter descriptor.

Returns

DbParameter

A new PostgreSQL parameter configured from parameter.

Exceptions

ArgumentException

The descriptor carries a TypeName (SQL Server only), or a table parameter is not Input.

IsGenuineCompositeColumn(DbDataReader, int)

True only when reader is an Npgsql.NpgsqlDataReader that reports a genuine PostgreSQL composite for ordinal (a caller-registered named or struct composite). Any other reader, or a column whose catalog metadata is unknown (hstore/ltree or an unresolved cold composite → Npgsql.PostgresTypes.UnknownBackendType), returns false so classification never guesses from a data type name or an exception shape. The metadata is read from the existing reader; no second command, connection or type reload is used.

protected override bool IsGenuineCompositeColumn(DbDataReader reader, int ordinal)

Parameters

reader DbDataReader

The reader whose current result set is being classified.

ordinal int

The zero-based column ordinal.

Returns

bool

true when the driver reports a genuine composite type for the column.

MapColumnExpression(SelectExpression, Expression)

Materializes a projected Range<T> column from the driver's NpgsqlRange<T> through the typed GetFieldValue accessor, converting it to the provider-agnostic range. Every other column is mapped by the base implementation.

public override Expression MapColumnExpression(SelectExpression column, Expression param)

Parameters

column SelectExpression

The projected column being read.

param Expression

The data-reader expression the accessor is built from.

Returns

Expression

An expression that reads the column value.