This repository has been archived by the owner on Feb 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
MySqlProvider_Crud.cs
101 lines (84 loc) · 3.53 KB
/
MySqlProvider_Crud.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
using Postulate.Lite.Core;
using Postulate.Lite.Core.Attributes;
using Postulate.Lite.Core.Extensions;
using Postulate.Lite.Core.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
namespace Postulate.Lite.MySql
{
public partial class MySqlProvider<TKey> : CommandProvider<TKey>
{
public MySqlProvider(Func<object, TKey> identityConverter) : base(identityConverter, new MySqlIntegrator(), "auto_increment")
{
}
protected override string ApplyDelimiter(string name)
{
return string.Join(".", name.Split('.').Select(s => $"`{s}`"));
}
protected override string FindCommand<T>(string whereClause, string tableName = null)
{
var type = typeof(T);
var props = _integrator.GetMappedColumns(type);
var columns = props.Select(pi => new ColumnInfo(pi));
return $"SELECT {string.Join(", ", columns.Select(col => ApplyDelimiter(col.ColumnName)))} FROM {ApplyDelimiter(GetTableName<T>(tableName))} WHERE {whereClause}";
}
protected override string InsertCommand<T>(string tableName = null)
{
GetInsertComponents<T>(out string columnList, out string valueList);
return $"INSERT INTO {ApplyDelimiter(GetTableName<T>(tableName))} ({columnList}) VALUES ({valueList}); SELECT LAST_INSERT_ID()";
}
protected override string PlainInsertCommand<T>(string tableName = null)
{
string insertTable = GetTableName<T>(tableName);
GetInsertComponents<T>(out string columnList, out string valueList);
return $"INSERT INTO {ApplyDelimiter(insertTable)} ({columnList}) VALUES ({valueList});";
}
protected override string UpdateCommand<T>(string tableName = null)
{
var columns = _integrator.GetEditableColumns(typeof(T), SaveAction.Update);
return $"UPDATE {ApplyDelimiter(GetTableName<T>(tableName))} SET {string.Join(", ", columns.Select(col => $"{ApplyDelimiter(col.ColumnName)}=@{col.PropertyName}"))} WHERE {ApplyDelimiter(typeof(T).GetIdentityName())}=@id";
}
protected override string DeleteCommand<T>(string tableName = null)
{
return $"DELETE FROM {ApplyDelimiter(GetTableName<T>(tableName))} WHERE {ApplyDelimiter(typeof(T).GetIdentityName())}=@id";
}
protected override string SqlColumnSyntax(PropertyInfo propertyInfo, bool isIdentity)
{
ColumnInfo col = new ColumnInfo(propertyInfo);
string result = ApplyDelimiter(col.ColumnName);
var calcAttr = propertyInfo.GetCustomAttribute<CalculatedAttribute>();
if (calcAttr != null)
{
result += $" AS {calcAttr.Expression}";
}
else
{
string nullSyntax = (col.AllowNull) ? "NULL" : "NOT NULL";
var typeMap = _integrator.SupportedTypes(col.Length, col.Precision, col.Scale);
Type t = propertyInfo.GetMappedType();
if (!typeMap.ContainsKey(t)) throw new KeyNotFoundException($"Type name {t.Name} not supported.");
string dataType = (col.HasExplicitType()) ?
col.DataType :
typeMap[t].FormattedName;
if (isIdentity) dataType += " " + IdentityColumnSyntax();
result += $" {dataType} {nullSyntax}";
}
return result;
}
protected override string SqlSelectNextVersion(string tableName)
{
return $"SELECT MAX(`NextVersion`) FROM {tableName} WHERE `RecordId`=@id";
}
protected override string SqlUpdateNextVersion(string tableName)
{
return $"UPDATE {ApplyDelimiter(tableName)} SET `NextVersion`=`NextVersion`+1 WHERE `RecordId`=@id";
}
protected override string SqlInsertRowVersion(string tableName)
{
return $"INSERT INTO {ApplyDelimiter(tableName)} (`RecordId`, `NextVersion`) VALUES (@recordId, @nextVersion)";
}
}
}