From 79647852430cf61305b50ec4a78d02a5becb1076 Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@qq.com> Date: Thu, 29 Jun 2023 15:42:27 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E5=A2=9E=E5=8A=A0=20pgsql=20=E7=B4=A2?= =?UTF-8?q?=E5=BC=95=20B=5FTree,=20Hash,=20GiST,=20GIN,=20SP=5FGiST,=20BRI?= =?UTF-8?q?N=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Examples/base_entity/Program.cs | 25 ++-- Examples/base_entity/base_entity.csproj | 4 +- Examples/base_entity/pgsql_test.cs | 25 ++++ FreeSql.DbContext/FreeSql.DbContext.xml | 9 ++ FreeSql/DataAnnotations/IndexAttribute.cs | 11 ++ FreeSql/FreeSql.xml | 109 ++---------------- FreeSql/Internal/CommonUtils.cs | 6 +- FreeSql/Internal/Model/IndexInfo.cs | 1 + FreeSql/Internal/UtilsExpressionTree.cs | 6 +- .../PostgreSQLCodeFirst.cs | 15 ++- 10 files changed, 95 insertions(+), 116 deletions(-) create mode 100644 Examples/base_entity/pgsql_test.cs diff --git a/Examples/base_entity/Program.cs b/Examples/base_entity/Program.cs index eae8cb3d..a4bc603a 100644 --- a/Examples/base_entity/Program.cs +++ b/Examples/base_entity/Program.cs @@ -19,7 +19,6 @@ using System.ComponentModel; using System.Data.Common; using System.Data.Odbc; using System.Data.SqlClient; -using System.Data.SQLite; using System.Diagnostics; using System.Linq; using System.Linq.Expressions; @@ -35,7 +34,7 @@ using System.Threading.Tasks; namespace base_entity { - static class Program + static partial class Program { class TestConfig { @@ -62,11 +61,6 @@ namespace base_entity static AsyncLocal _asyncUow = new AsyncLocal(); - public class TestEnumCls - { - public CollationTypeEnum val { get; set; } = CollationTypeEnum.Binary; - } - class Sys_reg_user { public Guid Id { get; set; } @@ -590,11 +584,13 @@ namespace base_entity //.UseNameConvert(FreeSql.Internal.NameConvertType.ToUpper) //.UseConnectionString(FreeSql.DataType.OdbcDameng, "Driver={DM8 ODBC DRIVER};Server=127.0.0.1:5236;Persist Security Info=False;Trusted_Connection=Yes;UID=USER1;PWD=123456789") - .UseConnectionString(DataType.QuestDb, "host=localhost;port=8812;username=admin;password=quest;database=qdb;ServerCompatibilityMode=NoTypeLoading;") + //.UseConnectionString(DataType.QuestDb, "host=localhost;port=8812;username=admin;password=quest;database=qdb;ServerCompatibilityMode=NoTypeLoading;") .UseMonitorCommand(cmd => { Console.WriteLine(cmd.CommandText + "\r\n"); //cmd.CommandText = null; //不执行 + + //if (cmd.CommandText.StartsWith("")) }) .UseLazyLoading(true) .UseGenerateCommandParameterWithLambda(true) @@ -602,6 +598,19 @@ namespace base_entity BaseEntity.Initialization(fsql, () => _asyncUow.Value); #endregion + test_pgsql(fsql); + + var sqliteConnection = new Microsoft.Data.Sqlite.SqliteConnection("data source=123.db"); + using(var sqliteSql = new FreeSqlBuilder() + .UseConnectionFactory(DataType.Sqlite,() => sqliteConnection) + .UseAutoSyncStructure(true) + .UseMonitorCommand(cmd => Console.WriteLine(cmd.CommandText)) + .Build()) + { + sqliteSql.Insert(new User1 { Avatar = "xxxavatar" }).ExecuteAffrows(); + var xkdkd = sqliteSql.Select().ToList(); + } + var qr1 = fsql.SelectLongSequence(10, () => new { rndstr = QuestFunc.rnd_str(10, 5, 10, 0), diff --git a/Examples/base_entity/base_entity.csproj b/Examples/base_entity/base_entity.csproj index 930f5fcd..026d991b 100644 --- a/Examples/base_entity/base_entity.csproj +++ b/Examples/base_entity/base_entity.csproj @@ -3,6 +3,7 @@ Exe net5.0 + AnyCPU @@ -15,6 +16,7 @@ + @@ -32,7 +34,7 @@ - + diff --git a/Examples/base_entity/pgsql_test.cs b/Examples/base_entity/pgsql_test.cs new file mode 100644 index 00000000..0b8d89a2 --- /dev/null +++ b/Examples/base_entity/pgsql_test.cs @@ -0,0 +1,25 @@ +using FreeSql.DataAnnotations; +using NetTopologySuite.Geometries; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace base_entity +{ + partial class Program + { + public static void test_pgsql(IFreeSql fsql) + { + var ddl = fsql.CodeFirst.GetComparisonDDLStatements(); + } + } + + [Index("sidx_zjds_geom", nameof(Geom), IndexMethod = IndexMethod.GiST)] + class gistIndex + { + public int bb { get; set; } + public LineString Geom { get; set; } + } +} diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml index 537315e2..26522f10 100644 --- a/FreeSql.DbContext/FreeSql.DbContext.xml +++ b/FreeSql.DbContext/FreeSql.DbContext.xml @@ -800,5 +800,14 @@ + + + 批量注入 Repository,可以参考代码自行调整 + + + + + + diff --git a/FreeSql/DataAnnotations/IndexAttribute.cs b/FreeSql/DataAnnotations/IndexAttribute.cs index ed36653c..c4b31a24 100644 --- a/FreeSql/DataAnnotations/IndexAttribute.cs +++ b/FreeSql/DataAnnotations/IndexAttribute.cs @@ -47,5 +47,16 @@ namespace FreeSql.DataAnnotations /// 是否唯一 /// public bool IsUnique { get => _IsUnique ?? false; set => _IsUnique = value; } + + /// + /// 索引类型 + /// 暂时只有 FreeSql.Provider.PostgreSQL 有效 + /// + public IndexMethod IndexMethod { get; set; } } + + /// + /// 暂时只有 FreeSql.Provider.PostgreSQL 有效 + /// + public enum IndexMethod { B_Tree, Hash, GiST, GIN, SP_GiST, BRIN } } diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml index 661eed05..f65842e4 100644 --- a/FreeSql/FreeSql.xml +++ b/FreeSql/FreeSql.xml @@ -367,6 +367,17 @@ 是否唯一 + + + 索引类型 + 暂时只有 FreeSql.Provider.PostgreSQL 有效 + + + + + 暂时只有 FreeSql.Provider.PostgreSQL 有效 + + OneToOne:[Navigate(nameof(Primary))] <-> (缺省)外表.Primary @@ -1073,82 +1084,6 @@ - - - 动态创建实体类型 - - - - - 配置Class - - 类名 - 类标记的特性[Table(Name = "xxx")] [Index(xxxx)] - - - - - 配置属性 - - 属性名称 - 属性类型 - 属性标记的特性-支持多个 - - - - - 配置属性 - - 属性名称 - 属性类型 - 该属性是否重写父类属性 - 属性标记的特性-支持多个 - - - - - 配置属性 - - 属性名称 - 属性类型 - 该属性是否重写父类属性 - 属性默认值 - 属性标记的特性-支持多个 - - - - - 配置父类 - - 父类类型 - - - - - Override属性 - - - - - - Emit动态创建出Class - Type - - - - - - 首字母小写 - - - - - - - 首字母大写 - - - - 获取实体的主键值,以 "*|_,[,_|*" 分割,当任意一个主键属性无值时,返回 null @@ -5799,28 +5734,6 @@ 请使用 fsql.InsertDict(dict) 方法插入字典数据 - - - 动态构建Class Type - - - - - - 根据字典,创建 table 对应的实体对象 - - - - - - - - 根据实体对象,创建 table 对应的字典 - - - - - C#: that >= between && that <= and diff --git a/FreeSql/Internal/CommonUtils.cs b/FreeSql/Internal/CommonUtils.cs index ca3198d1..eb39d5cb 100644 --- a/FreeSql/Internal/CommonUtils.cs +++ b/FreeSql/Internal/CommonUtils.cs @@ -386,7 +386,7 @@ namespace FreeSql.Internal if (!string.IsNullOrEmpty(idxattr.Name) && !string.IsNullOrEmpty(idxattr.Fields)) { if (ret.ContainsKey(idxattr.Name)) ret.Remove(idxattr.Name); - ret.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique }); + ret.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique, IndexMethod = idxattr.IndexMethod }); } } break; @@ -397,7 +397,7 @@ namespace FreeSql.Internal if (!string.IsNullOrEmpty(idxattr.Name) && !string.IsNullOrEmpty(idxattr.Fields)) { if (ret.ContainsKey(idxattr.Name)) ret.Remove(idxattr.Name); - ret.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique }); + ret.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique, IndexMethod = idxattr.IndexMethod }); } } break; @@ -410,7 +410,7 @@ namespace FreeSql.Internal if (!string.IsNullOrEmpty(idxattr.Name) && !string.IsNullOrEmpty(idxattr.Fields)) { if (ret.ContainsKey(idxattr.Name)) ret.Remove(idxattr.Name); - ret.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique }); + ret.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique, IndexMethod = idxattr.IndexMethod }); } } break; diff --git a/FreeSql/Internal/Model/IndexInfo.cs b/FreeSql/Internal/Model/IndexInfo.cs index 149730f0..618f55e2 100644 --- a/FreeSql/Internal/Model/IndexInfo.cs +++ b/FreeSql/Internal/Model/IndexInfo.cs @@ -11,6 +11,7 @@ namespace FreeSql.Internal.Model public string Name { get; set; } public IndexColumnInfo[] Columns { get; set; } public bool IsUnique { get; set; } + public IndexMethod IndexMethod { get; set; } } public class IndexColumnInfo diff --git a/FreeSql/Internal/UtilsExpressionTree.cs b/FreeSql/Internal/UtilsExpressionTree.cs index 9f76543a..b23ef858 100644 --- a/FreeSql/Internal/UtilsExpressionTree.cs +++ b/FreeSql/Internal/UtilsExpressionTree.cs @@ -458,7 +458,8 @@ namespace FreeSql.Internal { Name = dbidx.Key, Columns = indexColumns.ToArray(), - IsUnique = dbidx.Value.IsUnique + IsUnique = dbidx.Value.IsUnique, + IndexMethod = IndexMethod.B_Tree }); } } @@ -493,7 +494,8 @@ namespace FreeSql.Internal { Name = indexName, Columns = indexColumns.ToArray(), - IsUnique = index.IsUnique + IsUnique = index.IsUnique, + IndexMethod = index.IndexMethod }); } trytb.Indexes = indexesDict.Values.ToArray(); diff --git a/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLCodeFirst.cs b/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLCodeFirst.cs index a626c718..8818e4e4 100644 --- a/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLCodeFirst.cs +++ b/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLCodeFirst.cs @@ -1,4 +1,5 @@ -using FreeSql.Internal; +using FreeSql.DataAnnotations; +using FreeSql.Internal; using FreeSql.Internal.Model; using Newtonsoft.Json.Linq; using Npgsql.LegacyPostgis; @@ -210,7 +211,9 @@ namespace FreeSql.PostgreSQL if (uk.IsUnique) sb.Append("UNIQUE "); sb.Append("INDEX "); if (isPg95) sb.Append("IF NOT EXISTS "); - sb.Append(_commonUtils.QuoteSqlName(ReplaceIndexName(uk.Name, tbname[1]))).Append(" ON ").Append(createTableName).Append("("); + sb.Append(_commonUtils.QuoteSqlName(ReplaceIndexName(uk.Name, tbname[1]))).Append(" ON ").Append(createTableName); + if (uk.IndexMethod != IndexMethod.B_Tree) sb.Append(" USING ").Append(uk.IndexMethod.ToString().ToUpper().Replace("_", "")); + sb.Append("("); foreach (var tbcol in uk.Columns) { sb.Append(_commonUtils.QuoteSqlName(tbcol.Column.Attribute.Name)); @@ -403,7 +406,9 @@ where ns.nspname in ({{0}}) and d.relname in ({{1}}) and a.indisprimary = 'f'", if (uk.IsUnique) sbalter.Append("UNIQUE "); sbalter.Append("INDEX "); if (isPg95) sbalter.Append("IF NOT EXISTS "); - sbalter.Append(_commonUtils.QuoteSqlName(ukname)).Append(" ON ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append("("); + sbalter.Append(_commonUtils.QuoteSqlName(ukname)).Append(" ON ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")); + if (uk.IndexMethod != IndexMethod.B_Tree) sbalter.Append(" USING ").Append(uk.IndexMethod.ToString().ToUpper().Replace("_", "")); + sbalter.Append("("); foreach (var tbcol in uk.Columns) { sbalter.Append(_commonUtils.QuoteSqlName(tbcol.Column.Attribute.Name)); @@ -500,7 +505,9 @@ where pg_namespace.nspname={0} and pg_class.relname={1} and pg_constraint.contyp if (uk.IsUnique) sb.Append("UNIQUE "); sb.Append("INDEX "); if (isPg95) sb.Append("IF NOT EXISTS "); - sb.Append(_commonUtils.QuoteSqlName(ReplaceIndexName(uk.Name, tbname[1]))).Append(" ON ").Append(tablename).Append("("); + sb.Append(_commonUtils.QuoteSqlName(ReplaceIndexName(uk.Name, tbname[1]))).Append(" ON ").Append(tablename); + if (uk.IndexMethod != IndexMethod.B_Tree) sb.Append(" USING ").Append(uk.IndexMethod.ToString().ToUpper().Replace("_", "")); + sb.Append("("); foreach (var tbcol in uk.Columns) { sb.Append(_commonUtils.QuoteSqlName(tbcol.Column.Attribute.Name));