Example #1
0
        private IEnumerable <Type> GetTypeDependencies(Type type)
        {
            var mappingFor      = _tenant.MappingFor(type);
            var documentMapping = mappingFor as DocumentMapping ?? (mappingFor as SubClassMapping)?.Parent;

            if (documentMapping == null)
            {
                return(Enumerable.Empty <Type>());
            }

            return(documentMapping.ForeignKeys.Where(x => x.ReferenceDocumentType != type)
                   .SelectMany(keyDefinition =>
            {
                var results = new List <Type>();
                var referenceMappingType =
                    _tenant.MappingFor(keyDefinition.ReferenceDocumentType) as DocumentMapping;
                // If the reference type has sub-classes, also need to insert/update them first too
                if (referenceMappingType != null && referenceMappingType.SubClasses.Any())
                {
                    results.AddRange(referenceMappingType.SubClasses.Select(s => s.DocumentType));
                }
                results.Add(keyDefinition.ReferenceDocumentType);
                return results;
            }));
        }
Example #2
0
        public void All <T>(string transformName)
        {
            var transform = _tenant.TransformFor(transformName);
            var mapping   = _tenant.MappingFor(typeof(T));

            var cmd = CommandBuilder.BuildCommand(sql =>
            {
                writeBasicSql(sql, mapping, transform);

                var where = mapping.ToQueryableDocument().DefaultWhereFragment();
                if (where != null)
                {
                    sql.Append(" where ");
                    where.Apply(sql);
                }
            });

            using (var conn = _tenant.OpenConnection())
            {
                conn.Execute(cmd, c =>
                {
                    c.ExecuteNonQuery();
                });
            }
        }
Example #3
0
        public void All <T>(string transformName)
        {
            var transform = _tenant.TransformFor(transformName);
            var mapping   = _tenant.MappingFor(typeof(T));

            using var session = (DocumentSessionBase)_store.LightweightSession();
            var operation = new DocumentTransformOperationFragment(mapping, transform);
            var statement = new StatementOperation(session.StorageFor <T>(), operation);

            // To bake in the default document filtering here
            statement.CompileLocal(session);
            session.UnitOfWork.Add(statement);
            session.SaveChanges();
        }
Example #4
0
        private void apply()
        {
            var transform = _tenant.TransformFor(StoreOptions.PatchDoc);
            var document  = _tenant.MappingFor(typeof(T)).ToQueryableDocument();
            var operation = new PatchOperation(transform, document, _fragment, Patch, _serializer);

            _unitOfWork.Patch(operation);
        }
Example #5
0
        private IEnumerable <Type> GetTypeDependencies(Type type)
        {
            var documentMapping = _tenant.MappingFor(type) as DocumentMapping;

            if (documentMapping == null)
            {
                return(Enumerable.Empty <Type>());
            }

            return(documentMapping.ForeignKeys.Where(x => x.ReferenceDocumentType != type).Select(keyDefinition => keyDefinition.ReferenceDocumentType));
        }
Example #6
0
        public PatchExpressionTests(DefaultStoreFixture fixture) : base(fixture)
        {
            var queryable = Substitute.For <IQueryableDocument>();

            queryable.DocumentType.Returns(typeof(Target));

            var mapping = Substitute.For <IDocumentMapping>();

            mapping.ToQueryableDocument().Returns(queryable);

            _schema.MappingFor(typeof(Target)).Returns(mapping);

            _expression = new PatchExpression <Target>(null, _schema, new UnitOfWork(theStore, theStore.Tenancy.Default), new JsonNetSerializer());
        }
Example #7
0
        private void assertCorrectIdType <T>(object id)
        {
            var mapping = Tenant.MappingFor(typeof(T));

            if (id.GetType() != mapping.IdType)
            {
                if (id.GetType() == typeof(int) && mapping.IdType == typeof(long))
                {
                    return;
                }

                throw new InvalidOperationException(
                          $"The id type for {typeof(T).FullName} is {mapping.IdType.Name}, but got {id.GetType().Name}");
            }
        }
Example #8
0
        internal static IWhereFragment BuildWhereFragment(DocumentStore store, QueryModel query, ITenant tenant)
        {
            var mapping = tenant.MappingFor(query.SourceType()).ToQueryableDocument();
            var wheres  = query.AllBodyClauses().OfType <WhereClause>().ToArray();

            if (wheres.Length == 0)
            {
                return(mapping.DefaultWhereFragment());
            }

            var @where = wheres.Length == 1
                ? store.Parser.ParseWhereFragment(mapping, wheres.Single().Predicate)
                : new CompoundWhereFragment(store.Parser, mapping, "and", wheres);

            return(mapping.FilterDocuments(query, @where));
        }
Example #9
0
        public PatchExpressionTests(DefaultStoreFixture fixture) : base(fixture)
        {
            var queryable = Substitute.For <IQueryableDocument>();

            queryable.DocumentType.Returns(typeof(Target));

            var mapping = Substitute.For <IDocumentMapping>();

            mapping.ToQueryableDocument().Returns(queryable);

            _schema.MappingFor(typeof(Target)).Returns(mapping);

            var session = theStore.LightweightSession();

            Disposables.Add(session);

            _expression = new PatchExpression <Target>(new ByGuidFilter(Guid.NewGuid()), (DocumentSessionBase)session);
        }
Example #10
0
        private async Task clearExistingState(CancellationToken token)
        {
            _logger.ClearingExistingState(this);
            var types = _projection.ProjectedTypes();

            using (var conn = _tenant.OpenConnection())
            {
                foreach (var type in types)
                {
                    var tableName = _tenant.MappingFor(type).TableName;
                    var sql       =
                        $"delete from {_store.Events.DatabaseSchemaName}.mt_event_progression where name = :name;truncate {tableName} cascade";

                    var cmd = new NpgsqlCommand(sql).With("name", _projection.GetEventProgressionName(type));

                    await conn.ExecuteAsync(cmd, token);
                }
            }
            LastEncountered = 0;
        }
Example #11
0
        private async Task clearExistingState(CancellationToken token)
        {
            _logger.ClearingExistingState(this);

            var tableName = _tenant.MappingFor(_projection.Produces).Table;
            var sql =
                $"delete from {_store.Events.DatabaseSchemaName}.mt_event_progression where name = :name;truncate {tableName} cascade";

            using (var conn = _tenant.OpenConnection())
            {
                await conn.ExecuteAsync(async (cmd, tkn) =>
                {
                    await cmd.Sql(sql)
                        .With("name", _projection.Produces.FullName)
                        .ExecuteNonQueryAsync(tkn)
                        .ConfigureAwait(false);
                }, token).ConfigureAwait(false);
            }

            LastEncountered = 0;
        }
Example #12
0
 public IDocumentMapping MappingFor(Type documentType)
 {
     return(_inner.MappingFor(documentType));
 }