Esempio n. 1
0
        public void Test_ContainsBrackets(string name, bool expectedValue)
        {
            Game game = new Game()
            {
                Name = name
            };

            NameUpdater nameUpdater = new NameUpdater();
            bool        actualValue = nameUpdater.ContainsBrackets(game);

            Assert.Equal(expectedValue, actualValue);
        }
Esempio n. 2
0
        public void Test_RemoveSpaceBeforeColons()
        {
            Game game = new Game()
            {
                Name = "The Game : Subtitle"
            };

            NameUpdater nameUpdater = new NameUpdater();

            nameUpdater.RemoveSpaceBeforeColons(game);
            string actualValue   = game.Name;
            string expectedValue = "The Game: Subtitle";

            Assert.Equal(expectedValue, actualValue);
        }
Esempio n. 3
0
        public void Test_MoveTheToEndOfName_PrefixTheColonNoSpace()
        {
            Game game = new Game()
            {
                Name = "The Game: Subtitle"
            };

            NameUpdater nameUpdater = new NameUpdater();

            nameUpdater.MoveTheToEndOfName(game);
            string actualValue   = game.Name;
            string expectedValue = "Game, The: Subtitle";

            Assert.Equal(expectedValue, actualValue);
        }
Esempio n. 4
0
        public void Test_MoveTheToEndOfName_PrefixTheNoPunctuation()
        {
            Game game = new Game()
            {
                Name = "The Game"
            };

            NameUpdater nameUpdater = new NameUpdater();

            nameUpdater.MoveTheToEndOfName(game);
            string actualValue   = game.Name;
            string expectedValue = "Game, The";

            Assert.Equal(expectedValue, actualValue);
        }
        /// <summary>
        /// Looks for table names in cachedLines, then uses that information to update column names
        /// Writes the updated text to disk
        /// </summary>
        /// <param name="cachedLines"></param>
        /// <param name="tableNameMap">
        /// Dictionary where keys are the original (source) table names
        /// and values are WordReplacer classes that track the new table names and new column names in PostgreSQL
        /// </param>
        /// <param name="columnNameMap">
        /// Dictionary where keys are new table names
        /// and values are a Dictionary of mappings of original column names to new column names in PostgreSQL;
        /// names should not have double quotes around them
        /// </param>
        /// <param name="writer"></param>
        /// <param name="matchedViews">List of matched view names</param>
        /// <returns></returns>
        private bool ProcessCachedLines(
            IEnumerable <string> cachedLines,
            IReadOnlyDictionary <string, WordReplacer> tableNameMap,
            Dictionary <string, Dictionary <string, WordReplacer> > columnNameMap,
            TextWriter writer,
            ICollection <string> matchedViews)
        {
            // Keys in this list are the original version of the line
            // Values are the updated version
            var updatedLines = new List <KeyValuePair <string, string> >();

            var referencedTables = new SortedSet <string>();

            var createViewMatcher   = new Regex(@"\s*CREATE VIEW +(?<ViewName>.+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            var createViewAsMatcher = new Regex(@"\s*CREATE VIEW +(?<ViewName>.+) +AS *$", RegexOptions.Compiled | RegexOptions.IgnoreCase);

            var stringConcatenationMatcher1 = new Regex(@"' *\+", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            var stringConcatenationMatcher2 = new Regex(@"\+ *'", RegexOptions.Compiled | RegexOptions.IgnoreCase);

            // Look for table names in cachedLines, updating as appropriate
            foreach (var dataLine in cachedLines)
            {
                var match1 = createViewAsMatcher.Match(dataLine);
                if (match1.Success)
                {
                    AppendCreateView(match1, writer, matchedViews);
                    writer.WriteLine("AS");
                    continue;
                }

                var match2 = createViewMatcher.Match(dataLine);
                if (match2.Success)
                {
                    AppendCreateView(match2, writer, matchedViews);
                    continue;
                }

                var updatedLine = NameUpdater.FindAndUpdateTableNames(tableNameMap, referencedTables, dataLine, true);

                updatedLines.Add(new KeyValuePair <string, string>(dataLine, updatedLine));
            }

            // Look for column names in updatedLines, updating as appropriate
            foreach (var dataLine in updatedLines)
            {
                var originalLine = dataLine.Key;

                var workingCopy = NameUpdater.UpdateColumnNames(columnNameMap, referencedTables, dataLine.Value, true);

                // Use || for string concatenation, instead of +
                if (stringConcatenationMatcher1.IsMatch(workingCopy))
                {
                    workingCopy = stringConcatenationMatcher1.Replace(workingCopy, "' ||");
                }

                if (stringConcatenationMatcher2.IsMatch(workingCopy))
                {
                    workingCopy = stringConcatenationMatcher2.Replace(workingCopy, "|| '");
                }

                if (originalLine.Equals(workingCopy))
                {
                    writer.WriteLine(originalLine);
                    continue;
                }

                OnDebugEvent(string.Format("Updating {0} \n        to {1}", originalLine, workingCopy));
                writer.WriteLine(workingCopy);
            }

            return(true);
        }