Esempio n. 1
0
        private string GenerateUpdateSql(Type type, BaseEntity entity)
        {
            PropertyInfo[]     propertyInfos = type.GetProperties();
            TableInfoAttribute tableInfo     = TableInfoAttribute.GetAttribute(type);
            string             tableName     = tableInfo == null ? type.Name : tableInfo.TableName;
            var updateString = new StringBuilder();

            updateString.AppendFormat("UPDATE {0} SET ", tableName);
            int columnCount = 0;

            foreach (PropertyInfo info in propertyInfos)
            {
                ExtendedAttribute extended = ExtendedAttribute.GetAttribute(info);
                if (extended != null)
                {
                    continue;
                }
                ExcludeFieldAttribute exclude = ExcludeFieldAttribute.GetAttribute(info);
                if (exclude != null)
                {
                    continue;
                }
                IdentityAttribute identity = IdentityAttribute.GetAttribute(info);
                if (identity != null)
                {
                    continue;
                }
                RefFieldAttribute refField = RefFieldAttribute.GetAttribute(info);
                if (refField != null)
                {
                    continue;
                }
                GuidIdentityAttribute guidIdentity = GuidIdentityAttribute.GetAttribute(info);
                if (guidIdentity != null)
                {
                    continue;
                }
                //var dbv = entity.Dbvalue.FirstOrDefault(p => p.Key == info.Name);
                //var newvalue = info.GetValue(entity, null);
                //if (dbv.Value != null && newvalue != null)
                //    if (dbv.ToString() == newvalue.ToString()) continue;

                if (columnCount != 0)
                {
                    updateString.Append(",");
                }
                updateString.AppendFormat("{0}=@{0}", info.Name);
                columnCount++;
            }
            if (columnCount == 0)
            {
                return("");
            }
            return(updateString.ToString());
        }
Esempio n. 2
0
        private string GenerateUpdateSql(Type type)
        {
            //判断缓存中存在已经生成的Sql语句,则直接返回
            if (_updateSqlCaches.ContainsKey(type))
            {
                return(_updateSqlCaches[type]);
            }
            PropertyInfo[]     propertyInfos = type.GetProperties();
            TableInfoAttribute tableInfo     = TableInfoAttribute.GetAttribute(type);
            string             tableName     = tableInfo == null ? type.Name : tableInfo.TableName;
            var updateSql = new StringBuilder();

            updateSql.AppendFormat("UPDATE {0} SET ", tableName);
            int columnCount = 0;

            foreach (PropertyInfo info in propertyInfos)
            {
                ExtendedAttribute extended = ExtendedAttribute.GetAttribute(info);
                if (extended != null)
                {
                    continue;
                }
                ExcludeFieldAttribute exclude = ExcludeFieldAttribute.GetAttribute(info);
                if (exclude != null)
                {
                    continue;
                }
                IdentityAttribute identity = IdentityAttribute.GetAttribute(info);
                if (identity != null)
                {
                    continue;
                }
                RefFieldAttribute refField = RefFieldAttribute.GetAttribute(info);
                if (refField != null)
                {
                    continue;
                }
                GuidIdentityAttribute guidIdentity = GuidIdentityAttribute.GetAttribute(info);
                if (guidIdentity != null)
                {
                    continue;
                }

                if (columnCount != 0)
                {
                    updateSql.Append(",");
                }
                updateSql.AppendFormat("{0}=@{0}", info.Name);
                columnCount++;
            }
            string updateString = updateSql.ToString();

            _updateSqlCaches[type] = updateString;
            return(updateString);
        }
Esempio n. 3
0
        /// <summary>
        ///     添加一个实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public string AddEntity(BaseEntity entity)
        {
            //判断是否启用了缓存
            Type type = entity.GetType();


            PropertyInfo[] propertyInfos = type.GetProperties();

            var ps          = new DynamicParameters {
            };
            PropertyInfo pk = null;

            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                if (IdentityAttribute.GetAttribute(propertyInfo) != null)
                {
                    pk = propertyInfo;
                }
                if (GuidIdentityAttribute.GetAttribute(propertyInfo) != null)
                {
                    pk = propertyInfo;
                }
                if (ExcludeFieldAttribute.GetAttribute(propertyInfo) != null)
                {
                    continue;
                }

                ps.Add(propertyInfo.Name, propertyInfo.GetValue(entity, null));
            }



            string insertSql = GenerateInsertSql(type);

            try
            {
                using (IDbConnection connection = OpenConnection())
                {
                    string id = connection.Query <string>(insertSql, ps).FirstOrDefault();


                    if (pk != null)
                    {
                        pk.SetValue(entity, id, null);
                    }

                    return(id);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("发生错误,SQL语句为:" + insertSql + "\r\n实体为:" + entity, ex);
            }
        }
Esempio n. 4
0
        private string GenerateUpdateSql(BaseEntity entity)
        {
            var type = entity.GetType();

            PropertyInfo[]     propertyInfos = type.GetProperties();
            TableInfoAttribute tableInfo     = TableInfoAttribute.GetAttribute(type);
            string             tableName     = tableInfo == null ? type.Name : tableInfo.TableName;
            var updateString = new StringBuilder();

            updateString.AppendFormat("UPDATE {0} SET ", tableName);
            int columnCount = 0;

            foreach (PropertyInfo info in propertyInfos)
            {
                ExtendedAttribute extended = ExtendedAttribute.GetAttribute(info);
                if (extended != null)
                {
                    continue;
                }
                ExcludeFieldAttribute exclude = ExcludeFieldAttribute.GetAttribute(info);
                if (exclude != null)
                {
                    continue;
                }
                IdentityAttribute identity = IdentityAttribute.GetAttribute(info);
                if (identity != null)
                {
                    continue;
                }
                RefFieldAttribute refField = RefFieldAttribute.GetAttribute(info);
                if (refField != null)
                {
                    continue;
                }

                if (!object.Equals(entity.Dbvalue[info.Name], info.GetValue(entity, null)))
                {
                    if (columnCount != 0)
                    {
                        updateString.Append(",");
                    }
                    updateString.AppendFormat("{0}=@{0}", info.Name);
                    columnCount++;
                }
            }
            if (columnCount == 0)
            {
                return("");
            }
            return(updateString.ToString());
        }
        /// <summary>
        /// 生成更新的Sql
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        protected string GenerateUpdateSql(Type type)
        {
            PropertyInfo[]     propertyInfos = type.GetProperties();
            TableInfoAttribute tableInfo     = TableInfoAttribute.GetAttribute(type);
            string             tableName     = tableInfo == null ? type.Name : tableInfo.TableName;
            var updateSql = new StringBuilder();

            updateSql.AppendFormat("UPDATE {0} SET ", tableName);
            int columnCount = 0;

            foreach (PropertyInfo info in propertyInfos)
            {
                ExtendedAttribute extended = ExtendedAttribute.GetAttribute(info);
                if (extended != null)
                {
                    continue;
                }
                ExcludeFieldAttribute exclude = ExcludeFieldAttribute.GetAttribute(info);
                if (exclude != null)
                {
                    continue;
                }
                IdentityAttribute identity = IdentityAttribute.GetAttribute(info);
                if (identity != null)
                {
                    continue;
                }
                RefFieldAttribute refField = RefFieldAttribute.GetAttribute(info);
                if (refField != null)
                {
                    continue;
                }

                if (columnCount != 0)
                {
                    updateSql.Append(",");
                }
                updateSql.AppendFormat("{0}=@{0}", info.Name);
                columnCount++;
            }
            string updateString = updateSql.ToString();

            return(updateString);
        }
Esempio n. 6
0
        /// <summary>
        ///     添加一个实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public override int AddEntity(BaseEntity entity)
        {
            //判断是否启用了缓存
            Type type = entity.GetType();


            PropertyInfo[] propertyInfos = type.GetProperties();

            var ps          = new DynamicParameters {
            };
            PropertyInfo pk = null;
            bool         hasIdentityField = false;

            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                if (IdentityAttribute.GetAttribute(propertyInfo) != null)
                {
                    pk = propertyInfo;
                    hasIdentityField = true;
                }
                if (ExcludeFieldAttribute.GetAttribute(propertyInfo) != null)
                {
                    continue;
                }

                ps.Add(propertyInfo.Name, propertyInfo.GetValue(entity, null));
            }



            string insertSql = GenerateInsertSql(type);

            try
            {
                using (IDbConnection connection = OpenConnection(SqlExecuteType.Write))
                {
                    OnExecutingCommand(insertSql);
                    decimal id = connection.Query <decimal>(insertSql, ps).FirstOrDefault();
                    OnExecutedCommand(insertSql);

                    if (hasIdentityField)
                    {
                        pk.SetValue(entity, Convert.ToInt32(id), null);

                        if (entity.IsEnableCache)
                        {
                            RetechWing.Infrastructure.Caching.CacheHelper.CacheService.Add(
                                EntityUpdateTrackHelper.GetEntityKey(entity), entity,
                                CachingExpirationType.SingleObject
                                );
                        }
                    }

                    return(Convert.ToInt32(id));
                }
            }
            catch (Exception ex)
            {
                throw new Exception("发生错误,SQL语句为:" + insertSql + "\r\n实体为:" + entity, ex);
            }
        }
        /// <summary>
        ///     生成新增的Sql语句
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        protected string GenerateInsertSql(object obj)
        {
            Type type = obj.GetType();

            PropertyInfo[]     propertyInfos = type.GetProperties();
            var                insertSql     = new StringBuilder();
            TableInfoAttribute tableInfo     = TableInfoAttribute.GetAttribute(type);
            string             tableName;

            if (tableInfo == null)
            {
                tableName = type.Name;
            }
            else
            {
                tableName = tableInfo.TableName;
            }

            insertSql.AppendFormat("INSERT INTO {0} (", tableName);

            var values      = new StringBuilder(" VALUES (");
            int columnCount = 0;

            foreach (PropertyInfo info in propertyInfos)
            {
                ExtendedAttribute extended = ExtendedAttribute.GetAttribute(info);
                if (extended != null)
                {
                    continue;
                }
                ExcludeFieldAttribute exclude = ExcludeFieldAttribute.GetAttribute(info);
                if (exclude != null)
                {
                    continue;
                }
                IdentityAttribute identity = IdentityAttribute.GetAttribute(info);
                if (identity != null)
                {
                    continue;
                }
                RefFieldAttribute refField = RefFieldAttribute.GetAttribute(info);
                if (refField != null)
                {
                    continue;
                }
                if (columnCount != 0)
                {
                    insertSql.Append(",");
                    values.Append(",");
                }
                object value = info.GetValue(obj, null);
                if (value == null || value == DBNull.Value)
                {
                    value = "NULL";
                }
                else
                {
                    value = string.Format("'{0}'", value.ToString().ReplaceInsertSql());
                }
                insertSql.AppendFormat("{0}", info.Name);
                values.Append(value);
                columnCount++;
            }
            insertSql.AppendFormat(") {0} ) ", values);

            string insertSqlstr = insertSql.ToString() + ";";

            //_insertSqlCaches.Add(type, insertSqlstr);//加入缓存
            return(insertSqlstr);
        }
Esempio n. 8
0
        /// <summary>
        ///     生成新增的Sql语句
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        internal string GenerateInsertSql(Type type)
        {
            //判断缓存中存在已经生成的Sql语句,则直接返回
            if (_insertSqlCaches.ContainsKey(type))
            {
                return(_insertSqlCaches[type]);
            }
            PropertyInfo[] propertyInfos = type.GetProperties();
            var            insertSql     = new StringBuilder();

            bool hasIdentityField        = false;
            TableInfoAttribute tableInfo = TableInfoAttribute.GetAttribute(type);
            string             tableName = tableInfo == null ? type.Name : tableInfo.TableName;

            insertSql.AppendFormat("INSERT INTO {0} (", tableName);

            var values      = new StringBuilder(" VALUES (");
            int columnCount = 0;

            foreach (PropertyInfo info in propertyInfos)
            {
                ExtendedAttribute extended = ExtendedAttribute.GetAttribute(info);
                if (extended != null)
                {
                    continue;
                }
                ExcludeFieldAttribute exclude = ExcludeFieldAttribute.GetAttribute(info);
                if (exclude != null)
                {
                    continue;
                }
                IdentityAttribute identity = IdentityAttribute.GetAttribute(info);
                if (identity != null)
                {
                    hasIdentityField = true;
                    continue;
                }
                GuidIdentityAttribute guidIdentity = GuidIdentityAttribute.GetAttribute(info);
                if (guidIdentity != null)
                {
                    hasIdentityField = true;
                }
                RefFieldAttribute refField = RefFieldAttribute.GetAttribute(info);
                if (refField != null)
                {
                    continue;
                }
                if (columnCount != 0)
                {
                    insertSql.Append(",");
                    values.Append(",");
                }
                insertSql.AppendFormat("{0}", info.Name);
                values.AppendLine("@" + info.Name);
                columnCount++;
            }
            insertSql.AppendFormat(") {0} ) ", values);

            if (hasIdentityField)
            {
                insertSql.AppendFormat(_identity);
            }
            string insertSqlstr = insertSql.ToString();

            _insertSqlCaches.Add(type, insertSqlstr); //加入缓存
            return(insertSqlstr);
        }