Exemplo n.º 1
0
        public void TestJsonConstructorAsDocumentProjection()
        {
            // same as we use in find().field("{...}")
            string     projString = "{'a':'value for a', 'b':1+1, 'c'::bindvar, 'd':$.member[22], 'e':{'nested':'doc'}}";
            Projection proj       = new Projection();

            proj.Source = new ExprParser(projString, false).Parse();
            Assert.Equal(Expr.Types.Type.Object, proj.Source.Type);

            IEnumerator <Mysqlx.Expr.Object.Types.ObjectField> fields = proj.Source.Object.Fld.GetEnumerator();

            string[][] array = new string[][] {
                new string[] { "a", "\"value for a\"" },
                new string[] { "b", "(1 + 1)" },
                new string[] { "c", ":0" },
                new string[] { "d", "$.member[22]" },
                new string[] { "e", "{'nested':\"doc\"}" }
            };
            array.ToList().ForEach(pair =>
            {
                fields.MoveNext();
                Mysqlx.Expr.Object.Types.ObjectField f = fields.Current;
                Assert.Equal(pair[0], f.Key);
                Assert.Equal(pair[1], ExprUnparser.ExprToString(f.Value));
            });
            Assert.False(fields.MoveNext());
        }
Exemplo n.º 2
0
        public long TableCount(Schema schema, string name)
        {
            string sql = String.Format("SELECT COUNT(*) FROM {0}.{1}",
                                       ExprUnparser.QuoteIdentifier(schema.Name), ExprUnparser.QuoteIdentifier(name));

            return((long)ExecuteQueryAsScalar(sql));
        }
Exemplo n.º 3
0
        public void TestExprAsPathDocumentProjection()
        {
            List <Projection> projList = new ExprParser("$.a as b, (1 + 1) * 100 as x, 2 as j42").ParseDocumentProjection();

            Assert.Equal(3, projList.Count);

            // check $.a as b
            Projection proj = projList[0];
            IList <DocumentPathItem> paths = proj.Source.Identifier.DocumentPath;

            Assert.Equal(1, paths.Count);
            Assert.Equal(DocumentPathItem.Types.Type.Member, paths[0].Type);
            Assert.Equal("a", paths[0].Value);

            Assert.Equal("b", proj.Alias);

            // check (1 + 1) * 100 as x
            proj = projList[1];
            Assert.Equal("((1 + 1) * 100)", ExprUnparser.ExprToString(proj.Source));
            Assert.Equal("x", proj.Alias);

            // check 2 as j42
            proj = projList[2];
            Assert.Equal("2", ExprUnparser.ExprToString(proj.Source));
            Assert.Equal("j42", proj.Alias);
        }
Exemplo n.º 4
0
        public void TestStarTableSelectProjection()
        {
            List <Projection> proj = new ExprParser("*, b as c").ParseTableSelectProjection();

            Assert.Equal(2, proj.Count);
            Assert.Equal("*", ExprUnparser.ExprToString(proj[0].Source));
            Assert.Equal(string.Empty, proj[0].Alias);
            Assert.Equal("b", ExprUnparser.ExprToString(proj[1].Source));
            Assert.NotEqual(string.Empty, proj[1].Alias);
            Assert.Equal("c", proj[1].Alias);
        }
Exemplo n.º 5
0
        public void UnqualifiedDocPaths()
        {
            Expr expr = new ExprParser("1 + b[0]", false).Parse();

            Assert.Equal("(1 + $.b[0])", ExprUnparser.ExprToString(expr));
            expr = new ExprParser("a.*", false).Parse();
            Assert.Equal("$.a.*", ExprUnparser.ExprToString(expr));
            expr = new ExprParser("bL . vT .*", false).Parse();
            Assert.Equal("$.bL.vT.*", ExprUnparser.ExprToString(expr));
            expr = new ExprParser("dd ** .X", false).Parse();
            Assert.Equal("$.dd**.X", ExprUnparser.ExprToString(expr));
        }
Exemplo n.º 6
0
        public void TestComplexTableSelectProjection()
        {
            string            projectionString = "(1 + 1) * 100 as `one-o-two`, 'a is \\'a\\'' as `what is 'a'`";
            List <Projection> proj             = new ExprParser(projectionString).ParseTableSelectProjection();

            Assert.Equal(2, proj.Count);

            Assert.Equal("((1 + 1) * 100)", ExprUnparser.ExprToString(proj[0].Source));
            Assert.Equal("one-o-two", proj[0].Alias);

            Assert.Equal("a is 'a'", proj[1].Source.Literal.VString.Value.ToStringUtf8());
            Assert.Equal("what is 'a'", proj[1].Alias);
        }
Exemplo n.º 7
0
 public void JsonColumnPath(string exprString, string unparserString, bool isRelational)
 {
     if (unparserString == null)
     {
         Assert.Equal($"Unable to parse query '{exprString}'",
                      Assert.ThrowsAny <ArgumentException>(() => new ExprParser(exprString, isRelational).Parse()).Message);
     }
     else
     {
         Expr expr = new ExprParser(exprString, isRelational).Parse();
         Assert.Equal(unparserString, ExprUnparser.ExprToString(expr));
     }
 }
Exemplo n.º 8
0
 public long TableCount(Schema schema, string name, string type)
 {
     try
     {
         string sql = String.Format("SELECT COUNT(*) FROM {0}.{1}",
                                    ExprUnparser.QuoteIdentifier(schema.Name), ExprUnparser.QuoteIdentifier(name));
         return((long)ExecuteQueryAsScalar(sql));
     }
     catch (MySqlException ex) when(ex.Code == 1146)
     {
         throw new MySqlException(string.Format(ResourcesX.CollectionTableDoesNotExist, type.ToString(), name, schema.Name));
     }
 }
Exemplo n.º 9
0
        public void TestOrderByParserBasic()
        {
            List <Order> orderSpec = new ExprParser("a, b desc").ParseOrderSpec();

            Assert.Equal(2, orderSpec.Count);
            Order o1 = orderSpec[0];

            Assert.Equal(Order.Types.Direction.None, o1.Direction);
            Assert.Equal("a", ExprUnparser.ExprToString(o1.Expr));
            Order o2 = orderSpec[1];

            Assert.NotEqual(Order.Types.Direction.None, o2.Direction);
            Assert.Equal(Order.Types.Direction.Desc, o2.Direction);
            Assert.Equal("b", ExprUnparser.ExprToString(o2.Expr));
        }
Exemplo n.º 10
0
        /**
         * Check that a string parses and is reconstituted as a string that we expect. Futher we parse the canonical version to make sure it doesn't change.
         */
        private void CheckParseRoundTrip(string input, string expected)
        {
            if (expected == null)
            {
                expected = input;
            }
            Expr   expr          = new ExprParser(input).Parse();
            string canonicalized = ExprUnparser.ExprToString(expr);

            Assert.Equal(expected, canonicalized);

            // System.err.println("Canonicalized: " + canonicalized);
            Expr   expr2           = new ExprParser(canonicalized).Parse();
            string recanonicalized = ExprUnparser.ExprToString(expr2);

            Assert.Equal(expected, recanonicalized);
        }
Exemplo n.º 11
0
        public void TestOrderByParserComplexExpressions()
        {
            List <Order> orderSpec = new ExprParser("field not in ('a',func('b', 2.0),'c') desc, 1-a$**[0].*, now () + $.b + c > 2 asc").ParseOrderSpec();

            Assert.Equal(3, orderSpec.Count);
            Order o1 = orderSpec[0];

            Assert.NotEqual(Order.Types.Direction.None, o1.Direction);
            Assert.Equal(Order.Types.Direction.Desc, o1.Direction);
            Assert.Equal("field not in(\"a\", func(\"b\", 2), \"c\")", ExprUnparser.ExprToString(o1.Expr));
            Order o2 = orderSpec[1];

            Assert.Equal(Order.Types.Direction.None, o2.Direction);
            Assert.Equal("(1 - a$**[0].*)", ExprUnparser.ExprToString(o2.Expr));
            Order o3 = orderSpec[2];

            Assert.NotEqual(Order.Types.Direction.None, o3.Direction);
            Assert.Equal(Order.Types.Direction.Asc, o3.Direction);
            Assert.Equal("(((now() + $.b) + c) > 2)", ExprUnparser.ExprToString(o3.Expr));
        }
Exemplo n.º 12
0
        public void TestJsonLiteral()
        {
            Expr e = new ExprParser("{'a':1, 'b':\"a string\"}").Parse();

            Assert.Equal("{'a':1, 'b':\"a string\"}", ExprUnparser.ExprToString(e));

            Assert.Equal(Expr.Types.Type.Object, e.Type);
            Mysqlx.Expr.Object o = e.Object;
            Assert.Equal(2, o.Fld.Count);
            Mysqlx.Expr.Object.Types.ObjectField of;

            of = o.Fld[0];
            Assert.Equal("a", of.Key);
            e = of.Value;
            Assert.Equal(Expr.Types.Type.Literal, e.Type);
            Assert.Equal(1, e.Literal.VSignedInt);

            of = o.Fld[1];
            Assert.Equal("b", of.Key);
            e = of.Value;
            Assert.Equal(Expr.Types.Type.Literal, e.Type);
            Assert.Equal("a string", e.Literal.VString.Value.ToStringUtf8());
        }