public override SqlObject VisitStringPathExpression([NotNull] sqlParser.StringPathExpressionContext context)
        {
            Contract.Requires(context != null);

            SqlPathExpression pathExpression = (SqlPathExpression)this.Visit(context.path_expression());
            SqlStringLiteral  stringIndex    = SqlStringLiteral.Create(CstToAstVisitor.GetStringValueFromNode(context.STRING_LITERAL()));

            return(SqlStringPathExpression.Create(pathExpression, stringIndex));
        }
        public override void Visit(SqlStringPathExpression sqlStringPathExpression)
        {
            if (sqlStringPathExpression.ParentPath != null)
            {
                sqlStringPathExpression.ParentPath.Accept(this);
            }

            this.writer.Write("[");
            sqlStringPathExpression.Value.Accept(this);
            this.writer.Write("]");
        }
        public override int Visit(SqlStringPathExpression sqlStringPathExpression)
        {
            int hashCode = SqlStringPathExpressionHashCode;

            if (sqlStringPathExpression.ParentPath != null)
            {
                hashCode = CombineHashes(hashCode, sqlStringPathExpression.ParentPath.Accept(this));
            }

            hashCode = CombineHashes(hashCode, sqlStringPathExpression.Value.Accept(this));
            return(hashCode);
        }
        public void SqlArrayScalarExpressionTest()
        {
            CosmosObject tag = CosmosObject.Create(new Dictionary <string, CosmosElement>
            {
                ["name"] = CosmosString.Create("asdf")
            });

            CosmosObject tags = CosmosObject.Create(new Dictionary <string, CosmosElement>
            {
                ["tags"] = CosmosArray.Create(new List <CosmosElement>()
                {
                    tag
                }),
                ["_rid"] = CosmosString.Create("AYIMAMmFOw8YAAAAAAAAAA==")
            });

            CosmosObject tagsWrapped = CosmosObject.Create(new Dictionary <string, CosmosElement>
            {
                ["c"]    = tags,
                ["_rid"] = CosmosString.Create("AYIMAMmFOw8YAAAAAAAAAA==")
            });

            // ARRAY(SELECT VALUE t.name FROM t in c.tags)
            SqlArrayScalarExpression arrayScalarExpression = SqlArrayScalarExpression.Create(
                SqlQuery.Create(
                    SqlSelectClause.Create(
                        SqlSelectValueSpec.Create(
                            TestUtils.CreatePathExpression("t", "name"))),
                    SqlFromClause.Create(
                        SqlArrayIteratorCollectionExpression.Create(
                            SqlIdentifier.Create("t"),
                            SqlInputPathCollection.Create(
                                SqlIdentifier.Create("c"),
                                SqlStringPathExpression.Create(
                                    null,
                                    SqlStringLiteral.Create("tags"))))),
                    whereClause: null,
                    groupByClause: null,
                    orderByClause: null,
                    offsetLimitClause: null));

            CosmosArray tagNames = CosmosArray.Create(new List <CosmosElement>()
            {
                CosmosString.Create("asdf")
            });

            AssertEvaluation(tagNames, arrayScalarExpression, tagsWrapped);
        }
        public override bool Visit(SqlStringPathExpression first, SqlObject secondAsObject)
        {
            if (!(secondAsObject is SqlStringPathExpression second))
            {
                return(false);
            }

            if (!Equals(first.Value, second.Value))
            {
                return(false);
            }

            if (!Equals(first.ParentPath, second.ParentPath))
            {
                return(false);
            }

            return(true);
        }
        public void ArrayIteratorCollectionExpressionTest()
        {
            // FROM p in f.parents
            SqlArrayIteratorCollectionExpression fromPInFDotParents = SqlArrayIteratorCollectionExpression.Create(
                SqlIdentifier.Create("p"),
                SqlInputPathCollection.Create(
                    SqlIdentifier.Create("f"),
                    SqlStringPathExpression.Create(
                        null,
                        SqlStringLiteral.Create("parents"))));

            CosmosElement andersenParent1  = CosmosElement.Parse(@"{ ""p"" : { ""firstName"": ""Thomas"" }, ""_rid"": ""0fomAIxnukU1AQAAAAAAAA==""}");
            CosmosElement andersenParent2  = CosmosElement.Parse(@"{ ""p"" : { ""firstName"": ""Mary Kay""}, ""_rid"": ""0fomAIxnukU1AQAAAAAAAA=="" }");
            CosmosElement wakeFieldParent1 = CosmosElement.Parse(@"{ ""p"" : { ""familyName"": ""Wakefield"", ""givenName"": ""Robin"" }, ""_rid"": ""0fomAIxnukU1AQAAAAAAAB=="" }");
            CosmosElement wakeFieldParent2 = CosmosElement.Parse(@"{ ""p"" : { ""familyName"": ""Miller"", ""givenName"": ""Ben"" }, ""_rid"": ""0fomAIxnukU1AQAAAAAAAB=="" }");

            AssertEvaluation(new CosmosElement[]
            {
                andersenParent1,
                andersenParent2,
                wakeFieldParent1,
                wakeFieldParent2,
            }, fromPInFDotParents, DataSource);
        }
Exemple #7
0
 public abstract void Visit(SqlStringPathExpression sqlObject);
 public override SqlObject Visit(SqlStringPathExpression sqlStringPathExpression)
 {
     return(SqlStringPathExpression.Create(
                sqlStringPathExpression.ParentPath?.Accept(this) as SqlPathExpression,
                sqlStringPathExpression.Value.Accept(this) as SqlStringLiteral));
 }
Exemple #9
0
 public abstract TResult Visit(SqlStringPathExpression sqlObject);
        public void SqlExistsScalarExpressionTest()
        {
            CosmosObject tag = CosmosObject.Create(new Dictionary <string, CosmosElement>
            {
                ["name"] = CosmosString.Create("asdf")
            });

            CosmosObject tags = CosmosObject.Create(new Dictionary <string, CosmosElement>
            {
                ["tags"] = CosmosArray.Create(new List <CosmosElement>()
                {
                    tag
                }),
                ["_rid"] = CosmosString.Create("AYIMAMmFOw8YAAAAAAAAAA==")
            });

            CosmosObject tagsWrapped = CosmosObject.Create(new Dictionary <string, CosmosElement>
            {
                ["c"]    = tags,
                ["_rid"] = CosmosString.Create("AYIMAMmFOw8YAAAAAAAAAA==")
            });

            // EXISTS(SELECT VALUE t.name FROM t in c.tags where t.name = "asdf")
            SqlExistsScalarExpression existsScalarExpressionMatched = SqlExistsScalarExpression.Create(
                SqlQuery.Create(
                    SqlSelectClause.Create(
                        SqlSelectValueSpec.Create(
                            TestUtils.CreatePathExpression("t", "name"))),
                    SqlFromClause.Create(
                        SqlArrayIteratorCollectionExpression.Create(
                            SqlIdentifier.Create("t"),
                            SqlInputPathCollection.Create(
                                SqlIdentifier.Create("c"),
                                SqlStringPathExpression.Create(
                                    null,
                                    SqlStringLiteral.Create("tags"))))),
                    SqlWhereClause.Create(
                        SqlBinaryScalarExpression.Create(
                            SqlBinaryScalarOperatorKind.Equal,
                            TestUtils.CreatePathExpression("t", "name"),
                            SqlLiteralScalarExpression.Create(SqlStringLiteral.Create("asdf")))),
                    groupByClause: null,
                    orderByClause: null,
                    offsetLimitClause: null));

            AssertEvaluation(CosmosBoolean.Create(true), existsScalarExpressionMatched, tagsWrapped);

            SqlExistsScalarExpression existsScalarExpressionNotMatched = SqlExistsScalarExpression.Create(
                SqlQuery.Create(
                    SqlSelectClause.Create(
                        SqlSelectValueSpec.Create(
                            TestUtils.CreatePathExpression("t", "name"))),
                    SqlFromClause.Create(
                        SqlArrayIteratorCollectionExpression.Create(
                            SqlIdentifier.Create("t"),
                            SqlInputPathCollection.Create(
                                SqlIdentifier.Create("c"),
                                SqlStringPathExpression.Create(
                                    null,
                                    SqlStringLiteral.Create("tags"))))),
                    SqlWhereClause.Create(
                        SqlBinaryScalarExpression.Create(
                            SqlBinaryScalarOperatorKind.NotEqual,
                            TestUtils.CreatePathExpression("t", "name"),
                            SqlLiteralScalarExpression.Create(SqlStringLiteral.Create("asdf")))),
                    groupByClause: null,
                    orderByClause: null,
                    offsetLimitClause: null));

            AssertEvaluation(CosmosBoolean.Create(false), existsScalarExpressionNotMatched, tagsWrapped);
        }
        public void AliasedCollectionExpressionTest()
        {
            // FROM c
            SqlAliasedCollectionExpression fromC = SqlAliasedCollectionExpression.Create(
                SqlInputPathCollection.Create(
                    SqlIdentifier.Create("c"),
                    relativePath: null),
                alias: null);

            CosmosObject andersenWrapped = CosmosObject.Create(new Dictionary <string, CosmosElement>
            {
                ["c"]    = AndersenFamily,
                ["_rid"] = AndersenFamily["_rid"]
            });

            CosmosObject wakeFieldWrapped = CosmosObject.Create(new Dictionary <string, CosmosElement>
            {
                ["c"]    = WakefieldFamily,
                ["_rid"] = WakefieldFamily["_rid"]
            });

            AssertEvaluation(new CosmosElement[] { andersenWrapped, wakeFieldWrapped }, fromC, DataSource);

            // FROM c.id
            SqlAliasedCollectionExpression fromCDotId = SqlAliasedCollectionExpression.Create(
                SqlInputPathCollection.Create(
                    SqlIdentifier.Create("c"),
                    SqlIdentifierPathExpression.Create(
                        null,
                        SqlIdentifier.Create("id"))),
                alias: null);

            CosmosObject andersenId = CosmosObject.Create(new Dictionary <string, CosmosElement>
            {
                ["id"]   = CosmosString.Create("AndersenFamily"),
                ["_rid"] = AndersenFamily["_rid"]
            });

            CosmosObject wakefieldId = CosmosObject.Create(new Dictionary <string, CosmosElement>
            {
                ["id"]   = CosmosString.Create("WakefieldFamily"),
                ["_rid"] = WakefieldFamily["_rid"]
            });

            AssertEvaluation(new CosmosElement[] { andersenId, wakefieldId }, fromCDotId, DataSource);

            // FROM c.id AS familyId
            SqlAliasedCollectionExpression fromCDotIdAsFamilyId = SqlAliasedCollectionExpression.Create(
                SqlInputPathCollection.Create(
                    SqlIdentifier.Create("c"),
                    SqlIdentifierPathExpression.Create(
                        null,
                        SqlIdentifier.Create("id"))),
                SqlIdentifier.Create("familyId"));

            CosmosObject andersenIdAsFamilyId = CosmosObject.Create(new Dictionary <string, CosmosElement>
            {
                ["familyId"] = CosmosString.Create("AndersenFamily"),
                ["_rid"]     = AndersenFamily["_rid"]
            });

            CosmosObject wakefieldIdAsFamilyId = CosmosObject.Create(new Dictionary <string, CosmosElement>
            {
                ["familyId"] = CosmosString.Create("WakefieldFamily"),
                ["_rid"]     = WakefieldFamily["_rid"]
            });

            AssertEvaluation(new CosmosElement[] { andersenIdAsFamilyId, wakefieldIdAsFamilyId }, fromCDotIdAsFamilyId, DataSource);

            // FROM (SELECT VALUE child["grade"] FROM child IN c.children) grade
            SqlAliasedCollectionExpression fromSubqueryGrades = SqlAliasedCollectionExpression.Create(
                SqlSubqueryCollection.Create(
                    SqlQuery.Create(
                        SqlSelectClause.Create(
                            SqlSelectValueSpec.Create(
                                TestUtils.CreatePathExpression("child", "grade"))),
                        SqlFromClause.Create(
                            SqlArrayIteratorCollectionExpression.Create(
                                SqlIdentifier.Create("child"),
                                SqlInputPathCollection.Create(
                                    SqlIdentifier.Create("c"),
                                    SqlStringPathExpression.Create(
                                        null,
                                        SqlStringLiteral.Create("children"))))),
                        whereClause: null,
                        groupByClause: null,
                        orderByClause: null,
                        offsetLimitClause: null)),
                SqlIdentifier.Create("grade"));

            CosmosObject henrietteWrapped = CosmosObject.Create(new Dictionary <string, CosmosElement>
            {
                ["grade"] = CosmosNumber64.Create(5),
                ["_rid"]  = AndersenFamily["_rid"]
            });

            CosmosObject jesseWrapped = CosmosObject.Create(new Dictionary <string, CosmosElement>
            {
                ["grade"] = CosmosNumber64.Create(1),
                ["_rid"]  = WakefieldFamily["_rid"]
            });

            CosmosObject lisaWrapped = CosmosObject.Create(new Dictionary <string, CosmosElement>
            {
                ["grade"] = CosmosNumber64.Create(8),
                ["_rid"]  = WakefieldFamily["_rid"]
            });

            AssertEvaluation(
                new CosmosElement[]
            {
                henrietteWrapped,
                jesseWrapped,
                lisaWrapped
            },
                fromSubqueryGrades,
                DataSource);
        }
        public void JoinCollectionExpressionTest()
        {
            // FROM f
            // JOIN c IN f.children
            // JOIN p in c.pets
            SqlAliasedCollectionExpression fromf = SqlAliasedCollectionExpression.Create(
                SqlInputPathCollection.Create(
                    SqlIdentifier.Create("f"),
                    relativePath: null),
                alias: null);

            SqlArrayIteratorCollectionExpression cInFDotChildren = SqlArrayIteratorCollectionExpression.Create(
                SqlIdentifier.Create("c"),
                SqlInputPathCollection.Create(
                    SqlIdentifier.Create("f"),
                    SqlStringPathExpression.Create(
                        null,
                        SqlStringLiteral.Create("children"))));

            SqlJoinCollectionExpression joinFC = SqlJoinCollectionExpression.Create(
                fromf,
                cInFDotChildren);

            CosmosElement andersenChild1 = CosmosElement.Parse(@"
            {
              ""f"": {
                  ""id"": ""AndersenFamily"",
                  ""lastName"": ""Andersen"",
                  ""parents"": [
                     { ""firstName"": ""Thomas"" },
                     { ""firstName"": ""Mary Kay""}
                  ],
                  ""children"": [
                     {
                         ""firstName"": ""Henriette Thaulow"",
                         ""gender"": ""female"",
                         ""grade"": 5,
                         ""pets"": [{ ""givenName"": ""Fluffy"" }]
                     }
                  ],
                  ""address"": { ""state"": ""WA"", ""county"": ""King"", ""city"": ""seattle"" },
                  ""creationDate"": 1431620472,
                  ""isRegistered"": true, 
                  ""_rid"": ""0fomAIxnukU1AQAAAAAAAA==""
                },
              ""c"": {
                         ""firstName"": ""Henriette Thaulow"",
                         ""gender"": ""female"",
                         ""grade"": 5,
                         ""pets"": [{ ""givenName"": ""Fluffy"" }]
                     }, ""_rid"": ""0fomAIxnukU1AQAAAAAAAA==""
            }");

            CosmosElement wakeFieldChild1 = CosmosElement.Parse(@"
            {
              ""f"": {
                  ""id"": ""WakefieldFamily"",
                  ""parents"": [
                      { ""familyName"": ""Wakefield"", ""givenName"": ""Robin"" },
                      { ""familyName"": ""Miller"", ""givenName"": ""Ben"" }
                  ],
                  ""children"": [
                      {
                        ""familyName"": ""Merriam"",
                        ""givenName"": ""Jesse"",
                        ""gender"": ""female"", ""grade"": 1,
                        ""pets"": [
                            { ""givenName"": ""Goofy"" },
                            { ""givenName"": ""Shadow"" }
                        ]
                      },
                      { 
                        ""familyName"": ""Miller"", 
                         ""givenName"": ""Lisa"", 
                         ""gender"": ""female"", 
                         ""grade"": 8 }
                  ],
                  ""address"": { ""state"": ""NY"", ""county"": ""Manhattan"", ""city"": ""NY"" },
                  ""creationDate"": 1431620462,
                  ""isRegistered"": false, 
                  ""_rid"": ""0fomAIxnukU1AQAAAAAAAB==""
                },
              ""c"": {
                        ""familyName"": ""Merriam"",
                        ""givenName"": ""Jesse"",
                        ""gender"": ""female"", ""grade"": 1,
                        ""pets"": [
                            { ""givenName"": ""Goofy"" },
                            { ""givenName"": ""Shadow"" }
                        ]
                      }, ""_rid"": ""0fomAIxnukU1AQAAAAAAAB==""
            }");

            CosmosElement wakeFieldChild2 = CosmosElement.Parse(@"
            {
              ""f"": {
                  ""id"": ""WakefieldFamily"",
                  ""parents"": [
                      { ""familyName"": ""Wakefield"", ""givenName"": ""Robin"" },
                      { ""familyName"": ""Miller"", ""givenName"": ""Ben"" }
                  ],
                  ""children"": [
                      {
                        ""familyName"": ""Merriam"",
                        ""givenName"": ""Jesse"",
                        ""gender"": ""female"", ""grade"": 1,
                        ""pets"": [
                            { ""givenName"": ""Goofy"" },
                            { ""givenName"": ""Shadow"" }
                        ]
                      },
                      { 
                        ""familyName"": ""Miller"", 
                         ""givenName"": ""Lisa"", 
                         ""gender"": ""female"", 
                         ""grade"": 8 }
                  ],
                  ""address"": { ""state"": ""NY"", ""county"": ""Manhattan"", ""city"": ""NY"" },
                  ""creationDate"": 1431620462,
                  ""isRegistered"": false, 
                  ""_rid"": ""0fomAIxnukU1AQAAAAAAAB==""
                },
              ""c"": { 
                        ""familyName"": ""Miller"", 
                         ""givenName"": ""Lisa"", 
                         ""gender"": ""female"", 
                         ""grade"": 8 }, ""_rid"": ""0fomAIxnukU1AQAAAAAAAB==""
            }");

            AssertEvaluation(new CosmosElement[] { andersenChild1, wakeFieldChild1, wakeFieldChild2 }, joinFC, DataSource);

            SqlArrayIteratorCollectionExpression pInCDotPets = SqlArrayIteratorCollectionExpression.Create(
                SqlIdentifier.Create("p"),
                SqlInputPathCollection.Create(
                    SqlIdentifier.Create("c"),
                    SqlStringPathExpression.Create(
                        null,
                        SqlStringLiteral.Create("pets"))));

            SqlJoinCollectionExpression joinFCP = SqlJoinCollectionExpression.Create(
                SqlJoinCollectionExpression.Create(fromf, cInFDotChildren),
                pInCDotPets);

            CosmosElement andersenChild1Pet1 = CosmosElement.Parse(@"
            {
              ""f"": {
                  ""id"": ""AndersenFamily"",
                  ""lastName"": ""Andersen"",
                  ""parents"": [
                     { ""firstName"": ""Thomas"" },
                     { ""firstName"": ""Mary Kay""}
                  ],
                  ""children"": [
                     {
                         ""firstName"": ""Henriette Thaulow"",
                         ""gender"": ""female"",
                         ""grade"": 5,
                         ""pets"": [{ ""givenName"": ""Fluffy"" }]
                     }
                  ],
                  ""address"": { ""state"": ""WA"", ""county"": ""King"", ""city"": ""seattle"" },
                  ""creationDate"": 1431620472,
                  ""isRegistered"": true, 
                  ""_rid"": ""0fomAIxnukU1AQAAAAAAAA==""
                },
              ""c"": {
                         ""firstName"": ""Henriette Thaulow"",
                         ""gender"": ""female"",
                         ""grade"": 5,
                         ""pets"": [{ ""givenName"": ""Fluffy"" }]
                     },
              ""p"": { ""givenName"": ""Fluffy"" }, 
              ""_rid"": ""0fomAIxnukU1AQAAAAAAAA==""
            }");

            CosmosElement wakeFieldChild1Pet1 = CosmosElement.Parse(@"
            {
              ""f"": {
                  ""id"": ""WakefieldFamily"",
                  ""parents"": [
                      { ""familyName"": ""Wakefield"", ""givenName"": ""Robin"" },
                      { ""familyName"": ""Miller"", ""givenName"": ""Ben"" }
                  ],
                  ""children"": [
                      {
                        ""familyName"": ""Merriam"",
                        ""givenName"": ""Jesse"",
                        ""gender"": ""female"", ""grade"": 1,
                        ""pets"": [
                            { ""givenName"": ""Goofy"" },
                            { ""givenName"": ""Shadow"" }
                        ]
                      },
                      { 
                        ""familyName"": ""Miller"", 
                         ""givenName"": ""Lisa"", 
                         ""gender"": ""female"", 
                         ""grade"": 8 }
                  ],
                  ""address"": { ""state"": ""NY"", ""county"": ""Manhattan"", ""city"": ""NY"" },
                  ""creationDate"": 1431620462,
                  ""isRegistered"": false, 
                  ""_rid"": ""0fomAIxnukU1AQAAAAAAAB==""
                },
              ""c"": {
                        ""familyName"": ""Merriam"",
                        ""givenName"": ""Jesse"",
                        ""gender"": ""female"", ""grade"": 1,
                        ""pets"": [
                            { ""givenName"": ""Goofy"" },
                            { ""givenName"": ""Shadow"" }
                        ]
                      },
              ""p"": { ""givenName"": ""Goofy"" }, 
              ""_rid"": ""0fomAIxnukU1AQAAAAAAAB==""
            }");

            CosmosElement wakeFieldChild1Pet2 = CosmosElement.Parse(@"
            {
              ""f"": {
                  ""id"": ""WakefieldFamily"",
                  ""parents"": [
                      { ""familyName"": ""Wakefield"", ""givenName"": ""Robin"" },
                      { ""familyName"": ""Miller"", ""givenName"": ""Ben"" }
                  ],
                  ""children"": [
                      {
                        ""familyName"": ""Merriam"",
                        ""givenName"": ""Jesse"",
                        ""gender"": ""female"", ""grade"": 1,
                        ""pets"": [
                            { ""givenName"": ""Goofy"" },
                            { ""givenName"": ""Shadow"" }
                        ]
                      },
                      { 
                        ""familyName"": ""Miller"", 
                         ""givenName"": ""Lisa"", 
                         ""gender"": ""female"", 
                         ""grade"": 8 }
                  ],
                  ""address"": { ""state"": ""NY"", ""county"": ""Manhattan"", ""city"": ""NY"" },
                  ""creationDate"": 1431620462,
                  ""isRegistered"": false, 
                  ""_rid"": ""0fomAIxnukU1AQAAAAAAAB==""
                },
              ""c"": {
                        ""familyName"": ""Merriam"",
                        ""givenName"": ""Jesse"",
                        ""gender"": ""female"", ""grade"": 1,
                        ""pets"": [
                            { ""givenName"": ""Goofy"" },
                            { ""givenName"": ""Shadow"" }
                        ]
                      },
              ""p"": { ""givenName"": ""Shadow"" }, 
              ""_rid"": ""0fomAIxnukU1AQAAAAAAAB==""
            }");

            AssertEvaluation(new CosmosElement[] { andersenChild1Pet1, wakeFieldChild1Pet1, wakeFieldChild1Pet2 }, joinFCP, DataSource);

            // SELECT c.id
            // FROM c
            // JOIN c.nonExistent

            SqlAliasedCollectionExpression c = SqlAliasedCollectionExpression.Create(
                SqlInputPathCollection.Create(
                    SqlIdentifier.Create("c"),
                    relativePath: null),
                alias: null);

            SqlAliasedCollectionExpression cDotNonExistent = SqlAliasedCollectionExpression.Create(
                SqlInputPathCollection.Create(
                    SqlIdentifier.Create("c"),
                    SqlStringPathExpression.Create(
                        null,
                        SqlStringLiteral.Create("nonExistent"))),
                alias: null);

            SqlJoinCollectionExpression joinCAndCDotNonExistent = SqlJoinCollectionExpression.Create(
                c,
                cDotNonExistent);

            AssertEvaluation(new CosmosElement[] { }, joinCAndCDotNonExistent, DataSource);
        }
Exemple #13
0
 public abstract TOutput Visit(SqlStringPathExpression sqlObject, TArg input);
 public override IEnumerable <Either <long, string> > Visit(SqlStringPathExpression sqlObject)
 {
     this.tokens.Add(sqlObject.Value.Value);
     sqlObject.ParentPath?.Accept(this);
     return(this.tokens);
 }