Пример #1
0
        private void PerformActionOnChilds(Action action)
        {
            ColumnInfo[] fields = GetTableData().GetTableMapping().columns;
            foreach (ColumnInfo field in fields)
            {
                IList  childs   = new List <PersistentEntity>();
                String mappedBy = "";
                if (Reflections.IsAttributePresent(field.property, typeof(HasMany)))
                {
                    HasMany hasMany = (HasMany)Reflections.GetAttribute(field.property, typeof(HasMany));
                    mappedBy = hasMany.mappedBy;
                    childs   = (IList)field.property.GetValue(this);
                }
                else if (Reflections.IsAttributePresent(field.property, typeof(HasOne)))
                {
                    HasOne hasOne = (HasOne)Reflections.GetAttribute(field.property, typeof(HasOne));
                    mappedBy = hasOne.mappedBy;
                    childs.Add((PersistentEntity)field.property.GetValue(this));
                }
                if (childs != null)
                {
                    foreach (PersistentEntity child in childs)
                    {
                        switch (action)
                        {
                        case Action.Insert:
                            PropertyInfo childField = Reflections.GetDeclaredFieldRecursively(mappedBy, child.GetType(), typeof(PersistentEntity));
                            childField.SetValue(child, this);
                            child.Insert();
                            break;

                        case Action.Update:
                            child.Update();
                            break;

                        case Action.Delete:
                            child.Delete();
                            break;
                        }
                    }
                }
            }
        }
Пример #2
0
        /** Creates an object from the stored data in the cursor.
         *
         * @param cursor with the data of the query. It's should be pointing to the needed row.
         * @return the generated instance.
         */
        public E CursorToEntity(Cursor cursor)
        {
            try {
                E obj = (E)Activator.CreateInstance(tableMapping.type);

                obj.SetId(cursor.GetValue <long>(Configuration.ID_COLUMN_NAME));

                /*foreach (ColumnInfo column in this.tableMapping.columns) {
                 *      //object value = typeof(Cursor).GetTypeInfo().GetDeclaredMethod("GetValue").MakeGenericMethod(column.propertyType).Invoke(cursor, new object[] { column.name });
                 *      object value = cursor.GetValue(column.name, column.propertyType);
                 *      column.property.SetValue(obj, value);
                 * }*/

                for (int i = 1; i <= this.tableMapping.columns.Length; i++)
                {
                    ColumnInfo column = this.tableMapping.columns[i - 1];
                    if (column.IsPrimitiveField)
                    {
                        object value = cursor.GetValue(i, column.propertyType);
                        column.property.SetValue(obj, value);
                    }
                    else if (column.IsSingleRelationship)
                    {
                        object           value          = cursor.GetValue <long>(i);
                        PersistentEntity relationObject = (PersistentEntity)Activator.CreateInstance(column.propertyType);
                        if (Reflections.IsAttributePresent(column.property, typeof(BelongsTo)) ||
                            (Reflections.IsAttributePresent(column.property, typeof(HasOne)) &&
                             ((HasOne)Reflections.GetAttribute(column.property, typeof(HasOne))).lazy))
                        {
                            relationObject.SetServerId((long)value);
                            column.property.SetValue(obj, relationObject);
                        }
                        else
                        {
                            value = relationObject.GetTableData().GetByServerId((dynamic)value);
                            column.property.SetValue(obj, value);
                        }
                    }
                    else if (column.IsMultipleRelationship)
                    {
                        if (Reflections.IsAttributePresent(column.property, typeof(HasMany)))
                        {
                            HasMany hasMany = (HasMany)Reflections.GetAttribute(column.property, typeof(HasMany));
                            if (hasMany.lazy)
                            {
                                column.property.SetValue(obj, Activator.CreateInstance(column.propertyType));
                            }
                            else
                            {
                                Type   relationType           = column.propertyType.GenericTypeArguments[0];
                                String columnName             = "id" + hasMany.mappedBy.ToUpper().ToCharArray()[0] + hasMany.mappedBy.Substring(1);
                                List <PersistentEntity> value = ((PersistentEntity)Activator.CreateInstance(relationType)).GetTableData().GetAllByField(columnName);
                                column.property.SetValue(obj, value);
                            }
                        }
                        else
                        {
                            throw new NotSupportedException("Use relationships not supported in lists, you must create an auxiliar table");
                        }
                    }
                }

                PersistentEntity superObject = GetSuperObject(obj.GetId());
                if (superObject != null)
                {
                    Reflections.SetInstanceFromSuperInstance(obj, superObject);
                }

                return(obj);
            } catch (Exception e) {
                SQLConsole.WriteLine(e.ToString());
                return(null);
            }
        }