Esempio n. 1
0
 /// <summary>
 /// Gets the column names.the key is ColumnName and the value is PropertyName.
 /// </summary>
 public static ConcurrentDictionary <string, string> GetColumn2PropNameDics(Data.DirverType dirverType, Type modelType)
 {
     return(MemcacheHelper <ConcurrentDictionary <string, string> > .ReadAndWrite("[GetColumn2PropNameDics][" + modelType.FullName + "][" + dirverType + "]", () =>
     {
         ConcurrentDictionary <string, string> dic = new ConcurrentDictionary <string, string>();
         var props = modelType.GetProperties().ToList();
         foreach (var prop in props)
         {
             if (!prop.CanRead || !prop.CanWrite)
             {
                 continue;
             }
             var attrs = prop.GetCustomAttributes(typeof(Common.Attribute.ColumnAttribute), true);
             if (attrs == null)
             {
                 dic.TryAdd(prop.Name, prop.Name); continue;
             }
             var lstAttrs = attrs.ToList().Select(a => (Common.Attribute.ColumnAttribute)a);
             var targetAttr = lstAttrs.FirstOrDefault(a => a.DirverType == dirverType);
             if (targetAttr == null)
             {
                 targetAttr = lstAttrs.FirstOrDefault(a => a.DirverType == Data.DirverType.UnKnown);
             }
             if (targetAttr != null && !string.IsNullOrWhiteSpace(targetAttr.ColumnName))
             {
                 dic[targetAttr.ColumnName] = prop.Name;
             }
             else
             {
                 dic[prop.Name] = prop.Name;
             }
         }
         return dic;
     }));
 }
Esempio n. 2
0
 /// <summary>
 /// Gets the ignore columns but only with can read and can write property.
 /// </summary>
 public static List <string> GetIgnorePropertys(Type modelType)
 {
     return(MemcacheHelper <List <string> > .ReadAndWrite("[GetIgnorePropertys][" + modelType.FullName + "]", () =>
     {
         List <string> lst = new List <string>();
         var props = modelType.GetProperties().ToList();
         foreach (var prop in props)
         {
             if (!prop.CanRead || !prop.CanWrite)
             {
                 continue;
             }
             var attrs = prop.GetCustomAttributes(typeof(Common.Attribute.IgnoreAttribute), true);
             if (attrs == null)
             {
                 continue;
             }
             var lstAttrs = attrs.ToList().ConvertToAll(a => (Common.Attribute.IgnoreAttribute)a);
             var targetAttr = lstAttrs.FirstOrDefault();
             if (targetAttr != null && targetAttr.IsIgnore)
             {
                 lst.Add(prop.Name);
             }
         }
         return lst;
     }));
 }
Esempio n. 3
0
        /// <summary>
        /// Gets the name of the table. with schemaName.tableName, if you set safeNameFunc it can return like `schemaName`.`tableName` on mysql.
        /// </summary>
        public static string GetTableName(Type modelType, bool isWithSchemaName, Func <string, string> safeNameFunc = null)
        {
            if (safeNameFunc == null)
            {
                safeNameFunc = (name) => name;
            }
            var tup = MemcacheHelper <Attribute.TableAttribute> .ReadAndWrite("[GetTableName][" + modelType.FullName + "][" + isWithSchemaName + "]", () =>
            {
                var tableAttr = modelType.GetCustomAttributes(typeof(Common.Attribute.TableAttribute), true);
                if (tableAttr == null || tableAttr.Length == 0)
                {
                    return new Attribute.TableAttribute()
                    {
                        TableName = modelType.Name
                    }
                }
                ;
                var tabAttr = tableAttr[0] as Common.Attribute.TableAttribute;
                if (string.IsNullOrWhiteSpace(tabAttr.TableName))
                {
                    tabAttr.TableName = modelType.Name;
                }
                if (isWithSchemaName && !string.IsNullOrWhiteSpace(tabAttr.SchemaName))
                {
                    return new Attribute.TableAttribute()
                    {
                        SchemaName = tabAttr.SchemaName, TableName = tabAttr.TableName
                    }
                }
                ;
                return(new Attribute.TableAttribute {
                    TableName = tabAttr.TableName
                });
            });

            if (tup.SchemaName == null)
            {
                return(safeNameFunc(tup.TableName));
            }
            else
            {
                return(string.Format("{0}.{1}", safeNameFunc(tup.SchemaName), safeNameFunc(tup.TableName)));
            }
        }
Esempio n. 4
0
 /// <summary>
 /// Gets the create ddl script. the key is PropertyName and the value is CreateDdl.
 /// </summary>
 public static ConcurrentDictionary <string, string> GetPropertyName2DDLs(DirverType dirverType, Type modelType, Func <Type, int, string> typeMappingFunc)
 {
     return(MemcacheHelper <ConcurrentDictionary <string, string> > .ReadAndWrite("[GetPropertyName2DDLs][" + modelType.FullName + "][" + dirverType + "]", () =>
     {
         ConcurrentDictionary <string, string> dic = new ConcurrentDictionary <string, string>();
         var props = modelType.GetProperties().ToList();
         var ignoreProperys = GetIgnorePropertys(modelType);
         foreach (var prop in props)
         {
             if (!prop.CanRead || !prop.CanWrite)
             {
                 continue;
             }
             if (ignoreProperys.Contains(prop.Name))
             {
                 continue;
             }
             var attrs = prop.GetCustomAttributes(typeof(Common.Attribute.ColumnAttribute), true);
             if (attrs == null)
             {
                 dic.TryAdd(prop.Name, prop.Name); continue;
             }
             var lstAttrs = attrs.ToList().ConvertToAll(a => (Common.Attribute.ColumnAttribute)a);
             var targetAttr = lstAttrs.FirstOrDefault(a => a.DirverType == dirverType);
             if (targetAttr == null)
             {
                 targetAttr = lstAttrs.FirstOrDefault(a => a.DirverType == Data.DirverType.UnKnown);
             }
             if (targetAttr != null && !string.IsNullOrWhiteSpace(targetAttr.CreateDDL))
             {
                 dic.TryAdd(prop.Name, targetAttr.CreateDDL);
             }
             else if (typeMappingFunc != null)
             {
                 dic.TryAdd(prop.Name, typeMappingFunc(prop.PropertyType, 0));
             }
         }
         return dic;
     }));
 }
Esempio n. 5
0
 /// <summary>
 /// Gets the primary keys.
 /// </summary>
 public static List <string> GetPrimaryKeys(Data.DirverType dirverType, Type modelType, bool isColumnNameOrPropertyName = true)
 {
     return(MemcacheHelper <List <string> > .ReadAndWrite("[GetPrimaryKeys][" + modelType.FullName + "][" + dirverType + "][" + isColumnNameOrPropertyName + "]", () =>
     {
         List <string> lst = new List <string>();
         var props = modelType.GetProperties().ToList();
         foreach (var prop in props)
         {
             if (!prop.CanRead || !prop.CanWrite)
             {
                 continue;
             }
             var attrs = prop.GetCustomAttributes(typeof(Common.Attribute.ColumnAttribute), true);
             if (attrs == null)
             {
                 continue;
             }
             var lstAttrs = attrs.ToList().ConvertToAll(a => (Common.Attribute.ColumnAttribute)a);
             var targetAttr = lstAttrs.FirstOrDefault(a => a.DirverType == dirverType);
             if (targetAttr == null)
             {
                 targetAttr = lstAttrs.FirstOrDefault(a => a.DirverType == Data.DirverType.UnKnown);
             }
             if (targetAttr != null && targetAttr.PrimaryKey)
             {
                 if (isColumnNameOrPropertyName && !string.IsNullOrWhiteSpace(targetAttr.ColumnName))
                 {
                     lst.Add(targetAttr.ColumnName);
                 }
                 else
                 {
                     lst.Add(prop.Name);
                 }
             }
         }
         return lst;
     }));
 }