Skip to content

Commit

Permalink
Helper methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
bchavez committed Feb 20, 2016
1 parent 07a0b58 commit 66b836e
Show file tree
Hide file tree
Showing 11 changed files with 538 additions and 122 deletions.
3 changes: 3 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v2.2.9-beta-1
* Added helper overloads for GetAll, HasFields, WithFields, Pluck, Without, IndexStatus, IndexWait.

## v2.2.8
* Roll-up Release for Full .NET Framework since v2.2.7.
* CoreCLR users please continue using latest beta release until CoreCLR is RTM.
Expand Down
63 changes: 37 additions & 26 deletions Source/RethinkDb.Driver.Tests/ReQL/Examples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,29 @@ public class Foo
[TestFixture]
public class Examples : QueryTestFixture
{
private void InsertSomePocos()
{
var arr = new[]
{
new Foo {id = "a", Baz = 1, Bar = 1},
new Foo {id = "b", Baz = 2, Bar = 2},
new Foo {id = "c", Baz = 3, Bar = 3}
};
Result result = R.Db(DbName).Table(TableName).Insert(arr).Run<Result>(conn);
result.AssertInserted(3);
}

public override void BeforeEachTest()
{
base.BeforeEachTest();
ClearTable();
}

private void ClearTable()
{
ClearTable(DbName, TableName);
}

[Test]
public void test_booleans()
{
Expand All @@ -43,49 +66,39 @@ public void insert_test_without_id()
[Test]
public void insert_an_array_of_pocos()
{
var arr = new[]
{
new Foo {id = "a", Baz = 1, Bar = 1},
new Foo {id = "b", Baz = 2, Bar = 2},
new Foo {id = "c", Baz = 3, Bar = 3}
};
Result result = R.Db(DbName).Table(TableName).Insert(arr).Run<Result>(conn);
result.Dump();
InsertSomePocos();
}

[Test]
public void get_test()
{
InsertSomePocos();
Foo foo = R.Db(DbName).Table(TableName).Get("a").Run<Foo>(conn);
foo.Dump();
foo.Bar.Should().Be(1);
foo.Baz.Should().Be(1);
}

[Test]
public void get_with_time()
{
Foo foo = R.Db(DbName).Table(TableName).Get("4d4ba69e-048c-43b7-b842-c7b49dc6691c")
.Run<Foo>(conn);

foo.Dump();
}

[Test]
public void getall_test()
public void dynamic_getall_test()
{
InsertSomePocos();
Cursor<Foo> all = R.Db(DbName).Table(TableName).GetAll("a", "b", "c").Run<Foo>(conn);

all.BufferedItems.Dump();

foreach (var foo in all)
var items = all.ToList();
items.Count.Should().Be(3);

foreach (var foo in items)
{
Console.WriteLine($"Printing: {foo.id}!");
foo.Dump();
}
}

[Test]
public void use_a_cursor_to_get_items()
public void runcursor_use_a_cursor_to_get_items()
{
InsertSomePocos();
Cursor<Foo> all = R.Db(DbName).Table(TableName).GetAll("a", "b", "c").RunCursor<Foo>(conn);

var items = new List<Foo>();
Expand All @@ -99,8 +112,9 @@ public void use_a_cursor_to_get_items()
}

[Test]
public void getall_with_linq()
public void runcursor_getall_and_linq()
{
InsertSomePocos();
Cursor<Foo> all = R.Db(DbName).Table(TableName).GetAll("a", "b", "c").RunCursor<Foo>(conn);

var bazInOrder = all.OrderByDescending(f => f.Baz)
Expand All @@ -121,9 +135,6 @@ public void getall_using_an_index_with_optarg_indexer()
{
const string IndexName = "Idx";

DropTable(DbName, TableName);
CreateTable(DbName, TableName);

R.Db(DbName)
.Table(TableName)
.IndexCreate(IndexName).Run(conn);
Expand Down
44 changes: 44 additions & 0 deletions Source/RethinkDb.Driver.Tests/ReQL/GitterIssues.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Linq;
using FluentAssertions;
using NUnit.Framework;
using RethinkDb.Driver.Ast;

namespace RethinkDb.Driver.Tests.ReQL
{
public class GitterQuestions : QueryTestFixture
{

public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
[Test]
public void should_be_able_to_use_getall_with_listofguid()
{
ClearTable(DbName, TableName);
var items = new[]
{
new Person {FirstName = "Brian", LastName = "Chavez"},
new Person {FirstName = "Susie", LastName = "Baker"},
new Person {FirstName = "Donald", LastName = "Trump"}
};


var inserts = R.Db(DbName).Table(TableName)
.Insert(items)
.RunResult(conn);

var guids = inserts.GeneratedKeys.ToList();

var people = R.Db(DbName).Table(TableName)
.GetAll(guids)
.RunCursor<Person>(conn)
.ToList();

people.Should().HaveCount(3);

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
<Compile Include="ReQL\Examples.cs" />
<Compile Include="ReQL\ExperimentalTests.cs" />
<Compile Include="ReQL\GitHubIssues.cs" />
<Compile Include="ReQL\GitterIssues.cs" />
<Compile Include="ReQL\GroupingTests.cs" />
<Compile Include="ReQL\MapTests.cs" />
<Compile Include="ReQL\OtherTests.cs" />
Expand Down
19 changes: 16 additions & 3 deletions Source/RethinkDb.Driver/Ast/ExtensionHelper.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;

namespace RethinkDb.Driver.Ast
{
internal static class ExtensionHelper
/// <summary>
/// Extension helpers for working with the AST.
/// </summary>
public static class ExtensionHelper
{
public static JObject ToJObject(this object anonType)
internal static JObject ToJObject(this object anonType)
{
return anonType == null ? null : JObject.FromObject(anonType);
}

public static IDictionary<string, object> ToDict(this object anonType)
internal static IDictionary<string, object> ToDict(this object anonType)
{
return anonType == null ? null : PropertyHelper.ObjectToDictionary(anonType);
}
/// <summary>
/// Uses a collection as parameters for a method call.
/// </summary>
/// <param name="args">Same as calling params object[] overload. Instead of specifying each param, ICollection can be used for convenience.</param>
public static object[] AsParams<T>(this ICollection<T> args)
{
return args.OfType<object>().ToArray();
}
}
}
56 changes: 56 additions & 0 deletions Source/RethinkDb.Driver/Generated/Ast/ReqlExpr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1750,6 +1750,19 @@ public HasFields HasFields ( params object[] exprs )
arguments.CoerceAndAddAll(exprs);
return new HasFields (arguments );
}
/// <summary>
/// <para>Test if an object has one or more fields. An object has a field if it has that key and the key has a non-null value. For instance, the object <code>{'a': 1,'b': 2,'c': null}</code> has the fields <code>a</code> and <code>b</code>.</para>
/// </summary>
/// <example><para>Example: Return the players who have won games.</para>
/// <code>r.table('players').hasFields('games_won').run(conn, callback)
/// </code></example>
/// <param name="args">Same as calling params object[] overload, except the collection is applied as object[] params.</param>
public HasFields HasFields ( ICollection<string> args )
{
var arguments = new Arguments(this);
arguments.CoerceAndAddAll(args);
return new HasFields (arguments);
}
internal HasFields hasFields ( params object[] exprs )
{
return HasFields ( exprs );
Expand All @@ -1766,6 +1779,19 @@ public WithFields WithFields ( params object[] exprs )
arguments.CoerceAndAddAll(exprs);
return new WithFields (arguments );
}
/// <summary>
/// <para>Plucks one or more attributes from a sequence of objects, filtering out any objects in the sequence that do not have the specified fields. Functionally, this is identical to <code>hasFields</code> followed by <code>pluck</code> on a sequence.</para>
/// </summary>
/// <example><para>Example: Get a list of users and their posts, excluding any users who have not made any posts.</para>
/// <code>r.table('users').withFields('id', 'username', 'posts').run(conn, callback)
/// </code></example>
/// <param name="args">Same as calling params object[] overload, except the collection is applied as object[] params.</param>
public WithFields WithFields ( ICollection<string> args )
{
var arguments = new Arguments(this);
arguments.CoerceAndAddAll(args);
return new WithFields (arguments);
}
internal WithFields withFields ( params object[] exprs )
{
return WithFields ( exprs );
Expand All @@ -1784,6 +1810,21 @@ public Pluck Pluck ( params object[] exprs )
arguments.CoerceAndAddAll(exprs);
return new Pluck (arguments );
}
/// <summary>
/// <para>Plucks out one or more attributes from either an object or a sequence of objects
/// (projection).</para>
/// </summary>
/// <example><para>Example: We just need information about IronMan's reactor and not the rest of the
/// document.</para>
/// <code>r.table('marvel').get('IronMan').pluck('reactorState', 'reactorPower').run(conn, callback)
/// </code></example>
/// <param name="args">Same as calling params object[] overload, except the collection is applied as object[] params.</param>
public Pluck Pluck ( ICollection<string> args )
{
var arguments = new Arguments(this);
arguments.CoerceAndAddAll(args);
return new Pluck (arguments);
}
internal Pluck pluck ( params object[] exprs )
{
return Pluck ( exprs );
Expand All @@ -1802,6 +1843,21 @@ public Without Without ( params object[] exprs )
arguments.CoerceAndAddAll(exprs);
return new Without (arguments );
}
/// <summary>
/// <para>The opposite of pluck; takes an object or a sequence of objects, and returns them with
/// the specified paths removed.</para>
/// </summary>
/// <example><para>Example: Since we don't need it for this computation we'll save bandwidth and leave
/// out the list of IronMan's romantic conquests.</para>
/// <code>r.table('marvel').get('IronMan').without('personalVictoriesList').run(conn, callback)
/// </code></example>
/// <param name="args">Same as calling params object[] overload, except the collection is applied as object[] params.</param>
public Without Without ( ICollection<string> args )
{
var arguments = new Arguments(this);
arguments.CoerceAndAddAll(args);
return new Without (arguments);
}
internal Without without ( params object[] exprs )
{
return Without ( exprs );
Expand Down
60 changes: 60 additions & 0 deletions Source/RethinkDb.Driver/Generated/Ast/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,32 @@ public GetAll GetAll ( params object[] exprs )
arguments.CoerceAndAddAll(exprs);
return new GetAll (arguments );
}
/// <summary>
/// <para>Get all documents where the given value matches the value of the requested index.</para>
/// </summary>
/// <example><para>Example: Secondary index keys are not guaranteed to be unique so we cannot query via <a href="/api/javascript/get/">get</a> when using a secondary index.</para>
/// <code>r.table('marvel').getAll('man_of_steel', {index:'code_name'}).run(conn, callback)
/// </code></example>
/// <param name="args">Same as calling params object[] overload, except the collection is applied as object[] params.</param>
public GetAll GetAll ( ICollection<string> args )
{
var arguments = new Arguments(this);
arguments.CoerceAndAddAll(args);
return new GetAll (arguments);
}
/// <summary>
/// <para>Get all documents where the given value matches the value of the requested index.</para>
/// </summary>
/// <example><para>Example: Secondary index keys are not guaranteed to be unique so we cannot query via <a href="/api/javascript/get/">get</a> when using a secondary index.</para>
/// <code>r.table('marvel').getAll('man_of_steel', {index:'code_name'}).run(conn, callback)
/// </code></example>
/// <param name="args">Same as calling params object[] overload, except the collection is applied as object[] params.</param>
public GetAll GetAll ( ICollection<Guid> args )
{
var arguments = new Arguments(this);
arguments.CoerceAndAddAll(args);
return new GetAll (arguments);
}
internal GetAll getAll ( params object[] exprs )
{
return GetAll ( exprs );
Expand Down Expand Up @@ -407,6 +433,23 @@ public IndexStatus IndexStatus ( params object[] exprs )
arguments.CoerceAndAddAll(exprs);
return new IndexStatus (arguments );
}
/// <summary>
/// <para>Get the status of the specified indexes on this table, or the status
/// of all indexes on this table if no indexes are specified.</para>
/// </summary>
/// <example><para>Example: Get the status of all the indexes on <code>test</code>:</para>
/// <code>r.table('test').indexStatus().run(conn, callback)
/// </code>
/// <para>Example: Get the status of the <code>timestamp</code> index:</para>
/// <code>r.table('test').indexStatus('timestamp').run(conn, callback)
/// </code></example>
/// <param name="args">Same as calling params object[] overload, except the collection is applied as object[] params.</param>
public IndexStatus IndexStatus ( ICollection<string> args )
{
var arguments = new Arguments(this);
arguments.CoerceAndAddAll(args);
return new IndexStatus (arguments);
}
internal IndexStatus indexStatus ( params object[] exprs )
{
return IndexStatus ( exprs );
Expand All @@ -427,6 +470,23 @@ public IndexWait IndexWait ( params object[] exprs )
arguments.CoerceAndAddAll(exprs);
return new IndexWait (arguments );
}
/// <summary>
/// <para>Wait for the specified indexes on this table to be ready, or for all
/// indexes on this table to be ready if no indexes are specified.</para>
/// </summary>
/// <example><para>Example: Wait for all indexes on the table <code>test</code> to be ready:</para>
/// <code>r.table('test').indexWait().run(conn, callback)
/// </code>
/// <para>Example: Wait for the index <code>timestamp</code> to be ready:</para>
/// <code>r.table('test').indexWait('timestamp').run(conn, callback)
/// </code></example>
/// <param name="args">Same as calling params object[] overload, except the collection is applied as object[] params.</param>
public IndexWait IndexWait ( ICollection<string> args )
{
var arguments = new Arguments(this);
arguments.CoerceAndAddAll(args);
return new IndexWait (arguments);
}
internal IndexWait indexWait ( params object[] exprs )
{
return IndexWait ( exprs );
Expand Down
Loading

0 comments on commit 66b836e

Please sign in to comment.