示例#1
0
        private List <String> prepareFindByKeySql(ModelMappingInfo mappingInfo, string[] properties, ref List <string> whereString, ref List <SqlParamInfo> sqlParams)
        {
            List <string> selectString = new List <string>();

            whereString.Clear();
            sqlParams.Clear();

            bool existsPros = properties != null && properties.Length > 0;

            foreach (FieldPropertyInfo info in mappingInfo.FieldPropertys.Values)
            {
                if (mappingInfo.PrimaryKeys.ContainsKey(info.FieldName))
                {
                    whereString.Add(info.FieldName + "=" + ParameterPrefix + info.FieldName);
                    sqlParams.Add(new SqlParamInfo(info.FieldName, DbShare.Instance.SystemTypeNameToDbType(info.DeclaringType.FullName)));
                    continue;
                }
                if (existsPros && Array.IndexOf <string>(properties, info.FieldName) < 0)
                {
                    continue;
                }
                selectString.Add(info.FieldName);
            }
            return(selectString);
        }
        private static readonly string PARAM_CURRENT_VALUE      = "CurrentValue";      //当前值
        //private static readonly object IDENTITY_CREATE_SYN_LOCK = new object();

        /// <summary>
        /// 设置实体的键值。
        /// </summary>
        /// <param name="entity"></param>
        public void FillEntityIdentity(object entity)
        {
            Type             entityType  = entity.GetType();
            ModelMappingInfo mappingInfo = AttMappingManager.Instance.GetModelMappingInfo(entityType);

            if (string.IsNullOrEmpty(mappingInfo.MapTable))
            {
                throw new MB.Util.APPException(string.Format("数据实体{0} 配置有误,没有指定映射到的表。", entityType.FullName), MB.Util.APPMessageType.SysErrInfo);
            }

            if (mappingInfo.PrimaryKeys == null || mappingInfo.PrimaryKeys.Count != 1)
            {
                throw new MB.Util.APPException(string.Format("数据实体{0} 配置有误,没有指定主键或者不是单一主键配置。", entityType.FullName), MB.Util.APPMessageType.SysErrInfo);
            }

            FieldPropertyInfo keyInfo = mappingInfo.PrimaryKeys.Values.First <FieldPropertyInfo>();
            //判断是否存在主键
            bool exists = MB.Util.MyReflection.Instance.CheckObjectExistsProperty(entity, keyInfo.FieldName);

            if (!exists)
            {
                return;
            }
            object oldKey = MB.Util.MyReflection.Instance.InvokePropertyForGet(entity, keyInfo.FieldName);

            //判断是否已经存在键值。
            if (oldKey != null && (int)oldKey > 0)
            {
                return;
            }

            int id = GetEntityIdentity(mappingInfo.MapTable);

            MB.Util.MyReflection.Instance.InvokePropertyForSet(entity, keyInfo.FieldName, id);
        }
示例#3
0
        //准备生成SQL Update 语句处理。
        private List <String> prepareUpdateSql(ModelMappingInfo mappingInfo, string[] properties, ref List <string> whereString, ref List <SqlParamInfo> sqlParams)
        {
            List <string> setString = new List <string>();

            whereString.Clear();
            sqlParams.Clear();

            bool existsPros = properties != null && properties.Length > 0;

            foreach (FieldPropertyInfo info in mappingInfo.FieldPropertys.Values)
            {
                if (mappingInfo.PrimaryKeys.ContainsKey(info.FieldName))
                {
                    whereString.Add(info.FieldName + "=" + ParameterPrefix + info.FieldName);
                }
                else
                {
                    if (!existsPros || Array.IndexOf <string>(properties, info.FieldName) >= 0)
                    {
                        setString.Add(info.FieldName + "=" + ParameterPrefix + info.FieldName);
                    }
                }
                if (mappingInfo.PrimaryKeys.ContainsKey(info.FieldName) || (!existsPros || Array.IndexOf <string>(properties, info.FieldName) >= 0))
                {
                    sqlParams.Add(new SqlParamInfo(info.FieldName, info.DbType));
                }
            }
            return(setString);
        }
示例#4
0
        /// <summary>
        /// 获取执行更新实体最后修改时间的DbCommand。
        /// </summary>
        /// <param name="db"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
        public DbCommand GetSaveLastModifiedDateCommand(Database db, object entity)
        {
            bool exists = MB.Util.MyReflection.Instance.CheckObjectExistsProperty(entity, LAST_MODIFIED_DATE_PROPERTY);

            if (!exists)
            {
                return(null);
            }

            Type             entityType  = entity.GetType();
            ModelMappingInfo mappingInfo = AttMappingManager.Instance.GetModelMappingInfo(entityType);

            if (string.IsNullOrEmpty(mappingInfo.MapTable))
            {
                throw new MB.Util.APPException(string.Format("数据实体{0} 配置有误,没有指定映射到的表。", entityType.FullName), MB.Util.APPMessageType.SysErrInfo);
            }

            if (mappingInfo.PrimaryKeys == null || mappingInfo.PrimaryKeys.Count != 1)
            {
                throw new MB.Util.APPException(string.Format("数据实体{0} 配置有误,没有指定主键或者不是单一主键配置。", entityType.FullName), MB.Util.APPMessageType.SysErrInfo);
            }

            FieldPropertyInfo keyInfo = mappingInfo.PrimaryKeys.Values.First <FieldPropertyInfo>();

            var    dbType  = MB.Orm.Persistence.DatabaseHelper.GetDatabaseType(db);
            string sysDate = string.Empty;

            switch (dbType)
            {
            case MB.Orm.Enums.DatabaseType.Oracle:
                sysDate = "SysDate";
                break;

            case MB.Orm.Enums.DatabaseType.Sqlite:
                sysDate = "datetime(CURRENT_TIMESTAMP,'localtime')";
                break;

            case MB.Orm.Enums.DatabaseType.MSSQLServer:
                sysDate = "GetDate()";
                break;

            case Enums.DatabaseType.MySql:
                sysDate = "Now()";
                break;

            default:
                throw new MB.Util.APPException(string.Format("在获取数据库系统时间时,数据库类型{0} 还没有进行相应的处理", dbType.ToString()), Util.APPMessageType.SysErrInfo);
            }
            object key    = MB.Util.MyReflection.Instance.InvokePropertyForGet(entity, keyInfo.PropertyName);
            string sqlStr = string.Format("UPDATE {0} SET {1}= {2} WHERE {3}='{4}'", mappingInfo.MapTable, LAST_MODIFIED_DATE_PROPERTY,
                                          sysDate, keyInfo.FieldName, key.ToString());

            return(db.GetSqlStringCommand(sqlStr));
        }
示例#5
0
        /// <summary>
        /// 创建一个针对一个实体永久化操作的Command。
        /// </summary>
        /// <param name="db"></param>
        /// <param name="entity">操作实体</param>
        /// <param name="sqlName">SQL 语句配置对应的名称</param>
        /// <returns>返回包含的DbCommand</returns>
        public System.Data.Common.DbCommand[] GetDbCommand(Database db, BaseModel entity, string sqlName)
        {
            ModelMappingInfo mappingInfo = AttMappingManager.Instance.GetModelMappingInfo(entity.GetType());

            SqlString[] sqlStr = MB.Orm.Mapping.Xml.SqlConfigHelper.Instance.GetSqlString(mappingInfo.XmlConfigFileName, sqlName);
            List <System.Data.Common.DbCommand> cmds = new List <System.Data.Common.DbCommand>();

            foreach (SqlString s in sqlStr)
            {
                System.Data.Common.DbCommand dbCmd = PrepareExecuteCommand(db, entity, s);
                cmds.Add(dbCmd);
            }
            return(cmds.ToArray());
        }
        //获取对应的SQL 语句
        private SqlString[] getSqlString(Type entityType, OperationType operationType)
        {
            ModelMappingInfo mappingInfo = AttMappingManager.Instance.GetModelMappingInfo(entityType);
            string           xmlFileName = mappingInfo.XmlConfigFileName;
            string           sqlName     = getSqlName(operationType);

            MB.Orm.Mapping.Xml.SqlConfigHelper xmlConfig = MB.Orm.Mapping.Xml.SqlConfigHelper.Instance;

            SqlString[] sqlStrs  = xmlConfig.GetSqlString(xmlFileName, sqlName);
            string      cacheKey = GenerateCacheKey(entityType, operationType);

            CacheProxy.CacheSql(cacheKey, sqlStrs);
            return(sqlStrs);
        }
示例#7
0
        //选择
        private List <string> prepareSelectSql(ModelMappingInfo mappingInfo, string[] properties)
        {
            List <string> selectString = new List <string>();
            bool          existsPros   = properties != null && properties.Length > 0;

            foreach (FieldPropertyInfo info in mappingInfo.FieldPropertys.Values)
            {
                if (existsPros && Array.IndexOf <string>(properties, info.FieldName) < 0)
                {
                    continue;
                }
                selectString.Add(info.FieldName);
            }
            return(selectString);
        }
示例#8
0
        /// <summary>
        /// 根据配置的属性信息生成选择的Select 语句。
        /// </summary>
        /// <param name="entityType"></param>
        /// <param name="properties"></param>
        /// <returns></returns>
        public override SqlString[] GenerateSimpleSelectSql(Type entityType, string[] properties)
        {
            ModelMappingInfo mappingInfo = AttMappingManager.Instance.GetModelMappingInfo(entityType);
            string           tableName   = mappingInfo.MapTable;
            List <string>    columns     = prepareSelectSql(mappingInfo, properties);

            string sql = new StringBuilder("SELECT ").Append(string.Join(",", columns.ToArray())).Append(" FROM ").Append(tableName).ToString();

            SqlString ss       = new SqlString(sql, null);
            string    cacheKey = GenerateCacheKey(entityType, OperationType.Select, properties);

            SqlString[] asql = new SqlString[] { ss };
            CacheProxy.CacheSql(cacheKey, asql);
            return(asql);
        }
示例#9
0
        /// <summary>
        ///  针对一个实体永久化操作。
        ///   整个业务对象的所有实体操作在业务类的层次中进行整合操作处理。
        /// </summary>
        /// <param name="db"></param>
        /// <param name="entity"></param>
        /// <param name="operationType"></param>
        /// <param name="propertys"></param>
        /// <returns></returns>
        public System.Data.Common.DbCommand[] GetDbCommand(Database db, BaseModel entity, OperationType operationType, string[] propertys)
        {
            ModelMappingInfo   mappingInfo  = AttMappingManager.Instance.GetModelMappingInfo(entity.GetType());
            ModelConfigOptions cfgOptions   = mappingInfo.ConfigOptions;
            BaseSqlGenerator   sqlGenerator = SqlGeneratorManager.GetSqlGenerator(cfgOptions, propertys);

            SqlString[] sqlStr = sqlGenerator.GenerateSql(entity.GetType(), operationType, propertys);
            List <System.Data.Common.DbCommand> cmds = new List <System.Data.Common.DbCommand>();

            foreach (SqlString s in sqlStr)
            {
                System.Data.Common.DbCommand dbCmd = PrepareExecuteCommand(db, entity, s);
                cmds.Add(dbCmd);
            }
            return(cmds.ToArray());
        }
示例#10
0
        /// <summary>
        /// 通过键值自动获取某一条记录的SQL 语句。
        /// </summary>
        /// <param name="entityType"></param>
        /// <param name="properties"></param>
        /// <returns></returns>
        public override SqlString[] GenerateFindByKeySql(Type entityType, string[] properties)
        {
            ModelMappingInfo    mappingInfo  = AttMappingManager.Instance.GetModelMappingInfo(entityType);
            string              tableName    = mappingInfo.MapTable;
            List <string>       whereString  = new List <string>();
            List <SqlParamInfo> sqlParams    = new List <SqlParamInfo>();
            List <string>       selectString = prepareUpdateSql(mappingInfo, properties, ref whereString, ref sqlParams);
            SqlString           ss           = new SqlString(new StringBuilder("SELECT ").Append(string.Join(",", selectString.ToArray()))
                                                             .Append(" FROM ").Append(tableName).Append(" WHERE ").Append(string.Join(" AND ", whereString.ToArray())).ToString(), sqlParams);

            string cacheKey = GenerateCacheKey(entityType, OperationType.SelectByKey, properties);

            SqlString[] asql = new SqlString[] { ss };
            CacheProxy.CacheSql(cacheKey, asql);
            return(asql);
        }
示例#11
0
        //准备生成SQL Delete 语句处理。
        private List <string> prepareDeleteSql(ModelMappingInfo mappingInfo, ref List <SqlParamInfo> sqlParams)
        {
            List <string> whereString = new List <string>();

            sqlParams.Clear();

            foreach (FieldPropertyInfo info in mappingInfo.FieldPropertys.Values)
            {
                if (mappingInfo.PrimaryKeys.ContainsKey(info.FieldName))
                {
                    whereString.Add(info.FieldName + "=" + ParameterPrefix + info.FieldName);

                    sqlParams.Add(new SqlParamInfo(info.FieldName, DbShare.Instance.SystemTypeNameToDbType(info.DeclaringType.FullName)));
                }
            }
            return(whereString);
        }
示例#12
0
        /// <summary>
        /// 获取数据实体的键值。
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public object GetEntityKeyValue(object entity, out string keyPropertyName)
        {
            Type             entityType  = entity.GetType();
            ModelMappingInfo mappingInfo = AttMappingManager.Instance.GetModelMappingInfo(entityType);

            if (string.IsNullOrEmpty(mappingInfo.MapTable))
            {
                throw new MB.Util.APPException(string.Format("数据实体{0} 配置有误,没有指定映射到的表。", entityType.FullName), MB.Util.APPMessageType.SysErrInfo);
            }
            if (mappingInfo.PrimaryKeys == null || mappingInfo.PrimaryKeys.Count != 1)
            {
                throw new MB.Util.APPException(string.Format("数据实体{0} 配置有误,没有指定主键或者不是单一主键配置。", entityType.FullName), MB.Util.APPMessageType.SysErrInfo);
            }
            FieldPropertyInfo keyInfo = mappingInfo.PrimaryKeys.Values.First <FieldPropertyInfo>();

            object key = MB.Util.MyReflection.Instance.InvokePropertyForGet(entity, keyInfo.PropertyName);

            keyPropertyName = keyInfo.PropertyName;
            return(key);
        }
示例#13
0
        //准备生成SQL Insert  语句处理。
        private List <string> prepareInsertSql(ModelMappingInfo mappingInfo, string[] properties, ref List <string> parameters, ref List <SqlParamInfo> sqlParams)
        {
            parameters.Clear();
            sqlParams.Clear();

            List <string> fields     = new List <string>();
            bool          existsPros = properties != null && properties.Length > 0;

            foreach (FieldPropertyInfo info in mappingInfo.FieldPropertys.Values)
            {
                if (existsPros && Array.IndexOf <string>(properties, info.FieldName) < 0)
                {
                    continue;
                }
                fields.Add(info.FieldName);
                parameters.Add(ParameterPrefix + info.FieldName);
                sqlParams.Add(new SqlParamInfo(info.FieldName, DbShare.Instance.SystemTypeNameToDbType(info.DeclaringType.FullName)));
            }
            return(fields);
        }
示例#14
0
        /// <summary>
        /// 根据对象配置信息获取自动生成的Delete SQL 语句。
        /// </summary>
        /// <param name="entityType"></param>
        /// <returns></returns>
        public override SqlString[] GenerateDeleteSql(Type entityType)
        {
            SqlString        sqlStr      = null;
            ModelMappingInfo mappingInfo = AttMappingManager.Instance.GetModelMappingInfo(entityType);

            string tableName = mappingInfo.MapTable;
            List <SqlParamInfo> sqlParams   = new List <SqlParamInfo>();
            List <string>       whereString = prepareDeleteSql(mappingInfo, ref sqlParams);

            StringBuilder strSqlBuilder = new StringBuilder();

            strSqlBuilder.Append(" DELETE FROM ").Append(tableName).Append(" WHERE ").Append(string.Join(" AND ", whereString.ToArray()));

            sqlStr = new SqlString(strSqlBuilder.ToString(), sqlParams);

            string cacheKey = GenerateCacheKey(entityType, OperationType.Delete);

            SqlString[] asql = new SqlString[] { sqlStr };
            CacheProxy.CacheSql(cacheKey, asql);
            return(asql);
        }
示例#15
0
        //获取数据库中当前实体的最后修改时间。
        private DateTime getEntityLastModifiedDate(object entity)
        {
            Type             entityType  = entity.GetType();
            ModelMappingInfo mappingInfo = AttMappingManager.Instance.GetModelMappingInfo(entityType);

            if (string.IsNullOrEmpty(mappingInfo.MapTable))
            {
                throw new MB.Util.APPException(string.Format("数据实体{0} 配置有误,没有指定映射到的表。", entityType.FullName), MB.Util.APPMessageType.SysErrInfo);
            }

            if (mappingInfo.PrimaryKeys == null || mappingInfo.PrimaryKeys.Count != 1)
            {
                throw new MB.Util.APPException(string.Format("数据实体{0} 配置有误,没有指定主键或者不是单一主键配置。", entityType.FullName), MB.Util.APPMessageType.SysErrInfo);
            }

            FieldPropertyInfo keyInfo = mappingInfo.PrimaryKeys.Values.First <FieldPropertyInfo>();

            object key = MB.Util.MyReflection.Instance.InvokePropertyForGet(entity, keyInfo.PropertyName);

            if (key == null)
            {
                return(DateTime.MinValue);
            }

            string sqlStr = string.Format("SELECT {0} FROM {1} WHERE {2}='{3}'", LAST_MODIFIED_DATE_PROPERTY, mappingInfo.MapTable, keyInfo.FieldName, key.ToString());

            Database  db        = MB.Orm.Persistence.DatabaseHelper.CreateDatabase();
            int       lastID    = 1;
            DbCommand cmdSelect = db.GetSqlStringCommand(sqlStr);
            object    val       = db.ExecuteScalar(cmdSelect);

            if (val == null || val == System.DBNull.Value)
            {
                return(DateTime.MinValue);
            }

            cmdSelect.Dispose();

            return(System.Convert.ToDateTime(val));
        }
示例#16
0
        /// <summary>
        /// 根据配置的信息自动产生Insert 的SQL 语句。
        /// </summary>
        /// <param name="entityType"></param>
        /// <param name="properties"></param>
        /// <returns></returns>
        public override SqlString[] GenerateInsertSql(Type entityType, string[] properties)
        {
            SqlString        sqlStr      = null;
            ModelMappingInfo mappingInfo = AttMappingManager.Instance.GetModelMappingInfo(entityType);

            string tableName = mappingInfo.MapTable;

            List <string>       parameters = new List <string>();
            List <SqlParamInfo> sqlParams  = new List <SqlParamInfo>();
            List <string>       fields     = prepareInsertSql(mappingInfo, properties, ref parameters, ref sqlParams);

            StringBuilder strSqlBuilder = new StringBuilder("INSERT INTO ").
                                          Append(tableName).Append("(").Append(string.Join(",", fields.ToArray())).
                                          Append(") VALUES(").Append(string.Join(",", parameters.ToArray())).Append(") ");

            sqlStr = new SqlString(strSqlBuilder.ToString(), sqlParams);

            string cacheKey = GenerateCacheKey(entityType, OperationType.Insert);

            SqlString[] asql = new SqlString[] { sqlStr };
            CacheProxy.CacheSql(cacheKey, asql);
            return(asql);
        }
示例#17
0
 public ModelMapping() : base(typeof(TSource))
 {
     MappingInfo = new ModelMappingInfo <TSource>();
 }
示例#18
0
 /// <summary>
 /// 获取自增列的ID 值。
 /// </summary>
 /// <param name="mappingInfo"></param>
 /// <returns></returns>
 public int GetAutoIncreaseID(ModelMappingInfo mappingInfo)
 {
     throw new MB.Util.APPException("需要配置数据库自增列的处理方式!");
 }