示例#1
0
 public static SqlPreCommand?InsertEnumValuesScript()
 {
     return((from t in Schema.Current.Tables.Values
             let enumType = EnumEntity.Extract(t.Type)
                            where enumType != null
                            select EnumEntity.GetEntities(enumType).Select((e, i) => t.InsertSqlSync(e, suffix: t.Name.Name + i)).Combine(Spacing.Simple)
             ).Combine(Spacing.Double)?.PlainSqlCommand());
 }
示例#2
0
        static SqlPreCommand SynchronizeEnumsScript(Replacements replacements)
        {
            Schema schema = Schema.Current;

            List <SqlPreCommand> commands = new List <SqlPreCommand>();

            foreach (var table in schema.Tables.Values)
            {
                Type enumType = EnumEntity.Extract(table.Type);
                if (enumType != null)
                {
                    IEnumerable <Entity>        should       = EnumEntity.GetEntities(enumType);
                    Dictionary <string, Entity> shouldByName = should.ToDictionary(a => a.ToString());

                    List <Entity> current = Administrator.TryRetrieveAll(table.Type, replacements);
                    Dictionary <string, Entity> currentByName = current.ToDictionaryEx(a => a.toStr, table.Name.Name);

                    string key = Replacements.KeyEnumsForTable(table.Name.Name);

                    replacements.AskForReplacements(currentByName.Keys.ToHashSet(), shouldByName.Keys.ToHashSet(), key);

                    currentByName = replacements.ApplyReplacementsToOld(currentByName, key);

                    var mix = shouldByName.JoinDictionary(currentByName, (n, s, c) => new { s, c }).Where(a => a.Value.s.id != a.Value.c.id).ToDictionary();

                    HashSet <PrimaryKey> usedIds = current.Select(a => a.Id).ToHashSet();

                    Dictionary <string, Entity> middleByName = mix.Where(kvp => usedIds.Contains(kvp.Value.s.Id)).ToDictionary(kvp => kvp.Key, kvp => Clone(kvp.Value.c));

                    if (middleByName.Any())
                    {
                        var moveToAux = SyncEnums(schema, table,
                                                  currentByName.Where(a => middleByName.ContainsKey(a.Key)).ToDictionary(),
                                                  middleByName);
                        if (moveToAux != null)
                        {
                            commands.Add(moveToAux);
                        }
                    }

                    var com = SyncEnums(schema, table,
                                        currentByName.Where(a => !middleByName.ContainsKey(a.Key)).ToDictionary(),
                                        shouldByName.Where(a => !middleByName.ContainsKey(a.Key)).ToDictionary());
                    if (com != null)
                    {
                        commands.Add(com);
                    }

                    if (middleByName.Any())
                    {
                        var backFromAux = SyncEnums(schema, table,
                                                    middleByName,
                                                    shouldByName.Where(a => middleByName.ContainsKey(a.Key)).ToDictionary());
                        if (backFromAux != null)
                        {
                            commands.Add(backFromAux);
                        }
                    }
                }
            }

            return(SqlPreCommand.Combine(Spacing.Double, commands.ToArray()));
        }