wzp
2021-07-19 e65183d31755a0e5fae4bf428435d2e0fd6afdc5
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
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
 
namespace DapperExtensions
{
    public class DapperExtCommon
    {
        /// <summary>
        /// 关键字处理[name] `name`
        /// 获取id,sex,name
        /// </summary>
        /// <param name="fieldList"></param>
        /// <param name="leftChar">左符号</param>
        /// <param name="rightChar">右符号</param>
        /// <returns></returns>
        public static string GetFieldsStr(IEnumerable<string> fieldList, string leftChar, string rightChar)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var item in fieldList)
            {
                sb.AppendFormat("{0}{1}{2}", leftChar, item, rightChar);
 
                if (item != fieldList.Last())
                {
                    sb.Append(",");
                }
            }
 
            return sb.ToString();
        }
 
        /// <summary>
        /// //获取@id,@sex,@name
        /// </summary>
        /// <param name="fieldList"></param>
        /// <returns></returns>
        public static string GetFieldsAtStr(IEnumerable<string> fieldList)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var item in fieldList)
            {
                sb.AppendFormat(":{0}", item);
 
                if (item != fieldList.Last())
                {
                    sb.Append(",");
                }
            }
            return sb.ToString();
        }
 
        public static IEnumerable GetMultiExec(object param)
        {
            return (param is IEnumerable && !(param is string || param is IEnumerable<KeyValuePair<string, object>>)) ? (IEnumerable)param : null;
        }
 
        /// <summary>
        /// 判断输入参数是否有个数,用于in判断
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static bool ObjectIsEmpty(object param)
        {
            bool result = true;
            IEnumerable data = GetMultiExec(param);
            if (data != null)
            {
                foreach (var item in data)
                {
                    result = false;
                    break;
                }
            }
            return result;
        }
 
        /// <summary>
        /// 关键字处理[name] `name`
        /// 获取id=@id,name=@name
        /// </summary>
        /// <param name="fieldList"></param>
        /// <param name="leftChar">左符号</param>
        /// <param name="rightChar">右符号</param>
        /// <returns></returns>
        public static string GetFieldsEqStr(IEnumerable<string> fieldList, string leftChar, string rightChar)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var item in fieldList)
            {
                sb.AppendFormat("{0}{1}{2}=:{1}", leftChar, item, rightChar);
 
                if (item != fieldList.Last())
                {
                    sb.Append(",");
                }
            }
            return sb.ToString();
        }
 
 
        public static DapperExtSqls GetDapperExtSqls(Type t)
        {
            DapperExtSqls dapperextsqls = new DapperExtSqls();
            var tableAttr = t.GetCustomAttributes(false).FirstOrDefault(f => f is TableAttribute) as TableAttribute;
            if (tableAttr == null)
            {
                dapperextsqls.TableName = t.Name;
            }
            else
            {
                dapperextsqls.TableName = tableAttr.Name;
            }
 
            var allproperties = t.GetProperties();
            List<PropertyInfo> exceptKeyAndComputeproperties = new List<PropertyInfo>(); //去除主键和忽略
            List<PropertyInfo> exceptComputeproperties = new List<PropertyInfo>(); //去除忽略
 
            foreach (var item in allproperties)
            {
                var attribute = item.GetCustomAttributes(false).FirstOrDefault();
                if (attribute == null)
                {
                    exceptKeyAndComputeproperties.Add(item);
                    exceptComputeproperties.Add(item);
                }
                else
                {
                    if (attribute.GetType() != typeof(ComputedAttribute))
                    {
                        if (attribute.GetType() == typeof(KeyAttribute)) //主键列
                        {
                            dapperextsqls.HasKey = true;
                            dapperextsqls.KeyName = item.Name;
                            dapperextsqls.KeyType = item.PropertyType.Name;
                            KeyAttribute keyAttr = (KeyAttribute)attribute;
                            if (keyAttr.IsIdentity)
                            {
                                dapperextsqls.IsIdentity = true;
                            }
                        }
                        else
                        {
                            exceptKeyAndComputeproperties.Add(item);
                        }
 
                        exceptComputeproperties.Add(item);
                    }
                }
            }
 
            IEnumerable<string> exceptKeyAndComputeFieldsArr = exceptKeyAndComputeproperties.Select(s => s.Name);
            IEnumerable<string> exceptComputeFieldsArr = exceptComputeproperties.Select(s => s.Name);
 
            dapperextsqls.ExceptKeyFieldList = exceptKeyAndComputeFieldsArr;
            dapperextsqls.AllFieldList = exceptComputeFieldsArr;
 
            return dapperextsqls;
        }
 
        private static readonly ConcurrentDictionary<RuntimeTypeHandle, DapperExtSqls> dapperExtsqlsDict = new ConcurrentDictionary<RuntimeTypeHandle, DapperExtSqls>();
        public static DapperExtSqls GetDapperExtSqlsCache(Type t)
        {
            if (dapperExtsqlsDict.Keys.Contains(t.TypeHandle))
            {
                return dapperExtsqlsDict[t.TypeHandle];
            }
            else
            {
                DapperExtSqls dt = GetDapperExtSqls(t);
                dapperExtsqlsDict[t.TypeHandle] = dt;
                return dt;
            }
        }
    }
}