Exemple #1
0
 public EntityAssignment(IMemberMapping mm, Expression expression)
 {
     this.MemberMapping = mm;
     this.Member = mm.Member;
     System.Diagnostics.Debug.Assert(expression != null);
     this.Expression = expression;
 }
Exemple #2
0
 private Mapper(Type klassSrc, Type klassDst, Mapping mapping)
 {
     KlassSrc      = klassSrc;
     KlassDst      = klassDst;
     Mapping       = mapping;
     MemberMapping = mapping.CreateFor(klassSrc, klassDst);
 }
Exemple #3
0
 public EntityAssignment(IMemberMapping mm, Expression expression)
 {
     this.MemberMapping = mm;
     this.Member        = mm.Member;
     System.Diagnostics.Debug.Assert(expression != null);
     this.Expression = expression;
 }
Exemple #4
0
        private static void EmitMemberWrite(ILGenerator generator, IMemberMapping mapping)
        {
            generator.Emit(OpCodes.Ldarg_3);
            generator.Emit(OpCodes.Ldc_I4, mapping.LogicalIndex);
            generator.Emit(OpCodes.Ldarg_2);

            if (mapping.Member.MemberInfo is FieldInfo fieldInfo)
            {
                generator.Emit(OpCodes.Ldfld, fieldInfo);
                Type fieldType = fieldInfo.FieldType;
                if (!fieldType.GetTypeInfo().IsClass)
                {
                    generator.Emit(OpCodes.Box, fieldType);
                }
            }
            else if (mapping.Member.MemberInfo is PropertyInfo propertyInfo)
            {
                MethodInfo getter = propertyInfo.GetGetMethod(true);
                if (getter == null)
                {
                    string message = String.Format(null, Resources.WriteOnlyProperty, propertyInfo.Name);
                    throw new FlatFileException(message);
                }
                generator.Emit(OpCodes.Callvirt, getter);
                Type propertyType = propertyInfo.PropertyType;
                if (!propertyType.GetTypeInfo().IsClass)
                {
                    generator.Emit(OpCodes.Box, propertyType);
                }
            }
            generator.Emit(OpCodes.Stelem_Ref);
        }
        protected override Expression VisitMemberAccess(MemberExpression m)
        {
            Expression       source = this.Visit(m.Expression);
            EntityExpression ex     = source as EntityExpression;
            IMemberMapping   mm     = null;

            if (ex != null && (mm = ex.Entity.Get(m.Member)) != null && mm.IsRelationship)
            {
                ProjectionExpression projection = (ProjectionExpression)this.Visit(this.expressionBuilder.GetMemberExpression(source, ex.Entity, m.Member));
                if (this.currentFrom != null && mm.IsManyToOne)
                {
                    // convert singleton associations directly to OUTER APPLY
                    projection = this.expressionBuilder.AddOuterJoinTest(projection);
                    Expression newFrom = new JoinExpression(JoinType.OuterApply, this.currentFrom, projection.Select, null);
                    this.currentFrom = newFrom;
                    return(projection.Projector);
                }
                return(projection);
            }
            else
            {
                Expression       result = QueryBinder.BindMember(source, m.Member);
                MemberExpression mex    = result as MemberExpression;
                if (mex != null && mex.Member == m.Member && mex.Expression == m.Expression)
                {
                    return(m);
                }
                return(result);
            }
        }
Exemple #6
0
 internal CachedList(IList list, DataStore dataStore, IMemberMapping childPropertyMapping, object key)
     : base(list)
 {
     this.key = key;
     this.dataStore = dataStore;
     this.childPropertyMapping = childPropertyMapping;
 }
Exemple #7
0
 public IMapper Bind(Mapping m)
 {
     if (m != Mapping)
     {
         Mapping       = m;
         MemberMapping = m.CreateFor(KlassSrc, KlassDst);
     }
     //chosed to return the same instance because of the cache implementation
     return(this);
 }
 public override Expression GetGeneratedIdExpression(IMemberMapping member)
 {
     // the table name needs to be quoted, the column name does not.
     // http://archives.postgresql.org/pgsql-bugs/2007-01/msg00102.php
     //return new FunctionExpression(member.GetMemberType(), "CURRVAL",
     //    new FunctionExpression(typeof(object), "pg_get_serial_sequence",
     //        Expression.Constant(Quote(mapping.GetTableName(entity))),
     //        Expression.Constant(mapping.GetColumnName(entity, member))));
     throw new NotImplementedException();
 }
Exemple #9
0
 public override Expression GetGeneratedIdExpression(IMemberMapping member)
 {
     // the table name needs to be quoted, the column name does not.
     // http://archives.postgresql.org/pgsql-bugs/2007-01/msg00102.php
     //return new FunctionExpression(member.GetMemberType(), "CURRVAL",
     //    new FunctionExpression(typeof(object), "pg_get_serial_sequence",
     //        Expression.Constant(Quote(mapping.GetTableName(entity))),
     //        Expression.Constant(mapping.GetColumnName(entity, member))));
     throw new NotImplementedException();
 }
Exemple #10
0
        public Action <TEntity, object[]> GetWriter <TEntity>(IMemberMapping[] mappings)
        {
            Type          entityType = typeof(TEntity);
            DynamicMethod method     = new DynamicMethod(
                "__FlatFiles_TypeMapping_write",
                null,
                new Type[] { entityType, typeof(object[]) },
                true);
            var generator = method.GetILGenerator();

            for (int index = 0; index != mappings.Length; ++index)
            {
                IMemberMapping mapping = mappings[index];
                if (mapping.Member == null)
                {
                    continue;
                }

                generator.Emit(OpCodes.Ldarg_1);
                generator.Emit(OpCodes.Ldc_I4, mapping.WorkIndex);
                generator.Emit(OpCodes.Ldarg_0);

                if (mapping.Member.MemberInfo is FieldInfo fieldInfo)
                {
                    generator.Emit(OpCodes.Ldfld, fieldInfo);
                    Type fieldType = fieldInfo.FieldType;
                    if (!fieldType.GetTypeInfo().IsClass)
                    {
                        generator.Emit(OpCodes.Box, fieldType);
                    }
                }
                else if (mapping.Member.MemberInfo is PropertyInfo propertyInfo)
                {
                    MethodInfo getter = propertyInfo.GetGetMethod(true);
                    if (getter == null)
                    {
                        string message = String.Format(null, SharedResources.WriteOnlyProperty, propertyInfo.Name);
                        throw new FlatFileException(message);
                    }
                    generator.Emit(OpCodes.Callvirt, getter);
                    Type propertyType = propertyInfo.PropertyType;
                    if (!propertyType.GetTypeInfo().IsClass)
                    {
                        generator.Emit(OpCodes.Box, propertyType);
                    }
                }

                generator.Emit(OpCodes.Stelem_Ref);
            }
            generator.Emit(OpCodes.Ret);

            var result = (Action <TEntity, object[]>)method.CreateDelegate(typeof(Action <TEntity, object[]>));

            return(result);
        }
Exemple #11
0
        private static IColumnContext GetColumnContext(IRecordContext recordContext, IMemberMapping mapping)
        {
            var columnContext = new ColumnContext()
            {
                RecordContext = recordContext,
                PhysicalIndex = mapping.PhysicalIndex,
                LogicalIndex  = mapping.LogicalIndex
            };

            return(columnContext);
        }
 public StructMemberConverter(JsonSerializerOptions options, IMemberMapping memberMapping)
 {
     MemberNameAsString = memberMapping.MemberName;
     _memberName        = Encoding.UTF8.GetBytes(MemberNameAsString);
     _memberGetter      = GenerateGetter(memberMapping.MemberInfo);
     _memberSetter      = GenerateSetter(memberMapping.MemberInfo);
     _jsonConverter     = (JsonConverter <TM>)memberMapping.Converter;
     _defaultValue      = (TM)memberMapping.DefaultValue;
     _ignoreIfDefault   = memberMapping.IgnoreIfDefault;
     _requirementPolicy = memberMapping.RequirementPolicy;
 }
 // returns -1 if parameter is null
 public int Add(IMemberMapping value)
 {
     if (value != null && !hashByColumn.Contains(value.ColumnName)) {
         // throws NotSupportedException
         int i = List.Add(value);
         hashByColumn.Add(value.ColumnName, value);
         hashByProperty.Add(value.Name, value);
         return i;
     }
     else {
         return -1;
     }
 }
Exemple #14
0
        private IMemberAccessor getParentAccessor(IMemberMapping mapping)
        {
            string accessorName   = accessor?.Name ?? String.Empty;
            var    childAccessor  = mapping.Member;
            var    parentAccessor = childAccessor.ParentAccessor;

            while (parentAccessor != null && accessorName != parentAccessor.Name)
            {
                childAccessor  = parentAccessor;
                parentAccessor = childAccessor.ParentAccessor;
            }
            return(childAccessor);
        }
Exemple #15
0
        protected override string GetDefaultValue(IMemberMapping f, SqlType sqlType)
        {
            switch (sqlType.DbType)
            {
            case DBType.DateTime:
                break;

            case DBType.Guid:
                return(" DEFAULT NEWID()");
            }

            return(null);
        }
Exemple #16
0
        /// <summary>
        /// 构建特定映射对应的外键脚本
        /// </summary>
        /// <param name="member"></param>
        /// <returns></returns>
        protected virtual string BuildFKScript(IMemberMapping member)
        {
            var          mapping = member.Entity;
            var          m       = member as MemberMapping;
            const string fmt     = "ALTER TABLE {0}{1}  ADD CONSTRAINT FK_{2} FOREIGN KEY ({3}) REFERENCES {4}({5}){1}";

            return(string.Format(fmt
                                 , GetTableName(mapping)
                                 , Environment.NewLine
                                 , mapping.TableName.Replace(" ", "_") + "_" + member.Member.Name
                                 , m.thisKey
                                 , GetTableName(member.RelatedEntity)
                                 , m.otherKey));
        }
 public MemberConverter(JsonSerializerOptions options, IMemberMapping memberMapping)
 {
     MemberNameAsString              = memberMapping.MemberName;
     _memberName                     = Encoding.UTF8.GetBytes(MemberNameAsString);
     _memberGetter                   = GenerateGetter(memberMapping.MemberInfo);
     _memberSetter                   = GenerateSetter(memberMapping.MemberInfo);
     _jsonConverter                  = (JsonConverter <TM>)memberMapping.Converter;
     _defaultValue                   = (TM)memberMapping.DefaultValue;
     _ignoreIfDefault                = memberMapping.IgnoreIfDefault;
     _shouldSerializeMethod          = memberMapping.ShouldSerializeMethod;
     _requirementPolicy              = memberMapping.RequirementPolicy;
     _deserializableReadOnlyProperty = options.GetReadOnlyPropertyHandling() == ReadOnlyPropertyHandling.Read ||
                                       (memberMapping.MemberInfo.IsDefined(typeof(JsonDeserializeAttribute)) && options.GetReadOnlyPropertyHandling() == ReadOnlyPropertyHandling.Default);
 }
Exemple #18
0
 public Action <TEntity, object[]> GetWriter <TEntity>(IMemberMapping[] mappings)
 {
     void writer(TEntity entity, object[] values)
     {
         for (int index = 0; index != mappings.Length; ++index)
         {
             IMemberMapping mapping = mappings[index];
             if (mapping.Member != null)
             {
                 object value = mapping.Member.GetValue(entity);
                 values[mapping.WorkIndex] = value;
             }
         }
     };
     return(writer);
 }
Exemple #19
0
 public Action <TEntity, object[]> GetReader <TEntity>(IMemberMapping[] mappings)
 {
     void reader(TEntity entity, object[] values)
     {
         for (int index = 0; index != mappings.Length; ++index)
         {
             IMemberMapping mapping = mappings[index];
             if (mapping.Member != null)
             {
                 object value = values[mapping.WorkIndex];
                 mapping.Member.SetValue(entity, value);
             }
         }
     };
     return(reader);
 }
Exemple #20
0
 protected override string GetDefaultValue(IMemberMapping f, SqlType sqlType)
 {
     switch (sqlType.DbType)
     {
         case DBType.DateTime:
             break;
         case DBType.Guid:
             return " DEFAULT NEWID()";
         case DBType.Int16:
         case DBType.Int32:
         case DBType.Int64:
             if (f.IsPrimaryKey)
              return " AUTO_INCREMENT PRIMARY KEY";
             break;
     }
     return null;
 }
Exemple #21
0
        public Action <TEntity, object[]> GetReader <TEntity>(IMemberMapping[] mappings)
        {
            Type          entityType = typeof(TEntity);
            DynamicMethod method     = new DynamicMethod(
                "__FlatFiles_TypeMapping_read",
                typeof(void),
                new Type[] { entityType, typeof(object[]) },
                true);
            var generator = method.GetILGenerator();

            for (int index = 0; index != mappings.Length; ++index)
            {
                IMemberMapping mapping = mappings[index];
                if (mapping.Member == null)
                {
                    continue;
                }
                generator.Emit(OpCodes.Ldarg, 0);
                generator.Emit(OpCodes.Ldarg, 1);
                generator.Emit(OpCodes.Ldc_I4, mapping.WorkIndex);
                generator.Emit(OpCodes.Ldelem_Ref);

                if (mapping.Member.MemberInfo is FieldInfo fieldInfo)
                {
                    generator.Emit(OpCodes.Unbox_Any, fieldInfo.FieldType);
                    generator.Emit(OpCodes.Stfld, fieldInfo);
                }
                else if (mapping.Member.MemberInfo is PropertyInfo propertyInfo)
                {
                    MethodInfo setter = propertyInfo.GetSetMethod(true);
                    if (setter == null)
                    {
                        string message = String.Format(null, SharedResources.ReadOnlyProperty, propertyInfo.Name);
                        throw new FlatFileException(message);
                    }
                    generator.Emit(OpCodes.Unbox_Any, propertyInfo.PropertyType);
                    generator.Emit(OpCodes.Callvirt, setter);
                }
            }
            generator.Emit(OpCodes.Ret);

            var result = (Action <TEntity, object[]>)method.CreateDelegate(typeof(Action <TEntity, object[]>));

            return(result);
        }
        public StructMemberConverter(JsonSerializerOptions options, IMemberMapping memberMapping)
        {
            MemberInfo?memberInfo = memberMapping.MemberInfo;

            if (memberInfo == null)
            {
                throw new JsonException("MemberInfo must not be null");
            }

            MemberNameAsString = memberMapping.MemberName !;
            _memberName        = Encoding.UTF8.GetBytes(MemberNameAsString);
            _memberGetter      = GenerateGetter(memberInfo);
            _memberSetter      = GenerateSetter(memberInfo);
            _jsonConverter     = (JsonConverter <TM>)memberMapping.Converter !;
            _defaultValue      = (TM)memberMapping.DefaultValue !;
            _ignoreIfDefault   = memberMapping.IgnoreIfDefault;
            _requirementPolicy = memberMapping.RequirementPolicy;
        }
Exemple #23
0
        private void BuildColumn(StringBuilder sb, IMemberMapping f)
        {
            sb.AppendLine();
            sb.Append("\t").Append(Dialect.Quote(f.ColumnName));

            var sqlType = f.SqlType;

            sb.Append(" ").Append(GetDbType(f.SqlType));

            if (sqlType.Required || f.IsPrimaryKey)
            {
                sb.Append(" NOT NULL");
            }

            if (f.IsGenerated)
            {
                sb.Append(GetDefaultValue(f, sqlType));
            }
        }
Exemple #24
0
        public MemberConverter(CborConverterRegistry registry, IMemberMapping memberMapping)
        {
            MemberInfo?memberInfo = memberMapping.MemberInfo;

            if (memberInfo == null)
            {
                throw new CborException("MemberInfo must not be null");
            }

            _memberName            = Encoding.UTF8.GetBytes(memberMapping.MemberName !);
            _memberGetter          = GenerateGetter(memberInfo);
            _memberSetter          = GenerateSetter(memberInfo);
            _converter             = (ICborConverter <TM>)memberMapping.Converter !;
            _defaultValue          = (TM)memberMapping.DefaultValue !;
            _ignoreIfDefault       = memberMapping.IgnoreIfDefault;
            _shouldSerializeMethod = memberMapping.ShouldSerializeMethod;
            _lengthMode            = memberMapping.LengthMode;
            _requirementPolicy     = memberMapping.RequirementPolicy;
        }
Exemple #25
0
        public Action <IRecordContext, TEntity, object[]> GetReader <TEntity>(IMemberMapping[] mappings)
        {
            var entityType    = typeof(TEntity);
            var typeName      = GetUniqueTypeName($"{entityType.Name}Reader");
            var typeBuilder   = moduleBuilder.DefineType(typeName, TypeAttributes.Public | TypeAttributes.Sealed);
            var fieldBuilder  = typeBuilder.DefineField("mappings", typeof(IMemberMapping[]), FieldAttributes.Private);
            var ctorBuilder   = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new[] { typeof(IMemberMapping[]) });
            var ctorGenerator = ctorBuilder.GetILGenerator();

            ctorGenerator.Emit(OpCodes.Ldarg_0);
            ctorGenerator.Emit(OpCodes.Ldarg_1);
            ctorGenerator.Emit(OpCodes.Stfld, fieldBuilder);
            ctorGenerator.Emit(OpCodes.Ret);

            var methodBuilder   = typeBuilder.DefineMethod("Read", MethodAttributes.Public, null, new[] { typeof(IRecordContext), entityType, typeof(object[]) });
            var methodGenerator = methodBuilder.GetILGenerator();
            var contextBuilder  = methodGenerator.DeclareLocal(typeof(ColumnContext));
            var mappingBuilder  = methodGenerator.DeclareLocal(typeof(IMemberMapping));
            var indexBuilder    = methodGenerator.DeclareLocal(typeof(int));

            for (int index = 0; index != mappings.Length; ++index)
            {
                IMemberMapping mapping = mappings[index];
                if (mapping.Member != null)
                {
                    EmitMemberRead(methodGenerator, mapping);
                }
                else if (mapping.Reader != null)
                {
                    EmitCustomRead(methodGenerator, fieldBuilder, contextBuilder, mappingBuilder, indexBuilder, index);
                }
            }
            methodGenerator.Emit(OpCodes.Ret);

            var typeInfo = typeBuilder.CreateTypeInfo();
            var instance = Activator.CreateInstance(typeInfo.AsType(), (object)mappings);
            var readInfo = typeInfo.GetMethod(methodBuilder.Name);
            var reader   = (Action <IRecordContext, TEntity, object[]>)readInfo.CreateDelegate(typeof(Action <IRecordContext, TEntity, object[]>), instance);

            return(reader);
        }
Exemple #26
0
        protected override string GetDefaultValue(IMemberMapping f, SqlType sqlType)
        {
            switch (sqlType.DbType)
            {
            case DBType.DateTime:
                break;

            case DBType.Guid:
                return(" DEFAULT NEWID()");

            case DBType.Int16:
            case DBType.Int32:
            case DBType.Int64:
                if (f.IsPrimaryKey)
                {
                    return(" AUTO_INCREMENT PRIMARY KEY");
                }
                break;
            }
            return(null);
        }
Exemple #27
0
        public Action <IRecordContext, TEntity, object[]> GetWriter <TEntity>(IMemberMapping[] mappings)
        {
            void Writer(IRecordContext recordContext, TEntity entity, object[] values)
            {
                for (int index = 0; index != mappings.Length; ++index)
                {
                    IMemberMapping mapping = mappings[index];
                    if (mapping.Member != null)
                    {
                        object value = mapping.Member.GetValue(entity);
                        values[mapping.LogicalIndex] = value;
                    }
                    else if (mapping.Writer != null)
                    {
                        var columnContext = GetColumnContext(recordContext, mapping);
                        mapping.Writer(columnContext, entity, values);
                    }
                }
            }

            return(Writer);
        }
Exemple #28
0
        /// <summary>
        /// 得到缺省值
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="f"></param>
        /// <param name="sqlType"></param>
        protected virtual string GetDefaultValue(IMemberMapping f, SqlType sqlType)
        {
            switch (sqlType.DbType)
            {
            case DBType.DateTime:
                break;

            case DBType.Guid:
                return(" DEFAULT NEWID()");

            case DBType.Int16:
            case DBType.Int32:
            case DBType.Int64:
                if (f.IsPrimaryKey)
                {
                    return(" IDENTITY");
                }
                break;
            }

            return(null);
        }
Exemple #29
0
        private static void EmitMemberRead(ILGenerator generator, IMemberMapping mapping)
        {
            generator.Emit(OpCodes.Ldarg_2);
            generator.Emit(OpCodes.Ldarg_3);
            generator.Emit(OpCodes.Ldc_I4, mapping.LogicalIndex);
            generator.Emit(OpCodes.Ldelem_Ref);

            if (mapping.Member.MemberInfo is FieldInfo fieldInfo)
            {
                generator.Emit(OpCodes.Unbox_Any, fieldInfo.FieldType);
                generator.Emit(OpCodes.Stfld, fieldInfo);
            }
            else if (mapping.Member.MemberInfo is PropertyInfo propertyInfo)
            {
                MethodInfo setter = propertyInfo.GetSetMethod(true);
                if (setter == null)
                {
                    string message = String.Format(null, Resources.ReadOnlyProperty, propertyInfo.Name);
                    throw new FlatFileException(message);
                }
                generator.Emit(OpCodes.Unbox_Any, propertyInfo.PropertyType);
                generator.Emit(OpCodes.Callvirt, setter);
            }
        }
        /// <summary>
        /// 得到缺省值
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="f"></param>
        /// <param name="sqlType"></param>
        protected virtual string GetDefaultValue(IMemberMapping f, SqlType sqlType)
        {
            switch (sqlType.DbType)
            {
                case DBType.DateTime:
                    break;
                case DBType.Guid:
                    return " DEFAULT NEWID()";
                case DBType.Int16:
                case DBType.Int32:
                case DBType.Int64:
                    if (f.IsPrimaryKey)
                        return " IDENTITY";
                    break;
            }

            return null;
        }
        public override Expression GetGeneratedIdExpression(IMemberMapping member)
        {
            var sequenceName = string.IsNullOrEmpty(member.SequenceName) ? "NEXTID" : member.SequenceName;

            return(new FunctionExpression(member.MemberType, sequenceName + ".CURRVAL", null));
        }
        public virtual Expression GetMemberExpression(Expression root, IEntityMapping mapping, IMemberMapping mm)
        {
            var m = mm.Member;

            if (mm.IsRelationship)
            {
                IEntityMapping       relatedEntity = mm.RelatedEntity;
                ProjectionExpression projection    = this.GetQueryExpression(relatedEntity);

                // make where clause for joining back to 'root'
                var thisKeyMembers  = mm.ThisKeyMembers;
                var otherKeyMembers = mm.OtherKeyMembers;

                Expression where = null;
                for (int i = 0, n = otherKeyMembers.Length; i < n; i++)
                {
                    Expression equal =
                        this.GetMemberExpression(projection.Projector, relatedEntity, otherKeyMembers[i]).Equal(
                            this.GetMemberExpression(root, mapping, thisKeyMembers[i])
                            );
                    where = (where != null) ? where.And(equal) : equal;
                }

                TableAlias newAlias = new TableAlias();
                var        pc       = ColumnProjector.ProjectColumns(projection.Projector, null, newAlias, projection.Select.Alias);

                LambdaExpression aggregator = Aggregator.GetAggregator(mm.MemberType, typeof(IEnumerable <>).MakeGenericType(pc.Projector.Type));
                var result = new ProjectionExpression(
                    new SelectExpression(newAlias, pc.Columns, projection.Select, where),
                    pc.Projector, aggregator
                    );

                return(ApplyPolicy(result, m));
            }
            else
            {
                AliasedExpression aliasedRoot = root as AliasedExpression;
                if (aliasedRoot != null && mm.IsColumn)
                {
                    return(new ColumnExpression(mm.MemberType, mm.SqlType, aliasedRoot.Alias, mm.ColumnName));
                }
                return(QueryBinder.BindMember(root, m));
            }
        }
 public abstract Expression GetGeneratedIdExpression(IMemberMapping member);
Exemple #34
0
 /// <summary>
 /// 构建特定映射对应的检查约束脚本
 /// </summary>
 /// <param name="member"></param>
 /// <returns></returns>
 protected virtual string BuildCheckConstraintScript(IMemberMapping member)
 {
     return(null);
 }
 public override Expression GetGeneratedIdExpression(IMemberMapping member)
 {
     return new FunctionExpression(member.MemberType, "@@IDENTITY", null);
 }
 public void Insert(int index, IMemberMapping value)
 {
     if (value != null && !hashByColumn.Contains(value.ColumnName)) {
         // throws NotSupportedException
         List.Insert(index,value);
         hashByColumn.Add(value.ColumnName, value);
         hashByProperty.Add(value.Name, value);
     }
 }
 public override Expression GetGeneratedIdExpression(IMemberMapping member)
 {
     return(new FunctionExpression(member.MemberType, "SCOPE_IDENTITY()", null));
 }
 /// <summary>
 /// 构建特定映射对应的唯一性约束脚本
 /// </summary>
 /// <param name="member"></param>
 /// <returns></returns>
 protected virtual string BuildUniquleConstraintScript(IMemberMapping member)
 {
     return null;
 }
Exemple #39
0
 public MemberMappingError(MemberMappingErrorCode errorCode, IMemberMapping memberMapping)
 {
     this.memberMapping = memberMapping;
     this.errorCode = errorCode;
 }
        public virtual Expression GetMemberExpression(Expression root, IEntityMapping mapping, IMemberMapping mm)
        {
            var m = mm.Member;
            if (mm.IsRelationship)
            {
                IEntityMapping relatedEntity = mm.RelatedEntity;
                ProjectionExpression projection = this.GetQueryExpression(relatedEntity);

                // make where clause for joining back to 'root'
                var thisKeyMembers = mm.ThisKeyMembers;
                var otherKeyMembers = mm.OtherKeyMembers;

                Expression where = null;
                for (int i = 0, n = otherKeyMembers.Length; i < n; i++)
                {
                    Expression equal =
                        this.GetMemberExpression(projection.Projector, relatedEntity, otherKeyMembers[i]).Equal(
                            this.GetMemberExpression(root, mapping, thisKeyMembers[i])
                        );
                    where = (where != null) ? where.And(equal) : equal;
                }

                TableAlias newAlias = new TableAlias();
                var pc = ColumnProjector.ProjectColumns(projection.Projector, null, newAlias, projection.Select.Alias);

                LambdaExpression aggregator = Aggregator.GetAggregator(mm.MemberType, typeof(IEnumerable<>).MakeGenericType(pc.Projector.Type));
                var result = new ProjectionExpression(
                    new SelectExpression(newAlias, pc.Columns, projection.Select, where),
                    pc.Projector, aggregator
                    );

                return ApplyPolicy(result, m);
            }
            else
            {
                AliasedExpression aliasedRoot = root as AliasedExpression;
                if (aliasedRoot != null && mm.IsColumn)
                {
                    return new ColumnExpression(mm.MemberType, mm.SqlType, aliasedRoot.Alias, mm.ColumnName);
                }
                return QueryBinder.BindMember(root, m);
            }
        }
 public abstract Expression GetGeneratedIdExpression(IMemberMapping member);
 public override Expression GetGeneratedIdExpression(IMemberMapping member)
 {
     var sequenceName = string.IsNullOrEmpty(member.SequenceName) ? "NEXTID" : member.SequenceName;
     return new FunctionExpression(member.MemberType, sequenceName + ".CURRVAL", null);
 }
Exemple #43
0
 public EntityMapping(string tableName, Type entityType, IMemberMapping[] members)
 {
     this.tableName = tableName;
     this.entityType = entityType;
     this.members = members;
 }
 public override Expression GetGeneratedIdExpression(IMemberMapping member)
 {
     return new FunctionExpression(member.MemberType, "last_insert_rowid()", null);
 }
 public override Expression GetGeneratedIdExpression(IMemberMapping member)
 {
     return(new FunctionExpression(member.MemberType, "LAST_INSERT_ID()", null));
 }
        protected override string GetDefaultValue(IMemberMapping f, SqlType sqlType)
        {
            switch (sqlType.DbType)
            {
                case DBType.DateTime:
                    break;
                case DBType.Guid:
                    return " DEFAULT NEWID()";
            }

            return null;
        }
 /// <summary>
 /// 构建特定映射对应的主键脚本
 /// </summary>
 /// <param name="members"></param>
 /// <returns></returns>
 protected virtual string BuildPKScript(IMemberMapping[] members)
 {
     const string fmt = "ALTER TABLE {0} {1}\tADD CONSTRAINT PK_{2}  PRIMARY KEY CLUSTERED ({3}) ON [PRIMARY]{1}";
     var mapping = members[0].Entity;
     return string.Format(fmt
                          , GetTableName(mapping)
                          , Environment.NewLine
                          , mapping.TableName.Replace(" ", "_")
                          , members.Select(p => Dialect.Quote(p.ColumnName)).ToCSV(","));
 }
 public override Expression GetGeneratedIdExpression(IMemberMapping member)
 {
     return new FunctionExpression(member.MemberType, "LAST_INSERT_ID()", null);
 }
 /// <summary>
 /// 构建特定映射对应的外键脚本
 /// </summary>
 /// <param name="member"></param>
 /// <returns></returns>
 protected virtual string BuildFKScript(IMemberMapping member)
 {
     var mapping = member.Entity;
     var m = member as MemberMapping;
     const string fmt = "ALTER TABLE {0}{1}  ADD CONSTRAINT FK_{2} FOREIGN KEY ({3}) REFERENCES {4}({5}){1}";
     return string.Format(fmt
                     , GetTableName(mapping)
                     , Environment.NewLine
                     , mapping.TableName.Replace(" ", "_") + "_" + member.Member.Name
                     , m.thisKey
                     , GetTableName(member.RelatedEntity)
                     , m.otherKey);
 }
        private void BuildColumn(StringBuilder sb, IMemberMapping f)
        {
            sb.AppendLine();
            sb.Append("\t").Append(Dialect.Quote(f.ColumnName));

            var sqlType = f.SqlType;
            sb.Append(" ").Append(GetDbType(f.SqlType));

            if (sqlType.Required || f.IsPrimaryKey)
                sb.Append(" NOT NULL");

            if (f.IsGenerated)
                sb.Append(GetDefaultValue(f, sqlType));
        }