|
6 | 6 | var project = Model.Project;
|
7 | 7 | var buildTask = Model.Build;
|
8 | 8 | var table = Model.GetCurrentTable();
|
9 |
| - var primaryKeyType = table.PKColumn?.LanguageType ?? "int"; |
| 9 | + var pkCol = table.PKColumn; |
| 10 | + if (pkCol == null) |
| 11 | + { |
| 12 | + throw new SmartCodeException($"Repository Template can not find PKColumn,Table:{table.Name}."); |
| 13 | + } |
| 14 | + var primaryKeyType = pkCol.LanguageType ?? "int"; |
10 | 15 | var autoIncrement = table.AutoIncrement;
|
11 | 16 | var dbSource = Model.GetDataSource<DbSource>();
|
| 17 | + |
| 18 | + var PKNameEQId = pkCol.Name == "Id"; |
12 | 19 | }
|
13 | 20 |
|
14 | 21 | using System;
|
15 | 22 | using System.Collections.Generic;
|
16 | 23 | using System.Data;
|
17 | 24 | using System.Text;
|
| 25 | +using System.Threading.Tasks; |
18 | 26 | using SmartSql.DyRepository;
|
19 | 27 | using SmartSql.DyRepository.Annotations;
|
20 | 28 | using @(project.Module).Entity;
|
21 | 29 |
|
22 | 30 | namespace @(project.Module).@buildTask.Module
|
23 | 31 | {
|
24 |
| - public interface I@(table.ConvertedName)Repository : IRepository<@(table.ConvertedName), @(primaryKeyType)> |
| 32 | +public interface I@(table.ConvertedName)Repository : IRepository<@(table.ConvertedName), @(primaryKeyType)> |
| 33 | + ,IRepositoryAsync<@(table.ConvertedName), @(primaryKeyType)> |
25 | 34 | {
|
26 | 35 | @if (dbSource.DbRepository.DbProvider == DbProvider.PostgreSql
|
27 | 36 | && autoIncrement)
|
|
30 | 39 | @Html.NewLine()
|
31 | 40 | @Html.PadLeft(8) <text>new @primaryKeyType Insert(@table.ConvertedName entity);</text>
|
32 | 41 | @Html.NewLine()
|
| 42 | + |
| 43 | + <text>[Statement(Execute = ExecuteBehavior.QuerySingle)]</text> |
| 44 | + @Html.NewLine() |
| 45 | + @Html.PadLeft(8) <text>new Task<@primaryKeyType> InsertAsync(@table.ConvertedName entity);</text> |
| 46 | + @Html.NewLine() |
33 | 47 | }
|
34 | 48 | else if (autoIncrement && primaryKeyType != "int")
|
35 | 49 | {
|
36 | 50 | @Html.PadLeft(8) <text>new @primaryKeyType Insert(@table.ConvertedName entity);</text>
|
37 | 51 | @Html.NewLine()
|
| 52 | + |
| 53 | + @Html.PadLeft(8) <text>new Task<@primaryKeyType> InsertAsync(@table.ConvertedName entity);</text> |
| 54 | + @Html.NewLine() |
| 55 | + } |
| 56 | + |
| 57 | + @if (!PKNameEQId) |
| 58 | + { |
| 59 | + <text>[Statement(Id = "GetEntity")]</text> |
| 60 | + @Html.NewLine() |
| 61 | + @Html.PadLeft(8) <text>new @table.ConvertedName GetById([Param("@pkCol.Name")]@primaryKeyType id);</text> |
| 62 | + @Html.NewLine() |
| 63 | + |
| 64 | + <text>[Statement(Id = "GetEntity")]</text> |
| 65 | + @Html.NewLine() |
| 66 | + @Html.PadLeft(8) <text>new Task<@table.ConvertedName> GetByIdAsync([Param("@pkCol.Name")]@primaryKeyType id);</text> |
| 67 | + @Html.NewLine() |
| 68 | + |
| 69 | + <text>[Statement(Id = "Delete")]</text> |
| 70 | + @Html.NewLine() |
| 71 | + @Html.PadLeft(8) <text>new int DeleteById([Param("@pkCol.Name")]@primaryKeyType id);</text> |
| 72 | + @Html.NewLine() |
| 73 | + |
| 74 | + <text>[Statement(Id = "Delete")]</text> |
| 75 | + @Html.NewLine() |
| 76 | + @Html.PadLeft(8) <text>new Task<@("int")> DeleteByIdAsync([Param("@pkCol.Name")]@primaryKeyType id);</text> |
| 77 | + @Html.NewLine() |
| 78 | + |
38 | 79 | }
|
39 | 80 | }
|
40 | 81 | }
|
0 commit comments