Exemple #1
0
        public int FindInferredRelationships()
        {
            DbColumn primaryKeyColumn = GetSinglePrimaryKeyColumn();

            if (primaryKeyColumn == null)
            {
                return(0);
            }

            QList <DbColumn> references = new QList <DbColumn>(QListSort.Descending);

            foreach (DbColumn c in DbColumn.allNames.Each(name.ToLower()))
            {
                references.Add(c, 10);
            }
            foreach (DbColumn c in DbColumn.allNameStems.Each(name.Stem().ToLower()))
            {
                references.Add(c, 7);
            }
            foreach (DbColumn c in DbColumn.allWords.Each(name.ToLower()))
            {
                if (c.likeIdentifier)
                {
                    references.Add(c, 4 - c.objectNameWords.Count);
                }
            }
            foreach (DbColumn c in DbColumn.allWordStems.Each(name.Stem().ToLower()))
            {
                if (c.likeIdentifier)
                {
                    references.Add(c, 3 - c.objectNameWords.Count);
                }
            }

            /*
             * if (!primaryKeyColumn.objectName.In("id,code,name,label"))
             * {
             *  if (primaryKeyColumn.dataType == typeof(int) || primaryKeyColumn.dataType == typeof(string))
             *  {
             *      foreach (DbColumn c in DbColumn.allNames.Each(primaryKeyColumn.objectName.ToLower()))
             *          references.Add(c, 5);
             *  }
             * }
             */


            // todo: should these fields have zero empty values?

            int ct = 0;

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

            foreach (DbColumn c in references.Each())
            {
                if (c != primaryKeyColumn && !tablesAdded.Contains(c.objectName))
                {
                    if (c.dataType == primaryKeyColumn.dataType && c.columnLength == primaryKeyColumn.columnLength)
                    {
                        if (c.table.GetForeignKey(objectName, primaryKeyColumn.objectName) == null)
                        {
                            DbTableConstraint constraint = c.table.GetOrAddConstraint("inferred" + objectName, "inferred");
                            constraint.AddInferredRelationship(c.objectName, this, primaryKeyColumn);
                            tablesAdded.Add(c.table.name);
                            ct++;
                        }
                    }
                }
            }

            return(ct);
        }
Exemple #2
0
        public DbTableConstraintColumn AddInferredRelationship(string col, DbTable refTable, DbColumn refColumn)
        {
            DbTableConstraintColumn c = constraintColumns.ContainsKey(col) ? constraintColumns[col] :
                                        constraintColumns.Add(col, new DbTableConstraintColumn(this, dbTable.columns[col]));

            referencedTable = refTable;
            refTable.references.Add(this);
            if (refColumn != null)
            {
                c.referencedColumn = refColumn;
                refColumn.references.Add(c);
            }
            return(c);
        }
Exemple #3
0
 public void ColumnUsed(DbColumn col)
 {
     activeColumns.AddIfNotExists(col);
     col.table.columns.SetPosition(col.name, col.Used());
 }
Exemple #4
0
 public DbTableConstraintColumn(DbTableConstraint con, DbColumn c)
 {
     constraint = con;
     dbColumn   = c;
     c.constraints.Add(this);
 }
Exemple #5
0
        public virtual bool AnalyzeDatabaseData(out string msg)
        {
            msg = null;
            if (tablesToBeAnalyzed.Count > 0)
            {
                DbTable t = tablesToBeAnalyzed.First();
                tablesToBeAnalyzed.RemoveAt(0);
                msg = t.AnalyzeData();
                return(true);
            }
            else if (columnsToBeAnalyzed.Count > 0)
            {
                DbColumn c = columnsToBeAnalyzed.First();
                columnsToBeAnalyzed.RemoveAt(0);
                msg = c.AnalyzeData();
                return(true);
            }
            else
            {
                foreach (DbColumn c in DbColumn.allColumns)
                {
                    if (c.lastAnalyzed == 0)
                    {
                        columnsToBeAnalyzed.Insert(0, c);
                    }
                }

                if (columnsToBeAnalyzed.Count > 0)
                {
                    return(true);
                }

                foreach (DbTable t in tables.Values)
                {
                    if (t.lastAnalyzed == 0)
                    {
                        tablesToBeAnalyzed.Insert(0, t);
                    }
                    else if (!t.recentlyAnalyzed)
                    {
                        tablesToBeAnalyzed.Add(t);
                    }
                }
                if (tablesToBeAnalyzed.Count > 0)
                {
                    return(true);
                }

                foreach (DbColumn c in DbColumn.allColumns)
                {
                    if (!c.recentlyAnalyzed)
                    {
                        columnsToBeAnalyzed.Add(c);
                    }
                }

                if (columnsToBeAnalyzed.Count > 0)
                {
                    return(true);
                }
            }

            return(false);
        }