-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMongoConnectionString.cs
172 lines (134 loc) · 4.99 KB
/
MongoConnectionString.cs
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using CSMongo.Exceptions;
namespace CSMongo {
/// <summary>
/// Reads connection strings for MongoDatabases
/// </summary>
internal sealed class MongoConnectionString {
#region Constructors
/// <summary>
/// Creates a new empty connection string reader
/// </summary>
public MongoConnectionString() {
this.Port = Mongo.DefaultPort;
this.AutoConnect = true;
}
#endregion
#region Properties
/// <summary>
/// The server or IP address to connect to
/// </summary>
public string Host { get; private set; }
/// <summary>
/// The port number to connect on
/// </summary>
public int Port { get; private set; }
/// <summary>
/// The name of the database to open
/// </summary>
public string Database { get; private set; }
/// <summary>
/// The username to use when connecting
/// </summary>
public string Username { get; private set; }
/// <summary>
/// The password to use when connecting
/// </summary>
public string Password { get; private set; }
/// <summary>
/// Does this connection automatically open
/// </summary>
public bool AutoConnect { get; private set; }
#endregion
#region Static Creation
/// <summary>
/// Parses a string for Mongo connection data
/// </summary>
public static MongoConnectionString Parse(string connectionString) {
MongoConnectionString reader = new MongoConnectionString();
reader.ParseString(connectionString);
return reader;
}
#endregion
#region Public Methods
/// <summary>
/// Attempts to read the values for a connection string
/// </summary>
public void ParseString(string connectionString) {
try {
foreach (string pair in this._ExtractPairs(connectionString)) {
this._AssignValue(pair);
}
}
catch {
throw new InvalidMongoConnectionStringException();
}
}
#endregion
#region Private Methods
//handles reading the actual value of the string
private void _AssignValue(string item) {
//extract the values
Match pair = Regex.Match(item ?? string.Empty, "^(?<key>[^=]+)=(?<value>.*)$");
if (!pair.Success) { return; }
//otherwise, get the values
string key = pair.Groups["key"].Value.Trim();
if (string.IsNullOrEmpty(key)) { return; }
//get the value and undo escaping the ; values
string value = pair.Groups["value"].Value.Replace(@"\;", ";");
//depending on the value, set the property
if (key.Equals("username", StringComparison.OrdinalIgnoreCase)) {
this.Username = value.Trim();
}
else if (key.Equals("password", StringComparison.OrdinalIgnoreCase)) {
this.Password = value;
}
else if (key.Equals("database", StringComparison.OrdinalIgnoreCase)) {
this.Database = value.Trim();
}
else if (key.Equals("host", StringComparison.OrdinalIgnoreCase)) {
this.Host = value.Trim();
}
else if (key.Equals("autoconnect")) {
bool auto = true;
bool.TryParse(value, out auto);
this.AutoConnect = auto;
}
else if (key.Equals("port")) {
int port = 0;
int.TryParse(value, out port);
this.Port = port;
}
}
//extracts all of the pairs of values (but doesn't format them)
private IEnumerable<string> _ExtractPairs(string connectionString) {
//start looping each value in the string
bool escape = false;
string set = string.Empty;
foreach (char letter in connectionString) {
//check if this is the end of ths string
if (letter.Equals(';') && !escape) {
//return back the current value
yield return set;
//reset the values and continue parsing
set = string.Empty;
escape = false;
continue;
}
//reset escaping and check for it again
escape = letter.Equals('\\') && !escape;
//update the current string
set = string.Concat(set, letter);
}
//if there is remaining characters, return them now
if (set.Length > 0) {
yield return set;
}
}
#endregion
}
}