Пример #1
0
        public override ValidationErrors Validate()
        {
            ValidationErrors ve = new ValidationErrors();
            var myType          = this.GetType();

            var myValues = new List <MemberInfo>();

            myValues.AddRange(this.GetType().GetFields());
            myValues.AddRange(this.GetType().GetProperties());

            T obj = obj = (T)(IBusinessObject)this;

            foreach (var a in ValidationRules())
            {
                foreach (var err in a.Validate(obj))
                {
                    ve.Add(err);
                }
            }

            // Validations
            foreach (var field in myValues.Where((f) => f.GetCustomAttribute <ValidationAttribute>() != null))
            {
                var vAttribute = field.GetCustomAttribute <ValidationAttribute>();
                if (vAttribute != null)
                {
                    foreach (var a in vAttribute.Validate(field, ReflectionTool.GetMemberValue(field, this)))
                    {
                        ve.Add(a);
                    }
                }
            }

            return(ve);
        }
Пример #2
0
        internal QueryBuilder GenerateUpdateValueParams(IDataObject tabelaInput, bool OmmitPk = true)
        {
            QueryBuilder Query = new QueryBuilder();
            var          lifi  = GetMembers(tabelaInput.GetType());
            int          k     = 0;

            for (int i = 0; i < lifi.Count; i++)
            {
                if (OmmitPk && lifi[i].GetCustomAttribute(typeof(PrimaryKeyAttribute)) != null)
                {
                    continue;
                }
                foreach (CustomAttributeData att in lifi[i].CustomAttributes)
                {
                    if (att.AttributeType == typeof(FieldAttribute))
                    {
                        Object val = ReflectionTool.GetMemberValue(lifi[i], tabelaInput);
                        Query.Append($"{lifi[i].Name} = @{(++k)}", val);
                        if (i < lifi.Count - 1)
                        {
                            Query.Append(", ");
                        }
                    }
                }
            }
            return(Query);
        }
Пример #3
0
        public IQueryBuilder GenerateInsertQuery(IDataObject inputObject)
        {
            var          type  = inputObject.GetType();
            QueryBuilder query = new QbFmt($"INSERT INTO {inputObject.GetType().Name}");

            query.Append("(");
            query.Append(GenerateFieldsString(type, false));
            query.Append(")");
            query.Append("VALUES (");
            var members = GetMembers(type);

            // members.RemoveAll(m => m.GetCustomAttribute<PrimaryKeyAttribute>() != null);
            for (int i = 0; i < members.Count; i++)
            {
                query.Append($"@{i + 1}", ReflectionTool.GetMemberValue(members[i], inputObject));
                if (i < members.Count - 1)
                {
                    query.Append(",");
                }
            }
            query.Append(")");
            //query.Append("ON CONFLICT (" + GenerateKeysString(type) + ") DO UPDATE SET ");
            //var Fields = GetMembers(inputObject.GetType());
            //for (int i = 0; i < Fields.Count; ++i) {
            //    if (Fields[i].GetCustomAttribute(typeof(PrimaryKeyAttribute)) != null)
            //        continue;
            //    query.Append(String.Format("{0} = VALUES({0})", Fields[i].Name));
            //    if (i < Fields.Count - 1) {
            //        query.Append(",");
            //    }
            //}
            return(query);
        }
Пример #4
0
        public IQueryBuilder GenerateInsertQuery(IDataObject inputObject)
        {
            QueryBuilder query = new QbFmt($"INSERT INTO {inputObject.GetType().Name}");

            query.Append("(");
            query.Append(GenerateFieldsString(inputObject.GetType(), true));
            query.Append(")");
            query.Append("VALUES (");
            var members = GetMembers(inputObject.GetType());

            members.RemoveAll(m => m.GetCustomAttribute <PrimaryKeyAttribute>() != null);
            for (int i = 0; i < members.Count; i++)
            {
                var val = ReflectionTool.GetMemberValue(members[i], inputObject);
                if (val == null)
                {
                    var pc = members[i].GetCustomAttribute <PreemptiveCounter>();
                    var ic = members[i].GetCustomAttribute <IncrementalCounter>();

                    if (pc != null)
                    {
                        query.Append(pc.OnInsertSubQuery(inputObject.GetType(), members[i]));
                    }
                    else
                    if (ic != null)
                    {
                        query.Append(ic.OnInsertSubQuery(inputObject.GetType(), members[i]));
                    }
                    else
                    {
                        query.Append($"@{members[i].Name}_{i + 1}", val);
                    }
                }
                else
                {
                    query.Append($"@{members[i].Name}_{i + 1}", val);
                }

                if (i < members.Count - 1)
                {
                    query.Append(",");
                }
            }
            query.Append(")");
            //query.Append("ON DUPLICATE KEY UPDATE ");
            //var Fields = GetMembers(inputObject.GetType()).Where(field =>
            //    field.GetCustomAttribute(typeof(PrimaryKeyAttribute)) == null &&
            //    field.GetCustomAttribute(typeof(ReliableIdAttribute)) == null
            //).ToList();
            //for (int i = 0; i < Fields.Count; ++i) {
            //    query.Append(String.Format("{0} = VALUES({0})", Fields[i].Name));
            //    if (i < Fields.Count - 1) {
            //        query.Append(",");
            //    }
            //}
            return(query);
        }
Пример #5
0
        public IQueryBuilder GenerateMultiUpdate <T>(List <T> inputRecordset) where T : IDataObject
        {
            // --
            List <T> workingSet = new List <T>();

            var rid = FiTechBDadosExtensions.RidColumnOf[typeof(T)];

            workingSet.AddRange(inputRecordset.Where((record) => record.IsPersisted));
            if (workingSet.Count < 1)
            {
                return(null);
            }
            QueryBuilder Query = new QueryBuilder();

            Query.Append($"UPDATE IGNORE {typeof(T).Name} ");
            Query.Append("SET ");

            // --
            var members = GetMembers(typeof(T));

            members.RemoveAll(m => m.GetCustomAttribute <PrimaryKeyAttribute>() != null);
            int x = 0;

            for (var i = 0; i < members.Count; i++)
            {
                var memberType = ReflectionTool.GetTypeOf(members[i]);
                Query.Append($"{members[i].Name}=(CASE ");
                foreach (var a in inputRecordset)
                {
                    Query.Append($"WHEN {rid}=@_mu{x++} THEN @_mu{x++}", a.RID, ReflectionTool.GetMemberValue(members[i], a));
                }
                Query.Append($"ELSE {members[i].Name} END)");
                if (i < members.Count - 1)
                {
                    Query.Append(",");
                }
            }

            Query.Append($"WHERE {rid} IN (")
            .Append(Fi.Tech.ListRids(workingSet))
            .Append(");");
            // --
            return(Query);
        }
Пример #6
0
        public static Dictionary <string, object> ValuesToDictionary(this Object me)
        {
            if (me == null)
            {
                throw new NullReferenceException("Figlotech ToDictionary Extension method called on a null value, this is a natural NullReferenceException");
            }

            var retv = new Dictionary <string, object>();
            var refl = me.AsReflectable();

            foreach (var a in ReflectionTool.FieldsAndPropertiesOf(me.GetType()))
            {
                if (ReflectionTool.GetTypeOf(a).IsPublic)
                {
                    retv[a.Name] = ReflectionTool.GetMemberValue(a, me);
                }
            }
            return(retv);
        }
Пример #7
0
        void CascadingDoForFields <T>(Action <T> process, List <Type> prevList = null)
        {
            if (prevList == null)
            {
                prevList = new List <Type>();
            }
            // prevList is a resource I'm using to break the f**k out of
            // looping references.
            // Idk if its the best way to, but it works.
            if (prevList.Contains(this.GetType()))
            {
                return;
            }
            prevList.Add(this.GetType());
            if (!typeof(T).IsInterface)
            {
                throw new BDadosException("CascadingDoForFields expects T to be an Interface type.");
            }
            var myValues = new List <MemberInfo>();

            myValues.AddRange(this.GetType().GetFields());
            myValues.AddRange(this.GetType().GetProperties());

            foreach (var field in myValues)
            {
                var workingValue = ReflectionTool.GetMemberValue(field, this);
                if (workingValue is T)
                {
                    CascadingDoForFields <T>(process, prevList);
                }
                if (!ReflectionTool.GetTypeOf(field).GetInterfaces().Contains(typeof(T)))
                {
                    continue;
                }
                prevList.Add(ReflectionTool.GetTypeOf(field));
                try {
                    T workObject = ((T)workingValue);
                    process(workObject);
                }
                catch (Exception) { }
            }
        }
Пример #8
0
        public IQueryBuilder GenerateValuesString(IDataObject tabelaInput, bool OmmitPK = true)
        {
            QueryBuilder Query  = new QueryBuilder();
            var          fields = GetMembers(tabelaInput.GetType());

            if (OmmitPK)
            {
                fields.RemoveAll(m => m.GetCustomAttribute <PrimaryKeyAttribute>() != null);
            }
            for (int i = 0; i < fields.Count; i++)
            {
                Object val = ReflectionTool.GetMemberValue(fields[i], tabelaInput);
                if (!Query.IsEmpty)
                {
                    Query.Append(", ");
                }
                Query.Append($"@{gid++}_{fields[i].Name}", val);
            }
            return(Query);
        }
Пример #9
0
        public IQueryBuilder GenerateInsertQuery(IDataObject inputObject)
        {
            QueryBuilder query = new QbFmt($"INSERT INTO {inputObject.GetType().Name}");

            query.Append("(");
            query.Append(GenerateFieldsString(inputObject.GetType(), true));
            query.Append(")");
            query.Append("VALUES (");
            var members = GetMembers(inputObject.GetType());

            members.RemoveAll(m => m.GetCustomAttribute <PrimaryKeyAttribute>() != null);
            for (int i = 0; i < members.Count; i++)
            {
                query.Append($"@{i + 1}", ReflectionTool.GetMemberValue(members[i], inputObject));
                if (i < members.Count - 1)
                {
                    query.Append(",");
                }
            }
            query.Append(") ON DUPLICATE KEY UPDATE ");
            var Fields = GetMembers(inputObject.GetType());

            for (int i = 0; i < Fields.Count; ++i)
            {
                if (Fields[i].GetCustomAttribute(typeof(PrimaryKeyAttribute)) != null)
                {
                    continue;
                }
                query.Append(String.Format("{0} = VALUES({0})", Fields[i].Name));
                if (i < Fields.Count - 1)
                {
                    query.Append(",");
                }
            }
            return(query);
        }
Пример #10
0
            public void Apply(T element, IEnumerable <T> elements)
            {
                var value = Enumerable.Max <T, TResult>(elements, (e) => (TResult)ReflectionTool.GetMemberValue(member, e));

                ReflectionTool.SetMemberValue(member, element, Convert.ChangeType(value, typeof(TResult)));
            }
Пример #11
0
            public void Apply(T element, IEnumerable <T> elements)
            {
                var value = Enumerable.Sum <T>(elements, (e) => (decimal)ReflectionTool.GetMemberValue(member, e));

                ReflectionTool.SetMemberValue(member, element, Convert.ChangeType(value, ReflectionTool.GetTypeOf(member)));
            }
Пример #12
0
        public IQueryBuilder GenerateMultiUpdate <T>(List <T> inputRecordset) where T : IDataObject
        {
            // --
            var t = inputRecordset?.FirstOrDefault()?.GetType();

            if (t == null)
            {
                return(Qb.Fmt("SELECT 1"));
            }

            List <T> workingSet = new List <T>();

            var rid = FiTechBDadosExtensions.RidColumnOf[t];
            var upd = FiTechBDadosExtensions.UpdateColumnOf[t];

            workingSet.AddRange(inputRecordset.Where((record) => record.IsPersisted));
            if (workingSet.Count < 1)
            {
                return(null);
            }
            QueryBuilder Query = new QueryBuilder();

            Query.Append($"UPDATE {t.Name} ");
            Query.Append("SET \r\n");

            // --
            var members = GetMembers(t);

            members.RemoveAll(
                m =>
                m.GetCustomAttribute <PrimaryKeyAttribute>() != null ||
                m.GetCustomAttribute <ReliableIdAttribute>() != null
                );

            int x    = 0;
            int ggid = ++gid;

            Query.PrepareForQueryLength(inputRecordset.Count * 512);
            for (var i = 0; i < members.Count; i++)
            {
                var memberType = ReflectionTool.GetTypeOf(members[i]);
                Query.Append($"\t{members[i].Name}=(CASE ");
                for (int ridx = 0; ridx < inputRecordset.Count; ridx++)
                {
                    Query.Append($"WHEN {rid}=@r_{ridx}", inputRecordset[ridx].RID);
                    if (inputRecordset[ridx].IsReceivedFromSync)
                    {
                        Query.Append($"AND {upd}<@u_{ridx}", inputRecordset[ridx].UpdatedTime);
                    }
                    Query.Append($"THEN @{ggid}_{++x}", ReflectionTool.GetMemberValue(members[i], inputRecordset[ridx]));
                }
                Query.Append($"ELSE {members[i].Name} END)");
                if (i < members.Count - 1)
                {
                    Query.Append(",\r\n");
                }
            }

            Query.Append($"WHERE {rid} IN (")
            .Append(Fi.Tech.ListRids(workingSet))
            .Append(");");
            // --
            return(Query);
        }