Beispiel #1
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();
            }
        }
Beispiel #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));
        }
Beispiel #3
0
        public virtual T Clone <T>() where T : DbObject, new()
        {
            T local = DbObjectTools.Clone <T>(this);

            CloneUtils.CloneObject <DbObjectState>(this.State, local.State, new Expression <Func <DbObjectState, object> > [0]);
            CloneUtils.CloneObject <DbObjectState>(this.previousState, local.previousState, new Expression <Func <DbObjectState, object> > [0]);
            return(local);
        }
Beispiel #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));
        }
Beispiel #5
0
 public virtual void OnWrote(object sender, DbEventArgs e)
 {
     this.State.ObjectWrote = true;
     this.State.ModifiedValues.Clear();
     this.State.UpdateInitialValues(this);
     this.previousState              = DbObjectTools.Clone <DbObjectState>(this.State);
     e.Operator.TranscationRollback -= new EventHandler(this.OnTranscationRollback);
     e.Operator.TranscationRollback += new EventHandler(this.OnTranscationRollback);
     e.Operator.TranscationCommit   -= new EventHandler(this.OnTranscationCommit);
     e.Operator.TranscationCommit   += new EventHandler(this.OnTranscationCommit);
 }
Beispiel #6
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));
        }
Beispiel #7
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));
        }
Beispiel #8
0
        public void Write(IStorageObject obj)
        {
            FieldInfo[] fields    = DbObjectTools.GetFields(obj.GetType());
            string      tableName = DbObjectTools.GetTableName(obj.GetType());
            int         length    = fields.Length;

            for (int i = 0; i < length; i++)
            {
                DbParameterAttribute[] customAttributes = (DbParameterAttribute[])fields[i].GetCustomAttributes(typeof(DbParameterAttribute), true);
                if ((customAttributes != null) && (customAttributes.Length > 0))
                {
                    this.WriteField(tableName, fields[i], obj);
                }
            }
        }
Beispiel #9
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));
        }
Beispiel #10
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);
 }
Beispiel #11
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 }));
        }
Beispiel #12
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());
        }
Beispiel #13
0
 public virtual bool Compare(DbObject obj)
 {
     return(DbObjectTools.Compare(this, obj));
 }
Beispiel #14
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))));
        }
Beispiel #15
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 }));
        }