208 lines
9.6 KiB
C#
208 lines
9.6 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Reflection;
|
||
|
||
namespace CodeBuilderApi
|
||
{
|
||
/// <summary>
|
||
/// 数据表操作类
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
public class OperationDataHelper<T>
|
||
{
|
||
#region 方法 -> 类型匹配
|
||
/// <summary>
|
||
/// 类型匹配
|
||
/// </summary>
|
||
/// <param name="type">类型</param>
|
||
/// <param name="typeName">类型名称</param>
|
||
/// <returns></returns>
|
||
public static bool IsType(Type type, string typeName)
|
||
{
|
||
if (type.ToString() == typeName)
|
||
return true;
|
||
if (type.ToString() == "System.Object")
|
||
return false;
|
||
return IsType(type.BaseType, typeName);
|
||
}
|
||
#endregion
|
||
|
||
#region 获取通用表数据查询条件
|
||
/// <summary>
|
||
/// 获取通用表数据查询条件
|
||
/// </summary>
|
||
/// <param name="t">实体类</param>
|
||
/// <param name="QueryType">查询方式:0【模糊查询】,1【精确查询】</param>
|
||
/// <param name="OtherName">查询表定义名称,如A.</param>
|
||
/// <param name="ExcludeName">排除查询的字段</param>
|
||
/// <returns></returns>
|
||
public static string GetWhereSQL(T t, int? QueryType, string OtherName = "", params string[] ExcludeName)
|
||
{
|
||
string WhereSQL = "";
|
||
|
||
var pros = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
|
||
foreach (var item in pros)
|
||
{
|
||
object value = item.GetValue(t, null);
|
||
//如果排除字段中包含当前参数,则执行下一个参数查询
|
||
if (ExcludeName != null && ExcludeName.Contains(item.Name))
|
||
{
|
||
continue;
|
||
}
|
||
//查询字符串类型参数
|
||
if (IsType(item.PropertyType, "System.String") || item.PropertyType == typeof(String))
|
||
{
|
||
if (value != null && !string.IsNullOrEmpty(value.ToString()))
|
||
{
|
||
WhereSQL += (string.IsNullOrEmpty(WhereSQL) ? "" : " AND ") + OtherName + item.Name +
|
||
(QueryType == 0 ? " like '%" + value + "%'" : " = '" + value + "'");
|
||
}
|
||
continue;
|
||
}
|
||
//查询数字类型或bool型参数
|
||
if (IsType(item.PropertyType, "System.Nullable`1[System.Int16]") ||
|
||
IsType(item.PropertyType, "System.Nullable`1[System.Int32]") ||
|
||
IsType(item.PropertyType, "System.Nullable`1[System.Int64]") ||
|
||
IsType(item.PropertyType, "System.Nullable`1[System.Double]") ||
|
||
IsType(item.PropertyType, "System.Nullable`1[System.Decimal]") ||
|
||
IsType(item.PropertyType, "System.Nullable`1[System.Boolean]") ||
|
||
item.PropertyType == typeof(Int16) || item.PropertyType == typeof(Int32) || item.PropertyType == typeof(Int64) ||
|
||
item.PropertyType == typeof(Double) || item.PropertyType == typeof(Decimal) || item.PropertyType == typeof(Boolean))
|
||
{
|
||
if (value != null)
|
||
{
|
||
WhereSQL += (string.IsNullOrEmpty(WhereSQL) ? "" : " AND ") + OtherName + item.Name + " = " + value;
|
||
}
|
||
continue;
|
||
}
|
||
//查询日期类型参数
|
||
if (IsType(item.PropertyType, "System.Nullable`1[System.DateTime]") ||
|
||
item.PropertyType == typeof(DateTime))
|
||
{
|
||
if (value != null)
|
||
{
|
||
WhereSQL += (string.IsNullOrEmpty(WhereSQL) ? "" : " AND ") + OtherName + item.Name + " = " +
|
||
string.Format("TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", value);
|
||
}
|
||
continue;
|
||
}
|
||
}
|
||
|
||
return WhereSQL;
|
||
}
|
||
#endregion
|
||
|
||
#region 获取数据表执行语句(0:新增;1:修改)
|
||
/// <summary>
|
||
/// 获取数据表执行语句
|
||
/// </summary>
|
||
/// <param name="t">泛型数据对象</param>
|
||
/// <param name="ExcuteType">
|
||
/// 执行类型<br/>
|
||
/// 0:新增;1:修改
|
||
/// </param>
|
||
/// <returns></returns>
|
||
/// <param name="tableName">数据库表名称</param>
|
||
/// <param name="keyField">主键值名称</param>
|
||
/// <param name="seqName">表序列名称</param>
|
||
/// <param name="dateFieldList">日期类型的字段执行语句</param>
|
||
/// <param name="excludeField">排除执行的字段</param>
|
||
/// <param name="WhereSQL">where查询语句</param>
|
||
public static string GetTableExcuteSQL(T t, int ExcuteType, string tableName, string keyField,
|
||
string seqName, Dictionary<string, string> dateFieldList, List<string> excludeField, string WhereSQL = "")
|
||
{
|
||
string SQLString, InsertField = "", ExcuteSQL = "";
|
||
|
||
var pros = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
|
||
foreach (var item in pros)
|
||
{
|
||
//排除掉用于查询的字段,这些字段不属于表的字段
|
||
if (excludeField.Contains(item.Name))
|
||
{
|
||
continue;
|
||
}
|
||
//存储插入表中的字段名称
|
||
InsertField += (InsertField == "" ? "" : ",") + item.Name;
|
||
//获取当前对象中字段的值
|
||
object value = item.GetValue(t, null);
|
||
//判断是不是主键值
|
||
if (item.Name == keyField)
|
||
{
|
||
//只有插入语句才会更新主键值
|
||
if (ExcuteType == 0)
|
||
{
|
||
if (value != null)
|
||
{
|
||
//主键值存在则以对象中的值为准
|
||
ExcuteSQL += (string.IsNullOrEmpty(ExcuteSQL) ? "" : ",\r\n") + value;
|
||
}
|
||
else
|
||
{
|
||
//否则执行表的序列,获取主键值
|
||
ExcuteSQL += (string.IsNullOrEmpty(ExcuteSQL) ? "" : ",\r\n") + seqName + ".NEXTVAL";
|
||
}
|
||
}
|
||
continue;
|
||
}
|
||
else if (dateFieldList.ContainsKey(item.Name))
|
||
{
|
||
//判断是不是日期字段,如果是就按照日期格式的字段去执行
|
||
ExcuteSQL += (string.IsNullOrEmpty(ExcuteSQL) ? "" : ",\r\n") +
|
||
(ExcuteType == 1 ? item.Name + " = " : "") + dateFieldList[item.Name];
|
||
continue;
|
||
}
|
||
//查询字符串类型参数
|
||
if (IsType(item.PropertyType, "System.String") || item.PropertyType == typeof(String))
|
||
{
|
||
ExcuteSQL += (string.IsNullOrEmpty(ExcuteSQL) ? "" : ",\r\n") +
|
||
(ExcuteType == 1 ? item.Name + " = " : "") +
|
||
(value == null ? "NULL" : "'" + value + "'");
|
||
continue;
|
||
}
|
||
//查询数字类型或bool型参数
|
||
if (IsType(item.PropertyType, "System.Nullable`1[System.Int16]") ||
|
||
IsType(item.PropertyType, "System.Nullable`1[System.Int32]") ||
|
||
IsType(item.PropertyType, "System.Nullable`1[System.Int64]") ||
|
||
IsType(item.PropertyType, "System.Nullable`1[System.Double]") ||
|
||
IsType(item.PropertyType, "System.Nullable`1[System.Decimal]") ||
|
||
IsType(item.PropertyType, "System.Nullable`1[System.Boolean]") ||
|
||
item.PropertyType == typeof(Int16) || item.PropertyType == typeof(Int32) || item.PropertyType == typeof(Int64) ||
|
||
item.PropertyType == typeof(Double) || item.PropertyType == typeof(Decimal) || item.PropertyType == typeof(Boolean))
|
||
{
|
||
|
||
ExcuteSQL += (string.IsNullOrEmpty(ExcuteSQL) ? "" : ",\r\n") +
|
||
(ExcuteType == 1 ? item.Name + " = " : "") +
|
||
(value == null ? "NULL" : value.ToString());
|
||
continue;
|
||
}
|
||
//查询日期类型参数
|
||
if (IsType(item.PropertyType, "System.Nullable`1[System.DateTime]") ||
|
||
item.PropertyType == typeof(DateTime))
|
||
{
|
||
ExcuteSQL += (string.IsNullOrEmpty(ExcuteSQL) ? "" : ",\r\n") +
|
||
(ExcuteType == 1 ? item.Name + " = " : "") +
|
||
(value == null ? "NULL" : string.Format("TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", value));
|
||
continue;
|
||
}
|
||
}
|
||
|
||
switch (ExcuteType)
|
||
{
|
||
case 0://执行插入语句
|
||
SQLString = string.Format("INSERT INTO {0} ({1}) VALUES ({2})", tableName, InsertField, ExcuteSQL);
|
||
break;
|
||
case 1://执行更新语句
|
||
SQLString = string.Format("UPDATE {0} SET {1}{2}", tableName, ExcuteSQL, WhereSQL);
|
||
break;
|
||
default:
|
||
SQLString = "";
|
||
break;
|
||
}
|
||
|
||
return SQLString;
|
||
}
|
||
#endregion
|
||
}
|
||
}
|