Browse Source

Code optimization

pull/12/MERGE
sunkaixuan 8 years ago
parent
commit
08211f700b
  1. 4
      SqlSugar/Abstract/DbBindProvider/DbBindProvider.cs
  2. 38
      SqlSugar/Abstract/DbBindProvider/IDataReaderEntityBuilder.cs
  3. 4
      SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs
  4. 4
      SqlSugar/Interface/IDbBind.cs
  5. 4
      SqlSugar/Realization/MySql/DbBind/MySqlDbBind.cs
  6. 2
      SqlSugar/Realization/SqlServer/DbBind/SqlServerDbBind.cs

4
SqlSugar/Abstract/DbBindProvider/DbBindProvider.cs

@ -14,8 +14,8 @@ namespace SqlSugar
#endregion #endregion
#region Public methods #region Public methods
public abstract string GetCSharpType(string dbTypeName); public abstract string GetPropertyType(string dbTypeName);
public virtual string GetCSharpConvert(string dbTypeName) public virtual string GetConvertString(string dbTypeName)
{ {
string reval = string.Empty; string reval = string.Empty;
switch (dbTypeName.ToLower()) switch (dbTypeName.ToLower())

38
SqlSugar/Abstract/DbBindProvider/IDataReaderEntityBuilder.cs

@ -164,14 +164,14 @@ namespace SqlSugar
List<string> dateThrow = bind.DateThrow; List<string> dateThrow = bind.DateThrow;
List<string> shortThrow = bind.ShortThrow; List<string> shortThrow = bind.ShortThrow;
MethodInfo method = null; MethodInfo method = null;
var transformedPropertyName = bind.GetCSharpType(dbTypeName); var propertyType = bind.GetPropertyType(dbTypeName);
var objTypeName = bindType.Name.ToLower(); var objTypeName = bindType.Name.ToLower();
var isEnum = bindType.IsEnum; var isEnum = bindType.IsEnum;
if (isEnum) if (isEnum)
{ {
transformedPropertyName = "enum"; propertyType = "enum";
} }
else if (transformedPropertyName.IsIn("byte[]", "other", "object") || dbTypeName.Contains("hierarchyid")) else if (propertyType.IsIn("byte[]", "other", "object") || dbTypeName.Contains("hierarchyid"))
{ {
generator.Emit(OpCodes.Call, getValueMethod); generator.Emit(OpCodes.Call, getValueMethod);
generator.Emit(OpCodes.Unbox_Any, bindProperty.PropertyType); generator.Emit(OpCodes.Unbox_Any, bindProperty.PropertyType);
@ -179,10 +179,10 @@ namespace SqlSugar
} }
if (isNullableType) if (isNullableType)
{ {
switch (transformedPropertyName) switch (propertyType)
{ {
case "int": case "int":
CheckType(intThrow, objTypeName, transformedPropertyName, propertyName); CheckType(intThrow, objTypeName, propertyType, propertyName);
var isNotInt = objTypeName != "int32"; var isNotInt = objTypeName != "int32";
if (isNotInt) if (isNotInt)
method = getOtherNull.MakeGenericMethod(bindType); method = getOtherNull.MakeGenericMethod(bindType);
@ -194,30 +194,30 @@ namespace SqlSugar
else else
method = getConvertBoolean; break; method = getConvertBoolean; break;
case "string": case "string":
CheckType(stringThrow, objTypeName, transformedPropertyName, propertyName); CheckType(stringThrow, objTypeName, propertyType, propertyName);
method = getString; break; method = getString; break;
case "DateTime": case "DateTime":
CheckType(dateThrow, objTypeName, transformedPropertyName, propertyName); CheckType(dateThrow, objTypeName, propertyType, propertyName);
if (objTypeName != "datetime") if (objTypeName != "datetime")
method = getOtherNull.MakeGenericMethod(bindType); method = getOtherNull.MakeGenericMethod(bindType);
else else
method = getConvertDateTime; break; method = getConvertDateTime; break;
case "decimal": case "decimal":
CheckType(decimalThrow, objTypeName, transformedPropertyName, propertyName); CheckType(decimalThrow, objTypeName, propertyType, propertyName);
var isNotDecimal = objTypeName != "decimal"; var isNotDecimal = objTypeName != "decimal";
if (isNotDecimal) if (isNotDecimal)
method = getOtherNull.MakeGenericMethod(bindType); method = getOtherNull.MakeGenericMethod(bindType);
else else
method = getConvertDecimal; break; method = getConvertDecimal; break;
case "double": case "double":
CheckType(doubleThrow, objTypeName, transformedPropertyName, propertyName); CheckType(doubleThrow, objTypeName, propertyType, propertyName);
var isNotDouble = objTypeName != "double"; var isNotDouble = objTypeName != "double";
if (isNotDouble) if (isNotDouble)
method = getOtherNull.MakeGenericMethod(bindType); method = getOtherNull.MakeGenericMethod(bindType);
else else
method = getConvertDouble; break; method = getConvertDouble; break;
case "Guid": case "Guid":
CheckType(guidThrow, objTypeName, transformedPropertyName, propertyName); CheckType(guidThrow, objTypeName, propertyType, propertyName);
if (objTypeName != "guid") if (objTypeName != "guid")
method = getOtherNull.MakeGenericMethod(bindType); method = getOtherNull.MakeGenericMethod(bindType);
else else
@ -227,7 +227,7 @@ namespace SqlSugar
case "enum": case "enum":
method = getConvertEnum_Null.MakeGenericMethod(bindType); break; method = getConvertEnum_Null.MakeGenericMethod(bindType); break;
case "short": case "short":
CheckType(shortThrow, objTypeName, transformedPropertyName, propertyName); CheckType(shortThrow, objTypeName, propertyType, propertyName);
var isNotShort = objTypeName != "int16" && objTypeName != "short"; var isNotShort = objTypeName != "int16" && objTypeName != "short";
if (isNotShort) if (isNotShort)
method = getOtherNull.MakeGenericMethod(bindType); method = getOtherNull.MakeGenericMethod(bindType);
@ -241,10 +241,10 @@ namespace SqlSugar
} }
else else
{ {
switch (transformedPropertyName) switch (propertyType)
{ {
case "int": case "int":
CheckType(intThrow, objTypeName, transformedPropertyName, propertyName); CheckType(intThrow, objTypeName, propertyType, propertyName);
var isNotInt = objTypeName != "int32"; var isNotInt = objTypeName != "int32";
if (isNotInt) if (isNotInt)
method = getOther.MakeGenericMethod(bindType); method = getOther.MakeGenericMethod(bindType);
@ -256,30 +256,30 @@ namespace SqlSugar
else else
method = getBoolean; break; method = getBoolean; break;
case "string": case "string":
CheckType(stringThrow, objTypeName, transformedPropertyName, propertyName); CheckType(stringThrow, objTypeName, propertyType, propertyName);
method = getString; break; method = getString; break;
case "DateTime": case "DateTime":
CheckType(dateThrow, objTypeName, transformedPropertyName, propertyName); CheckType(dateThrow, objTypeName, propertyType, propertyName);
if (objTypeName != "datetime") if (objTypeName != "datetime")
method = getOther.MakeGenericMethod(bindType); method = getOther.MakeGenericMethod(bindType);
else else
method = getDateTime; break; method = getDateTime; break;
case "decimal": case "decimal":
CheckType(decimalThrow, objTypeName, transformedPropertyName, propertyName); CheckType(decimalThrow, objTypeName, propertyType, propertyName);
var isNotDecimal = objTypeName != "decimal"; var isNotDecimal = objTypeName != "decimal";
if (isNotDecimal) if (isNotDecimal)
method = getOther.MakeGenericMethod(bindType); method = getOther.MakeGenericMethod(bindType);
else else
method = getDecimal; break; method = getDecimal; break;
case "double": case "double":
CheckType(doubleThrow, objTypeName, transformedPropertyName, propertyName); CheckType(doubleThrow, objTypeName, propertyType, propertyName);
var isNotDouble = objTypeName != "double"; var isNotDouble = objTypeName != "double";
if (isNotDouble) if (isNotDouble)
method = getOther.MakeGenericMethod(bindType); method = getOther.MakeGenericMethod(bindType);
else else
method = getDouble; break; method = getDouble; break;
case "guid": case "guid":
CheckType(guidThrow, objTypeName, transformedPropertyName, propertyName); CheckType(guidThrow, objTypeName, propertyType, propertyName);
if (objTypeName != "guid") if (objTypeName != "guid")
method = getOther.MakeGenericMethod(bindType); method = getOther.MakeGenericMethod(bindType);
else else
@ -289,7 +289,7 @@ namespace SqlSugar
case "enum": case "enum":
method = getEnum.MakeGenericMethod(bindType); break; method = getEnum.MakeGenericMethod(bindType); break;
case "short": case "short":
CheckType(shortThrow, objTypeName, transformedPropertyName, propertyName); CheckType(shortThrow, objTypeName, propertyType, propertyName);
var isNotShort = objTypeName != "int16" && objTypeName != "short"; var isNotShort = objTypeName != "int16" && objTypeName != "short";
if (isNotShort) if (isNotShort)
method = getOther.MakeGenericMethod(bindType); method = getOther.MakeGenericMethod(bindType);

4
SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs

@ -266,7 +266,7 @@ namespace SqlSugar
private string GetPropertyTypeName(DbColumnInfo item) private string GetPropertyTypeName(DbColumnInfo item)
{ {
string result = this.Context.Ado.DbBind.GetCSharpType(item.DataType); string result = this.Context.Ado.DbBind.GetPropertyType(item.DataType);
if (result != "string"&&result!="byte[]"&&result!="object"&& item.IsNullable) if (result != "string"&&result!="byte[]"&&result!="object"&& item.IsNullable)
{ {
result += "?"; result += "?";
@ -277,7 +277,7 @@ namespace SqlSugar
{ {
var convertString = GetProertypeDefaultValue(item); var convertString = GetProertypeDefaultValue(item);
if (convertString == "DateTime.Now" || convertString == null) return convertString; if (convertString == "DateTime.Now" || convertString == null) return convertString;
string result = this.Context.Ado.DbBind.GetCSharpConvert(item.DataType) + "(\"" + convertString + "\")"; string result = this.Context.Ado.DbBind.GetConvertString(item.DataType) + "(\"" + convertString + "\")";
return result; return result;
} }

4
SqlSugar/Interface/IDbBind.cs

@ -15,8 +15,8 @@ namespace SqlSugar
List<string> DoubleThrow { get; } List<string> DoubleThrow { get; }
List<string> DateThrow { get; } List<string> DateThrow { get; }
List<string> ShortThrow { get; } List<string> ShortThrow { get; }
string GetCSharpType(string dbTypeName); string GetPropertyType(string dbTypeName);
string GetCSharpConvert(string dbTypeName); string GetConvertString(string dbTypeName);
List<T> DataReaderToList<T>(Type type, IDataReader reader, string fields); List<T> DataReaderToList<T>(Type type, IDataReader reader, string fields);
} }
} }

4
SqlSugar/Realization/MySql/DbBind/MySqlDbBind.cs

@ -4,7 +4,7 @@ namespace SqlSugar
{ {
public class MySqlDbBind : DbBindProvider public class MySqlDbBind : DbBindProvider
{ {
public override string GetCSharpType(string typeName) public override string GetPropertyType(string typeName)
{ {
string reval; string reval;
switch (typeName.ToLower()) switch (typeName.ToLower())
@ -94,7 +94,7 @@ namespace SqlSugar
return reval; return reval;
} }
public override string GetCSharpConvert(string dbTypeName) public override string GetConvertString(string dbTypeName)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

2
SqlSugar/Realization/SqlServer/DbBind/SqlServerDbBind.cs

@ -6,7 +6,7 @@ namespace SqlSugar
{ {
public class SqlServerDbBind : DbBindProvider public class SqlServerDbBind : DbBindProvider
{ {
public override string GetCSharpType(string dbTypeName) public override string GetPropertyType(string dbTypeName)
{ {
string reval = string.Empty; string reval = string.Empty;
switch (dbTypeName.ToLower()) switch (dbTypeName.ToLower())

Loading…
Cancel
Save