Exemplo n.º 1
0
 public TypeDescriptor(TypeDescriptorProvider typeDescriptorProvider, Type type)
 {
     this.Type = type;
     this.TypeDescriptorProvider    = typeDescriptorProvider;
     this.DataAccessObjectAttribute = type.GetFirstCustomAttribute <DataAccessObjectAttribute>(true);
     this.PersistedName             = this.DataAccessObjectAttribute.GetName(this, this.TypeDescriptorProvider.Configuration.NamingTransforms?.DataAccessObjectName);
 }
		public PostgresSqlDataTypeProvider(TypeDescriptorProvider typeDescriptorProvider, ConstraintDefaultsConfiguration constraintDefaultsConfiguration, bool nativeUuids, bool nativeEnums)
			: base(constraintDefaultsConfiguration)
		{
			this.typeDescriptorProvider = typeDescriptorProvider;

			this.NativeUuids = nativeUuids;
			this.NativeEnums = nativeEnums;
			
			this.blobSqlDataType = new DefaultBlobSqlDataType(constraintDefaultsConfiguration, "BYTEA");

			this.DefineSqlDataType(typeof(bool), "BOOLEAN", "GetBoolean");
			this.DefineSqlDataType(typeof(short), "SMALLINT", "GetInt16");
			this.DefineSqlDataType(typeof(int), "INTEGER", "GetInt32");

			this.DefineSqlDataType(typeof(ushort), "SMALLINT", "GetUInt16");
			this.DefineSqlDataType(typeof(uint), "INTEGER", "GetUInt32");
			this.DefineSqlDataType(typeof(ulong), "BIGINT", "GetUInt64");

			this.DefineSqlDataType(typeof(double), "DOUBLE PRECISION", "GetDouble");
			this.DefineSqlDataType(typeof(byte), "SMALLINT", "GetByte");
			this.DefineSqlDataType(typeof(sbyte), "SMALLINT", "GetByte");
			this.DefineSqlDataType(typeof(decimal), "NUMERIC(57, 28)", "GetDecimal");

			this.DefineSqlDataType(new UniversalTimeNormalisingDateTimeSqlDateType(this.ConstraintDefaultsConfiguration, "TIMESTAMP", false));
			this.DefineSqlDataType(new UniversalTimeNormalisingDateTimeSqlDateType(this.ConstraintDefaultsConfiguration, "TIMESTAMP", true));

			this.DefineSqlDataType(new PostgresTimespanSqlDataType(this.ConstraintDefaultsConfiguration, typeof(TimeSpan)));
			this.DefineSqlDataType(new PostgresTimespanSqlDataType(this.ConstraintDefaultsConfiguration, typeof(TimeSpan?)));

			if (nativeUuids)
			{
				this.DefineSqlDataType(new PostgresUuidSqlDataType(this.ConstraintDefaultsConfiguration, typeof(Guid)));
				this.DefineSqlDataType(new PostgresUuidSqlDataType(this.ConstraintDefaultsConfiguration, typeof(Guid?)));
			}
		}
        public override RuntimeDataAccessModelInfo GetDataAccessModelAssembly(Type dataAccessModelType, DataAccessModelConfiguration configuration)
        {
            var typeDescriptorProvider = new TypeDescriptorProvider(dataAccessModelType);
            var originalAssembly = dataAccessModelType.Assembly;
            var builtAssembly = BuildAssembly(typeDescriptorProvider, configuration);

            return new RuntimeDataAccessModelInfo(typeDescriptorProvider, builtAssembly, originalAssembly);
        }
Exemplo n.º 4
0
 public InjectionContext(DataAccessModel model, SqlDatabaseContextInfo contextInfo, Func <SqlDataTypeProvider> defaultProviderFactoryMethod, Func <string, Tuple <bool, object> > parameterNameToValue = null)
 {
     this.model       = model;
     this.contextInfo = contextInfo;
     this.defaultProviderFactoryMethod    = defaultProviderFactoryMethod;
     this.parameterNameToValue            = parameterNameToValue;
     this.typeDescriptorProvider          = this.model.TypeDescriptorProvider;
     this.constraintsDefaultConfiguration = this.model.Configuration.ConstraintDefaultsConfiguration;
 }
		private static Assembly BuildAssembly(TypeDescriptorProvider typeDescriptorProvider, DataAccessModelConfiguration configuration)
		{
			DataAccessObjectTypeBuilder dataAccessObjectTypeBuilder;

			var hash = configuration.GetSha1();
			var assemblyName = new AssemblyName(typeDescriptorProvider.DataAccessModelType.Assembly.GetName().Name + "." + typeDescriptorProvider.DataAccessModelType.Name);
			var sharedAssemblyName = new AssemblyName("Shaolinq.GeneratedDataAccessModel");
			var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(sharedAssemblyName, AssemblyBuilderAccess.RunAndSave);
			var moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name, assemblyName.Name + "." + hash + ".dll");

			var propertiesBuilder = moduleBuilder.DefineType("$$$DataAccessModelProperties", TypeAttributes.Class, typeof(object));
			
			var assemblyBuildContext = new AssemblyBuildContext(assemblyBuilder, propertiesBuilder);

			var dataAccessModelTypeBuilder = new DataAccessModelTypeBuilder(assemblyBuildContext, moduleBuilder);
			dataAccessModelTypeBuilder.BuildTypePhase1(typeDescriptorProvider.DataAccessModelType);

			var typeDescriptors = typeDescriptorProvider.GetTypeDescriptors();
			
			foreach (var typeDescriptor in typeDescriptors)
			{
				dataAccessObjectTypeBuilder = new DataAccessObjectTypeBuilder(typeDescriptorProvider, assemblyBuildContext, moduleBuilder, typeDescriptor.Type);
				dataAccessObjectTypeBuilder.Build(new DataAccessObjectTypeBuilder.TypeBuildContext(1));
			}

			foreach (var typeDescriptor in typeDescriptors)
			{
				dataAccessObjectTypeBuilder = assemblyBuildContext.TypeBuilders[typeDescriptor.Type];
				dataAccessObjectTypeBuilder.Build(new DataAccessObjectTypeBuilder.TypeBuildContext(2));
			}

			assemblyBuildContext.DataAccessModelPropertiesTypeBuilder.CreateType();
			dataAccessModelTypeBuilder.BuildTypePhase2();

			bool saveConcreteAssembly;
			bool.TryParse(ConfigurationManager.AppSettings["Shaolinq.SaveConcreteAssembly"], out saveConcreteAssembly);

#if DEBUG
			const bool isInDebugMode = true;
#else
			const bool isInDebugMode = false;
#endif

			// ReSharper disable once ConditionIsAlwaysTrueOrFalse
			if (saveConcreteAssembly || isInDebugMode)
			{
				ActionUtils.IgnoreExceptions(() => assemblyBuilder.Save(assemblyName + "." + hash + ".dll"));
			}

			return assemblyBuilder;
		}
        private static Assembly BuildAssembly(TypeDescriptorProvider typeDescriptorProvider, DataAccessModelConfiguration configuration)
        {
            DataAccessObjectTypeBuilder dataAccessObjectTypeBuilder;

            var configMd5 = configuration.GetMd5();
            var assemblyName = new AssemblyName(typeDescriptorProvider.DataAccessModelType.Assembly.GetName().Name + "." + typeDescriptorProvider.DataAccessModelType.Name);
            var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
            var moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name, assemblyName.Name + "." + configMd5 + ".dll");

            var assemblyBuildContext = new AssemblyBuildContext(assemblyBuilder);

            var dataAccessModelTypeBuilder = new DataAccessModelTypeBuilder(assemblyBuildContext, moduleBuilder);
            dataAccessModelTypeBuilder.BuildType(typeDescriptorProvider.DataAccessModelType);

            var typeDescriptors = typeDescriptorProvider.GetTypeDescriptors();

            foreach (var typeDescriptor in typeDescriptors)
            {
                dataAccessObjectTypeBuilder = new DataAccessObjectTypeBuilder(typeDescriptorProvider, assemblyBuildContext, moduleBuilder, typeDescriptor.Type);
                dataAccessObjectTypeBuilder.Build(new DataAccessObjectTypeBuilder.TypeBuildContext(1));
            }

            foreach (var typeDescriptor in typeDescriptors)
            {
                dataAccessObjectTypeBuilder = assemblyBuildContext.TypeBuilders[typeDescriptor.Type];
                dataAccessObjectTypeBuilder.Build(new DataAccessObjectTypeBuilder.TypeBuildContext(2));
            }

            bool saveConcreteAssembly;
            bool.TryParse(ConfigurationManager.AppSettings["Shaolinq.SaveConcreteAssembly"], out saveConcreteAssembly);

            #if DEBUG
            const bool isInDebugMode = true;
            #else
            const bool isInDebugMode = false;
            #endif

            if (saveConcreteAssembly || isInDebugMode)
            {
                ActionUtils.IgnoreExceptions(() => assemblyBuilder.Save(assemblyName + "." + configMd5 + ".dll"));
            }

            return assemblyBuilder;
        }
Exemplo n.º 7
0
        public ModelTypeDescriptor(TypeDescriptorProvider typeDescriptorProvider, Type type)
        {
            TypeDescriptorProvider = typeDescriptorProvider;
            this.Type = type;

            this.DataAccessModelAttribute = type.GetFirstCustomAttribute<DataAccessModelAttribute>(true);

            foreach (var propertyInfo in this.Type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                var queryableAttribute = propertyInfo.GetFirstCustomAttribute<DataAccessObjectsAttribute>(true);

                if (queryableAttribute == null)
                {
                    continue;
                }

                var propertyType = propertyInfo.PropertyType;
                Type genericType = null;

                while (propertyType != null)
                {
                    if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(DataAccessObjectsQueryable<>))
                    {
                        genericType = propertyType.GetGenericArguments()[0];
                    }

                    propertyType = propertyType.BaseType;
                }

                if (genericType == null)
                {
                    throw new ArgumentException("The DataAccessObjects queryable has no generic type");
                }

                var typeDescriptor = this.TypeDescriptorProvider.GetTypeDescriptor(genericType);

                if (typeDescriptor == null)
                {
                    throw new InvalidDataAccessObjectModelDefinition(string.Format("Type {0} is referenced by model but not resolvable", genericType.Name));
                }
            }
        }
Exemplo n.º 8
0
        public ModelTypeDescriptor(TypeDescriptorProvider typeDescriptorProvider, Type type)
        {
            this.TypeDescriptorProvider = typeDescriptorProvider;
            this.Type = type;

            this.DataAccessModelAttribute = type.GetFirstCustomAttribute <DataAccessModelAttribute>(true);

            foreach (var propertyInfo in this.Type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                var queryableAttribute = propertyInfo.GetFirstCustomAttribute <DataAccessObjectsAttribute>(true);

                if (queryableAttribute == null)
                {
                    continue;
                }

                var  propertyType = propertyInfo.PropertyType;
                Type genericType  = null;

                while (propertyType != null)
                {
                    if (propertyType.GetGenericTypeDefinitionOrNull() == typeof(DataAccessObjectsQueryable <>))
                    {
                        genericType = propertyType.GetGenericArguments()[0];
                    }

                    propertyType = propertyType.BaseType;
                }

                if (genericType == null)
                {
                    throw new ArgumentException("The DataAccessObjects queryable has no generic type");
                }

                var typeDescriptor = this.TypeDescriptorProvider.GetTypeDescriptor(genericType);

                if (typeDescriptor == null)
                {
                    throw new InvalidDataAccessObjectModelDefinition($"Type {genericType.Name} is referenced by model but not resolvable");
                }
            }
        }
Exemplo n.º 9
0
        public TypeDescriptor(TypeDescriptorProvider typeDescriptorProvider, Type type)
        {
            var propertyDescriptorsInOrder = new List <PropertyDescriptor>();

            this.Type = type;
            this.TypeDescriptorProvider = typeDescriptorProvider;

            this.DataAccessObjectAttribute = type.GetFirstCustomAttribute <DataAccessObjectAttribute>(true);

            this.relationshipInfos = new Dictionary <TypeDescriptor, TypeRelationshipInfo>();
            this.propertyDescriptorByColumnName   = new Dictionary <string, PropertyDescriptor>();
            this.propertyDescriptorByPropertyName = new Dictionary <string, PropertyDescriptor>();

            var alreadyEnteredProperties = new HashSet <string>();

            foreach (var propertyInfo in this.GetPropertiesInOrder())
            {
                if (alreadyEnteredProperties.Contains(propertyInfo.Name))
                {
                    continue;
                }

                alreadyEnteredProperties.Add(propertyInfo.Name);

                var attribute = propertyInfo.GetFirstCustomAttribute <PersistedMemberAttribute>(true);

                if (attribute != null)
                {
                    var propertyDescriptor = new PropertyDescriptor(this, type, propertyInfo);

                    if (propertyInfo.GetGetMethod() == null)
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} is missing a required getter method", propertyInfo.Name);
                    }

                    if (propertyInfo.GetSetMethod() == null && !propertyDescriptor.IsComputedTextMember && !propertyDescriptor.IsComputedMember)
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} is missing a required setter method", propertyInfo.Name);
                    }

                    if (!IsValidDataType(propertyInfo.PropertyType))
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} cannot have a return type of {1}", propertyInfo.Name, propertyInfo.PropertyType.Name);
                    }

                    if (!(propertyInfo.GetGetMethod().IsAbstract || propertyInfo.GetGetMethod().IsVirtual))
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} on {1} is not virtual or abstract", propertyInfo.Name, type.Name);
                    }

                    propertyDescriptorsInOrder.Add(propertyDescriptor);
                    this.propertyDescriptorByPropertyName[propertyInfo.Name] = propertyDescriptor;
                    this.propertyDescriptorByColumnName[attribute.GetName(propertyInfo, this)] = propertyDescriptor;
                }
            }

            var relatedProperties = new List <PropertyDescriptor>();

            foreach (var propertyInfo in this.Type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (propertyInfo.GetFirstCustomAttribute <DataAccessObjectsAttribute>(true) != null)
                {
                    throw new InvalidDataAccessObjectModelDefinition("The property {0} on {1} is decorated with a DataAccessObjects attribute.  Did you mean to you use RelatedDataAccessObjects attribute?", propertyInfo.Name, this.Type.Name);
                }

                if (propertyInfo.GetFirstCustomAttribute <RelatedDataAccessObjectsAttribute>(true) != null)
                {
                    if (!typeof(RelatedDataAccessObjects <>).IsAssignableFromIgnoreGenericParameters(propertyInfo.PropertyType.GetGenericTypeDefinition()))
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} on {1} is decorated with a RelatedDataAccessObjectsAttribute but the property type does not extend RelatedDataAccessObjects<OBJECT_TYPE>", this.Type.Name, propertyInfo.Name);
                    }

                    if (propertyInfo.GetSetMethod() != null)
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} is a related objects property and should not define a setter method", propertyInfo.Name);
                    }

                    if (propertyInfo.GetGetMethod() == null)
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} is missing a required getter method", propertyInfo.Name);
                    }

                    if (!(propertyInfo.GetGetMethod().IsAbstract || propertyInfo.GetGetMethod().IsVirtual))
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} on {1} is not virtual or abstract", propertyInfo.Name, type.Name);
                    }

                    var propertyDescriptor = new PropertyDescriptor(this, this.Type, propertyInfo);

                    relatedProperties.Add(propertyDescriptor);

                    this.propertyDescriptorByPropertyName[propertyInfo.Name] = propertyDescriptor;
                }
                else if (propertyInfo.GetFirstCustomAttribute <BackReferenceAttribute>(true) != null)
                {
                    if (!propertyInfo.PropertyType.IsDataAccessObjectType())
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} on {1} is decorated with a BackReference attribute but does not return a type that extends DataAccessObject<OBJECT_TYPE>", propertyInfo.Name, this.Type.Name);
                    }

                    if (propertyInfo.GetGetMethod() == null)
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} is missing a required getter method", propertyInfo.Name);
                    }

                    if (propertyInfo.GetSetMethod() == null)
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} is missing a required setter method", propertyInfo.Name);
                    }

                    if (!(propertyInfo.GetGetMethod().IsAbstract || propertyInfo.GetGetMethod().IsVirtual))
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} on {1} is not virtual or abstract", propertyInfo.Name, type.Name);
                    }

                    var propertyDescriptor = new PropertyDescriptor(this, this.Type, propertyInfo);

                    relatedProperties.Add(propertyDescriptor);

                    this.propertyDescriptorByPropertyName[propertyInfo.Name] = propertyDescriptor;
                }
            }

            this.RelatedProperties      = new ReadOnlyList <PropertyDescriptor>(relatedProperties);
            this.PersistedProperties    = new ReadOnlyList <PropertyDescriptor>(propertyDescriptorsInOrder);
            this.PrimaryKeyProperties   = new ReadOnlyList <PropertyDescriptor>(this.PersistedProperties.Where(propertyDescriptor => propertyDescriptor.IsPrimaryKey).ToList());
            this.ComputedTextProperties = new ReadOnlyList <PropertyDescriptor>(this.PersistedProperties.Where(c => c.IsComputedTextMember && !String.IsNullOrEmpty(c.ComputedTextMemberAttribute.Format)).ToList());
            this.ComputedProperties     = new ReadOnlyList <PropertyDescriptor>(this.PersistedProperties.Where(c => c.IsComputedMember && !String.IsNullOrEmpty(c.ComputedMemberAttribute.Expression)).ToList());
            this.PersistedAndRelatedObjectProperties = new ReadOnlyList <PropertyDescriptor>(this.PersistedProperties.Concat(this.RelatedProperties.Where(c => c.IsBackReferenceProperty)).ToList());

            if (this.PrimaryKeyProperties.Count(c => c.IsPropertyThatIsCreatedOnTheServerSide) > 1)
            {
                throw new InvalidDataAccessObjectModelDefinition("An object can only define one integer auto increment property");
            }
        }
Exemplo n.º 10
0
		public PostgresSqlQueryFormatter(SqlQueryFormatterOptions options, SqlDialect sqlDialect, SqlDataTypeProvider sqlDataTypeProvider, TypeDescriptorProvider typeDescriptorProvider, string schemaName, bool convertEnumsToText)
			: base(options, sqlDialect, sqlDataTypeProvider, typeDescriptorProvider)
		{
			this.schemaName = schemaName;
			this.ConvertEnumsToText = convertEnumsToText;
		}
Exemplo n.º 11
0
        public TypeDescriptor(TypeDescriptorProvider typeDescriptorProvider, Type type)
        {
            var propertyDescriptorsInOrder = new List<PropertyDescriptor>();

            this.Type = type;
            this.TypeDescriptorProvider = typeDescriptorProvider;

            this.DataAccessObjectAttribute = type.GetFirstCustomAttribute<DataAccessObjectAttribute>(true);

            this.relationshipInfos = new Dictionary<TypeDescriptor, TypeRelationshipInfo>();
            this.propertyDescriptorByColumnName = new Dictionary<string, PropertyDescriptor>();
            this.propertyDescriptorByPropertyName = new Dictionary<string, PropertyDescriptor>();

            var alreadyEnteredProperties = new HashSet<string>();

            foreach (var propertyInfo in this.GetPropertiesInOrder())
            {
                if (alreadyEnteredProperties.Contains(propertyInfo.Name))
                {
                    continue;
                }

                alreadyEnteredProperties.Add(propertyInfo.Name);

                var attribute = propertyInfo.GetFirstCustomAttribute<PersistedMemberAttribute>(true);

                if (attribute != null)
                {
                    var propertyDescriptor = new PropertyDescriptor(this, type, propertyInfo);

                    if (propertyInfo.GetGetMethod() == null)
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} is missing a required getter method", propertyInfo.Name);
                    }

                    if (propertyInfo.GetSetMethod() == null && !propertyDescriptor.IsComputedTextMember && !propertyDescriptor.IsComputedMember)
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} is missing a required setter method", propertyInfo.Name);
                    }

                    if (!IsValidDataType(propertyInfo.PropertyType))
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} cannot have a return type of {1}", propertyInfo.Name, propertyInfo.PropertyType.Name);
                    }

                    if (!(propertyInfo.GetGetMethod().IsAbstract || propertyInfo.GetGetMethod().IsVirtual))
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} on {1} is not virtual or abstract", propertyInfo.Name, type.Name);
                    }

                    propertyDescriptorsInOrder.Add(propertyDescriptor);
                    this.propertyDescriptorByPropertyName[propertyInfo.Name] = propertyDescriptor;
                    this.propertyDescriptorByColumnName[attribute.GetName(propertyInfo, this)] = propertyDescriptor;
                }
            }

            var relatedProperties = new List<PropertyDescriptor>();

            foreach (var propertyInfo in this.Type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (propertyInfo.GetFirstCustomAttribute<DataAccessObjectsAttribute>(true) != null)
                {
                    throw new InvalidDataAccessObjectModelDefinition("The property {0} on {1} is decorated with a DataAccessObjects attribute.  Did you mean to you use RelatedDataAccessObjects attribute?", propertyInfo.Name, this.Type.Name);
                }

                if (propertyInfo.GetFirstCustomAttribute<RelatedDataAccessObjectsAttribute>(true) != null)
                {
                    if (!typeof(RelatedDataAccessObjects<>).IsAssignableFromIgnoreGenericParameters(propertyInfo.PropertyType.GetGenericTypeDefinition()))
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} on {1} is decorated with a RelatedDataAccessObjectsAttribute but the property type does not extend RelatedDataAccessObjects<OBJECT_TYPE>", this.Type.Name, propertyInfo.Name);
                    }

                    if (propertyInfo.GetSetMethod() != null)
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} is a related objects property and should not define a setter method", propertyInfo.Name);
                    }

                    if (propertyInfo.GetGetMethod() == null)
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} is missing a required getter method", propertyInfo.Name);
                    }

                    if (!(propertyInfo.GetGetMethod().IsAbstract || propertyInfo.GetGetMethod().IsVirtual))
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} on {1} is not virtual or abstract", propertyInfo.Name, type.Name);
                    }

                    var propertyDescriptor = new PropertyDescriptor(this, this.Type, propertyInfo);

                    relatedProperties.Add(propertyDescriptor);

                    this.propertyDescriptorByPropertyName[propertyInfo.Name] = propertyDescriptor;
                }
                else if (propertyInfo.GetFirstCustomAttribute<BackReferenceAttribute>(true) != null)
                {
                    if (!propertyInfo.PropertyType.IsDataAccessObjectType())
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} on {1} is decorated with a BackReference attribute but does not return a type that extends DataAccessObject<OBJECT_TYPE>", propertyInfo.Name, this.Type.Name);
                    }

                    if (propertyInfo.GetGetMethod() == null)
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} is missing a required getter method", propertyInfo.Name);
                    }

                    if (propertyInfo.GetSetMethod() == null)
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} is missing a required setter method", propertyInfo.Name);
                    }

                    if (!(propertyInfo.GetGetMethod().IsAbstract || propertyInfo.GetGetMethod().IsVirtual))
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} on {1} is not virtual or abstract", propertyInfo.Name, type.Name);
                    }

                    var propertyDescriptor = new PropertyDescriptor(this, this.Type, propertyInfo);

                    relatedProperties.Add(propertyDescriptor);

                    this.propertyDescriptorByPropertyName[propertyInfo.Name] = propertyDescriptor;
                }
            }

            this.RelatedProperties = new ReadOnlyList<PropertyDescriptor>(relatedProperties);
            this.PersistedProperties = new ReadOnlyList<PropertyDescriptor>(propertyDescriptorsInOrder);
            this.PrimaryKeyProperties = new ReadOnlyList<PropertyDescriptor>(this.PersistedProperties.Where(propertyDescriptor => propertyDescriptor.IsPrimaryKey).ToList());
            this.ComputedTextProperties = new ReadOnlyList<PropertyDescriptor>(this.PersistedProperties.Where(c => c.IsComputedTextMember && !String.IsNullOrEmpty(c.ComputedTextMemberAttribute.Format)).ToList());
            this.ComputedProperties = new ReadOnlyList<PropertyDescriptor>(this.PersistedProperties.Where(c => c.IsComputedMember && !String.IsNullOrEmpty(c.ComputedMemberAttribute.Expression)).ToList());
            this.PersistedAndRelatedObjectProperties = new ReadOnlyList<PropertyDescriptor>(this.PersistedProperties.Concat(this.RelatedProperties.Where(c => c.IsBackReferenceProperty)).ToList());

            if (this.PrimaryKeyProperties.Count(c => c.IsPropertyThatIsCreatedOnTheServerSide) > 1)
            {
                throw new InvalidDataAccessObjectModelDefinition("An object can only define one integer auto increment property");
            }
        }
		public SqlServerSqlQueryFormatter(SqlQueryFormatterOptions options, SqlDialect sqlDialect, SqlDataTypeProvider sqlDataTypeProvider, TypeDescriptorProvider typeDescriptorProvider)
			: base(options, sqlDialect, sqlDataTypeProvider, typeDescriptorProvider)
		{
		}
Exemplo n.º 13
0
		public PostgresEnumSqlDataType(ConstraintDefaultsConfiguration constraintDefaultsConfiguration, Type supportedType, TypeDescriptorProvider typeDescriptorProvider)
			: base(constraintDefaultsConfiguration, supportedType, true)
		{
			this.typeDescriptorProvider = typeDescriptorProvider;
			this.underlyingType = Nullable.GetUnderlyingType(supportedType);
		}
		public static Expression Amend(TypeDescriptorProvider typeDescriptorProvider, Expression expression)
		{
			return new SqlServerIdentityInsertAndUpdateAmender(typeDescriptorProvider).Visit(expression);
		}
		public SqlServerIdentityInsertAndUpdateAmender(TypeDescriptorProvider typeDescriptorProvider)
		{
			this.typeDescriptorProvider = typeDescriptorProvider;
		}
Exemplo n.º 16
0
        public TypeDescriptor(TypeDescriptorProvider typeDescriptorProvider, Type type)
        {
            var propertyDescriptorsInOrder = new List <PropertyDescriptor>();

            this.Type = type;
            this.TypeDescriptorProvider = typeDescriptorProvider;

            this.DataAccessObjectAttribute = type.GetFirstCustomAttribute <DataAccessObjectAttribute>(true);

            var relatedProperties = new List <PropertyDescriptor>();

            this.relationshipInfos = new List <TypeRelationshipInfo>();
            this.propertyDescriptorByColumnName   = new Dictionary <string, PropertyDescriptor>();
            this.propertyDescriptorByPropertyName = new Dictionary <string, PropertyDescriptor>();

            var alreadyEnteredProperties = new HashSet <string>();

            this.PersistedName = this.DataAccessObjectAttribute.GetName(this, this.TypeDescriptorProvider.Configuration.NamingTransforms?.DataAccessObjectName);

            foreach (var propertyInfo in this.GetPropertiesInOrder())
            {
                if (alreadyEnteredProperties.Contains(propertyInfo.Name))
                {
                    continue;
                }

                alreadyEnteredProperties.Add(propertyInfo.Name);

                var attribute = (PersistedMemberAttribute)propertyInfo.GetCustomAttributes().FirstOrDefault(c => c is PersistedMemberAttribute);

                if (attribute != null)
                {
                    var propertyDescriptor = new PropertyDescriptor(this, type, propertyInfo);

                    if (propertyInfo.GetGetMethod() == null)
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} is missing a required getter method", propertyInfo.Name);
                    }

                    if (propertyInfo.GetSetMethod() == null && !propertyDescriptor.IsComputedTextMember && !propertyDescriptor.IsComputedMember)
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} is missing a required setter method", propertyInfo.Name);
                    }

                    if (!IsValidDataType(propertyInfo.PropertyType))
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} cannot have a return type of {1}", propertyInfo.Name, propertyInfo.PropertyType.Name);
                    }

                    if (!(propertyInfo.GetGetMethod().IsAbstract || propertyInfo.GetGetMethod().IsVirtual))
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} on {1} is not virtual or abstract", propertyInfo.Name, type.Name);
                    }

                    propertyDescriptorsInOrder.Add(propertyDescriptor);

                    this.propertyDescriptorByPropertyName[propertyInfo.Name] = propertyDescriptor;

                    if (propertyInfo.GetFirstCustomAttribute <BackReferenceAttribute>(true) != null)
                    {
                        if (!propertyInfo.PropertyType.IsDataAccessObjectType())
                        {
                            throw new InvalidDataAccessObjectModelDefinition("The property {0} on {1} is decorated with a BackReference attribute but does not return a type that extends DataAccessObject<OBJECT_TYPE>", propertyInfo.Name, this.Type.Name);
                        }

                        if (propertyInfo.GetGetMethod() == null)
                        {
                            throw new InvalidDataAccessObjectModelDefinition("The property {0} is missing a required getter method", propertyInfo.Name);
                        }

                        if (propertyInfo.GetSetMethod() == null)
                        {
                            throw new InvalidDataAccessObjectModelDefinition("The property {0} is missing a required setter method", propertyInfo.Name);
                        }

                        if (!(propertyInfo.GetGetMethod().IsAbstract || propertyInfo.GetGetMethod().IsVirtual))
                        {
                            throw new InvalidDataAccessObjectModelDefinition("The property {0} on {1} is not virtual or abstract", propertyInfo.Name, type.Name);
                        }

                        relatedProperties.Add(propertyDescriptor);
                    }
                }
            }

            foreach (var propertyInfo in this.Type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (propertyInfo.GetFirstCustomAttribute <DataAccessObjectsAttribute>(true) != null)
                {
                    throw new InvalidDataAccessObjectModelDefinition("The property {0} on {1} is decorated with a DataAccessObjects attribute.  Did you mean to you use RelatedDataAccessObjects attribute?", propertyInfo.Name, this.Type.Name);
                }

                if (propertyInfo.GetFirstCustomAttribute <RelatedDataAccessObjectsAttribute>(true) != null)
                {
                    if (!typeof(RelatedDataAccessObjects <>).IsAssignableFromIgnoreGenericParameters(propertyInfo.PropertyType.GetGenericTypeDefinition()))
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} on {1} is decorated with a RelatedDataAccessObjectsAttribute but the property type does not extend RelatedDataAccessObjects<T>", this.Type.Name, propertyInfo.Name);
                    }

                    if (propertyInfo.GetSetMethod() != null)
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} is a related objects property and should not define a setter method", propertyInfo.Name);
                    }

                    if (propertyInfo.GetGetMethod() == null)
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} is missing a required getter method", propertyInfo.Name);
                    }

                    if (!(propertyInfo.GetGetMethod().IsAbstract || propertyInfo.GetGetMethod().IsVirtual))
                    {
                        throw new InvalidDataAccessObjectModelDefinition("The property {0} on {1} is not virtual or abstract", propertyInfo.Name, type.Name);
                    }

                    var propertyDescriptor = new PropertyDescriptor(this, this.Type, propertyInfo);

                    relatedProperties.Add(propertyDescriptor);

                    this.propertyDescriptorByPropertyName[propertyInfo.Name] = propertyDescriptor;
                }
            }

            this.PersistedProperties                      = propertyDescriptorsInOrder;
            this.RelationshipRelatedProperties            = relatedProperties.ToReadOnlyCollection();
            this.PersistedPropertiesWithoutBackreferences = this.PersistedProperties.Where(c => !c.IsBackReferenceProperty).ToReadOnlyCollection();
            this.PrimaryKeyProperties                     = this.PersistedPropertiesWithoutBackreferences.Where(propertyDescriptor => propertyDescriptor.IsPrimaryKey).ToReadOnlyCollection();
            this.ComputedTextProperties                   = this.PersistedPropertiesWithoutBackreferences.Where(c => c.IsComputedTextMember && !string.IsNullOrEmpty(c.ComputedTextMemberAttribute.Format)).ToReadOnlyCollection();
            this.ComputedProperties = this.PersistedPropertiesWithoutBackreferences.Where(c => c.IsComputedMember && !string.IsNullOrEmpty(c.ComputedMemberAttribute.GetExpression)).ToReadOnlyCollection();

            this.PrimaryKeyDerivableProperties = this
                                                 .ComputedProperties
                                                 .Where(c => c.ComputedMemberAssignTarget != null)
                                                 .ToList();

            if (this.PrimaryKeyProperties.Count(c => c.IsPropertyThatIsCreatedOnTheServerSide) > 1)
            {
                throw new InvalidDataAccessObjectModelDefinition("An object can only define one integer auto increment property");
            }

            var organizationIdexes = this.PersistedProperties
                                     .Where(c => c.OrganizationIndexAttribute != null)
                                     .ToList();

            if (organizationIdexes.Count == 1)
            {
                if (organizationIdexes[0].OrganizationIndexAttribute.Disable && !organizationIdexes[0].IsPrimaryKey)
                {
                    throw new InvalidDataAccessObjectModelDefinition($"Disabling an organization/clustered requires {nameof(OrganizationIndexAttribute)} to be applied to a primary key property but is instead applied to the property '{organizationIdexes[0].PropertyName}'");
                }
            }
            else if (organizationIdexes.Count > 1)
            {
                if (organizationIdexes.Any(c => c.OrganizationIndexAttribute.Disable))
                {
                    throw new InvalidDataAccessObjectModelDefinition($"You have defined and/or disabled the organization/clustered index on {this.TypeName} multiple times. Remove one or more of the [{nameof(OrganizationIndexAttribute)}] attributes.");
                }
            }
        }
		public PostgresDotConnectSqlQueryFormatter(SqlQueryFormatterOptions options, SqlDialect sqlDialect, SqlDataTypeProvider sqlDataTypeProvider, TypeDescriptorProvider typeDescriptorProvider, string schemaName, bool convertEnumsToText)
			: base(options, sqlDialect, sqlDataTypeProvider, typeDescriptorProvider, schemaName, convertEnumsToText)
		{
		}