Пример #1
0
        public List <MultiContextSettings> ReadMultiContextSettings()
        {
            var result = new List <MultiContextSettings>();

            using (var conn = _factory.CreateConnection())
            {
                if (conn == null)
                {
                    return(result);
                }

                conn.ConnectionString = string.IsNullOrWhiteSpace(Settings.MultiContextSettingsConnectionString) ? Settings.ConnectionString : Settings.MultiContextSettingsConnectionString;
                conn.Open();

                var cmd = GetCmd(conn);
                if (cmd == null)
                {
                    return(result);
                }

                var sql = MultiContextSQL();
                if (string.IsNullOrWhiteSpace(sql))
                {
                    return(result);
                }

                cmd.CommandText = MultiContextSQL();

                var contextMap = new Dictionary <int, MultiContextSettings>();
                var tableMap   = new Dictionary <int, MultiContextTableSettings>();

                using (var rdr = cmd.ExecuteReader())
                {
                    // Contexts
                    while (rdr.Read())
                    {
                        var contextId = GetReaderInt(rdr, "Id");
                        if (!contextId.HasValue)
                        {
                            continue; // Cannot use context
                        }
                        var c = new MultiContextSettings
                        {
                            // Store standard fields
                            Name         = GetReaderString(rdr, "Name"),
                            Namespace    = GetReaderString(rdr, "Namespace"),
                            Description  = GetReaderString(rdr, "Description"),
                            BaseSchema   = GetReaderString(rdr, "BaseSchema"),
                            TemplatePath = GetReaderString(rdr, "TemplatePath"),
                            Filename     = GetReaderString(rdr, "Filename"),
                            AllFields    = ReadAllFields(rdr),

                            Tables           = new List <MultiContextTableSettings>(),
                            StoredProcedures = new List <MultiContextStoredProcedureSettings>(),
                            Enumerations     = new List <EnumerationSettings>(),
                            Functions        = new List <MultiContextFunctionSettings>(),
                            ForeignKeys      = new List <MultiContextForeignKeySettings>()
                        };

                        contextMap.Add(contextId.Value, c);
                        result.Add(c);
                    }

                    if (!result.Any())
                    {
                        return(result);
                    }

                    // Tables
                    rdr.NextResult();
                    MultiContextSettings context;
                    while (rdr.Read())
                    {
                        var tableId = GetReaderInt(rdr, "Id");
                        if (!tableId.HasValue)
                        {
                            continue; // Cannot use table
                        }
                        var contextId = GetReaderInt(rdr, "ContextId");
                        if (!contextId.HasValue)
                        {
                            continue; // No context
                        }
                        if (!contextMap.ContainsKey(contextId.Value))
                        {
                            continue; // Context not found
                        }
                        context = contextMap[contextId.Value];

                        var t = new MultiContextTableSettings
                        {
                            Name          = GetReaderString(rdr, "Name"),
                            Description   = GetReaderString(rdr, "Description"),
                            PluralName    = GetReaderString(rdr, "PluralName"),
                            DbName        = GetReaderString(rdr, "DbName"),
                            Attributes    = GetReaderString(rdr, "Attributes"),
                            DbSetModifier = GetReaderString(rdr, "DbSetModifier"),
                            AllFields     = ReadAllFields(rdr),

                            Columns = new List <MultiContextColumnSettings>()
                        };

                        tableMap.Add(tableId.Value, t);
                        context.Tables.Add(t);
                    }

                    // Columns
                    rdr.NextResult();
                    while (rdr.Read())
                    {
                        var tableId = GetReaderInt(rdr, "TableId");
                        if (tableId == null)
                        {
                            continue; // Cannot use column as not associated to a table
                        }
                        if (!tableMap.ContainsKey(tableId.Value))
                        {
                            continue; // Table not found
                        }
                        var table = tableMap[tableId.Value];

                        var col = new MultiContextColumnSettings
                        {
                            Name             = GetReaderString(rdr, "Name"),
                            DbName           = GetReaderString(rdr, "DbName"),
                            IsPrimaryKey     = GetReaderBool(rdr, "IsPrimaryKey"),
                            OverrideModifier = GetReaderBool(rdr, "OverrideModifier"),
                            EnumType         = GetReaderString(rdr, "EnumType"),
                            Attributes       = GetReaderString(rdr, "Attributes"),
                            PropertyType     = GetReaderString(rdr, "PropertyType"),
                            IsNullable       = GetReaderBool(rdr, "IsNullable"),
                            AllFields        = ReadAllFields(rdr)
                        };

                        table.Columns.Add(col);
                    }

                    // Stored Procedures
                    rdr.NextResult();
                    while (rdr.Read())
                    {
                        var contextId = GetReaderInt(rdr, "ContextId");
                        if (!contextId.HasValue)
                        {
                            continue; // No context
                        }
                        if (!contextMap.ContainsKey(contextId.Value))
                        {
                            continue; // Context not found
                        }
                        context = contextMap[contextId.Value];

                        var sp = new MultiContextStoredProcedureSettings
                        {
                            Name        = GetReaderString(rdr, "Name"),
                            DbName      = GetReaderString(rdr, "DbName"),
                            ReturnModel = GetReaderString(rdr, "ReturnModel"),
                            AllFields   = ReadAllFields(rdr)
                        };

                        context.StoredProcedures.Add(sp);
                    }

                    // Functions
                    rdr.NextResult();
                    while (rdr.Read())
                    {
                        var contextId = GetReaderInt(rdr, "ContextId");
                        if (!contextId.HasValue)
                        {
                            continue; // No context
                        }
                        if (!contextMap.ContainsKey(contextId.Value))
                        {
                            continue; // Context not found
                        }
                        context = contextMap[contextId.Value];

                        var f = new MultiContextFunctionSettings
                        {
                            Name      = GetReaderString(rdr, "Name"),
                            DbName    = GetReaderString(rdr, "DbName"),
                            AllFields = ReadAllFields(rdr)
                        };

                        context.Functions.Add(f);
                    }

                    // Enumerations
                    rdr.NextResult();
                    while (rdr.Read())
                    {
                        var contextId = GetReaderInt(rdr, "ContextId");
                        if (!contextId.HasValue)
                        {
                            continue; // No context
                        }
                        if (!contextMap.ContainsKey(contextId.Value))
                        {
                            continue; // Context not found
                        }
                        context = contextMap[contextId.Value];

                        var e = new EnumerationSettings
                        {
                            Name       = GetReaderString(rdr, "Name"),
                            Table      = GetReaderString(rdr, "Table"),
                            NameField  = GetReaderString(rdr, "NameField"),
                            ValueField = GetReaderString(rdr, "ValueField"),
                            AllFields  = ReadAllFields(rdr)
                        };

                        context.Enumerations.Add(e);
                    }

                    // Foreign keys
                    rdr.NextResult();
                    while (rdr.Read())
                    {
                        var contextId = GetReaderInt(rdr, "ContextId");
                        if (!contextId.HasValue)
                        {
                            continue; // No context
                        }
                        if (!contextMap.ContainsKey(contextId.Value))
                        {
                            continue; // Context not found
                        }
                        context = contextMap[contextId.Value];

                        var fk = new MultiContextForeignKeySettings
                        {
                            ConstraintName      = GetReaderString(rdr, "ConstraintName"),
                            ParentName          = GetReaderString(rdr, "ParentName"),
                            ChildName           = GetReaderString(rdr, "ChildName"),
                            PkSchema            = GetReaderString(rdr, "PkSchema") ?? context.BaseSchema,
                            PkTableName         = GetReaderString(rdr, "PkTableName"),
                            PkColumn            = GetReaderString(rdr, "PkColumn"),
                            FkSchema            = GetReaderString(rdr, "FkSchema") ?? context.BaseSchema,
                            FkTableName         = GetReaderString(rdr, "FkTableName"),
                            FkColumn            = GetReaderString(rdr, "FkColumn"),
                            Ordinal             = GetReaderInt(rdr, "Ordinal") ?? 0,
                            CascadeOnDelete     = GetReaderBool(rdr, "CascadeOnDelete") ?? false,
                            IsNotEnforced       = GetReaderBool(rdr, "IsNotEnforced") ?? false,
                            HasUniqueConstraint = GetReaderBool(rdr, "HasUniqueConstraint") ?? false
                        };

                        context.ForeignKeys.Add(fk);
                    }
                }
            }

            return(result);
        }
Пример #2
0
        public static void Copy(object source, MultiContextSettings dest)
        {
            CopyPropertiesFrom(source, dest);

            var fromProperties = source.GetType().GetProperties();

            // Tables
            var fromProperty = fromProperties.FirstOrDefault(x => x.Name == nameof(MultiContextSettings.Tables));

            if (fromProperty != null)
            {
                var listValue = (IList)fromProperty.GetValue(source);
                if (listValue != null)
                {
                    dest.Tables = new List <MultiContextTableSettings>();

                    foreach (var item in listValue)
                    {
                        var tableSettings = new MultiContextTableSettings();
                        CopyMultiContextTableSettings(item, tableSettings);
                        dest.Tables.Add(tableSettings);
                    }
                }
            }

            // StoredProcedures
            fromProperty = fromProperties.FirstOrDefault(x => x.Name == nameof(MultiContextSettings.StoredProcedures));
            if (fromProperty != null)
            {
                var listValue = (IList)fromProperty.GetValue(source);
                if (listValue != null)
                {
                    dest.StoredProcedures = new List <MultiContextStoredProcedureSettings>();

                    foreach (var item in listValue)
                    {
                        var spSettings = new MultiContextStoredProcedureSettings();
                        CopyPropertiesFrom(item, spSettings);
                        dest.StoredProcedures.Add(spSettings);
                    }
                }
            }

            // Functions
            fromProperty = fromProperties.FirstOrDefault(x => x.Name == nameof(MultiContextSettings.Functions));
            if (fromProperty != null)
            {
                var listValue = (IList)fromProperty.GetValue(source);
                if (listValue != null)
                {
                    dest.Functions = new List <MultiContextFunctionSettings>();

                    foreach (var item in listValue)
                    {
                        var functionSettings = new MultiContextFunctionSettings();
                        CopyPropertiesFrom(item, functionSettings);
                        dest.Functions.Add(functionSettings);
                    }
                }
            }

            // Enumerations
            fromProperty = fromProperties.FirstOrDefault(x => x.Name == nameof(MultiContextSettings.Enumerations));
            if (fromProperty != null)
            {
                var listValue = (IList)fromProperty.GetValue(source);
                if (listValue != null)
                {
                    dest.Enumerations = new List <EnumerationSettings>();

                    foreach (var item in listValue)
                    {
                        var enumSettings = new EnumerationSettings();
                        CopyPropertiesFrom(item, enumSettings);
                        dest.Enumerations.Add(enumSettings);
                    }
                }
            }


            // ForeignKeys
            fromProperty = fromProperties.FirstOrDefault(x => x.Name == nameof(MultiContextSettings.ForeignKeys));
            if (fromProperty != null)
            {
                var listValue = (IList)fromProperty.GetValue(source);
                if (listValue != null)
                {
                    dest.ForeignKeys = new List <MultiContextForeignKeySettings>();

                    foreach (var item in listValue)
                    {
                        var fkSettings = new MultiContextForeignKeySettings();
                        CopyPropertiesFrom(item, fkSettings);
                        dest.ForeignKeys.Add(fkSettings);
                    }
                }
            }
        }