Skip to content

Commit 92f7862

Browse files
committed
add OfficialRazor Templates
1 parent c4939b7 commit 92f7862

16 files changed

+147
-337
lines changed

doc/SmartCode.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ DataSource:
77
DbProvider: SqlServer
88
ConnectionString: Data Source=.;Initial Catalog=SmartSqlStarterDB;Integrated Security=True
99
Language: CSharp
10-
TemplateEngine: Razor
10+
TemplateEngine: OfficialRazor
1111
Output:
1212
Type: File
1313
Path: 'E://SmartSql-Starter'
@@ -108,7 +108,7 @@ Build:
108108

109109
SqlMap:
110110
Type: Table
111-
Template: SqlMap-SqlServer.cshtml
111+
Template: SqlMap.cshtml
112112
Output:
113113
Path: '{{Project.Module}}.API/Maps'
114114
Extension: .xml

src/SmartCode.CLI/OfficialRazorTemplates/Entity.cshtml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
var project = Model.Project;
66
var buildTask = Model.Build;
77
var table = Model.GetCurrentTable();
8-
8+
99
}
1010
@{ await Html.RenderPartialAsync("Copyright.cshtml", Model); }
1111

@@ -19,10 +19,14 @@ namespace @(project.Module)[email protected]
1919
{
2020
@foreach (var column in table.Columns)
2121
{
22-
@Html.Raw("///<summary>\r\n")
23-
@Html.Raw($"///{column.Description ?? column.ConvertedName}\r\n")
24-
@Html.Raw("///</summary>\r\n")
25-
<text>public</text> <text>@column.LanguageType</text><text>@(column.IsNullable && column.LanguageType != "string" ? "?" : "")</text> <text>@column.ConvertedName</text> <text>{ get; set; }</text>@Html.Raw("\r\n")
22+
@Html.PadLeft(8)@Html.Raw("///<summary>")
23+
@Html.PadLeft(8)@Html.NewLine()
24+
@Html.PadLeft(8)@Html.Raw($"///{column.Description ?? column.ConvertedName}")
25+
@Html.PadLeft(8)@Html.NewLine()
26+
@Html.PadLeft(8) @Html.Raw("///</summary>")
27+
@Html.PadLeft(8)@Html.NewLine()
28+
@Html.PadLeft(8)@Html.PadLeft(6)<text>public</text> <text>@column.LanguageType</text><text>@(column.IsNullable && column.LanguageType != "string" ? "?" : "")</text> <text>@column.ConvertedName</text> <text>{ get; set; }</text>
29+
@Html.PadLeft(8)@Html.NewLine()
2630
}
2731
}
2832
}

src/SmartCode.CLI/OfficialRazorTemplates/Proj-Repository.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<TargetFramework>netstandard2.0</TargetFramework>
1111
</PropertyGroup>
1212
<ItemGroup>
13-
<PackageReference Include="SmartSql.DyRepository" Version="3.6.1" />
13+
<PackageReference Include="SmartSql.DyRepository" Version="3.7.10" />
1414
</ItemGroup>
1515
<ItemGroup>
1616
<ProjectReference Include="..\@(project.Module).Entity\@(project.Module).Entity.csproj" />

src/SmartCode.CLI/OfficialRazorTemplates/Repository.cshtml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ namespace @(project.Module)[email protected]
2323
{
2424
@if (autoIncrement && primaryKeyType != "int")
2525
{
26-
<text>new @primaryKeyType Insert(@table.ConvertedName entity);</text>
26+
@Html.PadLeft(8) <text>new @primaryKeyType Insert(@table.ConvertedName entity);</text>
27+
@Html.NewLine()
2728
}
2829
}
2930
}

src/SmartCode.CLI/OfficialRazorTemplates/SqlMap-Columns.cshtml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,13 @@
44
@{
55
var project = Model.Project;
66
var dbSource = Model.GetDataSource<DbSource>();
7+
var table = Model.GetCurrentTable();
78
}
89

10+
<Statement Id="AllCols">
11+
@for (var colIndex = 0; colIndex < table.Columns.Count(); colIndex++)
12+
{
13+
var col = table.Columns.ElementAt(colIndex);
14+
@col.Name@(colIndex == table.Columns.Count() - 1 ? "" : ",")
15+
}
16+
</Statement>

src/SmartCode.CLI/OfficialRazorTemplates/SqlMap-Delete.cshtml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,11 @@
55
var project = Model.Project;
66
var dbSource = Model.GetDataSource<DbSource>();
77
var table = Model.GetCurrentTable();
8-
8+
var pkCol = table.PKColumn;
99
}
10+
11+
<!--删除-->
12+
<Statement Id="Delete">
13+
Delete From @table.Name
14+
Where @pkCol.Name=@pkCol.ConvertedName
15+
</Statement>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@using SmartCode
2+
@using SmartCode.Db
3+
@model BuildContext
4+
@{
5+
var project = Model.Project;
6+
var dbSource = Model.GetDataSource<DbSource>();
7+
var table = Model.GetCurrentTable();
8+
var pkCol = table.PKColumn;
9+
}
10+
11+
<!--获取记录数-->
12+
<Statement Id="GetRecord">
13+
Select Count(1) From @table.Name T
14+
<Include RefId="QueryParams" />
15+
</Statement>

src/SmartCode.CLI/OfficialRazorTemplates/SqlMap-Insert.cshtml

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,50 @@
1212
}
1313
var autoIncrement = table.AutoIncrement;
1414
var prefix = dbSource.DbProvider.ParameterPrefix;
15-
int colIndex = 0;
1615
}
1716

1817
<!--新增-->
1918
<Statement Id="Insert">
2019
INSERT INTO @table.Name
2120
(
22-
@{ colIndex = 0;}
23-
@foreach (var col in table.Columns)
21+
@for (var colIndex = 0; colIndex < table.Columns.Count(); colIndex++)
2422
{
23+
var col = table.Columns.ElementAt(colIndex);
2524
@if (!col.AutoIncrement)
2625
{
2726
@col.Name@(colIndex == table.Columns.Count() - 1 ? "" : ",")
27+
@Html.NewLine()
2828
}
29-
colIndex++;
3029
}
3130
)
3231
VALUES
3332
(
34-
@{ colIndex = 0;}
35-
@foreach (var col in table.Columns)
33+
@for (var colIndex = 0; colIndex < table.Columns.Count(); colIndex++)
3634
{
35+
var col = table.Columns.ElementAt(colIndex);
3736
@if (!col.AutoIncrement)
3837
{
3938
@[email protected]@(colIndex == table.Columns.Count() - 1 ? "" : ",")
39+
@Html.NewLine()
4040
}
41-
colIndex++;
4241
}
4342
);
4443
@if (autoIncrement)
4544
{
46-
<text>Select Last_Insert_Id();</text>
45+
@switch (dbSource.DbRepository.DbProvider)
46+
{
47+
case DbProvider.MariaDB:
48+
case DbProvider.MySql:
49+
{
50+
<text>Select Last_Insert_Id();</text>
51+
break;
52+
}
53+
case DbProvider.SqlServer:
54+
{
55+
<text>Select Scope_Identity();</text>
56+
break;
57+
}
58+
}
59+
@Html.NewLine()
4760
}
4861
</Statement>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@using SmartCode
2+
@using SmartCode.Db
3+
@model BuildContext
4+
@{
5+
var project = Model.Project;
6+
var dbSource = Model.GetDataSource<DbSource>();
7+
var table = Model.GetCurrentTable();
8+
var pkCol = table.PKColumn;
9+
}
10+
11+
<!--是否存在该记录-->
12+
<Statement Id="IsExist">
13+
Select Count(1) From @table.Name T
14+
<Include RefId="QueryParams" />
15+
</Statement>

src/SmartCode.CLI/OfficialRazorTemplates/SqlMap-MySql.cshtml

Lines changed: 7 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
var primaryKeyType = pkCol?.LanguageType ?? "int";
1515
var autoIncrement = table.AutoIncrement;
1616
var prefix = dbSource.DbProvider.ParameterPrefix;
17-
var colIndex = 0;
1817
var notEqCols = table.Columns.Where(m => m.ConvertedName != m.Name);
1918
var resultMapName = $"{table.ConvertedName}ResultMap";
2019
var queryStatementResultMap = table.HasColNameNotEqConvertedName ? $"ResultMap=\"{resultMapName}\"" : "";
@@ -27,79 +26,10 @@
2726
<Where>
2827
</Where>
2928
</Statement>
30-
<Statement Id="AllCols">
31-
@{ colIndex = 0;}
32-
@foreach (var col in table.Columns)
33-
{
34-
@col.Name@(colIndex == table.Columns.Count() - 1 ? "" : ",")
35-
colIndex++;
36-
}
37-
</Statement>
38-
<!--新增-->
39-
<Statement Id="Insert">
40-
INSERT INTO @table.Name
41-
(
42-
@{ colIndex = 0;}
43-
@foreach (var col in table.Columns)
44-
{
45-
@if (!col.AutoIncrement)
46-
{
47-
@col.Name@(colIndex == table.Columns.Count() - 1 ? "" : ",")
48-
}
49-
colIndex++;
50-
}
51-
)
52-
VALUES
53-
(
54-
@{ colIndex = 0;}
55-
@foreach (var col in table.Columns)
56-
{
57-
@if (!col.AutoIncrement)
58-
{
59-
@[email protected]@(colIndex == table.Columns.Count() - 1 ? "" : ",")
60-
}
61-
colIndex++;
62-
}
63-
);
64-
@if (autoIncrement)
65-
{
66-
<text>Select Last_Insert_Id();</text>
67-
}
68-
</Statement>
69-
<!--删除-->
70-
<Statement Id="Delete">
71-
Delete FROM @table.Name
72-
Where @pkCol.Name= @($"{prefix}{pkCol.ConvertedName}")
73-
</Statement>
74-
<!--更新-->
75-
<Statement Id="Update">
76-
UPDATE @table.Name
77-
SET
78-
<Dynamic>
79-
@foreach (var col in table.Columns)
80-
{
81-
if (!col.IsPrimaryKey)
82-
{
83-
<IsProperty Prepend="," Property="@col.ConvertedName">
84-
@col.Name = @($"{prefix}{col.ConvertedName}")
85-
</IsProperty>
86-
}
87-
}
88-
</Dynamic>
89-
Where @pkCol.Name=@[email protected]
90-
</Statement>
91-
<!--获取数据列-->
92-
<Statement Id="Query" @(queryStatementResultMap)>
93-
SELECT T.* From @table.Name T With(NoLock)
94-
<Include RefId="QueryParams" />
95-
Order By T.@pkCol.Name Desc
96-
<Switch Prepend="Order By" Property="OrderBy">
97-
<Default>
98-
T.@pkCol.Name Desc
99-
</Default>
100-
</Switch>
101-
<IsNotEmpty Prepend="Limit" Property="Taken">?Taken</IsNotEmpty>
102-
</Statement>
29+
@{ await Html.RenderPartialAsync("SqlMap-Insert.cshtml", Model); }
30+
@{ await Html.RenderPartialAsync("SqlMap-Delete.cshtml", Model); }
31+
@{ await Html.RenderPartialAsync("SqlMap-Update.cshtml", Model); }
32+
@{ await Html.RenderPartialAsync("SqlMap-Query.cshtml", Model); }
10333
<!--获取分页数据-->
10434
<Statement Id="QueryByPage" @(queryStatementResultMap)>
10535
Select T.* From @table.Name As T
@@ -111,11 +41,8 @@
11141
</Switch>
11242
Limit ?Offset,?PageSize
11343
</Statement>
114-
<!--获取记录数-->
115-
<Statement Id="GetRecord">
116-
Select Count(1) From @table.Name T With(NoLock)
117-
<Include RefId="QueryParams" />
118-
</Statement>
44+
@{ await Html.RenderPartialAsync("SqlMap-GetRecord.cshtml", Model); }
45+
11946
<!--获取表映射实体-->
12047
<Statement Id="GetEntity" @(queryStatementResultMap)>
12148
Select T.* From @table.Name As T
@@ -126,10 +53,6 @@
12653
</Where>
12754
Limit 1
12855
</Statement>
129-
<!--是否存在该记录-->
130-
<Statement Id="IsExist">
131-
Select Count(1) From @table.Name T With(NoLock)
132-
<Include RefId="QueryParams" />
133-
</Statement>
56+
@{ await Html.RenderPartialAsync("SqlMap-IsExist.cshtml", Model); }
13457
</Statements>
13558
</SmartSqlMap>

0 commit comments

Comments
 (0)