This repository has been archived by the owner on Dec 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 228
/
DatabaseMetadata.cs
142 lines (129 loc) · 4.72 KB
/
DatabaseMetadata.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
using MongoDB.Configuration;
using MongoDB.Connections;
using MongoDB.Util;
namespace MongoDB
{
/// <summary>
/// Administration of metadata for a database.
/// </summary>
public class DatabaseMetadata
{
private readonly MongoConfiguration _configuration;
private readonly Connection _connection;
private readonly MongoDatabase _database;
private readonly string _name;
/// <summary>
/// Initializes a new instance of the <see cref = "DatabaseMetadata" /> class.
/// </summary>
/// <param name = "configuration">The configuration.</param>
/// <param name = "name">The name.</param>
/// <param name = "conn">The conn.</param>
public DatabaseMetadata(MongoConfiguration configuration, string name, Connection conn)
{
_configuration = configuration;
_connection = conn;
_name = name;
_database = new MongoDatabase(_configuration, conn, name);
}
/// <summary>
/// Creates the collection.
/// </summary>
/// <param name = "name">The name.</param>
/// <returns></returns>
public IMongoCollection CreateCollection(string name)
{
return CreateCollection(name, null);
}
/// <summary>
/// Creates the collection.
/// </summary>
/// <param name = "name">The name.</param>
/// <param name = "options">The options.</param>
/// <returns></returns>
public IMongoCollection CreateCollection(string name, Document options)
{
var cmd = new Document();
cmd.Add("create", name).Merge(options);
_database.SendCommand(cmd);
return new MongoCollection(_configuration, _connection, _name, name);
}
/// <summary>
/// Drops the collection.
/// </summary>
/// <param name = "collection">The col.</param>
/// <returns></returns>
public bool DropCollection(MongoCollection collection)
{
return DropCollection(collection.Name);
}
/// <summary>
/// Drops the collection.
/// </summary>
/// <param name = "name">The name.</param>
/// <returns></returns>
public bool DropCollection(string name)
{
var result = _database.SendCommand(new Document().Add("drop", name));
return result.Contains("ok") && ((double)result["ok"] == 1);
}
/// <summary>
/// Drops the database.
/// </summary>
/// <returns></returns>
public bool DropDatabase()
{
var result = _database.SendCommand("dropDatabase");
return result.Contains("ok") && ((double)result["ok"] == 1);
}
/// <summary>
/// Adds the user.
/// </summary>
/// <param name = "username">The username.</param>
/// <param name = "password">The password.</param>
public void AddUser(string username, string password)
{
var users = _database["system.users"];
var pwd = MongoHash.Generate(username + ":mongo:" + password);
var user = new Document().Add("user", username).Add("pwd", pwd);
if(FindUser(username) != null)
throw new MongoException("A user with the name " + username + " already exists in this database.", null);
users.Insert(user);
}
/// <summary>
/// Removes the user.
/// </summary>
/// <param name = "username">The username.</param>
public void RemoveUser(string username)
{
var users = _database["system.users"];
users.Remove(new Document().Add("user", username));
}
/// <summary>
/// Lists the users.
/// </summary>
/// <returns></returns>
public ICursor ListUsers()
{
var users = _database["system.users"];
return users.FindAll();
}
/// <summary>
/// Finds the user.
/// </summary>
/// <param name = "username">The username.</param>
/// <returns></returns>
public Document FindUser(string username)
{
return FindUser(new Document().Add("user", username));
}
/// <summary>
/// Finds the user.
/// </summary>
/// <param name = "spec">The spec.</param>
/// <returns></returns>
public Document FindUser(Document spec)
{
return _database["system.users"].FindOne(spec);
}
}
}