Browse Source

Implemented CSHARP-216. Added support for nolock option to MongoDatabase.Eval.

pull/59/merge
rstam 14 years ago
parent
commit
64aa1ba2ac
  1. 22
      Driver/Core/MongoDatabase.cs
  2. 28
      DriverOnlineTests/Core/MongoDatabaseTests.cs

22
Driver/Core/MongoDatabase.cs

@ -397,19 +397,35 @@ namespace MongoDB.Driver {
/// </summary>
/// <param name="code">The code to evaluate.</param>
/// <param name="args">Optional arguments (only used when the code is a function with parameters).</param>
/// <param name="nolock">Whether to run without taking a write lock.</param>
/// <returns>The result of evaluating the code.</returns>
public virtual BsonValue Eval(
string code,
params object[] args
BsonJavaScript code,
object[] args,
bool nolock
) {
var command = new CommandDocument {
{ "$eval", code },
{ "args", new BsonArray(args) }
{ "args", BsonArray.Create(args), args != null && args.Length > 0 },
{ "nolock", true, nolock }
};
var result = RunCommand(command);
return result.Response["retval"];
}
/// <summary>
/// Evaluates JavaScript code at the server.
/// </summary>
/// <param name="code">The code to evaluate.</param>
/// <param name="args">Optional arguments (only used when the code is a function with parameters).</param>
/// <returns>The result of evaluating the code.</returns>
public virtual BsonValue Eval(
BsonJavaScript code,
params object[] args
) {
return Eval(code, args, false); // nolock = false
}
/// <summary>
/// Fetches the document referred to by the DBRef.
/// </summary>

28
DriverOnlineTests/Core/MongoDatabaseTests.cs

@ -69,6 +69,34 @@ namespace MongoDB.DriverOnlineTests {
Assert.IsFalse(database.CollectionExists(collectionName));
}
[Test]
public void TestEvalNoArgs() {
var code = "function() { return 1; }";
var result = database.Eval(code);
Assert.AreEqual(1, result.ToInt32());
}
[Test]
public void TestEvalNoArgsNoLock() {
var code = "function() { return 1; }";
var result = database.Eval(code, null, true);
Assert.AreEqual(1, result.ToInt32());
}
[Test]
public void TestEvalWithArgs() {
var code = "function(x, y) { return x / y; }";
var result = database.Eval(code, 6, 2);
Assert.AreEqual(3, result.ToInt32());
}
[Test]
public void TestEvalWithArgsNoLock() {
var code = "function(x, y) { return x / y; }";
var result = database.Eval(code, new object[] { 6, 2 }, true);
Assert.AreEqual(3, result.ToInt32());
}
[Test]
public void TestFetchDBRef() {
var collectionName = "testdbref";

Loading…
Cancel
Save