Ejemplo n.º 1
0
 private bool CheckDependOn(JoinedTable t1, JoinedTable t2)
 {
     foreach (ColumnRef column in t1.ForeignKeys)
     {
         ColumnRef columnRef = column;
         while (columnRef.Column is ForeignColumn)
         {
             columnRef = ((ForeignColumn)columnRef.Column).TargetColumn;
         }
         if (columnRef.Table != null && String.Equals(columnRef.Table.Name, t2.Name, StringComparison.OrdinalIgnoreCase))
         {
             return(true);
         }
     }
     return(false);
 }
Ejemplo n.º 2
0
        private TableView GenerateTableView(Type objectType)
        {
            TableJoinAttribute[]             atts         = (TableJoinAttribute[])objectType.GetCustomAttributes(typeof(TableJoinAttribute), true);
            Dictionary <string, JoinedTable> joinedTables = new Dictionary <string, JoinedTable>(StringComparer.OrdinalIgnoreCase);

            foreach (TableJoinAttribute tableJoin in atts)
            {
                JoinedTable joinedTable = new JoinedTable(GetTableDefinition(tableJoin.TargetType));
                if (String.IsNullOrEmpty(tableJoin.AliasName))
                {
                    tableJoin.AliasName = joinedTable.Name;
                }
                else
                {
                    joinedTable.Name = tableJoin.AliasName;
                }
                if (joinedTables.ContainsKey(joinedTable.Name))
                {
                    throw new ArgumentException(String.Format("Duplicate table alias name \"{0}\"", joinedTable.Name));
                }
                joinedTables.Add(joinedTable.Name, joinedTable);
            }

            List <Column> columns = new List <Column>();

            foreach (PropertyInfo property in objectType.GetProperties())
            {
                ColumnDefinition column = GetColumnDefinition(property);
                if (column != null)
                {
                    columns.Add(column);
                }
            }

            foreach (PropertyInfo property in objectType.GetProperties())
            {
                ForeignColumn foreignColumn = GenerateForeignColumn(property);
                if (foreignColumn != null)
                {
                    columns.Add(foreignColumn);
                }
            }

            foreach (Column column in columns)
            {
                if (column.ForeignType != null)
                {
                    bool foreignTypeExists = false;
                    foreach (JoinedTable joinedTable in joinedTables.Values)
                    {
                        if (joinedTable.TableDefinition.ObjectType == column.ForeignType && (joinedTable.Name == column.ForeignAlias || String.IsNullOrEmpty(column.ForeignAlias)))
                        {
                            foreignTypeExists = true;
                            break;
                        }
                    }

                    if (!foreignTypeExists)
                    {
                        TableDefinition foreignTable = GetTableDefinition(column.ForeignType);
                        JoinedTable     joinedTable  = new JoinedTable(foreignTable);
                        if (!String.IsNullOrEmpty(column.ForeignAlias))
                        {
                            joinedTable.Name = column.ForeignAlias;
                        }
                        List <ColumnRef> foreignKeys = new List <ColumnRef>();
                        foreignKeys.Add(new ColumnRef(column));
                        joinedTable.ForeignKeys = foreignKeys.AsReadOnly();
                        joinedTables.Add(joinedTable.Name, joinedTable);
                    }
                }
            }

            foreach (Column column in columns)
            {
                if (column is ForeignColumn)
                {
                    ForeignColumn          foreignColumn          = column as ForeignColumn;
                    ForeignColumnAttribute foreignColumnAttribute = Utility.GetAttribute <ForeignColumnAttribute>(column.Property);
                    string primeProperty = String.IsNullOrEmpty(foreignColumnAttribute.Property) ? column.PropertyName : foreignColumnAttribute.Property;
                    Type   primeType     = foreignColumnAttribute.Foreign as Type;
                    if (primeType != null)
                    {
                        foreach (JoinedTable joinedTable in joinedTables.Values)
                        {
                            if (joinedTable.TableDefinition.ObjectType == primeType)
                            {
                                if (foreignColumn.TargetColumn != null)
                                {
                                    throw new ArgumentException(String.Format("Undeterminate table. More than one table of type {0} joined.", primeType.Name));
                                }
                                if (joinedTable.GetColumn(primeProperty) == null)
                                {
                                    throw new ArgumentException(String.Format("Foreign property {0} not exist in type {1}.", primeProperty, primeType));
                                }

                                foreignColumn.TargetColumn = joinedTable.GetColumn(primeProperty);
                            }
                        }
                        if (foreignColumn.TargetColumn == null)
                        {
                            throw new ArgumentException(String.Format("Foreign type {0} of property {1} not exist in joined tables.", primeType, column.PropertyName));
                        }
                    }
                    else
                    {
                        string foreignTable = (string)foreignColumnAttribute.Foreign;
                        if (!joinedTables.ContainsKey(foreignTable))
                        {
                            throw new ArgumentException(String.Format("Foreign table name {0} of property {1} not exist in joined tables.", foreignColumnAttribute.Foreign, column.PropertyName));
                        }
                        if (joinedTables[foreignTable].GetColumn(primeProperty) == null)
                        {
                            throw new ArgumentException(String.Format("Foreign property {0} not exist in type {1}.", primeProperty, joinedTables[foreignTable].TableDefinition.ObjectType));
                        }

                        foreignColumn.TargetColumn = joinedTables[foreignTable].GetColumn(primeProperty);
                    }
                }
            }

            TableView tableView = new TableView(GetTableDefinition(objectType), joinedTables.Values, columns)
            {
                Name = objectType.Name
            };

            foreach (TableJoinAttribute tableJoin in atts)
            {
                if (tableJoin.Source == null)
                {
                    List <ColumnRef> foreignKeys = new List <ColumnRef>();
                    foreach (string keyName in tableJoin.ForeignKeys.Split(','))
                    {
                        foreignKeys.Add(new ColumnRef(tableView.GetColumn(keyName)));
                    }
                    joinedTables[tableJoin.AliasName].ForeignKeys = foreignKeys.AsReadOnly();
                }
                else
                {
                    JoinedTable sourceTable = null;
                    if (tableJoin.Source is string)
                    {
                        string sourceName = (string)tableJoin.Source;
                        if (!joinedTables.ContainsKey(sourceName))
                        {
                            throw new ArgumentException(String.Format("Source table \"{0}\" not exist in joined tables.", sourceName));
                        }
                        sourceTable = joinedTables[sourceName];
                    }
                    else
                    {
                        Type sourceType = (Type)tableJoin.Source;
                        foreach (JoinedTable joinedTable in joinedTables.Values)
                        {
                            if (joinedTable.TableDefinition.ObjectType == sourceType)
                            {
                                if (sourceTable == null)
                                {
                                    sourceTable = joinedTable;
                                }
                                else
                                {
                                    throw new ArgumentException(String.Format("Undeterminate table. More than one table of type {0} joined.", sourceType));
                                }
                            }
                        }
                        if (sourceTable == null)
                        {
                            throw new ArgumentException(String.Format("Source table type {0} not exist in joined tables.", sourceType));
                        }
                    }
                    List <ColumnRef> foreignKeys = new List <ColumnRef>();
                    foreach (string keyName in tableJoin.ForeignKeys.Split(','))
                    {
                        foreignKeys.Add(sourceTable.GetColumn(keyName));
                    }
                    joinedTables[tableJoin.AliasName].ForeignKeys = foreignKeys.AsReadOnly();
                }
            }

            return(tableView);
        }