forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPGUtil.cs
More file actions
97 lines (86 loc) · 3.48 KB
/
PGUtil.cs
File metadata and controls
97 lines (86 loc) · 3.48 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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Npgsql.Util
{
static class Statics
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static T Expect<T>(IBackendMessage msg, NpgsqlConnector connector)
{
if (msg is T asT)
return asT;
connector.Break();
throw new NpgsqlException($"Received backend message {msg.Code} while expecting {typeof(T).Name}. Please file a bug.");
}
}
// ReSharper disable once InconsistentNaming
static class PGUtil
{
internal static readonly UTF8Encoding UTF8Encoding = new UTF8Encoding(false, true);
internal static readonly UTF8Encoding RelaxedUTF8Encoding = new UTF8Encoding(false, false);
internal const int BitsInInt = sizeof(int) * 8;
internal static void ValidateBackendMessageCode(BackendMessageCode code)
{
switch (code)
{
case BackendMessageCode.AuthenticationRequest:
case BackendMessageCode.BackendKeyData:
case BackendMessageCode.BindComplete:
case BackendMessageCode.CloseComplete:
case BackendMessageCode.CompletedResponse:
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:
throw new NpgsqlException("Unknown message code: " + code);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static int RotateShift(int val, int shift)
=> (val << shift) | (val >> (BitsInInt - shift));
internal static readonly Task<bool> TrueTask = Task.FromResult(true);
internal static readonly Task<bool> FalseTask = Task.FromResult(false);
internal static StringComparer InvariantCaseIgnoringStringComparer => StringComparer.InvariantCultureIgnoreCase;
internal static bool IsWindows =>
#if NET461
Environment.OSVersion.Platform == PlatformID.Win32NT;
#else
System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows);
#endif
}
enum FormatCode : short
{
Text = 0,
Binary = 1
}
static class EnumerableExtensions
{
internal static string Join(this IEnumerable<string> values, string separator)
{
return string.Join(separator, values);
}
}
}