示例#1
0
        public int Update(Expression <Func <object> > expression)
        {
            HashSet <string> props = ExpressionHelper.GetExpression_PropertyList(expression);

            if (props.Count == 0)
            {
                throw new ObjectMappingException("not any property updated!");
            }

            object obj     = expression.Compile()();
            Type   objType = obj.GetType();

            foreach (var prop in props)
            {
                var           val    = objType.GetProperty(prop).GetValue(obj, null);
                ColumnMapping column = TableMapping.GetColumnMappingByProperty(prop);
                this.UpdateParameters.Add(new SqlParameter(column != null ? column.Name : prop, val));
            }

            DatabaseEngine db = DatabaseEngineFactory.GetDatabaseEngine();

            return(db.Update(this));
        }
        public virtual int Update(TableMapping table, object obj, HashSet <string> updateprops)
        {
            Criteria criteria = new Criteria(table);

            criteria.AddEqualTo(table.ColumnPK.Name, table.ColumnPK.GetValue(obj));
            if ((updateprops != null) && (updateprops.Count > 0))
            {
                foreach (string propname in updateprops)
                {
                    ColumnMapping column = table.GetColumnMappingByProperty(propname);
                    if (column == null)
                    {
                        continue;
                    }

                    if ((column.IsPK == false) && (column.IsAutoIncrement == false))
                    {
                        criteria.AddUpdateColumn(column.Name, column.GetValue(obj));
                    }
                    else
                    {
                        throw new ObjectMappingException(propname);
                    }
                }
            }
            else
            {
                foreach (ColumnMapping column in table.Columns)
                {
                    if ((column.IsPK == false) && (column.IsAutoIncrement == false))
                    {
                        criteria.AddUpdateColumn(column.Name, column.GetValue(obj));
                    }
                }
            };
            return(this.Update(criteria));
        }
        public void Load(Type objectType, IList list, IDataReader dr)
        {
            if (objectType.IsPrimitive || (objectType == typeof(string)))
            {
                while (dr.Read())
                {
                    object val = dr[0];
                    if (val == DBNull.Value)
                    {
                        if (objectType == typeof(string))
                        {
                            list.Add(null);
                        }
                        else
                        {
                            list.Add(Activator.CreateInstance(objectType));
                        }
                    }
                    else
                    {
                        list.Add(Convert.ChangeType(val, objectType));
                    }
                }
            }
            else
            {
                int           fieldcount = dr.FieldCount;
                List <string> fieldnames = new List <string>();
                for (int i = 0; i < fieldcount; i++)
                {
                    fieldnames.Add(dr.GetName(i));
                }

                TableMapping tableMapping = MappingService.Instance.GetTableMapping(objectType, false);

                while (dr.Read())
                {
                    object           obj    = Activator.CreateInstance(objectType);
                    IDynamicProperty dynObj = obj as IDynamicProperty;

                    foreach (string fieldname in fieldnames)
                    {
                        object val = dr[fieldname];

                        ColumnMapping column = null;
                        if (tableMapping != null)
                        {
                            column = tableMapping.GetColumnMappingByColumn(fieldname);
                        }
                        if (column != null)
                        {
                            column.SetValue(obj, val);
                        }
                        else if (val != DBNull.Value)
                        {
                            PropertyInfo prop = objectType.GetProperty(fieldname);
                            if (prop == null && dynObj != null)
                            {
                                dynObj.SetData(fieldname, val);
                            }
                            else if (prop != null)
                            {
                                if (val.GetType() != prop.PropertyType)
                                {
                                    val = Convert.ChangeType(val, prop.PropertyType);
                                }
                                prop.SetValue(obj, val, null);
                            }
                        }
                    }
                    list.Add(obj);
                }
            }
        }
示例#4
0
        public virtual int Create(object obj)
        {
            TableMapping table = MappingService.Instance.GetTableMapping(obj.GetType());

            return(Create(table, obj));
        }
        public int Update(object obj, HashSet <string> updateprops)
        {
            TableMapping table = MappingService.Instance.GetTableMapping(obj.GetType());

            return(Update(table, obj, updateprops));
        }
示例#6
0
        public static void BulkCopy(IList objs, Dictionary <string, object> bulkCopyConfig = null)
        {
            TableMapping tableMapping = MappingService.Instance.GetTableMapping(typeof(T));

            DatabaseEngineFactory.GetDatabaseEngine().BulkCopy(tableMapping, objs, bulkCopyConfig);
        }
示例#7
0
 public Criteria(TableMapping tableMapping)
     : this()
 {
     this.TableMapping = tableMapping;
 }
示例#8
0
        private TableMapping CreateTableMapping(TableAttribute tableattribute, Type type)
        {
            TableMapping table = new TableMapping();

            table.Name       = tableattribute.Name;
            table.ObjectType = type;

            if (string.IsNullOrEmpty(table.Name))
            {
                table.Name = table.ObjectType.Name;
            }

            List <PropertyInfo> props = new List <PropertyInfo>();

            GetProperties(table, type, props);

            foreach (PropertyInfo prop in props)
            {
                ColumnAttribute columnattribute = Attribute.GetCustomAttribute(prop, typeof(ColumnAttribute)) as ColumnAttribute;
                if (columnattribute == null)
                {
                    table.OtherPropertys.Add(prop);
                }
                else
                {
                    ColumnMapping column = new ColumnMapping();
                    column.Name         = columnattribute.Name;
                    column.PropertyInfo = prop;
                    column.IsViewColumn = columnattribute.IsViewColumn;

                    if (string.IsNullOrEmpty(column.Name))
                    {
                        column.Name = prop.Name;
                    }

                    column.IsAutoIncrement  = columnattribute.IsAutoIncrement;
                    column.IsPK             = columnattribute.IsPK;
                    column.IsUseSeedFactory = columnattribute.IsUseSeedFactory;
                    //--------------------------------------------------------------------------------------------------------------------
                    Attribute[] attrs = Attribute.GetCustomAttributes(prop);
                    foreach (Attribute attr in attrs)
                    {
                        if (attr is DataInterceptAttribute)
                        {
                            if (column.DataIntercept == null)
                            {
                                DataInterceptAttribute dataintercept = attr as DataInterceptAttribute;
                                dataintercept.ColumnMapping = column;
                                column.DataIntercept        = dataintercept;
                            }
                            else
                            {
                                throw new ObjectMappingException("Property have more DataInterceptAttribute define");
                            }
                        }
                    }
                    //--------------------------------------------------------------------------------------------------------------------
                    table.AddColumnMapping(column);
                }
            }

            if (table.ColumnPK == null)
            {
                throw new ObjectMappingException("not find primary key");
            }


            return(table);
        }
示例#9
0
 private void GetProperties(TableMapping table, Type type, List <PropertyInfo> list)
 {
     list.AddRange(type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public));
 }
示例#10
0
        public void SetMappingTable(Type type, string tablename)
        {
            TableMapping table = MappingService.Instance.GetTableMapping(type);

            MappingTable[table.Name] = tablename;
        }