Skip to content

Commit

Permalink
Merge pull request #39 from Soar360/pg_name_strategy
Browse files Browse the repository at this point in the history
Pg name strategy
  • Loading branch information
nnhy authored Sep 19, 2024
2 parents 9104fed + fe02796 commit 4dbc09c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 43 deletions.
73 changes: 41 additions & 32 deletions XCode/DataAccessLayer/Common/DbBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ public virtual String ServerVersion

/// <summary>性能追踪器</summary>
public ITracer? Tracer { get; set; }

/// <summary>数据库引擎默认的反向工程表名、字段名大小写设置</summary>
public virtual NameFormats DefaultNameFormat => NameFormats.None;
#endregion

#region 方法
Expand Down Expand Up @@ -704,23 +707,26 @@ public virtual String FormatName(IDataTable table, Boolean formatKeyword)
if (!pf.IsNullOrEmpty()) name = pf + name;

// 名称格式化,只有表名跟名称相同时才处理。否则认为用户指定了表名
switch (NameFormat)
if (table.TableName == table.Name)
{
case NameFormats.Upper:
name = name.ToUpper();
break;
case NameFormats.Lower:
name = name.ToLower();
break;
case NameFormats.Underline:
if (table.TableName == table.Name)
name = ChangeUnderline(name).ToLower();
else
name = name.ToLower();
break;
case NameFormats.Default:
default:
break;
var fmt = this.DefaultNameFormat;
if (this.NameFormat != NameFormats.Default) fmt = this.NameFormat;
switch (fmt)
{
case NameFormats.Upper:
name = name.ToUpperInvariant();
break;
case NameFormats.Lower:
name = name.ToLowerInvariant();
break;
case NameFormats.Underline:
name = ChangeUnderline(name).ToLowerInvariant();
break;
case NameFormats.Default:
case NameFormats.None:
default:
break;
}
}

return formatKeyword ? FormatName(name) : name;
Expand All @@ -736,23 +742,26 @@ public virtual String FormatName(IDataColumn column)
var name = column.ColumnName;

// 名称格式化,只有字段名名跟名称相同时才处理。否则认为用户指定了字段名
switch (NameFormat)
if (column.ColumnName == column.Name)
{
case NameFormats.Upper:
name = name.ToUpper();
break;
case NameFormats.Lower:
name = name.ToLower();
break;
case NameFormats.Underline:
if (column.ColumnName == column.Name)
name = ChangeUnderline(name).ToLower();
else
name = name.ToLower();
break;
case NameFormats.Default:
default:
break;
var fmt = this.DefaultNameFormat;
if (this.NameFormat != NameFormats.Default) fmt = this.NameFormat;
switch (fmt)
{
case NameFormats.Upper:
name = name.ToUpperInvariant();
break;
case NameFormats.Lower:
name = name.ToLowerInvariant();
break;
case NameFormats.Underline:
name = ChangeUnderline(name).ToLowerInvariant();
break;
case NameFormats.Default:
case NameFormats.None:
default:
break;
}
}

return FormatName(name);
Expand Down
5 changes: 3 additions & 2 deletions XCode/DataAccessLayer/Common/IDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ public enum Migration
/// <summary>名称格式化</summary>
public enum NameFormats
{
/// <summary>原样</summary>
/// <summary>默认</summary>
Default = 0,

/// <summary>保持原样</summary>
None,
/// <summary>全大写</summary>
Upper,

Expand Down
13 changes: 5 additions & 8 deletions XCode/DataAccessLayer/Database/PostgreSQL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,20 +208,17 @@ private string ValueToSQL(Type? type, bool isNullable, object? value)
/// <returns></returns>
public override String StringConcat(String left, String right) => (!String.IsNullOrEmpty(left) ? left : "''") + "||" + (!String.IsNullOrEmpty(right) ? right : "''");

/// <summary>
/// 格式化数据库名称,表名称,字段名称 增加双引号("")
/// PGSQL 默认情况下创建库表时自动转为小写,增加引号强制区分大小写
/// 以解决数据库创建查询时大小写问题
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
/// <inheritdoc/>
public override String FormatName(String name)
{
name = base.FormatName(name);

if (name.StartsWith("\"") || name.EndsWith("\"")) return name;

return $"\"{name}\"";
//如果包含大写字符,就加上引号
if (name.Any(char.IsUpper)) return $"\"{name}\"";

return name;
}

/// <inheritdoc/>
Expand Down
2 changes: 1 addition & 1 deletion XCode/Setting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class XCodeSetting : Config<XCodeSetting>
public Migration Migration { get; set; } = Migration.On;

/// <summary></summary>
[Description("表名称、字段名大小写格式。Default 根据模型生成;Upper 全大写;Lower 全小写;Underline下划线")]
[Description("表名称、字段名大小写格式。Default 默认(由数据库决定);None 与模型保持一致;Upper 全大写;Lower 全小写;Underline下划线")]
public NameFormats NameFormat { get; set; } = NameFormats.Default;

/// <summary>全表查行数下限。在获取数据表行数时,先根据索引表进行快速统计。如果快速统计的结果大于该值,则使用快速统计的结果。反之则进行 count(*) 操作获取精确统计。默认1000万</summary>
Expand Down

0 comments on commit 4dbc09c

Please sign in to comment.