-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDatabase.cs
More file actions
200 lines (189 loc) · 6.24 KB
/
Database.cs
File metadata and controls
200 lines (189 loc) · 6.24 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
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
namespace UCIS {
public class Database {
public delegate IDbConnection ConnectionConstructorDelegate();
private ConnectionConstructorDelegate ConnectionConstructor;
public string ConnectionString { get; private set; }
public Database(Type connectionType, String connectionString) {
this.ConnectionString = connectionString;
this.ConnectionConstructor = delegate() { return (IDbConnection)Activator.CreateInstance(connectionType); };
}
public Database(DbProviderFactory factory, String connectionString) {
this.ConnectionString = connectionString;
this.ConnectionConstructor = factory.CreateConnection;
}
public Database(ConnectionConstructorDelegate constructor, String connectionString) {
this.ConnectionString = connectionString;
this.ConnectionConstructor = constructor;
}
public virtual IDbConnection GetConnection() {
IDbConnection conn = ConnectionConstructor();
conn.ConnectionString = ConnectionString;
conn.Open();
return conn;
}
private static IDbCommand PrepareQuery(IDbConnection connection, String query, params Object[] parameters) {
IDbCommand command = connection.CreateCommand();
try {
command.CommandType = CommandType.Text;
command.CommandText = query;
command.Parameters.Clear();
int index = 0;
foreach (Object parameter in parameters) {
IDbDataParameter dbparameter = command.CreateParameter();
dbparameter.Direction = ParameterDirection.Input;
dbparameter.ParameterName = "?" + index.ToString();
dbparameter.Value = parameter;
command.Parameters.Add(dbparameter);
index++;
}
if (index > 0) command.Prepare();
} catch {
command.Dispose();
throw;
}
return command;
}
public IDbCommand PrepareQuery(String query, params Object[] parameters) {
IDbConnection connection = GetConnection();
try {
return PrepareQuery(connection, query, parameters);
} catch {
connection.Close();
throw;
}
}
public int NonQuery(String query, params Object[] parameters) {
using (IDbConnection connection = GetConnection()) {
using (IDbCommand command = PrepareQuery(connection, query, parameters)) {
return command.ExecuteNonQuery();
}
}
}
public Object FetchField(String query, params Object[] parameters) {
using (IDbConnection connection = GetConnection()) {
using (IDbCommand command = PrepareQuery(connection, query, parameters)) {
return command.ExecuteScalar();
}
}
}
public Object[] FetchRow(String query, params Object[] parameters) {
using (IDbConnection connection = GetConnection()) {
using (IDbCommand command = PrepareQuery(connection, query, parameters)) {
using (IDataReader reader = command.ExecuteReader()) {
if (!reader.Read()) return null;
Object[] result = new Object[reader.FieldCount];
reader.GetValues(result);
return result;
}
}
}
}
public Object[][] FetchRows(String query, params Object[] parameters) {
using (IDbConnection connection = GetConnection()) {
using (IDbCommand command = PrepareQuery(connection, query, parameters)) {
using (IDataReader reader = command.ExecuteReader()) {
List<Object[]> result = new List<Object[]>();
while (reader.Read()) {
Object[] resultarray = new Object[reader.FieldCount];
reader.GetValues(resultarray);
result.Add(resultarray);
}
return result.ToArray();
}
}
}
}
public void ForEachRow(Action<Object[]> f, String query, params Object[] parameters) {
using (IDbConnection connection = GetConnection()) {
using (IDbCommand command = PrepareQuery(connection, query, parameters)) {
using (IDataReader reader = command.ExecuteReader()) {
while (reader.Read()) {
Object[] resultarray = new Object[reader.FieldCount];
reader.GetValues(resultarray);
f(resultarray);
}
}
}
}
}
public void ForEachRow(Action<IDataRecord> callback, String query, params Object[] parameters) {
using (IDbConnection connection = GetConnection()) {
using (IDbCommand command = PrepareQuery(connection, query, parameters)) {
using (IDataReader reader = command.ExecuteReader()) {
while (reader.Read()) callback(reader);
}
}
}
}
public IDataReader ExecuteReader(String query, params Object[] parameters) {
IDbConnection connection = GetConnection();
try {
using (IDbCommand command = PrepareQuery(connection, query, parameters)) {
return command.ExecuteReader(CommandBehavior.CloseConnection);
}
} catch {
connection.Dispose();
throw;
}
}
public IEnumerable<IDataRecord> EnumerateRows(String query, params Object[] parameters) {
IDbConnection connection = GetConnection();
try {
return new DataEnumerator(PrepareQuery(connection, query, parameters));
} catch {
connection.Dispose();
throw;
}
}
class DataEnumerator : IEnumerable<IDataRecord>, IEnumerator<IDataRecord> {
IDbCommand command = null;
IDataReader reader = null;
public DataEnumerator(IDbCommand command) {
this.command = command;
try {
this.reader = command.ExecuteReader();
} catch {
Dispose();
throw;
}
}
IEnumerator<IDataRecord> IEnumerable<IDataRecord>.GetEnumerator() {
return this;
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
return this;
}
public IDataRecord Current {
get { return reader; }
}
object System.Collections.IEnumerator.Current {
get { return reader; }
}
public bool MoveNext() {
return reader.Read();
}
public void Reset() {
throw new NotSupportedException();
}
public Object[] CurrentRow {
get {
object[] array = new object[reader.FieldCount];
reader.GetValues(array);
return array;
}
}
public void Dispose() {
if (reader != null) reader.Dispose();
if (command != null) {
IDbConnection connection = command.Connection;
command.Dispose();
connection.Dispose();
}
}
}
}
}