コード例 #1
0
        private string ResovleBatchInsert(IEnumerable <T> entitys)
        {
            var table   = DbMetaInfoCache.GetTable(typeof(T)).TableName;
            var filters = new GroupExpressionResovle(_filterExpression).Resovle().Split(',');
            var columns = DbMetaInfoCache.GetColumns(typeof(T))
                          .Where(a => !a.IsComplexType).ToList();
            var intcolumns = columns
                             .Where(a => !filters.Contains(a.ColumnName) && !a.IsNotMapped && !a.IsIdentity)
                             .ToList();
            var columnNames = string.Join(",", intcolumns.Select(s => s.ColumnName));

            if (_context.DbContextType == DbContextType.Mysql)
            {
                var buffer = new StringBuilder();
                buffer.Append($"INSERT INTO {table}({columnNames}) VALUES ");
                var serializer = TypeConvert.GetDeserializer(typeof(T));
                var list       = entitys.ToList();
                for (var i = 0; i < list.Count; i++)
                {
                    var item   = list[i];
                    var values = serializer(item);
                    buffer.Append("(");
                    for (var j = 0; j < intcolumns.Count; j++)
                    {
                        var column = intcolumns[j];
                        var value  = values[column.CsharpName];
                        if (value == null)
                        {
                            buffer.Append(column.IsDefault ? "DEFAULT" : "NULL");
                        }
                        else if (column.CsharpType == typeof(bool) || column.CsharpType == typeof(bool?))
                        {
                            buffer.Append(Convert.ToBoolean(value) == true ? 1 : 0);
                        }
                        else if (column.CsharpType == typeof(DateTime) || column.CsharpType == typeof(DateTime?))
                        {
                            buffer.Append($"'{value}'");
                        }
                        else if (column.CsharpType == typeof(JsonElement) || column.CsharpType == typeof(JsonElement?))
                        {
                            buffer.Append($"'{value}'");
                        }
                        else if (column.CsharpType.IsValueType || (Nullable.GetUnderlyingType(column.CsharpType)?.IsValueType == true))
                        {
                            buffer.Append(value);
                        }
                        else
                        {
                            var str = SqlEncoding(value.ToString());
                            buffer.Append($"'{str}'");
                        }
                        if (j + 1 < intcolumns.Count)
                        {
                            buffer.Append(",");
                        }
                    }
                    buffer.Append(")");
                    if (i + 1 < list.Count)
                    {
                        buffer.Append(",");
                    }
                }
                return(buffer.Remove(buffer.Length - 1, 0).ToString());
            }
            throw new NotImplementedException();
        }