forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.cs
More file actions
206 lines (175 loc) · 8.88 KB
/
Database.cs
File metadata and controls
206 lines (175 loc) · 8.88 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Simple.Data
{
using System.Configuration;
using System.Diagnostics;
/// <summary>
/// The entry class for Simple.Data. Provides static methods for opening databases,
/// and implements runtime dynamic functionality for resolving database-level objects.
/// </summary>
public partial class Database : DataStrategy
{
private static readonly SimpleDataConfigurationSection Configuration;
private static readonly IDatabaseOpener DatabaseOpener;
private static IPluralizer _pluralizer;
private readonly Adapter _adapter;
static Database()
{
DatabaseOpener = new DatabaseOpener();
Configuration =
(SimpleDataConfigurationSection) ConfigurationManager.GetSection("simpleData/simpleDataConfiguration")
?? new SimpleDataConfigurationSection();
TraceLevel = Configuration.TraceLevel;
}
/// <summary>
/// Initializes a new instance of the <see cref="Database"/> class.
/// </summary>
/// <param name="adapter">The adapter to use for data access.</param>
internal Database(Adapter adapter)
{
_adapter = adapter;
}
public override Adapter GetAdapter()
{
return _adapter;
}
public static IDatabaseOpener Opener
{
get { return DatabaseOpener; }
}
/// <summary>
/// Gets a default instance of the Database. This connects to an ADO.NET data source
/// specified in the 'Simple.Data.Properties.Settings.ConnectionString' config ConnectionStrings setting.
/// </summary>
/// <value>The default database.</value>
public static dynamic Default
{
get { return DatabaseOpener.OpenDefault(); }
}
internal override IDictionary<string, object> FindOne(string tableName, SimpleExpression criteria)
{
return _adapter.FindOne(tableName, criteria);
}
internal override int UpdateMany(string tableName, IList<IDictionary<string, object>> dataList)
{
return _adapter.UpdateMany(tableName, dataList);
}
internal override int UpdateMany(string tableName, IList<IDictionary<string, object>> dataList, IEnumerable<string> criteriaFieldNames)
{
return _adapter.UpdateMany(tableName, dataList, criteriaFieldNames);
}
internal override int UpdateMany(string tableName, IList<IDictionary<string, object>> newValuesList, IList<IDictionary<string, object>> originalValuesList)
{
int count = 0;
for (int i = 0; i < newValuesList.Count; i++)
{
count += Update(tableName, newValuesList[i], originalValuesList[i]);
}
return count;
}
public override int Update(string tableName, IDictionary<string, object> newValuesDict, IDictionary<string, object> originalValuesDict)
{
SimpleExpression criteria = CreateCriteriaFromOriginalValues(tableName, newValuesDict, originalValuesDict);
var changedValuesDict = CreateChangedValuesDict(newValuesDict, originalValuesDict);
return _adapter.Update(tableName, changedValuesDict, criteria);
}
/// <summary>
/// Finds data from the specified "table".
/// </summary>
/// <param name="tableName">Name of the table.</param>
/// <param name="criteria">The criteria. This may be <c>null</c>, in which case all records should be returned.</param>
/// <returns>The list of records matching the criteria. If no records are found, return an empty list.</returns>
internal override IEnumerable<IDictionary<string, object>> Find(string tableName, SimpleExpression criteria)
{
return _adapter.Find(tableName, criteria);
}
/// <summary>
/// Inserts a record into the specified "table".
/// </summary><param name="tableName">Name of the table.</param><param name="data">The values to insert.</param><returns>If possible, return the newly inserted row, including any automatically-set values such as primary keys or timestamps.</returns>
internal override IDictionary<string, object> Insert(string tableName, IDictionary<string, object> data, bool resultRequired)
{
return _adapter.Insert(tableName, data, resultRequired);
}
/// <summary>
/// Inserts a record into the specified "table".
/// </summary><param name="tableName">Name of the table.</param><param name="data">The values to insert.</param><returns>If possible, return the newly inserted row, including any automatically-set values such as primary keys or timestamps.</returns>
internal override IEnumerable<IDictionary<string, object>> InsertMany(string tableName, IEnumerable<IDictionary<string, object>> data, ErrorCallback onError, bool resultRequired)
{
return _adapter.InsertMany(tableName, data, (dict, exception) => onError(new SimpleRecord(dict), exception), resultRequired);
}
/// <summary>
/// Updates the specified "table" according to specified criteria.
/// </summary><param name="tableName">Name of the table.</param><param name="data">The new values.</param><param name="criteria">The expression to use as criteria for the update operation.</param><returns>The number of records affected by the update operation.</returns>
internal override int Update(string tableName, IDictionary<string, object> data, SimpleExpression criteria)
{
return _adapter.Update(tableName, data, criteria);
}
public override IDictionary<string, object> Upsert(string tableName, IDictionary<string, object> dict, SimpleExpression criteriaExpression, bool isResultRequired)
{
return _adapter.Upsert(tableName, dict, criteriaExpression, isResultRequired);
}
public override IEnumerable<IDictionary<string, object>> UpsertMany(string tableName, IList<IDictionary<string, object>> list, bool isResultRequired, ErrorCallback errorCallback)
{
return _adapter.UpsertMany(tableName, list, isResultRequired, (dict, exception) => errorCallback(new SimpleRecord(dict), exception));
}
public override IDictionary<string, object> Get(string tableName, object[] args)
{
return _adapter.Get(tableName, args);
}
public override IEnumerable<IDictionary<string, object>> UpsertMany(string tableName, IList<IDictionary<string, object>> list, IEnumerable<string> keyFieldNames, bool isResultRequired, ErrorCallback errorCallback)
{
return _adapter.UpsertMany(tableName, list, keyFieldNames, isResultRequired, (dict, exception) => errorCallback(new SimpleRecord(dict), exception));
}
/// <summary>
/// Deletes from the specified table.
/// </summary><param name="tableName">Name of the table.</param><param name="criteria">The expression to use as criteria for the delete operation.</param><returns>The number of records which were deleted.</returns>
internal override int Delete(string tableName, SimpleExpression criteria)
{
return _adapter.Delete(tableName, criteria);
}
public SimpleTransaction BeginTransaction()
{
return SimpleTransaction.Begin(this);
}
public SimpleTransaction BeginTransaction(string name)
{
return SimpleTransaction.Begin(this, name);
}
protected internal override Database GetDatabase()
{
return this;
}
public static IPluralizer GetPluralizer()
{
return _pluralizer;
}
public static void SetPluralizer(IPluralizer pluralizer)
{
_pluralizer = pluralizer;
Extensions.StringExtensions.SetPluralizer(pluralizer);
}
public static void ClearAdapterCache()
{
DatabaseOpener.ClearAdapterCache();
}
public static void UseMockAdapter(Adapter mockAdapter)
{
Data.DatabaseOpener.UseMockAdapter(mockAdapter);
}
public static void UseMockAdapter(Func<Adapter> mockAdapterCreator)
{
Data.DatabaseOpener.UseMockAdapter(mockAdapterCreator());
}
private static TraceLevel? _traceLevel;
public static TraceLevel TraceLevel
{
get { return _traceLevel ?? Configuration.TraceLevel; }
set { _traceLevel = value; }
}
}
}