private void ConfigureTypeCountry(IObjectTypeDescriptor descriptor)
        {
            descriptor
            .Field("countries")
            .Type <ListType <CountryType> >()
            .Argument("load", x => { x.Type <BooleanType>(); x.DefaultValue(false); })
            .Resolver(context =>
            {
                bool load           = context.Argument <bool>("load");
                QLContext qlContext = context.Service <QLContext>();
                return(load
                  ? qlContext.Country.Include(x => x.State).ToList()
                  : qlContext.Country.ToList());
            });

            descriptor
            .Field("country_find")
            .Type <CountryType>()
            .Argument("id", x => { x.Type <IntType>(); x.DefaultValue(0); })
            .Argument("load", x => { x.Type <BooleanType>(); x.DefaultValue(false); })
            .Resolver(context =>
            {
                int id                     = context.Argument <int>("id");
                bool load                  = context.Argument <bool>("load");
                QLContext qlContext        = context.Service <QLContext>();
                IQueryable <Country> query = qlContext.Country.Where(x => x.Id == id);
                return(load
                  ? query.Include(x => x.State).FirstOrDefault()
                  : query.FirstOrDefault());
            });
        }
Example #2
0
        private bool CheckChildNodeSemantics(QLContext context, List <Message> messages)
        {
            var semanticsInChildNodesOk = true;

            foreach (var statement in ThenStatements)
            {
                if (!statement.CheckSemantics(context, messages))
                {
                    semanticsInChildNodesOk = false;
                }
            }

            foreach (var statement in ElseStatements)
            {
                if (!statement.CheckSemantics(context, messages))
                {
                    semanticsInChildNodesOk = false;
                }
            }

            if (!Condition.CheckSemantics(context, messages))
            {
                semanticsInChildNodesOk = false;
            }

            return(semanticsInChildNodesOk);
        }
Example #3
0
 public bool CheckSemantics(QLContext context, List <Message> messages)
 {
     return(Statements.TrueForAll((statement) =>
     {
         return statement.CheckSemantics(context, messages);
     }));
 }
Example #4
0
        public bool CheckSemantics(QLContext context, List <Message> messages)
        {
            // check the two child nodes have semantic errors
            var lhsSemanticsOk = Lhs.CheckSemantics(context, messages);
            var rhsSemanticsOk = Rhs.CheckSemantics(context, messages);

            // Don't continue with semantics checking if one of the child nodes has an error
            if (!lhsSemanticsOk || !rhsSemanticsOk)
            {
                return(false);
            }

            try
            {
                // Call the child class to get the return type of this epxression if the operation is not supported this will throw
                var resultType = GetResultType(context);
                return(true);
            }
            catch (NotSupportedException)
            {
                messages.Add(new Error(string.Format("Cannot apply operator {0} on arguments of type {1} and {2}", this, Lhs.GetResultType(context), Rhs.GetResultType(context))));
                return(false);
            }
            catch (DivideByZeroException)
            {
                return(true);
            }
        }
Example #5
0
        public bool CheckSemantics(QLContext context, List <Message> messages)
        {
            if (!context.ContainsQuestion(Name))
            {
                messages.Add(new Error(string.Format("Invalid use of undefined identifier {0}", Name)));
                return(false);
            }

            return(true);
        }
Example #6
0
        public static void Initialize(this QLContext db)
        {
            if (!db.Droids.Any())
            {
                Droid droid = new Droid
                {
                    Id   = 1,
                    Name = "R2-D2"
                };
                db.Droids.Add(droid);
                droid = new Droid
                {
                    Id   = 2,
                    Name = "R3-D3"
                };
                db.Droids.Add(droid);
                droid = new Droid
                {
                    Id   = 3,
                    Name = "R4-D4"
                };
                db.Droids.Add(droid);
                db.SaveChanges();
            }

            if (!db.Friends.Any())
            {
                Friend friend = new Friend {
                    Id = 1, Name = "R2-A", Sex = 0, Droid = new Droid {
                        Id = 1, Name = "R2-D2"
                    }
                };
                db.Friends.Add(friend);
                friend = new Friend {
                    Id = 2, Name = "R2-B", Sex = 0, Droid = new Droid {
                        Id = 1, Name = "R2-D2"
                    }
                };
                db.Friends.Add(friend);
                friend = new Friend {
                    Id = 3, Name = "R2-C", Sex = 1, Droid = new Droid {
                        Id = 2, Name = "R3-D3"
                    }
                };
                db.Friends.Add(friend);
                friend = new Friend {
                    Id = 4, Name = "R2-D", Sex = 1, Droid = new Droid {
                        Id = 3, Name = "R4-D4"
                    }
                };
                db.Friends.Add(friend);
                db.SaveChanges();
            }
        }
Example #7
0
        public bool CheckSemantics(QLContext context, List <Message> messages)
        {
            // Check the child nodes in the then and else branches for semantic errors
            if (!CheckChildNodeSemantics(context, messages))
            {
                return(false);
            }

            // We only accept conditions of boolean types (this is not C)
            if (!ValidateConditionType((dynamic)Condition.GetResultType(context)))
            {
                messages.Add(new Error("Condition for conditional statement cannot be resolved to boolean"));
                return(false);
            }
            return(true);
        }
Example #8
0
        private void ConfigureTypeState(IObjectTypeDescriptor descriptor)
        {
            descriptor
            .Field("states_in")
            .Type <ListType <StateType> >()
            .Argument("load", x => { x.Type <BooleanType>(); x.DefaultValue(false); })
            .Argument("ids", x => { x.Type <ListType <IntType> >(); x.DefaultValue(null); })
            .Resolver(context =>
            {
                bool load                = context.Argument <bool>("load");
                int[] ids                = context.Argument <int[]>("ids");
                QLContext qlContext      = context.Service <QLContext>();
                IQueryable <State> query = qlContext.State.Where(x => ids.Contains(x.Id));
                return(load
                  ? query.Include(x => x.Country).ToList()
                  : query.ToList());
            });

            descriptor
            .Field("states")
            .Type <ListType <StateType> >()
            .Argument("load", x => { x.Type <BooleanType>(); x.DefaultValue(false); })
            .Resolver(context =>
            {
                bool load           = context.Argument <bool>("load");
                QLContext qlContext = context.Service <QLContext>();
                return(load
                  ? qlContext.State.Include(x => x.Country).ToList()
                  : qlContext.State.ToList());
            });

            descriptor
            .Field("state_find")
            .Type <StateType>()
            .Argument("id", x => { x.Type <IntType>(); x.DefaultValue(0); })
            .Argument("load", x => { x.Type <BooleanType>(); x.DefaultValue(false); })
            .Resolver(context =>
            {
                int id                   = context.Argument <int>("id");
                bool load                = context.Argument <bool>("load");
                QLContext qlContext      = context.Service <QLContext>();
                IQueryable <State> query = qlContext.State.Where(x => x.Id == id);
                return(load
                  ? query.Include(x => x.Country).FirstOrDefault()
                  : query.FirstOrDefault());
            });
        }
Example #9
0
 public bool CheckSemantics(QLContext context, List <Message> messages)
 {
     try
     {
         var val = Value;
         return(true);
     }
     catch (FormatException)
     {
         messages.Add(new Error(string.Format("Cannot convert literal {0} to number", ValueAsString)));
     }
     catch (OverflowException)
     {
         messages.Add(new Error(string.Format("Value {0} is too large for number variable", ValueAsString)));
     }
     return(false);
 }
Example #10
0
        public bool CheckSemantics(QLContext context, List <Message> messages)
        {
            if (!Operand.CheckSemantics(context, messages))
            {
                return(false);
            }

            try
            {
                // Call the child class to get the return type of this epxression if the operation is not supported this will throw
                GetResultType(context);
                return(true);
            }
            catch (NotSupportedException)
            {
                messages.Add(new Error(string.Format("Cannot apply {0} operator on type {1}", this, Operand.GetResultType(context))));
                return(false);
            }
        }
Example #11
0
        public bool CheckSemantics(QLContext context, List <Message> messages)
        {
            try
            {
                // Literal is invalid if we cannot parse it to a decimal
                var val = Value;
                return(true);
            }
            catch (FormatException)
            {
                messages.Add(new Error(string.Format("Cannot convert literal {0} to money", ValueAsString)));
            }
            catch (OverflowException)
            {
                messages.Add(new Error(string.Format("Value {0} is too large for decimal variable", ValueAsString)));
            }

            return(false);
        }
Example #12
0
        public bool CheckSemantics(QLContext context, List <Message> messages)
        {
            // Check both child nodes
            var questionSemanticallyValid   = Question.CheckSemantics(context, messages);
            var expressionSemanticallyValid = Expression.CheckSemantics(context, messages);

            if (!questionSemanticallyValid || !expressionSemanticallyValid)
            {
                return(false);
            }

            // Computed question is like an assignment. Only valid when question type is
            // equal to the expression type. We do not support (implicit) casts
            if (!Question.Type.CanBeAssigned(Expression.GetResultType(context)))
            {
                messages.Add(new Error(string.Format("Cannot assign expression with type {0} to question of type {1}", Question.Type, Expression.GetResultType(context))));
                return(false);
            }

            return(true);
        }
Example #13
0
 public IType GetResultType(QLContext context)
 {
     return(new BooleanType());
 }
Example #14
0
 public bool CheckSemantics(QLContext context, List <Message> messages)
 {
     // Nothing to check for bool literal
     return(true);
 }
Example #15
0
 public override IType GetResultType(QLContext context)
 {
     return(Operand.GetResultType(context).Bang());
 }
Example #16
0
 public IType GetResultType(QLContext context)
 {
     return(context.GetQuestionType(Name));
 }
Example #17
0
 public IType GetResultType(QLContext context)
 {
     return(new IntegerType());
 }
Example #18
0
 public override IType GetResultType(QLContext context)
 {
     return(Lhs.GetResultType(context).Or(Rhs.GetResultType(context)));
 }
Example #19
0
 public FriendRepository(QLContext db) : base(db) => _db = db;
Example #20
0
 public override IType GetResultType(QLContext context)
 {
     return(Lhs.GetResultType(context).LessThanOrEqual(Rhs.GetResultType(context)));
 }
Example #21
0
 public IType GetResultType(QLContext context)
 {
     return(new MoneyType());
 }
Example #22
0
 public DroidRepository(QLContext db) : base(db)
 {
 }
Example #23
0
        private void ConfigureTypeSource(IObjectTypeDescriptor descriptor)
        {
            descriptor
            .Field("sources")
            .Type <ListType <SourceType> >()
            .Resolver(context =>
            {
                QLContext qlContext = context.Service <QLContext>();
                return(qlContext.Source.ToList());
            });

            descriptor
            .Field("source_add")
            .Type <SourceType>()
            .Argument("input", x => { x.Type <SourceInput>(); })
            .Resolver(context =>
            {
                Source source       = context.Argument <Source>("input");
                QLContext qlContext = context.Service <QLContext>();
                IDbContextTransaction transaction = qlContext.Database.BeginTransaction();
                try
                {
                    qlContext.Source.Add(source);
                    qlContext.SaveChanges();
                    transaction.Commit();
                }
                catch (System.Exception)
                {
                    transaction.Rollback();
                }
                finally
                {
                    transaction.Dispose();
                }
                return(source);
            });


            descriptor
            .Field("source_param_add")
            .Type <SourceType>()
            .Argument("id", x => { x.Type <UuidType>(); x.DefaultValue(null); })
            .Argument("name", x => { x.Type <StringType>(); x.DefaultValue(null); })
            .Argument("value", x => { x.Type <DecimalType>(); x.DefaultValue(null); })
            .Argument("created", x => { x.Type <DateTimeType>(); x.DefaultValue(null); })
            .Argument("active", x => { x.Type <BooleanType>(); x.DefaultValue(null); })
            .Argument("time", x => { x.Type <TimeSpanType>(); x.DefaultValue(null); })
            .Resolver(context =>
            {
                Guid?id          = context.Argument <Guid?>("id");
                string name      = context.Argument <string>("name");
                decimal?value    = context.Argument <decimal?>("value");
                DateTime?created = context.Argument <DateTime?>("created");
                bool?active      = context.Argument <bool?>("active");
                TimeSpan?time    = context.Argument <TimeSpan?>("time");
                if (id == Guid.Empty)
                {
                    id = null;
                }
                Source source = new Source()
                {
                    Id      = id,
                    Name    = name,
                    Value   = value,
                    Created = created,
                    Active  = active,
                    Time    = time
                };
                QLContext qlContext = context.Service <QLContext>();
                IDbContextTransaction transaction = qlContext.Database.BeginTransaction();
                try
                {
                    qlContext.Source.Add(source);
                    qlContext.SaveChanges();
                    transaction.Commit();
                }
                catch (System.Exception)
                {
                    transaction.Rollback();
                }
                finally
                {
                    transaction.Dispose();
                }
                return(source);
            });


            descriptor
            .Field("source_edit")
            .Type <SourceType>()
            .Argument("input", x => { x.Type <SourceInput>(); })
            .Resolver(context =>
            {
                Source source       = context.Argument <Source>("input");
                QLContext qlContext = context.Service <QLContext>();
                IDbContextTransaction transaction = qlContext.Database.BeginTransaction();
                try
                {
                    qlContext.Source.Update(source);
                    qlContext.SaveChanges();
                    transaction.Commit();
                }
                catch (System.Exception)
                {
                    transaction.Rollback();
                }
                finally
                {
                    transaction.Dispose();
                }
                return(source);
            });

            descriptor
            .Field("source_find")
            .Type <SourceType>()
            .Argument("id", x => { x.Type <UuidType>(); })
            .Resolver(context =>
            {
                Guid id             = context.Argument <Guid>("id");
                QLContext qlContext = context.Service <QLContext>();
                return(qlContext.Source.Find(id));
            });

            descriptor
            .Field("source_remove")
            .Type <RemoveType>()
            .Argument("id", x => { x.Type <UuidType>(); })
            .Resolver(context =>
            {
                Guid id             = context.Argument <Guid>("id");
                QLContext qlContext = context.Service <QLContext>();
                IDbContextTransaction transaction = qlContext.Database.BeginTransaction();
                int count = 0;
                try
                {
                    qlContext.Source.Remove(qlContext.Source.Find(id));
                    count = qlContext.SaveChanges();
                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                }
                finally
                {
                    transaction.Dispose();
                }
                return(Remove.Create(count));
            });
        }
Example #24
0
 public UserRepository(QLContext db) : base(db)
 {
 }
Example #25
0
 public override IType GetResultType(QLContext context)
 {
     return(Lhs.GetResultType(context).GreaterThan(Rhs.GetResultType(context)));
 }
Example #26
0
 public abstract IType GetResultType(QLContext context);
Example #27
0
        private void ConfigureTypeCar(IObjectTypeDescriptor descriptor)
        {
            descriptor
            .Field("cars")
            .Type <ListType <CarType> >()
            .Resolver(context =>
            {
                QLContext qlContext = context.Service <QLContext>();
                return(qlContext.Car.ToList());
            });

            descriptor
            .Field("car_add")
            .Type <CarType>()
            .Argument("input", x => { x.Type <CarInput>(); })
            .Resolver(context =>
            {
                Car car             = context.Argument <Car>("input");
                QLContext qlContext = context.Service <QLContext>();
                IDbContextTransaction transaction = qlContext.Database.BeginTransaction();
                try
                {
                    qlContext.Car.Add(car);
                    qlContext.SaveChanges();
                    transaction.Commit();
                }
                catch (System.Exception)
                {
                    transaction.Rollback();
                }
                finally
                {
                    transaction.Dispose();
                }
                return(car);
            });

            descriptor
            .Field("car_edit")
            .Type <CarType>()
            .Argument("input", x => { x.Type <CarInput>(); })
            .Resolver(context =>
            {
                Car car             = context.Argument <Car>("input");
                QLContext qlContext = context.Service <QLContext>();
                IDbContextTransaction transaction = qlContext.Database.BeginTransaction();
                try
                {
                    qlContext.Car.Update(car);
                    qlContext.SaveChanges();
                    transaction.Commit();
                }
                catch (System.Exception)
                {
                    transaction.Rollback();
                }
                finally
                {
                    transaction.Dispose();
                }
                return(car);
            });

            descriptor
            .Field("car_find")
            .Type <CarType>()
            .Argument("id", x => { x.Type <IntType>(); x.DefaultValue(0); })
            .Resolver(context =>
            {
                int id = context.Argument <int>("id");
                QLContext qlContext = context.Service <QLContext>();
                return(qlContext.Car.Find(id));
            });

            descriptor
            .Field("car_remove")
            .Type <RemoveType>()
            .Argument("id", x => { x.Type <IntType>(); x.DefaultValue(0); })
            .Resolver(context =>
            {
                int count           = 0;
                int id              = context.Argument <int>("id");
                QLContext qlContext = context.Service <QLContext>();
                var car             = qlContext.Car.Find(id);
                if (car != null)
                {
                    IDbContextTransaction transaction = qlContext.Database.BeginTransaction();
                    try
                    {
                        qlContext.Car.Remove(car);
                        count = qlContext.SaveChanges();
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                    }
                    finally
                    {
                        transaction.Dispose();
                    }
                }
                return(Remove.Create(count));
            });
        }
Example #28
0
 public bool CheckSemantics(QLContext context, List <Message> messages)
 {
     // Nothing to check for a question
     return(true);
 }
Example #29
0
 public ArticaleRepository(QLContext db) : base(db)
 {
     this._db = db;
 }
 public ClassificationRepository(QLContext db) : base(db)
 {
 }