public static AssetType Parse(string assetTypeToken, MetaModel metaModel) { AssetType assetType; if (!metaModel.TryGetAssetType(assetTypeToken, out assetType)) throw new InvalidOperationException($"{assetTypeToken} is not a valid assetType"); return assetType; }
public Query(AssetType assetType, IEnumerable<AttributeDefinition> selection, FilterTerm filter, IEnumerable<OrderTerm> orderTerms, int? skip, int? take, MetaModel metaModel) { AssetType = assetType; Selection = selection; Filter = filter; OrderTerms = orderTerms; Skip = skip; Take = take; MetaModel = metaModel; }
public static QueryBuilder For(string assetTypeToken, MetaModel metaModel) { var assetType = metaModel.GetAssetType(assetTypeToken); return new QueryBuilder(assetType, metaModel); }
private QueryBuilder(AssetType assetType, MetaModel metaModel) { _assetType = assetType; _metaModel = metaModel; }
public AssetType(Type type, MetaModel metaModel) { _type = type; _metaModel = metaModel; }
static void Main(string[] args) { XmlConfigurator.Configure(); var types = TypeScanner.Scan(x => { x.AssembliesFromApplicationBaseDirectory(); }).Where(x=>x.Namespace == "V1Antlr.Sample").ToArray(); var metaModel = new MetaModel(); metaModel.RegisterTypes(types); var testTokens = new[] { "Product.ID", "Product.Title", "Product.BodyHtml", "Product.Variants.Price.@Min", "OrderLineItem.Order.Name", "OrderLineItem.Order.CreatedAt", "OrderLineItem.Quantity", "OrderLineItem.Title", "OrderLineItem.VariantTitle", "OrderLineItem.Order.CreatedAt", //"Product.Visible", "OrderLineItem.Product", "OrderLineItem.Order.ID", "Product.Images[(Position='1'|Position='2');(Position>'0')].Source", "OrderLineItem.Properties[Name='Add a Monogram'].Value", "OrderLineItem.Properties[Name='Custom Text'].Value", "OrderLineItem.Properties[Name='Font'].Value", "OrderLineItem.Properties[Name='Thread Color'].Value", "OrderLineItem.Properties[Name='Color'].Value", "Order.LineItems.Properties[Name='Color'].Value", "Order.LineItems[+Properties;Quantity>'0'].Properties[Value!=''].Value" //"Product.Url", // Calculated? }; foreach(var testToken in testTokens) { var attr = metaModel.GetAttributeDefinition(testToken); Console.WriteLine(attr.Token); } Query query = QueryBuilder.For("Product", metaModel) .Select("Variants.Title", "Variants.Images", "Variants.Images.@Count", "Variants.Product.ID") //.Where("ID='3'") //.Where("Variants;ID>'0'") //.Order("Title", "-BodyHtml", "+ID") //.Skip(1) //.Take(10) .ToQuery(); var products = new Product[] { new Product { ID = 1, Title = "Product #1", BodyHtml = "Product #1 Body", }, new Product { ID = 2, Title = "Product #2", BodyHtml = "Product #2 Body", }, new Product { ID = 3, Title = "Product #3", BodyHtml = "Product #3 Body", Variants = new List<ProductVariant> { new ProductVariant { ID = 30, Price = 30.30m, Quantity = 30, Title = "Variant 30", Images = new List<ProductImage> { new ProductImage { ID = 300, Position = 1, Source = "http://google.com" }, new ProductImage { ID = 301, Position = 2, Source = "http://microsoft.com" }, new ProductImage { ID = 302, Position = 3, Source = "http://amazon.com" } } }, new ProductVariant { ID = 31, Price = 31.31m, Quantity = 31, Title = "Variant 31" }, new ProductVariant { ID = 32, Price = 32.32m, Quantity = 32, Title = "Variant 32" }, } }, new Product { ID = 4, Title = "Product #4", BodyHtml = "Product #4 Body", Variants = new List<ProductVariant> { new ProductVariant { ID = 40, Price = 40.40m, Quantity = 40, Title = "Variant 40" }, new ProductVariant { ID = 41, Price = 41.41m, Quantity = 41, Title = "Variant 41" }, new ProductVariant { ID = 42, Price = 42.42m, Quantity = 42, Title = "Variant 42" }, new ProductVariant { ID = 43, Price = 43.43m, Quantity = 43, Title = "Variant 43" }, } }, }; var sortQuery = QueryBuilder.For("Product", metaModel).Select("ID").Order("-ID").ToQuery(); var sorted = products.AsQueryable().ApplyQuery(sortQuery); var selected = products.AsQueryable().ApplyQuery(query); foreach (var asset in selected.Assets) { Console.WriteLine("=========="); var titlesDef = metaModel.GetAttributeDefinition("Product.Variants.Title"); var imagesDef = metaModel.GetAttributeDefinition("Product.Variants.Images"); var titles = asset.GetAttributeValue<IEnumerable<string>>(titlesDef).ToList(); var images = asset.GetAttributeValue<IEnumerable<object>>(imagesDef).ToList(); foreach (var attributeDefinition in query.Selection) { var value = asset.GetAttributeValue<object>(attributeDefinition); Console.WriteLine("\t{0}={1}", attributeDefinition.Name, value); } } var singleRelationJoinQuery = QueryBuilder.For("ProductImage", metaModel).Select("Product.Title").ToQuery(); var testImages = new List<ProductImage> { new ProductImage { ID = 1, Product = new Product { ID = 11, Title = "Product #11" } }, new ProductImage { ID = 2, Product = new Product { ID = 22, Title = "Product #22" } }, new ProductImage { ID = 3, Product = new Product { ID = 33, Title = "Product #33" } }, }; var singleRelationJoinResults = testImages.AsQueryable().ApplyQuery(singleRelationJoinQuery); Console.WriteLine("Done"); Console.ReadLine(); }
public AttributeDefinitionVisitor(AssetType rootAssetType, MetaModel metaModel) { _rootAssetType = rootAssetType; _metaModel = metaModel; }
public static OrderTerm ParseOrderTerm(string filterToken, AssetType assetType, MetaModel metaModel) { ICharStream charStream = new AntlrInputStream(filterToken); ITokenSource tokenSource = new V1QueryLexer(charStream); ITokenStream tokenStream = new CommonTokenStream(tokenSource); var parser = new V1QueryParser(tokenStream); var expression = parser.sort_token_term(); var visitor = new OrderVisitor(assetType, metaModel); return(visitor.Visit(expression)); }
public OrderVisitor(AssetType assetType, MetaModel metaModel) { _assetType = assetType; _metaModel = metaModel; }
public static AttributeDefinition Parse(string attributeNameToken, AssetType assetType, MetaModel metaModel) { ICharStream charStream = new AntlrInputStream(attributeNameToken); ITokenSource tokenSource = new V1QueryLexer(charStream); ITokenStream tokenStream = new CommonTokenStream(tokenSource); var parser = new V1QueryParser(tokenStream); var attributeName = parser.attribute_name(); var visitor = new AttributeDefinitionVisitor(assetType, metaModel); return(visitor.Visit(attributeName)); }
public FilterVisitor(AssetType rootAssetType, MetaModel metaModel) { _rootAssetType = rootAssetType; _metaModel = metaModel; }