Ejemplo n.º 1
0
        /// <summary>
        /// 构造一个表属性
        /// </summary>
        /// <param name="table"></param>
        /// <param name="PKGenerator"></param>
        /// <returns></returns>
        protected virtual string GenerateClassAttribute(Table table, String PKGenerator)
        {
            TableMappingAttribute Attribute = new TableMappingAttribute();

            Attribute.TypeName     = GenerateClassName(table);
            Attribute.IsTable      = true;
            Attribute.TableName    = table.Name;
            Attribute.KeyGenerator = PKGenerator;
            return(Attribute.ToString());
        }
Ejemplo n.º 2
0
        public List <SystemObjectFields> GetSystemObjectFields <T>()
        {
            var para  = new Hashtable();
            var typeT = typeof(T);
            TableMappingAttribute attribute = BaseDaoAttribute.GetTableMappingAttribute(typeT);

            if (attribute == null)
            {
                return(null);
            }

            para.Add("ObjectName", attribute.TableName);
            return(QueryForList <SystemObjectFields>("QuerySystemObjectFields", para));
        }
Ejemplo n.º 3
0
        public List <T> GetDataList <T>(string exp, string orderExp, bool isDesc = true)
        {
            var typeT = typeof(T);
            TableMappingAttribute attribute = BaseDaoAttribute.GetTableMappingAttribute(typeT);

            if (attribute == null)
            {
                return(null);
            }

            var sql  = $"SELECT *FROM dbo.{attribute.TableName} WHERE {exp}";
            var para = new Hashtable();

            para.Add("sql", sql);
            return(QueryForList <T>($"Query{attribute.TableName}", para));
        }
Ejemplo n.º 4
0
        public int CustomDelete <T>(List <int> ids)
        {
            //找到表名称
            var typeT = typeof(T);
            TableMappingAttribute attribute = BaseDaoAttribute.GetTableMappingAttribute(typeT);

            if (attribute == null)
            {
                return(-1);
            }

            var sql  = $@"DELETE FROM {attribute.TableName}  WHERE Id IN ({string.Join(",", ids)})";
            var para = new Hashtable();

            para.Add("sql", sql);
            return(this.Delete($"Delete{attribute.TableName}", para));
        }
        public static TableMappingAttribute GetTableMappingAttribute(Type t)
        {
            TableMappingAttribute tableAttribute = null;
            var tableMame  = string.Empty;
            var attributes = t.GetCustomAttributes(false);

            foreach (var attribute in attributes)
            {
                var tmp = attribute as Visitor.Reg.Common.Attributes.TableMappingAttribute;
                if (tmp != null)
                {
                    tableAttribute = new TableMappingAttribute(tmp.TableName, tmp.LikeQueryFields);
                    break;
                }
            }

            return(tableAttribute);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 获取数据检索结果
        /// </summary>
        /// <typeparam name="T">指定返回的数据类型</typeparam>
        /// <param name="param">执行参数中的SQL。Item1是获取数据SQL  item2是获取数据条数</param>
        /// <returns>返回结果模型</returns>
        public virtual ResultModel <T> GetDataRetrieveResult <T>(Tuple <string, string> param)
        {
            var typeT = typeof(T);
            TableMappingAttribute attribute = BaseDaoAttribute.GetTableMappingAttribute(typeT);

            ResultModel <T> result = new ResultModel <T>();
            var             para   = new Hashtable();

            LogHelper.Instance.Info(string.Format("获取数据SQL:{0}", param.Item1));
            para.Add("sql", param.Item1);
            result.data   = QueryForList <T>(string.Format("CommonDataQuery_{0}", attribute.TableName), para);
            result.length = queryBase.PageSize;
            para.Clear();

            para.Add("sql", param.Item2);
            result.recordsTotal = QueryForCount($"CommonDataQueryCount", para);
            return(result);
        }
Ejemplo n.º 7
0
        public T GetOne <T>(int id)
        {
            var typeT = typeof(T);

            TableMappingAttribute attribute = BaseDaoAttribute.GetTableMappingAttribute(typeT);

            if (attribute == null)
            {
                return(default(T));
            }



            var sql = $@"SELECT *FROM {attribute.TableName} where id={id} ";

            var param = new Hashtable();

            param.Add("sql", sql);
            return(QueryForObject <T>($"GetOne{attribute.TableName}", param));
        }
Ejemplo n.º 8
0
        public void CustomAdd <T>(T t, List <SystemObjectFields> fields)
        {
            //找到表名称
            var typeT = typeof(T);
            TableMappingAttribute attribute = BaseDaoAttribute.GetTableMappingAttribute(typeT);

            if (attribute == null)
            {
                return;
            }

            //新增用反射把各个字段赋值,动态创建插入语句
            List <string> list1      = new List <string>();
            List <string> list2      = new List <string>();
            var           properties = typeT.GetProperties();

            foreach (var propertiy in properties)
            {
                var tmp = fields.FirstOrDefault(c => c.FieldName == propertiy.Name && !c.PKConstraint);
                if (tmp != null)
                {
                    list1.Add($"[{propertiy.Name}]");
                    var propertyType = propertiy.PropertyType;
                    var newVal       = TypeConversionTool.ConvertToTarget(tmp.TypeName, propertyType, propertiy.GetValue(t));
                    var result       = string.IsNullOrEmpty(newVal.Item2) ? newVal.Item1 : "";
                    list2.Add($"'{result}'");
                }
            }
            string mainSql = string.Join(",", list1);
            var    subSql  = string.Join(",", list2);
            var    sql     = $@"INSERT INTO [dbo].{attribute.TableName}
                       ({mainSql})
                 VALUES
                       ({subSql})";

            var para = new Hashtable();

            para.Add("sql", sql);
            this.Add($"Add{attribute.TableName}", sql);
        }
Ejemplo n.º 9
0
        public void CustomUpdate <T>(T t, List <SystemObjectFields> fields)
        {
            //找到表名称
            var typeT = typeof(T);
            TableMappingAttribute attribute = BaseDaoAttribute.GetTableMappingAttribute(typeT);

            if (attribute == null)
            {
                return;
            }

            List <string> updateColumns = new List <string>();
            var           properties    = typeT.GetProperties();

            foreach (var propertiy in properties)
            {
                var tmp = fields.FirstOrDefault(c => c.FieldName == propertiy.Name && !c.PKConstraint);
                if (tmp != null)
                {
                    var propertyType = propertiy.PropertyType;
                    var str          = $"[{tmp.FieldName}]='{TypeConversionTool.ConvertToTarget(tmp.TypeName, propertyType, propertiy.GetValue(t)).Item1}'";
                    updateColumns.Add(str);
                }
            }

            var PKConstraintColumnName = fields.FirstOrDefault(c => c.PKConstraint);
            var pType = properties.FirstOrDefault(c => c.Name == PKConstraintColumnName.FieldName);
            var PKConstraintColumnValue = TypeConversionTool.ConvertToTarget(PKConstraintColumnName.TypeName, pType.PropertyType, pType.GetValue(t)).Item1;

            var sql  = $@" update {attribute.TableName} set {string.Join(",", updateColumns)} where {PKConstraintColumnName.FieldName}='{PKConstraintColumnValue}' ";
            var para = new Hashtable();

            para.Add("sql", sql);

            this.Update($"Update{attribute.TableName}", para);
        }
Ejemplo n.º 10
0
 public void Process(TypeDef type, TableMappingAttribute attribute) =>
 ProcessMappingName(type, attribute.Name, ValidationRule.Type);