Optimized code

This commit is contained in:
sunkaixuan
2023-05-14 12:12:50 +08:00
parent d49261fac9
commit 2be7778141
2 changed files with 40 additions and 33 deletions

View File

@@ -759,39 +759,10 @@ namespace SqlSugar
public int IntoTable(Type TableEntityType,string TableName) public int IntoTable(Type TableEntityType,string TableName)
{ {
//var entityInfo = this.Context.EntityMaintenance.GetEntityInfo(TableEntityType); KeyValuePair<string, List<SugarParameter>> sqlInfo;
var sqlInfo = this.ToSql(); string sql;
var name = this.SqlBuilder.GetTranslationTableName(TableName); OutIntoTableSql(TableName, out sqlInfo, out sql);
var columns = "";
var sql = "";
var isSqlFunc = this.QueryBuilder.GetSelectValue?.Contains(")") == true && this.QueryBuilder.SelectValue is Expression;
if (isSqlFunc)
{
columns = "(";
foreach (var item in ExpressionTool.GetNewExpressionItemList((Expression)this.QueryBuilder.SelectValue))
{
var column = item.Key;
columns += $"{column},";
}
columns = columns.TrimEnd(',') + ")";
sql = $" INSERT INTO {name} {columns} " + sqlInfo.Key;
}
else
{
if (this.QueryBuilder.GetSelectValue != null && this.QueryBuilder.GetSelectValue.Contains(",")) ;
{
columns = "(";
foreach (var item in this.QueryBuilder.GetSelectValue.Split(','))
{
var column = Regex.Split(item, "AS").Last().Trim();
columns += $"{column},";
}
columns = columns.TrimEnd(',') + ")";
}
sql = $" INSERT INTO {name} {columns} " + sqlInfo.Key;
}
return this.Context.Ado.ExecuteCommand(sql, sqlInfo.Value); return this.Context.Ado.ExecuteCommand(sql, sqlInfo.Value);
} }
}
}
} }

View File

@@ -1187,6 +1187,42 @@ namespace SqlSugar
#endregion #endregion
#region Other #region Other
private void OutIntoTableSql(string TableName, out KeyValuePair<string, List<SugarParameter>> sqlInfo, out string sql)
{
//var entityInfo = this.Context.EntityMaintenance.GetEntityInfo(TableEntityType);
sqlInfo = this.ToSql();
var name = this.SqlBuilder.GetTranslationTableName(TableName);
var columns = "";
sql = "";
var isSqlFunc = this.QueryBuilder.GetSelectValue?.Contains(")") == true && this.QueryBuilder.SelectValue is Expression;
if (isSqlFunc)
{
columns = "(";
foreach (var item in ExpressionTool.GetNewExpressionItemList((Expression)this.QueryBuilder.SelectValue))
{
var column = item.Key;
columns += $"{column},";
}
columns = columns.TrimEnd(',') + ")";
sql = $" INSERT INTO {name} {columns} " + sqlInfo.Key;
}
else
{
if (this.QueryBuilder.GetSelectValue != null && this.QueryBuilder.GetSelectValue.Contains(",")) ;
{
columns = "(";
foreach (var item in this.QueryBuilder.GetSelectValue.Split(','))
{
var column = Regex.Split(item, "AS").Last().Trim();
columns += $"{column},";
}
columns = columns.TrimEnd(',') + ")";
}
sql = $" INSERT INTO {name} {columns} " + sqlInfo.Key;
}
}
private string GetTableName(EntityInfo entity, string tableName) private string GetTableName(EntityInfo entity, string tableName)
{ {
var oldTableName = tableName; var oldTableName = tableName;