Beispiel #1
0
 public void DeSnake_BasicTest2()
 {
     Assert.AreEqual("NoNo5", StringHelper.AddNumberUntilUnique("NoNo5", "No", s => s == "NoNo5"));
     Assert.AreEqual("NoNo5", StringHelper.AddNumberUntilUnique("No", "No", s => s == "NoNo5"));
     Assert.AreEqual("NoNo5", StringHelper.AddNumberUntilUnique("NoNo2", "No", s => s == "NoNo5"));
     Assert.AreEqual("No2", StringHelper.AddNumberUntilUnique("No", "", s => s == "No2"));
 }
Beispiel #2
0
        private void EnsureTableNamesAreUnique(List <TableModel> result, string defaultSchema)
        {
            if (result.Count < 2)
            {
                return;
            }

            var dic = result
                      .Select((table, origIndex) =>
            {
                EnsureColumnNamesAreUnique(table);
                return(table, origIndex);
            })
                      //C# class names is case-sensitive but windows file system is not, so there might be class overwriting.
                      .GroupBy(t => t.table.Name, StringComparer.InvariantCultureIgnoreCase)
                      .ToDictionary(t => t.Key, t => t.ToList(), StringComparer.InvariantCultureIgnoreCase);

            foreach (var pair in dic.ToList())
            {
                while (pair.Value.Count > 1)
                {
                    var newName = pair.Key;

                    //Try add schema prefix
                    int?duplicateIndex = null;
                    for (int i = 0; i < pair.Value.Count; i++)
                    {
                        var next = pair.Value[i];
                        if (!string.Equals(next.table.DbName.Schema, defaultSchema, StringComparison.InvariantCultureIgnoreCase))
                        {
                            duplicateIndex = i;
                            break;
                        }
                    }
                    if (duplicateIndex != null)
                    {
                        var duplicate = pair.Value[duplicateIndex.Value];

                        var tableName = duplicate.table.Name;

                        if (!string.IsNullOrEmpty(this._options.TableClassPrefix))
                        {
                            tableName = tableName.Substring(this._options.TableClassPrefix.Length);
                        }

                        newName = this._options.TableClassPrefix + StringHelper.DeSnake(duplicate.table.DbName.Schema) + tableName;
                    }

                    newName = StringHelper.AddNumberUntilUnique(newName, "No", nn => !dic.ContainsKey(nn));

                    duplicateIndex ??= 1;//Second

                    var duplicateRes = pair.Value[duplicateIndex.Value];

                    var newTable = duplicateRes.table.WithNewName(newName);

                    result[duplicateRes.origIndex] = newTable;

                    dic.Add(newTable.Name, new List <(TableModel table, int origIndex)>(1)
                    {
                        (newTable, duplicateRes.origIndex)
                    });