示例#1
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);
                }
            }
        }
示例#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));
        }
示例#3
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);
             }
         }
     }
 }
示例#4
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();
            }
        }
示例#5
0
        public void DeleteData(IStorageObject storageObject)
        {
            var data = Data.FirstOrDefault(x => x != null && x.GetType() == storageObject.GetType() && x.DataId == storageObject.DataId);

            if (data != null)
            {
                Data.Remove(data);
            }
        }
示例#6
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);
        }
示例#7
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));
        }
示例#8
0
        public bool Retrieve(IStorageObject obj, string keyName, object keyValue)
        {
            bool   flag = false;
            string sql  = this.SqlFormatter.GetSelectSQL(obj.GetType(), keyName, keyValue);

            Debug.WriteLine("SelectSQL: " + sql);
            using (IDataReader reader = this.DbOperator.Query(sql))
            {
                if (reader.Read())
                {
                    this.Read(obj, reader);
                    DbEventArgs e = new DbEventArgs(this, DbOperationAction.Select);
                    obj.OnRead(this, e);
                    flag = true;
                }
            }
            return(flag);
        }
示例#9
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());
        }
示例#10
0
        public void SaveOrUpdateData(IStorageObject storageObject)
        {
            var data = Data.FirstOrDefault(x => x.DataId == storageObject.DataId && x.GetType() == storageObject.GetType());

            if (data != null)
            {
                Data.Remove(data);
            }

            Data.Add(storageObject);
        }
示例#11
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));
        }