Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IDbFirst接口添加GetTablesNameByDatabase方法,获取指定数据库所有表名. #1050

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Examples/orm_vs_net40/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/></startup></configuration>
6 changes: 5 additions & 1 deletion Examples/orm_vs_net40/orm_vs_net40.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
<OutputType>Exe</OutputType>
<RootNamespace>orm_vs_net40</RootNamespace>
<AssemblyName>orm_vs_net40</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -21,6 +22,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -30,6 +32,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand Down Expand Up @@ -70,6 +73,7 @@
<None Include="..\..\.editorconfig">
<Link>.editorconfig</Link>
</None>
<None Include="app.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45;net40</TargetFrameworks>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<Version>2.6.100</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>FreeSql;ncc;YeXiangQin</Authors>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45;net40</TargetFrameworks>
<TargetFrameworks>netstandard2.0;</TargetFrameworks>
<Version>2.6.100</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>FreeSql;ncc;YeXiangQin</Authors>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net45;net40</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<Version>2.6.100</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>FreeSql;ncc;YeXiangQin</Authors>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45;net40</TargetFrameworks>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<Version>2.6.100</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>FreeSql;ncc;YeXiangQin</Authors>
Expand Down
2 changes: 1 addition & 1 deletion FreeSql.All/FreeSql.All.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45;net40</TargetFrameworks>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<Version>2.6.100</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>FreeSql;ncc;YeXiangQin</Authors>
Expand Down
2 changes: 1 addition & 1 deletion FreeSql.DbContext/FreeSql.DbContext.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net50;netcoreapp31;netcoreapp21;net45;net40</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net50;netcoreapp31;netcoreapp21</TargetFrameworks>
<Version>2.6.100</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>FreeSql;ncc;YeXiangQin</Authors>
Expand Down
2 changes: 1 addition & 1 deletion FreeSql.Repository/FreeSql.Repository.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net50;netcoreapp31;netcoreapp21;net45;net40</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net50;netcoreapp31;netcoreapp21</TargetFrameworks>
<Version>2.6.100</Version>
<Authors>FreeSql;ncc;YeXiangQin</Authors>
<Description>FreeSql Implementation of General Repository, Support MySql/SqlServer/PostgreSQL/Oracle/Sqlite/Firebird/达梦/人大金仓/神舟通用/翰高/Access, and read/write separation、and split table.</Description>
Expand Down
2 changes: 1 addition & 1 deletion FreeSql/FreeSql.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45;net40</TargetFrameworks>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<Version>2.6.100</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>FreeSql;ncc;YeXiangQin</Authors>
Expand Down
7 changes: 7 additions & 0 deletions FreeSql/FreeSql.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion FreeSql/Interface/IDbFirst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ public interface IDbFirst
/// <param name="database"></param>
/// <returns></returns>
List<DbTableInfo> GetTablesByDatabase(params string[] database);

/// <summary>
/// 获取指定数据库的表名
/// </summary>
/// <param name="database"></param>
/// <returns></returns>
List<string> GetTablesNameByDatabase(params string[] database);
/// <summary>
/// 获取指定单表信息,包括列详情、主键、唯一键、索引、备注
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45;net40</TargetFrameworks>
<TargetFrameworks>netstandard2.0;</TargetFrameworks>
<Version>2.6.100</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>FreeSql;ncc;YeXiangQin</Authors>
Expand Down
28 changes: 28 additions & 0 deletions Providers/FreeSql.Provider.Dameng/DamengDbFirst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,34 @@ all_cons_columns d --主键表
return loc1;
}

public List<string> GetTablesNameByDatabase(params string[] database)
{
if (database == null || database.Any() == false)
{
var userUsers = _orm.Ado.ExecuteScalar(" select username from user_users")?.ToString();
if (string.IsNullOrEmpty(userUsers)) return new List<string>();
database = new[] { userUsers };
}

var databaseIn = string.Join(",", database.Select(a => _commonUtils.FormatSql("{0}", a)));

var sql = $@"
select
a.table_name
from all_tables a
where lower(a.owner) in ({databaseIn})

UNION ALL

select
a.view_name
from all_views a
where lower(a.owner) in ({databaseIn})";
var ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return new List<string>();
return ds.Select(x => x[0] as string).ToList();

}
public List<DbEnumInfo> GetEnumsByDatabase(params string[] database)
{
return new List<DbEnumInfo>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45;net40</TargetFrameworks>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<Version>2.6.100</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>FreeSql;ncc;YeXiangQin</Authors>
Expand Down
11 changes: 11 additions & 0 deletions Providers/FreeSql.Provider.Firebird/FirebirdDbFirst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,17 @@ from rdb$indices d
return loc1;
}

public List<string> GetTablesNameByDatabase(params string[] database)
{
var sql = @"
select
trim(rdb$relation_name) as name
from rdb$relations
where rdb$system_flag=0;";
var ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return new List<string>();
return ds.Select(z => z[0] as string).ToList();
}
public List<DbEnumInfo> GetEnumsByDatabase(params string[] database)
{
return new List<DbEnumInfo>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net452</TargetFrameworks>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<Version>2.6.100</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>FreeSql;ncc;YeXiangQin</Authors>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<Version>2.6.100</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>FreeSql;ncc;YeXiangQin</Authors>
Expand Down
37 changes: 37 additions & 0 deletions Providers/FreeSql.Provider.KingbaseES/KingbaseESDbFirst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,43 @@ from sys_constraint a
return tables;
}

public List<string> GetTablesNameByDatabase(params string[] database)
{
var olddatabase = "";
using (var conn = _orm.Ado.MasterPool.Get(TimeSpan.FromSeconds(5)))
{
olddatabase = conn.Value.Database;
}
string[] dbs = database == null || database.Any() == false ? new[] { olddatabase } : database;

var result = new List<string>();
foreach (var db in dbs)
{
if (string.IsNullOrEmpty(db) || string.Compare(db, olddatabase, true) != 0) continue;

var sql = $@"select
a.tablename
from sys_tables a
where
a.schemaname not in ('SYS_CATALOG', 'INFORMATION_SCHEMA', 'TOPOLOGY', 'SYSAUDIT', 'SYSLOGICAL', 'SYS_TEMP_1', 'SYS_TOAST', 'SYS_TOAST_TEMP_1', 'XLOG_RECORD_READ')
and
a.tablename not in ('PUBLIC.SPATIAL_REF_SYS')

union all

select
a.relname
from sys_class a
where a.relkind in ('m','v')
and
a.relname not in ('PUBLIC.GEOGRAPHY_COLUMNS','PUBLIC.GEOMETRY_COLUMNS','PUBLIC.RASTER_COLUMNS','PUBLIC.RASTER_OVERVIEWS')
;";
var ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return result;
result.AddRange(ds.Select(z => z[0] as string));
}
return result;
}
public class GetEnumsByDatabaseQueryInfo
{
public string name { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45;net40</TargetFrameworks>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<Version>2.6.100</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>FreeSql;ncc;YeXiangQin</Authors>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net452;net451;net45;net40</TargetFrameworks>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<Version>2.6.100</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>FreeSql;ncc;YeXiangQin</Authors>
Expand Down
23 changes: 23 additions & 0 deletions Providers/FreeSql.Provider.MySql/MySqlDbFirst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,29 @@ from information_schema.key_column_usage a
return loc1;
}

public List<string> GetTablesNameByDatabase(params string[] database)
{
if (database == null || database.Any() == false)
{
using (var conn = _orm.Ado.MasterPool.Get())
{
if (string.IsNullOrEmpty(conn.Value.Database)) return new List<string>();
database = new[] { conn.Value.Database };
}
}

var databaseIn = string.Join(",", database.Select(a => _commonUtils.FormatSql("{0}", a)));
var sql = $@"
select
a.table_name 'table'
from information_schema.tables a
where lower(a.table_schema) in ({databaseIn})";
var ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
var result = new List<string>();
if (ds == null) return result;
result.AddRange(ds.Select(z => z[0] as string));
return result;
}
public List<DbEnumInfo> GetEnumsByDatabase(params string[] database)
{
return new List<DbEnumInfo>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net50;net45;netstandard2.1;netcoreapp2.1;netcoreapp3.1;</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net50;netstandard2.1;netcoreapp2.1;netcoreapp3.1;</TargetFrameworks>
<Version>2.6.100</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>FreeSql;ncc;YeXiangQin</Authors>
Expand Down
28 changes: 28 additions & 0 deletions Providers/FreeSql.Provider.Odbc/Dameng/OdbcDamengDbFirst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,34 @@ public bool ExistsTable(string name, bool ignoreCase)
public DbTableInfo GetTableByName(string name, bool ignoreCase = true) => GetTables(null, name, ignoreCase)?.FirstOrDefault();
public List<DbTableInfo> GetTablesByDatabase(params string[] database) => GetTables(database, null, false);

public List<string> GetTablesNameByDatabase(params string[] database)
{
if (database == null || database.Any() == false)
{
var userUsers = _orm.Ado.ExecuteScalar(" select username from user_users")?.ToString();
if (string.IsNullOrEmpty(userUsers)) return new List<string>();
database = new[] { userUsers };
}

var databaseIn = string.Join(",", database.Select(a => _commonUtils.FormatSql("{0}", a)));

var sql = $@"
select
a.table_name
from all_tables a
where lower(a.owner) in ({databaseIn})

UNION ALL

select
a.view_name
from all_views a
where lower(a.owner) in ({databaseIn})";
var ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return new List<string>();
return ds.Select(x => x[0] as string).ToList();

}
public List<DbTableInfo> GetTables(string[] database, string tablename, bool ignoreCase)
{
var loc1 = new List<DbTableInfo>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45;net40</TargetFrameworks>
<TargetFrameworks>netstandard2.0;</TargetFrameworks>
<Version>2.6.100</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>FreeSql;ncc;YeXiangQin</Authors>
Expand Down
Loading