public IWhereFragment ParseWhereFragment(DocumentMapping mapping, Expression expression)
        {
            if (expression is BinaryExpression)
            {
                return GetWhereFragment(mapping, expression.As<BinaryExpression>());
            }

            if (expression.NodeType == ExpressionType.Call)
            {
                return GetMethodCall(mapping, expression.As<MethodCallExpression>());
            }

            if (expression is MemberExpression && expression.Type == typeof (bool))
            {
                var locator = JsonLocator(mapping, expression.As<MemberExpression>());
                return new WhereFragment("{0} = True".ToFormat(locator), true);
            }

            if (expression.NodeType == ExpressionType.Not)
            {
                return GetNotWhereFragment(mapping, expression.As<UnaryExpression>().Operand);
            }

            if (expression is SubQueryExpression)
            {
                return GetWhereFragment(mapping, expression.As<SubQueryExpression>());
            }

            throw new NotSupportedException();
        }
Exemple #2
0
        public SchemaDiff(IDocumentSchema schema, SchemaObjects existing, DocumentMapping mapping)
        {
            if (existing.HasNone())
            {
                AllMissing = true;
            }
            else
            {
                var expectedTable = mapping.ToTable(schema);
                TableDiff = new TableDiff(expectedTable, existing.Table);

                // TODO -- drop obsolete indices?

                mapping.Indexes.Each(index =>
                {
                    if (existing.ActualIndices.ContainsKey(index.IndexName))
                    {
                        var actualIndex = existing.ActualIndices[index.IndexName];
                        if (!index.Matches(actualIndex))
                        {
                            IndexChanges.Add($"drop index {expectedTable.Table.Schema}.{index.IndexName};{index.ToDDL()}");
                        }
                    }
                    else
                    {
                        IndexChanges.Add(index.ToDDL());
                    }
                });

            }

            _existing = existing;
            _mapping = mapping;
            _schema = schema;
        }
        public SingleFieldSelectorTests()
        {
            theMapping = DocumentMapping.For<User>();
            var prop = ReflectionHelper.GetProperty<User>(x => x.FirstName);

            theSelector = new SingleFieldSelector<string>(theMapping, new MemberInfo[] {prop});
        }
        public override void Modify(DocumentMapping mapping, MemberInfo member)
        {
            var fkDefinition = mapping.AddForeignKey(member.Name, _referenceType);
            mapping.AddIndex(fkDefinition.ColumnName);
            

        }
 public generating_code_and_sql_for_hierarchy_smoke_Tests_on_other_database_schema()
 {
     theHierarchy = DocumentMapping.For<Squad>("other");
     theHierarchy.AddSubClass(typeof(BasketballTeam));
     theHierarchy.AddSubClass(typeof(BaseballTeam));
     theHierarchy.AddSubClass(typeof(FootballTeam));
 }
Exemple #6
0
 public generating_code_and_sql_for_hierarchy_smoke_Tests()
 {
     theHierarchy = DocumentMapping.For<Squad>();
     theHierarchy.AddSubClass(typeof (BasketballTeam));
     theHierarchy.AddSubClass(typeof (BaseballTeam));
     theHierarchy.AddSubClass(typeof (FootballTeam));
 }
 public ComputedIndex(DocumentMapping mapping, MemberInfo[] members)
 {
     _members = members;
     var field = mapping.FieldFor(members);
     _locator = field.SqlLocator.Replace("d.", "");
     _table = mapping.Table;
 }
Exemple #8
0
 public SubClassMapping(Type documentType, DocumentMapping parent, string alias = null)
 {
     DocumentType = documentType;
     _inner = new DocumentMapping(documentType);
     _parent = parent;
     Alias = alias ?? documentType.GetTypeName().Replace(".", "_").SplitCamelCase().Replace(" ", "_").ToLowerInvariant();
 }
        public void CreateSchema(IDocumentSchema schema, DocumentMapping mapping)
        {
            var writer= new StringWriter();
            mapping.WriteSchemaObjects(schema, writer);

            _runner.Execute(writer.ToString());
        }
        public static IEnumerable<IDocumentStorage> Build(IDocumentSchema schema, DocumentMapping[] mappings)
        {
            var code = GenerateDocumentStorageCode(mappings);

            var generator = new AssemblyGenerator();
            generator.ReferenceAssembly(Assembly.GetExecutingAssembly());
            generator.ReferenceAssemblyContainingType<NpgsqlConnection>();
            generator.ReferenceAssemblyContainingType<QueryModel>();
            generator.ReferenceAssemblyContainingType<DbCommand>();
            generator.ReferenceAssemblyContainingType<Component>();

            mappings.Select(x => x.DocumentType.Assembly).Distinct().Each(assem => generator.ReferenceAssembly(assem));

            var assembly = generator.Generate(code);

            return assembly
                .GetExportedTypes()
                .Where(x => x.IsConcreteTypeOf<IDocumentStorage>())
                .Select(x =>
                {
                    var docType =
                        x.FindInterfaceThatCloses(typeof (IdAssignment<>)).GetGenericArguments().Single();

                    var mapping = mappings.Single(m => m.DocumentType == docType);

                    var arguments = mapping.IdStrategy.ToArguments().Select(arg => arg.GetValue(schema)).ToArray();

                    var ctor = x.GetConstructors().Single();

                    return ctor.Invoke(arguments).As<IDocumentStorage>();
                });
        }
Exemple #11
0
        public UpsertFunction(DocumentMapping mapping)
        {
            if (mapping == null) throw new ArgumentNullException(nameof(mapping));

            _functionName = mapping.UpsertFunction;
            _tableName = mapping.Table;
            _primaryKeyConstraintName = "pk_" + mapping.Table.Name;

            var idType = mapping.IdMember.GetMemberType();
            var pgIdType = TypeMappings.GetPgType(idType);

            Arguments.Add(new UpsertArgument
            {
                Arg = "docId",
                PostgresType = pgIdType,
                Column = "id",
                Members = new[] {mapping.IdMember}
            });
            Arguments.Add(new UpsertArgument
            {
                Arg = "doc",
                PostgresType = "JSONB",
                DbType = NpgsqlDbType.Jsonb,
                Column = "data",
                BulkInsertPattern = "writer.Write(serializer.ToJson(x), NpgsqlDbType.Jsonb);",
                BatchUpdatePattern = "*"
            });
        }
        public static string GenerateDocumentStorageCode(DocumentMapping[] mappings)
        {
            var writer = new SourceWriter();

            // TODO -- get rid of the magic strings
            var namespaces = new List<string> {"System", "Marten", "Marten.Schema", "Marten.Linq", "Marten.Util", "Npgsql", "Remotion.Linq"};
            namespaces.AddRange(mappings.Select(x => x.DocumentType.Namespace));

            namespaces.Distinct().OrderBy(x => x).Each(x => writer.WriteLine($"using {x};"));
            writer.BlankLine();

            writer.StartNamespace("Marten.GeneratedCode");

            mappings.Each(x =>
            {
                x.GenerateDocumentStorage(writer);
                writer.BlankLine();
                writer.BlankLine();
            });

            writer.FinishBlock();

            var code = writer.Code();
            return code;
        }
        public static IEnumerable<IDocumentStorage> Build(IDocumentSchema schema, DocumentMapping[] mappings)
        {
            // Generate the actual source code
            var code = GenerateDocumentStorageCode(mappings);

            var generator = new AssemblyGenerator();

            // Tell the generator which other assemblies that it should be referencing 
            // for the compilation
            generator.ReferenceAssembly(Assembly.GetExecutingAssembly());
            generator.ReferenceAssemblyContainingType<NpgsqlConnection>();
            generator.ReferenceAssemblyContainingType<QueryModel>();
            generator.ReferenceAssemblyContainingType<DbCommand>();
            generator.ReferenceAssemblyContainingType<Component>();
            generator.ReferenceAssemblyContainingType<DbDataReader>();

            mappings.Select(x => x.DocumentType.Assembly).Distinct().Each(assem => generator.ReferenceAssembly(assem));

            // build the new assembly -- this will blow up if there are any
            // compilation errors with the list of errors and the actual code

            var assembly = generator.Generate(code);

            return assembly
                .GetExportedTypes()
                .Where(x => x.IsConcreteTypeOf<IDocumentStorage>())
                .Select(x => BuildStorageObject(schema, mappings, x));
        }
        public static IDocumentStorage BuildStorageObject(IDocumentSchema schema, Type storageType, DocumentMapping mapping)
        {
            var arguments = mapping.ToArguments().Select(arg => arg.GetValue(schema)).ToArray();

            var ctor = storageType.GetConstructors().Single();

            return ctor.Invoke(arguments).As<IDocumentStorage>();
        }
        public static IDocumentStorage BuildStorageObject(IDocumentSchema schema, DocumentMapping[] mappings, Type storageType)
        {
            var docType = storageType.DocumentTypeForStorage();

            var mapping = mappings.Single(m => m.DocumentType == docType);

            return BuildStorageObject(schema, storageType, mapping);
        }
 public override void Modify(DocumentMapping mapping, MemberInfo member)
 {
     var field = mapping.DuplicateField(member.Name, PgType);
     var indexDefinition = mapping.AddIndex(field.ColumnName);
     indexDefinition.Method = IndexMethod;
     if (IndexName.IsNotEmpty())
         indexDefinition.IndexName = IndexName;
 }
        public void storage_arguments_adds_hierarchy_argument_with_subclasses()
        {
            var mapping = new DocumentMapping(typeof(Squad), new StoreOptions());
            mapping.AddSubClass(typeof(FootballTeam));

            mapping.ToArguments().OfType<HierarchyArgument>()
                .Single().Mapping.ShouldBeSameAs(mapping);
        }
        public generating_code_and_sql_for_hierarchy_smoke_Tests()
        {
            theHierarchy = new DocumentMapping(typeof(Squad), new StoreOptions());
            theHierarchy.AddSubClass(typeof (BasketballTeam));
            theHierarchy.AddSubClass(typeof (BaseballTeam));
            theHierarchy.AddSubClass(typeof (FootballTeam));

        }
        public void CreateSchema(IDocumentSchema schema, DocumentMapping mapping)
        {
            var className = nameof(StoreOptions);
            var propName = nameof(StoreOptions.AutoCreateSchemaObjects);

            string message = $"No document storage exists for type {mapping.DocumentType.FullName} and cannot be created dynamically unless the {className}.{propName} = true. See http://jasperfx.github.io/marten/documentation/documents/ for more information";
            throw new InvalidOperationException(message);
        }
Exemple #20
0
        public void generate_code()
        {
            var mapping = new DocumentMapping(typeof (Target));
            mapping.DuplicateField("Number");
            mapping.DuplicateField("Date");

            var code = DocumentStorageBuilder.GenerateDocumentStorageCode(new[] {mapping});
            Debug.WriteLine(code);
        }
        public SelectTransformerTests()
        {
            theMapping = DocumentMapping.For<User>();
            theTarget = new TargetObject(typeof(invoking_query_with_select_Tests.User2));
            theTarget.StartBinding(ReflectionHelper.GetProperty<User>(x => x.FirstName)).Members.Add(ReflectionHelper.GetProperty<User2>(x => x.First));
            theTarget.StartBinding(ReflectionHelper.GetProperty<User>(x => x.LastName)).Members.Add(ReflectionHelper.GetProperty<User2>(x => x.Last));

            theSelector = new SelectTransformer<User2>(theMapping, theTarget);
        }
        public HierarchyArgumentTests()
        {
            mapping = new DocumentMapping(typeof(Squad), new StoreOptions());

            arg = new HierarchyArgument(mapping);

            mapping.AddSubClass(typeof(BasketballTeam));
            mapping.AddSubClass(typeof(BaseballTeam));
            mapping.AddSubClass(typeof(FootballTeam), "football");
        }
        public void equivalency_positive()
        {
            var users = new DocumentMapping(typeof(User));
            var table1 = users.ToTable(null);
            var table2 = users.ToTable(null);

            table2.ShouldBe(table1);
            table1.ShouldBe(table2);
            table1.ShouldNotBeSameAs(table2);
        }
        public HierarchyArgumentTests()
        {
            mapping = DocumentMapping.For<Squad>();

            arg = new HierarchyArgument(mapping);

            mapping.AddSubClass(typeof(BasketballTeam));
            mapping.AddSubClass(typeof(BaseballTeam));
            mapping.AddSubClass(typeof(FootballTeam), "football");
        }
Exemple #25
0
        public void CreatePatch(DocumentMapping mapping, Action<string> executeSql)
        {
            var fields = Missing.Select(x => mapping.FieldForColumn(x.Name)).ToArray();
            if (fields.Length != Missing.Length)
            {
                throw new InvalidOperationException("The expected columns did not match with the DocumentMapping");
            }

            fields.Each(x => x.WritePatch(mapping, executeSql));
        }
        public void equivalency_negative_different_numbers_of_columns()
        {
            var users = new DocumentMapping(typeof(User));
            var table1 = users.ToTable(null);
            var table2 = users.ToTable(null);

            table2.Columns.Add(new TableColumn("user_name", "character varying"));

            table2.ShouldNotBe(table1);
        }
        public void equivalency_negative_column_type_changed()
        {
            var users = new DocumentMapping(typeof(User));
            var table1 = users.ToTable(null);
            var table2 = users.ToTable(null);

            table2.PrimaryKey.Type = "int";

            table2.ShouldNotBe(table1);
        }
Exemple #28
0
        public when_deriving_the_table_definition_from_the_database_schema_Tests()
        {
            _schema = theStore.Schema;

            theMapping = _schema.MappingFor(typeof(User)).As<DocumentMapping>();
            theMapping.DuplicateField("UserName");

            _storage = _schema.StorageFor(typeof(User));

            theDerivedTable = _schema.DbObjects.TableSchema(theMapping);
        }
        public void do_not_blow_up_building_more_than_one()
        {
            var mappings = new DocumentMapping[]
            {
                new DocumentMapping(typeof(User)),
                new DocumentMapping(typeof(Company)),
                new DocumentMapping(typeof(Issue)),
            };

            DocumentStorageBuilder.Build(null, mappings).Count()
                .ShouldBe(3);
        }
Exemple #30
0
        public void tryit()
        {
            var mapping = new DocumentMapping(typeof(User));
            var writer = new SourceWriter();
            writer.StartNamespace("MyApplication");

            mapping.GenerateDocumentStorage(writer);

            writer.FinishBlock();

            Debug.WriteLine(writer.Code());
        }
 public LightweightBug616AccountDocumentStorage155584575(Marten.Schema.DocumentMapping document) : base(document)
 {
     _document = document;
 }
 public DirtyTrackingBug616AccountDocumentStorage155584575(Marten.Schema.DocumentMapping document) : base(document)
 {
     _document = document;
 }
 public InsertBug616AccountOperation155584575(CoreTests.Bugs.Bug616Account document, System.Guid id, System.Collections.Generic.Dictionary <System.Guid, System.Guid> versions, Marten.Schema.DocumentMapping mapping) : base(document, id, versions, mapping)
 {
     _document = document;
     _id       = id;
     _versions = versions;
     _mapping  = mapping;
 }
 public IdentityMapUserWithoutIdSetterDocumentStorage1320849530(Marten.Schema.DocumentMapping document) : base(document)
 {
     _document = document;
 }
Exemple #35
0
 public LightweightIssueSelector2074714110(Marten.Internal.IMartenSession session, Marten.Schema.DocumentMapping mapping) : base(session, mapping)
 {
     _session = session;
     _mapping = mapping;
 }
 public DirtyTrackingTypeWithInnerCollectionsWithJsonConverterAttributeSelector818716525(Marten.Internal.IMartenSession session, Marten.Schema.DocumentMapping mapping) : base(session, mapping)
 {
     _session = session;
     _mapping = mapping;
 }
Exemple #37
0
 public InsertRouteDetailsOperation784762106(DocumentDbTests.Bugs.RouteDetails document, System.Guid id, System.Collections.Generic.Dictionary <System.Guid, System.Guid> versions, Marten.Schema.DocumentMapping mapping) : base(document, id, versions, mapping)
 {
     _document = document;
     _id       = id;
     _versions = versions;
     _mapping  = mapping;
 }
Exemple #38
0
 public ProductProvider1360380871(Marten.Schema.DocumentMapping mapping) : base(new ProductBulkLoader1360380871(new QueryOnlyProductDocumentStorage1360380871(mapping)), new QueryOnlyProductDocumentStorage1360380871(mapping), new LightweightProductDocumentStorage1360380871(mapping), new IdentityMapProductDocumentStorage1360380871(mapping), new DirtyTrackingProductDocumentStorage1360380871(mapping))
 {
     _mapping = mapping;
 }
Exemple #39
0
 public InsertProductOperation1360380871(DocumentDbTests.Reading.Linq.Product document, System.Guid id, System.Collections.Generic.Dictionary <System.Guid, System.Guid> versions, Marten.Schema.DocumentMapping mapping) : base(document, id, versions, mapping)
 {
     _document = document;
     _id       = id;
     _versions = versions;
     _mapping  = mapping;
 }
Exemple #40
0
 public DirtyTrackingProductDocumentStorage1360380871(Marten.Schema.DocumentMapping document) : base(document)
 {
     _document = document;
 }
Exemple #41
0
 public IdentityMapProductDocumentStorage1360380871(Marten.Schema.DocumentMapping document) : base(document)
 {
     _document = document;
 }
Exemple #42
0
 public LightweightProductDocumentStorage1360380871(Marten.Schema.DocumentMapping document) : base(document)
 {
     _document = document;
 }
 public InsertUserWithoutIdSetterOperation1320849530(Marten.Testing.Documents.UserWithoutIdSetter document, System.Guid id, System.Collections.Generic.Dictionary <System.Guid, System.Guid> versions, Marten.Schema.DocumentMapping mapping) : base(document, id, versions, mapping)
 {
     _document = document;
     _id       = id;
     _versions = versions;
     _mapping  = mapping;
 }
 public DirtyTrackingTypeWithInnerCollectionsWithJsonConverterAttributeDocumentStorage818716525(Marten.Schema.DocumentMapping document) : base(document)
 {
     _document = document;
 }
Exemple #45
0
 public RouteDetailsProvider784762106(Marten.Schema.DocumentMapping mapping) : base(new RouteDetailsBulkLoader784762106(new QueryOnlyRouteDetailsDocumentStorage784762106(mapping)), new QueryOnlyRouteDetailsDocumentStorage784762106(mapping), new LightweightRouteDetailsDocumentStorage784762106(mapping), new IdentityMapRouteDetailsDocumentStorage784762106(mapping), new DirtyTrackingRouteDetailsDocumentStorage784762106(mapping))
 {
     _mapping = mapping;
 }
 public TypeWithInnerCollectionsWithJsonConverterAttributeProvider818716525(Marten.Schema.DocumentMapping mapping) : base(new TypeWithInnerCollectionsWithJsonConverterAttributeBulkLoader818716525(new QueryOnlyTypeWithInnerCollectionsWithJsonConverterAttributeDocumentStorage818716525(mapping)), new QueryOnlyTypeWithInnerCollectionsWithJsonConverterAttributeDocumentStorage818716525(mapping), new LightweightTypeWithInnerCollectionsWithJsonConverterAttributeDocumentStorage818716525(mapping), new IdentityMapTypeWithInnerCollectionsWithJsonConverterAttributeDocumentStorage818716525(mapping), new DirtyTrackingTypeWithInnerCollectionsWithJsonConverterAttributeDocumentStorage818716525(mapping))
 {
     _mapping = mapping;
 }
Exemple #47
0
 public UpsertIssueOperation2074714110(Marten.Testing.Documents.Issue document, System.Guid id, System.Collections.Generic.Dictionary <System.Guid, System.Guid> versions, Marten.Schema.DocumentMapping mapping) : base(document, id, versions, mapping)
 {
     _document = document;
     _id       = id;
     _versions = versions;
     _mapping  = mapping;
 }
 public InsertTypeWithInnerCollectionsWithJsonConverterAttributeOperation818716525(DocumentDbTests.Reading.Linq.TypeWithInnerCollectionsWithJsonConverterAttribute document, System.Guid id, System.Collections.Generic.Dictionary <System.Guid, System.Guid> versions, Marten.Schema.DocumentMapping mapping) : base(document, id, versions, mapping)
 {
     _document = document;
     _id       = id;
     _versions = versions;
     _mapping  = mapping;
 }
 public UserWithoutIdSetterProvider1320849530(Marten.Schema.DocumentMapping mapping) : base(new UserWithoutIdSetterBulkLoader1320849530(new QueryOnlyUserWithoutIdSetterDocumentStorage1320849530(mapping)), new QueryOnlyUserWithoutIdSetterDocumentStorage1320849530(mapping), new LightweightUserWithoutIdSetterDocumentStorage1320849530(mapping), new IdentityMapUserWithoutIdSetterDocumentStorage1320849530(mapping), new DirtyTrackingUserWithoutIdSetterDocumentStorage1320849530(mapping))
 {
     _mapping = mapping;
 }
Exemple #50
0
 public DirtyTrackingRouteDetailsSelector784762106(Marten.Internal.IMartenSession session, Marten.Schema.DocumentMapping mapping) : base(session, mapping)
 {
     _session = session;
     _mapping = mapping;
 }
 public DirtyTrackingUserWithoutIdSetterDocumentStorage1320849530(Marten.Schema.DocumentMapping document) : base(document)
 {
     _document = document;
 }
Exemple #52
0
 public QueryOnlyRouteDetailsDocumentStorage784762106(Marten.Schema.DocumentMapping document) : base(document)
 {
     _document = document;
 }
 public DirtyTrackingUserWithoutIdSetterSelector1320849530(Marten.Internal.IMartenSession session, Marten.Schema.DocumentMapping mapping) : base(session, mapping)
 {
     _session = session;
     _mapping = mapping;
 }
Exemple #54
0
 public LightweightRouteDetailsDocumentStorage784762106(Marten.Schema.DocumentMapping document) : base(document)
 {
     _document = document;
 }
 public Bug616AccountProvider155584575(Marten.Schema.DocumentMapping mapping) : base(new Bug616AccountBulkLoader155584575(new QueryOnlyBug616AccountDocumentStorage155584575(mapping)), new QueryOnlyBug616AccountDocumentStorage155584575(mapping), new LightweightBug616AccountDocumentStorage155584575(mapping), new IdentityMapBug616AccountDocumentStorage155584575(mapping), new DirtyTrackingBug616AccountDocumentStorage155584575(mapping))
 {
     _mapping = mapping;
 }
Exemple #56
0
 public IdentityMapRouteDetailsDocumentStorage784762106(Marten.Schema.DocumentMapping document) : base(document)
 {
     _document = document;
 }
 public IdentityMapBug616AccountDocumentStorage155584575(Marten.Schema.DocumentMapping document) : base(document)
 {
     _document = document;
 }
Exemple #58
0
 public DirtyTrackingRouteDetailsDocumentStorage784762106(Marten.Schema.DocumentMapping document) : base(document)
 {
     _document = document;
 }
Exemple #59
0
 public QueryOnlyProductDocumentStorage1360380871(Marten.Schema.DocumentMapping document) : base(document)
 {
     _document = document;
 }
Exemple #60
0
 public DirtyTrackingProductSelector1360380871(Marten.Internal.IMartenSession session, Marten.Schema.DocumentMapping mapping) : base(session, mapping)
 {
     _session = session;
     _mapping = mapping;
 }