|
| 1 | +using System.Text.Json; |
| 2 | +using SqlServerSimulator.Storage; |
| 3 | + |
| 4 | +namespace SqlServerSimulator.Parser.Expressions; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// SQL <c>JSON_QUERY(json, path)</c>: extracts an object or array subtree |
| 8 | +/// from a JSON text by path. Returns <c>nvarchar(MAX)</c>; scalar matches |
| 9 | +/// and missing-path cases yield SQL NULL under the default lax mode — |
| 10 | +/// complement of <see cref="JsonValue"/> (which returns NULL for non-scalar |
| 11 | +/// matches and the scalar text otherwise). |
| 12 | +/// </summary> |
| 13 | +/// <remarks> |
| 14 | +/// The path argument is optional in real SQL Server (default <c>'$'</c> — |
| 15 | +/// returns the whole document). DACFx-emitted computed columns (WWI's |
| 16 | +/// <c>Application.People.OtherLanguages</c>, <c>Warehouse.StockItems.Tags</c>) |
| 17 | +/// supply an explicit path so this implementation requires it; the bare |
| 18 | +/// 1-arg form raises Msg 102 at parse via SyntaxErrorNear. |
| 19 | +/// </remarks> |
| 20 | +internal sealed class JsonQuery : Expression |
| 21 | +{ |
| 22 | + private readonly Expression jsonInput; |
| 23 | + private readonly Expression pathInput; |
| 24 | + |
| 25 | + public JsonQuery(ParserContext context) |
| 26 | + { |
| 27 | + this.jsonInput = Parse(context); |
| 28 | + if (context.Token is not Tokens.Operator { Character: ',' }) |
| 29 | + throw SimulatedSqlException.SyntaxErrorNear(context); |
| 30 | + this.pathInput = Parse(context.MoveNextRequiredReturnSelf()); |
| 31 | + } |
| 32 | + |
| 33 | + public override SqlValue Run(RuntimeContext runtime) |
| 34 | + { |
| 35 | + var jsonValue = this.jsonInput.Run(runtime); |
| 36 | + var pathValue = this.pathInput.Run(runtime); |
| 37 | + if (jsonValue.IsNull || pathValue.IsNull) |
| 38 | + return SqlValue.Null(SqlType.NVarchar); |
| 39 | + |
| 40 | + var path = JsonPath.Parse(pathValue.AsString); |
| 41 | + |
| 42 | + JsonDocument doc; |
| 43 | + try |
| 44 | + { |
| 45 | + doc = JsonDocument.Parse(jsonValue.AsString); |
| 46 | + } |
| 47 | + catch (JsonException) |
| 48 | + { |
| 49 | + return path.Mode == JsonPathMode.Strict ? throw SimulatedSqlException.JsonInvalidText() : SqlValue.Null(SqlType.NVarchar); |
| 50 | + } |
| 51 | + |
| 52 | + using (doc) |
| 53 | + { |
| 54 | + var match = path.Walk(doc.RootElement); |
| 55 | + if (match is null) |
| 56 | + return SqlValue.Null(SqlType.NVarchar); |
| 57 | + |
| 58 | + var element = match.Value; |
| 59 | + return element.ValueKind switch |
| 60 | + { |
| 61 | + // Object / Array — JSON_QUERY returns the raw JSON text of |
| 62 | + // the subtree (re-serialized via GetRawText, which preserves |
| 63 | + // the input's whitespace shape — matching SQL Server's |
| 64 | + // observable behavior on round-trip). |
| 65 | + JsonValueKind.Object or JsonValueKind.Array => SqlValue.FromNVarchar(element.GetRawText()), |
| 66 | + // Scalar match (String / Number / True / False / Null) — |
| 67 | + // JSON_QUERY returns NULL in lax mode (Msg 13624 in strict, |
| 68 | + // which EF / DACFx never depend on). |
| 69 | + _ => SqlValue.Null(SqlType.NVarchar), |
| 70 | + }; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public override SqlType GetSqlType(Func<MultiPartName, SqlType> resolveColumnType) => SqlType.NVarchar; |
| 75 | + |
| 76 | + internal override string DebugDisplay() => $"JSON_QUERY({this.jsonInput.DebugDisplay()}, {this.pathInput.DebugDisplay()})"; |
| 77 | +} |
0 commit comments