public void execute_has_type_mapping_resolves_custom_types() { NpgsqlConnection.GlobalTypeMapper.MapComposite <MappedTarget>("varchar"); TypeMappings.HasTypeMapping(typeof(MappedTarget)).ShouldBeTrue(); TypeMappings.HasTypeMapping(typeof(UnmappedTarget)).ShouldBeFalse(); }
protected override Expression VisitSubQuery(SubQueryExpression expression) { var queryType = expression.QueryModel.MainFromClause.ItemType; // Simple types if (TypeMappings.HasTypeMapping(queryType)) { var contains = expression.QueryModel.ResultOperators.OfType <ContainsResultOperator>().FirstOrDefault(); if (contains != null) { var @where = ContainmentWhereFragment.SimpleArrayContains(_parent._serializer, expression.QueryModel.MainFromClause.FromExpression, contains.Item.Value()); _register.Peek()(@where); return(null); } } if (expression.QueryModel.ResultOperators.Any(x => x is AnyResultOperator)) { var @where = new CollectionAnyContainmentWhereFragment(_parent._serializer, expression); _register.Peek()(@where); return(null); } return(base.VisitSubQuery(expression)); }
public DocumentMapping(Type documentType, StoreOptions options) { DocumentType = documentType; Alias = defaultDocumentAliasName(documentType); IdMember = (MemberInfo)documentType.GetProperties().FirstOrDefault(x => x.Name.EqualsIgnoreCase("id")) ?? documentType.GetFields().FirstOrDefault(x => x.Name.EqualsIgnoreCase("id")); if (IdMember == null) { throw new InvalidDocumentException( $"Could not determine an 'id/Id' field or property for requested document type {documentType.FullName}"); } assignIdStrategy(documentType, options); documentType.ForAttribute <MartenAttribute>(att => att.Modify(this)); documentType.GetProperties().Where(x => TypeMappings.HasTypeMapping(x.PropertyType)).Each(prop => { var field = new LateralJoinField(prop); _fields[field.MemberName] = field; prop.ForAttribute <MartenAttribute>(att => att.Modify(this, prop)); }); documentType.GetFields().Where(x => TypeMappings.HasTypeMapping(x.FieldType)).Each(fieldInfo => { var field = new LateralJoinField(fieldInfo); _fields.AddOrUpdate(field.MemberName, field, (key, f) => f); fieldInfo.ForAttribute <MartenAttribute>(att => att.Modify(this, fieldInfo)); }); }
private void applyAnyMartenAttributes(Type documentType) { documentType.ForAttribute <MartenAttribute>(att => att.Modify(this)); documentType.GetProperties() .Where(x => TypeMappings.HasTypeMapping(x.PropertyType)) .Each(prop => { prop.ForAttribute <MartenAttribute>(att => att.Modify(this, prop)); }); documentType.GetFields() .Where(x => TypeMappings.HasTypeMapping(x.FieldType)) .Each(fieldInfo => { fieldInfo.ForAttribute <MartenAttribute>(att => att.Modify(this, fieldInfo)); }); }
public void Parse() { var invalidOperators = _query.ResultOperators.Where(x => !ValidOperators.Contains(x.GetType())) .ToArray(); if (invalidOperators.Any()) { var names = invalidOperators.Select(x => x.GetType().Name).Join(", "); throw new NotSupportedException($"Marten does not yet support {names} operators in child collection queries"); } var members = FindMembers.Determine(_query.MainFromClause.FromExpression); var queryType = _query.SourceType(); var isPrimitive = TypeMappings.HasTypeMapping(queryType); Visit(_expression); // Simple types if (isPrimitive) { var contains = _query.ResultOperators.OfType <ContainsResultOperator>().FirstOrDefault(); if (contains != null) { var @where = ContainmentWhereFragment.SimpleArrayContains(members, _serializer, _query.MainFromClause.FromExpression, contains.Item.Value()); _registerFilter(@where); return; } } if (_query.ResultOperators.Any(x => x is AnyResultOperator)) { // Any() without predicate if (!_query.BodyClauses.Any()) { var @where_any_nopredicate = new CollectionAnyNoPredicateWhereFragment(members, _expression); _registerFilter(@where_any_nopredicate); return; } var @where = new CollectionAnyContainmentWhereFragment(members, _serializer, _expression); _registerFilter(@where); } }
private IWhereFragment GetWhereFragment(IDocumentMapping mapping, SubQueryExpression expression) { var queryType = expression.QueryModel.MainFromClause.ItemType; if (TypeMappings.HasTypeMapping(queryType)) { var contains = expression.QueryModel.ResultOperators.OfType <ContainsResultOperator>().FirstOrDefault(); if (contains != null) { return(ContainmentWhereFragment.SimpleArrayContains(_serializer, expression.QueryModel.MainFromClause.FromExpression, Value(contains.Item))); } } if (expression.QueryModel.ResultOperators.Any(x => x is AnyResultOperator)) { return(new CollectionAnyContainmentWhereFragment(_serializer, expression)); } throw new NotImplementedException(); }
private void applyAnyMartenAttributes(Type documentType) { documentType.ForAttribute <MartenAttribute>(att => att.Modify(this)); documentType.GetProperties() .Where(x => !x.HasAttribute <DuplicateFieldAttribute>() && TypeMappings.HasTypeMapping(x.PropertyType)) .Each(prop => { prop.ForAttribute <MartenAttribute>(att => att.Modify(this, prop)); }); documentType.GetFields() .Where(x => !x.HasAttribute <DuplicateFieldAttribute>() && TypeMappings.HasTypeMapping(x.FieldType)) .Each(fieldInfo => { fieldInfo.ForAttribute <MartenAttribute>(att => att.Modify(this, fieldInfo)); }); // DuplicateFieldAttribute does not require TypeMappings check documentType.GetProperties() .Where(x => x.HasAttribute <DuplicateFieldAttribute>()) .Each(prop => { prop.ForAttribute <DuplicateFieldAttribute>(att => att.Modify(this, prop)); }); documentType.GetFields() .Where(x => x.HasAttribute <DuplicateFieldAttribute>()) .Each(fieldInfo => { fieldInfo.ForAttribute <DuplicateFieldAttribute>(att => att.Modify(this, fieldInfo)); }); }
public ArrayField(string dataLocator, string pgType, ISerializer serializer, MemberInfo[] members) : base(dataLocator, pgType, serializer.Casing, members) { var rawLocator = RawLocator; RawLocator = $"CAST({rawLocator} as jsonb)"; var collectionType = members.Last().GetMemberType(); ElementType = collectionType.DetermineElementType(); var innerPgType = TypeMappings.GetPgType(ElementType, EnumStorage.AsInteger); if (TypeMappings.HasTypeMapping(ElementType)) { PgType = innerPgType + "[]"; } if (PgType == "jsonb[]") { PgType = "jsonb"; } TypedLocator = $"CAST({rawLocator} as {PgType})"; LocatorForIncludedDocumentId = $"unnest(CAST(ARRAY(SELECT jsonb_array_elements_text(CAST({rawLocator} as jsonb))) as {innerPgType}[]))"; if (PgType.EqualsIgnoreCase("JSONB")) { LocatorForFlattenedElements = $"unnest(CAST(ARRAY(SELECT jsonb_array_elements(CAST({rawLocator} as jsonb))) as jsonb[]))"; } else { LocatorForFlattenedElements = LocatorForIncludedDocumentId; } }
public ISelector <T> ToSelector <T>(ISerializer serializer, IIncludeJoin[] joins) { if (IsComplex(joins)) { if (HasSelectTransform()) { var visitor = new SelectorParser(_query); visitor.Visit(_query.SelectClause.Selector); return(visitor.ToSelector <T>("x", _schema, _document)); } if (typeof(T) == typeof(string)) { return((ISelector <T>) new JsonSelector()); } if (typeof(T) != _documentType) { // TODO -- going to have to come back to this one. // think this is related to hierarchical documents return(null); } return(new DeserializeSelector <T>(serializer, RawChildElementField())); } if (typeof(T) == typeof(string)) { return(new SingleFieldSelector <T>(IsDistinct, $"jsonb_array_elements_text({_field.SqlLocator}) as x")); } else if (TypeMappings.HasTypeMapping(typeof(T))) { return(new ArrayElementFieldSelector <T>(IsDistinct, _field, conversions)); } return(new DeserializeSelector <T>(serializer, $"jsonb_array_elements_text({_field.SqlLocator}) as x")); }
public DocumentMapping(Type documentType) { DocumentType = documentType; IdMember = (MemberInfo)documentType.GetProperties().FirstOrDefault(x => x.Name.EqualsIgnoreCase("id")) ?? documentType.GetFields().FirstOrDefault(x => x.Name.EqualsIgnoreCase("id")); TableName = TableNameFor(documentType); UpsertName = UpsertNameFor(documentType); documentType.GetProperties().Where(x => TypeMappings.HasTypeMapping(x.PropertyType)).Each(prop => { var field = new LateralJoinField(prop); _fields[field.MemberName] = field; }); documentType.GetFields().Where(x => TypeMappings.HasTypeMapping(x.FieldType)).Each(fieldInfo => { var field = new LateralJoinField(fieldInfo); _fields.AddOrUpdate(field.MemberName, field, (key, f) => f); }); }
public ISelector <T> ToSelector <T>(ISerializer serializer, IIncludeJoin[] joins) { if (IsComplex(joins)) { if (HasSelectTransform()) { var visitor = new SelectorParser(_query); visitor.Visit(_query.SelectClause.Selector); return(visitor.ToSelector <T>("x", _store.Tenancy.Default, _document)); } if (typeof(T) == typeof(string)) { return((ISelector <T>) new JsonSelector()); } if (typeof(T) != _documentType) { return(null); } return(new DeserializeSelector <T>(serializer, RawChildElementField())); } if (typeof(T) == typeof(string)) { return(new SingleFieldSelector <T>(IsDistinct, $"jsonb_array_elements_text({_field.JSONBLocator}) as x")); } if (TypeMappings.HasTypeMapping(typeof(T))) { return(new ArrayElementFieldSelector <T>(IsDistinct, _field, conversions)); } return(new DeserializeSelector <T>(serializer, $"jsonb_array_elements_text({_field.JSONBLocator}) as x")); }
public LinqFixture() { Title = "Linq Support"; // set the expressions expression(x => x.Number == 1); expression(x => x.Number > 3); expression(x => x.Number < 3); expression(x => x.Number <= 3); expression(x => x.Number >= 3); expression(x => x.Number != 3); expression(x => x.Long == 1); expression(x => x.Long > 3); expression(x => x.Long < 3); expression(x => x.Long <= 3); expression(x => x.Long >= 3); expression(x => x.Long != 3); expression(x => x.String == "A"); expression(x => x.String != "A"); expression(x => x.String == "A" && x.Number == 1); expression(x => x.String == "A" || x.Number == 1); expression(x => x.String.Contains("B")); expression(x => x.String.StartsWith("Bar"), "x.String.StartsWith(\"Bar\")"); expression(x => x.String.EndsWith("Foo"), "x.String.EndsWith(\"Foo\")"); expression(x => x.String == null); expression(x => x.Flag); expression(x => x.Flag == true); expression(x => !x.Flag, "!Flag"); expression(x => x.Flag == false); expression(x => x.Inner.Flag, "Inner.Flag"); expression(x => !x.Inner.Flag, "!Inner.Flag"); expression(x => x.Inner.Flag == true); expression(x => x.Inner.Flag == false); expression(x => x.Double == 10); expression(x => x.Double != 10); expression(x => x.Double > 10); expression(x => x.Double < 10); expression(x => x.Double <= 10); expression(x => x.Double >= 10); expression(x => x.Decimal == 10); expression(x => x.Decimal != 10); expression(x => x.Decimal > 10); expression(x => x.Decimal < 10); expression(x => x.Decimal <= 10); expression(x => x.Decimal >= 10); var today = DateTime.Today; expression(x => x.Date == today, "x.Date == Today"); expression(x => x.Date != today, "x.Date != Today"); expression(x => x.Date > today, "x.Date > Today"); expression(x => x.Date < today, "x.Date < Today"); expression(x => x.Date >= today, "x.Date >= Today"); expression(x => x.Date <= today, "x.Date <= Today"); AddSelectionValues("Expressions", _wheres.Keys.ToArray()); AddSelectionValues("Fields", typeof(Target).GetProperties().Where(x => TypeMappings.HasTypeMapping(x.PropertyType)).Select(x => x.Name).ToArray()); }