Skip to content

Commit

Permalink
Fixed use of OPENJSON in CROSS APPLY
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMpn committed Nov 15, 2023
1 parent c7c5b81 commit 588d0b1
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
90 changes: 90 additions & 0 deletions MarkMpn.Sql4Cds.Engine.Tests/AdoProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1799,5 +1799,95 @@ FROM OPENJSON(@json2)
}
}
}

[TestMethod]
public void NestedJson()
{
using (var con = new Sql4CdsConnection(_localDataSource))
using (var cmd = con.CreateCommand())
{
cmd.CommandText = @"
--simple cross apply example
DECLARE @JSON NVARCHAR(MAX) = N'[
{
""OrderNumber"":""SO43659"",
""OrderDate"":""2011-05-31T00:00:00"",
""AccountNumber"":""AW29825"",
""ItemPrice"":2024.9940,
""ItemQuantity"":1
},
{
""OrderNumber"":""SO43661"",
""OrderDate"":""2011-06-01T00:00:00"",
""AccountNumber"":""AW73565"",
""ItemPrice"":2024.9940,
""ItemQuantity"":3
}
]'
SELECT root.[key] AS [Order],TheValues.[key], TheValues.[value]
FROM OPENJSON ( @JSON ) AS root
CROSS APPLY OPENJSON ( root.value) AS TheValues";

using (var reader = cmd.ExecuteReader())
{
Assert.AreEqual("Order", reader.GetName(0));
Assert.AreEqual("key", reader.GetName(1));
Assert.AreEqual("value", reader.GetName(2));

Assert.IsTrue(reader.Read());
Assert.AreEqual("0", reader.GetString(0));
Assert.AreEqual("OrderNumber", reader.GetString(1));
Assert.AreEqual("SO43659", reader.GetString(2));

Assert.IsTrue(reader.Read());
Assert.AreEqual("0", reader.GetString(0));
Assert.AreEqual("OrderDate", reader.GetString(1));
Assert.AreEqual("2011-05-31T00:00:00", reader.GetString(2));

Assert.IsTrue(reader.Read());
Assert.AreEqual("0", reader.GetString(0));
Assert.AreEqual("AccountNumber", reader.GetString(1));
Assert.AreEqual("AW29825", reader.GetString(2));

Assert.IsTrue(reader.Read());
Assert.AreEqual("0", reader.GetString(0));
Assert.AreEqual("ItemPrice", reader.GetString(1));
Assert.AreEqual("2024.9940", reader.GetString(2));

Assert.IsTrue(reader.Read());
Assert.AreEqual("0", reader.GetString(0));
Assert.AreEqual("ItemQuantity", reader.GetString(1));
Assert.AreEqual("1", reader.GetString(2));

Assert.IsTrue(reader.Read());
Assert.AreEqual("1", reader.GetString(0));
Assert.AreEqual("OrderNumber", reader.GetString(1));
Assert.AreEqual("SO43661", reader.GetString(2));

Assert.IsTrue(reader.Read());
Assert.AreEqual("1", reader.GetString(0));
Assert.AreEqual("OrderDate", reader.GetString(1));
Assert.AreEqual("2011-06-01T00:00:00", reader.GetString(2));

Assert.IsTrue(reader.Read());
Assert.AreEqual("1", reader.GetString(0));
Assert.AreEqual("AccountNumber", reader.GetString(1));
Assert.AreEqual("AW73565", reader.GetString(2));

Assert.IsTrue(reader.Read());
Assert.AreEqual("1", reader.GetString(0));
Assert.AreEqual("ItemPrice", reader.GetString(1));
Assert.AreEqual("2024.9940", reader.GetString(2));

Assert.IsTrue(reader.Read());
Assert.AreEqual("1", reader.GetString(0));
Assert.AreEqual("ItemQuantity", reader.GetString(1));
Assert.AreEqual("3", reader.GetString(2));

Assert.IsFalse(reader.Read());
}
}
}
}
}
7 changes: 7 additions & 0 deletions MarkMpn.Sql4Cds.Engine/Visitors/RewriteVisitorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,12 @@ public override void ExplicitVisit(SchemaObjectFunctionTableReference node)
}
}
}

public override void ExplicitVisit(OpenJsonTableReference node)
{
base.ExplicitVisit(node);
ReplaceExpression(node, n => n.Variable);
ReplaceExpression(node, n => n.RowPattern);
}
}
}

0 comments on commit 588d0b1

Please sign in to comment.