Skip to content

Commit fc49052

Browse files
committed
修改验证表和字段方式
1 parent 25fe655 commit fc49052

File tree

10 files changed

+118
-71
lines changed

10 files changed

+118
-71
lines changed

Diff for: APIJSON.NET.zip

1.74 MB
Binary file not shown.

Diff for: APIJSON.NET/APIJSON.NET.Test/APIJSON.NET.Test.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="JsonApiFramework.Server" Version="1.7.0" />
109
<PackageReference Include="RestSharp" Version="106.3.1" />
1110
</ItemGroup>
1211

Diff for: APIJSON.NET/APIJSON.NET.Test/Program.cs

+34-36
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,45 @@
1-
using JsonApiFramework.Server;
2-
using RestSharp;
3-
using System;
1+
using System;
42

53
namespace APIJSON.NET.Test
64
{
75
class Program
86
{
97
static void Main(string[] args)
108
{
11-
var client = new RestClient("http://localhost:5000/");
12-
var request = new RestRequest("get", Method.POST);
13-
request.AddJsonBody(@"{
14-
'User': {
15-
'id': 38710
16-
}
17-
}
18-
");
19-
IRestResponse response = client.Execute(request);
20-
Console.WriteLine(response.Content);
21-
22-
request = new RestRequest("get", Method.POST);
23-
request.AddJsonBody(@"{
24-
'User': {
25-
'id': 38710
26-
},
27-
'[]': {
28-
'page': 0,
29-
'count': 3,
30-
'Moment': {
31-
'userId': 38710
32-
},
33-
'Comment[]': {
34-
'count': 3,
35-
'Comment': {
36-
'momentId@': '[]/Moment/id'
37-
}
38-
}
39-
}
40-
}
41-
");
42-
IRestResponse response2 = client.Execute(request);
43-
Console.WriteLine(response2.Content);
9+
//var client = new RestClient("http://localhost:5000/");
10+
//var request = new RestRequest("get", Method.POST);
11+
//request.AddJsonBody(@"{
12+
// 'User': {
13+
// 'id': 38710
14+
// }
15+
// }
16+
// ");
17+
//IRestResponse response = client.Execute(request);
18+
//Console.WriteLine(response.Content);
4419

20+
//request = new RestRequest("get", Method.POST);
21+
//request.AddJsonBody(@"{
22+
// 'User': {
23+
// 'id': 38710
24+
// },
25+
// '[]': {
26+
// 'page': 0,
27+
// 'count': 3,
28+
// 'Moment': {
29+
// 'userId': 38710
30+
// },
31+
// 'Comment[]': {
32+
// 'count': 3,
33+
// 'Comment': {
34+
// 'momentId@': '[]/Moment/id'
35+
// }
36+
// }
37+
// }
38+
//}
39+
// ");
40+
//IRestResponse response2 = client.Execute(request);
41+
//Console.WriteLine(response2.Content);
42+
4543

4644
Console.ReadLine();
4745
}

Diff for: APIJSON.NET/APIJSON.NET.sln

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ VisualStudioVersion = 15.0.27703.2035
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "APIJSON.NET", "APIJSON.NET\APIJSON.NET.csproj", "{FF647576-A104-4D54-954D-3547B4FDCDB2}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "APIJSON.NET.Test", "APIJSON.NET.Test\APIJSON.NET.Test.csproj", "{0828346E-207E-49F8-AD57-E1AB6B6E4077}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "APIJSON.NET.Test", "APIJSON.NET.Test\APIJSON.NET.Test.csproj", "{0828346E-207E-49F8-AD57-E1AB6B6E4077}"
99
EndProject
1010
Global
1111
GlobalSection(SolutionConfigurationPlatforms) = preSolution

Diff for: APIJSON.NET/APIJSON.NET/Controllers/JsonController.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public ActionResult Query([FromBody]string json)
154154
}
155155
ht.Add("func", bb);
156156
}
157-
else if (key.IsTable())
157+
else if (selectTable.IsTable(key))
158158
{
159159
var template = selectTable.GetFirstData(key, item.Value.ToString(), ht);
160160
if (template != null)

Diff for: APIJSON.NET/APIJSON.NET/Infrastructure/StringExtensions.cs

+2-18
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,7 @@
44
using System.Text.RegularExpressions;
55
public static class StringExtensions
66
{
7-
/// <summary>
8-
/// 是否合法表名(大写字母数字下划线 长度在1-15之间)
9-
/// </summary>
10-
/// <param name="str"></param>
11-
/// <returns></returns>
12-
public static bool IsTable(this string str)
13-
{
14-
return Regex.IsMatch(str, @"^[a-zA-Z][a-zA-Z0-9_]{1,15}$");
15-
}
16-
/// <summary>
17-
///
18-
/// </summary>
19-
/// <param name="str"></param>
20-
/// <returns></returns>
21-
public static bool IsField(this string str)
22-
{
23-
return Regex.IsMatch(str, @"^[a-zA-Z][a-zA-Z0-9_()]{1,15}$");
24-
}
7+
258
/// <summary>
269
/// 是否有值
2710
/// </summary>
@@ -35,5 +18,6 @@ public static string GetParamName(this string param)
3518
{
3619
return param + new Random().Next(1, 100);
3720
}
21+
3822
}
3923
}

Diff for: APIJSON.NET/APIJSON.NET/SelectTable.cs

+77-11
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,29 @@ public SelectTable(IIdentityService identityService, ITableMapper tableMapper, D
1818
_tableMapper = tableMapper;
1919
db = _db;
2020
}
21-
21+
/// <summary>
22+
/// 判断表名是否正确
23+
/// </summary>
24+
/// <param name="table"></param>
25+
/// <returns></returns>
26+
public bool IsTable(string table)
27+
{
28+
return db.Db.DbMaintenance.GetTableInfoList().Any(it => it.Name.Equals(table, StringComparison.CurrentCultureIgnoreCase));
29+
}
30+
/// <summary>
31+
/// 判断表的列名是否正确
32+
/// </summary>
33+
/// <param name="table"></param>
34+
/// <param name="col"></param>
35+
/// <returns></returns>
36+
public bool IsCol(string table, string col)
37+
{
38+
return db.Db.DbMaintenance.GetColumnInfosByTableName(table).Any(it => it.DbColumnName.Equals(table, StringComparison.CurrentCultureIgnoreCase));
39+
}
40+
2241
public (dynamic,int) GetTableData(string subtable, int page, int count, string json, JObject dd)
2342
{
24-
if (!subtable.IsTable())
43+
if (!IsTable(subtable))
2544
{
2645
throw new Exception($"表名{subtable}不正确!");
2746
}
@@ -32,6 +51,7 @@ public SelectTable(IIdentityService identityService, ITableMapper tableMapper, D
3251
}
3352
string selectrole = role.Item2;
3453
subtable = _tableMapper.GetTableName(subtable);
54+
3555
JObject values = JObject.Parse(json);
3656
page = values["page"] == null ? page : int.Parse(values["page"].ToString());
3757
count = values["count"] == null ? count : int.Parse(values["count"].ToString());
@@ -50,7 +70,7 @@ public SelectTable(IIdentityService identityService, ITableMapper tableMapper, D
5070
}
5171
public dynamic GetFirstData(string subtable, string json, JObject dd)
5272
{
53-
if (!subtable.IsTable())
73+
if (IsTable(subtable))
5474
{
5575
throw new Exception($"表名{subtable}不正确!");
5676
}
@@ -70,7 +90,6 @@ public dynamic GetFirstData(string subtable, string json, JObject dd)
7090
}
7191
private ISugarQueryable<System.Dynamic.ExpandoObject> sugarQueryable(string subtable, string selectrole, JObject values, JObject dd)
7292
{
73-
7493
var tb = db.Db.Queryable(subtable, "tb");
7594
if (values["@column"].IsValue())
7695
{
@@ -80,15 +99,15 @@ public dynamic GetFirstData(string subtable, string json, JObject dd)
8099
string[] ziduan = item.Split(":");
81100
if (ziduan.Length > 1)
82101
{
83-
if (_identitySvc.ColIsRole(ziduan[0], selectrole.Split(",")))
102+
if (IsCol(subtable,ziduan[0]) &&_identitySvc.ColIsRole(ziduan[0], selectrole.Split(",")))
84103
{
85104

86105
str.Append(ziduan[0] + " as " + ziduan[1] + ",");
87106
}
88107
}
89108
else
90109
{
91-
if (_identitySvc.ColIsRole(item, selectrole.Split(",")))
110+
if (IsCol(subtable, item) && _identitySvc.ColIsRole(item, selectrole.Split(",")))
92111
{
93112
str.Append(item + ",");
94113
}
@@ -111,7 +130,7 @@ public dynamic GetFirstData(string subtable, string json, JObject dd)
111130
string vakey = va.Key.Trim();
112131
if (vakey.EndsWith("$"))//模糊查询
113132
{
114-
if (vakey.TrimEnd('$').IsTable())
133+
if (IsCol(subtable,vakey.TrimEnd('$')))
115134
{
116135
conModels.Add(new ConditionalModel() { FieldName = vakey.TrimEnd('$'), ConditionalType = ConditionalType.Like, FieldValue = va.Value.ToString() });
117136
}
@@ -172,7 +191,7 @@ public dynamic GetFirstData(string subtable, string json, JObject dd)
172191
conModels.Add(new ConditionalModel() { FieldName = vakey.TrimEnd('@'), ConditionalType = ConditionalType.Equal, FieldValue = value });
173192

174193
}
175-
else if (vakey.IsTable()) //其他where条件
194+
else if (IsCol(subtable,vakey)) //其他where条件
176195
{
177196
conModels.Add(new ConditionalModel() { FieldName = vakey, ConditionalType = ConditionalType.Equal, FieldValue = va.Value.ToString() });
178197
}
@@ -184,7 +203,7 @@ public dynamic GetFirstData(string subtable, string json, JObject dd)
184203
{
185204
foreach (var item in values["@order"].ToString().Split(","))
186205
{
187-
if (item.Replace("-", "").IsTable())
206+
if (IsCol(subtable,item.Replace("-", "")))
188207
{
189208
if (item.EndsWith("-"))
190209
{
@@ -203,7 +222,7 @@ public dynamic GetFirstData(string subtable, string json, JObject dd)
203222
var str = new System.Text.StringBuilder(100);
204223
foreach (var and in values["@group"].ToString().Split(','))
205224
{
206-
if (and.IsField())
225+
if (IsCol(subtable, and))
207226
{
208227
str.Append(and + ",");
209228
}
@@ -212,7 +231,54 @@ public dynamic GetFirstData(string subtable, string json, JObject dd)
212231
}
213232
if (values["@having"].IsValue())
214233
{
215-
tb.Having($"{values["@having"].ToString()}");
234+
List<IConditionalModel> hw = new List<IConditionalModel>();
235+
JArray jArray = JArray.Parse(values["@having"].ToString());
236+
foreach (var item in jArray)
237+
{
238+
string and = item.ToString();
239+
var model = new ConditionalModel();
240+
if (and.Contains(">="))
241+
{
242+
model.FieldName = and.Split(new string[] { ">=" }, StringSplitOptions.RemoveEmptyEntries)[0];
243+
model.ConditionalType = ConditionalType.GreaterThanOrEqual;
244+
model.FieldValue = and.Split(new string[] { ">=" }, StringSplitOptions.RemoveEmptyEntries)[1];
245+
}
246+
else if (and.Contains("<="))
247+
{
248+
249+
model.FieldName = and.Split(new string[] { "<=" }, StringSplitOptions.RemoveEmptyEntries)[0];
250+
model.ConditionalType = ConditionalType.LessThanOrEqual;
251+
model.FieldValue = and.Split(new string[] { "<=" }, StringSplitOptions.RemoveEmptyEntries)[1];
252+
}
253+
else if (and.Contains(">"))
254+
{
255+
model.FieldName = and.Split(new string[] { ">" }, StringSplitOptions.RemoveEmptyEntries)[0];
256+
model.ConditionalType = ConditionalType.GreaterThan;
257+
model.FieldValue = and.Split(new string[] { ">" }, StringSplitOptions.RemoveEmptyEntries)[1];
258+
}
259+
else if (and.Contains("<"))
260+
{
261+
model.FieldName = and.Split(new string[] { "<" }, StringSplitOptions.RemoveEmptyEntries)[0];
262+
model.ConditionalType = ConditionalType.LessThan;
263+
model.FieldValue = and.Split(new string[] { "<" }, StringSplitOptions.RemoveEmptyEntries)[1];
264+
}
265+
else if (and.Contains("!="))
266+
{
267+
model.FieldName = and.Split(new string[] { "!=" }, StringSplitOptions.RemoveEmptyEntries)[0];
268+
model.ConditionalType = ConditionalType.NoEqual;
269+
model.FieldValue = and.Split(new string[] { "!=" }, StringSplitOptions.RemoveEmptyEntries)[1];
270+
}
271+
else if (and.Contains("="))
272+
{
273+
model.FieldName = and.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries)[0];
274+
model.ConditionalType = ConditionalType.Equal;
275+
model.FieldValue = and.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries)[1];
276+
}
277+
hw.Add(model);
278+
}
279+
280+
var d=db.Db.Context.Utilities.ConditionalModelToSql(hw);
281+
tb.Having(d.Key,d.Value);
216282
}
217283
return tb;
218284
}

Diff for: APIJSON.NET/APIJSON.NET/Services/IdentityService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public Role GetRole()
4848
var role = GetRole();
4949
if (role == null || role.Select == null || role.Select.Table == null)
5050
{
51-
return (false, $"select.json权限配置不正确!");
51+
return (false, $"appsettings.json权限配置不正确!");
5252
}
5353
string tablerole = role.Select.Table.FirstOrDefault(it => it.Equals(table, StringComparison.CurrentCultureIgnoreCase));
5454

Diff for: APIJSON.NET/APIJSON.NET/appsettings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"ConnectionStrings": {
33
"DbType": 0, //0:MySql,1:SqlServer,2:Sqlite
44
//"ConnectionString": "Server=liaozengbo\\sql2018; Database=test; User Id=sa;Password=sa123;",
5-
"ConnectionString": "Server=localhost; Database=test; User Id=root;Password=password;charset=UTF8;"
5+
"ConnectionString": "Server=192.168.0.133; Database=test; User Id=root;Password=password;charset=UTF8;"
66
},
77
"Authentication": {
88
"JwtBearer": {

Diff for: APIJSON.NET/APIJSON.NET/wwwroot/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<option value="add">add</option>
3636
<option value="edit">edit</option>
3737
<option value="remove">remove</option>
38-
<option value="getmethod">method</option>
38+
3939
</select>
4040
<button @click="hpost()">发送请求</button>
4141
</div>

0 commit comments

Comments
 (0)