using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace CodeBuilderApi
{
///
/// 数据表操作类
///
///
public class OperationDataHelper
{
#region 方法 -> 类型匹配
///
/// 类型匹配
///
/// 类型
/// 类型名称
///
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 获取通用表数据查询条件
///
/// 获取通用表数据查询条件
///
/// 实体类
/// 查询方式:0【模糊查询】,1【精确查询】
/// 查询表定义名称,如A.
/// 排除查询的字段
///
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:修改)
///
/// 获取数据表执行语句
///
/// 泛型数据对象
///
/// 执行类型
/// 0:新增;1:修改
///
///
/// 数据库表名称
/// 主键值名称
/// 表序列名称
/// 日期类型的字段执行语句
/// 排除执行的字段
/// where查询语句
public static string GetTableExcuteSQL(T t, int ExcuteType, string tableName, string keyField,
string seqName, Dictionary dateFieldList, List 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
}
}