Skip to main content

TdsQueryExecutor

  • Namespace: PPDS.Dataverse.Query
  • Assembly: PPDS.Dataverse
  • Kind: Class
  • Implements: ITdsQueryExecutor

Summary

Executes SQL queries against Dataverse's TDS Endpoint using the SQL Server wire protocol. The TDS Endpoint provides read-only access to Dataverse data on port 5558.

Members

Constructors

TdsQueryExecutor(string orgUrl, Func<CancellationToken, Task<string>> tokenProvider, ILogger<TdsQueryExecutor> logger)

TdsQueryExecutor(string orgUrl, Func<CancellationToken, Task<string>> tokenProvider, ILogger<TdsQueryExecutor> logger)

Initializes a new instance of the PPDS.Dataverse.Query.TdsQueryExecutor class.

  • Param orgUrl: The Dataverse organization URL (e.g., "https://org.crm.dynamics.com").

  • Param tokenProvider: A function that provides a valid MSAL access token for the Dataverse environment. Called for each query execution to ensure fresh tokens.

  • Param logger: Optional logger.

Methods

BuildConnectionString

string BuildConnectionString(string orgHost)

Builds the SQL Server connection string for the TDS Endpoint.

  • Param orgHost: The normalized org hostname.

  • Returns: The connection string.

ExecuteSqlAsync

Task<QueryResult> ExecuteSqlAsync(string sql, Nullable<int> maxRows, CancellationToken cancellationToken)

Executes a SQL query against the Dataverse TDS Endpoint and returns structured results.

  • Param sql: The SQL SELECT query to execute.

  • Param maxRows: Optional maximum number of rows to return. If null, returns all rows.

  • Param cancellationToken: Cancellation token.

  • Returns: The query result containing records, columns, and execution metadata.

ExtractColumnsFromReader

List<QueryColumn> ExtractColumnsFromReader(SqlDataReader reader)

Extracts column metadata from the SqlDataReader schema.

InferEntityName

string InferEntityName(string sql)

Attempts to infer the entity logical name from a SQL statement. Looks for the FROM clause and extracts the table name.

MapClrTypeToColumnType

QueryColumnType MapClrTypeToColumnType(Type clrType)

Maps a CLR type from SqlDataReader to a PPDS.Dataverse.Query.QueryColumnType.

MapReaderRow

Dictionary<string, QueryValue> MapReaderRow(SqlDataReader reader, List<QueryColumn> columns)

Maps a single row from the SqlDataReader to a record dictionary.

MapSqlValue

QueryValue MapSqlValue(object value)

Maps a raw SQL value to a PPDS.Dataverse.Query.QueryValue. Handles DBNull, common .NET types, and falls back to string representation.

Fields

TdsPort

int TdsPort

The port used by the Dataverse TDS Endpoint.