Beispiel #1
0
        /// <summary>
        /// Get a pair of key:value for the given field of the given entity.
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="field"></param>
        /// <returns></returns>
        public string[] GetKeyValue(object entity, string field)
        {
            PropertyInfo property  = entity.GetType().GetProperty(field);
            Collumn      attribute = this.GetAttributeFrom <Collumn>(entity, field);
            object       value     = property.GetValue(entity);

            string[] keyvalue =
            {
                attribute.Name,
                value != null ? this.StringValue(value.ToString()) : null
            };

            return(keyvalue);
        }
Beispiel #2
0
        /// <summary>
        /// Fill an entity from a tuple by linking automaticaly
        /// entity properties and table collumns.
        ///
        /// The first execution generate a table schema,
        /// the second just read the schema, allowing for better performances.
        /// </summary>
        /// <param name="table"></param>
        /// <param name="entity"></param>
        /// <param name="tuple"></param>
        /// <returns></returns>
        public object FillEntityFromTuple(string table, object entity, Dictionary <string, string> tuple)
        {
            if (tuple == null)
            {
                return(null);
            }

            PropertyInfo[] properties = entity.GetType().GetProperties();

            foreach (PropertyInfo property in properties)
            {
                Collumn attribute = this.GetAttributeFrom <Collumn>(entity, property.Name);

                if (attribute == null || attribute.Name == null || !tuple.ContainsKey(attribute.Name))
                {
                    continue;
                }

                string value = tuple[attribute.Name];
                Type   type  = property.PropertyType;

                if (type == typeof(Entity.DefaultStatus))
                {
                    Entity.DefaultStatus status = (Entity.DefaultStatus)Enum.Parse(typeof(Entity.DefaultStatus), value);
                    property.SetValue(entity, status);
                }
                else if (type == typeof(Boolean))
                {
                    property.SetValue(entity, Convert.ChangeType((value == "1" ? true : false), type));
                }
                else if (!String.IsNullOrEmpty(value))
                {
                    try {
                        property.SetValue(entity, Convert.ChangeType(value, type));
                    } catch { }
                }
            }

            return(entity);
        }
Beispiel #3
0
        /// <summary>
        /// Get a list with keys and values to pass to Insert/Update fonctions from DB plateform.
        /// </summary>
        /// <param name="entity">An instance of any entity</param>
        /// <returns></returns>
        public List <string[]> GetKeysAndValues(object entity)
        {
            List <string[]> keysvalues = new List <string[]>();

            foreach (PropertyInfo property in entity.GetType().GetProperties())
            {
                Collumn attribute = this.GetAttributeFrom <Collumn>(entity, property.Name);

                // Non-mapped fields doesn't have a column in table.
                if (attribute != null && attribute.IsMapped && attribute.Name != "id")
                {
                    object propVal = property.GetValue(entity);

                    keysvalues.Add(new string[] {
                        attribute.Name,
                        propVal != null ? this.StringValue(propVal.ToString()) : null
                    });
                }
            }

            return(keysvalues);
        }
Beispiel #4
0
        /// <summary>
        /// The the list of fields in an entity.
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="onlyName"></param>
        /// <returns></returns>
        public string GetFieldsList(object entity, bool onlyName = false)
        {
            string        expression = "";
            List <string> properties = this.GetPropertiesList(entity);
            byte          count      = (byte)properties.Count;

            for (byte i = 0; i < count; i++)
            {
                Collumn attr = this.GetAttributeFrom <Collumn>(entity, properties[i]);

                if (attr == null || !attr.IsMapped)
                {
                    continue;
                }

                expression += attr.Name;

                if (!onlyName)
                {
                    expression += " " + attr.Datatype;

                    if (attr.Options != null)
                    {
                        expression += " " + attr.Options;
                    }
                }

                expression += ", ";
            }

            if (expression.EndsWith(", "))
            {
                expression = expression.Substring(0, expression.Length - 2);
            }

            return(expression);
        }