public void CreateStores(IReadOnlyCollection <DataTypeDescriptor> dataTypeDescriptors)
        {
            var dataTypes = DataTypeTypesManager.GetDataTypes(dataTypeDescriptors);

            var storesToCreate = new List <GeneratedTypesInfo>();

            foreach (var dataTypeDescriptor in dataTypeDescriptors)
            {
                Type interfaceType = dataTypes[dataTypeDescriptor.DataTypeId];

                storesToCreate.Add(BuildGeneratedTypesInfo(dataTypeDescriptor, interfaceType));
            }

            CompileMissingTypes(storesToCreate);

            foreach (var storeToCreate in storesToCreate)
            {
                var dataTypeDescriptor = storeToCreate.DataTypeDescriptor;

                InterfaceConfigurationManipulator.AddNew(_dataProviderContext.ProviderName, dataTypeDescriptor);

                var xmlDataTypeStoreCreator = new XmlDataTypeStoreCreator(_fileStoreDirectory);

                XmlDataTypeStore xmlDateTypeStore = xmlDataTypeStoreCreator.CreateStoreResult(
                    dataTypeDescriptor,
                    storeToCreate.DataProviderHelperClass,
                    storeToCreate.DataIdClass, null);

                Type interfaceType = storeToCreate.InterfaceType;

                AddDataTypeStore(dataTypeDescriptor, interfaceType, xmlDateTypeStore);
            }
        }
        public void DropStore(DataTypeDescriptor typeDescriptor)
        {
            XmlDataProviderDocumentCache.ClearCache();

            InterfaceConfigurationManipulator.Remove(_dataProviderContext.ProviderName, typeDescriptor);

            _dataTypeConfigurationElements = _dataTypeConfigurationElements.Where(s => s.DataTypeId != typeDescriptor.DataTypeId).Evaluate();
        }
Example #3
0
        public void AlterStore(UpdateDataTypeDescriptor updateDataTypeDescriptor, bool forceCompile)
        {
            var dataTypeChangeDescriptor = updateDataTypeDescriptor.CreateDataTypeChangeDescriptor();

            using (TimerProfilerFacade.CreateTimerProfiler())
            {
                SqlStoreManipulator.AlterStoresForType(updateDataTypeDescriptor);

                bool localizationChanged = dataTypeChangeDescriptor.AlteredType.Localizeable !=
                                           dataTypeChangeDescriptor.OriginalType.Localizeable;

                var oldElement = _interfaceConfigurationElements.Single(f => f.DataTypeId == updateDataTypeDescriptor.OldDataTypeDescriptor.DataTypeId);

                var newElement = InterfaceConfigurationManipulator.Change(_dataProviderContext.ProviderName, dataTypeChangeDescriptor, localizationChanged);
                if (newElement != null)
                {
                    _interfaceConfigurationElements.Remove(oldElement);
                    _interfaceConfigurationElements.Add(newElement);
                }

                if (forceCompile)
                {
                    Dictionary <DataTypeDescriptor, IEnumerable <SqlDataTypeStoreDataScope> > allSqlDataTypeStoreDataScopes = BuildAllExistingDataTypeStoreDataScopes();

                    InitializeStoreResult initializeStoreResult = InitializeStore(newElement ?? oldElement, allSqlDataTypeStoreDataScopes, true);

                    if (!updateDataTypeDescriptor.NewDataTypeDescriptor.IsCodeGenerated)
                    {
                        var interfaceType = updateDataTypeDescriptor.NewDataTypeDescriptor.GetInterfaceType();

                        if (!DataTypeValidationRegistry.IsValidForProvider(interfaceType, _dataProviderContext.ProviderName))
                        {
                            // Revalidating alternated static data type
                            _sqlDataTypeStoresContainer.RemoveKnownInterface(interfaceType);

                            DataTypeValidationRegistry.ClearValidationError(interfaceType, _dataProviderContext.ProviderName);

                            AddDataTypeStore(initializeStoreResult);
                        }
                    }
                }
            }
        }
Example #4
0
        public void RemoveLocale(CultureInfo cultureInfo)
        {
            var supportedInterfaces = GetSupportedInterfaces();

            foreach (var type in supportedInterfaces)
            {
                if (!DataLocalizationFacade.IsLocalized(type))
                {
                    continue;
                }

                var typeDesrciptor = DynamicTypeManager.GetDataTypeDescriptor(type);
                SqlStoreManipulator.RemoveLocale(_dataProviderContext.ProviderName, typeDesrciptor, cultureInfo);

                InterfaceConfigurationElement oldElement = _interfaceConfigurationElements.Where(f => f.DataTypeId == typeDesrciptor.DataTypeId).Single();

                InterfaceConfigurationElement newElement = InterfaceConfigurationManipulator.RefreshLocalizationInfo(_dataProviderContext.ProviderName, typeDesrciptor);

                _interfaceConfigurationElements.Remove(oldElement);
                _interfaceConfigurationElements.Add(newElement);
            }
        }
        public void AlterStore(UpdateDataTypeDescriptor updateDataTypeDescriptor, bool forceCompile)
        {
            XmlDataProviderDocumentCache.ClearCache();

            XmlProviderInterfaceConfigurationElement element = InterfaceConfigurationManipulator.Change(updateDataTypeDescriptor);

            if (forceCompile)
            {
                DataTypeDescriptor dataTypeDescriptor = updateDataTypeDescriptor.NewDataTypeDescriptor;

                Type dataProviderHelperType;
                Type dataIdClassType;
                bool typesExists = EnsureNeededTypes(dataTypeDescriptor, out dataProviderHelperType, out dataIdClassType, true);
                Verify.That(typesExists, "Could not find or code generated the type '{0}' or one of the needed helper types", dataTypeDescriptor.GetFullInterfaceName());

                var xmlDataTypeStoreDataScopes = new List <XmlDataTypeStoreDataScope>();
                foreach (DataScopeConfigurationElement dataScopeConfigurationElement in element.ConfigurationStores)
                {
                    var xmlDataTypeStoreDataScope = new XmlDataTypeStoreDataScope
                    {
                        DataScopeName = dataScopeConfigurationElement.DataScope,
                        CultureName   = dataScopeConfigurationElement.CultureName,
                        ElementName   = dataScopeConfigurationElement.ElementName,
                        Filename      = Path.Combine(_fileStoreDirectory, dataScopeConfigurationElement.Filename)
                    };

                    xmlDataTypeStoreDataScopes.Add(xmlDataTypeStoreDataScope);
                }

                var xmlDataTypeStoreCreator = new XmlDataTypeStoreCreator(_fileStoreDirectory);

                XmlDataTypeStore xmlDateTypeStore = xmlDataTypeStoreCreator.CreateStoreResult(dataTypeDescriptor, dataProviderHelperType, dataIdClassType, xmlDataTypeStoreDataScopes);

                Type interfaceType = DataTypeTypesManager.GetDataType(dataTypeDescriptor);

                UpdateDataTypeStore(dataTypeDescriptor, interfaceType, xmlDateTypeStore);
            }
        }
Example #6
0
        public void DropStore(DataTypeDescriptor dataTypeDescriptor)
        {
            using (TimerProfilerFacade.CreateTimerProfiler())
            {
                SqlStoreManipulator.DropStoresForType(_dataProviderContext.ProviderName, dataTypeDescriptor);

                InterfaceConfigurationManipulator.Remove(_dataProviderContext.ProviderName, dataTypeDescriptor);
                InterfaceConfigurationElement oldElement = _interfaceConfigurationElements.FirstOrDefault(f => f.DataTypeId == dataTypeDescriptor.DataTypeId);
                if (oldElement != null)
                {
                    _interfaceConfigurationElements.Remove(oldElement);
                }

                Guid dataTypeId    = dataTypeDescriptor.DataTypeId;
                int  storesRemoved = _createdSqlDataTypeStoreTables.RemoveAll(item => item.DataTypeId == dataTypeId);

                if (storesRemoved > 0)
                {
                    Type interfaceType = dataTypeDescriptor.GetInterfaceType();

                    _sqlDataTypeStoresContainer.ForgetInterface(interfaceType);
                }
            }
        }
Example #7
0
        public void CreateStores(IReadOnlyCollection <DataTypeDescriptor> dataTypeDescriptors)
        {
            var types = DataTypeTypesManager.GetDataTypes(dataTypeDescriptors);

            foreach (var dataTypeDescriptor in dataTypeDescriptors)
            {
                if (InterfaceConfigurationManipulator.ConfigurationExists(_dataProviderContext.ProviderName, dataTypeDescriptor))
                {
                    var filePath = InterfaceConfigurationManipulator.GetConfigurationFilePath(_dataProviderContext.ProviderName);

                    throw new InvalidOperationException(
                              $"SqlDataProvider configuration already contains a interface named '{dataTypeDescriptor.TypeManagerTypeName}', Id: '{dataTypeDescriptor.DataTypeId}'. Remove it from the configuration file '{filePath}' and restart the application.");
                }
            }

            // Creating Sql tables and adding to the configuration
            var configElements = new Dictionary <DataTypeDescriptor, InterfaceConfigurationElement>();

            foreach (var dataTypeDescriptor in dataTypeDescriptors)
            {
                Type type = types[dataTypeDescriptor.DataTypeId];

                Action <string> existingTablesValidator = tableName =>
                {
                    var errors        = new StringBuilder();
                    var interfaceType = type;
                    if (!ValidateTable(interfaceType, tableName, errors))
                    {
                        throw new InvalidOperationException("Table '{0}' already exist but isn't valid: {1}".FormatWith(tableName, errors.ToString()));
                    }
                };

                SqlStoreManipulator.CreateStoresForType(dataTypeDescriptor, existingTablesValidator);

                InterfaceConfigurationElement element = InterfaceConfigurationManipulator.AddNew(_dataProviderContext.ProviderName, dataTypeDescriptor);
                _interfaceConfigurationElements.Add(element);

                configElements.Add(dataTypeDescriptor, element);
            }


            // Generating necessary classes and performing validation
            Dictionary <DataTypeDescriptor, IEnumerable <SqlDataTypeStoreDataScope> > allSqlDataTypeStoreDataScopes = BuildAllExistingDataTypeStoreDataScopes();

            bool dataContextRecompilationNeeded = false;

            var toCompileList        = new List <HelperClassesGenerationInfo>();
            var generatedClassesInfo = new Dictionary <DataTypeDescriptor, InterfaceGeneratedClassesInfo>();

            Type dataContextClass = _sqlDataTypeStoresContainer.DataContextClass;

            foreach (var dataTypeDescriptor in dataTypeDescriptors)
            {
                var element = configElements[dataTypeDescriptor];

                // InitializeStoreResult initializeStoreResult = InitializeStore(element, allSqlDataTypeStoreDataScopes);

                HelperClassesGenerationInfo toCompile = null;

                var classesInfo = InitializeStoreTypes(element, allSqlDataTypeStoreDataScopes, dataContextClass, null, false, ref dataContextRecompilationNeeded, ref toCompile);

                if (classesInfo != null && toCompile != null)
                {
                    toCompileList.Add(toCompile);

                    generatedClassesInfo[dataTypeDescriptor] = classesInfo;
                }
            }

            // Compiling missing classes
            if (toCompileList.Any())
            {
                var codeGenerationBuilder = new CodeGenerationBuilder(_dataProviderContext.ProviderName + ":CreateStores");

                foreach (var toCompile in toCompileList)
                {
                    toCompile.GenerateCodeAction(codeGenerationBuilder);
                }

                var generatedHelperClasses = CodeGenerationManager.CompileRuntimeTempTypes(codeGenerationBuilder, false).ToArray();

                foreach (var toCompile in toCompileList)
                {
                    toCompile.PopulateFieldsAction(generatedHelperClasses);
                }
            }

            // Emitting a new DataContext class
            if (dataContextRecompilationNeeded)
            {
                var newDataTypeIds = new HashSet <Guid>(dataTypeDescriptors.Select(d => d.DataTypeId));

                _createdSqlDataTypeStoreTables.RemoveAll(f => newDataTypeIds.Contains(f.DataTypeId));

                var fields = _createdSqlDataTypeStoreTables.Select(s => new Tuple <string, Type>(s.DataContextFieldName, s.DataContextFieldType)).ToList();

                foreach (var classesInfo in generatedClassesInfo.Values)
                {
                    fields.AddRange(classesInfo.Fields.Select(f => new Tuple <string, Type>(f.Value.FieldName, f.Value.FieldType)));
                }

                dataContextClass = DataContextAssembler.EmitDataContextClass(fields);

                UpdateCreatedSqlDataTypeStoreTables(dataContextClass);
            }

            _sqlDataTypeStoresContainer.DataContextClass = dataContextClass;

            // Registering the new type/tables
            foreach (var dataTypeDescriptor in dataTypeDescriptors)
            {
                InterfaceGeneratedClassesInfo classesInfo;

                if (!generatedClassesInfo.TryGetValue(dataTypeDescriptor, out classesInfo))
                {
                    throw new InvalidOperationException("No generated classes for data type '{0}' found".FormatWith(dataTypeDescriptor.Name));
                }
                InitializeStoreResult initInfo = EmbedDataContextInfo(classesInfo, dataContextClass);

                AddDataTypeStore(initInfo, false);
            }
        }
        public void RemoveLocale(CultureInfo cultureInfo)
        {
            XmlDataProviderDocumentCache.ClearCache();

            InterfaceConfigurationManipulator.RemoveLocale(_dataProviderContext.ProviderName, this.GetSupportedInterfaces(), cultureInfo);
        }
        public IDataProvider Assemble(IBuilderContext context, DataProviderData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
        {
            var sqlDataProviderData = (SqlDataProviderData)objectConfiguration;

            string configFilePath = InterfaceConfigurationManipulator.GetConfigurationFilePath(sqlDataProviderData.Name);
            var    configuration  = new C1Configuration(configFilePath);

            var section = configuration.GetSection(SqlDataProviderConfigurationSection.SectionName) as SqlDataProviderConfigurationSection;

            if (section == null)
            {
                section = new SqlDataProviderConfigurationSection();
                configuration.Sections.Add(SqlDataProviderConfigurationSection.SectionName, section);
                configuration.Save();
            }


            var interfaceConfigurationElements = new List <InterfaceConfigurationElement>();

            foreach (InterfaceConfigurationElement table in section.Interfaces)
            {
                interfaceConfigurationElements.Add(table);
            }

            var sqlLoggingContext = new SqlLoggingContext
            {
                Enabled        = sqlDataProviderData.SqlQueryLoggingEnabled,
                IncludeStack   = sqlDataProviderData.SqlQueryLoggingIncludeStack,
                TypesToIgnore  = new List <Type>(),
                TablesToIgnore = new List <string>()
            };

            if (sqlDataProviderData.SqlQueryLoggingEnabled)
            {
                foreach (LoggingIgnoreInterfacesConfigurationElement element in sqlDataProviderData.LoggingIgnoreInterfaces)
                {
                    Type interfaceType = TypeManager.TryGetType(element.InterfaceType);
                    if (interfaceType != null)
                    {
                        sqlLoggingContext.TypesToIgnore.Add(interfaceType);

                        InterfaceConfigurationElement interfaceElement = interfaceConfigurationElements.SingleOrDefault(f => f.DataTypeId == interfaceType.GetImmutableTypeId());
                        if (interfaceElement == null)
                        {
                            continue;
                        }

                        foreach (StoreConfigurationElement store in interfaceElement.ConfigurationStores)
                        {
                            sqlLoggingContext.TablesToIgnore.Add(store.TableName);
                        }
                    }
                }
            }


            string connectionString = sqlDataProviderData.ConnectionString;

            if (string.IsNullOrEmpty(connectionString))
            {
                string connectionStringName = sqlDataProviderData.ConnectionStringName;

                if (string.IsNullOrEmpty(connectionStringName))
                {
                    throw new ConfigurationErrorsException("SqlDataProvider requires one of the following properties to be specified: 'connectionString', 'connectionStringName'");
                }

                var connStringConfigNode = System.Web.Configuration.WebConfigurationManager.ConnectionStrings[connectionStringName];
                Verify.IsNotNull(connStringConfigNode, "Failed to find an SQL connection string by name '{0}'", connectionStringName);

                connectionString = connStringConfigNode.ConnectionString;
            }

            return(new SqlDataProvider(connectionString, interfaceConfigurationElements, sqlLoggingContext));
        }