Exemplo n.º 1
0
        private IQueryable <TEntity> SyncWith(Sync sync)
        {
            if (!sync.Target.HasProjection)
            {
                var fields = GetSharedColumns(sync.Source, sync.Target.From.Table);
                Func <string, IList <SelectProjection> > createProjection =
                    alias => fields.Select(x => new SelectProjection {
                    Projection = new Projection {
                        Type  = Projection.ProjectionType.Field,
                        Field = new Field {
                            Name       = x.Projection.Field.Name,
                            Key        = x.Projection.Field.Key,
                            HasKey     = x.Projection.Field.HasKey,
                            TableAlias = alias
                        }
                    }
                }).ToList();
                sync.Target.Projection = createProjection(sync.Target.From.Alias);
                sync.Source.Projection = createProjection(sync.Source.From.Alias);
            }

            var statement = SyncWriter <TEntity> .CreateStatement(sync, _map);

            Command.Create(statement, _profiler).ExecuteNonQuery(_connectionManager);
            return(new Table <TEntity>(_connectionManager, sync.Target
                                       .From.Table.Name, _map, _profiler, _noLock));
        }
Exemplo n.º 2
0
        public void should_render_sync_include_sql()
        {
            var target = MockQueryable <Entity> .Create(TableName1);

            var source = MockQueryable <Entity> .Create(TableName2);

            var importId = Guid.NewGuid();

            target.Where(x => x.ImportId != importId).SyncWith(source.Where(x => x.ImportId == importId), x => x.Created, SyncFields.Include, x => x.Name, x => x.Flag);
            var query = QueryVisitor <Entity> .CreateModel(target.Expression, x => ((MockQueryable <Entity>)x).Name);

            query.Operation.ShouldEqual(Query.OperationType.SyncWith);
            query.SyncWith.ShouldNotBeNull();

            var sync = query.SyncWith;

            var statement = SyncWriter <Entity> .CreateStatement(sync, Map);

            statement.Result.ShouldEqual(Statement.ResultType.None);
            statement.Parameters.Count.ShouldEqual(2);
            statement.Parameters.First().Value.ShouldEqual(importId);
            statement.Parameters.Skip(1).First().Value.ShouldEqual(importId);
            statement.Text.ShouldEqual(string.Format(
                                           "UPDATE [{0}] SET [{0}].[name] = [{1}].[name], [{0}].[flag] = [{1}].[flag] FROM [{2}] [{0}] INNER JOIN [{3}] [{1}] ON [{0}].[created] = [{1}].[created] AND " +
                                           "([{1}].[import_id] = @{4}) WHERE (([{0}].[import_id] <> @{5}) OR [{0}].[import_id] IS NULL)",
                                           sync.Target.From.Alias, sync.Source.From.Alias, TableName1, TableName2,
                                           statement.Parameters.First().Key, statement.Parameters.Skip(1).First().Key));
        }