- 修复 PgCopy 处理 jsonb 类型的问题;#1532

This commit is contained in:
2881099 2023-06-15 19:34:56 +08:00
parent d1be9ec629
commit 17206e8ac9
2 changed files with 49 additions and 6 deletions

View File

@ -569,9 +569,9 @@ namespace base_entity
//.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3;TrustServerCertificate=true") //.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3;TrustServerCertificate=true")
//.UseConnectionString(FreeSql.DataType.PostgreSQL, "Host=192.168.164.10;Port=5432;Username=postgres;Password=123456;Database=tedb;Pooling=true;Maximum Pool Size=2") .UseConnectionString(FreeSql.DataType.PostgreSQL, "Host=192.168.164.10;Port=5432;Username=postgres;Password=123456;Database=tedb;Pooling=true;Maximum Pool Size=2")
////.UseConnectionString(FreeSql.DataType.PostgreSQL, "Host=192.168.164.10;Port=5432;Username=postgres;Password=123456;Database=toc;Pooling=true;Maximum Pool Size=2") //.UseConnectionString(FreeSql.DataType.PostgreSQL, "Host=192.168.164.10;Port=5432;Username=postgres;Password=123456;Database=toc;Pooling=true;Maximum Pool Size=2")
//.UseNameConvert(FreeSql.Internal.NameConvertType.ToLower) .UseNameConvert(FreeSql.Internal.NameConvertType.ToLower)
//.UseConnectionString(FreeSql.DataType.Oracle, "user id=user1;password=123456;data source=//127.0.0.1:1521/XE;Pooling=true;Max Pool Size=2") //.UseConnectionString(FreeSql.DataType.Oracle, "user id=user1;password=123456;data source=//127.0.0.1:1521/XE;Pooling=true;Max Pool Size=2")
//.UseNameConvert(FreeSql.Internal.NameConvertType.ToUpper) //.UseNameConvert(FreeSql.Internal.NameConvertType.ToUpper)
@ -602,6 +602,14 @@ namespace base_entity
BaseEntity.Initialization(fsql, () => _asyncUow.Value); BaseEntity.Initialization(fsql, () => _asyncUow.Value);
#endregion #endregion
fsql.Insert(new[]
{
new pgjson_copy001{ json = JObject.FromObject(new {x=1,y=2 })},
new pgjson_copy001{ json = JObject.FromObject(new {x=1,y=2 })},
new pgjson_copy001{ json = JObject.FromObject(new {x=1,y=2 })},
new pgjson_copy001{ json = JObject.FromObject(new {x=1,y=2 })},
}).ExecutePgCopy();
fsql.CodeFirst.GetTableByEntity(typeof(MFUser)).GetTableRef(nameof(MFUser.ExtInfo), true); fsql.CodeFirst.GetTableByEntity(typeof(MFUser)).GetTableRef(nameof(MFUser.ExtInfo), true);
fsql.Delete<MFUser>().Where(a => true).ExecuteAffrows(); fsql.Delete<MFUser>().Where(a => true).ExecuteAffrows();
fsql.Delete<MFUserExt>().Where(a => true).ExecuteAffrows(); fsql.Delete<MFUserExt>().Where(a => true).ExecuteAffrows();
@ -2446,4 +2454,10 @@ var sql11111 = fsql.Select<Class1111>()
public string? Source { get; set; } public string? Source { get; set; }
public DateTime EndTime { get; set; } public DateTime EndTime { get; set; }
} }
class pgjson_copy001
{
public Guid id { get; set; }
public JObject json { get; set; }
}
} }

View File

@ -3,6 +3,7 @@ using FreeSql.Internal.CommonProvider;
using FreeSql.Internal.Model; using FreeSql.Internal.Model;
using FreeSql.PostgreSQL.Curd; using FreeSql.PostgreSQL.Curd;
using Npgsql; using Npgsql;
using NpgsqlTypes;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
@ -173,7 +174,21 @@ public static partial class FreeSqlPostgreSQLGlobalExtensions
using (var writer = conn.BeginBinaryImport(copyFromCommand.ToString())) using (var writer = conn.BeginBinaryImport(copyFromCommand.ToString()))
{ {
foreach (DataRow item in dt.Rows) foreach (DataRow item in dt.Rows)
writer.WriteRow(item.ItemArray); {
writer.StartRow();
foreach (DataColumn col in dt.Columns)
{
NpgsqlDbType? npgsqlDbType = null;
var trycol = insert._table.Columns[col.ColumnName];
var tp = insert._orm.CodeFirst.GetDbInfo(trycol.Attribute.MapType)?.type;
if (tp != null) npgsqlDbType = (NpgsqlDbType)tp.Value;
if (npgsqlDbType.HasValue && npgsqlDbType != NpgsqlDbType.Unknown)
writer.Write(item[col.ColumnName], npgsqlDbType.Value);
else
writer.Write(item[col.ColumnName]);
}
//writer.WriteRow(item.ItemArray); #1532
}
writer.Complete(); writer.Complete();
} }
copyFromCommand.Clear(); copyFromCommand.Clear();
@ -258,8 +273,22 @@ public static partial class FreeSqlPostgreSQLGlobalExtensions
using (var writer = conn.BeginBinaryImport(copyFromCommand.ToString())) using (var writer = conn.BeginBinaryImport(copyFromCommand.ToString()))
{ {
foreach (DataRow item in dt.Rows) foreach (DataRow item in dt.Rows)
await writer.WriteRowAsync(cancellationToken, item.ItemArray); {
writer.Complete(); await writer.StartRowAsync(cancellationToken);
foreach (DataColumn col in dt.Columns)
{
NpgsqlDbType? npgsqlDbType = null;
var trycol = insert._table.Columns[col.ColumnName];
var tp = insert._orm.CodeFirst.GetDbInfo(trycol.Attribute.MapType)?.type;
if (tp != null) npgsqlDbType = (NpgsqlDbType)tp.Value;
if (npgsqlDbType.HasValue && npgsqlDbType != NpgsqlDbType.Unknown)
await writer.WriteAsync(item[col.ColumnName], npgsqlDbType.Value, cancellationToken);
else
await writer.WriteAsync(item[col.ColumnName], cancellationToken);
}
//await writer.WriteRowAsync(cancellationToken, item.ItemArray); #1532
}
await writer.CompleteAsync(cancellationToken);
} }
copyFromCommand.Clear(); copyFromCommand.Clear();
}; };