Пример #1
0
        public static void Increment_PropertyGet_MatchesCtorArgument()
        {
            const int initialValue = 10;
            const int increment    = 20;
            var       aliasAttr    = new AutoIncrementAttribute(initialValue, increment);

            Assert.That(aliasAttr.Increment, Is.EqualTo(increment));
        }
Пример #2
0
        static MappingMemberDescriptor GetAutoIncrementMemberDescriptor(TypeDescriptor typeDescriptor)
        {
            List <MappingMemberDescriptor> autoIncrementMemberDescriptors = typeDescriptor.MappingMemberDescriptors.Values.Where(a =>
            {
                AutoIncrementAttribute attr = (AutoIncrementAttribute)a.GetCustomAttribute(typeof(AutoIncrementAttribute));
                return(attr != null);
            }).ToList();

            if (autoIncrementMemberDescriptors.Count > 1)
            {
                throw new ChloeException(string.Format("Mapping type '{0}' can not define multiple identity members.", typeDescriptor.EntityType.FullName));
            }

            MappingMemberDescriptor autoIncrementMemberDescriptor = autoIncrementMemberDescriptors.FirstOrDefault();

            if (autoIncrementMemberDescriptor != null)
            {
                EnsureAutoIncrementMemberType(autoIncrementMemberDescriptor);
            }

            return(autoIncrementMemberDescriptor);
        }
Пример #3
0
        static MappingMemberDescriptor GetAutoIncrementMemberDescriptor(TypeDescriptor typeDescriptor)
        {
            var autoIncrementMemberDescriptors = typeDescriptor.MappingMemberDescriptors.Values.Where(a =>
            {
                AutoIncrementAttribute attr = (AutoIncrementAttribute)a.GetCustomAttribute(typeof(AutoIncrementAttribute));
                return(attr != null);
            }).ToList();

            if (autoIncrementMemberDescriptors.Count > 1)
            {
                throw new Exception(string.Format("实体类型 {0} 定义多个自增成员", typeDescriptor.EntityType.FullName));
            }

            var autoIncrementMemberDescriptor = autoIncrementMemberDescriptors.FirstOrDefault();

            if (autoIncrementMemberDescriptor != null)
            {
                EnsureAutoIncrementMemberType(autoIncrementMemberDescriptor);
            }

            return(autoIncrementMemberDescriptor);
        }
Пример #4
0
        public override int Update <T>(Expression <Func <T, T> > body, Expression <Func <T, bool> > condition)
        {
            Utils.CheckNull(body);
            Utils.CheckNull(condition);

            TypeDescriptor typeDescriptor = TypeDescriptor.GetDescriptor(typeof(T));

            Dictionary <MemberInfo, Expression> updateColumns = InitMemberExtractor.Extract(body);
            DbExpression conditionExp = typeDescriptor.Visitor.VisitFilterPredicate(condition);

            DbUpdateExpression e = new DbUpdateExpression(typeDescriptor.Table, conditionExp);

            foreach (var kv in updateColumns)
            {
                MemberInfo key = kv.Key;
                MappingMemberDescriptor memberDescriptor = typeDescriptor.TryGetMappingMemberDescriptor(key);

                if (memberDescriptor == null)
                {
                    throw new ChloeException(string.Format("The member '{0}' does not map any column.", key.Name));
                }

                if (memberDescriptor.IsPrimaryKey)
                {
                    throw new ChloeException(string.Format("Could not update the primary key '{0}'.", memberDescriptor.Column.Name));
                }

                AutoIncrementAttribute attr = (AutoIncrementAttribute)memberDescriptor.GetCustomAttribute(typeof(AutoIncrementAttribute));
                if (attr != null)
                {
                    throw new ChloeException(string.Format("Could not update the identity column '{0}'.", memberDescriptor.Column.Name));
                }

                e.UpdateColumns.Add(memberDescriptor.Column, typeDescriptor.Visitor.Visit(kv.Value));
            }

            return(this.ExecuteSqlCommand(e));
        }
Пример #5
0
        public InsertQueryType <T> Insert(object obj, bool replace = false)
        {
            var properties = obj.GetType().GetProperties();

            foreach (var property in properties)
            {
                string propertyName = property.Name;

                object value           = property.GetValue(obj);
                bool   isAutoincrement = AutoIncrementAttribute.IsAutoIncrement(property);

                if (value != null && !isAutoincrement)
                {
                    Insert(propertyName, value);
                }
            }

            if (replace)
            {
                ShouldReplace = true;
            }

            return(this);
        }
Пример #6
0
        public override string GenerateQuery(Type type)
        {
            string tableName = TableAttribute.GetAttributeTable(type);

            StringBuilder queryBuilder = new StringBuilder($"CREATE TABLE IF NOT EXISTS `{tableName}` (");

            List <string> tableFields = new List <string>();
            List <string> primaryKeys = new List <string>();

            var properties = type.GetProperties();

            foreach (var property in properties)
            {
                string propertyTypeName = property.PropertyType.Name;
                string propertyName     = property.Name;

                string sqlType = null;

                try
                {
                    sqlType = CSharpTypesToSqlTypesDictionary[propertyTypeName];
                }
                catch (Exception)
                {
                    throw new NotSupportedException("The C# type " + propertyTypeName + " is not supported for C# to SQL conversion.");
                }

                string dataType = sqlType;

                int?columnLength = ColumnLengthAttribute.GetAttributeColumnLength(property);
                if (columnLength != null || propertyTypeName == "String")
                {
                    dataType = string.Format(dataType, columnLength ?? 50);
                }

                bool isAutoIncrement = AutoIncrementAttribute.IsAutoIncrement(property);
                if (isAutoIncrement)
                {
                    if (Database.DatabaseDriver is MySQLoo)
                    {
                        dataType += " AUTO_INCREMENT";
                    }
                }

                bool isPrimaryKey = PrimaryKeyAttribute.IsPrimaryKey(property);
                if (isPrimaryKey)
                {
                    // Check if is SQLite/MySQL
                    if (Database.DatabaseDriver is MySQLoo)
                    {
                        primaryKeys.Add(propertyName);
                    }
                    else if (Database.DatabaseDriver is SQLite)
                    {
                        dataType += " PRIMARY KEY";
                    }
                }

                tableFields.Add($"{propertyName} {dataType}");
            }

            queryBuilder.Append(string.Join(", ", tableFields));
            if (primaryKeys.Count > 0)
            {
                queryBuilder.Append(", PRIMARY KEY (");
                queryBuilder.Append(string.Join(", ", primaryKeys));
                queryBuilder.Append(")");
            }

            queryBuilder.Append(");");

            return(queryBuilder.ToString());
        }
Пример #7
0
        public override int Update <T>(T entity)
        {
            Utils.CheckNull(entity);

            TypeDescriptor typeDescriptor = TypeDescriptor.GetDescriptor(entity.GetType());

            EnsureMappingTypeHasPrimaryKey(typeDescriptor);

            object keyVal = null;
            MappingMemberDescriptor keyMemberDescriptor = typeDescriptor.PrimaryKey;
            MemberInfo keyMember = keyMemberDescriptor.MemberInfo;

            IEntityState entityState = this.TryGetTrackedEntityState(entity);
            Dictionary <MappingMemberDescriptor, DbExpression> updateColumns = new Dictionary <MappingMemberDescriptor, DbExpression>();

            foreach (var kv in typeDescriptor.MappingMemberDescriptors)
            {
                MemberInfo member = kv.Key;
                MappingMemberDescriptor memberDescriptor = kv.Value;

                if (member == keyMember)
                {
                    keyVal = memberDescriptor.GetValue(entity);
                    keyMemberDescriptor = memberDescriptor;
                    continue;
                }

                AutoIncrementAttribute attr = (AutoIncrementAttribute)memberDescriptor.GetCustomAttribute(typeof(AutoIncrementAttribute));
                if (attr != null)
                {
                    continue;
                }

                var val = memberDescriptor.GetValue(entity);

                if (entityState != null && !entityState.HasChanged(memberDescriptor, val))
                {
                    continue;
                }

                DbExpression valExp = DbExpression.Parameter(val, memberDescriptor.MemberInfoType);
                updateColumns.Add(memberDescriptor, valExp);
            }

            if (keyVal == null)
            {
                throw new ChloeException(string.Format("The primary key '{0}' could not be null.", keyMember.Name));
            }

            if (updateColumns.Count == 0)
            {
                return(0);
            }

            DbExpression left         = new DbColumnAccessExpression(typeDescriptor.Table, keyMemberDescriptor.Column);
            DbExpression right        = DbExpression.Parameter(keyVal, keyMemberDescriptor.MemberInfoType);
            DbExpression conditionExp = new DbEqualExpression(left, right);

            DbUpdateExpression e = new DbUpdateExpression(typeDescriptor.Table, conditionExp);

            foreach (var item in updateColumns)
            {
                e.UpdateColumns.Add(item.Key.Column, item.Value);
            }

            int ret = this.ExecuteSqlCommand(e);

            if (entityState != null)
            {
                entityState.Refresh();
            }
            return(ret);
        }
Пример #8
0
        public static void Ctor_UsingDefaultCtor_SetsInitialValueToOne()
        {
            var aliasAttr = new AutoIncrementAttribute();

            Assert.That(aliasAttr.InitialValue, Is.EqualTo(1));
        }