forked from samus/mongodb-csharp
/
Database.cs
116 lines (102 loc) · 3.72 KB
/
Database.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using MongoDB.Driver.Bson;
namespace MongoDB.Driver
{
public class Database
{
private Connection connection;
private Collection command;
private String name;
public string Name {
get { return name; }
}
private DatabaseMetaData metaData;
public DatabaseMetaData MetaData {
get {
if(metaData == null){
metaData = new DatabaseMetaData(this.Name,this.connection);
}
return metaData;
}
}
public Database(Connection conn, String name){
this.connection = conn;
this.name = name;
this.command = this["$cmd"];
}
public List<String> GetCollectionNames(){
Collection namespaces = this["system.namespaces"];
Cursor cursor = namespaces.Find(null);
List<String> names = new List<string>();
foreach (Document doc in cursor.Documents){
names.Add((String)doc["name"]); //Fix Me: Should filter built-ins
}
return names;
}
public Collection this[ String name ] {
get{
return this.GetCollection(name);
}
}
public Collection GetCollection(String name){
Collection col = new Collection(name, this.connection, this.Name);
return col;
}
public Document FollowReference(DBRef reference){
if(reference == null) throw new ArgumentNullException("reference cannot be null");
Document query = new Document().Append("_id", reference.Id);
return this[reference.CollectionName].FindOne(query);
}
public bool Authenticate(string username, string password){
Document nonceResult = this.SendCommand("getnonce");
String nonce = (String)nonceResult["nonce"];
if (nonce == null){
throw new MongoException("Error retrieving nonce", null);
}
else {
string pwd = Database.Hash(username + ":mongo:" + password);
Document auth = new Document();
auth.Add("authenticate", 1.0);
auth.Add("user", username);
auth.Add("nonce", nonce);
auth.Add("key", Database.Hash(nonce + username + pwd));
try{
this.SendCommand(auth);
return true;
}catch(MongoCommandException){
return false;
}
}
}
public void Logout(){
this.SendCommand("logout");
}
public Document SendCommand(string str){
Document cmd = new Document().Append(str,1.0);
return this.SendCommand(cmd);
}
public Document SendCommand(Document cmd){
Document result = this.command.FindOne(cmd);
double ok = (double)result["ok"];
if (ok != 1.0){
string msg;
if(result.Contains("msg")){
msg = (string)result["msg"];
}else{
msg = string.Empty;
}
throw new MongoCommandException(msg,result,cmd);
}
return result;
}
internal static string Hash(string text){
MD5 md5 = MD5.Create();
byte[] hash = md5.ComputeHash(Encoding.Default.GetBytes(text));
return BitConverter.ToString(hash).Replace("-","").ToLower();
}
}
}