mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 09:15:27 +08:00 
			
		
		
		
	- 优化 pgsql DbFirst 序列的识别,以及 pgsql10 的自增识别;
This commit is contained in:
		@@ -125,6 +125,13 @@
 | 
				
			|||||||
            清空状态数据
 | 
					            清空状态数据
 | 
				
			||||||
            </summary>
 | 
					            </summary>
 | 
				
			||||||
        </member>
 | 
					        </member>
 | 
				
			||||||
 | 
					        <member name="M:FreeSql.DbSet`1.RemoveAsync(System.Linq.Expressions.Expression{System.Func{`0,System.Boolean}})">
 | 
				
			||||||
 | 
					            <summary>
 | 
				
			||||||
 | 
					            根据 lambda 条件删除数据
 | 
				
			||||||
 | 
					            </summary>
 | 
				
			||||||
 | 
					            <param name="predicate"></param>
 | 
				
			||||||
 | 
					            <returns></returns>
 | 
				
			||||||
 | 
					        </member>
 | 
				
			||||||
        <member name="M:FreeSql.DbSet`1.Add(`0)">
 | 
					        <member name="M:FreeSql.DbSet`1.Add(`0)">
 | 
				
			||||||
            <summary>
 | 
					            <summary>
 | 
				
			||||||
            添加
 | 
					            添加
 | 
				
			||||||
@@ -513,5 +520,14 @@
 | 
				
			|||||||
            <param name="that"></param>
 | 
					            <param name="that"></param>
 | 
				
			||||||
            <returns></returns>
 | 
					            <returns></returns>
 | 
				
			||||||
        </member>
 | 
					        </member>
 | 
				
			||||||
 | 
					        <member name="M:Microsoft.Extensions.DependencyInjection.FreeSqlRepositoryDependencyInjection.AddFreeRepository(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action{FreeSql.FluentDataFilter},System.Reflection.Assembly[])">
 | 
				
			||||||
 | 
					            <summary>
 | 
				
			||||||
 | 
					            批量注入 Repository,可以参考代码自行调整
 | 
				
			||||||
 | 
					            </summary>
 | 
				
			||||||
 | 
					            <param name="services"></param>
 | 
				
			||||||
 | 
					            <param name="globalDataFilter"></param>
 | 
				
			||||||
 | 
					            <param name="assemblies"></param>
 | 
				
			||||||
 | 
					            <returns></returns>
 | 
				
			||||||
 | 
					        </member>
 | 
				
			||||||
    </members>
 | 
					    </members>
 | 
				
			||||||
</doc>
 | 
					</doc>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +1,6 @@
 | 
				
			|||||||
using FreeSql.DataAnnotations;
 | 
					using FreeSql.DataAnnotations;
 | 
				
			||||||
using System;
 | 
					using System;
 | 
				
			||||||
 | 
					using System.Linq;
 | 
				
			||||||
using Xunit;
 | 
					using Xunit;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace FreeSql.Tests.Odbc.PostgreSQL
 | 
					namespace FreeSql.Tests.Odbc.PostgreSQL
 | 
				
			||||||
@@ -20,6 +21,9 @@ namespace FreeSql.Tests.Odbc.PostgreSQL
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            var t2 = g.pgsql.DbFirst.GetTablesByDatabase(g.pgsql.DbFirst.GetDatabases()[2]);
 | 
					            var t2 = g.pgsql.DbFirst.GetTablesByDatabase(g.pgsql.DbFirst.GetDatabases()[2]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            var tb_alltype = t2.Where(a => a.Name == "tb_alltype").FirstOrDefault();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            var tb_identity = t2.Where(a => a.Name == "test_new").FirstOrDefault();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -23,6 +23,8 @@ namespace FreeSql.Tests.PostgreSQL
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            var tb_alltype = t2.Where(a => a.Name == "tb_alltype").FirstOrDefault();
 | 
					            var tb_alltype = t2.Where(a => a.Name == "tb_alltype").FirstOrDefault();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            var tb_identity = t2.Where(a => a.Name == "test_new").FirstOrDefault();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -215,7 +215,7 @@ where ns.nspname = {0} and c.relname = {1}", tboldname ?? tbname);
 | 
				
			|||||||
                        sqlType = string.Concat(sqlType),
 | 
					                        sqlType = string.Concat(sqlType),
 | 
				
			||||||
                        max_length = long.Parse(string.Concat(a[2])),
 | 
					                        max_length = long.Parse(string.Concat(a[2])),
 | 
				
			||||||
                        is_nullable = string.Concat(a[4]) == "1",
 | 
					                        is_nullable = string.Concat(a[4]) == "1",
 | 
				
			||||||
                        is_identity = string.Concat(a[5]).StartsWith(@"NEXTVAL('") && string.Concat(a[5]).EndsWith(@"'::REGCLASS)"),
 | 
					                        is_identity = string.Concat(a[5]).StartsWith(@"NEXTVAL('") && (string.Concat(a[5]).EndsWith(@"'::REGCLASS)") || string.Concat(a[5]).EndsWith(@"')")),
 | 
				
			||||||
                        attndims,
 | 
					                        attndims,
 | 
				
			||||||
                        comment = string.Concat(a[7])
 | 
					                        comment = string.Concat(a[7])
 | 
				
			||||||
                    };
 | 
					                    };
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -247,7 +247,7 @@ where {loc8.ToString().Replace("a.table_name", "ns.nspname || '.' || c.relname")
 | 
				
			|||||||
                    var max_length = int.Parse(string.Concat(row[3]));
 | 
					                    var max_length = int.Parse(string.Concat(row[3]));
 | 
				
			||||||
                    var sqlType = string.Concat(row[4]);
 | 
					                    var sqlType = string.Concat(row[4]);
 | 
				
			||||||
                    var is_nullable = string.Concat(row[5]) == "1";
 | 
					                    var is_nullable = string.Concat(row[5]) == "1";
 | 
				
			||||||
                    var is_identity = string.Concat(row[6]).StartsWith(@"NEXTVAL('") && string.Concat(row[6]).EndsWith(@"'::REGCLASS)");
 | 
					                    var is_identity = string.Concat(row[6]).StartsWith(@"NEXTVAL('") && (string.Concat(row[6]).EndsWith(@"'::REGCLASS)") || string.Concat(row[6]).EndsWith(@"')"));
 | 
				
			||||||
                    var comment = string.Concat(row[7]);
 | 
					                    var comment = string.Concat(row[7]);
 | 
				
			||||||
                    var defaultValue = string.Concat(row[6]);
 | 
					                    var defaultValue = string.Concat(row[6]);
 | 
				
			||||||
                    int attndims = int.Parse(string.Concat(row[8]));
 | 
					                    int attndims = int.Parse(string.Concat(row[8]));
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -227,7 +227,7 @@ where ns.nspname = {0} and c.relname = {1}", tboldname ?? tbname);
 | 
				
			|||||||
                        sqlType = string.Concat(sqlType),
 | 
					                        sqlType = string.Concat(sqlType),
 | 
				
			||||||
                        max_length = long.Parse(string.Concat(a[2])),
 | 
					                        max_length = long.Parse(string.Concat(a[2])),
 | 
				
			||||||
                        is_nullable = string.Concat(a[4]) == "1",
 | 
					                        is_nullable = string.Concat(a[4]) == "1",
 | 
				
			||||||
                        is_identity = string.Concat(a[5]).StartsWith(@"nextval('") && string.Concat(a[5]).EndsWith(@"'::regclass)"),
 | 
					                        is_identity = string.Concat(a[5]).StartsWith(@"nextval('") && (string.Concat(a[5]).EndsWith(@"'::regclass)") || string.Concat(a[5]).EndsWith(@"')")),
 | 
				
			||||||
                        attndims,
 | 
					                        attndims,
 | 
				
			||||||
                        comment = string.Concat(a[7])
 | 
					                        comment = string.Concat(a[7])
 | 
				
			||||||
                    };
 | 
					                    };
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -23,6 +23,28 @@ namespace FreeSql.Odbc.PostgreSQL
 | 
				
			|||||||
            _commonExpression = commonExpression;
 | 
					            _commonExpression = commonExpression;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public bool IsPg10 => ServerVersion >= 10;
 | 
				
			||||||
 | 
					        public int ServerVersion
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            get
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                if (_ServerVersionValue == 0 && _orm.Ado.MasterPool != null)
 | 
				
			||||||
 | 
					                    using (var conn = _orm.Ado.MasterPool.Get())
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        try
 | 
				
			||||||
 | 
					                        {
 | 
				
			||||||
 | 
					                            _ServerVersionValue = int.Parse(conn.Value.ServerVersion.Split('.')[0]);
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                        catch
 | 
				
			||||||
 | 
					                        {
 | 
				
			||||||
 | 
					                            _ServerVersionValue = 9;
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                return _ServerVersionValue;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        int _ServerVersionValue = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public int GetDbType(DbColumnInfo column) => (int)GetOdbcType(column);
 | 
					        public int GetDbType(DbColumnInfo column) => (int)GetOdbcType(column);
 | 
				
			||||||
        OdbcType GetOdbcType(DbColumnInfo column)
 | 
					        OdbcType GetOdbcType(DbColumnInfo column)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
@@ -217,7 +239,7 @@ d.description as comment,
 | 
				
			|||||||
a.attndims,
 | 
					a.attndims,
 | 
				
			||||||
case when t.typelem = 0 then t.typtype else t2.typtype end,
 | 
					case when t.typelem = 0 then t.typtype else t2.typtype end,
 | 
				
			||||||
ns2.nspname,
 | 
					ns2.nspname,
 | 
				
			||||||
a.attnum
 | 
					a.attnum{(IsPg10 ? ", a.attidentity" : "")}
 | 
				
			||||||
from pg_class c
 | 
					from pg_class c
 | 
				
			||||||
inner join pg_attribute a on a.attnum > 0 and a.attrelid = c.oid
 | 
					inner join pg_attribute a on a.attnum > 0 and a.attrelid = c.oid
 | 
				
			||||||
inner join pg_type t on t.oid = a.atttypid
 | 
					inner join pg_type t on t.oid = a.atttypid
 | 
				
			||||||
@@ -239,7 +261,8 @@ where {loc8.ToString().Replace("a.table_name", "ns.nspname || '.' || c.relname")
 | 
				
			|||||||
                    var max_length = int.Parse(string.Concat(row[3]));
 | 
					                    var max_length = int.Parse(string.Concat(row[3]));
 | 
				
			||||||
                    var sqlType = string.Concat(row[4]);
 | 
					                    var sqlType = string.Concat(row[4]);
 | 
				
			||||||
                    var is_nullable = string.Concat(row[5]) == "1";
 | 
					                    var is_nullable = string.Concat(row[5]) == "1";
 | 
				
			||||||
                    var is_identity = string.Concat(row[6]).StartsWith(@"nextval('") && string.Concat(row[6]).EndsWith(@"'::regclass)");
 | 
					                    var is_identity = string.Concat(row[6]).StartsWith(@"nextval('") && (string.Concat(row[6]).EndsWith(@"'::regclass)") || string.Concat(row[6]).EndsWith(@"')"))
 | 
				
			||||||
 | 
					                        || IsPg10 && new[] { "a", "d" }.Contains(string.Concat(row[12])); //pg10 GENERATED { BY DEFAULT | AWAYS } AS IDENTITY
 | 
				
			||||||
                    var comment = string.Concat(row[7]);
 | 
					                    var comment = string.Concat(row[7]);
 | 
				
			||||||
                    var defaultValue = string.Concat(row[6]);
 | 
					                    var defaultValue = string.Concat(row[6]);
 | 
				
			||||||
                    int attndims = int.Parse(string.Concat(row[8]));
 | 
					                    int attndims = int.Parse(string.Concat(row[8]));
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -274,7 +274,7 @@ where ns.nspname = {0} and c.relname = {1}", tboldname ?? tbname);
 | 
				
			|||||||
                        sqlType = string.Concat(sqlType),
 | 
					                        sqlType = string.Concat(sqlType),
 | 
				
			||||||
                        max_length = long.Parse(string.Concat(a[2])),
 | 
					                        max_length = long.Parse(string.Concat(a[2])),
 | 
				
			||||||
                        is_nullable = string.Concat(a[4]) == "1",
 | 
					                        is_nullable = string.Concat(a[4]) == "1",
 | 
				
			||||||
                        is_identity = string.Concat(a[5]).StartsWith(@"nextval('") && string.Concat(a[5]).EndsWith(@"'::regclass)"),
 | 
					                        is_identity = string.Concat(a[5]).StartsWith(@"nextval('") && (string.Concat(a[5]).EndsWith(@"'::regclass)") || string.Concat(a[5]).EndsWith(@"')")), //pgsql10
 | 
				
			||||||
                        attndims,
 | 
					                        attndims,
 | 
				
			||||||
                        comment = string.Concat(a[7])
 | 
					                        comment = string.Concat(a[7])
 | 
				
			||||||
                    };
 | 
					                    };
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -29,6 +29,28 @@ namespace FreeSql.PostgreSQL
 | 
				
			|||||||
            _commonExpression = commonExpression;
 | 
					            _commonExpression = commonExpression;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public bool IsPg10 => ServerVersion >= 10;
 | 
				
			||||||
 | 
					        public int ServerVersion
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            get
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                if (_ServerVersionValue == 0 && _orm.Ado.MasterPool != null)
 | 
				
			||||||
 | 
					                    using (var conn = _orm.Ado.MasterPool.Get())
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        try
 | 
				
			||||||
 | 
					                        {
 | 
				
			||||||
 | 
					                            _ServerVersionValue = int.Parse(conn.Value.ServerVersion.Split('.')[0]);
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                        catch
 | 
				
			||||||
 | 
					                        {
 | 
				
			||||||
 | 
					                            _ServerVersionValue = 9;
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                return _ServerVersionValue;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        int _ServerVersionValue = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public int GetDbType(DbColumnInfo column) => (int)GetNpgsqlDbType(column);
 | 
					        public int GetDbType(DbColumnInfo column) => (int)GetNpgsqlDbType(column);
 | 
				
			||||||
        NpgsqlDbType GetNpgsqlDbType(DbColumnInfo column)
 | 
					        NpgsqlDbType GetNpgsqlDbType(DbColumnInfo column)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
@@ -327,7 +349,7 @@ d.description as comment,
 | 
				
			|||||||
a.attndims,
 | 
					a.attndims,
 | 
				
			||||||
case when t.typelem = 0 then t.typtype else t2.typtype end,
 | 
					case when t.typelem = 0 then t.typtype else t2.typtype end,
 | 
				
			||||||
ns2.nspname,
 | 
					ns2.nspname,
 | 
				
			||||||
a.attnum
 | 
					a.attnum{(IsPg10 ? ", a.attidentity" : "")}
 | 
				
			||||||
from pg_class c
 | 
					from pg_class c
 | 
				
			||||||
inner join pg_attribute a on a.attnum > 0 and a.attrelid = c.oid
 | 
					inner join pg_attribute a on a.attnum > 0 and a.attrelid = c.oid
 | 
				
			||||||
inner join pg_type t on t.oid = a.atttypid
 | 
					inner join pg_type t on t.oid = a.atttypid
 | 
				
			||||||
@@ -349,7 +371,8 @@ where {loc8.ToString().Replace("a.table_name", "ns.nspname || '.' || c.relname")
 | 
				
			|||||||
                    var max_length = int.Parse(string.Concat(row[3]));
 | 
					                    var max_length = int.Parse(string.Concat(row[3]));
 | 
				
			||||||
                    var sqlType = string.Concat(row[4]);
 | 
					                    var sqlType = string.Concat(row[4]);
 | 
				
			||||||
                    var is_nullable = string.Concat(row[5]) == "1";
 | 
					                    var is_nullable = string.Concat(row[5]) == "1";
 | 
				
			||||||
                    var is_identity = string.Concat(row[6]).StartsWith(@"nextval('") && string.Concat(row[6]).EndsWith(@"'::regclass)");
 | 
					                    var is_identity = string.Concat(row[6]).StartsWith(@"nextval('") && (string.Concat(row[6]).EndsWith(@"'::regclass)") || string.Concat(row[6]).EndsWith(@"')"))
 | 
				
			||||||
 | 
					                        || IsPg10 && new[] { "a", "d" }.Contains(string.Concat(row[12])); //pg10 GENERATED { BY DEFAULT | AWAYS } AS IDENTITY
 | 
				
			||||||
                    var comment = string.Concat(row[7]);
 | 
					                    var comment = string.Concat(row[7]);
 | 
				
			||||||
                    var defaultValue = string.Concat(row[6]);
 | 
					                    var defaultValue = string.Concat(row[6]);
 | 
				
			||||||
                    int attndims = int.Parse(string.Concat(row[8]));
 | 
					                    int attndims = int.Parse(string.Concat(row[8]));
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -227,7 +227,7 @@ where ns.nspname = {0} and c.relname = {1}", tboldname ?? tbname);
 | 
				
			|||||||
                        sqlType = string.Concat(sqlType),
 | 
					                        sqlType = string.Concat(sqlType),
 | 
				
			||||||
                        max_length = long.Parse(string.Concat(a[2])),
 | 
					                        max_length = long.Parse(string.Concat(a[2])),
 | 
				
			||||||
                        is_nullable = string.Concat(a[4]) == "1",
 | 
					                        is_nullable = string.Concat(a[4]) == "1",
 | 
				
			||||||
                        is_identity = string.Concat(a[5]).StartsWith(@"NEXTVAL('") && string.Concat(a[5]).EndsWith(@"'::text)"),
 | 
					                        is_identity = string.Concat(a[5]).StartsWith(@"NEXTVAL('") && (string.Concat(a[5]).EndsWith(@"'::text)") || string.Concat(a[5]).EndsWith(@"')")),
 | 
				
			||||||
                        attndims,
 | 
					                        attndims,
 | 
				
			||||||
                        comment = string.Concat(a[7])
 | 
					                        comment = string.Concat(a[7])
 | 
				
			||||||
                    };
 | 
					                    };
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -259,7 +259,7 @@ where {loc8.ToString().Replace("a.table_name", "ns.nspname || '.' || c.relname")
 | 
				
			|||||||
                    var max_length = int.Parse(string.Concat(row[3]));
 | 
					                    var max_length = int.Parse(string.Concat(row[3]));
 | 
				
			||||||
                    var sqlType = string.Concat(row[4]);
 | 
					                    var sqlType = string.Concat(row[4]);
 | 
				
			||||||
                    var is_nullable = string.Concat(row[5]) == "1";
 | 
					                    var is_nullable = string.Concat(row[5]) == "1";
 | 
				
			||||||
                    var is_identity = string.Concat(row[6]).StartsWith(@"NEXTVAL('") && string.Concat(row[6]).EndsWith(@"'::text)");
 | 
					                    var is_identity = string.Concat(row[6]).StartsWith(@"NEXTVAL('") && (string.Concat(row[6]).EndsWith(@"'::text)") || string.Concat(row[6]).EndsWith(@"')"));
 | 
				
			||||||
                    var comment = string.Concat(row[7]);
 | 
					                    var comment = string.Concat(row[7]);
 | 
				
			||||||
                    var defaultValue = string.Concat(row[6]);
 | 
					                    var defaultValue = string.Concat(row[6]);
 | 
				
			||||||
                    int attndims = int.Parse(string.Concat(row[8]));
 | 
					                    int attndims = int.Parse(string.Concat(row[8]));
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user