-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotAORM.cs
More file actions
233 lines (218 loc) · 6.49 KB
/
Copy pathNotAORM.cs
File metadata and controls
233 lines (218 loc) · 6.49 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Threading;
namespace NotAORM
{
[AttributeUsage(AttributeTargets.Class)]
public class Database : Attribute
{
public string DbConnectionString { get; set; }
}
public class NotAORMBase<T>
{
private readonly SqlConnection _sqlConnection;
public SqlConnection Intance
{
get
{
return _sqlConnection;
}
}
public NotAORMBase()
{
string dbConnectionString = null;
try
{
dbConnectionString = (Attribute.GetCustomAttributes(typeof(T)).Where((val) => (val as Database) != null).First() as Database).DbConnectionString;
_sqlConnection = new SqlConnection(dbConnectionString);
#if DEBUG
_sqlConnection.InfoMessage += delegate (object sender, SqlInfoMessageEventArgs e)
{
Console.WriteLine("DB-LOGGER: {0}", e.Message);
};
#endif
}
catch (Exception ex)
{
throw new Exception("NotAORMBase - Creating Intance of DatabaseConnection", ex);
}
}
private void CloseOnCommandExecution(SqlConnection conn)
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
Thread.Sleep(10);
conn.Open();
}
else if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
}
private void ExecuteSql(Action<SqlDataAdapter> Execute, string query, CommandType type = CommandType.Text, List<SqlParameter> parameters = null)
{
bool active = false;
try
{
CloseOnCommandExecution(_sqlConnection);
active = true;
using (SqlCommand cmd = _sqlConnection.CreateCommand())
{
cmd.CommandType = type;
cmd.CommandText = query;
if (parameters != null)
{
cmd.Parameters.AddRange(parameters.ToArray());
}
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
Execute(adapter);
cmd.Parameters.Clear();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (active) _sqlConnection.Close();
}
}
private PropertyInfo GetPropInfo<TA>(TA t, string name)
{
return t.GetType().GetProperty(name);
}
private void AddValue<TA>(TA t, string Name, dynamic Value, Type TypeOf)
{
var prop = GetPropInfo(t, Name);
if (prop != null && prop.CanWrite)
{
prop.SetValue(t, TypeOf.IsInstanceOfType(DBNull.Value) ? null : Value, null);
}
}
public void Raw(Action<SqlDataReader> Execute, string Query, CommandType type = CommandType.Text, List<SqlParameter> parameters = null)
{
bool active = false;
try
{
CloseOnCommandExecution(_sqlConnection);
active = true;
using (SqlCommand cmd = _sqlConnection.CreateCommand())
{
cmd.CommandType = type;
cmd.CommandText = Query;
if (parameters != null)
{
cmd.Parameters.AddRange(parameters.ToArray());
}
SqlDataReader reader = cmd.ExecuteReader();
Execute(reader);
reader.Close();
cmd.Parameters.Clear();
}
}
catch (Exception ex)
{
throw new Exception("Raw - SqlDataReader", ex);
}
finally
{
if (active) _sqlConnection.Close();
}
}
public TA Raw<TA>(string Query, bool isDataTable, CommandType type = CommandType.Text, List<SqlParameter> parameters = null)
{
DataTable dt = new DataTable();
DataSet ds = new DataSet();
try
{
ExecuteSql((adapter) =>
{
if (isDataTable) adapter.Fill(dt);
else adapter.Fill(ds);
}, Query, type, parameters);
}
catch (Exception ex)
{
string msg = isDataTable ? "DataTable" : "DataSet";
throw new Exception($"Raw - {msg}", ex);
}
return isDataTable ? (TA) Convert.ChangeType(dt, typeof(TA)) : (TA) Convert.ChangeType(ds, typeof(TA));
}
public TA Raw<TA>(string Query, CommandType type = CommandType.Text, List<SqlParameter> parameters = null)
{
TA result = Activator.CreateInstance<TA>();
bool isList = typeof(TA).IsGenericType && typeof(TA).GetGenericTypeDefinition() == typeof(List<>);
IList list = isList ? (IList)Activator.CreateInstance(typeof(TA)) : null;
try
{
ExecuteSql((adapter) =>
{
DataTable dt = new DataTable();
adapter.Fill(dt);
foreach (DataRow row in dt.Rows)
{
if (isList)
{
Type itemType = typeof(TA).GetGenericArguments()[0];
object item = Activator.CreateInstance(itemType);
foreach (DataColumn col in dt.Columns)
{
AddValue(item, col.ColumnName, row[col.ColumnName], row[col.ColumnName].GetType());
}
list.Add(item);
}
else
{
foreach (DataColumn col in dt.Columns)
{
AddValue(result, col.ColumnName, row[col.ColumnName], row[col.ColumnName].GetType());
}
}
}
}, Query, type, parameters);
}
catch (Exception ex)
{
throw new Exception("An error occurred while executing Raw<TA>", ex);
}
return isList ? (TA)list : result;
}
public int Execute(string Query, CommandType type = CommandType.Text, List<SqlParameter> parameters = null)
{
bool active = false;
int result = 0;
try
{
CloseOnCommandExecution(_sqlConnection);
active = true;
using (SqlCommand cmd = _sqlConnection.CreateCommand())
{
cmd.CommandType = type;
cmd.CommandText = Query;
if (parameters != null)
{
cmd.Parameters.AddRange(parameters.ToArray());
}
result = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
}
catch (Exception ex)
{
throw new Exception("Raw - SqlDataReader", ex);
}
finally
{
if (active) _sqlConnection.Close();
}
return result;
}
}
}