Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'filteraskey' of github.com:simplefx/Simple.OData

  • Loading branch information...
commit 9c233c116020d2ec4cded2b06ab25607bb7849dc 2 parents 5dd0880 + dd8b2fc
@object object authored
View
10 Simple.Data.OData/ODataTableAdapter.cs
@@ -113,14 +113,20 @@ private int UpdateByExpression(string tableName, IDictionary<string, object> dat
{
var cmd = new CommandBuilder().BuildCommand(tableName, criteria);
var clientCommand = GetODataClientCommand(cmd);
- return GetODataClient(transaction).UpdateEntries(tableName, clientCommand.CommandText, data);
+ var client = GetODataClient(transaction);
+ return clientCommand.FilterIsKey ?
+ client.UpdateEntry(tableName, clientCommand.FilterAsKey, data) :
+ client.UpdateEntries(tableName, clientCommand.CommandText, data);
}
private int DeleteByExpression(string tableName, SimpleExpression criteria, IAdapterTransaction transaction)
{
var cmd = new CommandBuilder().BuildCommand(tableName, criteria);
var clientCommand = GetODataClientCommand(cmd);
- return GetODataClient(transaction).DeleteEntries(tableName, clientCommand.CommandText);
+ var client = GetODataClient(transaction);
+ return clientCommand.FilterIsKey ?
+ client.DeleteEntry(tableName, clientCommand.FilterAsKey) :
+ client.DeleteEntries(tableName, clientCommand.CommandText);
}
private ODataClient GetODataClient(IAdapterTransaction transaction = null)
View
68 Simple.OData.Client.Tests/FilterAsKeyTests.cs
@@ -6,7 +6,7 @@ namespace Simple.OData.Client.Tests
public class FilterAsKeyTests : TestBase
{
[Fact]
- public void FindAllByProductID()
+ public void FindAllByFilterAsKeyEqual()
{
var x = ODataFilter.Expression;
var command = _client
@@ -17,6 +17,72 @@ public void FindAllByProductID()
}
[Fact]
+ public void FindAllByFilterAsKeyNotEqual()
+ {
+ var x = ODataFilter.Expression;
+ var command = _client
+ .From("Products")
+ .Filter(x.ProductID != 1);
+ string commandText = command.CommandText;
+ Assert.Equal("Products?$filter=ProductID+ne+1", commandText);
+ }
+
+ [Fact]
+ public void FindAllByFilterAsNotKeyEqual()
+ {
+ var x = ODataFilter.Expression;
+ var command = _client
+ .From("Products")
+ .Filter(!(x.ProductID == 1));
+ string commandText = command.CommandText;
+ Assert.Equal("Products?$filter=not(ProductID+eq+1)", commandText);
+ }
+
+ [Fact]
+ public void FindAllByFilterAsKeyEqualAndExtraClause()
+ {
+ var x = ODataFilter.Expression;
+ var command = _client
+ .From("Products")
+ .Filter(x.ProductID == 1 && x.ProductName == "abc");
+ string commandText = command.CommandText;
+ Assert.Equal("Products?$filter=ProductID+eq+1+and+ProductName+eq+%27abc%27", commandText);
+ }
+
+ [Fact]
+ public void FindAllByFilterAsKeyEqualDuplicateClause()
+ {
+ var x = ODataFilter.Expression;
+ var command = _client
+ .From("Products")
+ .Filter(x.ProductID == 1 && x.ProductID == 1);
+ string commandText = command.CommandText;
+ Assert.Equal("Products(1)", commandText);
+ }
+
+ [Fact]
+ public void FindAllByFilterAsCompleteCompoundKey()
+ {
+ var x = ODataFilter.Expression;
+ var command = _client
+ .From("OrderDetails")
+ .Filter(x.OrderID == 1 && x.ProductID == 2);
+ string commandText = command.CommandText;
+ Assert.Equal("Order_Details(OrderID=1,ProductID=2)", commandText);
+ }
+
+ [Fact]
+ public void FindAllByFilterAsInCompleteCompoundKey()
+ {
+ var x = ODataFilter.Expression;
+ var command = _client
+ .From("OrderDetails")
+ .Filter(x.OrderID == 1);
+ string commandText = command.CommandText;
+ Assert.Equal("Order_Details?$filter=OrderID+eq+1", commandText);
+ }
+
+ [Fact]
public void FindAllEmployeeSuperiors()
{
var x = ODataFilter.Expression;
View
9 Simple.OData.Client/Filter/FilterExpression.cs
@@ -76,14 +76,14 @@ public override string ToString()
return Format(new ExpressionContext());
}
- internal void ExtractEqualityComparisons(IDictionary<string, object> columnEqualityComparisons)
+ internal bool ExtractEqualityComparisons(IDictionary<string, object> columnEqualityComparisons)
{
switch (_operator)
{
case ExpressionOperator.AND:
_left.ExtractEqualityComparisons(columnEqualityComparisons);
_right.ExtractEqualityComparisons(columnEqualityComparisons);
- break;
+ return true;
case ExpressionOperator.EQ:
if (!string.IsNullOrEmpty(_left._reference))
@@ -92,7 +92,10 @@ internal void ExtractEqualityComparisons(IDictionary<string, object> columnEqual
if (!columnEqualityComparisons.ContainsKey(key))
columnEqualityComparisons.Add(key, _right);
}
- break;
+ return true;
+
+ default:
+ return false;
}
}
}
View
2  Simple.OData.Client/ICommand.cs
@@ -23,5 +23,7 @@ public interface ICommand
IClientWithCommand NavigateTo(string linkName);
IClientWithCommand Function(string functionName);
IClientWithCommand Parameters(IDictionary<string, object> parameters);
+ bool FilterIsKey { get; }
+ IDictionary<string, object> FilterAsKey { get; }
}
}
View
10 Simple.OData.Client/ODataClientWithCommand.cs
@@ -204,5 +204,15 @@ public IClientWithCommand NavigateTo(string linkName)
{
return _command.NavigateTo(linkName);
}
+
+ public bool FilterIsKey
+ {
+ get { return _command.FilterIsKey; }
+ }
+
+ public IDictionary<string, object> FilterAsKey
+ {
+ get { return _command.FilterAsKey; }
+ }
}
}
View
21 Simple.OData.Client/ODataCommand.cs
@@ -178,6 +178,22 @@ public IClientWithCommand NavigateTo(string linkName)
return _client.Link(this, linkName);
}
+ public bool FilterIsKey
+ {
+ get
+ {
+ return _namedKeyValues != null;
+ }
+ }
+
+ public IDictionary<string, object> FilterAsKey
+ {
+ get
+ {
+ return _namedKeyValues;
+ }
+ }
+
public ODataCommand WithInlineCount()
{
_inlineCount = true;
@@ -301,12 +317,13 @@ private bool HasKey
private IDictionary<string, object> TryInterpretFilterExpressionAsKey(FilterExpression expression)
{
+ bool ok = false;
IDictionary<string, object> namedKeyValues = new Dictionary<string, object>();
if (!ReferenceEquals(expression, null))
{
- expression.ExtractEqualityComparisons(namedKeyValues);
+ ok = expression.ExtractEqualityComparisons(namedKeyValues);
}
- return _table.GetKeyNames().All(namedKeyValues.ContainsKey) ? namedKeyValues : null;
+ return ok && _table.GetKeyNames().Count == namedKeyValues.Count() && _table.GetKeyNames().All(namedKeyValues.ContainsKey) ? namedKeyValues : null;
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.