c# 使用COPY批量快速插入实体到PGSQL数据库

using Microsoft.Extensions.Configuration;
using Npgsql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

namespace Platform.Utils
{
    /// 
    /// 实体批量插入类
    /// 
    /// 
    public static class BulkInsert
    {
        /// 
        /// 实体表
        /// 
        private static Dictionary TableNameDict = new Dictionary();

        /// 
        /// 字符串配置
        /// 
        connectionstring here

        /// 
        /// 插入整个实体到数据库
        /// 
        /// 
        public static void BulkEntityInsert(List list)
        {
            var PropNames = new List();
            var typeArgs = GetPropertyFromTEntity();
            foreach (PropertyInfo tParam in typeArgs)
            {
                PropNames.Add(tParam.Name);
            }
            var copySql = CreatSQL(PropNames);
            Insert(copySql, list, typeArgs);
        }
        /// 
        /// 选择属性插入数据库
        /// 
        /// 
        /// 
        public static void BulkEntityInsert(List list, params Expression>[] expression)
        {
            var proper = new List();
            foreach (var param in expression)
            {
                var body = param.Body;
                if(body.Type == typeof(string))
                {
                    var prop = body.ToString();
                    proper.Add(prop.Substring(prop.IndexOf(".") + 1));
                }
                else
                {
                    var prop = body.ToString();
                    proper.Add(prop.Substring(0, prop.IndexOf(",")).Split(".")[1]);
                }
            }
            var copySql = CreatSQL(proper);
            var typeArgs = new List();
            foreach (var prop in proper)
            {
                typeArgs.Add(GetPropertyFromTEntity(prop));
            }
            Insert(copySql, list, typeArgs.ToArray());
        }
        /// 
        /// 获取数据库实体表名
        /// 
        /// 
        /// 
        private static object GetTableName()
        {
            var type = typeof(T);
            if (TableNameDict.ContainsKey(type)) return TableNameDict[type];

            else
            {
                var val = type.CustomAttributes.FirstOrDefault().ConstructorArguments.FirstOrDefault().Value;
                TableNameDict.Add(type, val);
                return val;
            }
        }
        /// 
        /// 获取TEntity的属性信息
        /// 
        /// TEntity的属性信息的列表
        private static PropertyInfo[] GetPropertyFromTEntity()
        {
            var t = typeof(TEntity);
            var typeArgs = t.GetProperties();
            return typeArgs;
        }
        /// 
        /// 获取TEntity的属性信息
        /// 
        /// TEntity的属性信息的列表
        private static PropertyInfo GetPropertyFromTEntity(string name)
        {
            var t = typeof(TEntity);
            var typeArg = t.GetProperty(name);
            return typeArg;
        }
        /// 
        /// 创建sql语句
        /// 
        /// 
        /// 
        private static string CreatSQL(List paramas)
        {
            var tableName = GetTableName().ToString();
            var proper = string.Empty;
            foreach(var parama in paramas)
            {
                proper += $"\"{parama}\",";
            }
            var copySql = $"copy {tableName}({proper.Substring(0,proper.LastIndexOf(","))}) FROM STDIN (FORMAT BINARY)";
            return copySql;
        }
        /// 
        /// 批量插入pgsql数据库
        /// 
        /// 
        /// 
        /// 
        private static void Insert(string copySql,List list, PropertyInfo[] typeArgs)
        {
            using (var conn = new NpgsqlConnection(ConnectionString))
            {
                conn.Open();
                using (var writer = conn.BeginBinaryImport(copySql))
                {
                    foreach (var wrapper in list)
                    {
                        writer.StartRow();
                        foreach (PropertyInfo tParam in typeArgs)
                        {
                            writer.Write(tParam.GetValue(wrapper));
                        }
                    }
                    writer.Complete();
                }
            }
        }
    }
}

你可能感兴趣的:(学习笔记)