Browse Source

QuestDbSupport Index

SqlSugar5
sunkaixuan 2 years ago
parent
commit
9fd461d508
  1. 8
      Src/Asp.NetCore2/QuestDbTest/Demo/DemoE_CodeFirst.cs
  2. 56
      Src/Asp.NetCore2/SqlSugar/Realization/QuestDB/DbMaintenance/QuestDBDbMaintenance.cs

8
Src/Asp.NetCore2/QuestDbTest/Demo/DemoE_CodeFirst.cs

@ -23,10 +23,18 @@ namespace OrmTest
db.CodeFirst.InitTables(typeof(CodeFirstTable1));//Create CodeFirstTable1
db.Insertable(new CodeFirstTable1() { Name = "a", Text="a" }).ExecuteCommand();
var list = db.Queryable<CodeFirstTable1>().ToList();
db.CodeFirst.InitTables<IndexClass>();
Console.WriteLine("#### CodeFirst end ####");
}
}
[SugarIndex(null,nameof(IndexClass.Name),OrderByType.Asc)]
public class IndexClass
{
public int Id { get; set; }
[SugarColumn(ColumnDataType ="symbol")]
public string Name { get; set; }
}
public class CodeFirstTable1
{
[SugarColumn(IsIdentity = true, IsPrimaryKey = true)]

56
Src/Asp.NetCore2/SqlSugar/Realization/QuestDB/DbMaintenance/QuestDBDbMaintenance.cs

@ -214,6 +214,53 @@ namespace SqlSugar
#endregion
#region Methods
public override void AddIndex(EntityInfo entityInfo)
{
if (entityInfo.Indexs != null)
{
foreach (var item in entityInfo.Indexs)
{
CreateIndex(entityInfo.DbTableName, item.IndexFields.Select(it => it.Key).ToArray());
}
}
}
public override bool CreateIndex(string tableName, string[] columnNames, bool isUnique = false)
{
if (isUnique)
throw new Exception("no support unique index");
var columnInfos = this.Context.Ado.SqlQuery<QuestDbColumn>("SHOW COLUMNS FROM '" + tableName + "'");
foreach (var columnInfo in columnInfos)
{
if (columnNames.Any(z => z.EqualCase(columnInfo.Column)))
{
if (!columnInfo.Type.EqualCase("SYMBOL"))
{
Check.ExceptionEasy(true, "Only the SYMBOL type can be indexed", $"字段{columnInfo.Column} 不是SYMBOL并且实体是string才能添加索引,CodeFirst需要指定类型: SYMBOL");
}
if (columnInfo.Indexed == false)
{
var indexSql = $"ALTER TABLE '{tableName}' ALTER COLUMN {columnInfo.Column} ADD INDEX ";
this.Context.Ado.ExecuteCommand(indexSql);
}
}
}
return true;
}
public override bool CreateIndex(string tableName, string[] columnNames, string IndexName, bool isUnique = false)
{
if(isUnique)
throw new Exception("no support unique index");
return CreateIndex(tableName, columnNames, isUnique);
}
public override bool CreateUniqueIndex(string tableName, string[] columnNames)
{
throw new Exception("no support unique index");
}
public override bool IsAnyIndex(string indexName)
{
return false;
}
public override List<DbTableInfo> GetTableInfoList(bool isCache = true)
{
var dt = this.Context.Ado.GetDataTable(GetTableInfoListSql);
@ -431,5 +478,14 @@ namespace SqlSugar
}
#endregion
#region HelperClass
internal class QuestDbColumn
{
public string Column { get; set; }
public string Type { get; set; }
public bool Indexed { get; set; }
}
#endregion
}
}

Loading…
Cancel
Save