mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-24 07:22:57 +08:00
Update Access
This commit is contained in:
parent
d568528dec
commit
fd9b4b2310
@ -8,6 +8,7 @@ using System.Data.OleDb;
|
|||||||
using System.Data.SqlClient;
|
using System.Data.SqlClient;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
namespace SqlSugar.Access
|
namespace SqlSugar.Access
|
||||||
{
|
{
|
||||||
@ -117,6 +118,49 @@ namespace SqlSugar.Access
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
public string SplitCommandTag = UtilConstants.ReplaceCommaKey;
|
||||||
|
public override int ExecuteCommand(string sql, SugarParameter[] parameters)
|
||||||
|
{
|
||||||
|
if (sql == null) throw new Exception("sql is null");
|
||||||
|
if (sql.IndexOf(this.SplitCommandTag) > 0)
|
||||||
|
{
|
||||||
|
var sqlParts = Regex.Split(sql, this.SplitCommandTag).Where(it => !string.IsNullOrEmpty(it)).ToList();
|
||||||
|
int result = 0;
|
||||||
|
foreach (var item in sqlParts)
|
||||||
|
{
|
||||||
|
if (item.TrimStart('\r').TrimStart('\n') != "")
|
||||||
|
{
|
||||||
|
result += base.ExecuteCommand(item, parameters);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return base.ExecuteCommand(sql, parameters);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public override async Task<int> ExecuteCommandAsync(string sql, SugarParameter[] parameters)
|
||||||
|
{
|
||||||
|
if (sql == null) throw new Exception("sql is null");
|
||||||
|
if (sql.IndexOf(this.SplitCommandTag) > 0)
|
||||||
|
{
|
||||||
|
var sqlParts = Regex.Split(sql, this.SplitCommandTag).Where(it => !string.IsNullOrEmpty(it)).ToList();
|
||||||
|
int result = 0;
|
||||||
|
foreach (var item in sqlParts)
|
||||||
|
{
|
||||||
|
if (item.TrimStart('\r').TrimStart('\n') != "")
|
||||||
|
{
|
||||||
|
result += await base.ExecuteCommandAsync(item, parameters);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return base.ExecuteCommand(sql, parameters);
|
||||||
|
}
|
||||||
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// if mysql return MySqlParameter[] pars
|
/// if mysql return MySqlParameter[] pars
|
||||||
/// if sqlerver return SqlParameter[] pars ...
|
/// if sqlerver return SqlParameter[] pars ...
|
||||||
|
@ -8,6 +8,13 @@ namespace SqlSugar.Access
|
|||||||
{
|
{
|
||||||
public class AccessInsertBuilder : InsertBuilder
|
public class AccessInsertBuilder : InsertBuilder
|
||||||
{
|
{
|
||||||
|
public override string SqlTemplateBatch
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "INSERT INTO {0} ({1})";
|
||||||
|
}
|
||||||
|
}
|
||||||
public override bool IsOleDb { get; set; } = true;
|
public override bool IsOleDb { get; set; } = true;
|
||||||
public override string SqlTemplate
|
public override string SqlTemplate
|
||||||
{
|
{
|
||||||
@ -47,42 +54,99 @@ namespace SqlSugar.Access
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
StringBuilder batchInsetrSql = new StringBuilder();
|
StringBuilder batchInsetrSql = new StringBuilder();
|
||||||
int pageSize = 200;
|
int pageSize = groupList.Count;
|
||||||
if (this.EntityInfo.Columns.Count > 30)
|
|
||||||
{
|
|
||||||
pageSize = 50;
|
|
||||||
}
|
|
||||||
else if (this.EntityInfo.Columns.Count > 20)
|
|
||||||
{
|
|
||||||
pageSize = 100;
|
|
||||||
}
|
|
||||||
int pageIndex = 1;
|
int pageIndex = 1;
|
||||||
int totalRecord = groupList.Count;
|
int totalRecord = groupList.Count;
|
||||||
int pageCount = (totalRecord + pageSize - 1) / pageSize;
|
int pageCount = (totalRecord + pageSize - 1) / pageSize;
|
||||||
while (pageCount >= pageIndex)
|
while (pageCount >= pageIndex)
|
||||||
{
|
{
|
||||||
batchInsetrSql.AppendFormat(SqlTemplateBatch, GetTableNameString, columnsString);
|
var temp=string.Format(SqlTemplateBatch, GetTableNameString, columnsString);
|
||||||
|
//batchInsetrSql.AppendFormat("\r\nSelect * FROM(");
|
||||||
int i = 0;
|
int i = 0;
|
||||||
foreach (var columns in groupList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList())
|
foreach (var columns in groupList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList())
|
||||||
{
|
{
|
||||||
var isFirst = i == 0;
|
//var isFirst = i == 0;
|
||||||
if (!isFirst)
|
//if (!isFirst)
|
||||||
{
|
//{
|
||||||
batchInsetrSql.Append(SqlTemplateBatchUnion);
|
// batchInsetrSql.Append("\t\r\nUNION ");
|
||||||
}
|
//}
|
||||||
batchInsetrSql.Append("\r\n SELECT " + string.Join(",", columns.Select(it => string.Format(SqlTemplateBatchSelect, FormatValue(it.Value), Builder.GetTranslationColumnName(it.DbColumnName)))));
|
//temp+" Values ( " + string.Join(",", columns.Select(it => FormatValue(it.Value) )+")"
|
||||||
|
batchInsetrSql.Append($"{temp} values ({string.Join(",", columns.Select(it => FormatValue(it.Value)))}) "+UtilConstants.ReplaceCommaKey);
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
pageIndex++;
|
pageIndex++;
|
||||||
batchInsetrSql.Append("\r\n;\r\n");
|
//batchInsetrSql.Append(") AS tblTemp\r\n");
|
||||||
}
|
}
|
||||||
var result = batchInsetrSql.ToString();
|
var result = batchInsetrSql.ToString();
|
||||||
if (this.Context.CurrentConnectionConfig.DbType == DbType.SqlServer)
|
|
||||||
{
|
|
||||||
result += "select SCOPE_IDENTITY();";
|
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public override object FormatValue(object value)
|
||||||
|
{
|
||||||
|
var n = "";
|
||||||
|
if (value == null)
|
||||||
|
{
|
||||||
|
return "NULL";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var type = UtilMethods.GetUnderType(value.GetType());
|
||||||
|
if (type == UtilConstants.DateType)
|
||||||
|
{
|
||||||
|
return GetDateTimeString(value);
|
||||||
|
}
|
||||||
|
else if (value is DateTimeOffset)
|
||||||
|
{
|
||||||
|
return GetDateTimeOffsetString(value);
|
||||||
|
}
|
||||||
|
else if (type == UtilConstants.ByteArrayType)
|
||||||
|
{
|
||||||
|
string bytesString = "0x" + BitConverter.ToString((byte[])value).Replace("-", "");
|
||||||
|
return bytesString;
|
||||||
|
}
|
||||||
|
else if (type.IsEnum())
|
||||||
|
{
|
||||||
|
if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true)
|
||||||
|
{
|
||||||
|
return value.ToSqlValue(); ;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return Convert.ToInt64(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (SqlSugar.UtilMethods.IsNumber(type.Name))
|
||||||
|
{
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
else if (type == UtilConstants.BoolType)
|
||||||
|
{
|
||||||
|
return value.ObjToBool() ? "1" : "0";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return n + "'" + value + "'";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private object GetDateTimeOffsetString(object value)
|
||||||
|
{
|
||||||
|
var date = UtilMethods.ConvertFromDateTimeOffset((DateTimeOffset)value);
|
||||||
|
if (date == DateTime.MinValue)
|
||||||
|
{
|
||||||
|
date = Convert.ToDateTime("1900-01-01");
|
||||||
|
}
|
||||||
|
return "'" + date.ToString("yyyy-MM-dd HH:mm") + "'";
|
||||||
|
}
|
||||||
|
|
||||||
|
private object GetDateTimeString(object value)
|
||||||
|
{
|
||||||
|
var date = value.ObjToDate();
|
||||||
|
if (date == DateTime.MinValue)
|
||||||
|
{
|
||||||
|
date = Convert.ToDateTime("1900-01-01");
|
||||||
|
}
|
||||||
|
return "'" + date.ToString("yyyy-MM-dd HH:mm") + "'";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user