Example #1
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);
            }
        }
Example #2
0
        /** Creates an object from the stored data in the JSON.
         *
         * @param _JSONObject with the data of the object.
         * @return the generated instance.
         */
        public E Parse(JToken _JSONObject)
        {
            try {
                E obj = (E)Activator.CreateInstance(tableMapping.type);
                for (int i = 0; i < this.tableMapping.columns.Length; i++)
                {
                    ColumnInfo column = this.tableMapping.columns[i];
                    if (column.IsPrimitiveField)
                    {
                        if (_JSONObject[column.name] != null)
                        {
                            object value = column.propertyType == typeof(String) ? _JSONObject[column.name].Value <String>() : JsonConvert.DeserializeObject(_JSONObject[column.name].ToString(), column.propertyType);
                            column.property.SetValue(obj, value);
                        }
                        else
                        {
                            column.property.SetValue(obj, null);
                        }
                    }
                    else if (column.IsSingleRelationship)
                    {
                        object           value          = null;
                        PersistentEntity relationObject = (PersistentEntity)Activator.CreateInstance(column.propertyType);

                        if (_JSONObject[QueryGenerator <E> .ColumnName(column)] != null)
                        {
                            relationObject.SetServerId(JsonConvert.DeserializeObject <long>(_JSONObject[QueryGenerator <E> .ColumnName(column)].ToString()));
                            value = relationObject;
                        }
                        else if (_JSONObject[column.property.Name] != null)
                        {
                            value = relationObject.GetTableData().Parse(_JSONObject[column.name]);
                        }

                        column.property.SetValue(obj, value);
                    }
                    else if (column.IsMultipleRelationship)
                    {
                        if (_JSONObject[column.name] != null)
                        {
                            PersistentEntity        relationObject = (PersistentEntity)Activator.CreateInstance(column.propertyType.GenericTypeArguments[0]);
                            List <PersistentEntity> value          = relationObject.GetTableData().Parse(JArray.Parse(_JSONObject[column.name].ToString()));
                            column.property.SetValue(obj, value);
                        }
                        else
                        {
                            column.property.SetValue(obj, null);
                        }
                    }
                }

                Type baseType = Reflections.GetBaseType(this.tableMapping.type);
                if (baseType != typeof(PersistentEntity))
                {
                    Reflections.SetInstanceFromSuperInstance(obj, ((PersistentEntity)Activator.CreateInstance(baseType)).GetTableData().Parse(_JSONObject));
                }
                return(obj);
            } catch (Exception e) {
                SQLConsole.WriteLine(e.ToString());
                return(null);
            }
        }