override public void ExitComputedEntity(QueryBuilderQueriesParser.ComputedEntityContext context) { var computedEntityRight = new List <QueryBuilderQueriesParser.EntityContext>(context._computedEntityRight); var qRelationship = metadata.EntityRelationshipModel.FindByName(context.computedEntityRelationshipName.Text); if (qRelationship.GetType() != typeof(Relationship)) { throw new Exception($"Element {qRelationship.Name} is not a Relationship!"); } var cEntityName = context.computedEntityLeft.qEntity.Element.Name + string.Join("", computedEntityRight.ConvertAll(cer => cer.qEntity.Element.Name)); var cEntityAlias = context.computedEntityLeft.qEntity.Alias + string.Join("", computedEntityRight.ConvertAll(cer => cer.qEntity.Alias)); string computedEntityRelationshipAlias = null; if (context.computedEntityRelationshipAlias != null) { computedEntityRelationshipAlias = context.computedEntityRelationshipAlias.Text; } ComputedEntity cEntity = new ComputedEntity(cEntityName, context.computedEntityLeft.qEntity, (Relationship)qRelationship, computedEntityRelationshipAlias, computedEntityRight.ConvertAll(cer => cer.qEntity)); context.qEntity = new QueryableEntity(cEntity, cEntityAlias); }
public void ManyToManyComputedEntityMultipleEntities() { // Asserts if the query result for a relationship join operation is equal // to a handcrafted query RequiredDataContainer ModelData = ComputedEntityDataProvider.ManyToManyComputedEntity(); // Load handcrafted query string HandcraftedQuery = Utils.ReadQueryFromFile("HandcraftedQueries/ceManyToMany.js"); // Assert if the handcrafted query is not null Assert.IsNotNull(HandcraftedQuery); // Prepare query generator ComputedEntity CarRepairedByGarage = new ComputedEntity("CarRepairedByGarage", new QueryableEntity(( Entity)ModelData.EntityRelationshipModel.FindByName("Car"), "car"), (Relationship)ModelData.EntityRelationshipModel.FindByName("Repaired"), new List <QueryableEntity> { new QueryableEntity((Entity)ModelData.EntityRelationshipModel.FindByName("Garage"), "garage"), new QueryableEntity((Entity)ModelData.EntityRelationshipModel.FindByName("Supplier"), "supplier") }); RelationshipJoinOperator RJoinOp = new RelationshipJoinOperator( new QueryableEntity(( Entity)ModelData.EntityRelationshipModel.FindByName("Person"), "person"), (Relationship)ModelData.EntityRelationshipModel.FindByName("Owns"), new List <QueryableEntity> { new QueryableEntity(CarRepairedByGarage), new QueryableEntity((Entity)ModelData.EntityRelationshipModel.FindByName("Insurance"), "insurance") }, ModelData.ERMongoMapping); List <AlgebraOperator> OpList = new List <AlgebraOperator> { RJoinOp }; FromArgument StartArg = new FromArgument(new QueryableEntity(ModelData.EntityRelationshipModel.FindByName("Person")), ModelData.ERMongoMapping); QueryGenerator QueryGen = new QueryGenerator(StartArg, OpList); string GeneratedQuery = QueryGen.Run(); // Assert if generated query is not null Assert.IsNotNull(GeneratedQuery); // Run Queries QueryRunner Runner = new QueryRunner("mongodb://localhost:27017", "ceManyToMany"); string HandcraftedResult = Runner.GetJSON(HandcraftedQuery); string GeneratedResult = Runner.GetJSON(GeneratedQuery); // Check if either result is null Assert.IsNotNull(HandcraftedResult); Assert.IsNotNull(GeneratedResult); // Check if both results are equal Assert.IsTrue(JToken.DeepEquals(JToken.Parse(HandcraftedResult), JToken.Parse(GeneratedResult))); }
private QueryableEntity getFirstEntityOnly(QueryableEntity qEntity) { if (qEntity.Element.GetType() == typeof(Entity)) { return(qEntity); } else if (qEntity.Element.GetType() == typeof(ComputedEntity)) { ComputedEntity ce = qEntity.Element as ComputedEntity; return(getFirstEntityOnly(ce.SourceEntity)); } else { throw new InvalidOperationException("There should not be another element type here!"); } }
private FromArgument getStartArg(QueryableEntity qEntity) { if (qEntity.Element.GetType() == typeof(Entity)) { return(new FromArgument(qEntity, metadata.ERMongoMapping)); } else if (qEntity.Element.GetType() == typeof(ComputedEntity)) { ComputedEntity ce = qEntity.Element as ComputedEntity; return(getStartArg(ce.SourceEntity)); } else { throw new InvalidOperationException("There should not be another element type here!"); } }
private List <RelationshipJoinOperator> getRelationshipJoinOperators(QueryableEntity qEntity) { var ret = new List <RelationshipJoinOperator>(); if (qEntity.Element.GetType() == typeof(ComputedEntity)) { ComputedEntity ce = qEntity.Element as ComputedEntity; ret.AddRange(getRelationshipJoinOperators(ce.SourceEntity)); ret.Add(new RelationshipJoinOperator(getFirstEntityOnly(ce.SourceEntity), ce.Relationship, ce.RelationshipAlias, ce.TargetEntities, metadata.ERMongoMapping)); } return(ret); }
static void Main(string[] args) { var mapping = QueryBuilderParser.ParseMapping(new FileStream(args[0], FileMode.Open)); Console.WriteLine($"ERModel: {mapping.EntityRelationshipModel.Name}"); Console.WriteLine("\n\n****** ER Model ********\n\n"); foreach (var e in mapping.EntityRelationshipModel.Elements.FindAll(e => e.GetType() == typeof(Entity))) { Console.WriteLine($"Entity: {e.Name}"); e.Attributes.ForEach(a => { Console.WriteLine($" Attribute: {a.Name} of type {a.OfType}, multivalued={a.MultiValued}"); }); } foreach (var r in mapping.EntityRelationshipModel.Elements.FindAll(e => e.GetType() == typeof(Relationship)).ConvertAll <Relationship>(e => (Relationship)e)) { Console.WriteLine($"Relationship: {r.Name}"); r.Ends.ForEach(e => { Console.WriteLine($" End: {e.TargetEntity.Name}"); }); r.Attributes.ForEach(a => { Console.WriteLine($" Attribute: {a.Name} of type {a.OfType}, multivalued={a.MultiValued}"); }); } Console.WriteLine("\n\n****** Mongo DB Schema ********\n\n"); foreach (var c in mapping.MongoDBSchema.Collections) { Console.WriteLine($"Collection: {c.Name}"); c.DocumentSchema.Attributes.ForEach(a => { Console.WriteLine($" Field: {a.Name} of type {a.OfType}, multivalued={a.MultiValued}"); }); } Console.WriteLine("\n\n****** Mapping ********\n\n"); foreach (var r in mapping.ERMongoMapping.Rules) { Console.WriteLine($"Rule: {r.Source.Name} = {r.Target.Name} (Main={r.IsMain})"); foreach (var sr in r.Rules) { Console.WriteLine($" {sr.Key} - {sr.Value}"); } } Console.WriteLine("\n\n****** Warnings and Errors ********\n\n"); mapping.Warnings.ForEach(w => Console.WriteLine(w)); mapping.Errors.ForEach(e => Console.WriteLine(e)); //string[] queries = { "from Author a rjoin <BookAndAuthor ba> (Book b)" }; string[] queries = { "from Author a rjoin <BookAndAuthor ba> (Book b rjoin <PublishedBy pb> (Publisher p))" }; // string[] queries = { "from Person p rjoin <Insurance i> (Car c, InsuranceCompany ic)", // "from Car c rjoin <Repaired r> (Garage g)", // "from Person p rjoin <Insurance i> (Car c rjoin <Repaired r> (Garage g), InsuranceCompany ic)", // "from (Person p rjoin <Drives d> (Car c)) rjoin <Repaired r> (Garage g)", // "from (Person p rjoin <Drives d> (Car c rjoin <Repaired r> (Garage g)))", // "from Person p rjoin <Drives d> (Car c rjoin <Repaired r> (Garage g))" }; foreach (var q in queries) { Console.WriteLine(q); var generatedQuery = QueryBuilderParser.ParseQuery(q, mapping); Console.WriteLine("*************"); Console.WriteLine($"Start Argument: {generatedQuery.StartArgument.Entity.Element.Name} AS {generatedQuery.StartArgument.Entity.Alias}"); var i = 1; foreach (var Op in generatedQuery.PipelineOperators) { Console.WriteLine($"Operator {i++}: {Op.ToString()}"); } Console.WriteLine("*************"); QueryRunner runner = new QueryRunner("mongodb://localhost:27017", "sampleAuthorBookPublisher"); Console.WriteLine(runner.GetJSON(generatedQuery.Run())); } // Nested join example using manual code Console.WriteLine("Running example ============"); QueryableEntity Author = new QueryableEntity(mapping.EntityRelationshipModel.FindByName("Author")); QueryableEntity Book = new QueryableEntity(mapping.EntityRelationshipModel.FindByName("Book")); QueryableEntity Publisher = new QueryableEntity(mapping.EntityRelationshipModel.FindByName("Publisher")); Relationship BookAndAuthor = (Relationship)mapping.EntityRelationshipModel.FindByName("BookAndAuthor"); Relationship PublishedBy = (Relationship)mapping.EntityRelationshipModel.FindByName("PublishedBy"); ComputedEntity BookAndPublisher = new ComputedEntity("BookAndPublisher", Book, PublishedBy, new List <QueryableEntity>() { Publisher }); RelationshipJoinOperator JoinOp = new RelationshipJoinOperator(Author, BookAndAuthor, new List <QueryableEntity>() { new QueryableEntity(BookAndPublisher) }, mapping.ERMongoMapping); FromArgument fromArg = new FromArgument(Author, mapping.ERMongoMapping); List <AlgebraOperator> Operations = new List <AlgebraOperator>() { JoinOp }; QueryGenerator queryGen = new QueryGenerator(fromArg, Operations); string queryString = queryGen.Run(); QueryRunner queryRunner = new QueryRunner("mongodb://localhost:27017", "sampleAuthorBookPublisher"); Console.WriteLine(queryRunner.GetJSON(queryString)); }
public void ProjectComputedEntity() { // Asserts if the query result for a relationship join operation is equal // to a handcrafted query RequiredDataContainer ModelData = ProjectDataProvider.ComputedEntityData(); // Load handcrafted query string HandcraftedQuery = Utils.ReadQueryFromFile("HandcraftedQueries/projectQuery.js"); // Assert if the handcrafted query is not null Assert.IsNotNull(HandcraftedQuery); // Prepare query generator ComputedEntity CarRepairedByGarage = new ComputedEntity("CarManufacturedBy", new QueryableEntity((Entity)ModelData.EntityRelationshipModel.FindByName("Car"), "car"), (Relationship)ModelData.EntityRelationshipModel.FindByName("ManufacturedBy"), new List <QueryableEntity> { new QueryableEntity((Entity)ModelData.EntityRelationshipModel.FindByName("Manufacturer"), "manufacturer") }); RelationshipJoinOperator RJoinOp = new RelationshipJoinOperator( new QueryableEntity((Entity)ModelData.EntityRelationshipModel.FindByName("Person"), "person"), (Relationship)ModelData.EntityRelationshipModel.FindByName("Owns"), new List <QueryableEntity> { new QueryableEntity(CarRepairedByGarage) }, ModelData.ERMongoMapping); VirtualMap VMap = RJoinOp.ComputeVirtualMap(); Dictionary <string, ProjectExpression> ProjectPersonAttrs = new Dictionary <string, ProjectExpression>(); QueryableEntity Person = new QueryableEntity(ModelData.EntityRelationshipModel.FindByName("Person")); QueryableEntity Car = new QueryableEntity(ModelData.EntityRelationshipModel.FindByName("Car")); QueryableEntity Manufacturer = new QueryableEntity(ModelData.EntityRelationshipModel.FindByName("Manufacturer")); List <ProjectArgument> ProjectArguments = new List <ProjectArgument>(); ProjectArguments.Add(new ProjectArgument(Person.GetAttribute("name"), Person, new BooleanExpr(true))); ProjectArguments.Add(new ProjectArgument(Car.GetAttribute("model"), Car, new BooleanExpr(true))); ProjectArguments.Add(new ProjectArgument(Car.GetAttribute("year"), Car, new BooleanExpr(true))); ProjectArguments.Add(new ProjectArgument(Manufacturer.GetAttribute("name"), Manufacturer, new BooleanExpr(true))); ProjectStage ProjectOp = new ProjectStage(ProjectArguments, VMap); List <AlgebraOperator> OpList = new List <AlgebraOperator> { RJoinOp, ProjectOp }; FromArgument StartArg = new FromArgument(new QueryableEntity(ModelData.EntityRelationshipModel.FindByName("Person")), ModelData.ERMongoMapping); QueryGenerator QueryGen = new QueryGenerator(StartArg, OpList); string GeneratedQuery = QueryGen.Run(); // Assert if generated query is not null Assert.IsNotNull(GeneratedQuery); // Run Queries QueryRunner Runner = new QueryRunner("mongodb://localhost:27017", "ceManyToMany2"); string HandcraftedResult = Runner.GetJSON(HandcraftedQuery); string GeneratedResult = Runner.GetJSON(GeneratedQuery); // Check if either result is null Assert.IsNotNull(HandcraftedResult); Assert.IsNotNull(GeneratedResult); // Check if both results are equal Assert.IsTrue(JToken.DeepEquals(JToken.Parse(HandcraftedResult), JToken.Parse(GeneratedResult))); }