///*************************************************************************/
///*
///* 文件名 :GenerateSqlCmdByObjectClass.cs
///*
///* 程序说明 : 由对象实体类模型创建SQL命令生成器
///* 原创作者 :孙中吕
///*
///* Copyright 2006-2021 C/S框架网 www.csframework.com
///*
///**************************************************************************/
using System;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Reflection;
namespace CSFrameworkV5.Core.CodeGenerator
{
///
/// 由对象实体类模型创建SQL命令生成器
///
public class GenerateSqlCmdByObjectClass : GenerateSqlCmdBase
{
///
/// 构造器
///
/// 表结构字段定义类
public GenerateSqlCmdByObjectClass(Type ORM_Model, DataTable ORM_Table,
ICommandFactory factory)
: base(factory)
{
GenerateSQL(ORM_Model, ORM_Table);
}
///
/// 构造器
///
/// 表结构字段定义类
public GenerateSqlCmdByObjectClass(Type ORM_Model, DataTable ORM_Table,
ICommandFactory factory, GeneratedSQLEventHandle eventHandle)
: base(factory)
{
GenerateSQL(ORM_Model, ORM_Table);
//触发事件
if (eventHandle != null)
eventHandle(_sqlInsert, _sqlDelete, _sqlUpdate);
}
///
/// 构造器
///
/// ORM类的类型,如:typeof(tb_PO),typeof(tb_SO)
public void GenerateSQL(Type ORM_Model, DataTable ORM_Table)
{
object[] attrClass;
//查找ORM的属性定义
ORM_TableAttribute classAttribute = null;
attrClass =
ORM_Model.GetCustomAttributes(typeof(ORM_TableAttribute),
false);
if (attrClass != null && attrClass.Length == 0)
throw new Exception("ORM_ObjectClassAttribute未定义!");
classAttribute = attrClass[0] as ORM_TableAttribute;
//查找ORM控制并发的属性定义
ORM_ConcurrentAttribute concurrentAttribute = null;
attrClass =
ORM_Model.GetCustomAttributes(typeof(ORM_ConcurrentAttribute),
false);
if (attrClass != null && attrClass.Length > 0 &&
classAttribute.IsSummaryTable) //仅主表控制并发
{
concurrentAttribute = attrClass[0] as ORM_ConcurrentAttribute;
_ConcurrentAttribute = concurrentAttribute;
}
//是否主表属性
_IsSummary = classAttribute.IsSummaryTable;
_PrimaryFieldName =
classAttribute.PrimaryKey; //主键,复合主键盘用";"分开
//生成一个新的对象
var obj = ORM_Model.Assembly.CreateInstance(ORM_Model.FullName);
//获取类中的所有属性
var infosSelf = ORM_Model.GetProperties();
var infosBase = ORM_Model.BaseType.GetProperties();
PropertyInfo[] infos = null;
if (infosBase.Length == 0)
{
infos = infosSelf;
}
else
{
var iLength = infosBase.Length;
Array.Resize(ref infosBase,
infosBase.Length + infosSelf.Length); //相加两数组
infosSelf.CopyTo(infosBase, iLength);
infos = infosBase;
}
var fieldArr = new ArrayList();
_cmdInsert = _Factory.CreateCommand();
_cmdUpdate = _Factory.CreateCommand();
_cmdDelete = _Factory.CreateCommand();
var paramSymbol = _Factory.ParamSymboName;
DbParameter p;
foreach (var info in infos)
{
//取字段属性定义
var attrField =
info.GetCustomAttributes(typeof(ORM_FieldAttribute), false);
if (attrField == null || attrField.Length == 0) continue;
var fieldName = info.Name;
var fieldAttr = attrField[0] as ORM_FieldAttribute;
if (ORM_Table.Columns[fieldName] == null) continue;
//构造SQL命令带参数的SQL语句
if (fieldAttr.IsAddOrUpdate)
{
//构造带参数的Insert命令的SQL语句
if (!_cmdInsert.Parameters.Contains(paramSymbol +
fieldName))
{
p = _Factory.CreateParameter(paramSymbol + fieldName,
fieldAttr.Type, fieldAttr.Size, fieldName);
_cmdInsert.Parameters.Add(p);
}
//构造带参数的Update命令的SQL语句
if (!_cmdUpdate.Parameters.Contains(paramSymbol +
fieldName))
{
p = _Factory.CreateParameter(paramSymbol + fieldName,
fieldAttr.Type, fieldAttr.Size, fieldName);
_cmdUpdate.Parameters.Add(p);
}
var colProper = new ColumnProperty(fieldName,
fieldAttr.IsPrimaryKey ? true : false);
fieldArr.Add(colProper);
}
//生成主键的参数
if (fieldAttr.IsPrimaryKey)
{
if (!_cmdUpdate.Parameters.Contains(paramSymbol +
fieldName))
{
p = _Factory.CreateParameter(paramSymbol + fieldName,
fieldAttr.Type, fieldAttr.Size, fieldName);
_cmdUpdate.Parameters.Add(p);
}
if (!_cmdDelete.Parameters.Contains(paramSymbol +
fieldName))
{
p = _Factory.CreateParameter(paramSymbol + fieldName,
fieldAttr.Type, fieldAttr.Size, fieldName);
_cmdDelete.Parameters.Add(p);
}
}
//控制并发的字段名
if (concurrentAttribute != null &&
concurrentAttribute.TimestampFieldName == fieldName)
if (!_cmdUpdate.Parameters.Contains(paramSymbol +
fieldName))
{
p = _Factory.CreateParameter(paramSymbol + fieldName,
fieldAttr.Type, fieldAttr.Size, fieldName);
_cmdUpdate.Parameters.Add(p);
}
//流水号字段名
if (fieldAttr.IsDocFieldName) _DocNoFieldName = fieldName;
//外键字段名
if (fieldAttr.IsForeignKey)
_ForeignFieldName =
(string.IsNullOrEmpty(_ForeignFieldName)
? ""
: _ForeignFieldName + ";") + fieldName; //组合外键
}
_sqlInsert = GenerateInsertSql(classAttribute.TableName,
classAttribute.PrimaryKey, fieldArr); //新增的SQL
_sqlUpdate = GenerateUpdateSql(classAttribute.TableName,
classAttribute.PrimaryKey, fieldArr); //修改的SQL
_sqlDelete = GenerateDeleteSql(classAttribute.TableName,
classAttribute.PrimaryKey); //删除的SQL
}
}
}