forked from cyq1162/cyqdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBFast.cs
213 lines (209 loc) · 6.94 KB
/
DBFast.cs
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
using System;
using System.Collections.Generic;
using System.Text;
using CYQ.Data.Table;
namespace CYQ.Data.Orm
{
/// <summary>
/// 快速操作操作类。
/// </summary>
public static class DBFast
{
/// <summary>
/// 查找单条记录
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="where">条件</param>
/// <param name="columns">指定查询的列(可选)</param>
/// <returns></returns>
public static T Find<T>(object where, params string[] columns)
{
T result = default(T);
MDataRow row = null;
using (MAction action = GetMAction<T>())
{
if (columns != null && columns.Length > 0)
{
action.SetSelectColumns(columns);
}
if (action.Fill(where))
{
row = action.Data;
}
}
if (row != null)
{
result = row.ToEntity<T>();
}
return result;
}
public static List<T> Select<T>()
{
int count;
return Select<T>(0, 0, null, out count, null);
}
/// <summary>
/// 列表查询
/// </summary>
/// <param name="where">查询条件[可附带 order by 语句]</param>
/// <returns></returns>
public static List<T> Select<T>(string where, params string[] columns)
{
int count;
return Select<T>(0, 0, where, out count, columns);
}
/// <summary>
/// 列表查询
/// </summary>
/// <param name="topN">查询几条</param>
/// <param name="where">查询条件[可附带 order by 语句]</param>
/// <returns></returns>
public static List<T> Select<T>(int topN, string where, params string[] columns)
{
int count;
return Select<T>(1, topN, where, out count, columns);
}
public static List<T> Select<T>(int pageIndex, int pageSize, params string[] columns)
{
int count;
return Select<T>(pageIndex, pageSize, null, out count, columns);
}
public static List<T> Select<T>(int pageIndex, int pageSize, string where, params string[] columns)
{
int count;
return Select<T>(pageIndex, pageSize, where, out count, columns);
}
/// <summary>
/// 查找多条记录
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="pageIndex">第N页</param>
/// <param name="pageSize">每页N条</param>
/// <param name="where">条件</param>
/// <param name="count">返回记录总数</param>
/// <param name="columns">指定查询的列(可选)</param>
/// <returns></returns>
public static List<T> Select<T>(int pageIndex, int pageSize, object where, out int count, params string[] columns)
{
MDataTable dt = null;
using (MAction action = GetMAction<T>())
{
if (columns != null && columns.Length > 0)
{
action.SetSelectColumns(columns);
}
dt = action.Select(pageIndex, pageSize, where, out count);
}
return dt.ToList<T>();
}
/// <summary>
/// 删除记录
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="where">条件</param>
/// <returns></returns>
public static bool Delete<T>(object where)
{
using (MAction action = GetMAction<T>())
{
return action.Delete(where);
}
}
public static bool Insert<T>(T t)
{
return Insert<T>(t, InsertOp.ID, false);
}
public static bool Insert<T>(T t, InsertOp op)
{
return Insert<T>(t, op, false);
}
/// <summary>
/// 添加一条记录
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="t">实体对象</param>
/// <returns></returns>
public static bool Insert<T>(T t, InsertOp op, bool insertID)
{
bool result = false;
MDataRow row = null;
using (MAction action = GetMAction<T>())
{
action.AllowInsertID = insertID;
action.Data.LoadFrom(t, BreakOp.Null);
result = action.Insert(op);
if (result && op != InsertOp.None)
{
row = action.Data;
}
}
if (row != null)
{
row.SetToEntity(t);
}
return result;
}
public static bool Update<T>(T t)
{
return Update<T>(t, null);
}
/// <summary>
/// 更新记录
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="t">实体对象</param>
/// <param name="where">条件</param>
/// <returns></returns>
public static bool Update<T>(T t, object where)
{
using (MAction action = GetMAction<T>())
{
action.Data.LoadFrom(t, BreakOp.Null);
return action.Update(where);
}
}
/// <summary>
/// 是否存在指定的条件
/// </summary>
public static bool Exists<T>(object where)
{
using (MAction action = GetMAction<T>())
{
return action.Exists(where);
}
}
public static int GetCount<T>(object where)
{
using (MAction action = GetMAction<T>())
{
return action.GetCount(where);
}
}
private static MAction GetMAction<T>()
{
string conn = string.Empty;
MAction action = new MAction(GetTableName<T>(out conn), conn);
//action.SetAopState(CYQ.Data.Aop.AopOp.CloseAll);
return action;
}
private static string GetTableName<T>(out string conn)
{
conn = string.Empty;
Type t = typeof(T);
string[] items = t.FullName.Split('.');
if (items.Length > 1)
{
conn = items[items.Length - 2] + "Conn";
items = null;
}
string tName = t.Name;
t = null;
if (tName.EndsWith(AppConfig.EntitySuffix))
{
tName = tName.Substring(0, tName.Length - AppConfig.EntitySuffix.Length);
}
tName = CYQ.Data.Tool.DBTool.GetMapTableName(conn, tName);
return tName;
}
}
}