private Dictionary <string, IDbContextFilter> _filters;  // Key = database context name, which also becomes the sub-namespace used to encapsulate the many db contexts

        public bool ReadDbContextSettings(DatabaseReader reader, string singleDbContextSubNamespace = "")
        {
            _filters = new Dictionary <string, IDbContextFilter>();

            if (Settings.GenerateSingleDbContext)
            {
                // No need to read the database for settings, as they are provided by the user customisable class SingleContextFilter
                var filter = new SingleContextFilter {
                    SubNamespace = singleDbContextSubNamespace
                };
                _filters.Add(string.IsNullOrWhiteSpace(singleDbContextSubNamespace) ? string.Empty : singleDbContextSubNamespace, filter);

                return(true);
            }

            // Read in multi database context settings
            if (!string.IsNullOrWhiteSpace(Settings.MultiContextSettingsPlugin))
            {
                // Use plugin
                // We know the plugin implements (IMultiContextSettingsPlugin) interface but we can't direct cast it so we copy the object
                var plugin = AssemblyHelper.LoadPlugin(Settings.MultiContextSettingsPlugin);

                var pluginType = plugin.GetType();
                // remoteSettingsListObject is a List<MultiContextSettings> object
                var remoteSettingsListObject = (IList)pluginType.InvokeMember("ReadSettings", System.Reflection.BindingFlags.InvokeMethod, null, plugin, null);

                _multiContextSettings = new List <MultiContextSettings>();
                var remoteSettingsList = remoteSettingsListObject.Cast <object>();

                foreach (var remoteSettings in remoteSettingsList)
                {
                    var multiContextSettings = new MultiContextSettings();
                    MultiContextSettingsCopy.Copy(remoteSettings, multiContextSettings);
                    _multiContextSettings.Add(multiContextSettings);
                }
            }
            else
            {
                if (reader == null)
                {
                    return(false);
                }

                // Read from database
                _multiContextSettings = reader.ReadMultiContextSettings();
            }

            if (_multiContextSettings == null || _multiContextSettings.Count == 0)
            {
                return(false);
            }

            foreach (var setting in _multiContextSettings)
            {
                var filter = new MultiContextFilter(setting);
                if (!string.IsNullOrWhiteSpace(setting.Filename) && !_filters.ContainsKey(setting.Filename))
                {
                    _filters.Add(setting.Filename, filter);
                }
                else
                {
                    _filters.Add(setting.Name, filter);
                }
            }

            return(true);
        }
예제 #2
0
        private readonly List <string> _allowedFunctions;                     // Table/Scalar valued functions

        public MultiContextFilter(MultiContextSettings settings)
        {
            _settings = settings;

            IncludeViews                 = settings.IncludeViews();
            IncludeSynonyms              = false;
            IncludeTableValuedFunctions  = settings.IncludeFunctions(); // If true, for EF6 install the "EntityFramework.CodeFirstStoreFunctions" Nuget Package.
            IncludeScalarValuedFunctions = IncludeTableValuedFunctions; // Scalar/Table function filters are not separate in this filter.
            IncludeStoredProcedures      = IncludeScalarValuedFunctions || IncludeTableValuedFunctions || settings.IncludeStoredProcedures();
            SubNamespace                 = settings.GetNamespace();

            _allowedSchemas     = new List <string>();
            _allowedTables      = new List <string>();
            _allowedColumns     = new Dictionary <string, List <string> >();
            _allowedStoredProcs = new List <string>();
            _allowedFunctions   = new List <string>();

            // Pre-process the settings
            _normalisation = new MultiContextNameNormalisation(settings.BaseSchema);
            _allowedSchemas.Add(_normalisation.DefaultSchema);

            // Tables
            foreach (var t in settings.Tables)
            {
                var           tableName   = _normalisation.Normalise(t.Name);
                SchemaAndName tableDbName = null;
                if (!string.IsNullOrEmpty(t.DbName))
                {
                    t.DbName    = t.DbName.Replace("[", "").Replace("]", "");
                    tableDbName = _normalisation.Normalise(t.DbName);
                    _allowedTables.Add(tableDbName.ToString());

                    // Override schema with the one defined in DbName
                    tableName.Schema = tableDbName.Schema;
                }
                _allowedTables.Add(tableName.ToString());


                SchemaAndName tablePluralName = null;
                if (!string.IsNullOrEmpty(t.PluralName))
                {
                    tablePluralName        = _normalisation.Normalise(t.PluralName);
                    tablePluralName.Schema = tableName.Schema;
                    _allowedTables.Add(tablePluralName.ToString());
                }


                var cols = new List <string>();
                foreach (var c in t.Columns)
                {
                    var columnName = _normalisation.Normalise(c.Name);
                    cols.Add(columnName.Name.ToLowerInvariant());

                    if (!string.IsNullOrEmpty(c.DbName))
                    {
                        c.DbName = c.DbName.Replace("[", "").Replace("]", "");
                        var columnDbName = _normalisation.Normalise(c.DbName);
                        cols.Add(columnDbName.Name.ToLowerInvariant());
                    }
                }

                if (cols.Any())
                {
                    cols = cols.Distinct().ToList();
                    _allowedColumns.Add(tableName.ToString(), cols);

                    if (tableDbName != null && !_allowedColumns.ContainsKey(tableDbName.ToString()))
                    {
                        _allowedColumns.Add(tableDbName.ToString(), cols);
                    }

                    if (tablePluralName != null && !_allowedColumns.ContainsKey(tablePluralName.ToString()))
                    {
                        _allowedColumns.Add(tablePluralName.ToString(), cols);
                    }
                }
            }
            _allowedTables = _allowedTables.Distinct().ToList();

            // Find schemas used in tables
            var tableSchemas = _allowedTables
                               .Where(x => x.Contains('.'))
                               .Select(t => t.Split('.').First().ToLowerInvariant())
                               .Distinct()
                               .ToList();

            _allowedSchemas.AddRange(tableSchemas);
            _allowedSchemas = _allowedSchemas.Distinct().ToList();


            // Stored procedures
            foreach (var sp in settings.StoredProcedures)
            {
                var spName = _normalisation.Normalise(sp.Name);
                if (!string.IsNullOrEmpty(sp.DbName))
                {
                    var spDbName = _normalisation.Normalise(sp.DbName);
                    _allowedStoredProcs.Add(spDbName.ToString());

                    // Override schema with the one defined in DbName
                    spName.Schema = spDbName.Schema;
                }
                _allowedStoredProcs.Add(spName.ToString());
            }
            _allowedStoredProcs = _allowedStoredProcs.Distinct().ToList();


            // Functions
            foreach (var f in settings.Functions)
            {
                var funcName = _normalisation.Normalise(f.Name);
                if (!string.IsNullOrEmpty(f.DbName))
                {
                    var funcDbName = _normalisation.Normalise(f.DbName);
                    _allowedFunctions.Add(funcDbName.ToString());

                    // Override schema with the one defined in DbName
                    funcName.Schema = funcDbName.Schema;
                }
                _allowedFunctions.Add(funcName.ToString());
            }
            _allowedFunctions = _allowedFunctions.Distinct().ToList();
        }