コード例 #1
0
        /// <summary>
        /// Saves (inserts/updates) an object based on the following criteria:
        /// 1) If autoincrement is set to true and the primary key is the default value, it inserts
        /// 2) If autoincrement is set to true and the primary key is not the default value, it updates
        /// 3) If autoincrement is set to false and the primary key is the default value, it inserts
        /// 4) If autoincrement is set to false and the primary key is not the default value,
        /// it does an Any call to see if the item is already in the database. If it is, it does an
        /// update. Otherwise it does an insert.
        /// On an insert, the primary key property is updated with the resulting value of the insert.
        /// </summary>
        /// <param name="Object">Object to save</param>
        /// <param name="Parameters">Extra parameters to be added to the insert/update function</param>
        public virtual void Save <PrimaryKeyType>(ClassType Object, params IParameter[] Parameters)
        {
            PrimaryKeyType PrimaryKeyVal = (PrimaryKeyType)GetPrimaryKey(Object);
            GenericEqualityComparer <PrimaryKeyType> Comparer = new GenericEqualityComparer <PrimaryKeyType>();

            if (Comparer.Equals(PrimaryKeyVal, default(PrimaryKeyType)))
            {
                PrimaryKeyVal = Insert <PrimaryKeyType>(Object, Parameters);
                PrimaryKeyMapping.CopyRightToLeft(PrimaryKeyVal, Object);
                return;
            }
            if (AutoIncrement)
            {
                Update(Object, Parameters);
                return;
            }
            Parameter <PrimaryKeyType> Param1 = new Parameter <PrimaryKeyType>(PrimaryKeyVal, PrimaryKey, ParameterStarter);
            ClassType TempVal = Any(PrimaryKey, null, null, Param1);

            if (TempVal == null)
            {
                PrimaryKeyVal = Insert <PrimaryKeyType>(Object, Parameters);
                PrimaryKeyMapping.CopyRightToLeft(PrimaryKeyVal, Object);
                return;
            }
            Update(Object, Parameters);
        }
コード例 #2
0
        /// <summary>
        /// 从属性加载表映射关系
        /// </summary>
        /// <param name="Class">实体类型</param>
        /// <returns></returns>
        public ITableMapping LoadTableMapping(Type Class)
        {
            object[] TAttrs = Class.GetCustomAttributes(typeof(TableMappingAttribute), false);
            if (TAttrs == null || TAttrs.Length == 0)
            {
                throw new MappingException("无法识别的实体类型[" + Class.FullName + "],找不到表映射关系");
            }
            TableMappingAttribute TblMappingAttr = TAttrs[0] as TableMappingAttribute;
            TableMapping          TabMapping     = new TableMapping();

            TabMapping.TypeName  = TblMappingAttr.TableName;
            TabMapping.TableName = TblMappingAttr.TableName;
            TabMapping.Generator = TblMappingAttr.KeyGenerator;
            TabMapping.IsTable   = TblMappingAttr.IsTable;

            //开始查找属性映射
            PropertyInfo[] Properties = Class.GetProperties();
            if (Properties != null && Properties.Length > 0)
            {
                foreach (PropertyInfo Property in Properties)
                {
                    object[] FAttrs = Property.GetCustomAttributes(typeof(FieldMappingAttribute), false);
                    if (FAttrs != null && FAttrs.Length > 0)
                    {
                        FieldMappingAttribute FMAttr = FAttrs[0] as FieldMappingAttribute;
                        FieldMapping          FM     = new FieldMapping();
                        FM.PropertyName = FMAttr.PropertyName;
                        FM.ColumnName   = FMAttr.ColumnName;
                        FM.DefauleValue = FMAttr.DefauleValue;
                        FM.FieldLength  = FMAttr.FieldLength;
                        FM.FieldType    = FMAttr.FieldType;
                        FM.IsNullable   = FMAttr.IsNullable;
                        FM.Remark       = FMAttr.Remark;
                        TabMapping.FieldMappingCollection.Add(FM);
                    }
                    object[] KeyAttrs = Property.GetCustomAttributes(typeof(PrimaryKeyMappingAttribute), false);
                    if (KeyAttrs != null && KeyAttrs.Length > 0)
                    {
                        PrimaryKeyMappingAttribute FMAttr = KeyAttrs[0] as PrimaryKeyMappingAttribute;
                        PrimaryKeyMapping          FM     = new PrimaryKeyMapping();
                        FM.PropertyName = FMAttr.PropertyName;
                        FM.ColumnName   = FMAttr.ColumnName;
                        FM.FieldLength  = FMAttr.FieldLength;
                        FM.FieldType    = FMAttr.FieldType;
                        FM.Remark       = FMAttr.Remark;
                        TabMapping.PrimaryKeyCollection.Add(FM);
                    }
                }
            }
            return(TabMapping);
        }
コード例 #3
0
ファイル: EntityMapping.cs プロジェクト: npenin/uss
        public void Initialize()
        {
            foreach (PrimaryKeyMapping pkm in _Ids)
            {
                // Browse any Business Id to map the field the data must be loaded from
                if (pkm.Generator.Name == GeneratorMapping.GeneratorType.business)
                {
                    foreach (AttributeMapping a in _Attributes)
                    {
                        if (a.Field == GetIdField(pkm))
                        {
                            ArrayList ps = new ArrayList();
                            ps.Add(new ParamMapping("name", a.Name));
                            ps.Add(new ParamMapping("dbType", a.DbType.ToString()));
                            ps.Add(new ParamMapping("size", a.Size.ToString()));
                            pkm.Generator.Params = (ParamMapping[])ps.ToArray(typeof(ParamMapping));
                        }
                    }
                }
                pkm.ParentEntity = this;
                pkm.Initialize();
            }
            if (!string.IsNullOrEmpty(_DiscriminatorField) && Ids[0].Generator.Name == GeneratorMapping.GeneratorType.business)
            {
                PrimaryKeyMapping discriminatorPkm = new PrimaryKeyMapping(_DiscriminatorField, this);
                _Ids.Add(discriminatorPkm);
                discriminatorPkm.Generator = new GeneratorMapping();
                discriminatorPkm.Generator.Name = GeneratorMapping.GeneratorType.business;
                ArrayList ps = new ArrayList();
                ps.Add(new ParamMapping("dbType", DbType.AnsiString.ToString()));
                ps.Add(new ParamMapping("size", "255"));
                discriminatorPkm.Generator.Params = (ParamMapping[])ps.ToArray(typeof(ParamMapping));
                discriminatorPkm.Initialize();
            }

            foreach (AttributeMapping a in _Attributes)
            {
                a.ParentEntity = this;
                a.Initialize();
            }

            foreach (ReferenceMapping r in _References)
            {
                r.EntityParent = this;
                r.Initialize();
            }
        }
コード例 #4
0
        /// <summary>
        /// 把字符串格式转化成此格式
        /// </summary>
        /// <param name="stuExpress"></param>
        /// <returns></returns>
        public static PrimaryKeyMapping ParseTo(string stuExpress)
        {
            if (String.IsNullOrEmpty(stuExpress))
            {
                return(null);
            }

            PrimaryKeyMapping mapping = new PrimaryKeyMapping();

            string[] atts = stuExpress.Split(',');
            if (atts == null || atts.Length == 0)
            {
                return(mapping);
            }
            foreach (string att in atts)
            {
                if (att.Length == 0)
                {
                    continue;
                }
                string[] nmVal = att.Split('=');
                if (nmVal == null || nmVal.Length != 2)
                {
                    continue;
                }
                string name = nmVal[0].Trim().ToUpper();
                string val  = nmVal[1].Trim();
                switch (name)
                {
                case "PROPERTYNAME": mapping.PropertyName = val; break;

                case "COLUMNNAME": mapping.ColumnName = val; break;

                case "FIELDTYPE": mapping.FieldType = Utility.ParseFieldType(val); break;

                case "FIELDLENGTH": { int l = 0; Int32.TryParse(val, out l); mapping.FieldLength = l; break; }

                case "REMARK": mapping.Remark = val; break;
                }
            }
            return(mapping);
        }
コード例 #5
0
        /// <summary>
        /// Saves (inserts/updates) an object based on the following criteria:
        /// 1) If autoincrement is set to true and the primary key is the default value, it inserts
        /// 2) If autoincrement is set to true and the primary key is not the default value, it updates
        /// 3) If autoincrement is set to false and the primary key is the default value, it inserts
        /// 4) If autoincrement is set to false and the primary key is not the default value,
        /// it does an Any call to see if the item is already in the database. If it is, it does an
        /// update. Otherwise it does an insert.
        /// On an insert, the primary key property is updated with the resulting value of the insert.
        /// </summary>
        /// <param name="Object">Object to save</param>
        /// <param name="Parameters">Extra parameters to be added to the insert/update function</param>
        public virtual void Save <PrimaryKeyType>(ClassType Object, params IParameter[] Parameters)
        {
            PrimaryKeyType PrimaryKeyVal = GetPrimaryKey(Object).TryTo(default(PrimaryKeyType));
            GenericEqualityComparer <PrimaryKeyType> Comparer = new GenericEqualityComparer <PrimaryKeyType>();

            if (Comparer.Equals(PrimaryKeyVal, default(PrimaryKeyType)))
            {
                PrimaryKeyVal = Insert <PrimaryKeyType>(Object, Parameters);
                PrimaryKeyMapping.CopyRightToLeft(PrimaryKeyVal, Object);
                return;
            }
            if (AutoIncrement)
            {
                Update(Object, Parameters);
                return;
            }
            IParameter Param1 = null;

            if (typeof(PrimaryKeyType).IsOfType(typeof(string)))
            {
                Param1 = new StringEqualParameter(PrimaryKeyVal.ToString(), PrimaryKey, -1, ParameterStarter);
            }
            else
            {
                Param1 = new EqualParameter <PrimaryKeyType>(PrimaryKeyVal, PrimaryKey, ParameterStarter);
            }
            ClassType TempVal = Any(PrimaryKey, null, null, Param1);

            if (TempVal == null)
            {
                PrimaryKeyVal = Insert <PrimaryKeyType>(Object, Parameters);
                PrimaryKeyMapping.CopyRightToLeft(PrimaryKeyVal, Object);
                return;
            }
            Update(Object, Parameters);
        }
コード例 #6
0
		public bool Contains(PrimaryKeyMapping value)
		{
			return base.List.Contains( (object)value);
		}
コード例 #7
0
		public void Insert(int index, PrimaryKeyMapping value)
		{
			base.List.Insert(index, (object)value);
		}
コード例 #8
0
		public void Remove(PrimaryKeyMapping value)
		{
			base.List.Remove( (object)value);
		}
コード例 #9
0
		public int Add(PrimaryKeyMapping value)
		{
			return base.List.Add(value as object);
		}
コード例 #10
0
ファイル: EntityMapping.cs プロジェクト: npenin/uss
 public string GetIdFieldAs(PrimaryKeyMapping pk)
 {
     return string.Concat(PREFIX_ID, pk.Field);
 }
コード例 #11
0
ファイル: EntityMapping.cs プロジェクト: npenin/uss
 public string GetIdField(PrimaryKeyMapping pk)
 {
     return pk.Field;
 }
コード例 #12
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="stuExpress"></param>
 public PrimaryKeyMappingAttribute(string stuExpress)
 {
     this.m_Mapping = ParseTo(stuExpress);
 }