Browse Source

First version of MongoConnectionStringBuilder.

pull/1/head
rstam 15 years ago
parent
commit
05af608b6a
  1. 120
      MongoDBClient/MongoConnectionStringBuilder.cs
  2. 42
      MongoDBClient/MongoServerAddress.cs

120
MongoDBClient/MongoConnectionStringBuilder.cs

@ -0,0 +1,120 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace MongoDB.MongoDBClient {
public class MongoConnectionStringBuilder {
#region private fields
private List<MongoServerAddress> servers = new List<MongoServerAddress>();
private string database;
private string username;
private string password;
#endregion
#region constructors
public MongoConnectionStringBuilder() {
}
public MongoConnectionStringBuilder(
string connectionString
) {
ConnectionString = connectionString;
}
#endregion
#region public properties
public string ConnectionString {
get { return ToString(); }
set { Parse(value); }
}
public List<MongoServerAddress> Servers {
get { return servers; }
}
public string Database {
get { return database; }
set { database = value; }
}
public string Username {
get { return username; }
set { username = value; }
}
public string Password {
get { return password; }
set { password = value; }
}
#endregion
#region public methods
public void Parse(
string connectionString
) {
const string pattern =
@"^mongodb://" +
@"((?<username>.+?):(?<password>.+?)@)?" +
@"(?<addresses>.+?(:\d+)?(,.+?(:\d+)?)*)" +
@"(/(?<database>.+))?";
Match match = Regex.Match(connectionString, pattern);
if (match.Success) {
string username = match.Groups["username"].Value;
string password = match.Groups["password"].Value;
string addresses = match.Groups["addresses"].Value;
string database = match.Groups["database"].Value;
List<MongoServerAddress> servers = new List<MongoServerAddress>();
foreach (string address in addresses.Split(',')) {
match = Regex.Match(address, @"^(?<host>.+?)(:(?<port>\d+))?$");
if (match.Success) {
string host = match.Groups["host"].Value;
string port = match.Groups["port"].Value;
MongoServerAddress server = new MongoServerAddress(
host,
port == "" ? 27017 : int.Parse(port)
);
servers.Add(server);
} else {
throw new ArgumentException("Invalid connection string");
}
}
this.servers = servers;
this.database = database != "" ? database : null;
this.username = username != "" ? username : null;
this.password = password != "" ? password : null;
} else {
throw new ArgumentException("Invalid connection string");
}
}
public override string ToString() {
StringBuilder sb = new StringBuilder();
sb.Append("mongodb://");
if (username != null && password != null) {
sb.Append(username);
sb.Append(":");
sb.Append(password);
sb.Append("@");
}
bool first = true;
foreach (MongoServerAddress server in servers) {
if (!first) { sb.Append(","); }
sb.Append(server.Host);
if (server.Port != 27017) {
sb.Append(":");
sb.Append(server.Port);
}
first = false;
}
if (database != null) {
sb.Append("/");
sb.Append(database);
}
return sb.ToString();
}
#endregion
}
}

42
MongoDBClient/MongoServerAddress.cs

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MongoDB.MongoDBClient {
public class MongoServerAddress {
#region private fields
private string host;
private int port;
#endregion
#region constructors
public MongoServerAddress(
string host
) {
this.host = host;
this.port = 27017;
}
public MongoServerAddress(
string host,
int port
) {
this.host = host;
this.port = port;
}
#endregion
#region public properties
public string Host {
get { return host; }
}
public int Port {
get { return port; }
}
#endregion
// TODO: implement GetHashcode, Equals, etc...
}
}
Loading…
Cancel
Save