- 增加 DbFirst 获取字段的默认值信息;

This commit is contained in:
28810 2020-04-13 19:00:22 +08:00
parent 44638a1e97
commit 5e336a0173
13 changed files with 110 additions and 26 deletions

View File

@ -18,7 +18,7 @@ namespace FreeSql.Tests.Odbc.PostgreSQL
public void GetTablesByDatabase()
{
var t2 = g.pgsql.DbFirst.GetTablesByDatabase(g.pgsql.DbFirst.GetDatabases()[1]);
var t2 = g.pgsql.DbFirst.GetTablesByDatabase(g.pgsql.DbFirst.GetDatabases()[2]);
}
}

View File

@ -48,5 +48,9 @@ namespace FreeSql.DatabaseModel
/// 备注
/// </summary>
public string Coment { get; set; }
/// <summary>
/// 数据库默认值
/// </summary>
public string DefaultValue { get; set; }
}
}

View File

@ -440,6 +440,11 @@
备注
</summary>
</member>
<member name="P:FreeSql.DatabaseModel.DbColumnInfo.DefaultValue">
<summary>
数据库默认值
</summary>
</member>
<member name="P:FreeSql.DatabaseModel.DbEnumInfo.Name">
<summary>
枚举类型标识

View File

@ -238,7 +238,8 @@ ifnull(a.character_maximum_length, 0) 'len',
a.column_type,
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'
a.column_comment 'comment',
a.column_default 'default_value'
from information_schema.columns a
where a.table_schema in ({1}) and {0}
", loc8, databaseIn);
@ -257,6 +258,7 @@ where a.table_schema in ({1}) and {0}
bool is_nullable = string.Concat(row[5]) == "1";
bool is_identity = string.Concat(row[6]) == "1";
string comment = string.Concat(row[7]);
string defaultValue = string.Concat(row[8]);
if (max_length == 0) max_length = -1;
if (database.Length == 1)
{
@ -272,7 +274,8 @@ where a.table_schema in ({1}) and {0}
DbTypeText = type,
DbTypeTextFull = sqlType,
Table = loc2[table_id],
Coment = comment
Coment = comment,
DefaultValue = defaultValue
});
loc3[table_id][column].DbType = this.GetDbType(loc3[table_id][column]);
loc3[table_id][column].CsType = this.GetCsTypeInfo(loc3[table_id][column]);

View File

@ -307,7 +307,8 @@ a.data_scale,
a.char_used,
case when a.nullable = 'N' then 0 else 1 end,
nvl((select 1 from user_sequences where upper(sequence_name)=upper(a.table_name||'_seq_'||a.column_name) and rownum < 2), 0),
b.comments
b.comments,
a.data_default
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 {0}
@ -318,7 +319,7 @@ where a.owner in ({1}) and {0}
var ds2 = new List<object[]>();
foreach (var row in ds)
{
var ds2item = new object[8];
var ds2item = new object[9];
ds2item[0] = row[0];
ds2item[1] = row[1];
ds2item[2] = Regex.Replace(string.Concat(row[2]), @"\(\d+\)", "");
@ -326,6 +327,7 @@ where a.owner in ({1}) and {0}
ds2item[5] = string.Concat(row[7]) == "1";
ds2item[6] = string.Concat(row[8]) == "1";
ds2item[7] = string.Concat(row[9]);
ds2item[8] = string.Concat(row[10]);
ds2.Add(ds2item);
}
foreach (var row in ds2)
@ -340,6 +342,7 @@ where a.owner in ({1}) and {0}
bool is_nullable = string.Concat(row[5]) == "1";
bool is_identity = string.Concat(row[6]) == "1";
string comment = string.Concat(row[7]);
string defaultValue = string.Concat(row[8]);
if (max_length == 0) max_length = -1;
if (database.Length == 1)
{
@ -355,7 +358,8 @@ where a.owner in ({1}) and {0}
DbTypeText = type,
DbTypeTextFull = sqlType,
Table = loc2[table_id],
Coment = comment
Coment = comment,
DefaultValue = defaultValue
});
loc3[table_id][column].DbType = this.GetDbType(loc3[table_id][column]);
loc3[table_id][column].CsType = this.GetCsTypeInfo(loc3[table_id][column]);

View File

@ -208,7 +208,8 @@ ifnull(a.character_maximum_length, 0) 'len',
a.column_type,
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'
a.column_comment 'comment',
a.column_default 'default_value'
from information_schema.columns a
where a.table_schema in ({1}) and {0}
", loc8, databaseIn);
@ -227,6 +228,7 @@ where a.table_schema in ({1}) and {0}
bool is_nullable = string.Concat(row[5]) == "1";
bool is_identity = string.Concat(row[6]) == "1";
string comment = string.Concat(row[7]);
string defaultValue = string.Concat(row[8]);
if (max_length == 0) max_length = -1;
if (database.Length == 1)
{
@ -242,7 +244,8 @@ where a.table_schema in ({1}) and {0}
DbTypeText = type,
DbTypeTextFull = sqlType,
Table = loc2[table_id],
Coment = comment
Coment = comment,
DefaultValue = defaultValue
});
loc3[table_id][column].DbType = this.GetDbType(loc3[table_id][column]);
loc3[table_id][column].CsType = this.GetCsTypeInfo(loc3[table_id][column]);

View File

@ -250,6 +250,25 @@ where a.owner in ({0})", databaseIn);
}
loc8.Append(")");
_orm.Ado.ExecuteNonQuery(CommandType.Text, @"
CREATE OR REPLACE FUNCTION FREESQL_LONG_TO_CHAR_DEFAULT
(
TABLE_NAME VARCHAR,
COLUMN VARCHAR2
)
RETURN VARCHAR AS
TEXT_C1 VARCHAR2(32767);
SQL_CUR VARCHAR2(2000);
BEGIN
DBMS_OUTPUT.ENABLE(BUFFER_SIZE => NULL);
SQL_CUR := 'SELECT T.DATA_DEFAULT FROM USER_TAB_COLUMNS T WHERE T.TABLE_NAME = '''||TABLE_NAME||''' AND T.COLUMN_NAME='''||COLUMN||'''';
DBMS_OUTPUT.PUT_LINE(SQL_CUR);
EXECUTE IMMEDIATE SQL_CUR
INTO TEXT_C1;
TEXT_C1 := SUBSTR(TEXT_C1, 1, 4000);
RETURN TEXT_C1;
END;");
sql = string.Format(@"
select
a.owner || '.' || a.table_name,
@ -261,7 +280,8 @@ a.data_scale,
a.char_used,
case when a.nullable = 'N' then 0 else 1 end,
nvl((select 1 from user_sequences where upper(sequence_name)=upper(a.table_name||'_seq_'||a.column_name) and rownum < 2), 0),
b.comments
to_char(b.comments),
nvl(FREESQL_LONG_TO_CHAR_DEFAULT(a.table_name, a.column_name),'')
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 {0}
@ -272,7 +292,7 @@ where a.owner in ({1}) and {0}
var ds2 = new List<object[]>();
foreach (var row in ds)
{
var ds2item = new object[8];
var ds2item = new object[9];
ds2item[0] = row[0];
ds2item[1] = row[1];
ds2item[2] = Regex.Replace(string.Concat(row[2]), @"\(\d+\)", "");
@ -280,6 +300,7 @@ where a.owner in ({1}) and {0}
ds2item[5] = string.Concat(row[7]);
ds2item[6] = string.Concat(row[8]);
ds2item[7] = string.Concat(row[9]);
ds2item[8] = string.Concat(row[10]);
ds2.Add(ds2item);
}
foreach (var row in ds2)
@ -294,6 +315,7 @@ where a.owner in ({1}) and {0}
bool is_nullable = string.Concat(row[5]) == "1";
bool is_identity = string.Concat(row[6]) == "1";
string comment = string.Concat(row[7]);
string defaultValue = string.Concat(row[8]);
if (max_length == 0) max_length = -1;
if (database.Length == 1)
{
@ -309,7 +331,8 @@ where a.owner in ({1}) and {0}
DbTypeText = type,
DbTypeTextFull = sqlType,
Table = loc2[table_id],
Coment = comment
Coment = comment,
DefaultValue = defaultValue
});
loc3[table_id][column].DbType = this.GetDbType(loc3[table_id][column]);
loc3[table_id][column].CsType = this.GetCsTypeInfo(loc3[table_id][column]);

View File

@ -238,6 +238,7 @@ where {loc8.ToString().Replace("a.table_name", "ns.nspname || '.' || c.relname")
var is_nullable = string.Concat(row[5]) == "1";
var is_identity = string.Concat(row[6]).StartsWith(@"nextval('") && string.Concat(row[6]).EndsWith(@"'::regclass)");
var comment = string.Concat(row[7]);
var defaultValue = string.Concat(row[6]);
int attndims = int.Parse(string.Concat(row[8]));
string typtype = string.Concat(row[9]);
string owner = string.Concat(row[10]);
@ -273,7 +274,8 @@ where {loc8.ToString().Replace("a.table_name", "ns.nspname || '.' || c.relname")
DbTypeText = type,
DbTypeTextFull = sqlType,
Table = loc2[object_id],
Coment = comment
Coment = comment,
DefaultValue = defaultValue
});
loc3[object_id][column].DbType = this.GetDbType(loc3[object_id][column]);
loc3[object_id][column].CsType = this.GetCsTypeInfo(loc3[object_id][column]);

View File

@ -243,13 +243,16 @@ isnull(e.name,'') + '.' + isnull(d.name,'')
{0} a
inner join sys.types b on b.user_type_id = a.user_type_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
left join sys.schemas e on e.schema_id = d.schema_id{2}
where {1}
";
sql = string.Format(tsql_place, @"
,a.is_nullable 'IsNullable'
,a.is_identity 'IsIdentity'
from sys.columns", loc8.ToString().Replace("a.table_name", "a.object_id"));
,f.text as 'DefaultValue'
from sys.columns", loc8.ToString().Replace("a.table_name", "a.object_id"), @"
left join syscomments f on f.id = a.default_object_id
");
if (loc88.Length > 0)
{
sql += "union all" +
@ -258,7 +261,8 @@ from sys.columns", loc8.ToString().Replace("a.table_name", "a.object_id"));
" select value from sys.extended_properties where major_id = a.object_id AND minor_id = a.parameter_id"), @"
,cast(0 as bit) 'IsNullable'
,a.is_output 'IsIdentity'
from sys.parameters", loc88.ToString().Replace("a.table_name", "a.object_id"));
,'' as 'DefaultValue'
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,6 +279,7 @@ from sys.parameters", loc88.ToString().Replace("a.table_name", "a.object_id"));
var comment = string.Concat(row[6]);
var is_nullable = bool.Parse(string.Concat(row[7]));
var is_identity = bool.Parse(string.Concat(row[8]));
var defaultValue = string.Concat(row[9]);
if (max_length == 0) max_length = -1;
loc3[object_id].Add(column, new DbColumnInfo
@ -287,7 +292,8 @@ from sys.parameters", loc88.ToString().Replace("a.table_name", "a.object_id"));
DbTypeText = type,
DbTypeTextFull = sqlType,
Table = loc2[object_id],
Coment = comment
Coment = comment,
DefaultValue = defaultValue
});
loc3[object_id][column].DbType = this.GetDbType(loc3[object_id][column]);
loc3[object_id][column].CsType = this.GetCsTypeInfo(loc3[object_id][column]);

View File

@ -250,6 +250,25 @@ where a.owner in ({0})", databaseIn);
}
loc8.Append(")");
_orm.Ado.ExecuteNonQuery(CommandType.Text, @"
CREATE OR REPLACE FUNCTION FREESQL_LONG_TO_CHAR_DEFAULT
(
TABLE_NAME VARCHAR,
COLUMN VARCHAR2
)
RETURN VARCHAR AS
TEXT_C1 VARCHAR2(32767);
SQL_CUR VARCHAR2(2000);
BEGIN
DBMS_OUTPUT.ENABLE(BUFFER_SIZE => NULL);
SQL_CUR := 'SELECT T.DATA_DEFAULT FROM USER_TAB_COLUMNS T WHERE T.TABLE_NAME = '''||TABLE_NAME||''' AND T.COLUMN_NAME='''||COLUMN||'''';
DBMS_OUTPUT.PUT_LINE(SQL_CUR);
EXECUTE IMMEDIATE SQL_CUR
INTO TEXT_C1;
TEXT_C1 := SUBSTR(TEXT_C1, 1, 4000);
RETURN TEXT_C1;
END;");
sql = string.Format(@"
select
a.owner || '.' || a.table_name,
@ -261,7 +280,8 @@ a.data_scale,
a.char_used,
case when a.nullable = 'N' then 0 else 1 end,
nvl((select 1 from user_sequences where upper(sequence_name)=upper(a.table_name||'_seq_'||a.column_name) and rownum < 2), 0),
b.comments
to_char(b.comments),
nvl(FREESQL_LONG_TO_CHAR_DEFAULT(a.table_name, a.column_name),'')
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 {0}
@ -272,7 +292,7 @@ where a.owner in ({1}) and {0}
var ds2 = new List<object[]>();
foreach (var row in ds)
{
var ds2item = new object[8];
var ds2item = new object[9];
ds2item[0] = row[0];
ds2item[1] = row[1];
ds2item[2] = Regex.Replace(string.Concat(row[2]), @"\(\d+\)", "");
@ -280,6 +300,7 @@ where a.owner in ({1}) and {0}
ds2item[5] = string.Concat(row[7]);
ds2item[6] = string.Concat(row[8]);
ds2item[7] = string.Concat(row[9]);
ds2item[8] = string.Concat(row[10]);
ds2.Add(ds2item);
}
foreach (var row in ds2)
@ -294,6 +315,7 @@ where a.owner in ({1}) and {0}
bool is_nullable = string.Concat(row[5]) == "1";
bool is_identity = string.Concat(row[6]) == "1";
string comment = string.Concat(row[7]);
string defaultValue = string.Concat(row[8]);
if (max_length == 0) max_length = -1;
if (database.Length == 1)
{
@ -309,7 +331,8 @@ where a.owner in ({1}) and {0}
DbTypeText = type,
DbTypeTextFull = sqlType,
Table = loc2[table_id],
Coment = comment
Coment = comment,
DefaultValue = defaultValue
});
loc3[table_id][column].DbType = this.GetDbType(loc3[table_id][column]);
loc3[table_id][column].CsType = this.GetCsTypeInfo(loc3[table_id][column]);

View File

@ -348,6 +348,7 @@ where {loc8.ToString().Replace("a.table_name", "ns.nspname || '.' || c.relname")
var is_nullable = string.Concat(row[5]) == "1";
var is_identity = string.Concat(row[6]).StartsWith(@"nextval('") && string.Concat(row[6]).EndsWith(@"'::regclass)");
var comment = string.Concat(row[7]);
var defaultValue = string.Concat(row[6]);
int attndims = int.Parse(string.Concat(row[8]));
string typtype = string.Concat(row[9]);
string owner = string.Concat(row[10]);
@ -383,7 +384,8 @@ where {loc8.ToString().Replace("a.table_name", "ns.nspname || '.' || c.relname")
DbTypeText = type,
DbTypeTextFull = sqlType,
Table = loc2[object_id],
Coment = comment
Coment = comment,
DefaultValue = defaultValue
});
loc3[object_id][column].DbType = this.GetDbType(loc3[object_id][column]);
loc3[object_id][column].CsType = this.GetCsTypeInfo(loc3[object_id][column]);

View File

@ -246,13 +246,16 @@ isnull(e.name,'') + '.' + isnull(d.name,'')
{0} a
inner join sys.types b on b.user_type_id = a.user_type_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
left join sys.schemas e on e.schema_id = d.schema_id{2}
where {1}
";
sql = string.Format(tsql_place, @"
,a.is_nullable 'IsNullable'
,a.is_identity 'IsIdentity'
from sys.columns", loc8.ToString().Replace("a.table_name", "a.object_id"));
,f.text as 'DefaultValue'
from sys.columns", loc8.ToString().Replace("a.table_name", "a.object_id"), @"
left join syscomments f on f.id = a.default_object_id
");
if (loc88.Length > 0)
{
sql += "union all" +
@ -261,7 +264,8 @@ from sys.columns", loc8.ToString().Replace("a.table_name", "a.object_id"));
" select value from sys.extended_properties where major_id = a.object_id AND minor_id = a.parameter_id"), @"
,cast(0 as bit) 'IsNullable'
,a.is_output 'IsIdentity'
from sys.parameters", loc88.ToString().Replace("a.table_name", "a.object_id"));
,'' as 'DefaultValue'
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,6 +282,7 @@ from sys.parameters", loc88.ToString().Replace("a.table_name", "a.object_id"));
var comment = string.Concat(row[6]);
var is_nullable = bool.Parse(string.Concat(row[7]));
var is_identity = bool.Parse(string.Concat(row[8]));
var defaultValue = string.Concat(row[9]);
if (max_length == 0) max_length = -1;
loc3[object_id].Add(column, new DbColumnInfo
@ -290,7 +295,8 @@ from sys.parameters", loc88.ToString().Replace("a.table_name", "a.object_id"));
DbTypeText = type,
DbTypeTextFull = sqlType,
Table = loc2[object_id],
Coment = comment
Coment = comment,
DefaultValue = defaultValue
});
loc3[object_id][column].DbType = this.GetDbType(loc3[object_id][column]);
loc3[object_id][column].CsType = this.GetCsTypeInfo(loc3[object_id][column]);

View File

@ -172,6 +172,7 @@ namespace FreeSql.Sqlite
bool is_identity = string.Concat(row[6]) == "1";
bool is_primary = string.Concat(row[7]) == "1";
string comment = string.Concat(row[8]);
string defaultValue = string.Concat(row[9]);
if (max_length == 0) max_length = -1;
loc3[table_id].Add(column, new DbColumnInfo
{
@ -183,7 +184,8 @@ namespace FreeSql.Sqlite
DbTypeText = type,
DbTypeTextFull = sqlType,
Table = loc2[table_id],
Coment = comment
Coment = comment,
DefaultValue = defaultValue
});
loc3[table_id][column].DbType = this.GetDbType(loc3[table_id][column]);
loc3[table_id][column].CsType = this.GetCsTypeInfo(loc3[table_id][column]);
@ -256,7 +258,7 @@ from {db}.sqlite_master where type = 'table'";
if (dsqlLastIdx > 0) is_identity = dsql.Substring(dsqlIdx.Value, dsqlLastIdx - dsqlIdx.Value).Contains("AUTOINCREMENT");
}
var ds2item = new object[9];
var ds2item = new object[10];
ds2item[0] = table_id;
ds2item[1] = col_name;
ds2item[2] = Regex.Replace(string.Concat(col[2]), @"\(\d+(\b*,\b*\d+)?\)", "").ToUpper();
@ -265,6 +267,7 @@ from {db}.sqlite_master where type = 'table'";
ds2item[6] = is_identity;
ds2item[7] = string.Concat(col[5]) == "1" ? 1 : 0;
ds2item[8] = "";
ds2item[9] = string.Concat(col[4]);
addColumn(ds2item);
}