Repository navigation
Expand file tree
/
Copy pathSqlGenerator.cs
More file actions
216 lines (179 loc) · 8.28 KB
/
Copy pathSqlGenerator.cs
File metadata and controls
216 lines (179 loc) · 8.28 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DapperExtensions.Mapper;
namespace DapperExtensions.Sql
{
public interface ISqlGenerator
{
IDapperExtensionsConfiguration Configuration { get; }
string Select(IClassMapper classMap, IPredicate predicate, IList<ISort> sort, IDictionary<string, object> parameters);
string SelectPaged(IClassMapper classMap, IPredicate predicate, IList<ISort> sort, int page, int resultsPerPage, IDictionary<string, object> parameters);
string Count(IClassMapper classMap, IPredicate predicate, IDictionary<string, object> parameters);
string Insert(IClassMapper classMap);
string Update(IClassMapper classMap, IPredicate predicate, IDictionary<string, object> parameters);
string Delete(IClassMapper classMap, IPredicate predicate, IDictionary<string, object> parameters);
string IdentitySql(IClassMapper classMap);
string GetTableName(IClassMapper map);
string GetColumnName(IClassMapper map, IPropertyMap property, bool includeAlias);
string GetColumnName(IClassMapper map, string propertyName, bool includeAlias);
bool SupportsMultipleStatements();
}
public class SqlGeneratorImpl : ISqlGenerator
{
public SqlGeneratorImpl(IDapperExtensionsConfiguration configuration)
{
Configuration = configuration;
}
public IDapperExtensionsConfiguration Configuration { get; private set; }
public virtual string Select(IClassMapper classMap, IPredicate predicate, IList<ISort> sort, IDictionary<string, object> parameters)
{
if (parameters == null)
{
throw new ArgumentNullException("Parameters");
}
StringBuilder sql = new StringBuilder(string.Format("SELECT {0} FROM {1}",
BuildSelectColumns(classMap),
GetTableName(classMap)));
if (predicate != null)
{
sql.Append(" WHERE ")
.Append(predicate.GetSql(this, parameters));
}
if (sort != null && sort.Any())
{
sql.Append(" ORDER BY ")
.Append(sort.Select(s => GetColumnName(classMap, s.PropertyName, false) + (s.Ascending ? " ASC" : " DESC")).AppendStrings());
}
return sql.ToString();
}
public virtual string SelectPaged(IClassMapper classMap, IPredicate predicate, IList<ISort> sort, int page, int resultsPerPage, IDictionary<string, object> parameters)
{
if (sort == null || !sort.Any())
{
throw new ArgumentNullException("Sort", "Sort cannot be null or empty.");
}
if (parameters == null)
{
throw new ArgumentNullException("Parameters");
}
StringBuilder innerSql = new StringBuilder(string.Format("SELECT {0} FROM {1}",
BuildSelectColumns(classMap),
GetTableName(classMap)));
if (predicate != null)
{
innerSql.Append(" WHERE ")
.Append(predicate.GetSql(this, parameters));
}
string orderBy = sort.Select(s => GetColumnName(classMap, s.PropertyName, false) + (s.Ascending ? " ASC" : " DESC")).AppendStrings();
innerSql.Append(" ORDER BY " + orderBy);
string sql = Configuration.Dialect.GetPagingSql(innerSql.ToString(), page, resultsPerPage, parameters);
return sql;
}
public virtual string Count(IClassMapper classMap, IPredicate predicate, IDictionary<string, object> parameters)
{
if (parameters == null)
{
throw new ArgumentNullException("Parameters");
}
StringBuilder sql = new StringBuilder(string.Format("SELECT COUNT(*) AS {0}Total{1} FROM {2}",
Configuration.Dialect.OpenQuote,
Configuration.Dialect.CloseQuote,
GetTableName(classMap)));
if (predicate != null)
{
sql.Append(" WHERE ")
.Append(predicate.GetSql(this, parameters));
}
return sql.ToString();
}
public virtual string Insert(IClassMapper classMap)
{
var columns = classMap.Properties.Where(p => !(p.Ignored || p.IsReadOnly || p.KeyType == KeyType.Identity));
if (!columns.Any())
{
throw new ArgumentException("No columns were mapped.");
}
var columnNames = columns.Select(p => GetColumnName(classMap, p, false));
var parameters = columns.Select(p => "@" + p.Name);
string sql = string.Format("INSERT INTO {0} ({1}) VALUES ({2})",
GetTableName(classMap),
columnNames.AppendStrings(),
parameters.AppendStrings());
return sql;
}
public virtual string Update(IClassMapper classMap, IPredicate predicate, IDictionary<string, object> parameters)
{
if (predicate == null)
{
throw new ArgumentNullException("Predicate");
}
if (parameters == null)
{
throw new ArgumentNullException("Parameters");
}
var columns = classMap.Properties.Where(p => !(p.Ignored || p.IsReadOnly || p.KeyType == KeyType.Identity));
if (!columns.Any())
{
throw new ArgumentException("No columns were mapped.");
}
var setSql = columns.Select(p => GetColumnName(classMap, p, false) + " = @" + p.Name);
return string.Format("UPDATE {0} SET {1} WHERE {2}",
GetTableName(classMap),
setSql.AppendStrings(),
predicate.GetSql(this, parameters));
}
public virtual string Delete(IClassMapper classMap, IPredicate predicate, IDictionary<string, object> parameters)
{
if (predicate == null)
{
throw new ArgumentNullException("Predicate");
}
if (parameters == null)
{
throw new ArgumentNullException("Parameters");
}
StringBuilder sql = new StringBuilder(string.Format("DELETE FROM {0}", GetTableName(classMap)));
sql.Append(" WHERE ").Append(predicate.GetSql(this, parameters));
return sql.ToString();
}
public virtual string IdentitySql(IClassMapper classMap)
{
return Configuration.Dialect.GetIdentitySql(GetTableName(classMap));
}
public virtual string GetTableName(IClassMapper map)
{
return Configuration.Dialect.GetTableName(map.SchemaName, map.TableName, null);
}
public virtual string GetColumnName(IClassMapper map, IPropertyMap property, bool includeAlias)
{
string alias = null;
if (property.ColumnName != property.Name && includeAlias)
{
alias = property.Name;
}
return Configuration.Dialect.GetColumnName(GetTableName(map), property.ColumnName, alias);
}
public virtual string GetColumnName(IClassMapper map, string propertyName, bool includeAlias)
{
IPropertyMap propertyMap = map.Properties.SingleOrDefault(p => p.Name.Equals(propertyName, StringComparison.InvariantCultureIgnoreCase));
if (propertyMap == null)
{
throw new ArgumentException(string.Format("Could not find '{0}' in Mapping.", propertyName));
}
return GetColumnName(map, propertyMap, includeAlias);
}
public virtual bool SupportsMultipleStatements()
{
return Configuration.Dialect.SupportsMultipleStatements;
}
public virtual string BuildSelectColumns(IClassMapper classMap)
{
var columns = classMap.Properties
.Where(p => !p.Ignored)
.Select(p => GetColumnName(classMap, p, true));
return columns.AppendStrings();
}
}
}