Skip to content

Commit bea23d0

Browse files
committed
Add DM Database support.
1 parent 4ac1211 commit bea23d0

File tree

14 files changed

+314
-10
lines changed

14 files changed

+314
-10
lines changed

Database/DM/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 这是用于达梦数据库(DM8, dameng)的数据库初始化脚本
2+
3+
右键“模式”,选择导入dmp,然后添加导入文件,点确定即可。
4+
5+
## 数据库配置
6+
7+
使用达梦数据库的时候需要注意,由于达梦数据库的表名和字段名都是大写的,所以请配置表名和字段名为大写`"TableNaming": "UpperCase"`, `"ColumnNaming": "UpperCase"`
8+
9+
appsettings.json
10+
``` json
11+
{
12+
"Database": {
13+
"DbType": "DM",
14+
"ConnectionString": "Server=localhost;Port=5236;SCHEMA=ZKEACMS;User Id=SYSDBA;Password=SYSDBA001;",
15+
"TableNaming": "UpperCase",
16+
"ColumnNaming": "UpperCase"
17+
}
18+
}
19+
```

Database/DM/dump.dmp

233 KB
Binary file not shown.

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<ItemGroup>
88
<PackageVersion Include="Alipay.AopSdk.F2FPay.AspnetCore" Version="2.2.7" />
99
<PackageVersion Include="CommonMark.NET" Version="0.15.1" />
10+
<PackageVersion Include="DM.Microsoft.EntityFrameworkCore" Version="9.0.0.37033" />
1011
<PackageVersion Include="DocumentFormat.OpenXml" Version="3.3.0" />
1112
<PackageVersion Include="FastExpressionCompiler" Version="5.3.0" />
1213
<PackageVersion Include="Fluid.Core" Version="2.25.0" />

src/EasyFrameWork/Constant/DataEnumerate.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,14 @@ public enum UserType
3333
Administrator = 1,
3434
Customer = 2
3535
}
36+
public enum NameCaseStrategy
37+
{
38+
None = 0,
39+
LowerCase = 1,
40+
UpperCase = 2,
41+
CamelCase = 3, // userName
42+
PascalCase = 4, // UserName
43+
SnakeCase = 5, // user_name
44+
KebabCase = 6 // user-name
45+
}
3646
}

src/EasyFrameWork/DbTypes.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public enum DbTypes
2929
/// <summary>
3030
/// Oracle
3131
/// </summary>
32-
Oracle = 5
32+
Oracle = 5,
33+
/// <summary>
34+
/// Dameng DB
35+
/// </summary>
36+
DM = 6
3337
}
3438
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using Easy.Constant;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Easy
9+
{
10+
public class NamingConverter
11+
{
12+
public NamingConverter(NameCaseStrategy strategy)
13+
{
14+
Strategy = strategy;
15+
}
16+
17+
public NameCaseStrategy Strategy { get; set; }
18+
19+
public string Prefix { get; set; }
20+
21+
public string Suffix { get; set; }
22+
23+
public string ConvertName(string name)
24+
{
25+
if (string.IsNullOrEmpty(name)) return name;
26+
27+
var result = name;
28+
29+
result = Strategy switch
30+
{
31+
NameCaseStrategy.LowerCase => result.ToLower(),
32+
NameCaseStrategy.UpperCase => result.ToUpper(),
33+
NameCaseStrategy.CamelCase => ToCamelCase(result),
34+
NameCaseStrategy.PascalCase => ToPascalCase(result),
35+
NameCaseStrategy.SnakeCase => ToSnakeCase(result),
36+
NameCaseStrategy.KebabCase => ToKebabCase(result),
37+
_ => result
38+
};
39+
40+
if (!string.IsNullOrEmpty(Prefix)) result = Prefix + result;
41+
if (!string.IsNullOrEmpty(Suffix)) result = result + Suffix;
42+
43+
return result;
44+
}
45+
46+
private static string ToCamelCase(string name)
47+
{
48+
if (string.IsNullOrEmpty(name)) return name;
49+
return char.ToLower(name[0]) + name.Substring(1);
50+
}
51+
52+
private static string ToPascalCase(string name)
53+
{
54+
if (string.IsNullOrEmpty(name)) return name;
55+
return char.ToUpper(name[0]) + name.Substring(1);
56+
}
57+
58+
private static string ToSnakeCase(string name)
59+
{
60+
var result = new StringBuilder();
61+
result.Append(char.ToLower(name[0]));
62+
63+
for (int i = 1; i < name.Length; i++)
64+
{
65+
if (char.IsUpper(name[i]))
66+
{
67+
result.Append('_');
68+
result.Append(char.ToLower(name[i]));
69+
}
70+
else
71+
{
72+
result.Append(name[i]);
73+
}
74+
}
75+
76+
return result.ToString();
77+
}
78+
79+
private static string ToKebabCase(string name)
80+
{
81+
return ToSnakeCase(name).Replace('_', '-');
82+
}
83+
}
84+
}

src/EasyFrameWork/RepositoryPattern/DataTableAttribute.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ namespace Easy.RepositoryPattern
1111
{
1212
public class DataTableAttribute : TableAttribute
1313
{
14-
public static bool IsLowerCaseTableNames { get; set; }
15-
public DataTableAttribute(string name) : base(IsLowerCaseTableNames ? name.ToLowerInvariant() : name)
14+
public DataTableAttribute(string name) : base(name)
1615
{
1716

1817
}

src/ZKEACMS.WebHost/appsettings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
//"ConnectionString": "Data Source=App_Data/Database.sqlite"
77
//"DbType": "MySql",
88
//"ConnectionString": "Server=localhost;Database=ZKEACMS;User Id=root;Password=root;"
9+
//"DbType": "DM",
10+
//"ConnectionString": "Server=localhost;Port=5236;SCHEMA=ZKEACMS;User Id=SYSDBA;Password=SYSDBA001;",
11+
//"TableNaming": "UpperCase",
12+
//"ColumnNaming": "UpperCase"
913
},
1014
"Logging": {
1115
"LogLevel": {

src/ZKEACMS/BuilderDatabase.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public static void ConfigDatabase(this IServiceCollection services, IConfigurati
3838
services.AddScoped<DbContext>((provider) => provider.GetService<CMSDbContext>());
3939
services.AddScoped<EasyDbContext>((provider) => provider.GetService<CMSDbContext>());
4040
DatabaseOption databaseOption = configuration.GetSection("Database").Get<DatabaseOption>();
41-
DataTableAttribute.IsLowerCaseTableNames = databaseOption.IsLowerCaseTableNames;
4241
services.AddSingleton(databaseOption);
4342
#endregion
4443
}

src/ZKEACMS/CMSDbContext.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
* Copyright (c) ZKEASOFT. All rights reserved.
33
* http://www.zkea.net/licenses */
44

5-
using System.Collections.Generic;
65
using Easy;
6+
using Easy.Constant;
77
using Easy.RepositoryPattern;
88
using Microsoft.EntityFrameworkCore;
9+
using System.Collections.Generic;
910
using ZKEACMS.Common.Models;
1011
using ZKEACMS.ExtendField;
1112
using ZKEACMS.Layout;
1213
using ZKEACMS.Media;
14+
using ZKEACMS.Options;
1315
using ZKEACMS.Page;
1416
using ZKEACMS.PendingTask;
1517
using ZKEACMS.Setting;
@@ -22,9 +24,14 @@ namespace ZKEACMS
2224
{
2325
public class CMSDbContext : EasyDbContext
2426
{
25-
public CMSDbContext(DbContextOptions<CMSDbContext> dbContextOptions, IEnumerable<IOnModelCreating> modelCreatings) : base(dbContextOptions)
27+
private readonly DatabaseOption _databaseOption;
28+
public CMSDbContext(DbContextOptions<CMSDbContext> dbContextOptions,
29+
IEnumerable<IOnModelCreating> modelCreatings,
30+
DatabaseOption databaseOption)
31+
: base(dbContextOptions)
2632
{
2733
ModelCreatings = modelCreatings;
34+
_databaseOption = databaseOption;
2835
}
2936

3037
public DbSet<WidgetBasePart> WidgetBasePart { get; set; }
@@ -48,5 +55,27 @@ public CMSDbContext(DbContextOptions<CMSDbContext> dbContextOptions, IEnumerable
4855
public DbSet<StyleSheetWidget> StyleSheetWidget { get; set; }
4956
public DbSet<VideoWidget> VideoWidget { get; set; }
5057
public DbSet<BreadcrumbWidget> BreadcrumbWidget { get; set; }
58+
59+
protected override void OnModelCreating(ModelBuilder modelBuilder)
60+
{
61+
base.OnModelCreating(modelBuilder);
62+
63+
var tableNamingConverter = new NamingConverter(_databaseOption.TableNaming);
64+
var columnNamingConverter = new NamingConverter(_databaseOption.ColumnNaming);
65+
foreach (var entity in modelBuilder.Model.GetEntityTypes())
66+
{
67+
if (tableNamingConverter.Strategy != NameCaseStrategy.None)
68+
{
69+
entity.SetTableName(tableNamingConverter.ConvertName(entity.GetTableName()));
70+
}
71+
foreach (var property in entity.GetProperties())
72+
{
73+
if (columnNamingConverter.Strategy != NameCaseStrategy.None)
74+
{
75+
property.SetColumnName(columnNamingConverter.ConvertName(property.GetColumnName()));
76+
}
77+
}
78+
}
79+
}
5180
}
5281
}

0 commit comments

Comments
 (0)