PgSql Support array

This commit is contained in:
skx 2020-10-13 18:14:16 +08:00
parent 92ee57967e
commit 8bc4e14b2d
4 changed files with 79 additions and 0 deletions

View File

@ -0,0 +1,53 @@
//using SqlSugar;
//using System;
//using System.Collections.Generic;
//using System.ComponentModel.DataAnnotations;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
//namespace OrmTest.Test
//{
// public class BugTest
// {
// public static void Init()
// {
// SqlSugarClient Db = new SqlSugarClient(new ConnectionConfig()
// {
// ConnectionString = @"PORT=5433;DATABASE=x;HOST=localhost;PASSWORD=haosql;USER ID=postgres",
// DbType = DbType.PostgreSQL,
// IsAutoCloseConnection = true,
// //MoreSettings = new ConnMoreSettings()
// //{
// // PgSqlIsAutoToLower = true //我们这里需要设置为false
// //},
// InitKeyType = InitKeyType.Attribute,
// });
// //调式代码 用来打印SQL
// Db.Aop.OnLogExecuting = (sql, pars) =>
// {
// // Debug.WriteLine(sql);
// };
// Db.CodeFirst.InitTables(typeof(My12311x));
// var id= Db.Insertable(new My12311x { Menu_Id = new int[] { 1,2 } }).ExecuteReturnIdentity();
// var list = Db.Queryable<My12311x>().InSingle(id);
// list.Menu_Id = new int[] { 3};
// Db.Updateable(list).ExecuteCommand();
// list = Db.Queryable<My12311x>().InSingle(id);
// }
// }
// public class My12311x
// {
// [Key]
// [Display(Name = "ID")]
// [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
// public int Id { get; set; }
// [SugarColumn( ColumnDataType = "int []", IsArray =true)]
// public int[] Menu_Id { get; set; }
// }
//}

View File

@ -61,6 +61,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Bugs\BugTest.cs" />
<Compile Include="Bugs\BugTest2.cs" />
<Compile Include="Demo\Demo0_SqlSugarClient.cs" />
<Compile Include="Demo\Demo1_Queryable.cs" />
<Compile Include="Demo\Demo2_Updateable.cs" />

View File

@ -64,6 +64,7 @@ namespace SqlSugar
private static readonly MethodInfo getOtherNull = typeof(IDataRecordExtensions).GetMethod("GetOtherNull");
private static readonly MethodInfo getOther = typeof(IDataRecordExtensions).GetMethod("GetOther");
private static readonly MethodInfo getJson = typeof(IDataRecordExtensions).GetMethod("GetJson");
private static readonly MethodInfo getArray = typeof(IDataRecordExtensions).GetMethod("GetArray");
private static readonly MethodInfo getEntity = typeof(IDataRecordExtensions).GetMethod("GetEntity", new Type[] { typeof(SqlSugarProvider) });
private delegate T Load(IDataRecord dataRecord);
@ -153,6 +154,22 @@ namespace SqlSugar
generator.Emit(OpCodes.Callvirt, columnInfo.PropertyInfo.GetSetMethod(true));
generator.MarkLabel(endIfLabel);
}
if (columnInfo.IsArray)
{
MethodInfo arrayMehtod = getArray.MakeGenericMethod(columnInfo.PropertyInfo.PropertyType);
int i = DataRecord.GetOrdinal(fieldName);
Label endIfLabel = generator.DefineLabel();
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldc_I4, i);
generator.Emit(OpCodes.Callvirt, isDBNullMethod);
generator.Emit(OpCodes.Brtrue, endIfLabel);
generator.Emit(OpCodes.Ldloc, result);
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldc_I4, i);
generator.Emit(OpCodes.Call, arrayMehtod);
generator.Emit(OpCodes.Callvirt, columnInfo.PropertyInfo.GetSetMethod(true));
generator.MarkLabel(endIfLabel);
}
}
private void BindField(ILGenerator generator, LocalBuilder result, EntityColumnInfo columnInfo, string fieldName)
{

View File

@ -248,6 +248,14 @@ namespace SqlSugar
var value = obj.ObjToString();
return new SerializeService().DeserializeObject<T>(value);
}
public static T GetArray<T>(this IDataReader dr, int i)
{
//pgsql
var obj = dr.GetValue(i);
if (obj == null)
return default(T);
return (T)obj;
}
public static Nullable<T> GetConvertEnum_Null<T>(this IDataReader dr, int i) where T : struct
{