The C# SqlAggregator Class

SqlAggregator provides aggregated SQL execution over multiple SqlLocalConnection shards within a single process. It exposes the common ISqlConnection contract while routing statements and queries to one or more local SQL engines.

For an overview see page SQL Classes

Class Definition

    public class SqlAggregator : ISqlConnection, IDisposable
    {
        // ===== CONSTRUCTORS =====
        public SqlAggregator(Database db, SqlLocalConnection[] engines);
        public SqlAggregator(Database db, SqlLocalConnection[] engines, int bufferSize);

        // ===== PROPERTIES =====
        public IDatabaseWrapper Wrapper { get; }

        // ===== QUERY EXECUTION METHODS =====
        public SqlResultSet ExecuteQuery(String query, params Object[] args);
        public int ExecuteStatement(String stmt, params Object[] args);

        // ===== LIFECYCLE METHODS =====
        public void Disconnect();
        public void Dispose();

        // ===== INTERNAL MANAGEMENT =====
        public void DetachResultSet(SqlResultSet result);
    }

Member Descriptions

SqlAggregator(Database, SqlLocalConnection[])
Creates an SQL aggregator over the specified local SQL engines. Uses the default internal statement buffer size of 64 KB.
SqlAggregator(Database, SqlLocalConnection[], int)
Creates an SQL aggregator with a custom internal statement buffer size. This buffer is used to serialize SQL text and parameters for execution across the aggregated engines.
Wrapper
Gets the internal IDatabaseWrapper instance for low-level database operations. Part of the ISqlConnection interface contract.
ExecuteQuery(String, params Object[])
Executes a parameterized SQL query through the aggregator and returns a SqlResultSet for iterating through the aggregated rows. If another result set is still attached to the aggregator, it is closed before the new query is executed. Throws SqlException if the aggregator is already closed.
ExecuteStatement(String, params Object[])
Executes a parameterized SQL statement through the aggregator. Depending on the SQL prefix, the statement can target one shard, all shards, or a selected routing mode. The return value is the statement count reported by the aggregated execution. Throws SqlException if the aggregator is already closed.
Disconnect()
Closes the aggregator: disposes any active result set, releases the native aggregator handle, and marks the aggregator as closed. This method does not disconnect the underlying SqlLocalConnection shards.
Dispose()
Releases resources by calling Disconnect(). Implements the IDisposable pattern for use with using statements.
DetachResultSet(SqlResultSet)
Internal callback used by SqlResultSet when the active result set is closed or disposed. Applications should not call this method directly.
Aggregation Model
The SqlAggregator behaves like a distributed SQL connection within one process. It works over an array of SqlLocalConnection shards and can execute statements against all shards or against selected shards depending on the SQL text.
Shard Targeting
Aggregator SQL statements can use shard prefixes such as 1:, 2:, or ?: to control where a statement is executed. Statements without an explicit shard prefix can be broadcast to all aggregated shards.
Thread Safety
Each SqlAggregator instance is not thread-safe. Use separate aggregator instances per thread. The underlying shard connections must remain valid for the aggregator's lifetime and should not be used directly elsewhere while the aggregator is active.
Parameter Binding
Both ExecuteQuery and ExecuteStatement support parameter binding through the params Object[] argument list. In most cases the standard ? placeholder can be used, while the SQL engine also supports typed %x placeholders when explicit argument formatting is required.
Resource Management
Always call Dispose() or use a using block to ensure timely release of native aggregator resources. The aggregator does not own the underlying engine connections; they must be disconnected separately after the aggregator is closed.
Error Handling
Methods throw SqlException if the aggregator is already closed. Query execution errors from individual engines are propagated to the caller.
String Encoding
Uses the database's defaultStringEncoding for converting .NET strings to the wire format expected by the eXtremeDB engine.
SQL String Length
For SqlAggregator, the maximum SQL statement or query string length is limited by the bufferSize constructor argument. The default value is 64 KB.