public override Task <bool> Open(long auditKey, SelectQuery query, CancellationToken cancellationToken)
        {
            AuditKey = auditKey;

            try
            {
                //if (_isOpen)
                //{
                //    throw new ConnectionException($"The webservice is already open");
                //}

                var rowCreator = new ReaderRowCreator();
                rowCreator.InitializeRowCreator(1, 1, 1);
                ReferenceTransform = rowCreator;

                _filter = query?.Filters;
                if (_filter == null)
                {
                    _filter = new List <Filter>();
                }

                _isOpen = true;

                //create a dummy inreader to allow fieldcount and other queries to work.
                return(Task.FromResult(true));
            }
            catch (Exception ex)
            {
                throw new ConnectionException($"Opening the web service reader failed. {ex.Message}", ex);
            }
        }
        public override async Task <ReturnValue> Open(Int64 auditKey, SelectQuery query, CancellationToken cancelToken)
        {
            AuditKey = auditKey;
            try
            {
                if (_isOpen)
                {
                    return(new ReturnValue(false, "The web service connection is already open.", null));
                }

                var wsResult = await((ConnectionSoap)ReferenceConnection).GetWebService();
                if (wsResult.Success == false)
                {
                    return(wsResult);
                }

                _webServiceObject = wsResult.Value;
                _webServiceType   = _webServiceObject.GetType();

                //if no driving table is set, then use the row creator to simulate a single row.
                if (ReferenceTransform == null)
                {
                    ReaderRowCreator rowCreator = new ReaderRowCreator();
                    rowCreator.InitializeRowCreator(1, 1, 1);
                    ReferenceTransform = rowCreator;
                }
                else
                {
                    var result = await ReferenceTransform.Open(auditKey, null, cancelToken);

                    if (!result.Success)
                    {
                        return(result);
                    }
                }

                //create a dummy inreader to allow fieldcount and other queries to work.
                return(new ReturnValue(true));
            }
            catch (Exception ex)
            {
                return(new ReturnValue(false, "The following error occurred when starting the web service: " + ex.Message, ex));
            }
        }
        public (Transform sourceTransform, Table sourceTable) GetSourceTransform(DexihHub hub, DexihDatalinkTable hubDatalinkTable, InputColumn[] inputColumns, TransformWriterOptions transformWriterOptions)
        {
            try
            {
                Transform sourceTransform;
                Table     sourceTable;
                var       referenceTableAlias = hubDatalinkTable.Key.ToString();

                switch (hubDatalinkTable.SourceType)
                {
                case ESourceType.Datalink:
                    var datalink = hub.DexihDatalinks.SingleOrDefault(c => c.Key == hubDatalinkTable.SourceDatalinkKey);
                    if (datalink == null)
                    {
                        throw new TransformManagerException($"The source datalink with the key {hubDatalinkTable.SourceDatalinkKey} was not found");
                    }

                    (sourceTransform, sourceTable) = CreateRunPlan(hub, datalink, inputColumns, null, false, null);
                    break;

                case ESourceType.Table:
                    if (hubDatalinkTable.SourceTableKey == null)
                    {
                        throw new TransformManagerException($"The source table key was null.");
                    }

                    var sourceDbTable = hub.GetTableFromKey(hubDatalinkTable.SourceTableKey.Value);
                    if (sourceDbTable == null)
                    {
                        throw new TransformManagerException($"The source table with the key {hubDatalinkTable.SourceTableKey.Value} could not be found.");
                    }

                    var sourceDbConnection = hub.DexihConnections.SingleOrDefault(c => c.Key == sourceDbTable.ConnectionKey && c.IsValid);

                    if (sourceDbConnection == null)
                    {
                        throw new TransformException($"The connection with key {sourceDbTable.ConnectionKey} could not be found.");
                    }

                    var sourceConnection = sourceDbConnection.GetConnection(_transformSettings);
                    sourceTable = sourceDbTable.GetTable(hub, sourceConnection, inputColumns, _transformSettings, referenceTableAlias);
                    if (hubDatalinkTable.DisableVersioning)
                    {
                        sourceTable.IsVersioned = false;
                    }
                    sourceTransform = sourceConnection.GetTransformReader(sourceTable, transformWriterOptions.PreviewMode);
                    break;

                case ESourceType.Rows:
                    var rowCreator = new ReaderRowCreator();
                    rowCreator.InitializeRowCreator(hubDatalinkTable.RowsStartAt ?? 1, hubDatalinkTable.RowsEndAt ?? 1, hubDatalinkTable.RowsIncrement ?? 1);
                    rowCreator.TableAlias = hubDatalinkTable.Key.ToString();
                    sourceTable           = rowCreator.GetTable();
                    sourceTransform       = rowCreator;
                    break;

                case ESourceType.Function:
                    sourceTable = hubDatalinkTable.GetTable(null, inputColumns);
                    var data = new object[sourceTable.Columns.Count];
                    for (var i = 0; i < sourceTable.Columns.Count; i++)
                    {
                        data[i] = sourceTable.Columns[i].DefaultValue;
                    }
                    sourceTable.Data.Add(data);
                    var defaultRow = new ReaderDynamic(sourceTable);
                    defaultRow.Reset();
                    sourceTransform = defaultRow;
                    break;

                default:
                    throw new TransformManagerException($"Error getting the source transform.");
                }

                sourceTransform.TableAlias = referenceTableAlias;

                // compare the table in the transform to the source datalink columns.  If any are missing, add a mapping
                // transform to include them.
                var transformColumns = sourceTransform.CacheTable.Columns;
                var datalinkColumns  = hubDatalinkTable.DexihDatalinkColumns;


                // add a mapping transform to include inputColumns.
                var mappings = new Mappings();

                foreach (var column in datalinkColumns.Where(c => c.DeltaType != EDeltaType.IgnoreField))
                {
                    var transformColumn = transformColumns.SingleOrDefault(c => c.Name == column.Name);
                    if (transformColumn == null)
                    {
                        var newColumn = column.GetTableColumn(inputColumns);
                        mappings.Add(new MapInputColumn(newColumn));
                    }
                    else
                    {
                        transformColumn.DeltaType           = column.DeltaType;
                        transformColumn.IsIncrementalUpdate = column.IsIncrementalUpdate;
                    }
                }

                if (mappings.Count > 0)
                {
                    sourceTransform = new TransformMapping(sourceTransform, mappings)
                    {
                        Name = "Input Column Mapping"
                    };
                }

                sourceTransform.IgnoreQuery = hubDatalinkTable.DisablePushDown;

                return(sourceTransform, sourceTable);
            }
            catch (Exception ex)
            {
                throw new TransformManagerException($"Get source transform failed.  {ex.Message}", ex);
            }
        }