-
Notifications
You must be signed in to change notification settings - Fork 874
Expand file tree
/
Copy pathStatics.cs
More file actions
100 lines (89 loc) · 3.73 KB
/
Statics.cs
File metadata and controls
100 lines (89 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using Npgsql.Internal;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Text;
namespace Npgsql.Util;
static class Statics
{
internal static readonly bool EnableAssertions;
#if DEBUG
internal static bool LegacyTimestampBehavior;
internal static bool DisableDateTimeInfinityConversions;
#else
internal static readonly bool LegacyTimestampBehavior;
internal static readonly bool DisableDateTimeInfinityConversions;
#endif
static Statics()
{
EnableAssertions = AppContext.TryGetSwitch("Npgsql.EnableAssertions", out var enabled) && enabled;
LegacyTimestampBehavior = AppContext.TryGetSwitch("Npgsql.EnableLegacyTimestampBehavior", out enabled) && enabled;
DisableDateTimeInfinityConversions = AppContext.TryGetSwitch("Npgsql.DisableDateTimeInfinityConversions", out enabled) && enabled;
}
/// Returns the escaped SQL representation of a string literal.
/// <param name="literal">The identifier to be escaped.</param>
internal static string EscapeLiteral(string literal)
{
// There is no support for escape sequences in quoted values for PostgreSQL, so replacing ' is enough.
// (to be able to use escaped characters an alternative syntax exists, it requires E to appear directly before the opening quote)
return literal.Replace("'", "''");
}
internal static T Expect<T>(IBackendMessage msg, NpgsqlConnector connector)
{
if (msg.GetType() != typeof(T))
ThrowIfMsgWrongType<T>(msg, connector);
return (T)msg;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static T ExpectAny<T>(IBackendMessage msg, NpgsqlConnector connector)
{
if (msg is T t)
return t;
ThrowIfMsgWrongType<T>(msg, connector);
return default;
}
[DoesNotReturn]
static void ThrowIfMsgWrongType<T>(IBackendMessage msg, NpgsqlConnector connector)
=> throw connector.Break(
new NpgsqlException($"Received backend message {msg.Code} while expecting {typeof(T).Name}. Please file a bug."));
[Conditional("DEBUG")]
internal static void ValidateBackendMessageCode(BackendMessageCode code)
{
switch (code)
{
case BackendMessageCode.AuthenticationRequest:
case BackendMessageCode.BackendKeyData:
case BackendMessageCode.BindComplete:
case BackendMessageCode.CloseComplete:
case BackendMessageCode.CommandComplete:
case BackendMessageCode.CopyData:
case BackendMessageCode.CopyDone:
case BackendMessageCode.CopyBothResponse:
case BackendMessageCode.CopyInResponse:
case BackendMessageCode.CopyOutResponse:
case BackendMessageCode.DataRow:
case BackendMessageCode.EmptyQueryResponse:
case BackendMessageCode.ErrorResponse:
case BackendMessageCode.FunctionCall:
case BackendMessageCode.FunctionCallResponse:
case BackendMessageCode.NoData:
case BackendMessageCode.NoticeResponse:
case BackendMessageCode.NotificationResponse:
case BackendMessageCode.ParameterDescription:
case BackendMessageCode.ParameterStatus:
case BackendMessageCode.ParseComplete:
case BackendMessageCode.PasswordPacket:
case BackendMessageCode.PortalSuspended:
case BackendMessageCode.ReadyForQuery:
case BackendMessageCode.RowDescription:
return;
default:
ThrowUnknownMessageCode(code);
return;
}
static void ThrowUnknownMessageCode(BackendMessageCode code)
=> ThrowHelper.ThrowNpgsqlException($"Unknown message code: {code}");
}
}