Beispiel #1
0
        public virtual void ComplicatedQuery()
        {
            FromTerm tCustomers = FromTerm.Table("customers", "c");
            FromTerm tProducts  = FromTerm.Table("products", "p");
            FromTerm tOrders    = FromTerm.Table("orders", "o");

            SelectQuery query = new SelectQuery();

            query.Columns.Add(new SelectColumn("name", tCustomers));
            query.Columns.Add(new SelectColumn(SqlExpression.IfNull(SqlExpression.Field("name", tCustomers), SqlExpression.Constant(SqlConstant.String("name"))), "notNull"));
            query.Columns.Add(new SelectColumn(SqlExpression.Null(), "nullValue"));
            query.Columns.Add(new SelectColumn("name", tProducts, "productName", SqlAggregationFunction.None));
            query.Columns.Add(new SelectColumn("price", tProducts));

            query.FromClause.BaseTable = tCustomers;
            query.FromClause.Join(JoinType.Left, tCustomers, tOrders, "customerId", "customerId");
            query.FromClause.Join(JoinType.Inner, tOrders, tProducts, "productId", "productId");

            query.WherePhrase.Terms.Add(WhereTerm.CreateCompare(SqlExpression.Field("name", tCustomers), SqlExpression.String("John"), CompareOperator.Equal));
            query.WherePhrase.Terms.Add(WhereTerm.CreateCompare(SqlExpression.String("Dohe"), SqlExpression.Field("name", tCustomers), CompareOperator.NotEqual));
            query.WherePhrase.Terms.Add(WhereTerm.CreateCompare(SqlExpression.Field("name", tCustomers), SqlExpression.String("J%"), CompareOperator.Like));
            query.WherePhrase.Terms.Add(WhereTerm.CreateCompare(SqlExpression.Date(DateTime.Now), SqlExpression.Field("date", tOrders), CompareOperator.Greater));
            query.WherePhrase.Terms.Add(WhereTerm.CreateCompare(SqlExpression.Number(10), SqlExpression.Number(9), CompareOperator.Greater));
            query.WherePhrase.Terms.Add(WhereTerm.CreateCompare(SqlExpression.Number(10), SqlExpression.Number(9), CompareOperator.GreaterOrEqual));
            query.WherePhrase.Terms.Add(WhereTerm.CreateCompare(SqlExpression.Number(10), SqlExpression.Number(11.5), CompareOperator.LessOrEqual));
            query.WherePhrase.Terms.Add(WhereTerm.CreateCompare(SqlExpression.Number(1), SqlExpression.Number(1), CompareOperator.BitwiseAnd));

            WhereClause group = new WhereClause(WhereClauseRelationship.Or);

            group.Terms.Add(WhereTerm.CreateBetween(SqlExpression.Field("price", tProducts), SqlExpression.Number(1), SqlExpression.Number(10)));
            group.Terms.Add(WhereTerm.CreateIn(SqlExpression.Field("name", tProducts), SqlConstantCollection.FromList(new string[] { "Nail", "Hamer", "Skrewdriver" })));
            group.Terms.Add(WhereTerm.CreateIn(SqlExpression.Field("name", tProducts), "select name from products"));
            group.Terms.Add(WhereTerm.CreateNotIn(SqlExpression.Field("name", tProducts), SqlConstantCollection.FromList(new string[] { "Unkown" })));
            group.Terms.Add(WhereTerm.CreateNotIn(SqlExpression.Field("name", tProducts), "select name from products"));
            group.Terms.Add(WhereTerm.CreateIsNull(SqlExpression.Field("name", tProducts)));
            group.Terms.Add(WhereTerm.CreateIsNotNull(SqlExpression.Field("name", tProducts)));
            group.Terms.Add(WhereTerm.CreateExists("select productId from products"));
            group.Terms.Add(WhereTerm.CreateNotExists("select productId from products"));

            query.WherePhrase.SubClauses.Add(group);

            query.OrderByTerms.Add(new OrderByTerm("name", tCustomers, OrderByDirection.Descending));
            query.OrderByTerms.Add(new OrderByTerm("price", OrderByDirection.Ascending));

            query.Distinct = true;
            query.Top      = 10;

            RenderSelect(query);
        }