|
| 1 | +using SqlServerSimulator.Storage; |
| 2 | + |
| 3 | +namespace SqlServerSimulator.Parser.Expressions; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// SQL <c>SESSION_CONTEXT(N'key')</c>: reads a value previously stored by |
| 7 | +/// <c>sp_set_session_context</c> on this session. Real SQL Server returns |
| 8 | +/// <c>sql_variant</c> (type-preserving); the simulator has no |
| 9 | +/// <c>sql_variant</c>, so the stored value surfaces as |
| 10 | +/// <see cref="SqlType.NVarchar"/> — the same proxy <see cref="ServerProperty"/> |
| 11 | +/// uses. A missing key returns NULL; a NULL key raises Msg 8116 |
| 12 | +/// (probe-confirmed). Keys are case-sensitive — see |
| 13 | +/// <see cref="SimulatedDbConnection.SessionContext"/>. |
| 14 | +/// </summary> |
| 15 | +internal sealed class SessionContext : Expression |
| 16 | +{ |
| 17 | + private readonly Expression keyArg; |
| 18 | + |
| 19 | + public SessionContext(ParserContext context) |
| 20 | + { |
| 21 | + this.keyArg = Parse(context); |
| 22 | + if (context.Token is not Tokens.Operator { Character: ')' }) |
| 23 | + throw SimulatedSqlException.SyntaxErrorNear(context); |
| 24 | + } |
| 25 | + |
| 26 | + public override SqlValue Run(RuntimeContext runtime) |
| 27 | + { |
| 28 | + var k = this.keyArg.Run(runtime); |
| 29 | + if (k.IsNull) |
| 30 | + throw SimulatedSqlException.InvalidArgumentDataType("NULL", argumentIndex: 1, "session_context"); |
| 31 | + var key = k.CoerceTo(SqlType.NVarchar).AsString; |
| 32 | + return runtime.Batch.Connection.SessionContext.TryGetValue(key, out var entry) |
| 33 | + ? entry.Value.CoerceTo(SqlType.NVarchar) |
| 34 | + : SqlValue.Null(SqlType.NVarchar); |
| 35 | + } |
| 36 | + |
| 37 | + public override SqlType GetSqlType(BatchContext batch, Func<MultiPartName, SqlType> resolveColumnType) => SqlType.NVarchar; |
| 38 | + |
| 39 | + internal override string DebugDisplay() => $"SESSION_CONTEXT({this.keyArg.DebugDisplay()})"; |
| 40 | +} |
| 41 | + |
| 42 | +/// <summary> |
| 43 | +/// SQL <c>CONTEXT_INFO()</c>: returns the session's 128-byte context-info |
| 44 | +/// buffer, or NULL when <c>SET CONTEXT_INFO</c> hasn't run. The buffer is |
| 45 | +/// always exactly 128 bytes once set (SQL Server right-pads / truncates), |
| 46 | +/// so <c>DATALENGTH(CONTEXT_INFO())</c> is 128. Result type is |
| 47 | +/// <see cref="SqlType.Varbinary"/>. |
| 48 | +/// </summary> |
| 49 | +internal sealed class ContextInfoFunction : Expression |
| 50 | +{ |
| 51 | + public ContextInfoFunction(ParserContext context) |
| 52 | + { |
| 53 | + if (context.Token is not Tokens.Operator { Character: ')' }) |
| 54 | + throw SimulatedSqlException.FunctionRequiresNArguments("context_info", 0); |
| 55 | + } |
| 56 | + |
| 57 | + public override SqlValue Run(RuntimeContext runtime) => |
| 58 | + runtime.Batch.Connection.ContextInfo is { } bytes |
| 59 | + ? SqlValue.FromVarbinary(bytes) |
| 60 | + : SqlValue.Null(SqlType.Varbinary); |
| 61 | + |
| 62 | + public override SqlType GetSqlType(BatchContext batch, Func<MultiPartName, SqlType> resolveColumnType) => SqlType.Varbinary; |
| 63 | + |
| 64 | + internal override string DebugDisplay() => "CONTEXT_INFO()"; |
| 65 | +} |
| 66 | + |
| 67 | +/// <summary> |
| 68 | +/// SQL <c>CONNECTIONPROPERTY('property')</c>: returns connection-level |
| 69 | +/// attributes. Real SQL Server projects <c>sql_variant</c>; the simulator |
| 70 | +/// surfaces values as <see cref="SqlType.NVarchar"/> (same proxy as |
| 71 | +/// <see cref="ServerProperty"/>). The in-process connection has no real |
| 72 | +/// network identity, so transport-shaped properties report fixed |
| 73 | +/// placeholder constants (probe-confirmed <c>net_transport = 'TCP'</c>, |
| 74 | +/// <c>protocol_type = 'TSQL'</c>); unknown property → NULL. |
| 75 | +/// </summary> |
| 76 | +internal sealed class ConnectionProperty : Expression |
| 77 | +{ |
| 78 | + private static readonly Dictionary<string, string?> Properties = new(StringComparer.OrdinalIgnoreCase) |
| 79 | + { |
| 80 | + ["net_transport"] = "TCP", |
| 81 | + ["protocol_type"] = "TSQL", |
| 82 | + ["auth_scheme"] = "SQL", |
| 83 | + ["physical_net_transport"] = "TCP", |
| 84 | + ["local_net_address"] = null, |
| 85 | + ["local_tcp_port"] = null, |
| 86 | + ["client_net_address"] = null, |
| 87 | + ["sni_consumer_node"] = null, |
| 88 | + }; |
| 89 | + |
| 90 | + private readonly Expression nameArg; |
| 91 | + |
| 92 | + public ConnectionProperty(ParserContext context) |
| 93 | + { |
| 94 | + this.nameArg = Parse(context); |
| 95 | + if (context.Token is not Tokens.Operator { Character: ')' }) |
| 96 | + throw SimulatedSqlException.SyntaxErrorNear(context); |
| 97 | + } |
| 98 | + |
| 99 | + public override SqlValue Run(RuntimeContext runtime) |
| 100 | + { |
| 101 | + var n = this.nameArg.Run(runtime); |
| 102 | + if (n.IsNull) |
| 103 | + return SqlValue.Null(SqlType.NVarchar); |
| 104 | + var name = n.CoerceTo(SqlType.NVarchar).AsString; |
| 105 | + return Properties.TryGetValue(name, out var value) && value is not null |
| 106 | + ? SqlValue.FromNVarchar(value) |
| 107 | + : SqlValue.Null(SqlType.NVarchar); |
| 108 | + } |
| 109 | + |
| 110 | + public override SqlType GetSqlType(BatchContext batch, Func<MultiPartName, SqlType> resolveColumnType) => SqlType.NVarchar; |
| 111 | + |
| 112 | + internal override string DebugDisplay() => $"CONNECTIONPROPERTY({this.nameArg.DebugDisplay()})"; |
| 113 | +} |
| 114 | + |
| 115 | +/// <summary> |
| 116 | +/// SQL <c>CURRENT_TRANSACTION_ID()</c>: returns a <c>bigint</c> transaction |
| 117 | +/// identifier. Apps use it for logging / correlation, not correctness; the |
| 118 | +/// simulator approximates it with the database's monotonic commit counter |
| 119 | +/// (<see cref="Database.CurrentTransactionCommitId"/>) — a plausible, |
| 120 | +/// increasing value rather than a stable per-transaction id. |
| 121 | +/// </summary> |
| 122 | +internal sealed class CurrentTransactionId : Expression |
| 123 | +{ |
| 124 | + public CurrentTransactionId(ParserContext context) |
| 125 | + { |
| 126 | + if (context.Token is not Tokens.Operator { Character: ')' }) |
| 127 | + throw SimulatedSqlException.FunctionRequiresNArguments("current_transaction_id", 0); |
| 128 | + } |
| 129 | + |
| 130 | + public override SqlValue Run(RuntimeContext runtime) => |
| 131 | + SqlValue.FromInt64(runtime.Batch.CurrentDatabase.CurrentTransactionCommitId); |
| 132 | + |
| 133 | + public override SqlType GetSqlType(BatchContext batch, Func<MultiPartName, SqlType> resolveColumnType) => SqlType.BigInt; |
| 134 | + |
| 135 | + internal override string DebugDisplay() => "CURRENT_TRANSACTION_ID()"; |
| 136 | +} |
| 137 | + |
| 138 | +/// <summary> |
| 139 | +/// SQL <c>CURRENT_REQUEST_ID()</c>: returns the <c>int</c> request id within |
| 140 | +/// the session. The simulator doesn't multiplex requests per session, so it |
| 141 | +/// reports <c>0</c> (probe-confirmed value for a single-request session). |
| 142 | +/// </summary> |
| 143 | +internal sealed class CurrentRequestId : Expression |
| 144 | +{ |
| 145 | + public CurrentRequestId(ParserContext context) |
| 146 | + { |
| 147 | + if (context.Token is not Tokens.Operator { Character: ')' }) |
| 148 | + throw SimulatedSqlException.FunctionRequiresNArguments("current_request_id", 0); |
| 149 | + } |
| 150 | + |
| 151 | + public override SqlValue Run(RuntimeContext runtime) => SqlValue.FromInt32(0); |
| 152 | + |
| 153 | + public override SqlType GetSqlType(BatchContext batch, Func<MultiPartName, SqlType> resolveColumnType) => SqlType.Int32; |
| 154 | + |
| 155 | + internal override string DebugDisplay() => "CURRENT_REQUEST_ID()"; |
| 156 | +} |
0 commit comments