forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartupMessage.cs
More file actions
executable file
·73 lines (60 loc) · 2.25 KB
/
StartupMessage.cs
File metadata and controls
executable file
·73 lines (60 loc) · 2.25 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
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
namespace Npgsql.FrontendMessages
{
class StartupMessage : SimpleFrontendMessage
{
internal string Database { get; set; }
internal string Username { get; set; }
internal string ApplicationName { get; set; }
internal string SearchPath { get; set; }
Dictionary<byte[], byte[]> _parameters;
int _length;
const int ProtocolVersion3 = 3 << 16; // 196608
internal StartupMessage(string database, string username)
{
Database = database;
Username = username;
}
internal override void Prepare()
{
Contract.Requires(!String.IsNullOrWhiteSpace(Database));
Contract.Requires(!String.IsNullOrWhiteSpace(Username));
var parameters = new Dictionary<String, String> {
{ "database", Database },
{ "user", Username },
{ "client_encoding", "UTF8" },
};
if (ApplicationName != null)
{
parameters.Add("application_name", ApplicationName);
}
if (SearchPath != null)
{
parameters.Add("search_path", SearchPath);
}
// TODO: Is this really UTF8 or can be ASCII?
_parameters = parameters.ToDictionary(kv => PGUtil.UTF8Encoding.GetBytes(kv.Key),
kv => PGUtil.UTF8Encoding.GetBytes(kv.Value));
_length = 4 + // len
4 + // protocol version
_parameters.Select(kv => kv.Key.Length + kv.Value.Length + 2).Sum() +
1; // trailing zero byte
}
internal override int Length { get { return _length; } }
internal override void Write(NpgsqlBuffer buf)
{
buf.WriteInt32(_length);
buf.WriteInt32(ProtocolVersion3);
foreach (var kv in _parameters)
{
buf.WriteBytesNullTerminated(kv.Key);
buf.WriteBytesNullTerminated(kv.Value);
}
buf.WriteByte(0);
}
}
}