/// <summary> /// Sets a parameter for a command execution. /// </summary> /// <param name="member">ORM member that contains the parameter name (should be the same as database field name).</param> /// <param name="value">Parameter value.</param> public static void SetParameter(ORMEntityMember member, object value) { SQLiteParameter sqlParam; try { if (value is Image) { Image img = value as Image; sqlParam = new SQLiteParameter(member.Attribute.FieldName, DbType.Binary); sqlParam.Value = (byte[])BinaryUtils.ImageToByteArray(img); } else if (value is byte[]) { sqlParam = new SQLiteParameter(member.Attribute.FieldName, DbType.Binary, ((byte[])value).Length); sqlParam.Value = value; } else if (ReflectionUtils.IsSubclassOfRawGeneric(typeof(ORMEntity <>), value.GetType())) { // Get primary key value of the ORMEntity instance sqlParam = new SQLiteParameter(member.Attribute.FieldName, ORMSqliteDriver.GetPrimaryKeyValue(value)); } else { sqlParam = new SQLiteParameter(member.Attribute.FieldName, value); } ORMSqliteDriver.parameters.Add(sqlParam); } catch (Exception ex) { Logger.LogError(ex); throw ex; } }
public ORMSqlCommand GetSelectByFieldCommand(string propertyName) { try { ORMSqlCommand cmd = new ORMSqlCommand(); ORMEntityMember member = ORMEntity <T> .ORMStructure.GetByPropertyName(propertyName); // Generate SELECT clause cmd.SqlCommand += " select "; cmd.SqlCommand += ORMEntity <T> .ORMStructure.PrimaryKey.Attribute.FieldName; cmd.SqlCommand += " as ID "; // Generate FROM clause cmd.SqlCommand += " from "; cmd.SqlCommand += ORMEntity <T> .ORMStructure.Table.TableName; // Generate WHERE clause cmd.SqlCommand += " where "; cmd.SqlCommand += member.Attribute.FieldName + "=@" + member.Attribute.FieldName; cmd.Parameters.Add(member); return(cmd); } catch (Exception ex) { Logger.LogError(this, ex); throw ex; } }
/// <summary> /// Create a new instance of <see cref="ORMEntityMember"/>. /// </summary> /// <param name="property">Field property.</param> /// <returns>The new instance.</returns> public static ORMEntityMember CreateInstance(PropertyInfo property) { ORMProperty[] atts = (ORMProperty[])property.GetCustomAttributes(typeof(ORMProperty), false); if (atts.Length <= 0) { return(null); } else { ORMEntityMember member = new ORMEntityMember(); member.Initialize(property, atts[0]); return(member); } }
/// <summary> /// Get all <see cref="ORMProperty"/> corresponding to the type properties marked as a foreign collections. /// </summary> /// <param name="type">Type of class for which we're getting the properties.</param> /// <returns>The requested list of attributes or an empty array if the table doesn't have any marked property as a foreign collection.</returns> private static List <Int64> GetForeignCollectionIdentifiers(object instance, ORMEntityMember member) { List <Int64> identifiers = new List <Int64>(); if (member.Property != null) { object collectionValue = (IEnumerable <ORMIdentifiableEntity>)member.Property.GetValue(instance); if (collectionValue != null) { foreach (var fValue in (IEnumerable <ORMIdentifiableEntity>)member.Property.GetValue(instance)) { identifiers.Add(fValue.ID); } } } return(identifiers); }
/// <summary> /// Initialize the instance data. /// </summary> private void Initialize(PropertyInfo property, ORMProperty attribute) { this.Property = property; this.Attribute = attribute; this.OwnerType = this.Property.DeclaringType; this.IsFileField = (this.Attribute is ORMPropertyFile); this.IsForeignField = this.IsOrmInstance(this.Property.PropertyType); this.IsForeignCollection = (this.Attribute is ORMForeignCollection); this.IsPrimaryKey = (this.Attribute is ORMPrimaryKey); this.IsField = !this.IsForeignField && !this.IsForeignCollection && !this.IsPrimaryKey && !this.IsFileField; if (this.IsForeignCollection) { this.ForeignCollectionType = this.Property.PropertyType.GetGenericArguments()[0]; this.ForeignCollectionMember = this.GetForeignFieldMember(this.ForeignCollectionType); } }
private static object GetORMForeignCollection(ORMEntityMember member, object instance) { // Get the unique identifier long id = (long)ORMEntity <T> .ORMStructure.GetPrimaryKeyValue(instance); if (id <= 0) { return(null); } // Get the method GET(long) Type at = typeof(ORMEntity <>).MakeGenericType(member.ForeignCollectionType); MethodInfo methodInfo = at.GetMethod("FindBy", new Type[] { typeof(String), typeof(Int64), typeof(Boolean) }); // Invoke the method object dummy = Activator.CreateInstance(member.ForeignCollectionType); return(methodInfo.Invoke(dummy, new object[] { member.ForeignCollectionMember.Property.Name, id, false })); }