forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdoAdapterGetter.cs
More file actions
126 lines (106 loc) · 5.43 KB
/
AdoAdapterGetter.cs
File metadata and controls
126 lines (106 loc) · 5.43 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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Dynamic;
using System.Linq;
namespace Simple.Data.Ado
{
class AdoAdapterGetter
{
private readonly ConcurrentDictionary<string, ConcurrentDictionary<string, CommandTemplate>> _commandCaches =
new ConcurrentDictionary<string, ConcurrentDictionary<string, CommandTemplate>>();
private readonly AdoAdapter _adapter;
private readonly IDbTransaction _transaction;
private readonly IDbConnection _connection;
public AdoAdapterGetter(AdoAdapter adapter) : this(adapter, null)
{
}
public AdoAdapterGetter(AdoAdapter adapter, IDbTransaction transaction)
{
if (adapter == null) throw new ArgumentNullException("adapter");
_adapter = adapter;
if (transaction != null)
{
_transaction = transaction;
_connection = transaction.Connection;
}
}
public Func<object[],IDictionary<string,object>> CreateGetDelegate(string tableName, params object[] keyValues)
{
var primaryKey = _adapter.GetSchema().FindTable(tableName).PrimaryKey;
if (primaryKey == null) throw new InvalidOperationException(string.Format("Table '{0}' has no primary key.", tableName));
if (primaryKey.Length != keyValues.Length) throw new ArgumentException("Incorrect number of values for key.");
var commandBuilder = new GetHelper(_adapter.GetSchema()).GetCommand(_adapter.GetSchema().FindTable(tableName), keyValues);
var command = commandBuilder.GetCommand(_adapter.CreateConnection(), _adapter.AdoOptions);
command = _adapter.CommandOptimizer.OptimizeFindOne(command);
var commandTemplate =
commandBuilder.GetCommandTemplate(
_adapter.GetSchema().FindTable(_adapter.GetSchema().BuildObjectName(tableName)));
var cloneable = command as ICloneable;
if (cloneable != null)
{
return args => ExecuteSingletonQuery((IDbCommand)cloneable.Clone(), args, commandTemplate.Index);
}
return args => ExecuteSingletonQuery(commandTemplate, args);
}
private IDictionary<string, object> ExecuteSingletonQuery(IDbCommand command, object[] parameterValues, IDictionary<string,int> index)
{
for (int i = 0; i < command.Parameters.Count; i++)
{
((IDbDataParameter) command.Parameters[i]).Value = FixObjectType(parameterValues[i]);
}
command.Connection = _connection ?? _adapter.CreateConnection();
command.Transaction = _transaction;
return TryExecuteSingletonQuery(command.Connection, command, index);
}
private IDictionary<string, object> ExecuteSingletonQuery(CommandTemplate commandTemplate, IEnumerable<object> parameterValues)
{
var connection = _connection ?? _adapter.CreateConnection();
var command = commandTemplate.GetDbCommand(_adapter, connection, parameterValues);
command.Transaction = _transaction;
return TryExecuteSingletonQuery(connection, command, commandTemplate.Index);
}
private static IDictionary<string, object> TryExecuteSingletonQuery(IDbConnection connection, IDbCommand command, IDictionary<string, int> index)
{
using (connection.MaybeDisposable())
using (command)
{
connection.OpenIfClosed();
using (var reader = command.TryExecuteReader())
{
if (reader.Read())
{
return reader.ToDictionary(index);
}
}
}
return null;
}
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 IDictionary<string, object> Get(string tableName, object[] parameterValues)
{
var primaryKey = _adapter.GetSchema().FindTable(tableName).PrimaryKey;
if (primaryKey == null) throw new InvalidOperationException(string.Format("Table '{0}' has no primary key.", tableName));
if (primaryKey.Length != parameterValues.Length) throw new ArgumentException("Incorrect number of values for key.");
var commandBuilder = new GetHelper(_adapter.GetSchema()).GetCommand(_adapter.GetSchema().FindTable(tableName), parameterValues);
var command = commandBuilder.GetCommand(_adapter.CreateConnection(), _adapter.AdoOptions);
command = _adapter.CommandOptimizer.OptimizeFindOne(command);
var commandTemplate =
commandBuilder.GetCommandTemplate(
_adapter.GetSchema().FindTable(_adapter.GetSchema().BuildObjectName(tableName)));
return ExecuteSingletonQuery(command, parameterValues, commandTemplate.Index);
}
}
}