-
Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathMassive.MySql.cs
299 lines (267 loc) · 11.5 KB
/
Massive.MySql.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
///////////////////////////////////////////////////////////////////////////////////////////////////
// Massive v2.0. MySQL specific code
///////////////////////////////////////////////////////////////////////////////////////////////////
// Licensed to you under the New BSD License
// http://www.opensource.org/licenses/bsd-license.php
// Massive is copyright (c) 2009-2017 various contributors.
// All rights reserved.
// See for sourcecode, full history and contributors list: /~https://github.com/FransBouma/Massive
//
// Redistribution and use in source and binary forms, with or without modification, are permitted
// provided that the following conditions are met:
//
// - Redistributions of source code must retain the above copyright notice, this list of conditions and the
// following disclaimer.
// - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
// the following disclaimer in the documentation and/or other materials provided with the distribution.
// - The names of its contributors may not be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
// WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
///////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Dynamic;
using System.Linq;
namespace Massive
{
/// <summary>
/// Class which provides extension methods for various ADO.NET objects.
/// </summary>
public static partial class ObjectExtensions
{
/// <summary>
/// Extension for adding single parameter.
/// </summary>
/// <param name="cmd">The command to add the parameter to.</param>
/// <param name="value">The value to add as a parameter to the command.</param>
public static void AddParam(this DbCommand cmd, object value)
{
var p = cmd.CreateParameter();
p.ParameterName = string.Format("@{0}", cmd.Parameters.Count);
if(value == null)
{
p.Value = DBNull.Value;
}
else
{
if(value is Guid)
{
p.Value = value.ToString();
p.DbType = DbType.String;
p.Size = 36;
}
else if(value is ExpandoObject)
{
var d = (IDictionary<string, object>)value;
p.Value = d.Values.FirstOrDefault();
}
else
{
p.Value = value;
}
var valueAsString = value as string;
if(valueAsString != null)
{
p.Size = valueAsString.Length > 4000 ? -1 : 4000;
}
}
cmd.Parameters.Add(p);
}
}
/// <summary>
/// A class that wraps your database table in Dynamic Funtime
/// </summary>
public partial class DynamicModel
{
#region Constants
// Mandatory constants every DB has to define.
/// <summary>
/// The default sequence name for initializing the pk sequence name value in the ctor.
/// </summary>
private const string _defaultSequenceName = "LAST_INSERT_ID()";
/// <summary>
/// Flag to signal whether the sequence retrieval call (if any) is executed before the insert query (true) or after (false). Not a const, to avoid warnings.
/// </summary>
private bool _sequenceValueCallsBeforeMainInsert = false;
#endregion
/// <summary>
/// Gets a default value for the column as defined in the schema.
/// </summary>
/// <param name="column">The column.</param>
/// <returns></returns>
private dynamic GetDefaultValue(dynamic column)
{
string defaultValue = column.COLUMN_DEFAULT;
if(string.IsNullOrEmpty(defaultValue))
{
return null;
}
dynamic result = null;
switch(defaultValue.ToUpper())
{
case "CURRENT_TIMESTAMP":
result = DateTime.Now;
break;
}
return result;
}
/// <summary>
/// Gets the aggregate function to use in a scalar query for the fragment specified
/// </summary>
/// <param name="aggregateCalled">The aggregate called on the dynamicmodel, which should be converted to a DB function. Expected to be lower case</param>
/// <returns>the aggregate function to use, or null if no aggregate function is supported for aggregateCalled</returns>
protected virtual string GetAggregateFunction(string aggregateCalled)
{
switch(aggregateCalled)
{
case "sum":
return "SUM";
case "max":
return "MAX";
case "min":
return "MIN";
case "avg":
return "AVG";
default:
return null;
}
}
/// <summary>
/// Gets the sql statement to use for obtaining the identity value of the last insert.
/// </summary>
/// <returns></returns>
protected virtual string GetIdentityRetrievalScalarStatement()
{
return string.IsNullOrEmpty(_primaryKeyFieldSequence) ? string.Empty : string.Format("SELECT {0} as newID", _primaryKeyFieldSequence);
}
/// <summary>
/// Gets the sql statement pattern for a count row query (count(*)). The pattern should include as place holders: {0} for source (FROM clause).
/// </summary>
/// <returns></returns>
protected virtual string GetCountRowQueryPattern()
{
return "SELECT COUNT(*) FROM {0} ";
}
/// <summary>
/// Gets the name of the parameter with the prefix to use in a query, e.g. @rawName or :rawName
/// </summary>
/// <param name="rawName">raw name of the parameter, without parameter prefix</param>
/// <returns>rawName prefixed with the db specific prefix (if any)</returns>
protected virtual string PrefixParameterName(string rawName)
{
return "@" + rawName;
}
/// <summary>
/// Gets the select query pattern, to use for building select queries. The pattern should include as place holders: {0} for project list, {1} for the source (FROM clause).
/// </summary>
/// <param name="limit">The limit for the resultset. 0 means no limit.</param>
/// <param name="whereClause">The where clause. Expected to have a prefix space if not empty</param>
/// <param name="orderByClause">The order by clause. Expected to have a prefix space if not empty</param>
/// <returns>
/// string pattern which is usable to build select queries.
/// </returns>
protected virtual string GetSelectQueryPattern(int limit, string whereClause, string orderByClause)
{
return string.Format("SELECT {{0}} FROM {{1}}{0}{1}{2}", whereClause, orderByClause, limit > 0 ? " LIMIT " + limit : string.Empty);
}
/// <summary>
/// Gets the insert query pattern, to use for building insert queries. The pattern should include as place holders: {0} for target, {1} for field list, {2} for parameter list
/// </summary>
/// <returns></returns>
protected virtual string GetInsertQueryPattern()
{
return "INSERT INTO {0} ({1}) VALUES ({2})";
}
/// <summary>
/// Gets the update query pattern, to use for building update queries. The pattern should include as placeholders: {0} for target, {1} for field list with sets. Has to have
/// trailing space
/// </summary>
/// <returns></returns>
protected virtual string GetUpdateQueryPattern()
{
return "UPDATE {0} SET {1} ";
}
/// <summary>
/// Gets the delete query pattern, to use for building delete queries. The pattern should include as placeholders: {0} for the target. Has to have trailing space
/// </summary>
/// <returns></returns>
protected virtual string GetDeleteQueryPattern()
{
return "DELETE FROM {0} ";
}
/// <summary>
/// Gets the name of the column using the expando object representing the column from the schema
/// </summary>
/// <param name="columnFromSchema">The column from schema in the form of an expando.</param>
/// <returns>the name of the column as defined in the schema</returns>
protected virtual string GetColumnName(dynamic columnFromSchema)
{
return columnFromSchema.COLUMN_NAME;
}
/// <summary>
/// Post-processes the query used to obtain the meta-data for the schema. If no post-processing is required, simply return a toList
/// </summary>
/// <param name="toPostProcess">To post process.</param>
/// <returns></returns>
private IEnumerable<dynamic> PostProcessSchemaQuery(IEnumerable<dynamic> toPostProcess)
{
return toPostProcess == null ? new List<dynamic>() : toPostProcess.ToList();
}
/// <summary>
/// Builds a paging query and count query pair.
/// </summary>
/// <param name="sql">The SQL statement to build the query pair for. Can be left empty, in which case the table name from the schema is used</param>
/// <param name="primaryKeyField">The primary key field. Used for ordering. If left empty the defined PK field is used</param>
/// <param name="whereClause">The where clause. Default is empty string.</param>
/// <param name="orderByClause">The order by clause. Default is empty string.</param>
/// <param name="columns">The columns to use in the project. Default is '*' (all columns, in table defined order).</param>
/// <param name="pageSize">Size of the page. Default is 20</param>
/// <param name="currentPage">The current page. 1-based. Default is 1.</param>
/// <returns>ExpandoObject with two properties: MainQuery for fetching the specified page and CountQuery for determining the total number of rows in the resultset</returns>
private dynamic BuildPagingQueryPair(string sql = "", string primaryKeyField = "", string whereClause = "", string orderByClause = "", string columns = "*", int pageSize = 20,
int currentPage = 1)
{
var orderByClauseFragment = string.IsNullOrEmpty(orderByClause) ? string.Format(" ORDER BY {0}", string.IsNullOrEmpty(primaryKeyField) ? PrimaryKeyField : primaryKeyField)
: ReadifyOrderByClause(orderByClause);
var coreQuery = string.Format(this.GetSelectQueryPattern(0, ReadifyWhereClause(whereClause), orderByClauseFragment), columns, string.IsNullOrEmpty(sql) ? this.TableName : sql);
dynamic toReturn = new ExpandoObject();
toReturn.CountQuery = string.Format("SELECT COUNT(*) FROM ({0}) q", coreQuery);
var pageStart = (currentPage - 1) * pageSize;
toReturn.MainQuery = string.Format("{0} LIMIT {1} OFFSET {2}", coreQuery, pageSize, (pageStart + pageSize));
return toReturn;
}
#region Properties
/// <summary>
/// Provides the default DbProviderFactoryName to the core to create a factory on the fly in generic code.
/// </summary>
protected virtual string DbProviderFactoryName
{
get { return "MySql.Data.MySqlClient"; }
}
/// <summary>
/// Gets the table schema query to use to obtain meta-data for a given table and schema
/// </summary>
protected virtual string TableWithSchemaQuery
{
get { return "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @0 AND TABLE_SCHEMA = @1"; }
}
/// <summary>
/// Gets the table schema query to use to obtain meta-data for a given table which is specified as the single parameter
/// </summary>
protected virtual string TableWithoutSchemaQuery
{
get { return "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @0"; }
}
#endregion
}
}