Support sql index include

This commit is contained in:
sunkaixuan 2022-11-06 12:27:41 +08:00
parent f393ad2b3e
commit e234ec1d37
2 changed files with 21 additions and 2 deletions

View File

@ -268,6 +268,7 @@ namespace SqlSugar
{ {
item.IndexName = (this.Context.CurrentConnectionConfig.IndexSuffix+ item.IndexName); item.IndexName = (this.Context.CurrentConnectionConfig.IndexSuffix+ item.IndexName);
} }
var include = "";
if (item.IndexName != null) if (item.IndexName != null)
{ {
var database = "{db}"; var database = "{db}";
@ -280,6 +281,11 @@ namespace SqlSugar
{ {
item.IndexName = item.IndexName.Replace(table, entityInfo.DbTableName); item.IndexName = item.IndexName.Replace(table, entityInfo.DbTableName);
} }
if (item.IndexName.ToLower().Contains("{include:"))
{
include=Regex.Match( item.IndexName,@"\{include\:.+$").Value;
item.IndexName = item.IndexName.Replace(include, "");
}
} }
if (!this.Context.DbMaintenance.IsAnyIndex(item.IndexName)) if (!this.Context.DbMaintenance.IsAnyIndex(item.IndexName))
{ {
@ -296,7 +302,7 @@ namespace SqlSugar
return new KeyValuePair<string, OrderByType>(dbColumn.DbColumnName, it.Value); return new KeyValuePair<string, OrderByType>(dbColumn.DbColumnName, it.Value);
}) })
.Select(it => querybulder.GetTranslationColumnName(it.Key) + " " + it.Value).ToArray(); .Select(it => querybulder.GetTranslationColumnName(it.Key) + " " + it.Value).ToArray();
this.Context.DbMaintenance.CreateIndex(entityInfo.DbTableName, fileds, item.IndexName, item.IsUnique); this.Context.DbMaintenance.CreateIndex(entityInfo.DbTableName, fileds, item.IndexName+ include, item.IsUnique);
} }
} }
} }

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
namespace SqlSugar namespace SqlSugar
{ {
@ -467,7 +468,19 @@ namespace SqlSugar
} }
public virtual bool CreateIndex(string tableName, string[] columnNames, string IndexName, bool isUnique = false) public virtual bool CreateIndex(string tableName, string[] columnNames, string IndexName, bool isUnique = false)
{ {
string sql = string.Format("CREATE {3} INDEX {2} ON {0}({1})", tableName, string.Join(",", columnNames), IndexName, isUnique ? "UNIQUE" : ""); var include = "";
if (IndexName.ToLower().Contains("{include:"))
{
include = Regex.Match(IndexName, @"\{include\:.+$").Value;
IndexName = IndexName.Replace(include, "");
if (include == null)
{
throw new Exception("include format error");
}
include = include.Replace("{include:", "").Replace("}", "");
include = $"include({include})";
}
string sql = string.Format("CREATE {3} INDEX {2} ON {0}({1})"+ include, tableName, string.Join(",", columnNames), IndexName, isUnique ? "UNIQUE" : "");
this.Context.Ado.ExecuteCommand(sql); this.Context.Ado.ExecuteCommand(sql);
return true; return true;
} }