Ejemplo n.º 1
0
        /// <summary>
        /// Create Empty System.Data.DataTable from ColumnAttribute of DPO fields
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private static DataTable GetEmptyDataTable(Type type)
        {
            DataTable dt = new DataTable();

            foreach (PropertyInfo propertyInfo in Reflex.GetColumnProperties(type))
            {
                ColumnAttribute a = Reflex.GetColumnAttribute(propertyInfo);
                if (a == null)
                {
                    continue;
                }

                Type       ty     = propertyInfo.PropertyType.InnullableType();
                DataColumn column = new DataColumn(a.ColumnName, ty);
                column.AllowDBNull   = a.Nullable;
                column.AutoIncrement = a.Identity;
                column.Caption       = a.Caption;

                //may need complicated logic for differentg type
                if (ty == typeof(string))
                {
                    column.MaxLength = a.Length;
                }

                dt.Columns.Add(column);
            }

            return(dt);
        }
Ejemplo n.º 2
0
        private RowObjectAdapter Bind(Selector columnNames)
        {
            foreach (PropertyInfo propertyInfo in Reflex.GetColumnProperties(obj))
            {
                ColumnAttribute attribute = Reflex.GetColumnAttribute(propertyInfo);

                if (attribute != null && this.Row.Table.Columns.Contains(attribute.ColumnNameSaved))
                {
                    DataField     field  = this.fields.Add(attribute.ColumnNameSaved, attribute.Type);
                    ColumnAdapter column = new ColumnAdapter(field);
                    this.Bind(column);

                    column.Field.Identity = attribute.Identity;
                    column.Field.Primary  = attribute.Primary;

                    if (attribute.Identity || attribute.Computed || !columnNames.Exists(attribute.ColumnNameSaved))
                    {
                        column.Field.Saved = false;
                    }
                    else
                    {
                        column.Field.Saved = attribute.Saved;
                    }
                }
            }

            //in case of ColumnAttribute not setup Identity and Primary Keys
            fields.UpdatePrimaryIdentity(obj.Primary, obj.Identity);

            return(this);
        }
Ejemplo n.º 3
0
        public static object FillField(object instance, PropertyInfo propertyInfo, DataRow dataRow, bool defaultValueUsed)
        {
            ColumnAttribute a = Reflex.GetColumnAttribute(dataRow, propertyInfo);

            if (a != null)
            {
                object value = dataRow[a.ColumnName];

                if (value == System.DBNull.Value)
                {
                    if (a.DefaultValue != null)
                    {
                        value = a.DefaultValue;
                    }
                    else if (defaultValueUsed)
                    {
                        Type dataType = dataRow.Table.Columns[a.ColumnName].DataType;
                        value = DefaultRowValue.SystemDefaultValue(dataType);
                    }
                    else
                    {
                        value = null;
                    }
                }

                propertyInfo.SetValue(instance, value, null);
            }

            return(a);
        }
Ejemplo n.º 4
0
 public static void FillInstance(object instance, DataRow dataRow, bool defaultValueUsed)
 {
     foreach (PropertyInfo propertyInfo in Reflex.GetColumnProperties(instance))
     {
         FillField(instance, propertyInfo, dataRow, defaultValueUsed);
     }
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Fill object properties
 /// </summary>
 /// <param name="dataRow"></param>
 public virtual void FillObject(DataRow dataRow)
 {
     foreach (PropertyInfo propertyInfo in this.columnProperties)
     {
         if (Reflex.FillField(this, propertyInfo, dataRow, dataTableAttribute.DefaultValueUsed) == null)
         {
             Mapping mapping = new Mapping(this, propertyInfo);
             mapping.SetValue();
         }
     }
 }
Ejemplo n.º 6
0
        private bool insertIdentityOn = false;      //used for re-create table data during SQL Server updating


        protected PersistentObject()
        {
            Type type = this.GetType();

            TableAttribute[] attributes = GetAttributes <TableAttribute>();
            if (attributes.Length > 0)
            {
                dataTableAttribute = attributes[0];
            }
            else
            {
                dataTableAttribute = new TableAttribute(type.Name, Level.Application);
            }

            this.columnProperties = Reflex.GetColumnProperties(type);
        }
Ejemplo n.º 7
0
        public PersistentCollection()
        {
            DataTable dataTable;

            if (TableName.Exists())
            {
                dataTable           = new SqlCmd(ConnectionProviderManager.DefaultProvider, $"SELECT TOP 1 * FROM {TableName}").FillDataTable();
                dataTable.TableName = TableName.Name;
                dataTable.Clear();
            }
            else
            {
                dataTable = Reflex.GetEmptyDataTable <T>();
            }

            this.Table = dataTable;
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Collect property values and save to DataRow
        /// </summary>
        /// <param name="dataRow"></param>
        public virtual void UpdateRow(DataRow dataRow)
        {
            foreach (PropertyInfo propertyInfo in Reflex.GetColumnProperties(this))
            {
                ColumnAttribute attribute = Reflex.GetColumnAttribute(dataRow, propertyInfo);

                if (attribute != null && dataRow.Table.Columns.Contains(attribute.ColumnNameSaved))
                {
                    if (propertyInfo.GetValue(this, null) == null)
                    {
                        dataRow[attribute.ColumnNameSaved] = System.DBNull.Value;
                    }
                    else
                    {
                        dataRow[attribute.ColumnNameSaved] = propertyInfo.GetValue(this, null);
                    }
                }
            }
        }
Ejemplo n.º 9
0
        public void Apply()
        {
            foreach (PropertyInfo propertyInfo in Reflex.GetColumnProperties(obj))
            {
                ColumnAttribute a = Reflex.GetColumnAttribute(propertyInfo);
                if (a != null && this.Row.Table.Columns.Contains(a.ColumnNameSaved))
                {
                    if (propertyInfo.GetValue(obj, null) == null)
                    {
                        this.Row[a.ColumnNameSaved] = System.DBNull.Value;
                    }
                    else
                    {
                        this.Row[a.ColumnNameSaved] = propertyInfo.GetValue(obj, null);
                    }
                }
            }

            this.Fill();
        }
Ejemplo n.º 10
0
        private void Apply(RowAdapter d)
        {
            foreach (PropertyInfo propertyInfo in this.columnProperties)
            {
                ColumnAttribute a = Reflex.GetColumnAttribute(propertyInfo);
                if (a != null && d.Row.Table.Columns.Contains(a.ColumnNameSaved))
                {
                    if (propertyInfo.GetValue(this, null) == null)
                    {
                        d.Row[a.ColumnNameSaved] = System.DBNull.Value;
                    }
                    else
                    {
                        d.Row[a.ColumnNameSaved] = propertyInfo.GetValue(this, null);
                    }
                }
            }

            d.Fill();
        }
Ejemplo n.º 11
0
        public virtual bool Delete()
        {
            RowObjectAdapter d = new RowObjectAdapter(this);

            d.Apply();

            //check 1..many table dependency
            foreach (PropertyInfo propertyInfo in this.columnProperties)
            {
                AssociationAttribute association = Reflex.GetAssociationAttribute(propertyInfo);
                if (association == null)
                {
                    continue;
                }

                if (!propertyInfo.PropertyType.IsGenericType)
                {
                    continue;
                }

                IDPCollection collection = (IDPCollection)propertyInfo.GetValue(this, null);
                if (collection.Count == 0)
                {
                    continue;
                }

                Type[] typeParameters = propertyInfo.PropertyType.GetGenericArguments();
                if (typeParameters.Length == 1 && typeParameters[0].IsSubclassOf(typeof(PersistentObject)))
                {
                    throw new ApplicationException(string.Format(
                                                       "Error: data persistent object on Table {0} cannot be deleted because Table {1} depends on it.",
                                                       TableName,
                                                       collection.TableName));
                }
            }

            d.RowChanged += RowChanged;

            return(d.Delete());
        }
Ejemplo n.º 12
0
        private SqlBuilder clause2;     //B := SELECT * FROM Roles WHERE Roles.Role_ID IN (A)

        public Mapping(PersistentObject dpo, PropertyInfo propertyInfo2)
        {
            this.association = Reflex.GetAssociationAttribute(propertyInfo2);

            if (association == null)
            {
                return;
            }

            this.dpoInstance   = dpo;
            this.propertyInfo2 = propertyInfo2;

            Type dpoType2;            //typeof(RoleDpo)

            if (propertyInfo2.PropertyType.IsGenericType)
            {
                dpoType2 = PersistentObject.GetCollectionGenericType(propertyInfo2);

                if (this.association.TRelation == null)
                {
                    mappingType = MappingType.One2Many;
                }
                else
                {
                    mappingType = MappingType.Many2Many;
                }
            }
            else
            {
                dpoType2    = propertyInfo2.PropertyType;
                mappingType = MappingType.One2One;
            }



            this.propertyInfo1 = dpo.GetType().GetProperty(association.Column1);


            if (mappingType == MappingType.Many2Many)
            {
                this.clause1 = new SqlBuilder()
                               .SELECT().COLUMNS(association.Relation2)
                               .FROM(association.TRelation)
                               .WHERE(association.Relation1.ColumnName() == association.Column1.ParameterName());

                this.clause2 = new SqlBuilder()
                               .SELECT()
                               .COLUMNS()
                               .FROM(dpoType2)
                               .WHERE(association.Relation2.ColumnName().IN(this.clause1));
            }
            else
            {
                SqlExpr where = association.Column2.ColumnName() == association.Column1.ParameterName();
                if (association.Filter != null)
                {
                    where = where.AND(association.Filter);
                }

                this.clause2 = new SqlBuilder()
                               .SELECT()
                               .COLUMNS()
                               .FROM(dpoType2)
                               .WHERE(where);

                if (association.OrderBy != null)
                {
                    this.clause2 = clause2.ORDER_BY(association.OrderBy);
                }
            }
        }