Skip to content

Commit fc204b3

Browse files
committed
2.1.10,修复插入数据时,参数位置错误可能引发的问题
1 parent 11536a4 commit fc204b3

File tree

14 files changed

+85
-122
lines changed

14 files changed

+85
-122
lines changed

.vs/MyStaging/v15/.suo

1 KB
Binary file not shown.

MyStaging.App/DAL/TablesDal.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,15 @@ protected void CreateSchema()
153153
writer.WriteLine($"\t\tpublic static {schemaName} Instance => new {schemaName}();");
154154
writer.WriteLine();
155155
// writer.WriteLine($"\t\tprivate static Dictionary<string, SchemaModel> schemas {{ get; }}");
156-
writer.WriteLine();
156+
//writer.WriteLine();
157157

158-
writer.WriteLine();
158+
//writer.WriteLine();
159159
// writer.WriteLine($"\t\tprivate static List<PropertyInfo> properties;");
160-
writer.WriteLine();
160+
// writer.WriteLine();
161161

162162
// writer.WriteLine($"\t\tstatic {schemaName}()");
163163
// writer.WriteLine("\t\t{");
164-
writer.WriteLine($"\t\tpublic Dictionary<string, SchemaModel> SchemaSet => new Dictionary<string, SchemaModel>");
164+
writer.WriteLine($"\t\tpublic List<SchemaModel> SchemaSet => new List<SchemaModel>");
165165
writer.WriteLine("\t\t\t{");
166166
for (int i = 0; i < fieldList.Count; i++)
167167
{
@@ -174,7 +174,7 @@ protected void CreateSchema()
174174
primarykey = " ,Primarykey = true";
175175
}
176176
var type = fi.PgDbType.HasValue ? $" NpgsqlDbType.{fi.PgDbType}{ap}" : "null";
177-
var line = $"{{\"{fi.Field}\", new SchemaModel{{ FieldName = \"{fi.Field}\", DbType = {type}, Size = {fi.Length}{primarykey}}} }}";
177+
var line = $"new SchemaModel{{ FieldName = \"{fi.Field}\", DbType = {type}, Size = {fi.Length}{primarykey}}}";
178178
writer.WriteLine("\t\t\t\t" + line + (i + 1 == fieldList.Count ? "" : ","));
179179
}
180180
writer.WriteLine("\t\t\t};");

MyStaging.App/MyStaging.App.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<AssemblyName>MyStaging.App</AssemblyName>
77
<ApplicationIcon />
88
<StartupObject></StartupObject>
9-
<Version>2.1.6</Version>
9+
<Version>2.1.10</Version>
1010
</PropertyGroup>
1111

1212
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

MyStaging.xUnitTest.db/DAL/Build/Post.cs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@ namespace MyStaging.xUnitTest.DAL
1616
public partial class Post : QueryContext<PostModel>
1717
{
1818
public static Post Context { get { return new Post(); } }
19-
public Post WhereRoleAny(params Et_role[] role)
20-
{
21-
if (role == null || role.Length == 0) return this;
22-
string text = JoinTo(role, "et_role");
23-
base.Where($"role @> array[{text}]");
24-
return this;
25-
}
26-
2719

2820
public static InsertBuilder<PostModel> InsertBuilder => new InsertBuilder<PostModel>(PostSchema.Instance);
2921
public static PostModel Insert(PostModel model) => InsertBuilder.Insert(model);
@@ -82,24 +74,14 @@ public PostUpdateBuilder SetContent(JToken content)
8274
base.SetField("content", NpgsqlDbType.Jsonb, content, -1);
8375
return this;
8476
}
85-
public PostUpdateBuilder SetState(Et_data_state state)
77+
public PostUpdateBuilder SetState(Et_data_state? state)
8678
{
8779
base.SetField("state", state, 4);
8880
return this;
8981
}
90-
public PostUpdateBuilder SetRole(Et_role[] role)
91-
{
92-
base.SetField("role", role, -1);
93-
return this;
94-
}
95-
public PostUpdateBuilder SetRoleAppend(Et_role role)
96-
{
97-
base.SetArrayAppend("role", role, -1);
98-
return this;
99-
}
100-
public PostUpdateBuilder SetRoleRemove(Et_role role)
82+
public PostUpdateBuilder SetRole(Et_role? role)
10183
{
102-
base.SetArrayRemove("role", role, -1);
84+
base.SetField("role", role, 4);
10385
return this;
10486
}
10587
public PostUpdateBuilder SetText(JToken text)

MyStaging.xUnitTest.db/Model/Build/PostModel.cs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,17 @@ namespace MyStaging.xUnitTest.Model
1212
[EntityMapping(name: "post", Schema = "public")]
1313
public partial class PostModel
1414
{
15-
/// <summary>
16-
/// 主键,编号
17-
/// </summary>
1815
[PrimaryKey]
1916
public Guid Id { get; set; }
2017

21-
/// <summary>
22-
/// 标题
23-
/// </summary>
2418
public string Title { get; set; }
2519

26-
/// <summary>
27-
/// 内容
28-
/// </summary>
2920
public JToken Content { get; set; }
3021

31-
/// <summary>
32-
/// 状态
33-
/// </summary>
34-
public Et_data_state State { get; set; }
22+
public Et_data_state? State { get; set; }
3523

36-
/// <summary>
37-
/// 角色
38-
/// </summary>
39-
public Et_role[] Role { get; set; }
24+
public Et_role? Role { get; set; }
4025

41-
/// <summary>
42-
/// 文本
43-
/// </summary>
4426
public JToken Text { get; set; }
4527

4628
[NonDbColumnMapping, JsonIgnore] public MyStaging.xUnitTest.DAL.Post.PostUpdateBuilder UpdateBuilder { get { return new MyStaging.xUnitTest.DAL.Post.PostUpdateBuilder(model =>{MyStaging.Helpers.MyStagingUtils.CopyProperty<PostModel>(this, model); PgSqlHelper.CacheManager?.RemoveItemCache<PostModel>(this.Id.ToString()); }, this.Id); } }

MyStaging.xUnitTest.db/Model/Schemas/ArticleSchema.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@
77

88
namespace MyStaging.xUnitTest.Model.Schemas
99
{
10-
public partial class ArticleSchema : ISchemaModel
11-
{
12-
public static ArticleSchema Instance => new ArticleSchema();
13-
public Dictionary<string, SchemaModel> SchemaSet => new Dictionary<string, SchemaModel>
14-
{
15-
{"id", new SchemaModel{ FieldName = "id", DbType = NpgsqlDbType.Varchar, Size = -1 ,Primarykey = true} },
16-
{"userid", new SchemaModel{ FieldName = "userid", DbType = NpgsqlDbType.Varchar, Size = -1 ,Primarykey = true} },
17-
{"title", new SchemaModel{ FieldName = "title", DbType = NpgsqlDbType.Varchar, Size = 255} },
18-
{"content", new SchemaModel{ FieldName = "content", DbType = NpgsqlDbType.Jsonb, Size = -1} },
19-
{"createtime", new SchemaModel{ FieldName = "createtime", DbType = NpgsqlDbType.Timestamp, Size = 8} }
20-
};
21-
public List<PropertyInfo> Properties => ContractUtils.GetProperties(typeof(ArticleModel));
10+
public partial class ArticleSchema : ISchemaModel
11+
{
12+
public static ArticleSchema Instance => new ArticleSchema();
2213

23-
}
14+
public List<SchemaModel> SchemaSet => new List<SchemaModel>
15+
{
16+
new SchemaModel{ FieldName = "id", DbType = NpgsqlDbType.Varchar, Size = -1 ,Primarykey = true},
17+
new SchemaModel{ FieldName = "userid", DbType = NpgsqlDbType.Varchar, Size = -1 ,Primarykey = true},
18+
new SchemaModel{ FieldName = "title", DbType = NpgsqlDbType.Varchar, Size = 255},
19+
new SchemaModel{ FieldName = "content", DbType = NpgsqlDbType.Jsonb, Size = -1},
20+
new SchemaModel{ FieldName = "createtime", DbType = NpgsqlDbType.Timestamp, Size = 8}
21+
};
22+
public List<PropertyInfo> Properties => ContractUtils.GetProperties(typeof(ArticleModel));
23+
24+
}
2425
}

MyStaging.xUnitTest.db/Model/Schemas/PostSchema.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,14 @@ public partial class PostSchema : ISchemaModel
1111
{
1212
public static PostSchema Instance => new PostSchema();
1313

14-
15-
16-
17-
public Dictionary<string, SchemaModel> SchemaSet => new Dictionary<string, SchemaModel>
14+
public List<SchemaModel> SchemaSet => new List<SchemaModel>
1815
{
19-
{"id", new SchemaModel{ FieldName = "id", DbType = NpgsqlDbType.Uuid, Size = 16 ,Primarykey = true} },
20-
{"title", new SchemaModel{ FieldName = "title", DbType = NpgsqlDbType.Varchar, Size = 255} },
21-
{"content", new SchemaModel{ FieldName = "content", DbType = NpgsqlDbType.Jsonb, Size = -1} },
22-
{"state", new SchemaModel{ FieldName = "state", DbType = null, Size = 4} },
23-
{"role", new SchemaModel{ FieldName = "role", DbType = null, Size = -1} },
24-
{"text", new SchemaModel{ FieldName = "text", DbType = NpgsqlDbType.Json, Size = -1} }
16+
new SchemaModel{ FieldName = "id", DbType = NpgsqlDbType.Uuid, Size = 16 ,Primarykey = true},
17+
new SchemaModel{ FieldName = "title", DbType = NpgsqlDbType.Varchar, Size = 255},
18+
new SchemaModel{ FieldName = "content", DbType = NpgsqlDbType.Jsonb, Size = -1},
19+
new SchemaModel{ FieldName = "state", DbType = null, Size = 4},
20+
new SchemaModel{ FieldName = "role", DbType = null, Size = 4},
21+
new SchemaModel{ FieldName = "text", DbType = NpgsqlDbType.Json, Size = -1}
2522
};
2623
public List<PropertyInfo> Properties => ContractUtils.GetProperties(typeof(PostModel));
2724

MyStaging.xUnitTest.db/Model/Schemas/TopicSchema.cs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,19 @@ public partial class TopicSchema : ISchemaModel
1111
{
1212
public static TopicSchema Instance => new TopicSchema();
1313

14-
15-
16-
17-
public Dictionary<string, SchemaModel> SchemaSet => new Dictionary<string, SchemaModel>
14+
public List<SchemaModel> SchemaSet => new List<SchemaModel>
1815
{
19-
{"id", new SchemaModel{ FieldName = "id", DbType = NpgsqlDbType.Uuid, Size = 16 ,Primarykey = true} },
20-
{"title", new SchemaModel{ FieldName = "title", DbType = NpgsqlDbType.Varchar, Size = 255} },
21-
{"create_time", new SchemaModel{ FieldName = "create_time", DbType = NpgsqlDbType.Timestamp, Size = 8} },
22-
{"update_time", new SchemaModel{ FieldName = "update_time", DbType = NpgsqlDbType.Timestamp, Size = 8} },
23-
{"last_time", new SchemaModel{ FieldName = "last_time", DbType = NpgsqlDbType.Timestamp, Size = 8} },
24-
{"user_id", new SchemaModel{ FieldName = "user_id", DbType = NpgsqlDbType.Uuid, Size = 16} },
25-
{"name", new SchemaModel{ FieldName = "name", DbType = NpgsqlDbType.Varchar, Size = 255} },
26-
{"age", new SchemaModel{ FieldName = "age", DbType = NpgsqlDbType.Integer, Size = 4} },
27-
{"sex", new SchemaModel{ FieldName = "sex", DbType = NpgsqlDbType.Boolean, Size = 1} },
28-
{"createtime", new SchemaModel{ FieldName = "createtime", DbType = NpgsqlDbType.Date, Size = 4} },
29-
{"updatetime", new SchemaModel{ FieldName = "updatetime", DbType = NpgsqlDbType.Time, Size = 8} }
16+
new SchemaModel{ FieldName = "id", DbType = NpgsqlDbType.Uuid, Size = 16 ,Primarykey = true},
17+
new SchemaModel{ FieldName = "title", DbType = NpgsqlDbType.Varchar, Size = 255},
18+
new SchemaModel{ FieldName = "create_time", DbType = NpgsqlDbType.Timestamp, Size = 8},
19+
new SchemaModel{ FieldName = "update_time", DbType = NpgsqlDbType.Timestamp, Size = 8},
20+
new SchemaModel{ FieldName = "last_time", DbType = NpgsqlDbType.Timestamp, Size = 8},
21+
new SchemaModel{ FieldName = "user_id", DbType = NpgsqlDbType.Uuid, Size = 16},
22+
new SchemaModel{ FieldName = "name", DbType = NpgsqlDbType.Varchar, Size = 255},
23+
new SchemaModel{ FieldName = "age", DbType = NpgsqlDbType.Integer, Size = 4},
24+
new SchemaModel{ FieldName = "sex", DbType = NpgsqlDbType.Boolean, Size = 1},
25+
new SchemaModel{ FieldName = "createtime", DbType = NpgsqlDbType.Date, Size = 4},
26+
new SchemaModel{ FieldName = "updatetime", DbType = NpgsqlDbType.Time, Size = 8}
3027
};
3128
public List<PropertyInfo> Properties => ContractUtils.GetProperties(typeof(TopicModel));
3229

MyStaging.xUnitTest.db/Model/Schemas/UserSchema.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,16 @@ public partial class UserSchema : ISchemaModel
1111
{
1212
public static UserSchema Instance => new UserSchema();
1313

14-
15-
16-
17-
public Dictionary<string, SchemaModel> SchemaSet => new Dictionary<string, SchemaModel>
14+
public List<SchemaModel> SchemaSet => new List<SchemaModel>
1815
{
19-
{"id", new SchemaModel{ FieldName = "id", DbType = NpgsqlDbType.Varchar, Size = -1 ,Primarykey = true} },
20-
{"loginname", new SchemaModel{ FieldName = "loginname", DbType = NpgsqlDbType.Varchar, Size = 255} },
21-
{"password", new SchemaModel{ FieldName = "password", DbType = NpgsqlDbType.Varchar, Size = 255} },
22-
{"nickname", new SchemaModel{ FieldName = "nickname", DbType = NpgsqlDbType.Varchar, Size = 255} },
23-
{"sex", new SchemaModel{ FieldName = "sex", DbType = NpgsqlDbType.Boolean, Size = 1} },
24-
{"age", new SchemaModel{ FieldName = "age", DbType = NpgsqlDbType.Integer, Size = 4} },
25-
{"money", new SchemaModel{ FieldName = "money", DbType = NpgsqlDbType.Numeric, Size = -1} },
26-
{"createtime", new SchemaModel{ FieldName = "createtime", DbType = NpgsqlDbType.Timestamp, Size = 8} }
16+
new SchemaModel{ FieldName = "id", DbType = NpgsqlDbType.Varchar, Size = -1 ,Primarykey = true},
17+
new SchemaModel{ FieldName = "loginname", DbType = NpgsqlDbType.Varchar, Size = 255},
18+
new SchemaModel{ FieldName = "password", DbType = NpgsqlDbType.Varchar, Size = 255},
19+
new SchemaModel{ FieldName = "nickname", DbType = NpgsqlDbType.Varchar, Size = 255},
20+
new SchemaModel{ FieldName = "sex", DbType = NpgsqlDbType.Boolean, Size = 1},
21+
new SchemaModel{ FieldName = "age", DbType = NpgsqlDbType.Integer, Size = 4},
22+
new SchemaModel{ FieldName = "money", DbType = NpgsqlDbType.Numeric, Size = -1},
23+
new SchemaModel{ FieldName = "createtime", DbType = NpgsqlDbType.Timestamp, Size = 8}
2724
};
2825
public List<PropertyInfo> Properties => ContractUtils.GetProperties(typeof(UserModel));
2926

MyStaging/Helpers/InsertBuilder.cs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ public override string ToString()
9595

9696
string fieldsName = string.Empty;
9797
var fieldCollection = this.schema.SchemaSet;
98-
foreach (var key in fieldCollection.Keys)
98+
foreach (var field in fieldCollection)
9999
{
100-
fieldsName += "\"" + key + "\",";
100+
fieldsName += "\"" + field.FieldName + "\",";
101101
}
102102

103103
fieldsName = fieldsName.Remove(fieldsName.Length - 1, 1);
@@ -108,26 +108,24 @@ public override string ToString()
108108
var mObj = this.models[i];
109109
string piNames = string.Empty;
110110

111-
for (int j = 0; j < this.schema.Properties.Count; j++)
111+
foreach (var field in fieldCollection)
112112
{
113-
PropertyInfo pi = this.schema.Properties[j];
114-
var key = pi.Name.ToLower();
115-
if (fieldCollection.ContainsKey(key))
113+
PropertyInfo pi = this.schema.Properties.Where(f => f.Name.ToLower() == field.FieldName.ToLower()).FirstOrDefault();
114+
if (pi == null)
115+
throw new KeyNotFoundException($"数据库字段名称:{field.FieldName} 在实体对象 {mObj.GetType().Name} 上未找到对应属性!");
116+
117+
var piName = $"@{field.FieldName}_{i}";
118+
piNames += piName + ",";
119+
var value = pi.GetValue(mObj);
120+
if (field.Primarykey || defaultValueField.ContainsKey(field.FieldName))
116121
{
117-
var piName = $"@{key}_{i}";
118-
piNames += piName + ",";
119-
var sm = fieldCollection[key];
120-
var value = pi.GetValue(mObj);
121-
if (sm.Primarykey || defaultValueField.ContainsKey(key))
122-
{
123-
if (value == null
124-
|| value.Equals(Guid.Empty)
125-
|| zeroTime.Equals(value))
126-
value = CreateDefaultValue(sm);
127-
}
128-
129-
base.AddParameter(piName, sm.DbType, value, sm.Size);
122+
if (value == null
123+
|| value.Equals(Guid.Empty)
124+
|| zeroTime.Equals(value))
125+
value = CreateDefaultValue(field);
130126
}
127+
128+
base.AddParameter(piName, field.DbType, value, field.Size);
131129
}
132130

133131
piNames = piNames.Remove(piNames.Length - 1, 1);

0 commit comments

Comments
 (0)