Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ef983f1

Browse filesBrowse files
Samuel CorderSamuel Corder
Samuel Corder
authored and
Samuel Corder
committed
Added lasterror querying to the Database class.
1 parent 6258104 commit ef983f1
Copy full SHA for ef983f1

File tree

Expand file treeCollapse file tree

4 files changed

+91
-29
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+91
-29
lines changed

‎.gitignore

Copy file name to clipboardExpand all lines: .gitignore
+3-21Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,10 @@
1414
*.orig
1515
*.cache
1616

17-
test-results/*.xml
18-
MongoDB.Net-Tests/bin/*
19-
MongoDB.Net-Tests/test-results/MongoDB.Driver.Tests.csproj.test-cache
20-
MongoDB.Linq.Tests/test-results/MongoDB.Linq.Tests.csproj.test-cache
17+
*/bin/*
18+
*/obj/*
19+
*/test-results/*
2120

22-
*.suo
2321
/_UpgradeReport_Files/*
24-
obj/*
25-
MongoDB.Linq/obj/*
26-
MongoDB.Linq.Tests/bin/*
27-
MongoDB.Linq.Tests/obj/*
28-
MongoDB.Linq.Tests/test-results/*
29-
30-
MongoDB.Driver.Benchmark/bin/*
31-
MongoDB.Driver.Benchmark/ProfilingSessions/*
32-
MongoDB.Driver.Benchmark/obj/*
33-
34-
35-
/MongoDB.GridFS/bin/*
36-
/MongoDB.GridFS/obj/*
37-
/MongoDB.GridFS.Tests/bin/*
38-
/MongoDB.GridFS.Tests/obj/*
39-
/MongoDB.GridFS.Tests/test-results/*
4022

4123
/redist/*.zip

‎MongoDB.Net-Tests/TestDatabase.cs

Copy file name to clipboardExpand all lines: MongoDB.Net-Tests/TestDatabase.cs
+46-3Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using NUnit.Framework;
44

@@ -83,6 +83,47 @@ public void TestEvalWithScopeAsFunctionParameters(){
8383
Assert.AreEqual(x + y, result["retval"]);
8484
}
8585

86+
[Test]
87+
public void TestGetLastErrorNoError(){
88+
db["noerror"].Insert(new Document(){{"a",1},{"b",2}});
89+
Document error = db.GetLastError();
90+
Assert.AreEqual(MongoDBNull.Value, error["err"]);
91+
}
92+
93+
[Test]
94+
public void TestGetLastError(){
95+
IMongoCollection errcol = db["errcol"];
96+
errcol.MetaData.CreateIndex(new Document(){{"x", IndexOrder.Ascending}}, true);
97+
Document dup = new Document(){{"x",1},{"y",2}};
98+
errcol.Insert(dup);
99+
Document error = db.GetLastError();
100+
Assert.AreEqual(MongoDBNull.Value, error["err"]);
101+
102+
errcol.Insert(dup);
103+
error = db.GetLastError();
104+
105+
Assert.IsFalse(MongoDBNull.Value == error["err"]);
106+
107+
}
108+
109+
[Test]
110+
public void TestGetPrevError(){
111+
IMongoCollection col = db["preverror"];
112+
col.MetaData.CreateIndex(new Document(){{"x", IndexOrder.Ascending}},true);
113+
List<Document> docs = new List<Document>();
114+
for(int x = 0; x < 10; x++){
115+
docs.Add(new Document(){{"x",x},{"y",2}});
116+
}
117+
docs.Add(new Document(){{"x",1},{"y",4}}); //the dupe
118+
db.ResetError();
119+
Assert.AreEqual(MongoDBNull.Value, db.GetLastError()["err"]);
120+
121+
col.Insert(docs);
122+
Document error = db.GetLastError();
123+
124+
Assert.IsFalse(MongoDBNull.Value == error["err"]);
125+
Console.WriteLine(error);
126+
}
86127

87128
[TestFixtureSetUp]
88129
public void Init(){
@@ -98,7 +139,9 @@ public void Dispose(){
98139

99140
protected void cleanDB(){
100141
mongo["tests"]["$cmd"].FindOne(new Document().Append("drop","refs"));
101-
102-
}
142+
mongo["tests"]["$cmd"].FindOne(new Document().Append("drop","noerror"));
143+
mongo["tests"]["$cmd"].FindOne(new Document().Append("drop","errcol"));
144+
mongo["tests"]["$cmd"].FindOne(new Document().Append("drop","preverror"));
145+
}
103146
}
104147
}

‎MongoDBDriver.sln

Copy file name to clipboardExpand all lines: MongoDBDriver.sln
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 10.00
33
# Visual Studio 2008
4+
# SharpDevelop 3.1.1.5327
45
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MongoDB.Driver", "MongoDBDriver\MongoDB.Driver.csproj", "{B125BBA6-BFFD-44FA-9254-9B1754CD8AF3}"
56
EndProject
67
Project("{9344bdbb-3e7f-41fc-a0dd-8665d75ee146}") = "Packages", "Packages.mdproj", "{502F3381-58AA-461B-B9D8-12578A588C61}"
@@ -57,6 +58,10 @@ Global
5758
{C8BC95AB-25C6-4133-BC9F-8B6BB782CA02}.Release|Any CPU.Build.0 = Release|Any CPU
5859
{DCBE47DD-59A6-4212-AA4A-142838088B69}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
5960
{DCBE47DD-59A6-4212-AA4A-142838088B69}.Release|Any CPU.ActiveCfg = Release|Any CPU
61+
{DCBE47DD-59A6-4212-AA4A-142838088B69}.Debug|Any CPU.Build.0 = Debug|Any CPU
62+
{DCBE47DD-59A6-4212-AA4A-142838088B69}.Release|Any CPU.Build.0 = Release|Any CPU
63+
{502F3381-58AA-461B-B9D8-12578A588C61}.Debug|Any CPU.Build.0 = Debug|Any CPU
64+
{502F3381-58AA-461B-B9D8-12578A588C61}.Release|Any CPU.Build.0 = Release|Any CPU
6065
EndGlobalSection
6166
GlobalSection(MonoDevelopProperties) = preSolution
6267
StartupItem = MongoDBDriver\MongoDB.Driver.csproj

‎MongoDBDriver/Database.cs

Copy file name to clipboardExpand all lines: MongoDBDriver/Database.cs
+37-5Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,46 @@ public IMongoCollection GetCollection(String name){
6262
return col;
6363
}
6464

65+
/// <summary>
66+
/// Gets the document that a reference is pointing to.
67+
/// </summary>
6568
public Document FollowReference(DBRef reference){
6669
if(reference == null) throw new ArgumentNullException("reference cannot be null");
6770
Document query = new Document().Append("_id", reference.Id);
6871
return this[reference.CollectionName].FindOne(query);
6972
}
70-
73+
74+
/// <summary>
75+
/// Most operations do not have a return code in order to save the client from having to wait for results.
76+
/// GetLastError can be called to retrieve the return code if clients want one.
77+
/// </summary>
78+
public Document GetLastError(){
79+
return SendCommand("getlasterror");
80+
}
81+
82+
/// <summary>
83+
/// Retrieves the last error and forces the database to fsync all files before returning.
84+
/// </summary>
85+
/// <remarks>Server version 1.3+</remarks>
86+
public Document GetLastErrorAndFSync(){
87+
return SendCommand(new Document(){{"getlasterror", 1.0},{"fsync", true}});
88+
}
89+
90+
/// <summary>
91+
/// Call after sending a bulk operation to the database.
92+
/// </summary>
93+
/// <returns>
94+
public Document GetPreviousError(){
95+
return SendCommand("getpreverror");
96+
}
97+
98+
/// <summary>
99+
/// Resets last error. This is good to call before a bulk operation.
100+
/// </summary>
101+
public void ResetError(){
102+
SendCommand("reseterror");
103+
}
104+
71105
public bool Authenticate(string username, string password){
72106
Document nonceResult = this.SendCommand("getnonce");
73107
String nonce = (String)nonceResult["nonce"];
@@ -107,8 +141,8 @@ public Document Eval(CodeWScope cw){
107141
return SendCommand(cmd);
108142
}
109143

110-
public Document SendCommand(string javascript){
111-
Document cmd = new Document().Append(javascript,1.0);
144+
public Document SendCommand(string command){
145+
Document cmd = new Document().Append(command,1.0);
112146
return this.SendCommand(cmd);
113147
}
114148

@@ -127,8 +161,6 @@ public Document SendCommand(Document cmd){
127161
return result;
128162
}
129163

130-
131-
132164
internal static string Hash(string text){
133165
MD5 md5 = MD5.Create();
134166
byte[] hash = md5.ComputeHash(Encoding.Default.GetBytes(text));

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.