예제 #1
0
        public virtual string GetUpdateSQL(IStorageObject obj)
        {
            StringBuilder builder      = new StringBuilder();
            DbObjectInfo  dbObjectInfo = DbObjectTools.GetDbObjectInfo(obj.GetType());

            this.CheckIsView(dbObjectInfo);
            DynamicPropertyInfo pkDynamicPropertyInfo = DbObjectTools.GetPkDynamicPropertyInfo(dbObjectInfo, true);

            string[] source = obj.ModifiedValues.Keys.ToArray <string>();
            foreach (KeyValuePair <string, DynamicPropertyInfo> pair in dbObjectInfo.DynamicPropertyInfos)
            {
                string key = pair.Key;
                DynamicPropertyInfo info3 = pair.Value;
                if ((!info3.PrimaryKey && !info3.ReadOnly) && ((!info3.AutoIncrement && (info3.DefaultValue == null)) && source.Contains <string>(key)))
                {
                    object obj2 = obj.GetValue(info3.PropertyName);
                    string str2 = this.FormatFieldName(info3.DataFieldName);
                    if (builder.Length > 0)
                    {
                        builder.Append(",");
                    }
                    builder.AppendFormat("{0} = {1}", str2, this.FormatValue(obj2, info3.AllowDBNull));
                }
            }
            object obj3 = obj.GetValue(pkDynamicPropertyInfo.PropertyName);
            string str3 = this.FormatFieldName(pkDynamicPropertyInfo.DataFieldName) + " = " + this.FormatValue(obj3, false);

            return(string.Format(this.GetUpdateSqlTemplate(), this.FormatTableOrViewName(dbObjectInfo.TableName), builder.ToString(), str3));
        }
예제 #2
0
 private void Read(IStorageObject obj, IDataReader reader)
 {
     if ((obj != null) && (reader != null))
     {
         Type         type         = obj.GetType();
         DbObjectInfo dbObjectInfo = DbObjectReflector.GetDbObjectInfo(type);
         if (dbObjectInfo == null)
         {
             throw new Exception("Cannot retrieve DbObjectInfo of type : " + type.ToString());
         }
         foreach (KeyValuePair <string, DynamicPropertyInfo> pair in dbObjectInfo.DynamicPropertyInfos)
         {
             string key = pair.Key;
             DynamicPropertyInfo info2 = pair.Value;
             try
             {
                 ConvertUtils.SetValueToObject(reader[info2.DataFieldName], obj, key, info2.PropertyType);
             }
             catch (Exception exception)
             {
                 throw new ConvertValueException(info2.DataFieldName, exception);
             }
         }
     }
 }
예제 #3
0
 private void CheckIsView(DbObjectInfo dbObjectInfo)
 {
     if (!(!string.IsNullOrEmpty(dbObjectInfo.TableName) || string.IsNullOrEmpty(dbObjectInfo.QueryTable)))
     {
         throw new Exception("Cannot modify a view: " + dbObjectInfo.QueryTable);
     }
 }
예제 #4
0
        private DbObjectInfo GetDbObjectInfo(Type type)
        {
            DbObjectInfo dbObjectInfo = DbObjectReflector.GetDbObjectInfo(type);

            if (dbObjectInfo == null)
            {
                throw new Exception("Cannot retrieve DbObjectInfo of type : " + type.ToString());
            }
            return(dbObjectInfo);
        }
예제 #5
0
        public static string GetViewOrTableName(Type type)
        {
            DbObjectInfo dbObjectInfo = GetDbObjectInfo(type);

            if (!string.IsNullOrEmpty(dbObjectInfo.QueryTable))
            {
                return(dbObjectInfo.QueryTable);
            }
            return(dbObjectInfo.TableName);
        }
예제 #6
0
        public static string GetTableName(Type type)
        {
            DbObjectInfo dbObjectInfo = GetDbObjectInfo(type);

            if (dbObjectInfo.TableName == null)
            {
                return(null);
            }
            return(dbObjectInfo.TableName);
        }
예제 #7
0
        public static string GetQueryName(Type type)
        {
            DbObjectInfo dbObjectInfo = GetDbObjectInfo(type);

            if (dbObjectInfo.QueryTable == null)
            {
                return(null);
            }
            return(dbObjectInfo.QueryTable);
        }
예제 #8
0
        public virtual string GetDeleteSQL(IStorageObject obj)
        {
            DbObjectInfo dbObjectInfo = DbObjectTools.GetDbObjectInfo(obj.GetType());

            this.CheckIsView(dbObjectInfo);
            DynamicPropertyInfo pkDynamicPropertyInfo = DbObjectTools.GetPkDynamicPropertyInfo(dbObjectInfo, true);
            object obj2 = obj.GetValue(pkDynamicPropertyInfo.PropertyName);
            string str  = string.Format(" where {0} = {1}", this.FormatFieldName(pkDynamicPropertyInfo.DataFieldName), this.FormatValue(obj2, false));

            return(string.Format(this.GetDeleteSqlTemplate(), this.FormatTableOrViewName(dbObjectInfo.TableName), str));
        }
예제 #9
0
 public static void Register(Type type, DbObjectInfo info)
 {
     if (structures.ContainsKey(type))
     {
         structures[type] = info;
     }
     else
     {
         structures.Add(type, info);
     }
 }
예제 #10
0
        public static DynamicPropertyInfo GetPkDynamicPropertyInfo(DbObjectInfo dbObjectInfo, bool checkExist = true)
        {
            DynamicPropertyInfo info = (from inf in dbObjectInfo.DynamicPropertyInfos
                                        where inf.Value.PrimaryKey
                                        select inf.Value).SingleOrDefault <DynamicPropertyInfo>();

            if ((info == null) & checkExist)
            {
                throw new Exception("Cannot find primary key property of table (or view): " + dbObjectInfo.TableName);
            }
            return(info);
        }
예제 #11
0
        public virtual string GetQueryCountSQL <T>(string condition = null) where T : IStorageObject
        {
            string       str          = "";
            Type         type         = typeof(T);
            DbObjectInfo dbObjectInfo = DbObjectTools.GetDbObjectInfo(type);

            if (!string.IsNullOrEmpty(condition))
            {
                str = string.Format(" where {0}", condition);
            }
            string tableOrViewName = dbObjectInfo.QueryTable ?? dbObjectInfo.TableName;

            return(string.Format(this.GetSelectCountTemplate(), this.FormatTableOrViewName(tableOrViewName), str));
        }
예제 #12
0
        public virtual string GetDeleteSQL <T>(string condition) where T : IStorageObject
        {
            Type         type         = typeof(T);
            DbObjectInfo dbObjectInfo = DbObjectTools.GetDbObjectInfo(type);

            this.CheckIsView(dbObjectInfo);
            if (string.IsNullOrEmpty(condition))
            {
                condition = "";
            }
            else
            {
                condition = "where " + condition;
            }
            return(string.Format(this.GetDeleteSqlTemplate(), this.FormatTableOrViewName(dbObjectInfo.TableName), condition));
        }
예제 #13
0
        public static string GetDataFieldName(Type type, PropertyInfo property)
        {
            DbObjectInfo dbObjectInfo = GetDbObjectInfo(type);

            if (dbObjectInfo.DynamicPropertyInfos != null)
            {
                DynamicPropertyInfo info2 = (from t in dbObjectInfo.DynamicPropertyInfos.Values
                                             where t.PropertyName.Equals(property.Name)
                                             select t).FirstOrDefault <DynamicPropertyInfo>();
                if (info2 != null)
                {
                    return(info2.DataFieldName);
                }
            }
            return(null);
        }
예제 #14
0
        public void UpdateInitialValues(DbObject obj)
        {
            this.initialValues.Clear();
            DbObjectInfo dbObjectInfo = this.GetDbObjectInfo(obj.GetType());

            foreach (KeyValuePair <string, DynamicPropertyInfo> pair in dbObjectInfo.DynamicPropertyInfos)
            {
                string    key   = pair.Key;
                object    obj2  = obj.GetValue(key);
                ValueInfo info2 = new ValueInfo {
                    Value = obj2,
                    Name  = key
                };
                this.initialValues.Add(key, info2);
            }
        }
예제 #15
0
 public static DbObjectInfo GetDbObjectInfo(Type type)
 {
     if (structures.ContainsKey(type))
     {
         return(structures[type]);
     }
     object[] customAttributes = type.GetCustomAttributes(true);
     if (customAttributes != null)
     {
         DataTableAttribute attribute = (from a in customAttributes
                                         where a is DataTableAttribute
                                         select a).Cast <DataTableAttribute>().FirstOrDefault <DataTableAttribute>();
         QueryTableAttribute attribute2 = (from a in customAttributes
                                           where a is QueryTableAttribute
                                           select a).Cast <QueryTableAttribute>().FirstOrDefault <QueryTableAttribute>();
         if ((attribute == null) && (attribute2 == null))
         {
             return(null);
         }
         DbObjectInfo info = new DbObjectInfo();
         if (attribute != null)
         {
             info.TableName    = attribute.TableName;
             info.DatabaseName = attribute.DatabaseName;
         }
         if (attribute2 != null)
         {
             info.QueryTable = attribute2.TableName;
         }
         PropertyInfo[] properties = type.GetProperties();
         foreach (PropertyInfo info2 in properties)
         {
             DataFieldAttribute dataFieldAttribute = DbObjectTools.GetDataFieldAttribute(info2);
             if (dataFieldAttribute != null)
             {
                 DynamicPropertyInfo des = new DynamicPropertyInfo();
                 CloneUtils.CloneObject(dataFieldAttribute, des, new string[0]);
                 des.PropertyName = info2.Name;
                 des.PropertyType = info2.PropertyType;
                 info.AddDynamicPropertyInfo(des.PropertyName, des);
             }
         }
         Register(type, info);
         return(info);
     }
     return(null);
 }
예제 #16
0
        public virtual string GetSelectSQL(Type type, string condition)
        {
            DbObjectInfo  dbObjectInfo = DbObjectTools.GetDbObjectInfo(type);
            StringBuilder builder      = new StringBuilder();
            StringBuilder builder2     = new StringBuilder();
            List <string> list         = new List <string>();

            foreach (KeyValuePair <string, DynamicPropertyInfo> pair in dbObjectInfo.DynamicPropertyInfos)
            {
                string key = pair.Key;
                DynamicPropertyInfo info2 = pair.Value;
                string str2 = this.FormatTableOrViewName(!string.IsNullOrEmpty(dbObjectInfo.QueryTable) ? dbObjectInfo.QueryTable : dbObjectInfo.TableName);
                string str3 = this.FormatFieldName(info2.DataFieldName);
                if (builder.Length > 0)
                {
                    builder.Append(",");
                }
                builder.AppendFormat("{0}.{1}", str2, str3);
                if (!string.IsNullOrEmpty(info2.JoinTableName))
                {
                    string tableName = dbObjectInfo.TableName;
                    string str5      = this.FormatFieldName(info2.DataFieldName);
                    string str6      = this.FormatTableOrViewName(info2.JoinTableName);
                    if (!string.IsNullOrEmpty(info2.JoinDatabaseName))
                    {
                        str6 = this.FormatDatabaseName(info2.JoinDatabaseName) + ".." + str6;
                    }
                    string str7 = this.FormatFieldName(info2.JoinOnFieldName);
                    string item = string.Format(" {0} {1} join {2} on {3} = {4} ", new object[] { info2.RightJoin ? "Right" : (info2.OuterJoin ? "Left" : ""), info2.OuterJoin ? "Outer" : "", str6, str6 + "." + str7, tableName + "." + str5 });
                    if (!list.Contains(item))
                    {
                        builder2.Append(item);
                        list.Add(item);
                    }
                }
            }
            string str9 = this.FormatTableOrViewName(!string.IsNullOrEmpty(dbObjectInfo.QueryTable) ? dbObjectInfo.QueryTable : dbObjectInfo.TableName);

            if (!string.IsNullOrEmpty(dbObjectInfo.DatabaseName))
            {
                str9 = this.FormatDatabaseName(dbObjectInfo.DatabaseName) + ".." + str9;
            }
            return(string.Format(this.GetSelectSqlTemplate(), new object[] { builder.ToString(), str9, builder2.ToString(), condition }));
        }
예제 #17
0
        internal static bool Compare(IStorageObject obj1, IStorageObject obj2)
        {
            if (!obj1.GetType().Equals(obj2.GetType()))
            {
                return(false);
            }
            DbObjectInfo dbObjectInfo = DbObjectReflector.GetDbObjectInfo(obj1.GetType());

            foreach (KeyValuePair <string, DynamicPropertyInfo> pair in dbObjectInfo.DynamicPropertyInfos)
            {
                object obj3 = obj1.GetValue(pair.Value.PropertyName);
                object obj4 = obj2.GetValue(pair.Value.PropertyName);
                if (obj3 != obj4)
                {
                    return(false);
                }
            }
            return(true);
        }
예제 #18
0
        public virtual string GetInsertSQL(IStorageObject obj)
        {
            DbObjectInfo dbObjectInfo = DbObjectTools.GetDbObjectInfo(obj.GetType());

            this.CheckIsView(dbObjectInfo);
            StringBuilder       builder  = new StringBuilder();
            StringBuilder       builder2 = new StringBuilder();
            StringBuilder       builder3 = new StringBuilder();
            DynamicPropertyInfo autoIncrementDynamicPropertyInfo = DbObjectTools.GetAutoIncrementDynamicPropertyInfo(dbObjectInfo);

            foreach (KeyValuePair <string, DynamicPropertyInfo> pair in dbObjectInfo.DynamicPropertyInfos)
            {
                string key = pair.Key;
                DynamicPropertyInfo info3 = pair.Value;
                if ((!info3.AutoIncrement && !info3.ReadOnly) && (info3.DefaultValue == null))
                {
                    object obj2 = obj.GetValue(info3.PropertyName);
                    string str2 = this.FormatFieldName(info3.DataFieldName);
                    if (builder3.Length > 0)
                    {
                        builder3.Append(",");
                    }
                    if (builder2.Length > 0)
                    {
                        builder2.Append(",");
                    }
                    builder3.Append(str2);
                    builder2.Append(this.FormatValue(obj2, info3.AllowDBNull));
                }
            }
            builder.AppendFormat(this.GetInsertSqlTemplate(), this.FormatTableOrViewName(dbObjectInfo.TableName), builder3.ToString(), builder2.ToString());
            if (autoIncrementDynamicPropertyInfo != null)
            {
                builder.Append("select CAST(SCOPE_IDENTITY() AS INT);");
            }
            return(builder.ToString());
        }
예제 #19
0
 public static DynamicPropertyInfo GetAutoIncrementDynamicPropertyInfo(DbObjectInfo dbObjectInfo)
 {
     return((from info in dbObjectInfo.DynamicPropertyInfos
             where info.Value.AutoIncrement
             select info.Value).FirstOrDefault <DynamicPropertyInfo>());
 }
예제 #20
0
        public virtual string GetQueryObjectsSQL <T>(string condition, int rowCount, int pageIndex, params OrderBy[] orderBys) where T : IStorageObject
        {
            StringBuilder builder      = new StringBuilder();
            StringBuilder builder2     = new StringBuilder();
            StringBuilder builder3     = new StringBuilder();
            List <string> list         = new List <string>();
            string        str          = "";
            Type          type         = typeof(T);
            DbObjectInfo  dbObjectInfo = DbObjectTools.GetDbObjectInfo(type);
            int           num          = (rowCount == 0x7fffffff) ? 1 : ((pageIndex * rowCount) + 1);
            int           num2         = 0x7fffffff;

            if ((rowCount != 0x7fffffff) && (rowCount != 0))
            {
                num2 = (num + rowCount) - 1;
            }
            if ((orderBys != null) && (orderBys.Length > 0))
            {
                builder.Append(" order by ");
                for (int i = 0; i < orderBys.Length; i++)
                {
                    if (i != 0)
                    {
                        builder.Append(", ");
                    }
                    builder.Append(this.FormatFieldName(orderBys[i].Column));
                    if (orderBys[i].Desc)
                    {
                        builder.Append(" DESC");
                    }
                }
            }
            else
            {
                DynamicPropertyInfo pkDynamicPropertyInfo = DbObjectTools.GetPkDynamicPropertyInfo(dbObjectInfo, false);
                if (pkDynamicPropertyInfo != null)
                {
                    builder.AppendFormat(" order by {0}", this.FormatFieldName(pkDynamicPropertyInfo.DataFieldName));
                }
                else
                {
                    builder.Append(" order by Rand() ");
                }
            }
            foreach (KeyValuePair <string, DynamicPropertyInfo> pair in dbObjectInfo.DynamicPropertyInfos)
            {
                string key = pair.Key;
                DynamicPropertyInfo info3 = pair.Value;
                string str3 = this.FormatTableOrViewName(!string.IsNullOrEmpty(dbObjectInfo.QueryTable) ? dbObjectInfo.QueryTable : dbObjectInfo.TableName);
                string str4 = this.FormatFieldName(info3.DataFieldName);
                if (builder2.Length > 0)
                {
                    builder2.Append(",");
                }
                builder2.AppendFormat("{0}.{1}", str3, str4);
                if (!string.IsNullOrEmpty(info3.JoinTableName))
                {
                    string str5 = this.FormatTableOrViewName(dbObjectInfo.TableName);
                    string str6 = this.FormatFieldName(info3.DataFieldName);
                    string str7 = this.FormatTableOrViewName(info3.JoinTableName);
                    if (!string.IsNullOrEmpty(info3.JoinDatabaseName))
                    {
                        str7 = this.FormatDatabaseName(info3.JoinDatabaseName) + ".." + str7;
                    }
                    string str8 = this.FormatFieldName(info3.JoinOnFieldName);
                    string item = string.Format(" {0} {1} join {2} on {3} = {4} ", new object[] { info3.RightJoin ? "Right" : (info3.OuterJoin ? "Left" : ""), info3.OuterJoin ? "Outer" : "", str7, str7 + "." + str8, str5 + "." + str6 });
                    if (!list.Contains(item))
                    {
                        builder3.Append(item);
                        list.Add(item);
                    }
                }
            }
            string str10 = this.FormatTableOrViewName(!string.IsNullOrEmpty(dbObjectInfo.QueryTable) ? dbObjectInfo.QueryTable : dbObjectInfo.TableName);

            if (!string.IsNullOrEmpty(dbObjectInfo.DatabaseName))
            {
                str10 = this.FormatDatabaseName(dbObjectInfo.DatabaseName) + ".." + str10;
            }
            if (!string.IsNullOrEmpty(condition))
            {
                str = string.Format(" where {0}", condition);
            }
            return(string.Format(this.GetSelectSqlWithPagingTemplate(), new object[] { builder2.ToString(), builder.ToString(), str10, builder3.ToString(), str, num, num2 }));
        }