/** Creates a <code>ContentValues</code> from the object. * * @param object to be converted in a <code>ContentValues</code> for persistent context * @return the generated <code>ContentValues</code>. */ public Dictionary <String, object> GetContentValues(E obj) { Dictionary <String, object> values = new Dictionary <String, object>(); if (obj.GetId() != -1) { values[Configuration.ID_COLUMN_NAME] = obj.GetId(); } foreach (ColumnInfo column in this.tableMapping.columns) { if (!column.IsMultipleRelationship) { if (column.IsSingleRelationship) { if (Reflections.IsAttributePresent(column.property, typeof(BelongsTo)) || !Reflections.IsAttributePresent(column.property, typeof(HasOne))) { values[QueryGenerator <E> .ColumnName(column)] = ((PersistentEntity)column.property.GetValue(obj)).GetServerId(); } } else { values[QueryGenerator <E> .ColumnName(column)] = column.property.GetValue(obj); } } } return(values); }
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; } } } } }
/** * Generate the column constraints for SQL from a <code>Field</code> if <code>Constraint</code> annotation is present * @param field the field of the persistent entity * @return the column constraints */ private static String ColumnConstraints(ColumnInfo column) { String result = ""; if (Reflections.IsAttributePresent(column.property, typeof(Constraints))) { Constraints constraints = (Constraints)Reflections.GetAttribute(column.property, typeof(Constraints)); if (constraints.unique == true) { result += " UNIQUE"; } if (constraints.nullable == false) { result += " NOT NULL"; } } // if (EntityFieldHelper.isSingleRelationField(field) && !field.isAnnotationPresent(HasOne.class)) { // result += " REFERENCES " + getTableName((Class<PersistentEntity>) field.getType()) + "(" + Configuration.ID_COLUMN_NAME + ")"; // if (field.isAnnotationPresent(BelongsTo.class)) { // result += " ON DELETE CASCADE"; // } // } return(result); }
/** 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); } }