Пример #1
0
        public UpdateQuery <T> Where(T obj)
        {
            List <MemberInfo> members = obj.GetType().GetValueMembers();

            foreach (MemberInfo member in members)
            {
                Type                 memberType      = member.GetUnderlyingType();
                DbFieldAttribute     columnAttribute = member.GetCustomAttribute <DbFieldAttribute>(true);
                WhereIgnoreAttribute whereIgnore     = member.GetCustomAttribute <WhereIgnoreAttribute>(true);

                if (columnAttribute != null && whereIgnore == null)
                {
                    object value = member.GetValue(obj);

                    if (columnAttribute.ValidFormatter(memberType))
                    {
                        value = columnAttribute.FormatGetValue(memberType, value);
                    }

                    Where(columnAttribute.Name, value);
                }
            }

            return(this);
        }
Пример #2
0
        internal SelectQuery(QueryBuilder builder) : base(builder)
        {
            // Get table name
            DbTableAttribute tableAttribute = typeof(T)
                                              .GetCustomAttributes <DbTableAttribute>(true)
                                              .FirstOrDefault();

            if (tableAttribute != null)
            {
                From(tableAttribute.Name);
            }
            else
            {
                From(typeof(T).Name);
            }

            foreach (MemberInfo member in typeof(T).GetValueMembers())
            {
                Type memberType = member.GetUnderlyingType();

                DbFieldAttribute      columnAttribute = member.GetCustomAttribute <DbFieldAttribute>(true);
                SelectIgnoreAttribute selectIgnore    = member.GetCustomAttribute <SelectIgnoreAttribute>(true);

                if (columnAttribute != null && selectIgnore == null)
                {
                    Select(columnAttribute.Name);
                }
            }
        }
Пример #3
0
        internal CreateTableQuery(QueryBuilder builder) : base(builder, Utils.GetTableName <T>())
        {
            foreach (MemberInfo member in typeof(T).GetValueMembers())
            {
                Type memberType = member.GetUnderlyingType();

                DbFieldAttribute      columnAttribute = member.GetCustomAttribute <DbFieldAttribute>(true);
                CreateIgnoreAttribute createIgnore    = member.GetCustomAttribute <CreateIgnoreAttribute>(true);

                if (columnAttribute != null && createIgnore == null)
                {
                    Field(columnAttribute.Name, memberType, columnAttribute.Length, columnAttribute.Precision, columnAttribute.FieldProprties, columnAttribute.DefaultValue);
                }
            }
        }
Пример #4
0
        public UpdateQuery <T> Set(T obj)
        {
            List <MemberInfo> members = obj.GetType().GetValueMembers();

            foreach (MemberInfo member in members)
            {
                DbFieldAttribute      columnAttribute = member.GetCustomAttribute <DbFieldAttribute>(true);
                UpdateIgnoreAttribute updateIgnore    = member.GetCustomAttribute <UpdateIgnoreAttribute>(true);

                if (columnAttribute != null && updateIgnore == null)
                {
                    object value = member.GetValue(obj);

                    Set(columnAttribute.Name, value);
                }
            }

            return(this);
        }
Пример #5
0
        internal InsertQuery(QueryBuilder builder, T obj) : base(builder)
        {
            // Get table name
            DbTableAttribute tableAttribute = obj.GetType()
                                              .GetCustomAttributes <DbTableAttribute>(true)
                                              .FirstOrDefault();

            if (tableAttribute != null)
            {
                table = tableAttribute.Name;
            }
            else
            {
                table = obj.GetType().Name;
            }

            // Get custom fields
            List <MemberInfo> members = obj.GetType().GetValueMembers();

            foreach (MemberInfo member in members)
            {
                Type memberType = member.GetUnderlyingType();

                DbFieldAttribute      columnAttribute = member.GetCustomAttribute <DbFieldAttribute>(true);
                InsertIgnoreAttribute insertIgnore    = member.GetCustomAttribute <InsertIgnoreAttribute>(true);

                if (columnAttribute != null && insertIgnore == null)
                {
                    object value = member.GetValue(obj);

                    if (columnAttribute.ValidFormatter(memberType))
                    {
                        value = columnAttribute.FormatGetValue(memberType, value);
                    }

                    Value(columnAttribute.Name, value);
                }
            }
        }
Пример #6
0
        internal DeleteQuery(QueryBuilder builder, T obj) : base(builder)
        {
            // Get table name
            DbTableAttribute tableAttribute = obj.GetType()
                                              .GetCustomAttributes <DbTableAttribute>(true)
                                              .FirstOrDefault();

            if (tableAttribute != null)
            {
                From(tableAttribute.Name);
            }
            else
            {
                From(obj.GetType().Name);
            }

            List <MemberInfo> members = obj.GetType().GetValueMembers();

            foreach (MemberInfo member in members)
            {
                Type memberType = member.GetUnderlyingType();

                DbFieldAttribute     columnAttribute = member.GetCustomAttribute <DbFieldAttribute>(true);
                WhereIgnoreAttribute whereIgnore     = member.GetCustomAttribute <WhereIgnoreAttribute>(true);

                if (columnAttribute != null && whereIgnore == null)
                {
                    object value = member.GetValue(obj);

                    if (columnAttribute.ValidFormatter(memberType))
                    {
                        value = columnAttribute.FormatGetValue(memberType, value);
                    }

                    Where(columnAttribute.Name, value);
                }
            }
        }
Пример #7
0
        async Task <object> GetMemberValue(MemberInfo member, DbClient conn, ResultRow row)
        {
            Type memberType = member.GetUnderlyingType();

            DbFieldAttribute     columnAttribute    = member.GetCustomAttribute <DbFieldAttribute>(true);
            DbReferenceAttribute referenceAttribute = member.GetCustomAttribute <DbReferenceAttribute>(true);

            if (columnAttribute != null)
            {
                object value      = row[columnAttribute.Name ?? member.Name];
                string columnName = columnAttribute.Name ?? member.Name;

                if (columnAttribute.ValidFormatter(memberType))
                {
                    value = columnAttribute.FormatSetValue(memberType, value);
                }
                else if (memberType == typeof(DateTime))
                {
                    value = row.GetDateTime(columnName, default(DateTime));
                }
                else if (memberType == typeof(bool))
                {
                    value = row.GetBoolean(columnName);
                }

                if (!(value is DBNull))
                {
                    return(value);
                }
            }
            if (referenceAttribute != null)
            {
                object value = row[referenceAttribute.Column];

                ReferenceType refType = ReferenceType.Singular;

                if (memberType.IsGenericType && memberType.GetGenericTypeDefinition() == typeof(List <>))
                {
                    memberType = memberType.GetGenericArguments()[0];
                    refType    = ReferenceType.List;
                }
                else if (memberType.IsArray)
                {
                    memberType = memberType.GetElementType();
                    refType    = ReferenceType.Array;
                }

                object result = await(Task <object>) GetNestedMethod().MakeGenericMethod(memberType).Invoke(
                    this,
                    new object[]
                {
                    conn,
                    referenceAttribute.ForeignColumn,
                    value,
                    refType
                }
                    );

                if (!(value is DBNull))
                {
                    return(result);
                }
            }
            return(null);
        }