Exemple #1
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);
             }
         }
     }
 }
Exemple #2
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));
        }
Exemple #3
0
        public void Insert(IStorageObject obj)
        {
            DbEventArgs         args;
            string              insertSQL = this.SqlFormatter.GetInsertSQL(obj);
            Type                type      = obj.GetType();
            int                 num       = this.DbOperator.ExecuteScalar <int>(insertSQL);
            DynamicPropertyInfo autoIncrementDynamicPropertyInfo = DbObjectTools.GetAutoIncrementDynamicPropertyInfo(DbObjectTools.GetDbObjectInfo(type));

            if ((autoIncrementDynamicPropertyInfo != null) & (num != 0))
            {
                obj.SetValue(autoIncrementDynamicPropertyInfo.PropertyName, num);
            }
            Action item = delegate
            {
                args = new DbEventArgs(this, DbOperationAction.Insert);
                obj.OnWrote(this, args);
            };

            if (this.DbOperator.IsBeginTransaction)
            {
                this.transActions.Add(item);
            }
            else
            {
                item();
            }
        }
Exemple #4
0
        public bool RetrieveByKey(IStorageObject obj, object keyValue)
        {
            DynamicPropertyInfo pkDynamicPropertyInfo = DbObjectTools.GetPkDynamicPropertyInfo(DbObjectReflector.GetDbObjectInfo(obj.GetType()), true);

            if (pkDynamicPropertyInfo == null)
            {
                throw new ArgumentException("Primary Key Not Found");
            }
            return(this.Retrieve(obj, pkDynamicPropertyInfo.DataFieldName, keyValue));
        }
Exemple #5
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));
        }
Exemple #6
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);
        }
Exemple #7
0
 public void AddDynamicPropertyInfo(string propertyName, DynamicPropertyInfo info)
 {
     if (info != null)
     {
         if (this.dynamicPropertyInfos.ContainsKey(propertyName))
         {
             this.dynamicPropertyInfos[propertyName] = info;
         }
         else
         {
             this.dynamicPropertyInfos.Add(propertyName, info);
         }
     }
 }
Exemple #8
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);
        }
Exemple #9
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);
 }
Exemple #10
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 }));
        }
Exemple #11
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());
        }
Exemple #12
0
        public virtual string GetSelectSQLByKeyValue(Type type, object keyValue)
        {
            DynamicPropertyInfo pkDynamicPropertyInfo = DbObjectTools.GetPkDynamicPropertyInfo(DbObjectTools.GetDbObjectInfo(type), true);

            return(this.GetSelectSQL(type, string.Format("{0} = {1}", this.FormatFieldName(pkDynamicPropertyInfo.DataFieldName), this.FormatValue(keyValue, false))));
        }
Exemple #13
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 }));
        }