- 修复 DbFirst 当表数量过大时(如 oracle 表数量大于 1000),可能报错的 bug;

This commit is contained in:
28810 2019-10-08 23:58:10 +08:00
parent dde6562ef8
commit 47b8cd6d9a
8 changed files with 306 additions and 76 deletions

View File

@ -5,6 +5,7 @@ using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace FreeSql.MySql
@ -170,8 +171,10 @@ where a.table_schema in ({0})", databaseIn);
var ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return loc1;
var loc6 = new List<string>();
var loc66 = new List<string>();
var loc6 = new List<string[]>();
var loc66 = new List<string[]>();
var loc6_1000 = new List<string>();
var loc66_1000 = new List<string>();
foreach (var row in ds)
{
var table_id = string.Concat(row[0]);
@ -190,16 +193,40 @@ where a.table_schema in ({0})", databaseIn);
{
case DbTableType.TABLE:
case DbTableType.VIEW:
loc6.Add(table.Replace("'", "''"));
loc6_1000.Add(table.Replace("'", "''"));
if (loc6_1000.Count >= 500)
{
loc6.Add(loc6_1000.ToArray());
loc6_1000.Clear();
}
break;
case DbTableType.StoreProcedure:
loc66.Add(table.Replace("'", "''"));
loc66_1000.Add(table.Replace("'", "''"));
if (loc66_1000.Count >= 500)
{
loc66.Add(loc66_1000.ToArray());
loc66_1000.Clear();
}
break;
}
}
if (loc6_1000.Count > 0) loc6.Add(loc6_1000.ToArray());
if (loc66_1000.Count > 0) loc66.Add(loc66_1000.ToArray());
if (loc6.Count == 0) return loc1;
var loc8 = "'" + string.Join("','", loc6.ToArray()) + "'";
var loc88 = "'" + string.Join("','", loc66.ToArray()) + "'";
var loc8 = new StringBuilder().Append("(");
for (var loc8idx = 0; loc8idx < loc6.Count; loc8idx++)
{
if (loc8idx > 0) loc8.Append(" OR ");
loc8.Append("a.table_name in (");
for (var loc8idx2 = 0; loc8idx2 < loc6[loc8idx].Length; loc8idx2++)
{
if (loc8idx2 > 0) loc8.Append(",");
loc8.Append($"'{loc6[loc8idx][loc8idx2]}'");
}
loc8.Append(")");
}
loc8.Append(")");
sql = string.Format(@"
select
@ -212,7 +239,7 @@ case when a.is_nullable = 'YES' then 1 else 0 end 'is_nullable',
case when locate('auto_increment', a.extra) > 0 then 1 else 0 end 'is_identity',
a.column_comment 'comment'
from information_schema.columns a
where a.table_schema in ({1}) and a.table_name in ({0})
where a.table_schema in ({1}) and {0}
", loc8, databaseIn);
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return loc1;
@ -260,7 +287,7 @@ case when a.index_name = 'PRIMARY' then 1 else 0 end 'IsPrimaryKey',
0 'IsClustered',
0 'IsDesc'
from information_schema.statistics a
where a.table_schema in ({1}) and a.table_name in ({0}) and a.index_name <> 'PRIMARY'
where a.table_schema in ({1}) and {0} and a.index_name <> 'PRIMARY'
", loc8, databaseIn);
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return loc1;
@ -321,7 +348,7 @@ concat(a.referenced_table_schema, '.', a.referenced_table_name) 'ref_table_id',
1 'IsForeignKey',
a.referenced_column_name 'ref_column'
from information_schema.key_column_usage a
where a.constraint_schema in ({1}) and a.table_name in ({0}) and not isnull(position_in_unique_constraint)
where a.constraint_schema in ({1}) and {0} and not isnull(position_in_unique_constraint)
", loc8, databaseIn);
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return loc1;

View File

@ -5,6 +5,7 @@ using System.Collections.Generic;
using System.Data;
using System.Data.Odbc;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace FreeSql.Odbc.MySql
@ -140,8 +141,10 @@ where a.table_schema in ({0})", databaseIn);
var ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return loc1;
var loc6 = new List<string>();
var loc66 = new List<string>();
var loc6 = new List<string[]>();
var loc66 = new List<string[]>();
var loc6_1000 = new List<string>();
var loc66_1000 = new List<string>();
foreach (var row in ds)
{
var table_id = string.Concat(row[0]);
@ -160,16 +163,40 @@ where a.table_schema in ({0})", databaseIn);
{
case DbTableType.TABLE:
case DbTableType.VIEW:
loc6.Add(table.Replace("'", "''"));
loc6_1000.Add(table.Replace("'", "''"));
if (loc6_1000.Count >= 500)
{
loc6.Add(loc6_1000.ToArray());
loc6_1000.Clear();
}
break;
case DbTableType.StoreProcedure:
loc66.Add(table.Replace("'", "''"));
loc66_1000.Add(table.Replace("'", "''"));
if (loc66_1000.Count >= 500)
{
loc66.Add(loc66_1000.ToArray());
loc66_1000.Clear();
}
break;
}
}
if (loc6_1000.Count > 0) loc6.Add(loc6_1000.ToArray());
if (loc66_1000.Count > 0) loc66.Add(loc66_1000.ToArray());
if (loc6.Count == 0) return loc1;
var loc8 = "'" + string.Join("','", loc6.ToArray()) + "'";
var loc88 = "'" + string.Join("','", loc66.ToArray()) + "'";
var loc8 = new StringBuilder().Append("(");
for (var loc8idx = 0; loc8idx < loc6.Count; loc8idx++)
{
if (loc8idx > 0) loc8.Append(" OR ");
loc8.Append("a.table_name in (");
for (var loc8idx2 = 0; loc8idx2 < loc6[loc8idx].Length; loc8idx2++)
{
if (loc8idx2 > 0) loc8.Append(",");
loc8.Append($"'{loc6[loc8idx][loc8idx2]}'");
}
loc8.Append(")");
}
loc8.Append(")");
sql = string.Format(@"
select
@ -182,7 +209,7 @@ case when a.is_nullable = 'YES' then 1 else 0 end 'is_nullable',
case when locate('auto_increment', a.extra) > 0 then 1 else 0 end 'is_identity',
a.column_comment 'comment'
from information_schema.columns a
where a.table_schema in ({1}) and a.table_name in ({0})
where a.table_schema in ({1}) and {0}
", loc8, databaseIn);
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return loc1;
@ -230,7 +257,7 @@ case when a.index_name = 'PRIMARY' then 1 else 0 end 'IsPrimaryKey',
0 'IsClustered',
0 'IsDesc'
from information_schema.statistics a
where a.table_schema in ({1}) and a.table_name in ({0}) and a.index_name <> 'PRIMARY'
where a.table_schema in ({1}) and {0} and a.index_name <> 'PRIMARY'
", loc8, databaseIn);
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return loc1;
@ -291,7 +318,7 @@ concat(a.referenced_table_schema, '.', a.referenced_table_name) 'ref_table_id',
1 'IsForeignKey',
a.referenced_column_name 'ref_column'
from information_schema.key_column_usage a
where a.constraint_schema in ({1}) and a.table_name in ({0}) and not isnull(position_in_unique_constraint)
where a.constraint_schema in ({1}) and {0} and not isnull(position_in_unique_constraint)
", loc8, databaseIn);
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return loc1;

View File

@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Data;
using System.Data.Odbc;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace FreeSql.Odbc.Oracle
@ -191,8 +192,10 @@ where a.owner in ({0})", databaseIn);
var ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return loc1;
var loc6 = new List<string>();
var loc66 = new List<string>();
var loc6 = new List<string[]>();
var loc66 = new List<string[]>();
var loc6_1000 = new List<string>();
var loc66_1000 = new List<string>();
foreach (var row in ds)
{
var table_id = string.Concat(row[0]);
@ -211,16 +214,40 @@ where a.owner in ({0})", databaseIn);
{
case DbTableType.TABLE:
case DbTableType.VIEW:
loc6.Add(table.Replace("'", "''"));
loc6_1000.Add(table.Replace("'", "''"));
if (loc6_1000.Count >= 999)
{
loc6.Add(loc6_1000.ToArray());
loc6_1000.Clear();
}
break;
case DbTableType.StoreProcedure:
loc66.Add(table.Replace("'", "''"));
loc66_1000.Add(table.Replace("'", "''"));
if (loc66_1000.Count >= 999)
{
loc66.Add(loc66_1000.ToArray());
loc66_1000.Clear();
}
break;
}
}
if (loc6_1000.Count > 0) loc6.Add(loc6_1000.ToArray());
if (loc66_1000.Count > 0) loc66.Add(loc66_1000.ToArray());
if (loc6.Count == 0) return loc1;
var loc8 = "'" + string.Join("','", loc6.ToArray()) + "'";
var loc88 = "'" + string.Join("','", loc66.ToArray()) + "'";
var loc8 = new StringBuilder().Append("(");
for (var loc8idx = 0; loc8idx < loc6.Count; loc8idx++)
{
if (loc8idx > 0) loc8.Append(" OR ");
loc8.Append("a.table_name in (");
for (var loc8idx2 = 0; loc8idx2 < loc6[loc8idx].Length; loc8idx2++)
{
if (loc8idx2 > 0) loc8.Append(",");
loc8.Append($"'{loc6[loc8idx][loc8idx2]}'");
}
loc8.Append(")");
}
loc8.Append(")");
sql = string.Format(@"
select
@ -236,7 +263,7 @@ nvl((select 1 from user_sequences where upper(sequence_name)=upper(a.table_name|
b.comments
from all_tab_cols a
left join all_col_comments b on b.owner = a.owner and b.table_name = a.table_name and b.column_name = a.column_name
where a.owner in ({1}) and a.table_name in ({0})
where a.owner in ({1}) and {0}
", loc8, databaseIn);
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return loc1;
@ -303,7 +330,7 @@ all_ind_columns c
where a.index_name = c.index_name
and a.table_owner = c.table_owner
and a.table_name = c.table_name
and a.table_owner in ({1}) and a.table_name in ({0})
and a.table_owner in ({1}) and {0}
and not exists(select 1 from all_constraints where constraint_name = a.index_name and constraint_type = 'P')
", loc8, databaseIn);
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
@ -390,7 +417,7 @@ and a.owner = c.owner   
and a.table_name = c.table_name   
and b.owner = d.owner   
and b.table_name = d.table_name
and a.owner in ({1}) and a.table_name in ({0})
and a.owner in ({1}) and {0}
", loc8, databaseIn);
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return loc1;

View File

@ -5,6 +5,7 @@ using System.Collections.Generic;
using System.Data;
using System.Data.Odbc;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace FreeSql.Odbc.PostgreSQL
@ -147,8 +148,10 @@ and b.nspname || '.' || a.relname not in ('public.geography_columns','public.geo
var ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return loc1;
var loc6 = new List<string>();
var loc66 = new List<string>();
var loc6 = new List<string[]>();
var loc66 = new List<string[]>();
var loc6_1000 = new List<string>();
var loc66_1000 = new List<string>();
foreach (object[] row in ds)
{
var object_id = string.Concat(row[0]);
@ -162,16 +165,40 @@ and b.nspname || '.' || a.relname not in ('public.geography_columns','public.geo
{
case DbTableType.VIEW:
case DbTableType.TABLE:
loc6.Add(object_id);
loc6_1000.Add(object_id);
if (loc6_1000.Count >= 500)
{
loc6.Add(loc6_1000.ToArray());
loc6_1000.Clear();
}
break;
case DbTableType.StoreProcedure:
loc66.Add(object_id);
loc66_1000.Add(object_id);
if (loc66_1000.Count >= 500)
{
loc66.Add(loc66_1000.ToArray());
loc66_1000.Clear();
}
break;
}
}
if (loc6_1000.Count > 0) loc6.Add(loc6_1000.ToArray());
if (loc66_1000.Count > 0) loc66.Add(loc66_1000.ToArray());
if (loc6.Count == 0) return loc1;
string loc8 = "'" + string.Join("','", loc6.ToArray()) + "'";
string loc88 = "'" + string.Join("','", loc66.ToArray()) + "'";
var loc8 = new StringBuilder().Append("(");
for (var loc8idx = 0; loc8idx < loc6.Count; loc8idx++)
{
if (loc8idx > 0) loc8.Append(" OR ");
loc8.Append("a.table_name in (");
for (var loc8idx2 = 0; loc8idx2 < loc6[loc8idx].Length; loc8idx2++)
{
if (loc8idx2 > 0) loc8.Append(",");
loc8.Append($"'{loc6[loc8idx][loc8idx2]}'");
}
loc8.Append(")");
}
loc8.Append(")");
sql = $@"
select
@ -196,7 +223,7 @@ left join pg_description d on d.objoid = a.attrelid and d.objsubid = a.attnum
left join pg_attrdef e on e.adrelid = a.attrelid and e.adnum = a.attnum
inner join pg_namespace ns on ns.oid = c.relnamespace
inner join pg_namespace ns2 on ns2.oid = t.typnamespace
where ns.nspname || '.' || c.relname in ({loc8})";
where {loc8.ToString().Replace("a.table_name", "ns.nspname || '.' || c.relname")}";
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return loc1;
@ -259,7 +286,7 @@ inner join pg_class b on b.oid = a.indexrelid
inner join pg_attribute c on c.attnum > 0 and c.attrelid = b.oid
inner join pg_namespace ns on ns.oid = b.relnamespace
inner join pg_class d on d.oid = a.indrelid
where ns.nspname || '.' || d.relname in ({loc8}) and a.indisprimary = 'f'
where {loc8.ToString().Replace("a.table_name", "ns.nspname || '.' || d.relname")} and a.indisprimary = 'f'
";
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return loc1;
@ -335,7 +362,7 @@ inner join pg_class b on b.oid = a.conrelid
inner join pg_class c on c.oid = a.confrelid
inner join pg_namespace ns on ns.oid = b.relnamespace
inner join pg_namespace ns2 on ns2.oid = c.relnamespace
where ns.nspname || '.' || b.relname in ({loc8})
where {loc8.ToString().Replace("a.table_name", "ns.nspname || '.' || b.relname")}
";
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return loc1;

View File

@ -5,6 +5,7 @@ using System.Collections.Generic;
using System.Data;
using System.Data.Odbc;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace FreeSql.Odbc.SqlServer
@ -163,8 +164,10 @@ use [{olddatabase}];
var ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return loc1;
var loc6 = new List<int>();
var loc66 = new List<int>();
var loc6 = new List<int[]>();
var loc66 = new List<int[]>();
var loc6_1000 = new List<int>();
var loc66_1000 = new List<int>();
foreach (object[] row in ds)
{
int object_id = int.Parse(string.Concat(row[0]));
@ -178,16 +181,47 @@ use [{olddatabase}];
{
case DbTableType.VIEW:
case DbTableType.TABLE:
loc6.Add(object_id);
loc6_1000.Add(object_id);
if (loc6_1000.Count >= 500)
{
loc6.Add(loc6_1000.ToArray());
loc6_1000.Clear();
}
break;
case DbTableType.StoreProcedure:
loc66.Add(object_id);
loc66_1000.Add(object_id);
if (loc66_1000.Count >= 500)
{
loc66.Add(loc66_1000.ToArray());
loc66_1000.Clear();
}
break;
}
}
if (loc6_1000.Count > 0) loc6.Add(loc6_1000.ToArray());
if (loc66_1000.Count > 0) loc66.Add(loc66_1000.ToArray());
if (loc6.Count == 0) return loc1;
var loc8 = string.Join(",", loc6.Select(a => string.Concat(a)));
var loc88 = string.Join(",", loc66.Select(a => string.Concat(a)));
Func<List<int[]>, StringBuilder> getloc8Sb = loclist =>
{
if (loclist.Count == 0) return new StringBuilder();
var loc8sb = new StringBuilder().Append("(");
for (var loc8sbidx = 0; loc8sbidx < loclist.Count; loc8sbidx++)
{
if (loc8sbidx > 0) loc8sb.Append(" OR ");
loc8sb.Append("a.table_name in (");
for (var loc8sbidx2 = 0; loc8sbidx2 < loclist[loc8sbidx].Length; loc8sbidx2++)
{
if (loc8sbidx2 > 0) loc8sb.Append(",");
loc8sb.Append(loclist[loc8sbidx][loc8sbidx2]);
}
loc8sb.Append(")");
}
loc8sb.Append(")");
return loc8sb;
};
var loc8 = getloc8Sb(loc6);
var loc88 = getloc8Sb(loc66);
var tsql_place = @"
@ -213,12 +247,12 @@ inner join sys.types b on b.user_type_id = a.user_type_id
left join sys.extended_properties AS c ON c.major_id = a.object_id AND c.minor_id = a.column_id
left join sys.tables d on d.object_id = a.object_id
left join sys.schemas e on e.schema_id = d.schema_id
where a.object_id in ({1})
where {1}
";
sql = string.Format(tsql_place, @"
,a.is_nullable 'IsNullable'
,a.is_identity 'IsIdentity'
from sys.columns", loc8);
from sys.columns", loc8.ToString().Replace("a.table_name", "a.object_id"));
if (loc88.Length > 0)
{
sql += "union all" +
@ -227,7 +261,7 @@ from sys.columns", loc8);
"left join sys.extended_properties AS c ON c.major_id = a.object_id AND c.minor_id = a.parameter_id"), @"
,cast(0 as bit) 'IsNullable'
,a.is_output 'IsIdentity'
from sys.parameters", loc88);
from sys.parameters", loc88.ToString().Replace("a.table_name", "a.object_id"));
}
sql = $"use [{db}];{sql};use [{olddatabase}]; ";
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
@ -275,7 +309,7 @@ select
from sys.index_columns a
inner join sys.indexes b on b.object_id = a.object_id and b.index_id = a.index_id
left join sys.columns c on c.object_id = a.object_id and c.column_id = a.column_id
where a.object_id in ({loc8}) and b.is_primary_key = 0
where {loc8.ToString().Replace("a.table_name", "a.object_id")} and b.is_primary_key = 0
;
use [{olddatabase}];
";
@ -344,7 +378,7 @@ inner join sys.tables b on b.object_id = a.parent_object_id
inner join sys.columns c on c.object_id = a.parent_object_id and c.column_id = a.parent_column_id
inner join sys.columns d on d.object_id = a.referenced_object_id and d.column_id = a.referenced_column_id
left join sys.foreign_keys e on e.object_id = a.constraint_object_id
where b.object_id in ({loc8})
where {loc8.ToString().Replace("a.table_name", "b.object_id")}
;
use [{olddatabase}];
";

View File

@ -6,6 +6,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace FreeSql.Oracle
@ -191,8 +192,10 @@ where a.owner in ({0})", databaseIn);
var ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return loc1;
var loc6 = new List<string>();
var loc66 = new List<string>();
var loc6 = new List<string[]>();
var loc66 = new List<string[]>();
var loc6_1000 = new List<string>();
var loc66_1000 = new List<string>();
foreach (var row in ds)
{
var table_id = string.Concat(row[0]);
@ -211,16 +214,40 @@ where a.owner in ({0})", databaseIn);
{
case DbTableType.TABLE:
case DbTableType.VIEW:
loc6.Add(table.Replace("'", "''"));
loc6_1000.Add(table.Replace("'", "''"));
if (loc6_1000.Count >= 999)
{
loc6.Add(loc6_1000.ToArray());
loc6_1000.Clear();
}
break;
case DbTableType.StoreProcedure:
loc66.Add(table.Replace("'", "''"));
loc66_1000.Add(table.Replace("'", "''"));
if (loc66_1000.Count >= 999)
{
loc66.Add(loc66_1000.ToArray());
loc66_1000.Clear();
}
break;
}
}
if (loc6_1000.Count > 0) loc6.Add(loc6_1000.ToArray());
if (loc66_1000.Count > 0) loc66.Add(loc66_1000.ToArray());
if (loc6.Count == 0) return loc1;
var loc8 = "'" + string.Join("','", loc6.ToArray()) + "'";
var loc88 = "'" + string.Join("','", loc66.ToArray()) + "'";
var loc8 = new StringBuilder().Append("(");
for (var loc8idx = 0; loc8idx < loc6.Count; loc8idx++)
{
if (loc8idx > 0) loc8.Append(" OR ");
loc8.Append("a.table_name in (");
for (var loc8idx2 = 0; loc8idx2 < loc6[loc8idx].Length; loc8idx2++)
{
if (loc8idx2 > 0) loc8.Append(",");
loc8.Append($"'{loc6[loc8idx][loc8idx2]}'");
}
loc8.Append(")");
}
loc8.Append(")");
sql = string.Format(@"
select
@ -236,7 +263,7 @@ nvl((select 1 from user_sequences where upper(sequence_name)=upper(a.table_name|
b.comments
from all_tab_cols a
left join all_col_comments b on b.owner = a.owner and b.table_name = a.table_name and b.column_name = a.column_name
where a.owner in ({1}) and a.table_name in ({0})
where a.owner in ({1}) and {0}
", loc8, databaseIn);
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return loc1;
@ -303,7 +330,7 @@ all_ind_columns c
where a.index_name = c.index_name
and a.table_owner = c.table_owner
and a.table_name = c.table_name
and a.table_owner in ({1}) and a.table_name in ({0})
and a.table_owner in ({1}) and {0}
and not exists(select 1 from all_constraints where constraint_name = a.index_name and constraint_type = 'P')
", loc8, databaseIn);
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
@ -390,7 +417,7 @@ and a.owner = c.owner   
and a.table_name = c.table_name   
and b.owner = d.owner   
and b.table_name = d.table_name
and a.owner in ({1}) and a.table_name in ({0})
and a.owner in ({1}) and {0}
", loc8, databaseIn);
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return loc1;

View File

@ -12,6 +12,7 @@ using System.Data.Common;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Text.RegularExpressions;
namespace FreeSql.PostgreSQL
@ -258,8 +259,10 @@ and b.nspname || '.' || a.relname not in ('public.geography_columns','public.geo
var ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return loc1;
var loc6 = new List<string>();
var loc66 = new List<string>();
var loc6 = new List<string[]>();
var loc66 = new List<string[]>();
var loc6_1000 = new List<string>();
var loc66_1000 = new List<string>();
foreach (object[] row in ds)
{
var object_id = string.Concat(row[0]);
@ -273,16 +276,40 @@ and b.nspname || '.' || a.relname not in ('public.geography_columns','public.geo
{
case DbTableType.VIEW:
case DbTableType.TABLE:
loc6.Add(object_id);
loc6_1000.Add(object_id);
if (loc6_1000.Count >= 500)
{
loc6.Add(loc6_1000.ToArray());
loc6_1000.Clear();
}
break;
case DbTableType.StoreProcedure:
loc66.Add(object_id);
loc66_1000.Add(object_id);
if (loc66_1000.Count >= 500)
{
loc66.Add(loc66_1000.ToArray());
loc66_1000.Clear();
}
break;
}
}
if (loc6_1000.Count > 0) loc6.Add(loc6_1000.ToArray());
if (loc66_1000.Count > 0) loc66.Add(loc66_1000.ToArray());
if (loc6.Count == 0) return loc1;
string loc8 = "'" + string.Join("','", loc6.ToArray()) + "'";
string loc88 = "'" + string.Join("','", loc66.ToArray()) + "'";
var loc8 = new StringBuilder().Append("(");
for (var loc8idx = 0; loc8idx < loc6.Count; loc8idx++)
{
if (loc8idx > 0) loc8.Append(" OR ");
loc8.Append("a.table_name in (");
for (var loc8idx2 = 0; loc8idx2 < loc6[loc8idx].Length; loc8idx2++)
{
if (loc8idx2 > 0) loc8.Append(",");
loc8.Append($"'{loc6[loc8idx][loc8idx2]}'");
}
loc8.Append(")");
}
loc8.Append(")");
sql = $@"
select
@ -307,7 +334,7 @@ left join pg_description d on d.objoid = a.attrelid and d.objsubid = a.attnum
left join pg_attrdef e on e.adrelid = a.attrelid and e.adnum = a.attnum
inner join pg_namespace ns on ns.oid = c.relnamespace
inner join pg_namespace ns2 on ns2.oid = t.typnamespace
where ns.nspname || '.' || c.relname in ({loc8})";
where {loc8.ToString().Replace("a.table_name", "ns.nspname || '.' || c.relname")}";
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return loc1;
@ -370,7 +397,7 @@ inner join pg_class b on b.oid = a.indexrelid
inner join pg_attribute c on c.attnum > 0 and c.attrelid = b.oid
inner join pg_namespace ns on ns.oid = b.relnamespace
inner join pg_class d on d.oid = a.indrelid
where ns.nspname || '.' || d.relname in ({loc8}) and a.indisprimary = 'f'
where {loc8.ToString().Replace("a.table_name", "ns.nspname || '.' || d.relname")} and a.indisprimary = 'f'
";
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return loc1;
@ -446,7 +473,7 @@ inner join pg_class b on b.oid = a.conrelid
inner join pg_class c on c.oid = a.confrelid
inner join pg_namespace ns on ns.oid = b.relnamespace
inner join pg_namespace ns2 on ns2.oid = c.relnamespace
where ns.nspname || '.' || b.relname in ({loc8})
where {loc8.ToString().Replace("a.table_name", "ns.nspname || '.' || b.relname")}
";
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return loc1;

View File

@ -4,6 +4,7 @@ using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace FreeSql.SqlServer
@ -166,8 +167,10 @@ use [{olddatabase}];
var ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
if (ds == null) return loc1;
var loc6 = new List<int>();
var loc66 = new List<int>();
var loc6 = new List<int[]>();
var loc66 = new List<int[]>();
var loc6_1000 = new List<int>();
var loc66_1000 = new List<int>();
foreach (object[] row in ds)
{
int object_id = int.Parse(string.Concat(row[0]));
@ -181,16 +184,47 @@ use [{olddatabase}];
{
case DbTableType.VIEW:
case DbTableType.TABLE:
loc6.Add(object_id);
loc6_1000.Add(object_id);
if (loc6_1000.Count >= 500)
{
loc6.Add(loc6_1000.ToArray());
loc6_1000.Clear();
}
break;
case DbTableType.StoreProcedure:
loc66.Add(object_id);
loc66_1000.Add(object_id);
if (loc66_1000.Count >= 500)
{
loc66.Add(loc66_1000.ToArray());
loc66_1000.Clear();
}
break;
}
}
if (loc6_1000.Count > 0) loc6.Add(loc6_1000.ToArray());
if (loc66_1000.Count > 0) loc66.Add(loc66_1000.ToArray());
if (loc6.Count == 0) return loc1;
var loc8 = string.Join(",", loc6.Select(a => string.Concat(a)));
var loc88 = string.Join(",", loc66.Select(a => string.Concat(a)));
Func<List<int[]>, StringBuilder> getloc8Sb = loclist =>
{
if (loclist.Count == 0) return new StringBuilder();
var loc8sb = new StringBuilder().Append("(");
for (var loc8sbidx = 0; loc8sbidx < loclist.Count; loc8sbidx++)
{
if (loc8sbidx > 0) loc8sb.Append(" OR ");
loc8sb.Append("a.table_name in (");
for (var loc8sbidx2 = 0; loc8sbidx2 < loclist[loc8sbidx].Length; loc8sbidx2++)
{
if (loc8sbidx2 > 0) loc8sb.Append(",");
loc8sb.Append(loclist[loc8sbidx][loc8sbidx2]);
}
loc8sb.Append(")");
}
loc8sb.Append(")");
return loc8sb;
};
var loc8 = getloc8Sb(loc6);
var loc88 = getloc8Sb(loc66);
var tsql_place = @"
@ -216,12 +250,12 @@ inner join sys.types b on b.user_type_id = a.user_type_id
left join sys.extended_properties AS c ON c.major_id = a.object_id AND c.minor_id = a.column_id
left join sys.tables d on d.object_id = a.object_id
left join sys.schemas e on e.schema_id = d.schema_id
where a.object_id in ({1})
where {1}
";
sql = string.Format(tsql_place, @"
,a.is_nullable 'IsNullable'
,a.is_identity 'IsIdentity'
from sys.columns", loc8);
from sys.columns", loc8.ToString().Replace("a.table_name", "a.object_id"));
if (loc88.Length > 0)
{
sql += "union all" +
@ -230,7 +264,7 @@ from sys.columns", loc8);
"left join sys.extended_properties AS c ON c.major_id = a.object_id AND c.minor_id = a.parameter_id"), @"
,cast(0 as bit) 'IsNullable'
,a.is_output 'IsIdentity'
from sys.parameters", loc88);
from sys.parameters", loc88.ToString().Replace("a.table_name", "a.object_id"));
}
sql = $"use [{db}];{sql};use [{olddatabase}]; ";
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
@ -278,7 +312,7 @@ select
from sys.index_columns a
inner join sys.indexes b on b.object_id = a.object_id and b.index_id = a.index_id
left join sys.columns c on c.object_id = a.object_id and c.column_id = a.column_id
where a.object_id in ({loc8}) and b.is_primary_key = 0
where {loc8.ToString().Replace("a.table_name", "a.object_id")} and b.is_primary_key = 0
;
use [{olddatabase}];
";
@ -347,7 +381,7 @@ inner join sys.tables b on b.object_id = a.parent_object_id
inner join sys.columns c on c.object_id = a.parent_object_id and c.column_id = a.parent_column_id
inner join sys.columns d on d.object_id = a.referenced_object_id and d.column_id = a.referenced_column_id
left join sys.foreign_keys e on e.object_id = a.constraint_object_id
where b.object_id in ({loc8})
where {loc8.ToString().Replace("a.table_name", "b.object_id")}
;
use [{olddatabase}];
";