Skip to content

Commit

Permalink
Add LENGTH support #986
Browse files Browse the repository at this point in the history
  • Loading branch information
mbdavid committed May 21, 2018
1 parent 5c596cb commit 49163e1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
2 changes: 1 addition & 1 deletion LiteDB.Shell/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"LiteDB.Shell": {
"commandName": "Project",
"commandLineArgs": "C:\\Temp\\app.db"
"commandLineArgs": "bin/Debug/net40/Game.db"
}
}
}
6 changes: 3 additions & 3 deletions LiteDB.Shell/Shell/ShellProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static void Start(InputCommand input, Display display)

if (string.IsNullOrEmpty(cmd)) continue;

try
//try
{
var s = new StringScanner(cmd);

Expand All @@ -51,9 +51,9 @@ public static void Start(InputCommand input, Display display)
display.WriteResult(env.Engine.Run(cmd));
}
}
catch (Exception ex)
//catch (Exception ex)
{
display.WriteError(ex);
//display.WriteError(ex);
}
}
}
Expand Down
45 changes: 45 additions & 0 deletions LiteDB.Tests/Engine/Delete_Query_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace LiteDB.Tests.Engine
{
[TestClass]
public class Delete_Query_Tests
{
[TestMethod]
public void Delete_Query()
{
using (var file = new TempFile())
{
var initial = new DateTime(2000, 01, 01);

using (var db = new LiteEngine(file.Filename))
{
for(var i = 0; i < 5000; i++)
{
db.Insert("col", new BsonDocument { { "dt", initial.AddDays(i) } });
}

db.EnsureIndex("col", "dt");

Assert.AreEqual(5000, db.Count("col"));

Assert.AreEqual(0, db.Count("col", Query.GT("dd", initial)));

var del = db.Delete("col", Query.GT("dd", initial));

Assert.AreEqual(0, del);

Assert.AreEqual(0, db.Count("col", Query.GT("dd", initial)));

}
}
}
}
}
14 changes: 14 additions & 0 deletions LiteDB/Document/Expression/Methods/Misc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,19 @@ public static IEnumerable<BsonValue> IIF(IEnumerable<BsonValue> condition, IEnum
yield return value.First.AsBoolean ? value.Second : value.Third;
}
}

/// <summary>
/// Return length of variant value (valid only for String, Binary, Array or Document [keys])
/// </summary>
public static IEnumerable<BsonValue> LENGTH(IEnumerable<BsonValue> values)
{
foreach (var value in values)
{
if (value.IsString) yield return value.AsString.Length;
else if (value.IsBinary) yield return value.AsBinary.Length;
else if (value.IsArray) yield return value.AsArray.Count;
else if (value.IsDocument) yield return value.AsDocument.Keys.Count;
}
}
}
}

0 comments on commit 49163e1

Please sign in to comment.