Skip to content

Commit

Permalink
Added parameters test and IMeta implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
borrrden committed Jul 8, 2017
1 parent d13c71e commit 23f1d6d
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 24 deletions.
62 changes: 62 additions & 0 deletions src/Couchbase.Lite.Tests.Shared/QueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,68 @@ public void TestGroupBy()
}
}

[Fact]
public void TestParameters()
{
LoadNumbers(10);

var NUMBER1 = Expression.Property("number1");
var PARAM_N1 = Expression.Parameter("num1");
var PARAM_N2 = Expression.Parameter("num2");

using (var q = Query.Select(SelectResult.Expression(NUMBER1))
.From(DataSource.Database(Db))
.Where(NUMBER1.Between(PARAM_N1, PARAM_N2))
.OrderBy(Ordering.Expression(NUMBER1))) {
q.Parameters.Set("num1", 2);
q.Parameters.Set("num2", 5);

var expectedNumbers = new[] {2, 3, 4, 5};
var numRows = VerifyQuery(q, (n, row) =>
{
var number = row.GetInt(0);
number.Should().Be(expectedNumbers[n - 1]);
});

numRows.Should().Be(4);
}
}

[Fact]
public void TestMeta()
{
LoadNumbers(5);

var DOC_ID = Expression.Meta().DocumentID;
var DOC_SEQ = Expression.Meta().Sequence;
var NUMBER1 = Expression.Property("number1");

var RES_DOC_ID = SelectResult.Expression(DOC_ID);
var RES_DOC_SEQ = SelectResult.Expression(DOC_SEQ);
var RES_NUMBER1 = SelectResult.Expression(NUMBER1);

using (var q = Query.Select(RES_DOC_ID, RES_DOC_SEQ, RES_NUMBER1)
.From(DataSource.Database(Db))
.OrderBy(Ordering.Expression(DOC_SEQ))) {
var expectedDocIDs = new[] {"doc1", "doc2", "doc3", "doc4", "doc5"};
var expectedSeqs = new[] {1, 2, 3, 4, 5};
var expectedNumbers = expectedSeqs;

var numRows = VerifyQuery(q, (n, row) =>
{
var docID = row.GetString(0);
var seq = row.GetInt(1);
var number = row.GetInt(2);

docID.Should().Be(expectedDocIDs[n - 1]);
seq.Should().Be(expectedSeqs[n - 1]);
number.Should().Be(expectedNumbers[n - 1]);
});

numRows.Should().Be(5);
}
}

private bool TestWhereCompareValidator(IDictionary<string, object> properties, object context)
{
var ctx = (Func<int, bool>)context;
Expand Down
12 changes: 9 additions & 3 deletions src/Couchbase.Lite/API/Query/Expression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ internal static IExpression Group(params IExpression[] expressions)
throw new NotImplementedException();
}

internal static IMeta Meta()
/// <summary>
/// Creates an object that can generate expressions for retrieving metadata about
/// a result
/// </summary>
/// <returns>An object that can generate expressions for retrieving metadata about
/// a result</returns>
public static IMeta Meta()
{
throw new NotImplementedException();
return new QueryMeta();
}

/// <summary>
Expand Down Expand Up @@ -93,7 +99,7 @@ public static IExpression Parameter(string name)
/// </summary>
/// <param name="property">The name of the property to fetch</param>
/// <returns>An expression representing the value of a named property</returns>
public static IPropertySource Property(string property)
public static IPropertyExpression Property(string property)
{
return new QueryTypeExpression(property, ExpressionType.KeyPath);
}
Expand Down
26 changes: 14 additions & 12 deletions src/Couchbase.Lite/API/Query/IMeta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,24 @@

namespace Couchbase.Lite.Query
{
internal interface IMeta
/// <summary>
/// An interface representing an object that can generate expressions
/// for retrieving metadata information during an <see cref="IQuery"/>
/// </summary>
public interface IMeta
{
#region Properties

IMetaSource DocumentID { get; }
/// <summary>
/// Gets an expression for retrieving the unique ID of a document
/// </summary>
IMetaExpression DocumentID { get; }

IMetaSource Sequence { get; }

#endregion
}

internal interface IMetaSource : IExpression
{
#region Public Methods

IExpression From(string alias);
/// <summary>
/// Gets an expression for retrieving the sequence of a document
/// (i.e. the auto-incrementing entry in the database)
/// </summary>
IMetaExpression Sequence { get; }

#endregion
}
Expand Down
41 changes: 41 additions & 0 deletions src/Couchbase.Lite/API/Query/IMetaExpression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// IMetaExpression.cs
//
// Author:
// Jim Borden <[email protected]>
//
// Copyright (c) 2017 Couchbase, Inc All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
namespace Couchbase.Lite.Query
{
/// <summary>
/// Represents an expression that is meant to retrieve metadata information
/// inside of an <see cref="IQuery"/>
/// </summary>
public interface IMetaExpression : IExpression
{
#region Public Methods

/// <summary>
/// Specifies the source to retrieve the information from
/// if multiple sources are used in a query
/// </summary>
/// <param name="alias">The name of the data source</param>
/// <returns>The expression with the alias added</returns>
IExpression From(string alias);

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// IPropertySource.cs
// IPropertyExpression.cs
//
// Author:
// Jim Borden <[email protected]>
Expand All @@ -21,19 +21,19 @@

namespace Couchbase.Lite.Query
{
/// <summary>
/// <summary>
/// An interface for an expression that will retrieve a property
/// from a keypath
/// from a keypath
/// </summary>
public interface IPropertySource : IExpression
public interface IPropertyExpression : IExpression
{
#region Public Methods

/// <summary>
/// <summary>
/// Specifies where to retrieve the property from (necessary
/// in instances where the query is coming from multiple sources)
/// </summary>
/// <param name="alias">The alias of the source to retrieve from</param>
/// in instances where the query is coming from multiple sources)
/// </summary>
/// <param name="alias">The alias of the source to retrieve from</param>
/// <returns>The expression, for further processing</returns>
IExpression From(string alias);

Expand Down
35 changes: 35 additions & 0 deletions src/Couchbase.Lite/Query/QueryMeta.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// QueryMeta.cs
//
// Author:
// Jim Borden <[email protected]>
//
// Copyright (c) 2017 Couchbase, Inc All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
using Couchbase.Lite.Query;

namespace Couchbase.Lite.Internal.Query
{
internal sealed class QueryMeta : IMeta
{
#region Properties

public IMetaExpression DocumentID => new QueryTypeExpression("_id", ExpressionType.KeyPath);

public IMetaExpression Sequence => new QueryTypeExpression("_sequence", ExpressionType.KeyPath);

#endregion
}
}
2 changes: 1 addition & 1 deletion src/Couchbase.Lite/Query/QueryTypeExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal enum ExpressionType
Aggregate,
}

internal sealed class QueryTypeExpression : QueryExpression, IPropertySource
internal sealed class QueryTypeExpression : QueryExpression, IPropertyExpression, IMetaExpression
{
#region Variables

Expand Down

0 comments on commit 23f1d6d

Please sign in to comment.