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
#region Public methods
public abstract string GetCSharpType(string dbTypeName);
public virtual string GetCSharpConvert(string dbTypeName)
public abstract string GetPropertyType(string dbTypeName);
public virtual string GetConvertString(string dbTypeName)
{
string reval = string.Empty;
switch (dbTypeName.ToLower())

38
SqlSugar/Abstract/DbBindProvider/IDataReaderEntityBuilder.cs

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

4
SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs

@ -266,7 +266,7 @@ namespace SqlSugar
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)
{
result += "?";
@ -277,7 +277,7 @@ namespace SqlSugar
{
var convertString = GetProertypeDefaultValue(item);
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;
}

4
SqlSugar/Interface/IDbBind.cs

@ -15,8 +15,8 @@ namespace SqlSugar
List<string> DoubleThrow { get; }
List<string> DateThrow { get; }
List<string> ShortThrow { get; }
string GetCSharpType(string dbTypeName);
string GetCSharpConvert(string dbTypeName);
string GetPropertyType(string dbTypeName);
string GetConvertString(string dbTypeName);
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 override string GetCSharpType(string typeName)
public override string GetPropertyType(string typeName)
{
string reval;
switch (typeName.ToLower())
@ -94,7 +94,7 @@ namespace SqlSugar
return reval;
}
public override string GetCSharpConvert(string dbTypeName)
public override string GetConvertString(string dbTypeName)
{
throw new NotImplementedException();
}

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

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

Loading…
Cancel
Save