예제 #1
0
        /// <summary>
        /// 处理类型中的字定义属性
        /// </summary>
        /// <param name="val">The value.</param>
        /// <param name="pi">The pi.</param>
        /// <returns>
        /// System.String
        /// </returns>
        public static string TypeToString(object val, PropertyInfo pi)
        {
            if (val == null)
            {
                return(null);
            }
            string rval = val.ToString();

            object[] attrs = pi.GetCustomAttributes(typeof(EntityPropertyAttribute), true);
            if (attrs.Length > 0)
            {
                EntityPropertyAttribute pa = attrs.GetValue(0) as EntityPropertyAttribute;
                if (pa != null)
                {
                    if (pa.Format == PropertyFormat.UnixTime)
                    {
                        rval = Utils.ToUnixTime(Utils.ToDateTime(val)).ToString();
                    }
                    //else if (pa.Format == PropertyFormat.Html)
                    //{
                    //    rval = Utils.HtmlDecode(val.ToString());
                    //}
                }
            }
            return(rval);
        }
예제 #2
0
파일: DBProvider.cs 프로젝트: tynr426/ict
        /// <summary>
        /// 获取待更新的数据库查询语句.
        /// </summary>
        /// <param name="entity">实体.</param>
        /// <param name="tableName">数据库表名.</param>
        /// <param name="fields">条件字段.</param>
        /// <param name="commandParameters">输出参数.</param>
        /// <param name="prefixParameter">The prefix paramter.</param>
        /// <returns></returns>
        /// <exception cref="System.Data.Common.DbException"></exception>
        public string UpdateCommandText(IEntity entity, string tableName, string[] fields, out DbParameter[] commandParameters, string prefixParameter = "")
        {
            try
            {
                Type           type = GetCacheType(entity);
                PropertyInfo[] pis  = type.GetProperties();
                string         sql  = "UPDATE " + tableName + " SET ";

                bool isValidateDataField = EntityClassAttribute.IsValidateField(type);

                using (MySqlCommand cmd = new MySqlCommand())
                {
                    string condition = "";
                    foreach (PropertyInfo pi in pis)
                    {
                        // 此类中包含有非数据库字段,需要进行排除
                        if (isValidateDataField)
                        {
                            if (EntityPropertyAttribute.IsNoDataField(pi))
                            {
                                continue;
                            }
                        }
                        string paraName = "@" + prefixParameter + entity.EntityFullName.Replace(",", "_").Replace(".", "_") + "_" + pi.Name;
                        //string type = pi.PropertyType.UnderlyingSystemType.ToString().ToLower();
                        if (fields != null && fields.Contains <string>(pi.Name, StringComparer.InvariantCultureIgnoreCase))
                        {
                            condition += (String.IsNullOrEmpty(condition) ? "" : " and ") + pi.Name + "=" + paraName;
                            cmd.Parameters.AddWithValue(paraName, pi.GetValue(entity, null));
                        }
                        else
                        {
                            if (pi.GetValue(entity, null) != null && pi.CanWrite && pi.Name.ToLower() != "entityfullname")
                            {
                                sql += pi.Name + "=" + paraName + ",";
                                cmd.Parameters.AddWithValue(paraName, pi.GetValue(entity, null));
                            }
                        }
                    }

                    sql = sql.Remove(sql.Length - 1);
                    if (!Utils.IsNullOrEmpty(condition))
                    {
                        sql = sql + " WHERE " + condition;
                    }


                    commandParameters = new MySqlParameter[cmd.Parameters.Count];
                    // 将cmd的Parameters参数集复制到discoveredParameters数组.
                    cmd.Parameters.CopyTo(commandParameters, 0);

                    return(sql + ";");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #3
0
파일: DBProvider.cs 프로젝트: tynr426/ict
        /// <summary>
        /// 获取待更新的数据库查询语句.
        /// </summary>
        /// <param name="entity">实体.</param>
        /// <param name="tableName">数据库表名.</param>
        /// <param name="fields">条件字段.</param>
        /// <returns></returns>
        public string UpdateCommandText(IEntity entity, string tableName, string[] fields)
        {
            try
            {
                Type           type = GetCacheType(entity);
                PropertyInfo[] pis  = type.GetProperties();
                // 获取此类中是否包含数据字段验证
                bool isValidateDataField = EntityClassAttribute.IsValidateField(type);

                string sql = "UPDATE " + tableName + " SET ";

                string condtion = "";
                foreach (PropertyInfo pi in pis)
                {
                    // 此类中包含有非数据库字段,需要进行排除
                    if (isValidateDataField)
                    {
                        if (EntityPropertyAttribute.IsNoDataField(pi))
                        {
                            continue;
                        }
                    }

                    //string type = pi.PropertyType.UnderlyingSystemType.ToString().ToLower();
                    if (fields != null && fields.Contains <string>(pi.Name, StringComparer.InvariantCultureIgnoreCase))
                    {
                        condtion += (String.IsNullOrEmpty(condtion) ? "" : " and ") + pi.Name + "=" + SqlString(pi, pi.GetValue(entity, null));
                    }
                    else
                    {
                        if (pi.GetValue(entity, null) != null && pi.CanWrite && pi.Name.ToLower() != "entityfullname")
                        {
                            sql += pi.Name + "=" + SqlString(pi, pi.GetValue(entity, null)) + ",";
                        }
                    }
                }

                sql = sql.Remove(sql.Length - 1);
                if (!Utils.IsNullOrEmpty(condtion))
                {
                    sql = sql + " WHERE " + condtion;
                }


                return(sql + ";");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="SmartEntityProperty"/> class.
        /// </summary>
        /// <param name="attribute">Атрибут <see cref="EntityPropertyAttribute"/></param>
        /// <param name="entityType">Тип интеллектуального объекта</param>
        /// <param name="value">Значение свойства</param>
        /// <param name="ownerObjectId">Идентификатор блока</param>
        public SmartEntityProperty(
            EntityPropertyAttribute attribute,
            Type entityType,
            object value,
            ObjectId ownerObjectId)
        {
            EntityType    = entityType;
            OwnerObjectId = ownerObjectId;
            Category      = attribute.Category;
            OrderIndex    = attribute.OrderIndex;
            Name          = attribute.Name;
            DisplayNameLocalizationKey = attribute.DisplayNameLocalizationKey;
            NameSymbolForStyleEditor   = attribute.NameSymbol;
            DescriptionLocalizationKey = attribute.DescriptionLocalizationKey;

            if (value != null && value.GetType() == typeof(AnnotationScale))
            {
                DefaultValue = new AnnotationScale
                {
                    Name         = attribute.DefaultValue.ToString(),
                    DrawingUnits = double.Parse(attribute.DefaultValue.ToString().Split(':')[0]),
                    PaperUnits   = double.Parse(attribute.DefaultValue.ToString().Split(':')[1])
                };
            }
            else if (Name == "LayerName" && string.IsNullOrEmpty(attribute.DefaultValue.ToString()))
            {
                DefaultValue = ModPlusAPI.Language.GetItem("defl");
            }
            else
            {
                DefaultValue = attribute.DefaultValue;
            }

            Minimum = attribute.Minimum;
            Maximum = attribute.Maximum;
            Value   = value;
            if (value is double d)
            {
                DoubleValue = d;
            }

            if (value is int i)
            {
                IntValue = i;
            }

            PropertyScope = attribute.PropertyScope;
            IsReadOnly    = attribute.IsReadOnly;
        }
예제 #5
0
파일: DBProvider.cs 프로젝트: tynr426/ict
        /// <summary>
        /// 获取删除数据的Sql语句.
        /// </summary>
        /// <param name="entity">已附值的实体.</param>
        /// <param name="tableName">需要删除数据的表.</param>
        /// <param name="commandParameters">输出参数.</param>
        /// <param name="prefixParameter">The prefix paramter.</param>
        /// <returns></returns>
        /// <exception cref="System.Data.Common.DbException"></exception>
        public string DeleteCommandText(IEntity entity, string tableName, out DbParameter[] commandParameters, string prefixParameter = "")
        {
            try
            {
                Type           type = entity.GetType();
                PropertyInfo[] pis  = type.GetProperties();


                bool isValidateDataField = EntityClassAttribute.IsValidateField(type);

                string sql = "DELETE FROM " + tableName + " WHERE ";

                using (MySqlCommand cmd = new MySqlCommand())
                {
                    string where = "";
                    foreach (PropertyInfo pi in pis)
                    {
                        if (pi.GetValue(entity, null) != null && pi.Name.ToLower() != "entityfullname")
                        {
                            // 此类中包含有非数据库字段,需要进行排除
                            if (isValidateDataField)
                            {
                                if (EntityPropertyAttribute.IsNoDataField(pi))
                                {
                                    continue;
                                }
                            }

                            string paraName = "@" + prefixParameter + entity.EntityFullName.Replace(",", "_").Replace(".", "_") + "_" + pi.Name;

                            where += (Utils.IsNullOrEmpty(where) ? "" : " AND ") + pi.Name + "=" + paraName;
                            cmd.Parameters.AddWithValue(paraName, pi.GetValue(entity, null));
                        }
                    }

                    commandParameters = new MySqlParameter[cmd.Parameters.Count];
                    // 将cmd的Parameters参数集复制到discoveredParameters数组.
                    cmd.Parameters.CopyTo(commandParameters, 0);

                    return(sql + where + ";");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #6
0
파일: DBProvider.cs 프로젝트: tynr426/ict
        /// <summary>
        /// 获取插入数据的sql语句.
        /// </summary>
        /// <param name="entity">实体.</param>
        /// <param name="tableName">Name of the table.</param>
        /// <returns></returns>
        public string InsertCommandText(IEntity entity, string tableName)
        {
            try
            {
                Type           type = GetCacheType(entity);
                PropertyInfo[] pis  = type.GetProperties();

                // 获取此类中是否包含数据字段验证
                bool isValidateDataField = EntityClassAttribute.IsValidateField(type);

                string sql = "INSERT INTO " + tableName + " (";
                string val = " VALUES(";

                foreach (PropertyInfo pi in pis)
                {
                    if (pi.GetValue(entity, null) != null && pi.CanWrite && pi.Name.ToLower() != "entityfullname")
                    {
                        // 此类中包含有非数据库字段,需要进行排除
                        if (isValidateDataField)
                        {
                            if (EntityPropertyAttribute.IsNoDataField(pi))
                            {
                                continue;
                            }
                        }

                        sql += pi.Name + ",";
                        val += SqlString(pi, pi.GetValue(entity, null)) + ",";
                    }
                }

                sql = sql.Remove(sql.Length - 1) + ") ";
                val = val.Remove(val.Length - 1) + ")";

                return(sql + val + ";");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #7
0
파일: DBProvider.cs 프로젝트: tynr426/ict
        /// <summary>
        /// 获取删除数据的Sql语句.
        /// </summary>
        /// <param name="entity">已附值的实体.</param>
        /// <param name="tableName">需要删除数据的表.</param>
        /// <returns></returns>
        public string DeleteCommandText(IEntity entity, string tableName)
        {
            try
            {
                Type           type = entity.GetType();
                PropertyInfo[] pis  = type.GetProperties();
                bool           isValidateDataField = EntityClassAttribute.IsValidateField(type);

                string sql = "DELETE FROM " + tableName + " WHERE ";

                using (MySqlCommand cmd = new MySqlCommand())
                {
                    string where = "";
                    foreach (PropertyInfo pi in pis)
                    {
                        if (pi.GetValue(entity, null) != null && pi.Name.ToLower() != "entityfullname")
                        {
                            // 此类中包含有非数据库字段,需要进行排除
                            if (isValidateDataField)
                            {
                                if (EntityPropertyAttribute.IsNoDataField(pi))
                                {
                                    continue;
                                }
                            }
                            where += (Utils.IsNullOrEmpty(where) ? "" : " AND ") + pi.Name + "=" + SqlString(pi, pi.GetValue(entity, null));
                        }
                    }

                    return(sql + where + ";");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #8
0
        /// <summary>
        /// 数据库类型转换
        /// </summary>
        /// <param name="val">待转换值.</param>
        /// <param name="pi">The pi.</param>
        /// <returns>
        /// System.Object
        /// </returns>
        public static object ChangeType(object val, PropertyInfo pi)
        {
            if (val == null)
            {
                return(null);
            }
            Type type = pi.PropertyType;

            type = Utils.GetNullableType(type);

            if (val.ToString() == "" && (type.Name == "Int32" || type.Name == "Int64"))
            {
                return(null);
            }

            if (type.Name == "bool" || type.Name == "Boolean")
            {
                if (val.ToString() == "1" || val.ToString().ToLower() == "true")
                {
                    val = true;
                }
                else
                {
                    val = false;
                }
            }
            else if (type.Name == "string" || type.Name == "String")
            {
                object[] attrs = pi.GetCustomAttributes(typeof(EntityPropertyAttribute), true);
                if (attrs.Length == 1)
                {
                    EntityPropertyAttribute pa = attrs.GetValue(0) as EntityPropertyAttribute;
                    if (pa != null)
                    {
                        if (pa.Format != PropertyFormat.Html)
                        {
                            val = Utils.EncodeHtml(val.ToString());
                        }
                        else if (pa.Format == PropertyFormat.UnixTime)
                        {
                            val = Utils.UnixTimeToTime(val.ToString());
                        }
                    }
                }
                else
                {
                    val = Utils.EncodeHtml(val.ToString());
                }
            }
            else if (type.Name.ToLower() == "int32")
            {
                val = Utils.ToInt(val);
            }
            else if (type.Name.ToLower() == "int64")//修改原因,是长整形转换成int会失败
            {
                val = Utils.ToInt64(val);
            }
            try
            {
                return(Convert.ChangeType(val, type, CultureInfo.CurrentCulture));
            }
            catch //(Exception ex)
            {
                //new ECFException(ex.Message, ex);
                if (val != null)
                {
                    if (type.Name == "DateTime")
                    {
                        val = new DateTime(1900, 0, 0);
                    }
                    else if (type == typeof(int) || type == typeof(Decimal) || type == typeof(double))
                    {
                        val = 0;
                    }
                    return(Convert.ChangeType(val, type, CultureInfo.CurrentCulture));
                }
                return(null);
            }
        }
예제 #9
0
파일: DBProvider.cs 프로젝트: tynr426/ict
        /// <summary>
        /// 获取待更新和插入的数据库查询语句.
        /// </summary>
        /// <param name="entity">实体.</param>
        /// <param name="tableName">数据库表名.</param>
        /// <param name="fields">条件字段.</param>
        /// <param name="commandParameters">输出参数.</param>
        /// <param name="prefixParameter">参数前缀.</param>
        /// <returns></returns>
        public string UpsertCommandText(IEntity entity, string tableName, string[] fields, out DbParameter[] commandParameters, string prefixParameter = "")
        {
            try
            {
                Type           type = GetCacheType(entity);
                PropertyInfo[] pis = type.GetProperties();
                string         updateSql = string.Empty, setSql = string.Empty;
                string         insertSql = "INSERT INTO " + tableName + "(";
                List <string>  vals      = new List <string>();
                //                    (name)
                //select 'test1'
                //FROM DUAL WHERE NOT EXISTS(SELECT 1 FROM test1 WHERE name = 'test1'); "
                bool isValidateDataField = EntityClassAttribute.IsValidateField(type);

                using (MySqlCommand cmd = new MySqlCommand())
                {
                    string condition = "";
                    foreach (PropertyInfo pi in pis)
                    {
                        if (pi.GetValue(entity, null) != null && pi.CanWrite && pi.Name.ToLower() != "entityfullname")
                        {
                            // 此类中包含有非数据库字段,需要进行排除
                            if (isValidateDataField)
                            {
                                if (EntityPropertyAttribute.IsNoDataField(pi))
                                {
                                    continue;
                                }
                            }
                            string paraName = "@" + prefixParameter + entity.EntityFullName.Replace(",", "_").Replace(".", "_") + "_" + pi.Name;

                            // 插入数据
                            insertSql += pi.Name + ",";
                            vals.Add(paraName);

                            if (fields != null && fields.Contains <string>(pi.Name, StringComparer.InvariantCultureIgnoreCase))
                            {
                                condition += (String.IsNullOrEmpty(condition) ? "" : " and ") + pi.Name + "=" + paraName;
                                cmd.Parameters.AddWithValue(paraName, pi.GetValue(entity, null));
                            }
                            else
                            {
                                setSql += pi.Name + "=" + paraName + ",";
                                cmd.Parameters.AddWithValue(paraName, pi.GetValue(entity, null));
                            }
                        }
                    }

                    // 组装插入语句
                    insertSql = insertSql.Remove(insertSql.Length - 1) + ") SELECT " + string.Join(",", vals)
                                + " FROM DUAL WHERE NOT EXISTS(SELECT 1 FROM " + tableName + " WHERE " + condition + ");";

                    // 组装更新语句
                    if (!string.IsNullOrEmpty(setSql))
                    {
                        updateSql = "UPDATE " + tableName + " SET " + setSql.Remove(setSql.Length - 1);
                        if (!Utils.IsNullOrEmpty(condition))
                        {
                            updateSql = updateSql + " WHERE " + condition;
                        }
                    }

                    commandParameters = new MySqlParameter[cmd.Parameters.Count];
                    // 将cmd的Parameters参数集复制到discoveredParameters数组.
                    cmd.Parameters.CopyTo(commandParameters, 0);

                    return(insertSql + updateSql + ";");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #10
0
파일: DBProvider.cs 프로젝트: tynr426/ict
        /// <summary>
        /// 获取插入数据的sql语句.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="tableName">Name of the table.</param>
        /// <param name="commandParameters">The command parameters.</param>
        /// <param name="prefixParameter">The prefix paramter.</param>
        /// <returns></returns>
        /// <exception cref="System.Data.Common.DbException"></exception>
        public string InsertCommandText(IEntity entity, string tableName, out DbParameter[] commandParameters, string prefixParameter = "")
        {
            try
            {
                Type           type = GetCacheType(entity);
                PropertyInfo[] pis  = type.GetProperties();

                // 获取此类中是否包含数据字段验证
                bool isValidateDataField = EntityClassAttribute.IsValidateField(type);

                string sql = "INSERT INTO " + tableName + " (";
                string val = " VALUES(";

                using (MySqlCommand cmd = new MySqlCommand())
                {
                    foreach (PropertyInfo pi in pis)
                    {
                        if (pi.GetValue(entity, null) != null && pi.CanWrite && pi.Name.ToLower() != "entityfullname")
                        {
                            // 此类中包含有非数据库字段,需要进行排除
                            if (isValidateDataField)
                            {
                                if (EntityPropertyAttribute.IsNoDataField(pi))
                                {
                                    continue;
                                }
                            }

                            string paraName = "@" + prefixParameter + entity.EntityFullName.Replace(",", "_").Replace(".", "_") + "_" + pi.Name;

                            sql += pi.Name + ",";
                            val += paraName + ",";
                            cmd.Parameters.AddWithValue(paraName, pi.GetValue(entity, null));
                        }
                    }
                    sql = sql.Remove(sql.Length - 1) + ") ";
                    val = val.Remove(val.Length - 1) + ")";


                    //foreach (PropertyInfo pi in pis)
                    //{
                    //    if (pi.GetValue(entity, null) != null && pi.CanWrite && pi.Name.ToLower() != "entityfullname")
                    //    {
                    //        sql += pi.Name + ",";
                    //        val += "@" + pi.Name + ",";
                    //        cmd.Parameters.AddWithValue("@" + pi.Name, pi.GetValue(entity, null));
                    //    }
                    //}
                    //sql = sql.Remove(sql.Length - 1) + ") ";
                    //val = val.Remove(val.Length - 1) + ")";

                    commandParameters = new MySqlParameter[cmd.Parameters.Count];

                    // 将cmd的Parameters参数集复制到discoveredParameters数组.
                    cmd.Parameters.CopyTo(commandParameters, 0);

                    return(sql + val + ";");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }