From 9fd461d508c0c2f83109c1d614c5c3db9fa17353 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sun, 31 Jul 2022 17:48:25 +0800 Subject: [PATCH] QuestDbSupport Index --- .../QuestDbTest/Demo/DemoE_CodeFirst.cs | 8 +++ .../DbMaintenance/QuestDBDbMaintenance.cs | 56 +++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/Src/Asp.NetCore2/QuestDbTest/Demo/DemoE_CodeFirst.cs b/Src/Asp.NetCore2/QuestDbTest/Demo/DemoE_CodeFirst.cs index 23e6ba1b3..db05be134 100644 --- a/Src/Asp.NetCore2/QuestDbTest/Demo/DemoE_CodeFirst.cs +++ b/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().ToList(); + db.CodeFirst.InitTables(); 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)] diff --git a/Src/Asp.NetCore2/SqlSugar/Realization/QuestDB/DbMaintenance/QuestDBDbMaintenance.cs b/Src/Asp.NetCore2/SqlSugar/Realization/QuestDB/DbMaintenance/QuestDBDbMaintenance.cs index 77d6ebff2..0e1571fce 100644 --- a/Src/Asp.NetCore2/SqlSugar/Realization/QuestDB/DbMaintenance/QuestDBDbMaintenance.cs +++ b/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("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 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 } }