1- using System ;
2- using System . Collections . Generic ;
3- using System . Data ;
4- using System . Data . Common ;
5- using System . Linq ;
6- using System . Reflection ;
7- using System . Text ;
8- using Simple . Data . Extensions ;
9-
10- namespace Simple . Data . Ado
1+ namespace Simple . Data . Ado
112{
3+ using System ;
4+ using System . Collections ;
5+ using System . Collections . Generic ;
6+ using System . Data ;
7+ using System . Data . Common ;
8+ using System . Linq ;
9+
1210 static class DbCommandExtensions
1311 {
14- public static IEnumerable < IDictionary < string , object > > ToBufferedEnumerable ( this IDbCommand command , IDbConnection connection )
12+ public static IEnumerable < IDictionary < string , object > > ToEnumerable ( this IDbCommand command , IDbConnection connection )
1513 {
16- try
17- {
18- if ( connection . State == ConnectionState . Closed )
19- connection . Open ( ) ;
20- }
21- catch ( DbException ex )
22- {
23- throw new AdoAdapterException ( ex . Message , ex ) ;
24- }
25- var reader = command . ExecuteReaderWithExceptionWrap ( ) ;
26- var index = reader . CreateDictionaryIndex ( ) ;
27- return BufferedEnumerable . Create ( ( ) => reader . Read ( )
28- ? Maybe . Some ( reader . ToDictionary ( index ) )
29- : Maybe < IDictionary < string , object > > . None ,
30- ( ) => DisposeCommandAndReader ( connection , command , reader ) ) ;
14+ return ToEnumerable ( command , connection , null ) ;
3115 }
3216
33- public static IEnumerable < IDictionary < string , object > > ToBufferedEnumerable ( this IDbCommand command , IDbConnection connection , IDictionary < string , int > index )
17+ public static IEnumerable < IDictionary < string , object > > ToEnumerable ( this IDbCommand command , IDbConnection connection , IDictionary < string , int > index )
3418 {
35- try
36- {
37- if ( connection . State == ConnectionState . Closed )
38- connection . Open ( ) ;
39- }
40- catch ( DbException ex )
41- {
42- throw new AdoAdapterException ( ex . Message , ex ) ;
43- }
44- var reader = command . ExecuteReaderWithExceptionWrap ( ) ;
45- return BufferedEnumerable . Create ( ( ) => reader . Read ( )
46- ? Maybe . Some ( reader . ToDictionary ( index ) )
47- : Maybe < IDictionary < string , object > > . None ,
48- ( ) => DisposeCommandAndReader ( connection , command , reader ) ) ;
19+ return new DataReaderEnumerator ( command , connection , index ) . Wrap ( ) ;
20+ }
21+
22+ public static IObservable < IDictionary < string , object > > ToObservable ( this IDbCommand command , IDbConnection connection , AdoAdapter adapter )
23+ {
24+ return ToObservable ( command , connection , adapter , null ) ;
25+ }
26+
27+ public static IObservable < IDictionary < string , object > > ToObservable ( this IDbCommand command , IDbConnection connection , AdoAdapter adapter , IDictionary < string , int > index )
28+ {
29+ var runner = adapter . ProviderHelper . GetCustomProvider < IObservableQueryRunner > ( adapter . ConnectionProvider ) ?? new ObservableQueryRunner ( ) ;
30+ return runner . Run ( command , connection , index ) ;
4931 }
5032
5133 public static IDbDataParameter AddParameter ( this IDbCommand command , string name , object value )
@@ -72,12 +54,39 @@ public static IDataReader ExecuteReaderWithExceptionWrap(this IDbCommand command
7254 }
7355 }
7456
75- private static void DisposeCommandAndReader ( IDbConnection connection , IDbCommand command , IDataReader reader )
57+ internal static void DisposeCommandAndReader ( IDbConnection connection , IDbCommand command , IDataReader reader )
7658 {
7759 using ( connection )
7860 using ( command )
7961 using ( reader )
8062 { /* NoOp */ }
8163 }
8264 }
65+
66+ class EnumerableShim < T > : IEnumerable < T >
67+ {
68+ private readonly IEnumerator < T > _enumerator ;
69+ public EnumerableShim ( IEnumerator < T > enumerator )
70+ {
71+ _enumerator = enumerator ;
72+ }
73+
74+ public IEnumerator < T > GetEnumerator ( )
75+ {
76+ return _enumerator ;
77+ }
78+
79+ IEnumerator IEnumerable . GetEnumerator ( )
80+ {
81+ return GetEnumerator ( ) ;
82+ }
83+ }
84+
85+ static class EnumerableShim
86+ {
87+ public static IEnumerable < T > Wrap < T > ( this IEnumerator < T > enumerator )
88+ {
89+ return new EnumerableShim < T > ( enumerator ) ;
90+ }
91+ }
8392}
0 commit comments