mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 18:52:50 +08:00
- 增加 创建表时指定字段位置,如:[Column(Position = 1],可为负数即反方向位置;
This commit is contained in:
parent
4d2406aa1e
commit
6e75a8cebc
@ -30,6 +30,7 @@ namespace FreeSql
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 主键
|
/// 主键
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[Column(Position = 1)]
|
||||||
public virtual TKey Id { get; set; }
|
public virtual TKey Id { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -42,18 +42,22 @@ namespace FreeSql
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 创建时间
|
/// 创建时间
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[Column(Position = -4)]
|
||||||
public DateTime CreateTime { get; set; }
|
public DateTime CreateTime { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 更新时间
|
/// 更新时间
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[Column(Position = -3)]
|
||||||
public DateTime UpdateTime { get; set; }
|
public DateTime UpdateTime { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 逻辑删除
|
/// 逻辑删除
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[Column(Position = -2)]
|
||||||
public bool IsDeleted { get; set; }
|
public bool IsDeleted { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 排序
|
/// 排序
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[Column(Position = -1)]
|
||||||
public int Sort { get; set; }
|
public int Sort { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -79,5 +79,17 @@ namespace FreeSql.DataAnnotations
|
|||||||
/// 类型映射,比如:可将 enum 属性映射成 typeof(string)
|
/// 类型映射,比如:可将 enum 属性映射成 typeof(string)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Type MapType { get; set; }
|
public Type MapType { get; set; }
|
||||||
}
|
|
||||||
|
internal short? _Position;
|
||||||
|
/// <summary>
|
||||||
|
/// 创建表时字段位置,规则如下:
|
||||||
|
/// <para></para>
|
||||||
|
/// >0时排前面,1,2,3...
|
||||||
|
/// <para></para>
|
||||||
|
/// =0时排中间(默认)
|
||||||
|
/// <para></para>
|
||||||
|
/// <0时排后面,...-3,-2,-1
|
||||||
|
/// </summary>
|
||||||
|
public short Position { get => _Position ?? 0; set => _Position = value; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -89,11 +89,28 @@ namespace FreeSql.DataAnnotations
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 类型映射,比如:可将 enum 属性映射成 typeof(string)
|
/// 类型映射,比如:可将 enum 属性映射成 typeof(string)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="type"></param>
|
/// <param name="value"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public ColumnFluent MapType(Type type)
|
public ColumnFluent MapType(Type value)
|
||||||
{
|
{
|
||||||
_column.MapType = type;
|
_column.MapType = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建表时字段位置,规则如下:
|
||||||
|
/// <para></para>
|
||||||
|
/// >0时排前面
|
||||||
|
/// <para></para>
|
||||||
|
/// =0时排中间(默认)
|
||||||
|
/// <para></para>
|
||||||
|
/// <0时排后面
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public ColumnFluent Position(short value)
|
||||||
|
{
|
||||||
|
_column.Position = value;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,6 +59,17 @@
|
|||||||
类型映射,比如:可将 enum 属性映射成 typeof(string)
|
类型映射,比如:可将 enum 属性映射成 typeof(string)
|
||||||
</summary>
|
</summary>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="P:FreeSql.DataAnnotations.ColumnAttribute.Position">
|
||||||
|
<summary>
|
||||||
|
创建表时字段位置,规则如下:
|
||||||
|
<para></para>
|
||||||
|
>0时排前面,1,2,3...
|
||||||
|
<para></para>
|
||||||
|
=0时排中间(默认)
|
||||||
|
<para></para>
|
||||||
|
<0时排后面,...-3,-2,-1
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
<member name="M:FreeSql.DataAnnotations.ColumnFluent.Name(System.String)">
|
<member name="M:FreeSql.DataAnnotations.ColumnFluent.Name(System.String)">
|
||||||
<summary>
|
<summary>
|
||||||
数据库列名
|
数据库列名
|
||||||
@ -110,7 +121,20 @@
|
|||||||
<summary>
|
<summary>
|
||||||
类型映射,比如:可将 enum 属性映射成 typeof(string)
|
类型映射,比如:可将 enum 属性映射成 typeof(string)
|
||||||
</summary>
|
</summary>
|
||||||
<param name="type"></param>
|
<param name="value"></param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.DataAnnotations.ColumnFluent.Position(System.Int16)">
|
||||||
|
<summary>
|
||||||
|
创建表时字段位置,规则如下:
|
||||||
|
<para></para>
|
||||||
|
>0时排前面
|
||||||
|
<para></para>
|
||||||
|
=0时排中间(默认)
|
||||||
|
<para></para>
|
||||||
|
<0时排后面
|
||||||
|
</summary>
|
||||||
|
<param name="value"></param>
|
||||||
<returns></returns>
|
<returns></returns>
|
||||||
</member>
|
</member>
|
||||||
<member name="P:FreeSql.DataAnnotations.NavigateAttribute.Bind">
|
<member name="P:FreeSql.DataAnnotations.NavigateAttribute.Bind">
|
||||||
|
@ -126,6 +126,7 @@ namespace FreeSql.Internal
|
|||||||
if (trycol._IsVersion != null) attr._IsVersion = trycol.IsVersion;
|
if (trycol._IsVersion != null) attr._IsVersion = trycol.IsVersion;
|
||||||
if (trycol._Uniques != null) attr._Uniques = trycol._Uniques;
|
if (trycol._Uniques != null) attr._Uniques = trycol._Uniques;
|
||||||
if (trycol.MapType != null) attr.MapType = trycol.MapType;
|
if (trycol.MapType != null) attr.MapType = trycol.MapType;
|
||||||
|
if (trycol._Position != null) attr._Position = trycol.Position;
|
||||||
if (trycol.DbDefautValue != null) attr.DbDefautValue = trycol.DbDefautValue;
|
if (trycol.DbDefautValue != null) attr.DbDefautValue = trycol.DbDefautValue;
|
||||||
}
|
}
|
||||||
var attrs = proto.GetCustomAttributes(typeof(ColumnAttribute), false);
|
var attrs = proto.GetCustomAttributes(typeof(ColumnAttribute), false);
|
||||||
@ -143,6 +144,7 @@ namespace FreeSql.Internal
|
|||||||
if (tryattr._IsVersion != null) attr._IsVersion = tryattr.IsVersion;
|
if (tryattr._IsVersion != null) attr._IsVersion = tryattr.IsVersion;
|
||||||
if (tryattr._Uniques != null) attr._Uniques = tryattr._Uniques;
|
if (tryattr._Uniques != null) attr._Uniques = tryattr._Uniques;
|
||||||
if (tryattr.MapType != null) attr.MapType = tryattr.MapType;
|
if (tryattr.MapType != null) attr.MapType = tryattr.MapType;
|
||||||
|
if (tryattr._Position != null) attr._Position = tryattr.Position;
|
||||||
if (tryattr.DbDefautValue != null) attr.DbDefautValue = tryattr.DbDefautValue;
|
if (tryattr.DbDefautValue != null) attr.DbDefautValue = tryattr.DbDefautValue;
|
||||||
}
|
}
|
||||||
ColumnAttribute ret = null;
|
ColumnAttribute ret = null;
|
||||||
@ -156,6 +158,7 @@ namespace FreeSql.Internal
|
|||||||
if (attr._IsVersion != null) ret = attr;
|
if (attr._IsVersion != null) ret = attr;
|
||||||
if (attr._Uniques != null) ret = attr;
|
if (attr._Uniques != null) ret = attr;
|
||||||
if (attr.MapType != null) ret = attr;
|
if (attr.MapType != null) ret = attr;
|
||||||
|
if (attr._Position != null) ret = attr;
|
||||||
if (attr.DbDefautValue != null) ret = attr;
|
if (attr.DbDefautValue != null) ret = attr;
|
||||||
if (ret != null && ret.MapType == null) ret.MapType = proto.PropertyType;
|
if (ret != null && ret.MapType == null) ret.MapType = proto.PropertyType;
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -14,6 +14,7 @@ namespace FreeSql.Internal.Model
|
|||||||
public Dictionary<string, ColumnInfo> Columns { get; set; } = new Dictionary<string, ColumnInfo>(StringComparer.CurrentCultureIgnoreCase);
|
public Dictionary<string, ColumnInfo> Columns { get; set; } = new Dictionary<string, ColumnInfo>(StringComparer.CurrentCultureIgnoreCase);
|
||||||
public Dictionary<string, ColumnInfo> ColumnsByCs { get; set; } = new Dictionary<string, ColumnInfo>(StringComparer.CurrentCultureIgnoreCase);
|
public Dictionary<string, ColumnInfo> ColumnsByCs { get; set; } = new Dictionary<string, ColumnInfo>(StringComparer.CurrentCultureIgnoreCase);
|
||||||
public Dictionary<string, ColumnInfo> ColumnsByCsIgnore { get; set; } = new Dictionary<string, ColumnInfo>(StringComparer.CurrentCultureIgnoreCase);
|
public Dictionary<string, ColumnInfo> ColumnsByCsIgnore { get; set; } = new Dictionary<string, ColumnInfo>(StringComparer.CurrentCultureIgnoreCase);
|
||||||
|
public ColumnInfo[] ColumnsByPosition { get; set; }
|
||||||
public ColumnInfo[] Primarys { get; set; }
|
public ColumnInfo[] Primarys { get; set; }
|
||||||
public Dictionary<string, List<ColumnInfo>> Uniques { get; set; }
|
public Dictionary<string, List<ColumnInfo>> Uniques { get; set; }
|
||||||
public string CsName { get; set; }
|
public string CsName { get; set; }
|
||||||
|
@ -70,6 +70,7 @@ namespace FreeSql.Internal
|
|||||||
var propsLazy = new List<(PropertyInfo, bool, bool)>();
|
var propsLazy = new List<(PropertyInfo, bool, bool)>();
|
||||||
var propsNavObjs = new List<PropertyInfo>();
|
var propsNavObjs = new List<PropertyInfo>();
|
||||||
var propsComment = CommonUtils.GetProperyCommentBySummary(entity);
|
var propsComment = CommonUtils.GetProperyCommentBySummary(entity);
|
||||||
|
var columnsList = new List<ColumnInfo>();
|
||||||
foreach (var p in trytb.Properties.Values)
|
foreach (var p in trytb.Properties.Values)
|
||||||
{
|
{
|
||||||
var setMethod = trytb.Type.GetMethod($"set_{p.Name}");
|
var setMethod = trytb.Type.GetMethod($"set_{p.Name}");
|
||||||
@ -169,6 +170,7 @@ namespace FreeSql.Internal
|
|||||||
|
|
||||||
trytb.Columns.Add(colattr.Name, col);
|
trytb.Columns.Add(colattr.Name, col);
|
||||||
trytb.ColumnsByCs.Add(p.Name, col);
|
trytb.ColumnsByCs.Add(p.Name, col);
|
||||||
|
columnsList.Add(col);
|
||||||
}
|
}
|
||||||
trytb.VersionColumn = trytb.Columns.Values.Where(a => a.Attribute.IsVersion == true).LastOrDefault();
|
trytb.VersionColumn = trytb.Columns.Values.Where(a => a.Attribute.IsVersion == true).LastOrDefault();
|
||||||
if (trytb.VersionColumn != null)
|
if (trytb.VersionColumn != null)
|
||||||
@ -241,6 +243,9 @@ namespace FreeSql.Internal
|
|||||||
}
|
}
|
||||||
var allunique = trytb.Columns.Values.Where(a => a.Attribute._Uniques != null).SelectMany(a => a.Attribute._Uniques).Distinct();
|
var allunique = trytb.Columns.Values.Where(a => a.Attribute._Uniques != null).SelectMany(a => a.Attribute._Uniques).Distinct();
|
||||||
trytb.Uniques = allunique.ToDictionary(a => a, a => trytb.Columns.Values.Where(b => b.Attribute._Uniques != null && b.Attribute._Uniques.Contains(a)).ToList());
|
trytb.Uniques = allunique.ToDictionary(a => a, a => trytb.Columns.Values.Where(b => b.Attribute._Uniques != null && b.Attribute._Uniques.Contains(a)).ToList());
|
||||||
|
trytb.ColumnsByPosition = columnsList.Where(a => a.Attribute.Position > 0).OrderBy(a => a.Attribute.Position)
|
||||||
|
.Concat(columnsList.Where(a => a.Attribute.Position == 0))
|
||||||
|
.Concat(columnsList.Where(a => a.Attribute.Position < 0).OrderBy(a => a.Attribute.Position)).ToArray();
|
||||||
tbc.AddOrUpdate(entity, trytb, (oldkey, oldval) => trytb);
|
tbc.AddOrUpdate(entity, trytb, (oldkey, oldval) => trytb);
|
||||||
|
|
||||||
#region 查找导航属性的关系、virtual 属性延时加载,动态产生新的重写类
|
#region 查找导航属性的关系、virtual 属性延时加载,动态产生新的重写类
|
||||||
|
@ -132,7 +132,7 @@ namespace FreeSql.MySql
|
|||||||
{
|
{
|
||||||
//创建表
|
//创建表
|
||||||
sb.Append("CREATE TABLE IF NOT EXISTS ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ( ");
|
sb.Append("CREATE TABLE IF NOT EXISTS ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ( ");
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
{
|
{
|
||||||
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
|
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
|
||||||
sb.Append(tbcol.Attribute.DbType);
|
sb.Append(tbcol.Attribute.DbType);
|
||||||
@ -197,7 +197,7 @@ where a.table_schema in ({0}) and a.table_name in ({1})", tboldname ?? tbname);
|
|||||||
if (istmpatler == false)
|
if (istmpatler == false)
|
||||||
{
|
{
|
||||||
var existsPrimary = ExecuteScalar(tbname[0], _commonUtils.FormatSql("select 1 from information_schema.key_column_usage where table_schema={0} and table_name={1} and constraint_name = 'PRIMARY' limit 1", tbname));
|
var existsPrimary = ExecuteScalar(tbname[0], _commonUtils.FormatSql("select 1 from information_schema.key_column_usage where table_schema={0} and table_name={1} and constraint_name = 'PRIMARY' limit 1", tbname));
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
{
|
{
|
||||||
var isIdentityChanged = tbcol.Attribute.IsIdentity == true && tbcol.Attribute.DbType.IndexOf("AUTO_INCREMENT", StringComparison.CurrentCultureIgnoreCase) == -1;
|
var isIdentityChanged = tbcol.Attribute.IsIdentity == true && tbcol.Attribute.DbType.IndexOf("AUTO_INCREMENT", StringComparison.CurrentCultureIgnoreCase) == -1;
|
||||||
if (tbstruct.TryGetValue(tbcol.Attribute.Name, out var tbstructcol) ||
|
if (tbstruct.TryGetValue(tbcol.Attribute.Name, out var tbstructcol) ||
|
||||||
@ -263,7 +263,7 @@ where a.constraint_schema IN ({0}) and a.table_name IN ({1})", tboldname ?? tbna
|
|||||||
var tmptablename = _commonUtils.QuoteSqlName($"{tbname[0]}.FreeSqlTmp_{tbname[1]}");
|
var tmptablename = _commonUtils.QuoteSqlName($"{tbname[0]}.FreeSqlTmp_{tbname[1]}");
|
||||||
//创建临时表
|
//创建临时表
|
||||||
sb.Append("CREATE TABLE IF NOT EXISTS ").Append(tmptablename).Append(" ( ");
|
sb.Append("CREATE TABLE IF NOT EXISTS ").Append(tmptablename).Append(" ( ");
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
{
|
{
|
||||||
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
|
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
|
||||||
sb.Append(tbcol.Attribute.DbType);
|
sb.Append(tbcol.Attribute.DbType);
|
||||||
@ -286,10 +286,10 @@ where a.constraint_schema IN ({0}) and a.table_name IN ({1})", tboldname ?? tbna
|
|||||||
sb.Remove(sb.Length - 1, 1);
|
sb.Remove(sb.Length - 1, 1);
|
||||||
sb.Append("\r\n) Engine=InnoDB;\r\n");
|
sb.Append("\r\n) Engine=InnoDB;\r\n");
|
||||||
sb.Append("INSERT INTO ").Append(tmptablename).Append(" (");
|
sb.Append("INSERT INTO ").Append(tmptablename).Append(" (");
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
sb.Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(", ");
|
sb.Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(", ");
|
||||||
sb.Remove(sb.Length - 2, 2).Append(")\r\nSELECT ");
|
sb.Remove(sb.Length - 2, 2).Append(")\r\nSELECT ");
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
{
|
{
|
||||||
var insertvalue = "NULL";
|
var insertvalue = "NULL";
|
||||||
if (tbstruct.TryGetValue(tbcol.Attribute.Name, out var tbstructcol) ||
|
if (tbstruct.TryGetValue(tbcol.Attribute.Name, out var tbstructcol) ||
|
||||||
|
@ -110,7 +110,7 @@ namespace FreeSql.Oracle
|
|||||||
{
|
{
|
||||||
//创建表
|
//创建表
|
||||||
sb.Append("execute immediate 'CREATE TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ( ");
|
sb.Append("execute immediate 'CREATE TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ( ");
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
{
|
{
|
||||||
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(tbcol.Attribute.DbType).Append(",");
|
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(tbcol.Attribute.DbType).Append(",");
|
||||||
if (tbcol.Attribute.IsIdentity == true) seqcols.Add((tbcol, tbname, true));
|
if (tbcol.Attribute.IsIdentity == true) seqcols.Add((tbcol, tbname, true));
|
||||||
@ -131,7 +131,7 @@ namespace FreeSql.Oracle
|
|||||||
sb.Remove(sb.Length - 1, 1);
|
sb.Remove(sb.Length - 1, 1);
|
||||||
sb.Append("\r\n) \r\nLOGGING \r\nNOCOMPRESS \r\nNOCACHE\r\n';\r\n");
|
sb.Append("\r\n) \r\nLOGGING \r\nNOCOMPRESS \r\nNOCACHE\r\n';\r\n");
|
||||||
//备注
|
//备注
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(tbcol.Comment) == false)
|
if (string.IsNullOrEmpty(tbcol.Comment) == false)
|
||||||
sb.Append("execute immediate 'COMMENT ON COLUMN ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}.{tbcol.Attribute.Name}")).Append(" IS ").Append(_commonUtils.FormatSql("{0}", tbcol.Comment).Replace("'", "''")).Append("';\r\n");
|
sb.Append("execute immediate 'COMMENT ON COLUMN ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}.{tbcol.Attribute.Name}")).Append(" IS ").Append(_commonUtils.FormatSql("{0}", tbcol.Comment).Replace("'", "''")).Append("';\r\n");
|
||||||
@ -182,7 +182,7 @@ where a.owner={{0}} and a.table_name={{1}}", tboldname ?? tbname);
|
|||||||
|
|
||||||
if (istmpatler == false)
|
if (istmpatler == false)
|
||||||
{
|
{
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
{
|
{
|
||||||
var dbtypeNoneNotNull = Regex.Replace(tbcol.Attribute.DbType, @"NOT\s+NULL", "NULL");
|
var dbtypeNoneNotNull = Regex.Replace(tbcol.Attribute.DbType, @"NOT\s+NULL", "NULL");
|
||||||
if (tbstruct.TryGetValue(tbcol.Attribute.Name, out var tbstructcol) ||
|
if (tbstruct.TryGetValue(tbcol.Attribute.Name, out var tbstructcol) ||
|
||||||
@ -264,7 +264,7 @@ and a.owner in ({0}) and a.table_name in ({1})", tboldname ?? tbname);
|
|||||||
var tmptablename = _commonUtils.QuoteSqlName($"{tbname[0]}.FTmp_{tbname[1]}");
|
var tmptablename = _commonUtils.QuoteSqlName($"{tbname[0]}.FTmp_{tbname[1]}");
|
||||||
//创建临时表
|
//创建临时表
|
||||||
sb.Append("execute immediate 'CREATE TABLE ").Append(tmptablename).Append(" ( ");
|
sb.Append("execute immediate 'CREATE TABLE ").Append(tmptablename).Append(" ( ");
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
{
|
{
|
||||||
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(tbcol.Attribute.DbType).Append(",");
|
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(tbcol.Attribute.DbType).Append(",");
|
||||||
if (tbcol.Attribute.IsIdentity == true) seqcols.Add((tbcol, tbname, true));
|
if (tbcol.Attribute.IsIdentity == true) seqcols.Add((tbcol, tbname, true));
|
||||||
@ -285,16 +285,16 @@ and a.owner in ({0}) and a.table_name in ({1})", tboldname ?? tbname);
|
|||||||
sb.Remove(sb.Length - 1, 1);
|
sb.Remove(sb.Length - 1, 1);
|
||||||
sb.Append("\r\n) LOGGING \r\nNOCOMPRESS \r\nNOCACHE\r\n';\r\n");
|
sb.Append("\r\n) LOGGING \r\nNOCOMPRESS \r\nNOCACHE\r\n';\r\n");
|
||||||
//备注
|
//备注
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(tbcol.Comment) == false)
|
if (string.IsNullOrEmpty(tbcol.Comment) == false)
|
||||||
sb.Append("execute immediate 'COMMENT ON COLUMN ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.FTmp_{tbname[1]}.{tbcol.Attribute.Name}")).Append(" IS ").Append(_commonUtils.FormatSql("{0}", tbcol.Comment).Replace("'", "''")).Append("';\r\n");
|
sb.Append("execute immediate 'COMMENT ON COLUMN ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.FTmp_{tbname[1]}.{tbcol.Attribute.Name}")).Append(" IS ").Append(_commonUtils.FormatSql("{0}", tbcol.Comment).Replace("'", "''")).Append("';\r\n");
|
||||||
}
|
}
|
||||||
sb.Append("execute immediate 'INSERT INTO ").Append(tmptablename).Append(" (");
|
sb.Append("execute immediate 'INSERT INTO ").Append(tmptablename).Append(" (");
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
sb.Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(", ");
|
sb.Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(", ");
|
||||||
sb.Remove(sb.Length - 2, 2).Append(")\r\nSELECT ");
|
sb.Remove(sb.Length - 2, 2).Append(")\r\nSELECT ");
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
{
|
{
|
||||||
var insertvalue = "NULL";
|
var insertvalue = "NULL";
|
||||||
if (tbstruct.TryGetValue(tbcol.Attribute.Name, out var tbstructcol) ||
|
if (tbstruct.TryGetValue(tbcol.Attribute.Name, out var tbstructcol) ||
|
||||||
|
@ -151,7 +151,7 @@ namespace FreeSql.PostgreSQL
|
|||||||
{
|
{
|
||||||
//创建表
|
//创建表
|
||||||
sb.Append("CREATE TABLE IF NOT EXISTS ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ( ");
|
sb.Append("CREATE TABLE IF NOT EXISTS ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ( ");
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
{
|
{
|
||||||
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(tbcol.Attribute.DbType).Append(",");
|
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(tbcol.Attribute.DbType).Append(",");
|
||||||
if (tbcol.Attribute.IsIdentity == true) seqcols.Add((tbcol, tbname, true));
|
if (tbcol.Attribute.IsIdentity == true) seqcols.Add((tbcol, tbname, true));
|
||||||
@ -171,7 +171,7 @@ namespace FreeSql.PostgreSQL
|
|||||||
sb.Remove(sb.Length - 1, 1);
|
sb.Remove(sb.Length - 1, 1);
|
||||||
sb.Append("\r\n) WITH (OIDS=FALSE);\r\n");
|
sb.Append("\r\n) WITH (OIDS=FALSE);\r\n");
|
||||||
//备注
|
//备注
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(tbcol.Comment) == false)
|
if (string.IsNullOrEmpty(tbcol.Comment) == false)
|
||||||
sb.Append("COMMENT ON COLUMN ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}.{tbcol.Attribute.Name}")).Append(" IS ").Append(_commonUtils.FormatSql("{0}", tbcol.Comment)).Append(";\r\n");
|
sb.Append("COMMENT ON COLUMN ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}.{tbcol.Attribute.Name}")).Append(" IS ").Append(_commonUtils.FormatSql("{0}", tbcol.Comment)).Append(";\r\n");
|
||||||
@ -242,7 +242,7 @@ where ns.nspname = {0} and c.relname = {1}", tboldname ?? tbname);
|
|||||||
|
|
||||||
if (istmpatler == false)
|
if (istmpatler == false)
|
||||||
{
|
{
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
{
|
{
|
||||||
if (tbstruct.TryGetValue(tbcol.Attribute.Name, out var tbstructcol) ||
|
if (tbstruct.TryGetValue(tbcol.Attribute.Name, out var tbstructcol) ||
|
||||||
string.IsNullOrEmpty(tbcol.Attribute.OldName) == false && tbstruct.TryGetValue(tbcol.Attribute.OldName, out tbstructcol))
|
string.IsNullOrEmpty(tbcol.Attribute.OldName) == false && tbstruct.TryGetValue(tbcol.Attribute.OldName, out tbstructcol))
|
||||||
@ -311,7 +311,7 @@ where pg_namespace.nspname={0} and pg_class.relname={1} and pg_constraint.contyp
|
|||||||
var tmptablename = _commonUtils.QuoteSqlName($"{tbname[0]}.FreeSqlTmp_{tbname[1]}");
|
var tmptablename = _commonUtils.QuoteSqlName($"{tbname[0]}.FreeSqlTmp_{tbname[1]}");
|
||||||
//创建临时表
|
//创建临时表
|
||||||
sb.Append("CREATE TABLE IF NOT EXISTS ").Append(tmptablename).Append(" ( ");
|
sb.Append("CREATE TABLE IF NOT EXISTS ").Append(tmptablename).Append(" ( ");
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
{
|
{
|
||||||
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(tbcol.Attribute.DbType).Append(",");
|
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(tbcol.Attribute.DbType).Append(",");
|
||||||
if (tbcol.Attribute.IsIdentity == true) seqcols.Add((tbcol, tbname, true));
|
if (tbcol.Attribute.IsIdentity == true) seqcols.Add((tbcol, tbname, true));
|
||||||
@ -331,16 +331,16 @@ where pg_namespace.nspname={0} and pg_class.relname={1} and pg_constraint.contyp
|
|||||||
sb.Remove(sb.Length - 1, 1);
|
sb.Remove(sb.Length - 1, 1);
|
||||||
sb.Append("\r\n) WITH (OIDS=FALSE);\r\n");
|
sb.Append("\r\n) WITH (OIDS=FALSE);\r\n");
|
||||||
//备注
|
//备注
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(tbcol.Comment) == false)
|
if (string.IsNullOrEmpty(tbcol.Comment) == false)
|
||||||
sb.Append("COMMENT ON COLUMN ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.FreeSqlTmp_{tbname[1]}.{tbcol.Attribute.Name}")).Append(" IS ").Append(_commonUtils.FormatSql("{0}", tbcol.Comment)).Append(";\r\n");
|
sb.Append("COMMENT ON COLUMN ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.FreeSqlTmp_{tbname[1]}.{tbcol.Attribute.Name}")).Append(" IS ").Append(_commonUtils.FormatSql("{0}", tbcol.Comment)).Append(";\r\n");
|
||||||
}
|
}
|
||||||
sb.Append("INSERT INTO ").Append(tmptablename).Append(" (");
|
sb.Append("INSERT INTO ").Append(tmptablename).Append(" (");
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
sb.Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(", ");
|
sb.Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(", ");
|
||||||
sb.Remove(sb.Length - 2, 2).Append(")\r\nSELECT ");
|
sb.Remove(sb.Length - 2, 2).Append(")\r\nSELECT ");
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
{
|
{
|
||||||
var insertvalue = "NULL";
|
var insertvalue = "NULL";
|
||||||
if (tbstruct.TryGetValue(tbcol.Attribute.Name, out var tbstructcol) ||
|
if (tbstruct.TryGetValue(tbcol.Attribute.Name, out var tbstructcol) ||
|
||||||
|
@ -162,7 +162,7 @@ ELSE
|
|||||||
//创建新表
|
//创建新表
|
||||||
sb.Append("use ").Append(_commonUtils.QuoteSqlName(tbname[0])).Append(";\r\nCREATE TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[1]}.{tbname[2]}")).Append(" ( ");
|
sb.Append("use ").Append(_commonUtils.QuoteSqlName(tbname[0])).Append(";\r\nCREATE TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[1]}.{tbname[2]}")).Append(" ( ");
|
||||||
var pkidx = 0;
|
var pkidx = 0;
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
{
|
{
|
||||||
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
|
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
|
||||||
sb.Append(tbcol.Attribute.DbType);
|
sb.Append(tbcol.Attribute.DbType);
|
||||||
@ -188,7 +188,7 @@ ELSE
|
|||||||
}
|
}
|
||||||
sb.Remove(sb.Length - 1, 1).Append("\r\n);\r\n");
|
sb.Remove(sb.Length - 1, 1).Append("\r\n);\r\n");
|
||||||
//备注
|
//备注
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(tbcol.Comment) == false)
|
if (string.IsNullOrEmpty(tbcol.Comment) == false)
|
||||||
AddOrUpdateMS_Description(sb, tbname[1], tbname[2], tbcol.Attribute.Name, tbcol.Comment);
|
AddOrUpdateMS_Description(sb, tbname[1], tbname[2], tbcol.Attribute.Name, tbcol.Comment);
|
||||||
@ -242,7 +242,7 @@ use " + database, tboldname ?? tbname);
|
|||||||
|
|
||||||
if (istmpatler == false)
|
if (istmpatler == false)
|
||||||
{
|
{
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
{
|
{
|
||||||
if (tbstruct.TryGetValue(tbcol.Attribute.Name, out var tbstructcol) ||
|
if (tbstruct.TryGetValue(tbcol.Attribute.Name, out var tbstructcol) ||
|
||||||
string.IsNullOrEmpty(tbcol.Attribute.OldName) == false && tbstruct.TryGetValue(tbcol.Attribute.OldName, out tbstructcol))
|
string.IsNullOrEmpty(tbcol.Attribute.OldName) == false && tbstruct.TryGetValue(tbcol.Attribute.OldName, out tbstructcol))
|
||||||
@ -321,7 +321,7 @@ use " + database, tboldname ?? tbname);
|
|||||||
//创建临时表
|
//创建临时表
|
||||||
sb.Append("CREATE TABLE ").Append(tmptablename).Append(" ( ");
|
sb.Append("CREATE TABLE ").Append(tmptablename).Append(" ( ");
|
||||||
var pkidx2 = 0;
|
var pkidx2 = 0;
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
{
|
{
|
||||||
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
|
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
|
||||||
sb.Append(tbcol.Attribute.DbType);
|
sb.Append(tbcol.Attribute.DbType);
|
||||||
@ -348,7 +348,7 @@ use " + database, tboldname ?? tbname);
|
|||||||
}
|
}
|
||||||
sb.Remove(sb.Length - 1, 1).Append("\r\n);\r\n");
|
sb.Remove(sb.Length - 1, 1).Append("\r\n);\r\n");
|
||||||
//备注
|
//备注
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(tbcol.Comment) == false)
|
if (string.IsNullOrEmpty(tbcol.Comment) == false)
|
||||||
AddOrUpdateMS_Description(sb, tbname[1], $"FreeSqlTmp_{tbname[2]}", tbcol.Attribute.Name, tbcol.Comment);
|
AddOrUpdateMS_Description(sb, tbname[1], $"FreeSqlTmp_{tbname[2]}", tbcol.Attribute.Name, tbcol.Comment);
|
||||||
@ -357,10 +357,10 @@ use " + database, tboldname ?? tbname);
|
|||||||
if (idents) sb.Append("SET IDENTITY_INSERT ").Append(tmptablename).Append(" ON;\r\n");
|
if (idents) sb.Append("SET IDENTITY_INSERT ").Append(tmptablename).Append(" ON;\r\n");
|
||||||
sb.Append("IF EXISTS(SELECT 1 FROM ").Append(tablename).Append(")\r\n");
|
sb.Append("IF EXISTS(SELECT 1 FROM ").Append(tablename).Append(")\r\n");
|
||||||
sb.Append("\tEXEC('INSERT INTO ").Append(tmptablename).Append(" (");
|
sb.Append("\tEXEC('INSERT INTO ").Append(tmptablename).Append(" (");
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
sb.Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(", ");
|
sb.Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(", ");
|
||||||
sb.Remove(sb.Length - 2, 2).Append(")\r\n\t\tSELECT ");
|
sb.Remove(sb.Length - 2, 2).Append(")\r\n\t\tSELECT ");
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
{
|
{
|
||||||
var insertvalue = "NULL";
|
var insertvalue = "NULL";
|
||||||
if (tbstruct.TryGetValue(tbcol.Attribute.Name, out var tbstructcol) ||
|
if (tbstruct.TryGetValue(tbcol.Attribute.Name, out var tbstructcol) ||
|
||||||
|
@ -100,7 +100,7 @@ namespace FreeSql.Sqlite
|
|||||||
{
|
{
|
||||||
//创建表
|
//创建表
|
||||||
sb.Append("CREATE TABLE IF NOT EXISTS ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ( ");
|
sb.Append("CREATE TABLE IF NOT EXISTS ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ( ");
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
{
|
{
|
||||||
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
|
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
|
||||||
sb.Append(tbcol.Attribute.DbType);
|
sb.Append(tbcol.Attribute.DbType);
|
||||||
@ -163,7 +163,7 @@ namespace FreeSql.Sqlite
|
|||||||
|
|
||||||
if (istmpatler == false)
|
if (istmpatler == false)
|
||||||
{
|
{
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
{
|
{
|
||||||
var dbtypeNoneNotNull = Regex.Replace(tbcol.Attribute.DbType, @"NOT\s+NULL", "NULL");
|
var dbtypeNoneNotNull = Regex.Replace(tbcol.Attribute.DbType, @"NOT\s+NULL", "NULL");
|
||||||
if (tbstruct.TryGetValue(tbcol.Attribute.Name, out var tbstructcol) ||
|
if (tbstruct.TryGetValue(tbcol.Attribute.Name, out var tbstructcol) ||
|
||||||
@ -217,7 +217,7 @@ namespace FreeSql.Sqlite
|
|||||||
//创建表
|
//创建表
|
||||||
isIndent = false;
|
isIndent = false;
|
||||||
sb.Append("CREATE TABLE IF NOT EXISTS ").Append(tmptablename).Append(" ( ");
|
sb.Append("CREATE TABLE IF NOT EXISTS ").Append(tmptablename).Append(" ( ");
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
{
|
{
|
||||||
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
|
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
|
||||||
sb.Append(tbcol.Attribute.DbType);
|
sb.Append(tbcol.Attribute.DbType);
|
||||||
@ -243,10 +243,10 @@ namespace FreeSql.Sqlite
|
|||||||
sb.Remove(sb.Length - 1, 1);
|
sb.Remove(sb.Length - 1, 1);
|
||||||
sb.Append("\r\n) \r\n;\r\n");
|
sb.Append("\r\n) \r\n;\r\n");
|
||||||
sb.Append("INSERT INTO ").Append(tmptablename).Append(" (");
|
sb.Append("INSERT INTO ").Append(tmptablename).Append(" (");
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
sb.Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(", ");
|
sb.Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(", ");
|
||||||
sb.Remove(sb.Length - 2, 2).Append(")\r\nSELECT ");
|
sb.Remove(sb.Length - 2, 2).Append(")\r\nSELECT ");
|
||||||
foreach (var tbcol in tb.Columns.Values)
|
foreach (var tbcol in tb.ColumnsByPosition)
|
||||||
{
|
{
|
||||||
var insertvalue = "NULL";
|
var insertvalue = "NULL";
|
||||||
if (tbstruct.TryGetValue(tbcol.Attribute.Name, out var tbstructcol) ||
|
if (tbstruct.TryGetValue(tbcol.Attribute.Name, out var tbstructcol) ||
|
||||||
|
Loading…
x
Reference in New Issue
Block a user