mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2026-01-19 17:51:36 +08:00
Update Core
This commit is contained in:
@@ -312,6 +312,7 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
CommandType = CommandType.Text;
|
||||||
if (ErrorEvent != null)
|
if (ErrorEvent != null)
|
||||||
ExecuteErrorEvent(sql, parameters, ex);
|
ExecuteErrorEvent(sql, parameters, ex);
|
||||||
throw ex;
|
throw ex;
|
||||||
@@ -346,6 +347,7 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
CommandType = CommandType.Text;
|
||||||
if (ErrorEvent != null)
|
if (ErrorEvent != null)
|
||||||
ExecuteErrorEvent(sql, parameters, ex);
|
ExecuteErrorEvent(sql, parameters, ex);
|
||||||
throw ex;
|
throw ex;
|
||||||
@@ -375,6 +377,7 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
CommandType = CommandType.Text;
|
||||||
if (ErrorEvent != null)
|
if (ErrorEvent != null)
|
||||||
ExecuteErrorEvent(sql, parameters, ex);
|
ExecuteErrorEvent(sql, parameters, ex);
|
||||||
throw ex;
|
throw ex;
|
||||||
@@ -403,6 +406,7 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
CommandType = CommandType.Text;
|
||||||
if (ErrorEvent != null)
|
if (ErrorEvent != null)
|
||||||
ExecuteErrorEvent(sql, parameters, ex);
|
ExecuteErrorEvent(sql, parameters, ex);
|
||||||
throw ex;
|
throw ex;
|
||||||
@@ -434,6 +438,7 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
CommandType = CommandType.Text;
|
||||||
if (ErrorEvent != null)
|
if (ErrorEvent != null)
|
||||||
ExecuteErrorEvent(sql,parameters,ex);
|
ExecuteErrorEvent(sql,parameters,ex);
|
||||||
throw ex;
|
throw ex;
|
||||||
|
|||||||
@@ -22,7 +22,8 @@ namespace SqlSugar
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual ICodeFirst SetStringDefaultLength(int length) {
|
public virtual ICodeFirst SetStringDefaultLength(int length)
|
||||||
|
{
|
||||||
DefultLength = length;
|
DefultLength = length;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -43,7 +44,23 @@ namespace SqlSugar
|
|||||||
});
|
});
|
||||||
Check.Exception(!executeResult.IsSuccess, executeResult.ErrorMessage);
|
Check.Exception(!executeResult.IsSuccess, executeResult.ErrorMessage);
|
||||||
}
|
}
|
||||||
public virtual void InitTables(Type[] entityTypes)
|
public void InitTables<T>()
|
||||||
|
{
|
||||||
|
InitTables(typeof(T));
|
||||||
|
}
|
||||||
|
public void InitTables<T, T2>()
|
||||||
|
{
|
||||||
|
InitTables(typeof(T),typeof(T2));
|
||||||
|
}
|
||||||
|
public void InitTables<T, T2, T3>()
|
||||||
|
{
|
||||||
|
InitTables(typeof(T), typeof(T2),typeof(T3));
|
||||||
|
}
|
||||||
|
public void InitTables<T, T2, T3, T4>()
|
||||||
|
{
|
||||||
|
InitTables(typeof(T), typeof(T2), typeof(T3),typeof(T4));
|
||||||
|
}
|
||||||
|
public virtual void InitTables(params Type[] entityTypes)
|
||||||
{
|
{
|
||||||
if (entityTypes.HasValue())
|
if (entityTypes.HasValue())
|
||||||
{
|
{
|
||||||
@@ -74,10 +91,12 @@ namespace SqlSugar
|
|||||||
protected virtual void Execute(Type entityType)
|
protected virtual void Execute(Type entityType)
|
||||||
{
|
{
|
||||||
var entityInfo = this.Context.EntityMaintenance.GetEntityInfo(entityType);
|
var entityInfo = this.Context.EntityMaintenance.GetEntityInfo(entityType);
|
||||||
if (this.DefultLength > 0) {
|
if (this.DefultLength > 0)
|
||||||
|
{
|
||||||
foreach (var item in entityInfo.Columns)
|
foreach (var item in entityInfo.Columns)
|
||||||
{
|
{
|
||||||
if (item.PropertyInfo.PropertyType == UtilConstants.StringType && item.DataType.IsNullOrEmpty()&& item.Length==0) {
|
if (item.PropertyInfo.PropertyType == UtilConstants.StringType && item.DataType.IsNullOrEmpty() && item.Length == 0)
|
||||||
|
{
|
||||||
item.Length = DefultLength;
|
item.Length = DefultLength;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -98,13 +117,13 @@ namespace SqlSugar
|
|||||||
List<DbColumnInfo> columns = new List<DbColumnInfo>();
|
List<DbColumnInfo> columns = new List<DbColumnInfo>();
|
||||||
if (entityInfo.Columns.HasValue())
|
if (entityInfo.Columns.HasValue())
|
||||||
{
|
{
|
||||||
foreach (var item in entityInfo.Columns.OrderBy(it=>it.IsPrimarykey?0:1).Where(it => it.IsIgnore == false))
|
foreach (var item in entityInfo.Columns.OrderBy(it => it.IsPrimarykey ? 0 : 1).Where(it => it.IsIgnore == false))
|
||||||
{
|
{
|
||||||
DbColumnInfo dbColumnInfo = EntityColumnToDbColumn(entityInfo, tableName, item);
|
DbColumnInfo dbColumnInfo = EntityColumnToDbColumn(entityInfo, tableName, item);
|
||||||
columns.Add(dbColumnInfo);
|
columns.Add(dbColumnInfo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.Context.DbMaintenance.CreateTable(tableName, columns,true);
|
this.Context.DbMaintenance.CreateTable(tableName, columns, true);
|
||||||
}
|
}
|
||||||
public virtual void ExistLogic(EntityInfo entityInfo)
|
public virtual void ExistLogic(EntityInfo entityInfo)
|
||||||
{
|
{
|
||||||
@@ -136,7 +155,7 @@ namespace SqlSugar
|
|||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
|
|
||||||
var isMultiplePrimaryKey = dbColumns.Where(it => it.IsPrimarykey).Count() > 1|| entityColumns.Where(it => it.IsPrimarykey).Count() > 1;
|
var isMultiplePrimaryKey = dbColumns.Where(it => it.IsPrimarykey).Count() > 1 || entityColumns.Where(it => it.IsPrimarykey).Count() > 1;
|
||||||
|
|
||||||
|
|
||||||
var isChange = false;
|
var isChange = false;
|
||||||
@@ -167,7 +186,7 @@ namespace SqlSugar
|
|||||||
if (dbColumn == null) continue;
|
if (dbColumn == null) continue;
|
||||||
bool pkDiff, idEntityDiff;
|
bool pkDiff, idEntityDiff;
|
||||||
KeyAction(item, dbColumn, out pkDiff, out idEntityDiff);
|
KeyAction(item, dbColumn, out pkDiff, out idEntityDiff);
|
||||||
if (dbColumn != null && pkDiff && !idEntityDiff&& isMultiplePrimaryKey==false)
|
if (dbColumn != null && pkDiff && !idEntityDiff && isMultiplePrimaryKey == false)
|
||||||
{
|
{
|
||||||
var isAdd = item.IsPrimarykey;
|
var isAdd = item.IsPrimarykey;
|
||||||
if (isAdd)
|
if (isAdd)
|
||||||
@@ -179,15 +198,17 @@ namespace SqlSugar
|
|||||||
this.Context.DbMaintenance.DropConstraint(tableName, string.Format("PK_{0}_{1}", tableName, item.DbColumnName));
|
this.Context.DbMaintenance.DropConstraint(tableName, string.Format("PK_{0}_{1}", tableName, item.DbColumnName));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if ((pkDiff || idEntityDiff)&& isMultiplePrimaryKey==false)
|
else if ((pkDiff || idEntityDiff) && isMultiplePrimaryKey == false)
|
||||||
{
|
{
|
||||||
ChangeKey(entityInfo, tableName, item);
|
ChangeKey(entityInfo, tableName, item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (isMultiplePrimaryKey) {
|
if (isMultiplePrimaryKey)
|
||||||
var oldPkNames = dbColumns.Where(it => it.IsPrimarykey).Select(it => it.DbColumnName.ToLower()).OrderBy(it=>it).ToList();
|
{
|
||||||
var newPkNames = entityColumns.Where(it => it.IsPrimarykey).Select(it => it.DbColumnName.ToLower()).OrderBy(it=>it).ToList();
|
var oldPkNames = dbColumns.Where(it => it.IsPrimarykey).Select(it => it.DbColumnName.ToLower()).OrderBy(it => it).ToList();
|
||||||
if (!Enumerable.SequenceEqual(oldPkNames,newPkNames)) {
|
var newPkNames = entityColumns.Where(it => it.IsPrimarykey).Select(it => it.DbColumnName.ToLower()).OrderBy(it => it).ToList();
|
||||||
|
if (!Enumerable.SequenceEqual(oldPkNames, newPkNames))
|
||||||
|
{
|
||||||
Check.Exception(true, ErrorMessage.GetThrowMessage("Modification of multiple primary key tables is not supported. Delete tables while creating", "不支持修改多主键表,请删除表在创建"));
|
Check.Exception(true, ErrorMessage.GetThrowMessage("Modification of multiple primary key tables is not supported. Delete tables while creating", "不支持修改多主键表,请删除表在创建"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -121,6 +121,14 @@ namespace SqlSugar
|
|||||||
resolveExpress.IgnoreComumnList = Context.IgnoreColumns;
|
resolveExpress.IgnoreComumnList = Context.IgnoreColumns;
|
||||||
resolveExpress.SqlFuncServices = Context.CurrentConnectionConfig.ConfigureExternalServices == null ? null : Context.CurrentConnectionConfig.ConfigureExternalServices.SqlFuncServices;
|
resolveExpress.SqlFuncServices = Context.CurrentConnectionConfig.ConfigureExternalServices == null ? null : Context.CurrentConnectionConfig.ConfigureExternalServices.SqlFuncServices;
|
||||||
}
|
}
|
||||||
|
resolveExpress.InitMappingInfo = Context.InitMppingInfo;
|
||||||
|
resolveExpress.RefreshMapping = () =>
|
||||||
|
{
|
||||||
|
resolveExpress.MappingColumns = Context.MappingColumns;
|
||||||
|
resolveExpress.MappingTables = Context.MappingTables;
|
||||||
|
resolveExpress.IgnoreComumnList = Context.IgnoreColumns;
|
||||||
|
resolveExpress.SqlFuncServices = Context.CurrentConnectionConfig.ConfigureExternalServices == null ? null : Context.CurrentConnectionConfig.ConfigureExternalServices.SqlFuncServices;
|
||||||
|
};
|
||||||
resolveExpress.Resolve(expression, resolveType);
|
resolveExpress.Resolve(expression, resolveType);
|
||||||
this.Parameters.AddRange(resolveExpress.Parameters);
|
this.Parameters.AddRange(resolveExpress.Parameters);
|
||||||
var result = resolveExpress.Result;
|
var result = resolveExpress.Result;
|
||||||
|
|||||||
@@ -162,7 +162,15 @@ namespace SqlSugar
|
|||||||
else if (isMapping)
|
else if (isMapping)
|
||||||
{
|
{
|
||||||
var mappingInfo = this.MappingTables.FirstOrDefault(it => it.EntityName.Equals(entityName, StringComparison.CurrentCultureIgnoreCase));
|
var mappingInfo = this.MappingTables.FirstOrDefault(it => it.EntityName.Equals(entityName, StringComparison.CurrentCultureIgnoreCase));
|
||||||
return SqlTranslationLeft + (mappingInfo == null ? entityName : mappingInfo.DbTableName) + SqlTranslationRight;
|
var name = (mappingInfo == null ? entityName : mappingInfo.DbTableName);
|
||||||
|
if (name.Contains("."))
|
||||||
|
{
|
||||||
|
return string.Join(".", name.Split('.').Select(it => SqlTranslationLeft + it + SqlTranslationRight));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return SqlTranslationLeft + name + SqlTranslationRight;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (isComplex)
|
else if (isComplex)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -124,9 +124,20 @@ namespace SqlSugar
|
|||||||
base.Expression = item;
|
base.Expression = item;
|
||||||
base.Start();
|
base.Start();
|
||||||
var subSql = base.Context.GetEqString(memberName, parameter.CommonTempData.ObjToString());
|
var subSql = base.Context.GetEqString(memberName, parameter.CommonTempData.ObjToString());
|
||||||
|
if (subSql.Contains(",")) {
|
||||||
|
subSql = subSql.Replace(",", UtilConstants.ReplaceCommaKey);
|
||||||
|
}
|
||||||
if (ResolveExpressType.Update == this.Context.ResolveType)
|
if (ResolveExpressType.Update == this.Context.ResolveType)
|
||||||
{
|
{
|
||||||
subSql = Regex.Replace(subSql, @" \[\w+?\]\.| ""\w+?""\.| \`\w+?\`\.", this.Context.GetTranslationTableName(parameter.CurrentExpression.Type.Name,true) +".");
|
string name = this.Context.GetTranslationTableName(parameter.CurrentExpression.Type.Name, true);
|
||||||
|
if (name.Contains("."))
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
subSql = Regex.Replace(subSql, @" \[\w+?\]\.| ""\w+?""\.| \`\w+?\`\.", name + ".");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
parameter.Context.Result.Append(subSql);
|
parameter.Context.Result.Append(subSql);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ namespace SqlSugar
|
|||||||
this.Context.InitMappingInfo(entityType);
|
this.Context.InitMappingInfo(entityType);
|
||||||
this.Context.RefreshMapping();
|
this.Context.RefreshMapping();
|
||||||
}
|
}
|
||||||
var result= "FROM "+this.Context.GetTranslationTableName(name, true);
|
var result= "FROM "+this.Context.GetTranslationTableName(name,true);
|
||||||
if (this.Context.SubQueryIndex > 0) {
|
if (this.Context.SubQueryIndex > 0) {
|
||||||
result += " subTableIndex"+this.Context.SubQueryIndex;
|
result += " subTableIndex"+this.Context.SubQueryIndex;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
public class MyContractResolver : Newtonsoft.Json.Serialization.DefaultContractResolver
|
public class MyContractResolver : Newtonsoft.Json.Serialization.DefaultContractResolver
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
public MyContractResolver()
|
public MyContractResolver()
|
||||||
{
|
{
|
||||||
@@ -35,24 +35,31 @@ namespace SqlSugar
|
|||||||
|
|
||||||
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
|
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
|
||||||
{
|
{
|
||||||
var list = type.GetProperties()
|
if (type.IsAnonymousType()||type==UtilConstants.ObjType|| type.Namespace=="SqlSugar"|| type.IsClass()==false)
|
||||||
.Where(x => !x.GetCustomAttributes(true).Any(a => (a is SugarColumn) && ((SugarColumn)a).NoSerialize == true))
|
|
||||||
.Select(p => new JsonProperty()
|
|
||||||
{
|
|
||||||
PropertyName = p.Name,
|
|
||||||
PropertyType = p.PropertyType,
|
|
||||||
Readable = true,
|
|
||||||
Writable = true,
|
|
||||||
ValueProvider = base.CreateMemberValueProvider(p)
|
|
||||||
}).ToList();
|
|
||||||
foreach (var item in list)
|
|
||||||
{
|
{
|
||||||
if (UtilMethods.GetUnderType(item.PropertyType) == UtilConstants.DateType)
|
return base.CreateProperties(type, memberSerialization);
|
||||||
{
|
}
|
||||||
CreateDateProperty(type, item);
|
else
|
||||||
}
|
{
|
||||||
|
var list = type.GetProperties()
|
||||||
|
.Where(x => !x.GetCustomAttributes(true).Any(a => (a is SugarColumn) && ((SugarColumn)a).NoSerialize == true))
|
||||||
|
.Select(p => new JsonProperty()
|
||||||
|
{
|
||||||
|
PropertyName = p.Name,
|
||||||
|
PropertyType = p.PropertyType,
|
||||||
|
Readable = true,
|
||||||
|
Writable = true,
|
||||||
|
ValueProvider = base.CreateMemberValueProvider(p)
|
||||||
|
}).ToList();
|
||||||
|
foreach (var item in list)
|
||||||
|
{
|
||||||
|
if (UtilMethods.GetUnderType(item.PropertyType) == UtilConstants.DateType)
|
||||||
|
{
|
||||||
|
CreateDateProperty(type, item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list;
|
||||||
}
|
}
|
||||||
return list;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void CreateDateProperty(Type type, JsonProperty item)
|
private static void CreateDateProperty(Type type, JsonProperty item)
|
||||||
@@ -66,5 +73,5 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,11 +7,15 @@ namespace SqlSugar
|
|||||||
public partial interface ICodeFirst
|
public partial interface ICodeFirst
|
||||||
{
|
{
|
||||||
SqlSugarClient Context { get; set; }
|
SqlSugarClient Context { get; set; }
|
||||||
ICodeFirst BackupTable(int maxBackupDataRows=int.MaxValue);
|
ICodeFirst BackupTable(int maxBackupDataRows = int.MaxValue);
|
||||||
ICodeFirst SetStringDefaultLength(int length);
|
ICodeFirst SetStringDefaultLength(int length);
|
||||||
void InitTables(string entitiesNamespace);
|
void InitTables(string entitiesNamespace);
|
||||||
void InitTables(string [] entitiesNamespaces);
|
void InitTables(string[] entitiesNamespaces);
|
||||||
void InitTables(params Type [] entityTypes);
|
void InitTables(params Type[] entityTypes);
|
||||||
void InitTables(Type entityType);
|
void InitTables(Type entityType);
|
||||||
|
void InitTables<T>();
|
||||||
|
void InitTables<T, T2>();
|
||||||
|
void InitTables<T, T2, T3>();
|
||||||
|
void InitTables<T, T2, T3, T4>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user