Table of Contents

Class BaseSqlReader

Namespace
Dynamicweb.DataIntegration.Integration
Assembly
Dynamicweb.DataIntegration.dll

Base class for source readers that read rows from a Microsoft SQL Server database. A source provider returns a reader from GetReader(Mapping); the job engine then drains it row by row via IsDone() and GetNext(). The class implements ISourceReader on top of a SqlDataReader and also implements IResponseWriter, so values returned by the destination can be written back to the source table through the mapping's response column mappings.

public abstract class BaseSqlReader : ISourceReader, IDisposable, IResponseWriter
Inheritance
BaseSqlReader
Implements
Derived
Inherited Members

Remarks

How to create a custom SQL reader: Subclass this class, chain the BaseSqlReader(Mapping, SqlConnection) constructor, compose the source SELECT statement — typically from GetColumns() and GetFromTables() — assign it to _command and set _reader to the result of _command.ExecuteReader(). Override GetFromTables() or GetColumns() to customize joins and column selection. The built-in Dynamicweb.DataIntegration.Providers.SqlProvider.SqlSourceReader is the reference implementation of this pattern.

Things to be careful of:

  • The base class never executes the source query itself. Until the derived class assigns _reader, IsDone() returns true and GetNext() returns an empty dictionary, so the table is silently treated as empty.
  • IsDone() advances the underlying SqlDataReader. The engine calls it once before each GetNext(); calling it more often skips rows.
  • The BaseSqlReader(Mapping, SqlConnection) constructor opens the connection and applies the command timeout from the global setting /Globalsettings/Settings/DataIntegration/SQLSourceCommandTimeout to both the source command and the response write-back command.
  • The SQL helpers compose statements from mapping metadata — schema, table and column names are bracket-quoted into the SQL text, not parameterized — so they are intended for trusted mapping configuration only. Response write-back values are always parameterized.
  • Response write-back requires key columns. They are resolved from the response mappings marked as key, falling back to the mapping's key column mappings and finally to the source table's primary key columns; when none can be found the constructor throws InvalidOperationException.
  • Response updates run immediately (in chunks of 100) only when a dedicated response connection was supplied; otherwise they are buffered and executed in Close(). Always dispose the reader so buffered response SQL is flushed.
  • GetFromTables() and GetColumns() special-case the EcomAssortmentPermissions table, joining AccessUser to resolve customer numbers and external ids.
A reader that executes the mapped source query:
using System;
using Microsoft.Data.SqlClient;
using Dynamicweb.DataIntegration.Integration;

public class MySqlSourceReader : BaseSqlReader
{
    public MySqlSourceReader(Mapping mapping, SqlConnection connection)
        : base(mapping, connection)
    {
        if (_command is null)
            throw new InvalidOperationException("The reader command was not created.");

        _command.CommandText = "SELECT " + GetColumns() + " FROM " + GetFromTables();
        _reader = _command.ExecuteReader();
    }
}

Constructors

BaseSqlReader()

Initializes the reader with an empty mapping and no command or connection. Intended for derived classes that configure the reader themselves; call SetReaderMapping(Mapping) to apply the real mapping.

protected BaseSqlReader()

BaseSqlReader(Mapping, SqlConnection)

Initializes the SQL reader for the specified mapping. Opens the connection, creates _command and prepares the response write-back command on the same connection, which defers response SQL execution until Close().

public BaseSqlReader(Mapping mapping, SqlConnection connection)

Parameters

mapping Mapping

The mapping whose source table and response mappings should drive the reader.

connection SqlConnection

The SQL connection to read from; opened by the constructor when not already open.

Fields

_command

The command used to read source rows. Created by the BaseSqlReader(Mapping, SqlConnection) constructor; null when the protected parameterless constructor was used. The derived class assigns the SQL text and executes it.

protected SqlCommand? _command

Field Value

SqlCommand

_reader

The active data reader that IsDone() and GetNext() operate on. Assigned by the derived class after executing the source query on _command; while null the source is treated as empty.

protected SqlDataReader? _reader

Field Value

SqlDataReader

mapping

The mapping whose source table drives the reader. Use SetReaderMapping(Mapping) to change it so the response-mapping metadata derived from it is refreshed as well.

protected Mapping mapping

Field Value

Mapping

Properties

PkColumnsForResponseMapping

Gets the source primary key columns used as fallback keys for response write-back. Only populated when the mapping has response mappings but neither the response mappings nor the column mappings declare key columns; GetColumns() appends these columns to the select list so the keys are available while reading.

protected IEnumerable<string> PkColumnsForResponseMapping { get; }

Property Value

IEnumerable<string>

Methods

Close()

Executes any buffered response write-back SQL and disposes the response command and connection. Does not close the source reader or its connection; Dispose() handles both.

public void Close()

Dispose()

Disposes the active SQL reader and releases the response write-back resources.

public void Dispose()

GetColumns()

Builds the complete, bracket-quoted select list for the source query: the mapped source columns, the fallback primary key columns needed for response write-back (see PkColumnsForResponseMapping) and the columns referenced only by mapping conditionals. For the EcomAssortmentPermissions table, customer number and external id columns are replaced with AccessUser subqueries. Override to customize the selected columns.

protected virtual string GetColumns()

Returns

string

The select list without a trailing separator.

GetColumnsFromMappingConditions()

Builds the bracket-quoted list of columns that are used by the mapping's conditionals but not covered by any column mapping, so conditional filtering can be applied to the rows being read.

protected string GetColumnsFromMappingConditions()

Returns

string

The column list, each column followed by ", "; empty when there are no conditionals.

GetColumnsFromMappingConditions(string[])

Builds the bracket-quoted list of columns that are used by the mapping's conditionals but not covered by any column mapping, excluding the specified columns.

protected string GetColumnsFromMappingConditions(string[] columnsToSkip)

Parameters

columnsToSkip string[]

The column names to leave out of the list; compared case-insensitively.

Returns

string

The column list, each column followed by ", "; empty when there are no conditionals.

GetDistinctColumnsFromMapping()

Builds the bracket-quoted list of distinct source columns used by the mapping's column mappings, including columns referenced from column-mapping code scripts.

protected string GetDistinctColumnsFromMapping()

Returns

string

The column list with a trailing ", ", ready for further columns to be appended.

GetDistinctColumnsFromMapping(string[])

Builds the bracket-quoted list of distinct source columns used by the mapping's column mappings, including columns referenced from column-mapping code scripts, excluding the specified columns.

protected string GetDistinctColumnsFromMapping(string[] columnsToSkip)

Parameters

columnsToSkip string[]

The column names to leave out of the list; compared case-insensitively.

Returns

string

The column list with a trailing ", ", ready for further columns to be appended.

GetFromTables()

Builds the FROM clause of the source query as [schema].[table] from the mapping's source table. For the EcomAssortmentPermissions table with user-related columns mapped, an AccessUser join is added. Override to read from a different table expression or add custom joins.

protected virtual string GetFromTables()

Returns

string

The table expression to select the source rows from.

GetNext()

Returns the current row from the active SQL reader, keyed by column name. Called by the job engine once per row after IsDone() returned false; the row position is advanced by IsDone(), not by this method.

public virtual Dictionary<string, object> GetNext()

Returns

Dictionary<string, object>

The current reader row, or an empty dictionary when no SQL reader has been initialized.

GetPrimaryKeyColumns(bool)

Gets the primary key column names of the mapping's source table.

protected IEnumerable<string> GetPrimaryKeyColumns(bool returnOnlyMissedInMappings = true)

Parameters

returnOnlyMissedInMappings bool

When true (the default), only primary key columns not already covered by a column mapping are returned.

Returns

IEnumerable<string>

The primary key column names; empty when the source table has none.

HandleResponseCommands(IReadOnlyCollection<KeyValuePair<string, Dictionary<string, object?>>>)

Handles response write-back commands either immediately on a dedicated connection or later during Close().

protected virtual void HandleResponseCommands(IReadOnlyCollection<KeyValuePair<string, Dictionary<string, object?>>> responseSqls)

Parameters

responseSqls IReadOnlyCollection<KeyValuePair<string, Dictionary<string, object>>>

The parameterized SQL update commands to execute for the current response rows.

IsDone()

Determines whether the active SQL reader has reached the end of the result set. Called by the job engine before each row; advances the underlying SqlDataReader to the next row and closes it when the end is reached. Returns true when _reader has not been assigned.

public virtual bool IsDone()

Returns

bool

true when no more rows are available; otherwise false.

SetReaderMapping(Mapping)

Applies the mapping used by the reader and refreshes response-mapping metadata derived from it.

protected void SetReaderMapping(Mapping targetMapping)

Parameters

targetMapping Mapping

The mapping that should drive source reads and response write-back behavior.

Write(Dictionary<string, object>)

Writes response-mapped values back to the source table for the current row. Called through IResponseWriter by destination providers that produce response data (for example the OData provider) while the reader is positioned on the row the response belongs to; each value becomes a parameterized UPDATE against the source table keyed by the resolved key columns. No-op when the row is empty or the mapping has no response mappings.

public void Write(Dictionary<string, object> row)

Parameters

row Dictionary<string, object>

The response row containing the destination values that should be written back.

Exceptions

InvalidOperationException

Thrown when response mappings depend on key values that are missing from row, or when fallback key resolution requires an active reader that is not available.

To top