Permalink
Browse files

Added lasterror querying to the Database class.

  • Loading branch information...
1 parent 6258104 commit ef983f1de9bfd1faa99df20c46374d26ccaac767 Samuel Corder committed Feb 23, 2010
Showing with 91 additions and 29 deletions.
  1. +3 −21 .gitignore
  2. +46 −3 MongoDB.Net-Tests/TestDatabase.cs
  3. +5 −0 MongoDBDriver.sln
  4. +37 −5 MongoDBDriver/Database.cs
View
@@ -14,28 +14,10 @@
*.orig
*.cache
-test-results/*.xml
-MongoDB.Net-Tests/bin/*
-MongoDB.Net-Tests/test-results/MongoDB.Driver.Tests.csproj.test-cache
-MongoDB.Linq.Tests/test-results/MongoDB.Linq.Tests.csproj.test-cache
+*/bin/*
+*/obj/*
+*/test-results/*
-*.suo
/_UpgradeReport_Files/*
-obj/*
-MongoDB.Linq/obj/*
-MongoDB.Linq.Tests/bin/*
-MongoDB.Linq.Tests/obj/*
-MongoDB.Linq.Tests/test-results/*
-
-MongoDB.Driver.Benchmark/bin/*
-MongoDB.Driver.Benchmark/ProfilingSessions/*
-MongoDB.Driver.Benchmark/obj/*
-
-
-/MongoDB.GridFS/bin/*
-/MongoDB.GridFS/obj/*
-/MongoDB.GridFS.Tests/bin/*
-/MongoDB.GridFS.Tests/obj/*
-/MongoDB.GridFS.Tests/test-results/*
/redist/*.zip
@@ -1,4 +1,4 @@
-using System;
+using System;
using System.Collections.Generic;
using NUnit.Framework;
@@ -83,6 +83,47 @@ public class TestDatabase
Assert.AreEqual(x + y, result["retval"]);
}
+ [Test]
+ public void TestGetLastErrorNoError(){
+ db["noerror"].Insert(new Document(){{"a",1},{"b",2}});
+ Document error = db.GetLastError();
+ Assert.AreEqual(MongoDBNull.Value, error["err"]);
+ }
+
+ [Test]
+ public void TestGetLastError(){
+ IMongoCollection errcol = db["errcol"];
+ errcol.MetaData.CreateIndex(new Document(){{"x", IndexOrder.Ascending}}, true);
+ Document dup = new Document(){{"x",1},{"y",2}};
+ errcol.Insert(dup);
+ Document error = db.GetLastError();
+ Assert.AreEqual(MongoDBNull.Value, error["err"]);
+
+ errcol.Insert(dup);
+ error = db.GetLastError();
+
+ Assert.IsFalse(MongoDBNull.Value == error["err"]);
+
+ }
+
+ [Test]
+ public void TestGetPrevError(){
+ IMongoCollection col = db["preverror"];
+ col.MetaData.CreateIndex(new Document(){{"x", IndexOrder.Ascending}},true);
+ List<Document> docs = new List<Document>();
+ for(int x = 0; x < 10; x++){
+ docs.Add(new Document(){{"x",x},{"y",2}});
+ }
+ docs.Add(new Document(){{"x",1},{"y",4}}); //the dupe
+ db.ResetError();
+ Assert.AreEqual(MongoDBNull.Value, db.GetLastError()["err"]);
+
+ col.Insert(docs);
+ Document error = db.GetLastError();
+
+ Assert.IsFalse(MongoDBNull.Value == error["err"]);
+ Console.WriteLine(error);
+ }
[TestFixtureSetUp]
public void Init(){
@@ -98,7 +139,9 @@ public class TestDatabase
protected void cleanDB(){
mongo["tests"]["$cmd"].FindOne(new Document().Append("drop","refs"));
-
- }
+ mongo["tests"]["$cmd"].FindOne(new Document().Append("drop","noerror"));
+ mongo["tests"]["$cmd"].FindOne(new Document().Append("drop","errcol"));
+ mongo["tests"]["$cmd"].FindOne(new Document().Append("drop","preverror"));
+ }
}
}
View
@@ -1,6 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
+# SharpDevelop 3.1.1.5327
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MongoDB.Driver", "MongoDBDriver\MongoDB.Driver.csproj", "{B125BBA6-BFFD-44FA-9254-9B1754CD8AF3}"
EndProject
Project("{9344bdbb-3e7f-41fc-a0dd-8665d75ee146}") = "Packages", "Packages.mdproj", "{502F3381-58AA-461B-B9D8-12578A588C61}"
@@ -57,6 +58,10 @@ Global
{C8BC95AB-25C6-4133-BC9F-8B6BB782CA02}.Release|Any CPU.Build.0 = Release|Any CPU
{DCBE47DD-59A6-4212-AA4A-142838088B69}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DCBE47DD-59A6-4212-AA4A-142838088B69}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {DCBE47DD-59A6-4212-AA4A-142838088B69}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {DCBE47DD-59A6-4212-AA4A-142838088B69}.Release|Any CPU.Build.0 = Release|Any CPU
+ {502F3381-58AA-461B-B9D8-12578A588C61}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {502F3381-58AA-461B-B9D8-12578A588C61}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = MongoDBDriver\MongoDB.Driver.csproj
View
@@ -62,12 +62,46 @@ public class Database
return col;
}
+ /// <summary>
+ /// Gets the document that a reference is pointing to.
+ /// </summary>
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);
}
-
+
+ /// <summary>
+ /// Most operations do not have a return code in order to save the client from having to wait for results.
+ /// GetLastError can be called to retrieve the return code if clients want one.
+ /// </summary>
+ public Document GetLastError(){
+ return SendCommand("getlasterror");
+ }
+
+ /// <summary>
+ /// Retrieves the last error and forces the database to fsync all files before returning.
+ /// </summary>
+ /// <remarks>Server version 1.3+</remarks>
+ public Document GetLastErrorAndFSync(){
+ return SendCommand(new Document(){{"getlasterror", 1.0},{"fsync", true}});
+ }
+
+ /// <summary>
+ /// Call after sending a bulk operation to the database.
+ /// </summary>
+ /// <returns>
+ public Document GetPreviousError(){
+ return SendCommand("getpreverror");
+ }
+
+ /// <summary>
+ /// Resets last error. This is good to call before a bulk operation.
+ /// </summary>
+ public void ResetError(){
+ SendCommand("reseterror");
+ }
+
public bool Authenticate(string username, string password){
Document nonceResult = this.SendCommand("getnonce");
String nonce = (String)nonceResult["nonce"];
@@ -107,8 +141,8 @@ public class Database
return SendCommand(cmd);
}
- public Document SendCommand(string javascript){
- Document cmd = new Document().Append(javascript,1.0);
+ public Document SendCommand(string command){
+ Document cmd = new Document().Append(command,1.0);
return this.SendCommand(cmd);
}
@@ -127,8 +161,6 @@ public class Database
return result;
}
-
-
internal static string Hash(string text){
MD5 md5 = MD5.Create();
byte[] hash = md5.ComputeHash(Encoding.Default.GetBytes(text));

0 comments on commit ef983f1

Please sign in to comment.