Skip to content

Commit 6602844

Browse files
committed
Added WithOptions to allow Command Timeout and Identity Insert (SQL Server only)
1 parent f9d43eb commit 6602844

39 files changed

Lines changed: 543 additions & 207 deletions
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System.Data;
2+
using NUnit.Framework;
3+
4+
namespace Simple.Data.Ado.Test
5+
{
6+
[TestFixture]
7+
public class ConnectionModifierTest
8+
{
9+
[Test]
10+
public void ModifiesConnection()
11+
{
12+
var adapter = new AdoAdapter(new StubConnectionProvider());
13+
adapter.SetConnectionModifier(c => new FooConnection(c));
14+
Assert.IsInstanceOf<FooConnection>(adapter.CreateConnection());
15+
}
16+
17+
[Test]
18+
public void ClearsConnection()
19+
{
20+
var adapter = new AdoAdapter(new StubConnectionProvider());
21+
adapter.SetConnectionModifier(c => new FooConnection(c));
22+
Assert.IsInstanceOf<FooConnection>(adapter.CreateConnection());
23+
adapter.ClearConnectionModifier();
24+
Assert.IsNotInstanceOf<FooConnection>(adapter.CreateConnection());
25+
}
26+
27+
private class FooConnection : IDbConnection
28+
{
29+
private readonly IDbConnection _wrapped;
30+
31+
public FooConnection(IDbConnection wrapped)
32+
{
33+
_wrapped = wrapped;
34+
}
35+
36+
public void Dispose()
37+
{
38+
throw new System.NotImplementedException();
39+
}
40+
41+
public IDbTransaction BeginTransaction()
42+
{
43+
throw new System.NotImplementedException();
44+
}
45+
46+
public IDbTransaction BeginTransaction(IsolationLevel il)
47+
{
48+
throw new System.NotImplementedException();
49+
}
50+
51+
public void Close()
52+
{
53+
throw new System.NotImplementedException();
54+
}
55+
56+
public void ChangeDatabase(string databaseName)
57+
{
58+
throw new System.NotImplementedException();
59+
}
60+
61+
public IDbCommand CreateCommand()
62+
{
63+
throw new System.NotImplementedException();
64+
}
65+
66+
public void Open()
67+
{
68+
throw new System.NotImplementedException();
69+
}
70+
71+
public string ConnectionString { get; set; }
72+
public int ConnectionTimeout { get; private set; }
73+
public string Database { get; private set; }
74+
public ConnectionState State { get; private set; }
75+
}
76+
}
77+
}

Simple.Data.Ado.Test/Simple.Data.Ado.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
</ItemGroup>
5959
<ItemGroup>
6060
<Compile Include="AdoAdapterExceptionTest.cs" />
61+
<Compile Include="ConnectionModifierTest.cs" />
6162
<Compile Include="Properties\AssemblyInfo.cs" />
6263
<Compile Include="ProviderHelperTest.cs" />
6364
<Compile Include="TableCollectionTest.cs" />

Simple.Data.Ado.Test/TestCustomInserter.cs

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.ComponentModel.Composition;
44
using System.Data;
5+
using System.Data.OleDb;
56
using System.Linq;
67
using System.Text;
78
using NUnit.Framework;
@@ -31,17 +32,17 @@ public void SetConnectionString(string connectionString)
3132

3233
public IDbConnection CreateConnection()
3334
{
34-
throw new NotImplementedException();
35+
return new OleDbConnection();
3536
}
3637

3738
public ISchemaProvider GetSchemaProvider()
3839
{
39-
throw new NotImplementedException();
40+
return new StubSchemaProvider();
4041
}
4142

4243
public string ConnectionString
4344
{
44-
get { throw new NotImplementedException(); }
45+
get { return "stub"; }
4546
}
4647

4748
public bool SupportsCompoundStatements
@@ -65,10 +66,58 @@ public IProcedureExecutor GetProcedureExecutor(AdoAdapter adapter, ObjectName pr
6566
}
6667
}
6768

69+
public class StubSchemaProvider : ISchemaProvider
70+
{
71+
public IEnumerable<Table> GetTables()
72+
{
73+
throw new NotImplementedException();
74+
}
75+
76+
public IEnumerable<Column> GetColumns(Table table)
77+
{
78+
throw new NotImplementedException();
79+
}
80+
81+
public IEnumerable<Procedure> GetStoredProcedures()
82+
{
83+
throw new NotImplementedException();
84+
}
85+
86+
public IEnumerable<Parameter> GetParameters(Procedure storedProcedure)
87+
{
88+
throw new NotImplementedException();
89+
}
90+
91+
public Key GetPrimaryKey(Table table)
92+
{
93+
throw new NotImplementedException();
94+
}
95+
96+
public IEnumerable<ForeignKey> GetForeignKeys(Table table)
97+
{
98+
throw new NotImplementedException();
99+
}
100+
101+
public string QuoteObjectName(string unquotedName)
102+
{
103+
throw new NotImplementedException();
104+
}
105+
106+
public string NameParameter(string baseName)
107+
{
108+
throw new NotImplementedException();
109+
}
110+
111+
public string GetDefaultSchema()
112+
{
113+
throw new NotImplementedException();
114+
}
115+
}
116+
68117
[Export(typeof(ICustomInserter))]
69118
public class StubCustomInserter : ICustomInserter
70119
{
71-
public IDictionary<string, object> Insert(AdoAdapter adapter, string tableName, IDictionary<string, object> data, IDbTransaction transaction = null)
120+
public IDictionary<string, object> Insert(AdoAdapter adapter, string tableName, IDictionary<string, object> data, IDbTransaction transaction = null, bool resultRequired = false)
72121
{
73122
throw new NotImplementedException();
74123
}

Simple.Data.Ado/AdoAdapter.cs

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.ComponentModel.Composition;
44
using System.Data;
5-
using System.Data.Common;
65
using System.Linq;
76
using Simple.Data.Ado.Schema;
87

@@ -18,6 +17,7 @@ public partial class AdoAdapter : Adapter, ICloneable
1817
private Lazy<AdoAdapterRelatedFinder> _relatedFinder;
1918
private DatabaseSchema _schema;
2019
private IDbConnection _sharedConnection;
20+
private Func<IDbConnection, IDbConnection> _connectionModifier = connection => connection;
2121

2222
public AdoAdapter()
2323
{
@@ -43,6 +43,11 @@ private AdoAdapter(IConnectionProvider connectionProvider, AdoAdapterFinder find
4343
_schema = schema;
4444
}
4545

46+
public AdoOptions AdoOptions
47+
{
48+
get { return Options as AdoOptions; }
49+
}
50+
4651
public CommandOptimizer CommandOptimizer
4752
{
4853
get { return _commandOptimizer; }
@@ -84,7 +89,7 @@ public override IDictionary<string, object> GetKey(string tableName, IDictionary
8489

8590
public object Clone()
8691
{
87-
return new AdoAdapter(_connectionProvider);
92+
return new AdoAdapter(_connectionProvider) {_connectionModifier = _connectionModifier};
8893
}
8994

9095
#endregion
@@ -176,11 +181,6 @@ private static bool FunctionIsLikeOrNotLike(string functionName, object[] args)
176181
&& args[0] is string);
177182
}
178183

179-
private static bool FunctionIsCount(string functionName, object[] args)
180-
{
181-
return (functionName.Equals("count", StringComparison.OrdinalIgnoreCase) && args.Length == 0);
182-
}
183-
184184
public override IObservable<IDictionary<string, object>> RunQueryAsObservable(SimpleQuery query,
185185
out
186186
IEnumerable
@@ -245,56 +245,53 @@ public override IList<string> GetKeyNames(string tableName)
245245
return _schema.FindTable(tableName).PrimaryKey.AsEnumerable().ToList();
246246
}
247247

248+
public void SetConnectionModifier(Func<IDbConnection, IDbConnection> connectionModifer)
249+
{
250+
_connectionModifier = connectionModifer;
251+
}
252+
253+
public void ClearConnectionModifier()
254+
{
255+
_connectionModifier = connection => connection;
256+
}
257+
248258
private int Execute(ICommandBuilder commandBuilder)
249259
{
250260
IDbConnection connection = CreateConnection();
251261
using (connection.MaybeDisposable())
252262
{
253-
using (IDbCommand command = commandBuilder.GetCommand(connection))
263+
using (IDbCommand command = commandBuilder.GetCommand(connection, AdoOptions))
254264
{
255265
connection.OpenIfClosed();
256-
return TryExecute(command);
266+
return command.TryExecuteNonQuery();
257267
}
258268
}
259269
}
260270

261-
internal static int Execute(ICommandBuilder commandBuilder, IDbConnection connection)
271+
internal int Execute(ICommandBuilder commandBuilder, IDbConnection connection)
262272
{
263273
using (connection.MaybeDisposable())
264274
{
265-
using (IDbCommand command = commandBuilder.GetCommand(connection))
275+
using (IDbCommand command = commandBuilder.GetCommand(connection, AdoOptions))
266276
{
267277
connection.OpenIfClosed();
268-
return TryExecute(command);
278+
return command.TryExecuteNonQuery();
269279
}
270280
}
271281
}
272282

273-
internal static int Execute(ICommandBuilder commandBuilder, IAdapterTransaction transaction)
283+
internal int Execute(ICommandBuilder commandBuilder, IAdapterTransaction transaction)
274284
{
275285
IDbTransaction dbTransaction = ((AdoAdapterTransaction) transaction).DbTransaction;
276286
return Execute(commandBuilder, dbTransaction);
277287
}
278288

279-
internal static int Execute(ICommandBuilder commandBuilder, IDbTransaction dbTransaction)
289+
internal int Execute(ICommandBuilder commandBuilder, IDbTransaction dbTransaction)
280290
{
281-
using (IDbCommand command = commandBuilder.GetCommand(dbTransaction.Connection))
291+
using (IDbCommand command = commandBuilder.GetCommand(dbTransaction.Connection, AdoOptions))
282292
{
283293
command.Transaction = dbTransaction;
284-
return TryExecute(command);
285-
}
286-
}
287-
288-
private static int TryExecute(IDbCommand command)
289-
{
290-
command.WriteTrace();
291-
try
292-
{
293-
return command.ExecuteNonQuery();
294-
}
295-
catch (DbException ex)
296-
{
297-
throw new AdoAdapterException(ex.Message, command);
294+
return command.TryExecuteNonQuery();
298295
}
299296
}
300297

@@ -310,7 +307,7 @@ public void StopUsingSharedConnection()
310307

311308
public IDbConnection CreateConnection()
312309
{
313-
return _sharedConnection ?? _connectionProvider.CreateConnection();
310+
return _sharedConnection ?? _connectionModifier(_connectionProvider.CreateConnection());
314311
}
315312

316313
public DatabaseSchema GetSchema()
@@ -325,7 +322,8 @@ public override IDictionary<string, object> Upsert(string tableName, IDictionary
325322

326323
public override IEnumerable<IDictionary<string, object>> UpsertMany(string tableName, IList<IDictionary<string, object>> list, bool isResultRequired, Func<IDictionary<string, object>, Exception, bool> errorCallback)
327324
{
328-
return new AdoAdapterUpserter(this).UpsertMany(tableName, list, isResultRequired, errorCallback);
325+
var upserter = new AdoAdapterUpserter(this);
326+
return upserter.UpsertMany(tableName, list, isResultRequired, errorCallback);
329327
}
330328

331329
public override IEnumerable<IDictionary<string, object>> UpsertMany(string tableName, IList<IDictionary<string, object>> list, IEnumerable<string> keyFieldNames, bool isResultRequired, Func<IDictionary<string, object>, Exception, bool> errorCallback)

Simple.Data.Ado/AdoAdapterFinder.cs

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public Func<object[],IDictionary<string,object>> CreateFindOneDelegate(string ta
5454
var commandBuilder = new FindHelper(_adapter.GetSchema())
5555
.GetFindByCommand(_adapter.GetSchema().BuildObjectName(tableName), criteria);
5656

57-
var command = commandBuilder.GetCommand(_adapter.CreateConnection());
57+
var command = commandBuilder.GetCommand(_adapter.CreateConnection(), _adapter.AdoOptions);
5858
command = _adapter.CommandOptimizer.OptimizeFindOne(command);
5959

6060
var commandTemplate =
@@ -169,39 +169,21 @@ private Func<IDbConnection> ConnectionCreator
169169

170170
private static IDictionary<string, object> TryExecuteSingletonQuery(IDbConnection connection, IDbCommand command, IDictionary<string, int> index)
171171
{
172-
command.WriteTrace();
173172
using (connection.MaybeDisposable())
174173
using (command)
175174
{
176-
try
175+
connection.OpenIfClosed();
176+
using (var reader = command.TryExecuteReader())
177177
{
178-
connection.OpenIfClosed();
179-
using (var reader = command.ExecuteReader())
178+
if (reader.Read())
180179
{
181-
if (reader.Read())
182-
{
183-
return reader.ToDictionary(index);
184-
}
180+
return reader.ToDictionary(index);
185181
}
186182
}
187-
catch (DbException ex)
188-
{
189-
throw new AdoAdapterException(ex.Message, command);
190-
}
191183
}
192184
return null;
193185
}
194186

195-
private static IDisposable DisposeWrap(IDbConnection connection)
196-
{
197-
if (connection.State == ConnectionState.Open)
198-
{
199-
return ActionDisposable.NoOp;
200-
}
201-
202-
return new ActionDisposable(connection.Dispose);
203-
}
204-
205187
private static object FixObjectType(object value)
206188
{
207189
if (value == null) return DBNull.Value;

0 commit comments

Comments
 (0)