Skip to content

Commit 802e9cd

Browse files
committed
Fixed DeleteByCommand to accept records to be deleted.
1 parent 7006e03 commit 802e9cd

5 files changed

Lines changed: 75 additions & 21 deletions

File tree

Simple.Data.Ado/ProcedureExecutor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private static void SetParameters(Procedure procedure, IDbCommand cmd, IDictiona
118118
|| sp.Key.Equals("_" + i)
119119
);
120120
var cmdParameter = cmd.CreateParameter();
121-
//Tim Cartwright: Using AddParameter does not allow for the "default" keyword to ever be passed into
121+
//Tim Cartwright: method AddParameter does not allow for the "default" keyword to ever be passed into
122122
// parameters in stored procedures with defualt values. Null is always sent in. This will allow for default
123123
// values to work properly. Not sure why this is so, in both cases the value gets set. Just is.
124124
//var cmdParameter = cmd.AddParameter(parameter.Name, value);

Simple.Data.BehaviourTest/Query/WithTest.cs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,46 @@ public class WithTest : DatabaseIntegrationContext
99
{
1010
protected override void SetSchema(MockSchemaProvider schemaProvider)
1111
{
12-
throw new NotImplementedException();
12+
schemaProvider.SetTables(new[] { "dbo", "Employee", "BASE TABLE" },
13+
new[] { "dbo", "Department", "BASE TABLE" },
14+
new[] { "dbo", "Activity", "BASE TABLE" },
15+
new[] { "dbo", "Activity_Join", "BASE TABLE" },
16+
new[] { "dbo", "Location", "BASE_TABLE" });
17+
18+
schemaProvider.SetColumns(new[] { "dbo", "Employee", "Id" },
19+
new[] { "dbo", "Employee", "Name" },
20+
new[] { "dbo", "Employee", "ManagerId" },
21+
new[] { "dbo", "Employee", "DepartmentId" },
22+
new[] { "dbo", "Department", "Id" },
23+
new[] { "dbo", "Department", "Name" },
24+
new[] { "dbo", "Activity", "ID_Activity" },
25+
new[] { "dbo", "Activity", "ID_Trip" },
26+
new[] { "dbo", "Activity", "Activity_Time" },
27+
new[] { "dbo", "Activity", "Is_Public" },
28+
new[] { "dbo", "Activity_Join", "ID_Activity" },
29+
new[] { "dbo", "Activity_Join", "ID_Location" },
30+
new[] { "dbo", "Location", "ID_Location" }
31+
);
32+
schemaProvider.SetPrimaryKeys(
33+
new object[] { "dbo", "Employee", "Id", 0 },
34+
new object[] { "dbo", "Department", "Id", 0 }
35+
);
36+
schemaProvider.SetForeignKeys(new object[] { "FK_Employee_Department", "dbo", "Employee", "DepartmentId", "dbo", "Department", "Id", 0 });
37+
}
38+
39+
[Test]
40+
public void SingleWithClauseShouldUseJoin()
41+
{
42+
const string expectedSql = "select [employee].[id] as [__with__employee__id],[employee].[name] as [__with__employee__name],"+
43+
"[employee].[managerid] as [__with__employee__managerid],[employee].[departmentid] as [__with__employee__departmentid],"+
44+
"[department].[id] as [__with__department__id],[department].[name] as [__with__department__name]"+
45+
"from [employee] left join [department] on [employee].[departmentid] = [department].[id]";
46+
47+
var q = _db.Employees.All().WithDepartment();
48+
49+
EatException(() => q.ToList());
50+
51+
GeneratedSqlIs(expectedSql);
1352
}
1453
}
1554
}

Simple.Data/Commands/DeleteByCommand.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Simple.Data.Commands
66
{
7+
using Extensions;
8+
79
class DeleteByCommand : ICommand
810
{
911
public bool IsCommandFor(string method)
@@ -24,10 +26,21 @@ public Func<object[], object> CreateDelegate(DataStrategy dataStrategy, DynamicT
2426

2527
private static SimpleExpression GetCriteriaExpression(InvokeMemberBinder binder, object[] args, DynamicTable table)
2628
{
27-
var criteria = binder.Name.Equals("delete", StringComparison.InvariantCultureIgnoreCase) ?
28-
binder.NamedArgumentsToDictionary(args)
29-
:
30-
MethodNameParser.ParseFromBinder(binder, args);
29+
IDictionary<string, object> criteria;
30+
if (binder.Name.Equals("delete", StringComparison.InvariantCultureIgnoreCase))
31+
{
32+
criteria = binder.NamedArgumentsToDictionary(args);
33+
if (criteria.Count == 0 && args.Length == 1)
34+
{
35+
criteria = args[0] as IDictionary<string, object> ?? args[0].ObjectToDictionary();
36+
}
37+
}
38+
else
39+
{
40+
criteria = MethodNameParser.ParseFromBinder(binder, args);
41+
}
42+
43+
if (criteria.Count == 0) throw new InvalidOperationException("No criteria specified for Delete. To delete all data, use DeleteAll().");
3144

3245
return ExpressionHelper.CriteriaDictionaryToExpression(table.GetQualifiedName(), criteria);
3346
}

Simple.Data/Simple.Data.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@
173173
<Compile Include="UnresolvableObjectException.cs" />
174174
<Compile Include="Commands\UpdateByCommand.cs" />
175175
<Compile Include="WhereClause.cs" />
176+
<Compile Include="WithClause.cs" />
176177
<Compile Include="WithCountClause.cs" />
178+
<Compile Include="WithMode.cs" />
177179
</ItemGroup>
178180
<ItemGroup>
179181
<None Include="app.config" />

Simple.Data/WithCountClause.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
namespace Simple.Data
2-
{
3-
using System;
4-
5-
public class WithCountClause : SimpleQueryClauseBase
6-
{
7-
private readonly Action<int> _setCount;
8-
9-
public WithCountClause(Action<int> setCount)
10-
{
11-
_setCount = setCount;
12-
}
13-
1+
namespace Simple.Data
2+
{
3+
using System;
4+
5+
public class WithCountClause : SimpleQueryClauseBase
6+
{
7+
private readonly Action<int> _setCount;
8+
9+
public WithCountClause(Action<int> setCount)
10+
{
11+
_setCount = setCount;
12+
}
13+
1414
public void SetCount(int count)
1515
{
1616
_setCount(count);
17-
}
18-
}
17+
}
18+
}
1919
}

0 commit comments

Comments
 (0)