Exemple #1
0
 public void TestGetDistinctConstraintNames()
 {
     var testData = new NoFuture.Hbm.DbQryContainers.SortedKeys();
     var testOutput = new List<ColumnMetadata>();
     var testResult = testData.GetKeyManyToOneColumns("dbo.TableWithCompositePk", ref testOutput);
     Assert.IsTrue(testResult);
 }
Exemple #2
0
 public void TestConstraintNameComparer()
 {
     var testData = new NoFuture.Hbm.DbQryContainers.SortedKeys();
     var testOutput = new List<ColumnMetadata>();
     var testInput = testData.GetKeyManyToOneColumns("dbo.TableWithCompositePk", ref testOutput);
     foreach(var cd in testOutput)
         System.Diagnostics.Debug.WriteLine(cd.constraint_name);
     var testResult = testOutput.Distinct(new ConstraintNameComparer()).ToList();
     Assert.AreEqual(1,testResult.Count);
 }
Exemple #3
0
        /// <summary>
        /// Generates JSON formatted file whose contents are of form and fit to many-to-one nodes of an hbm.xml file.
        /// This is the second of the three hbm sorting cmdlets that should
        /// be called.  
        ///     
        /// The effort of this cmdlet is to determine, of all 
        /// tables having a primary key, what this tables foreign keys
        /// are which were not already accounted for as part of a 
        /// composite-id primary key by the former cmdlet.
        /// </summary>
        /// <param name="sortedKeys">The output of GetHbmDbPkData</param>
        /// <returns></returns>
        public static SortedOneToMany GetHbmDbFkData(SortedKeys sortedKeys)
        {
            var pkItems = sortedKeys.Data;

            //get load/re-load psobjects to have output paths matching current settings
            Settings.LoadOutputPathCurrentSettings();

            //get needed json
            var cnJson = DbContainers.Fks.Data;
            var pkJson = DbContainers.Pks.Data;
            var akJson = DbContainers.AllKeys.Data;
            var aiJson = DbContainers.AllIndex.Data;
            var colJson = DbContainers.AllColumns.Data;

            //get a list of all the tables having a primary key
            var fks = new Dictionary<string, FkItem>();
            var allTables = pkJson.Select(x => x.table_name).Distinct();

            //proceed through each table
            foreach (var tbl in allTables)
            {
                var fkManifold = GetHbmDistinctFks(tbl);
                var iFkManifold = GetHbmFksNoRelToPks(fkManifold, pkItems, tbl);

                //produce output that is not already represented in the pk json hash
                if (iFkManifold == null || iFkManifold.Keys.Count <= 0)
                    continue;
                foreach (var iFkKeyValue in iFkManifold.Keys)
                {
                    //by constraint_name find the json entry for this table
                    var residentTableJsonData =
                        cnJson.First(
                            x => string.Equals(x.table_name, tbl, C) && string.Equals(x.constraint_name, iFkKeyValue, C));
                    //from json data entry discover the matching unique_constraint_name
                    var fkConstraintName = residentTableJsonData.unique_constraint_name;

                    //find the table-name this FK's unique_constraint_name is referring to
                    var matchedTable = akJson.FirstOrDefault(x => string.Equals(x.constraint_name, fkConstraintName)) ??
                                       aiJson.First(x => string.Equals(x.constraint_name, fkConstraintName));
                    var matchedTableName = matchedTable.table_name;

                    //get all the columns, not just first
                    var matchedFkColumns =
                        cnJson.Where(
                            x => string.Equals(x.table_name, tbl, C) && string.Equals(x.constraint_name, iFkKeyValue, C))
                            .ToList();

                    foreach(var matchCol in matchedFkColumns)
                        matchCol.CopyFromDataSrc(colJson);

                    if (!fks.ContainsKey(tbl))
                    {
                        var fkItem = new FkItem
                        {
                            ManyToOne = new Dictionary<string, List<ColumnMetadata>>
                            {
                                {matchedTableName, matchedFkColumns}
                            }
                        };
                        fks.Add(tbl, fkItem);
                    }
                    else if (!fks[tbl].ManyToOne.ContainsKey(matchedTableName))
                    {
                        fks[tbl].ManyToOne.Add(matchedTableName, matchedFkColumns);
                    }
                    else
                    {
                        fks[tbl].ManyToOne[matchedTableName].AddRange(matchedFkColumns);
                    }
                }
            }
            return new SortedOneToMany() {Data = fks};
        }