22using System . Collections . Generic ;
33using System . ComponentModel . Composition ;
44using System . Data ;
5- using System . Data . Common ;
65using System . Linq ;
76using 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 )
0 commit comments