Example #1
0
        public static List <string> GenerateOrderedRepeatedForeignKey(int numberOfRowsToGenerate, Dictionary <string, List <string> > valuesGeneratedForDatabase, DatabaseColumn column, DatabaseTable table)
        {
            string columnName        = column.Name;
            string foreignTableName  = column.ForeignTableName;
            string foreignColumnName = column.ForeignColumnName;
            bool   isNullable        = column.IsNullable == 1;



            int           n;
            List <string> tempListOfValuesOfForeignColumn = new List <string>();

            // If no values are to be referenced assume key is nullable, and return list of null values.
            if (foreignTableName == null ||
                foreignColumnName == null ||
                !valuesGeneratedForDatabase.TryGetValue($"{foreignTableName}:{foreignColumnName}", out tempListOfValuesOfForeignColumn))
            {
                if (isNullable == false)
                {
                    throw new GenerationException($"Values for column '{columnName}' referencing {foreignTableName}:{foreignColumnName} not found, and {columnName} is not nullable");
                }

                return(GenerationUtility.GenerateListOfNullValues(numberOfRowsToGenerate));
            }


            // problemi sa stranim kljucevima
            //if (columnName.ToLower().Contains("id") && !foreignColumnName.ToLower().Contains("id"))
            //{
            //    foreignColumnName = columnName;
            //    valuesGeneratedForDatabase.TryGetValue($"{foreignTableName}:{foreignColumnName}", out tempListOfValuesOfForeignColumn);
            //}
            //else if (columnName.ToLower().Contains("name") && !foreignColumnName.ToLower().Contains("name"))
            //{
            //    foreignColumnName = columnName;
            //    valuesGeneratedForDatabase.TryGetValue($"{foreignTableName}:{foreignColumnName}", out tempListOfValuesOfForeignColumn);
            //}

            n = tempListOfValuesOfForeignColumn.Count();

            int           counter    = 0;
            List <string> resultList = new List <string>();

            while (numberOfRowsToGenerate > 0)
            {
                resultList.Add(tempListOfValuesOfForeignColumn[counter]);

                counter++;
                if (counter >= n)
                {
                    counter = 0;
                }

                numberOfRowsToGenerate--;
            }

            return(resultList);
        }
Example #2
0
        public static List <string> GenerateOrderedUniqueValuesForForeignKey(int numberOfRowsToGenerate, Dictionary <string, List <string> > valuesGeneratedForDatabase, DatabaseColumn column, DatabaseTable table)
        {
            string columnName        = column.Name;
            string foreignTableName  = column.ForeignTableName;
            string foreignColumnName = column.ForeignColumnName;
            bool   isNullable        = column.IsNullable == 1;

            List <string> tempListOfValuesOfForeignColumn = new List <string>();

            // If no values are to be referenced assume key is nullable, and return list of null values.
            if (foreignTableName == null ||
                foreignColumnName == null ||
                !valuesGeneratedForDatabase.TryGetValue($"{foreignTableName}:{foreignColumnName}", out tempListOfValuesOfForeignColumn))
            {
                if (isNullable == false)
                {
                    throw new GenerationException($"Values for column '{columnName}' referencing {foreignTableName}:{foreignColumnName} not found, and {columnName} is not nullable");
                }

                return(GenerationUtility.GenerateListOfNullValues(numberOfRowsToGenerate));
            }

            // problemi sa stranim kljucevima
            //if (columnName.ToLower().Contains("id") && !foreignColumnName.ToLower().Contains("id"))
            //{
            //    foreignColumnName = columnName;
            //    valuesGeneratedForDatabase.TryGetValue($"{foreignTableName}:{foreignColumnName}", out tempListOfValuesOfForeignColumn);
            //}
            //else if (columnName.ToLower().Contains("name") && !foreignColumnName.ToLower().Contains("name"))
            //{
            //    foreignColumnName = columnName;
            //    valuesGeneratedForDatabase.TryGetValue($"{foreignTableName}:{foreignColumnName}", out tempListOfValuesOfForeignColumn);
            //}

            if (tempListOfValuesOfForeignColumn.Count() < numberOfRowsToGenerate)
            {
                throw new GenerationException($"Too many rows for foreign key relation. Max for configuration is {tempListOfValuesOfForeignColumn.Count()}");
            }

            List <string> resultList = new List <string>();

            for (int i = 0; i < numberOfRowsToGenerate; i++)
            {
                resultList.Add(tempListOfValuesOfForeignColumn[i]);
            }

            return(resultList);
        }
Example #3
0
        /// <summary>
        /// Generates list of random values that were generated for referenced column in referenced table. If no referenced values are found or generates list of NULL values.
        /// </summary>
        /// <param name="numberOfRowsToGenerate"></param>
        /// <param name="valuesGeneratedForDatabase">Hash containing lists of values generated for all tables and columns.</param>
        /// <param name="foreignTableName"></param>
        /// <param name="foreignColumnName"></param>
        /// <returns></returns>
        public static List <string> GenerateRandomValuesForForeignKey(int numberOfRowsToGenerate, Dictionary <string, List <string> > valuesGeneratedForDatabase, DatabaseColumn column, DatabaseTable table)
        {
            string columnName        = column.Name;
            string foreignTableName  = column.ForeignTableName;
            string foreignColumnName = column.ForeignColumnName;
            bool   isNullable        = column.IsNullable == 1;

            List <string> tempListOfValuesOfForeignColumn = new List <string>();

            // If no values are to be referenced assume key is nullable, and return list of null values.
            if (foreignTableName == null ||
                foreignColumnName == null ||
                !valuesGeneratedForDatabase.TryGetValue($"{foreignTableName}:{foreignColumnName}", out tempListOfValuesOfForeignColumn))
            {
                if (isNullable == false)
                {
                    throw new GenerationException($"Values for column '{columnName}' referencing {foreignTableName}:{foreignColumnName} not found, and {columnName} is not nullable");
                }

                return(GenerationUtility.GenerateListOfNullValues(numberOfRowsToGenerate));
            }

            // problemi sa stranim kljucevima
            //if (columnName.ToLower().Contains("id") && !foreignColumnName.ToLower().Contains("id"))
            //{
            //    foreignColumnName = columnName;
            //    valuesGeneratedForDatabase.TryGetValue($"{foreignTableName}:{foreignColumnName}", out tempListOfValuesOfForeignColumn);
            //}
            //else if (columnName.ToLower().Contains("name") && !foreignColumnName.ToLower().Contains("name"))
            //{
            //    foreignColumnName = columnName;
            //    valuesGeneratedForDatabase.TryGetValue($"{foreignTableName}:{foreignColumnName}", out tempListOfValuesOfForeignColumn);
            //}

            Random        random                = new Random();
            List <string> resultingList         = new List <string>();
            int           numberOfForeignValues = tempListOfValuesOfForeignColumn.Count();
            int           numberOfUniques       = table.DatabaseColumns.Where(c => c.IsUnique == 1).Count();

            // If this table is unique then there must be at least as many foreign keys
            if (column.IsUnique == 1 && numberOfRowsToGenerate > numberOfForeignValues && numberOfUniques == 1)
            {
                throw new GenerationException($@"Column {column.Table.Name}:{columnName} is unique and references {foreignTableName}:{foreignColumnName}. Table {column.Table.Name} has {numberOfRowsToGenerate} rows, {foreignTableName} has {numberOfForeignValues} rows. Table with unique foreign key to another table must have more or equal rows to referenced table.");
            }

            // If this is a selfreflection only use the values that were made before this row
            if (foreignTableName == column.Table.Name)
            {
                for (int i = 0; i < numberOfRowsToGenerate; i++)
                {
                    // If column is unique then watch out for duplicates
                    if (column.IsUnique == 1)
                    {
                        int    randomValue = random.Next(0, i);
                        string value       = tempListOfValuesOfForeignColumn[randomValue];
                        while (resultingList.Contains(value))
                        {
                            randomValue = randomValue + 1 % (i + 1);
                            value       = tempListOfValuesOfForeignColumn[randomValue];
                        }
                        resultingList.Add(value);
                    }
                    else
                    {
                        resultingList.Add(tempListOfValuesOfForeignColumn[random.Next(0, i)]);
                    }
                }
            }
            else
            {
                for (int i = 0; i < numberOfRowsToGenerate; i++)
                {
                    if (column.IsUnique == 1)
                    {
                        // I there are no more new values place old ones
                        if (resultingList.Count() >= numberOfForeignValues)
                        {
                            for (int j = i; j < numberOfRowsToGenerate; j++)
                            {
                                resultingList.Add(tempListOfValuesOfForeignColumn[random.Next(0, numberOfForeignValues - 1)]);
                            }

                            break;
                        }

                        int    randomValue = random.Next(0, numberOfForeignValues - 1);
                        string value       = tempListOfValuesOfForeignColumn[randomValue];
                        while (resultingList.Contains(value))
                        {
                            randomValue = (randomValue + 1) % numberOfForeignValues;
                            value       = tempListOfValuesOfForeignColumn[randomValue];
                        }
                        resultingList.Add(value);
                    }
                    else
                    {
                        resultingList.Add(tempListOfValuesOfForeignColumn[random.Next(0, numberOfForeignValues - 1)]);
                    }
                }
            }

            return(resultingList);
        }