forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbCommandExtensions.cs
More file actions
162 lines (141 loc) · 5.7 KB
/
DbCommandExtensions.cs
File metadata and controls
162 lines (141 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
namespace Simple.Data.Ado
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Dynamic;
using System.Linq;
public static class DbCommandExtensions
{
public static IEnumerable<IDictionary<string, object>> ToEnumerable(this IDbCommand command, Func<IDbConnection> createConnection)
{
return ToEnumerable(command, createConnection, null);
}
public static IEnumerable<IEnumerable<IDictionary<string, object>>> ToEnumerables(this IDbCommand command, IDbConnection connection)
{
return new DataReaderMultipleEnumerator(command, connection).Wrap();
}
public static IEnumerable<IDictionary<string, object>> ToEnumerable(this IDbCommand command, Func<IDbConnection> createConnection, IDictionary<string, int> index)
{
return new DataReaderEnumerable(command, createConnection, index);
}
public static IObservable<IDictionary<string, object>> ToObservable(this IDbCommand command, IDbConnection connection, AdoAdapter adapter)
{
return ToObservable(command, connection, adapter, null);
}
public static IObservable<IDictionary<string, object>> ToObservable(this IDbCommand command, IDbConnection connection, AdoAdapter adapter, IDictionary<string, int> index)
{
var runner = adapter.ProviderHelper.GetCustomProvider<IObservableQueryRunner>(adapter.ConnectionProvider) ?? new ObservableQueryRunner();
return runner.Run(command, connection, index);
}
public static IDbDataParameter AddParameter(this IDbCommand command, string name, object value)
{
var parameter = command.CreateParameter();
parameter.ParameterName = name;
parameter.Value = FixObjectType(value);
command.Parameters.Add(parameter);
return parameter;
}
private static object FixObjectType(object value)
{
if (value == null) return DBNull.Value;
if (TypeHelper.IsKnownType(value.GetType())) return value;
var dynamicObject = value as DynamicObject;
if (dynamicObject != null)
{
return dynamicObject.ToString();
}
return value;
}
public static IDataReader TryExecuteReader(this IDbCommand command)
{
command.WriteTrace();
try
{
return command.ExecuteReader();
}
catch (DbException ex)
{
throw CreateAdoAdapterException(command, ex);
}
}
public static int TryExecuteNonQuery(this IDbCommand command)
{
command.WriteTrace();
try
{
return command.ExecuteNonQuery();
}
catch (DbException ex)
{
throw CreateAdoAdapterException(command, ex);
}
}
private static AdoAdapterException CreateAdoAdapterException(IDbCommand command, DbException ex)
{
return new AdoAdapterException(ex.Message, command.CommandText,
command.Parameters.Cast<IDbDataParameter>()
.ToDictionary(p => p.ParameterName, p => p.Value), ex);
}
internal static void DisposeCommandAndReader(IDbConnection connection, IDbCommand command, IDataReader reader)
{
using (connection)
using (command)
using (reader)
{ /* NoOp */ }
}
public static void SetParameterValues(this IDbCommand command, IList<object> values)
{
int index = 0;
foreach (var parameter in command.Parameters.Cast<IDbDataParameter>())
{
parameter.Value = CommandHelper.FixObjectType(values[index]);
index++;
}
}
public static void ClearParameterValues(this IDbCommand command)
{
foreach (var parameter in command.Parameters.Cast<IDbDataParameter>())
{
parameter.Value = DBNull.Value;
}
}
public static void SetParameterValue(this IDbCommand command, int index, object value)
{
((IDbDataParameter) command.Parameters[index]).Value = CommandHelper.FixObjectType(value);
}
public static IEnumerable<IDictionary<string, object>> ToEnumerable(this IDbCommand command, IDbTransaction transaction)
{
return ToEnumerable(command, transaction, null);
}
public static IEnumerable<IDictionary<string, object>> ToEnumerable(this IDbCommand command, IDbTransaction transaction, IDictionary<string, int> index)
{
return new DataReaderEnumerable(command, transaction, index);
}
}
class EnumerableShim<T> : IEnumerable<T>
{
private readonly IEnumerator<T> _enumerator;
public EnumerableShim(IEnumerator<T> enumerator)
{
_enumerator = enumerator;
}
public IEnumerator<T> GetEnumerator()
{
return _enumerator;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
static class EnumerableShim
{
public static IEnumerable<T> Wrap<T>(this IEnumerator<T> enumerator)
{
return new EnumerableShim<T>(enumerator);
}
}
}