Exemplo n.º 1
0
        /// <summary>
        /// Crea una consulta tipo SELECT basica a partir de una clase
        /// </summary>
        /// <param name="Obj"></param>
        /// <returns></returns>
        private static QueryBuilder CreateSELECT(Object Obj, Type type = null)
        {
            Type Tipo = null;

            if (type == null)
            {
                if (Obj == null)
                {
                    throw new ArgumentNullException("Parámetro obj no debe ser nulo. Parámetro type es opcional, ambos parámetros deben ser del mismo tipo.");
                }
                else
                {
                    Tipo = Obj.GetType();
                }
            }
            else
            {
                Tipo = type;
            }

            var TableName = Tipo.GetCustomAttributesData()[0].ConstructorArguments[0].Value;

            PropertyInfo[] PropInfo = Tipo.GetProperties();

            string SqlQuery = $"SELECT ";

            foreach (var Prop in PropInfo)
            {
                var Attrs = Prop.GetCustomAttributes().Select(x => x.GetType().Name).ToArray();

                if (Attrs.Contains("ColumnAttribute"))
                {
                    SqlQuery += Prop.Name + ", ";
                }
            }

            SqlQuery  = SqlQuery.Substring(0, (SqlQuery.Length - 2));
            SqlQuery += $" FROM {TableName};";

            return(new QueryBuilder(SqlQuery, TypeQueryEnum.SELECT));
        }