Skip to content

Commit

Permalink
Merge pull request #37 from NewLifeX/pgsql_array
Browse files Browse the repository at this point in the history
Pgsql array
  • Loading branch information
Soar360 authored Sep 18, 2024
2 parents ff19e1d + 758899a commit c1a9c59
Show file tree
Hide file tree
Showing 9 changed files with 506 additions and 70 deletions.
16 changes: 16 additions & 0 deletions XCode/Attributes/BindColumnAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public sealed class BindColumnAttribute : Attribute

/// <summary>数据规模。time表示这是大数据单表的数据时间字段,timeShard:yyMMdd表示这是大数据多表的分表字段</summary>
public String? DataScale { get; set; }

/// <summary>是否数组</summary>
public Boolean IsArray { get; set; }
#endregion

#region 构造
Expand All @@ -65,6 +68,19 @@ public BindColumnAttribute(String name, String description, String rawType)
Description = description;
RawType = rawType;
}

/// <summary>构造函数</summary>
/// <param name="name">名称</param>
/// <param name="description"></param>
/// <param name="rawType"></param>
/// <param name="isArray">是否数组</param>
public BindColumnAttribute(String name, String description, String rawType, bool isArray)
{
Name = name;
Description = description;
RawType = rawType;
IsArray = isArray;
}
#endregion

#region 方法
Expand Down
66 changes: 27 additions & 39 deletions XCode/Code/EntityBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ protected override void BuildItem(IDataColumn column)
var type = dc.Properties["Type"];
if (type.IsNullOrEmpty()) type = dc.DataType?.Name;
if (type == "String" && Option.Nullable && column.Nullable) type = "String?";
if (column.IsArray) type = $"{type}[]";

// 字段
if (type == "String" && Option.Nullable)
Expand Down Expand Up @@ -723,6 +724,9 @@ protected override void BuildItem(IDataColumn column)
// 数据规模
if (!dc.DataScale.IsNullOrEmpty()) sb.AppendFormat(", DataScale = \"{0}\"", dc.DataScale);

// 是否数组
if (dc.IsArray) sb.Append(", IsArray = true");

////添加自定义控件默认值
//if (!dc.ItemDefaultValue.IsNullOrEmpty()) sb.AppendFormat(", ItemDefaultValue = \"{0}\"", dc.ItemDefaultValue);

Expand Down Expand Up @@ -771,7 +775,7 @@ protected override void BuildIndexItems()
{
WriteLine("switch (name)");
WriteLine("{");
var conv = typeof(Convert);
var conv = typeof(ValidHelper);
foreach (var column in Table.Columns)
{
// 跳过排除项
Expand All @@ -783,50 +787,34 @@ protected override void BuildIndexItems()

if (!type.IsNullOrEmpty())
{
if (!type.Contains(".") && conv.GetMethod("To" + type, [typeof(Object)]) != null)
var method = "To";
var typePara = string.Empty;
if (type.Contains("."))
{
switch (type)
//说明使用了自定义类型,比如:枚举
typePara = type;
if (column.DataType?.IsInt() == true)
{
case "Int32":
WriteLine("case \"{0}\": _{0} = value.ToInt(); break;", column.Name);
break;

case "Int64":
WriteLine("case \"{0}\": _{0} = value.ToLong(); break;", column.Name);
break;

case "Double":
WriteLine("case \"{0}\": _{0} = value.ToDouble(); break;", column.Name);
break;

case "Boolean":
WriteLine("case \"{0}\": _{0} = value.ToBoolean(); break;", column.Name);
break;

case "DateTime":
WriteLine("case \"{0}\": _{0} = value.ToDateTime(); break;", column.Name);
break;

default:
WriteLine("case \"{0}\": _{0} = Convert.To{1}(value); break;", column.Name, type);
break;
method += "Enum";
}
else
{
method += "Object";
}
}
else
{
try
{
// 特殊支持枚举
if (column.DataType.IsInt())
WriteLine("case \"{0}\": _{0} = ({1})value.ToInt(); break;", column.Name, type);
else
WriteLine("case \"{0}\": _{0} = ({1})value; break;", column.Name, type);
}
catch (Exception ex)
{
XTrace.WriteException(ex);
WriteLine("case \"{0}\": _{0} = ({1})value; break;", column.Name, type);
}
method += type;
}
if (column.IsArray) method += "Array";
if (conv.GetMethod(method, [typeof(object)]) != null)
{
if (!string.IsNullOrWhiteSpace(typePara)) method += $"<{typePara}>";
WriteLine("case \"{0}\": _{0} = ValidHelper.{1}(value); break;", column.Name, method);
}
else
{
WriteLine("case \"{0}\": _{0} = ({1})value; break;", column.Name, type);
}
}
}
Expand Down
40 changes: 40 additions & 0 deletions XCode/Common/ValidHelper.Array.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Collections;

namespace XCode;

/// <remarks>
/// Array 部分的实现,先尽量保持简洁。
/// 后续更强大的转换,再补充。
/// </remarks>
public partial class ValidHelper
{
private static T[]? ToArray<T>(Object? value, Func<object?, T> converter)
{
if (value is T[] arr) return arr;
if (value is null || Convert.IsDBNull(value)) return default;
if (value is IEnumerable<T> list) return list.ToArray();
if (value is T v) return new T[] { v };
if (value is IEnumerable)
{
var ret = new List<T>();
foreach (var item in (IEnumerable)value)
{
ret.Add(converter.Invoke(item));
}
return ret.ToArray();
}
return default;
}
public static Int32[]? ToInt32Array(Object? value) => ToArray(value, ToInt32);
public static Int64[]? ToInt64Array(Object? value) => ToArray(value, ToInt64);
public static Double[]? ToDoubleArray(Object? value) => ToArray(value, ToDouble);
public static Boolean[]? ToBooleanArray(Object? value) => ToArray(value, ToBoolean);
public static DateTime[]? ToDateTimeArray(Object? value) => ToArray(value, ToDateTime);
public static String[]? ToStringArray(Object? value) => ToArray(value, ToString);
public static Byte[]? ToByteArray(Object? value) => ToArray(value, ToByte);
public static Decimal[]? ToDecimalArray(Object? value) => ToArray(value, ToDecimal);
public static Int16[]? ToInt16Array(Object? value) => ToArray(value, ToInt16);
public static UInt64[]? ToUInt64Array(Object? value) => ToArray(value, ToUInt64);
public static T[]? ToEnumArray<T>(Object? value) where T : struct => ToArray(value, ToEnum<T>);
public static T[]? ToObjectArray<T>(Object? value) where T : class => ToArray(value, ToObject<T>);
}
116 changes: 116 additions & 0 deletions XCode/Common/ValidHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XCode;

/// <summary>用户数据类型转换</summary>
public static partial class ValidHelper
{
/// <summary>转为整数,转换失败时返回默认值。支持字符串、全角、字节数组(小端)、时间(Unix秒不转UTC)</summary>
/// <remarks>Int16/UInt32/Int64等,可以先转为最常用的Int32后再二次处理</remarks>
/// <param name="value">待转换对象</param>
public static Int32 ToInt32(object? value)
{
return value.ToInt();
}

/// <summary>转为长整数,转换失败时返回默认值。支持字符串、全角、字节数组(小端)、时间(Unix毫秒不转UTC)</summary>
/// <param name="value">待转换对象</param>
public static Int64 ToInt64(object? value)
{
return value.ToLong();
}

/// <summary>转为浮点数,转换失败时返回默认值。支持字符串、全角、字节数组(小端)</summary>
/// <remarks>Single可以先转为最常用的Double后再二次处理</remarks>
/// <param name="value">待转换对象</param>
public static Double ToDouble(object? value)
{
return value.ToDouble();
}

/// <summary>转为布尔型,转换失败时返回默认值。支持大小写True/False、0和非零</summary>
/// <param name="value">待转换对象</param>
public static Boolean ToBoolean(object? value)
{
return value.ToBoolean();
}

/// <summary>转为时间日期,转换失败时返回最小时间。支持字符串、整数(Unix秒不考虑UTC转本地)</summary>
/// <remarks>
/// 整数转时间日期时,取1970-01-01加上指定秒数,不考虑UTC时间和本地时间。
/// 长整数转时间日期时,取1970-01-01加上指定毫秒数,不考虑UTC时间和本地时间。
/// 在网络中传输时间日期时,特别是物联网设备到云平台的通信,一般取客户端本地UTC时间,转为长整型传输,服务端再转为本地时间。
/// 因为设备和服务端可能不在同一时区,甚至多个设备也没有处于同一个时区。
/// </remarks>
/// <param name="value">待转换对象</param>
public static DateTime ToDateTime(object? value)
{
return value.ToDateTime();
}

/// <summary>
/// 将指定的值转换为其等效的字符串表示形式。
/// </summary>
public static String ToString(object? value)
{
return Convert.ToString(value);
}

/// <summary>
/// Convert.ToByte
/// </summary>
public static Byte ToByte(object? value)
{
return Convert.ToByte(value);
}

/// <summary>
/// Convert.ToDecimal
/// </summary>
public static Decimal ToDecimal(object? value)
{
return Convert.ToDecimal(value);
}

/// <summary>
/// Convert.ToInt16
/// </summary>
public static Int16 ToInt16(object? value)
{
return Convert.ToInt16(value);
}

/// <summary>
/// Convert.ToUInt64
/// </summary>
public static UInt64 ToUInt64(object? value)
{
return Convert.ToUInt64(value);
}

/// <summary>
/// 转换为枚举
/// </summary>
public static T ToEnum<T>(object? value) where T : struct
{
if (value is T t) return t;
if (value is null || Convert.IsDBNull(value)) return default;
if (typeof(T).IsEnum)
{
if (value is string str) return (T)Enum.Parse(typeof(T), str, true);
}
return (T)value;
}

public static T? ToObject<T>(object? value) where T : class
{
if (value is T t) return t;
if (value is null || Convert.IsDBNull(value)) return default;
//这里怎么实现呢?
return default;
}
}
1 change: 1 addition & 0 deletions XCode/Configuration/FieldItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ public void Fill(IDataColumn field)
dc.DefaultValue = col.DefaultValue;
//dc.ItemDefaultValue = col.ItemDefaultValue;
dc.DataScale = col.DataScale;
dc.IsArray = col.IsArray;
}

// 特别处理,兼容旧版本
Expand Down
Loading

0 comments on commit c1a9c59

Please sign in to comment.