示例#1
0
        public TargetEntitySqlModel(DataSyncSqlModel dataSyncSqlModel, Target dbsh, IShellContext context)
        {
            this._dataSyncSqlModel = dataSyncSqlModel;
            this._dbsh = dbsh;
            TargetTable = new NameWithSchema(context.Replace(dbsh.TableSchema), context.Replace(dbsh.TableName));
            string findSchema = dbsh.TableSchema;
            if (findSchema != null && findSchema.StartsWith(NameWithSchema.NoQuotePrefix)) findSchema = null;
            Structure = dataSyncSqlModel.TargetStructure.FindTableLike(findSchema, TargetTable.Name);
            SqlAlias = _dbsh.Alias ?? "dst_" + _dataSyncSqlModel.Entities.Count;

            foreach (var col in dbsh.Columns)
            {
                var targetCol = new TargetNoRefColumnSqlModel(col, FindColumnInfo(col.Name));
                TargetColumns.Add(targetCol);

                foreach (string alias in ExtractColumnSources(col))
                {
                    SourceColumnSqlModel source = null;
                    if (dataSyncSqlModel.SourceGraphModel == null)
                    {
                        // flat sync
                        if (!String.IsNullOrEmpty(dbsh.PrimarySource))
                        {
                            var tableSource = DataSync.FlatSources.FirstOrDefault(x => x.Match(Dbsh.PrimarySource));
                            if (tableSource != null)
                            {
                                source = tableSource.Columns.FirstOrDefault(x => x.Alias == alias);
                            }
                        }
                    }
                    else
                    {
                        source = dataSyncSqlModel.SourceGraphModel[alias];
                        //targetCol.Sources.Add(source);
                    }
                    RequiredSourceColumns.Add(source);
                    if (col.IsKey) KeySourceColumns.Add(source);
                }
            }

            if (!String.IsNullOrEmpty(_dbsh.Connection))
            {
                var ctxConn = new NormalizedDatabaseConnectionInfo(new DatabaseConnectionInfoHolder { ProviderString = context.GetDefaultConnection() });
                var tableConn = new NormalizedDatabaseConnectionInfo(new DatabaseConnectionInfoHolder { ProviderString = context.Replace(_dbsh.Connection), LinkedInfo = _dbsh.LinkedInfo });

                if (ctxConn != tableConn)
                {
                    if (ctxConn.ServerConnectionString == tableConn.ServerConnectionString)
                    {
                        TargetLinkedInfo = tableConn.GetLinkedInfo();
                    }
                    else
                    {
                        throw new IncorrectRdsDefinitionException($"DBSH-00000 RDS target must be reachable by database or linked server: ({TargetTable})");
                    }
                }
            }
        }
示例#2
0
        public void InitializeQuerySource(ITabularDataSource dataSource, IShellContext context, string sourceTableVariable, string sourceQueryVariable)
        {
            if (!_dbsh.ForceExternalSource)
            {
                // try to create non-external source

                var tableOrView = dataSource as DbShell.Core.Utility.TableOrView;

                if (!String.IsNullOrEmpty(sourceTableVariable))
                {
                    QuerySource = new DmlfSource
                    {
                        Alias = SqlAlias,
                        TableOrView = new NameWithSchema($"###({sourceTableVariable})###"),
                    };
                    TableName = new NameWithSchema(sourceTableVariable);
                    return;
                }

                if (!String.IsNullOrEmpty(sourceQueryVariable))
                {
                    QuerySource = new DmlfSource
                    {
                        Alias = SqlAlias,
                        SubQueryString = $"###({sourceQueryVariable})###",
                    };
                    return;

                }

                if (tableOrView != null)
                {
                    bool canUseTable = true;
                    LinkedDatabaseInfo linked = null;

                    var ctxConn = new NormalizedDatabaseConnectionInfo(new DatabaseConnectionInfoHolder { ProviderString = context.GetDefaultConnection() });
                    var tableConn = tableOrView.GetNormalizedConnectionInfo(context);

                    if (ctxConn != tableConn)
                    {
                        if (ctxConn.ServerConnectionString == tableConn.ServerConnectionString)
                        {
                            linked = tableConn.GetLinkedInfo();
                        }
                        else
                        {
                            canUseTable = false;
                        }
                    }

                    if (canUseTable)
                    {
                        TableName = tableOrView.GetFullName(context);
                        QuerySource = new DmlfSource
                        {
                            Alias = SqlAlias,
                            TableOrView = TableName,
                            LinkedInfo = linked,
                        };
                        return;
                    }
                }

                var query = dataSource as DbShell.Core.Query;
                if (query != null && query.GetProviderString(context) == context.GetDefaultConnection())
                {
                    string sql = context.Replace(query.Text);
                    QuerySource = new DmlfSource
                    {
                        Alias = SqlAlias,
                        SubQueryString = sql,
                    };
                    return;
                }
            }

            IsExternal = true;
            _externalDataName = new NameWithSchema(null, $"##{SqlAlias}_{new Random().Next(10000, 100000)}");
            QuerySource = new DmlfSource
            {
                Alias = SqlAlias,
                TableOrView = _externalDataName,
            };
            _dataSync.AddExternalSource(this);
        }