Skip to content

Commit 8627f62

Browse files
committed
- 增加 ZeroDbContext.LoadTableDescriptor 方法从数据库中加载描述对象;
1 parent e63f23a commit 8627f62

File tree

9 files changed

+116
-12
lines changed

9 files changed

+116
-12
lines changed

Extensions/FreeSql.Extensions.ZeroEntity/ZeroDbContext.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ public void SyncTableStructure(string name)
132132
_orm.CodeFirst.SyncStructure(table, table.DbName, false);
133133
}
134134

135-
136135
static List<ZeroTableInfo> ValidateSchemaToInfoInternal(IFreeSql orm, IEnumerable<TableDescriptor> schemas)
137136
{
138137
var common = (orm.Ado as AdoProvider)._util;
@@ -264,6 +263,48 @@ static List<ZeroTableInfo> ValidateSchemaToInfoInternal(IFreeSql orm, IEnumerabl
264263
return tables;
265264
}
266265

266+
/// <summary>
267+
/// 从数据库中加载 TableDescriptor 描述<para></para>
268+
/// - 不支持 Navigates<para></para>
269+
/// - 不支持 Indexes IndexMethod<para></para>
270+
/// - 暂支持 SqlServer/MySql decimal(10,2)(其他数据库需实现对应 IDbFirst)
271+
/// </summary>
272+
static TableDescriptor LoadTableDescriptor(IFreeSql orm, string tableName)
273+
{
274+
var dbinfo = orm.DbFirst.GetTableByName(tableName, true);
275+
if (dbinfo == null) throw new Exception($"表“{tableName}”不存在");
276+
var tb = new TableDescriptor
277+
{
278+
Comment = dbinfo.Comment,
279+
DbName = dbinfo.Name,
280+
Name = dbinfo.Name,
281+
DisableSyncStructure = false,
282+
};
283+
tb.Columns.AddRange(dbinfo.Columns.Select(a => new TableDescriptor.ColumnDescriptor
284+
{
285+
Name = a.Name,
286+
DbType = a.DbTypeText,
287+
IsPrimary = a.IsPrimary,
288+
IsIdentity = a.IsIdentity,
289+
IsNullable = a.IsNullable,
290+
IsVersion = false,
291+
MapType = a.CsType,
292+
ServerTime = DateTimeKind.Unspecified,
293+
InsertValueSql = a.DefaultValue,
294+
StringLength = a.MaxLength,
295+
Precision = a.Precision,
296+
Scale = a.Scale,
297+
Comment = a.Comment,
298+
}));
299+
tb.Indexes.AddRange(dbinfo.Indexes.Select(a => new TableDescriptor.IndexDescriptor
300+
{
301+
Name = a.Name,
302+
IsUnique = a.IsUnique,
303+
Fields = string.Join(",", a.Columns.Select(b => b.Column)),
304+
}));
305+
return tb;
306+
}
307+
267308
public ZeroDbContext WithTransaction(DbTransaction value)
268309
{
269310
_transaction = value;

FreeSql-ZeroEntity.sln

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Provider.Sqlite", "
1111
EndProject
1212
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "zero_entity", "Examples\zero_entity\zero_entity.csproj", "{97E60B48-06AC-42F3-90A8-DFC5FD386582}"
1313
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FreeSql.Provider.Oracle", "Providers\FreeSql.Provider.Oracle\FreeSql.Provider.Oracle.csproj", "{3EDA83F1-0F47-D363-9FD7-6A68B81E93C8}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
@@ -33,8 +35,15 @@ Global
3335
{97E60B48-06AC-42F3-90A8-DFC5FD386582}.Debug|Any CPU.Build.0 = Debug|Any CPU
3436
{97E60B48-06AC-42F3-90A8-DFC5FD386582}.Release|Any CPU.ActiveCfg = Release|Any CPU
3537
{97E60B48-06AC-42F3-90A8-DFC5FD386582}.Release|Any CPU.Build.0 = Release|Any CPU
38+
{3EDA83F1-0F47-D363-9FD7-6A68B81E93C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39+
{3EDA83F1-0F47-D363-9FD7-6A68B81E93C8}.Debug|Any CPU.Build.0 = Debug|Any CPU
40+
{3EDA83F1-0F47-D363-9FD7-6A68B81E93C8}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{3EDA83F1-0F47-D363-9FD7-6A68B81E93C8}.Release|Any CPU.Build.0 = Release|Any CPU
3642
EndGlobalSection
3743
GlobalSection(SolutionProperties) = preSolution
3844
HideSolutionNode = FALSE
3945
EndGlobalSection
46+
GlobalSection(ExtensibilityGlobals) = postSolution
47+
SolutionGuid = {B7E48B6A-A1A8-477E-AC28-EFD2E83369EF}
48+
EndGlobalSection
4049
EndGlobal

FreeSql/DatabaseModel/DbColumnInfo.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ public class DbColumnInfo
3333
/// </summary>
3434
public int MaxLength { get; set; }
3535
/// <summary>
36+
/// 暂支持 SqlServer/MySql(其他数据库待补充)
37+
/// </summary>
38+
public int Precision { get; set; }
39+
/// <summary>
40+
/// 暂支持 SqlServer/MySql(其他数据库待补充)
41+
/// </summary>
42+
public int Scale { get; set; }
43+
/// <summary>
3644
/// 主键
3745
/// </summary>
3846
public bool IsPrimary { get; set; }

Providers/FreeSql.Provider.Custom/MySql/CustomMySqlDbFirst.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,9 @@ from information_schema.tables a
246246
case when locate('auto_increment', a.extra) > 0 then 1 else 0 end 'is_identity',
247247
a.column_comment 'comment',
248248
a.column_default 'default_value',
249-
a.ordinal_position
249+
a.ordinal_position,
250+
a.numeric_precision,
251+
a.numeric_scale
250252
from information_schema.columns a
251253
where {(ignoreCase ? "lower(a.table_schema)" : "a.table_schema")} in ({databaseIn}) and {loc8}
252254
";
@@ -267,6 +269,8 @@ from information_schema.columns a
267269
string comment = string.Concat(row[7]);
268270
string defaultValue = string.Concat(row[8]);
269271
var position = int.Parse(string.Concat(row[9]));
272+
int.TryParse(string.Concat(row[10]), out var numeric_precision);
273+
int.TryParse(string.Concat(row[11]), out var numeric_scale);
270274

271275
if (max_length == 0) max_length = -1;
272276
if (database.Length == 1)
@@ -285,7 +289,9 @@ from information_schema.columns a
285289
Table = loc2[table_id],
286290
Comment = comment,
287291
DefaultValue = defaultValue,
288-
Position = position
292+
Position = position,
293+
Precision = numeric_precision,
294+
Scale = numeric_scale,
289295
});
290296
loc3[table_id][column].DbType = this.GetDbType(loc3[table_id][column]);
291297
loc3[table_id][column].CsType = this.GetCsTypeInfo(loc3[table_id][column]);

Providers/FreeSql.Provider.Custom/SqlServer/CustomSqlServerDbFirst.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ when b.name in ('numeric', 'decimal') then '(' + cast(a.precision as varchar) +
283283
,a.is_nullable 'isnullable'
284284
,a.is_identity 'isidentity'
285285
,f.text as 'defaultvalue'
286+
,a.column_id as 'position'
287+
,a.precision
288+
,a.scale
286289
from sys.columns", loc8.ToString().Replace("a.table_name", "a.object_id"), @"
287290
left join syscomments f on f.id = a.default_object_id
288291
");
@@ -295,13 +298,15 @@ when b.name in ('numeric', 'decimal') then '(' + cast(a.precision as varchar) +
295298
,cast(0 as bit) 'isnullable'
296299
,a.is_output 'isidentity'
297300
,'' as 'defaultvalue'
301+
,1 as 'position'
302+
,a.precision
303+
,a.scale
298304
from sys.parameters", loc88.ToString().Replace("a.table_name", "a.object_id"), "");
299305
}
300306
sql = $"use [{db}];{sql};use [{olddatabase}]; ";
301307
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
302308
if (ds == null) return loc1;
303309

304-
var position = 0;
305310
foreach (object[] row in ds)
306311
{
307312
var table_id = string.Concat(row[0]);
@@ -314,7 +319,10 @@ when b.name in ('numeric', 'decimal') then '(' + cast(a.precision as varchar) +
314319
var is_nullable = bool.Parse(string.Concat(row[7]));
315320
var is_identity = bool.Parse(string.Concat(row[8]));
316321
var defaultValue = string.Concat(row[9]);
322+
var position = int.Parse(string.Concat(row[10]));
317323
if (max_length == 0) max_length = -1;
324+
int.TryParse(string.Concat(row[11]), out var numeric_precision);
325+
int.TryParse(string.Concat(row[12]), out var numeric_scale);
318326

319327
loc3[object_id].Add(column, new DbColumnInfo
320328
{
@@ -328,7 +336,9 @@ when b.name in ('numeric', 'decimal') then '(' + cast(a.precision as varchar) +
328336
Table = loc2[object_id],
329337
Comment = comment,
330338
DefaultValue = defaultValue,
331-
Position = ++position
339+
Position = position,
340+
Precision = numeric_precision,
341+
Scale = numeric_scale,
332342
});
333343
loc3[object_id][column].DbType = this.GetDbType(loc3[object_id][column]);
334344
loc3[object_id][column].CsType = this.GetCsTypeInfo(loc3[object_id][column]);

Providers/FreeSql.Provider.MySql/MySqlDbFirst.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,9 @@ from information_schema.tables a
279279
case when locate('auto_increment', a.extra) > 0 then 1 else 0 end 'is_identity',
280280
a.column_comment 'comment',
281281
a.column_default 'default_value',
282-
a.ordinal_position
282+
a.ordinal_position,
283+
a.numeric_precision,
284+
a.numeric_scale
283285
from information_schema.columns a
284286
where {(ignoreCase ? "lower(a.table_schema)" : "a.table_schema")} in ({databaseIn}) and {loc8}
285287
";
@@ -300,6 +302,8 @@ from information_schema.columns a
300302
string comment = string.Concat(row[7]);
301303
string defaultValue = string.Concat(row[8]);
302304
var position = int.Parse(string.Concat(row[9]));
305+
int.TryParse(string.Concat(row[10]), out var numeric_precision);
306+
int.TryParse(string.Concat(row[11]), out var numeric_scale);
303307

304308
if (max_length == 0) max_length = -1;
305309
if (database.Length == 1)
@@ -318,7 +322,9 @@ from information_schema.columns a
318322
Table = loc2[table_id],
319323
Comment = comment,
320324
DefaultValue = defaultValue,
321-
Position = position
325+
Position = position,
326+
Precision = numeric_precision,
327+
Scale = numeric_scale,
322328
});
323329
loc3[table_id][column].DbType = this.GetDbType(loc3[table_id][column]);
324330
loc3[table_id][column].CsType = this.GetCsTypeInfo(loc3[table_id][column]);

Providers/FreeSql.Provider.Odbc/MySql/OdbcMySqlDbFirst.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,9 @@ from information_schema.tables a
245245
case when locate('auto_increment', a.extra) > 0 then 1 else 0 end 'is_identity',
246246
a.column_comment 'comment',
247247
a.column_default 'default_value',
248-
a.ordinal_position
248+
a.ordinal_position,
249+
a.numeric_precision,
250+
a.numeric_scale
249251
from information_schema.columns a
250252
where {(ignoreCase ? "lower(a.table_schema)" : "a.table_schema")} in ({databaseIn}) and {loc8}
251253
";
@@ -266,6 +268,8 @@ from information_schema.columns a
266268
string comment = string.Concat(row[7]);
267269
string defaultValue = string.Concat(row[8]);
268270
var position = int.Parse(string.Concat(row[9]));
271+
int.TryParse(string.Concat(row[10]), out var numeric_precision);
272+
int.TryParse(string.Concat(row[11]), out var numeric_scale);
269273

270274
if (max_length == 0) max_length = -1;
271275
if (database.Length == 1)
@@ -284,7 +288,9 @@ from information_schema.columns a
284288
Table = loc2[table_id],
285289
Comment = comment,
286290
DefaultValue = defaultValue,
287-
Position = position
291+
Position = position,
292+
Position = position,
293+
Precision = numeric_precision,
288294
});
289295
loc3[table_id][column].DbType = this.GetDbType(loc3[table_id][column]);
290296
loc3[table_id][column].CsType = this.GetCsTypeInfo(loc3[table_id][column]);

Providers/FreeSql.Provider.Odbc/SqlServer/OdbcSqlServerDbFirst.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ when b.name in ('numeric', 'decimal') then '(' + cast(a.precision as varchar) +
286286
,a.is_nullable 'isnullable'
287287
,a.is_identity 'isidentity'
288288
,f.text as 'defaultvalue'
289+
,a.column_id as 'position'
290+
,a.precision
291+
,a.scale
289292
from sys.columns", loc8.ToString().Replace("a.table_name", "a.object_id"), @"
290293
left join syscomments f on f.id = a.default_object_id
291294
");
@@ -298,13 +301,15 @@ when b.name in ('numeric', 'decimal') then '(' + cast(a.precision as varchar) +
298301
,cast(0 as bit) 'isnullable'
299302
,a.is_output 'isidentity'
300303
,'' as 'defaultvalue'
304+
,1 as 'position'
305+
,a.precision
306+
,a.scale
301307
from sys.parameters", loc88.ToString().Replace("a.table_name", "a.object_id"), "");
302308
}
303309
sql = $"use [{db}];{sql};use [{olddatabase}]; ";
304310
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
305311
if (ds == null) return loc1;
306312

307-
var position = 0;
308313
foreach (object[] row in ds)
309314
{
310315
var table_id = string.Concat(row[0]);
@@ -317,7 +322,10 @@ when b.name in ('numeric', 'decimal') then '(' + cast(a.precision as varchar) +
317322
var is_nullable = bool.Parse(string.Concat(row[7]));
318323
var is_identity = bool.Parse(string.Concat(row[8]));
319324
var defaultValue = string.Concat(row[9]);
325+
var position = int.Parse(string.Concat(row[10]));
320326
if (max_length == 0) max_length = -1;
327+
int.TryParse(string.Concat(row[11]), out var numeric_precision);
328+
int.TryParse(string.Concat(row[12]), out var numeric_scale);
321329

322330
loc3[object_id].Add(column, new DbColumnInfo
323331
{
@@ -331,7 +339,9 @@ when b.name in ('numeric', 'decimal') then '(' + cast(a.precision as varchar) +
331339
Table = loc2[object_id],
332340
Comment = comment,
333341
DefaultValue = defaultValue,
334-
Position = ++position
342+
Position = position,
343+
Precision = numeric_precision,
344+
Scale = numeric_scale,
335345
});
336346
loc3[object_id][column].DbType = this.GetDbType(loc3[object_id][column]);
337347
loc3[object_id][column].CsType = this.GetCsTypeInfo(loc3[object_id][column]);

Providers/FreeSql.Provider.SqlServer/SqlServerDbFirst.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ when b.name in ('numeric', 'decimal') then '(' + cast(a.precision as varchar) +
290290
,a.is_identity 'isidentity'
291291
,f.text as 'defaultvalue'
292292
,a.column_id as 'position'
293+
,a.precision
294+
,a.scale
293295
from sys.columns", loc8.ToString().Replace("a.table_name", "a.object_id"), @"
294296
left join syscomments f on f.id = a.default_object_id
295297
");
@@ -303,6 +305,8 @@ when b.name in ('numeric', 'decimal') then '(' + cast(a.precision as varchar) +
303305
,a.is_output 'isidentity'
304306
,'' as 'defaultvalue'
305307
,1 as 'position'
308+
,a.precision
309+
,a.scale
306310
from sys.parameters", loc88.ToString().Replace("a.table_name", "a.object_id"), "");
307311
}
308312
sql = $"use [{db}];{sql};use [{olddatabase}]; ";
@@ -323,6 +327,8 @@ when b.name in ('numeric', 'decimal') then '(' + cast(a.precision as varchar) +
323327
var defaultValue = string.Concat(row[9]);
324328
var position = int.Parse(string.Concat(row[10]));
325329
if (max_length == 0) max_length = -1;
330+
int.TryParse(string.Concat(row[11]), out var numeric_precision);
331+
int.TryParse(string.Concat(row[12]), out var numeric_scale);
326332

327333
loc3[object_id].Add(column, new DbColumnInfo
328334
{
@@ -336,7 +342,9 @@ when b.name in ('numeric', 'decimal') then '(' + cast(a.precision as varchar) +
336342
Table = loc2[object_id],
337343
Comment = comment,
338344
DefaultValue = defaultValue,
339-
Position = position
345+
Position = position,
346+
Precision = numeric_precision,
347+
Scale = numeric_scale,
340348
});
341349
loc3[object_id][column].DbType = this.GetDbType(loc3[object_id][column]);
342350
loc3[object_id][column].CsType = this.GetCsTypeInfo(loc3[object_id][column]);

0 commit comments

Comments
 (0)