Пример #1
0
 public override Table VisitTable(Table table)
 {
     Table table2;
     currentTable = table;
     try
     {
         table2 = base.VisitTable(table);
     }
     finally
     {
         currentTable = null;
     }
     return table2;
 }
Пример #2
0
        private void CreateView(ViewSchema viewSchema)
        {
            Table table;

            if (Database.Tables.Contains(viewSchema.FullName))
            {
                table = Database.Tables[viewSchema.FullName];
            }
            else
            {
                Type type = new Type(ToClassName(viewSchema.Name));
                table = new Table(viewSchema.FullName, type);
                Database.Tables.Add(table);
            }

            if (string.IsNullOrEmpty(table.Type.Name))
                table.Type.Name = ToClassName(viewSchema.Name);

            if (string.IsNullOrEmpty(table.Member))
                table.Member = table.Type.Name;

            foreach (ViewColumnSchema columnSchema in viewSchema.Columns)
            {
                Column column;

                if (table.Type.Columns.Contains(columnSchema.Name))
                {
                    column = table.Type.Columns[columnSchema.Name];
                }
                else
                {
                    column = new Column(GetSystemType(columnSchema));
                    column.Name = columnSchema.Name;
                    table.Type.Columns.Add(column);
                }

                PopulateColumn(column, columnSchema, table.Type.Name);
            }

            table.Type.Columns.IsProcessed = true;
            table.Type.Associations.IsProcessed = true;
            table.IsProcessed = true;
        }
Пример #3
0
        private Table CreateTable(TableSchema tableSchema)
        {
            Type type = new Type(ToClassName(tableSchema.Name));
            Table t = new Table(tableSchema.FullName, type);
            t.Member = t.Type.Name;

            if (Array.BinarySearch(ExistingContextProperties, t.Type.Name) >= 1)
                t.Member += "Table";

            Database.Tables.Add(t);

            return t;
        }
Пример #4
0
        private void CreateColumns(Table table, TableSchema tableSchema)
        {
            foreach (ColumnSchema columnSchema in tableSchema.Columns)
            {
                // skip unsupported type
                if (Settings.IsUnsupportedDbType(columnSchema))
                {
                    Debug.WriteLine(string.Format("Skipping column '{0}' because it has an unsupported db type '{1}'.", columnSchema.Name, columnSchema.NativeType));
                    Trace.WriteLine(string.Format("Skipping column '{0}' because it has an unsupported db type '{1}'.", columnSchema.Name, columnSchema.NativeType));
                    continue;
                }

                Column column;
                bool isNew = !table.Type.TryFindColumn(columnSchema.Name, true, out column);

                if (isNew)
                {
                    column = new Column(GetColumnType(columnSchema));
                    column.Name = columnSchema.Name;
                    table.Type.Columns.Add(column);
                }

                PopulateColumn(column, columnSchema, table.Type.Name);

                if (column.IsPrimaryKey != true)
                    column.IsPrimaryKey = columnSchema.IsPrimaryKeyMember;
            }

            table.Type.Columns.IsProcessed = true;
        }
Пример #5
0
        private void CreateAssociations(Table table, TableSchema tableSchema)
        {
            foreach (TableKeySchema tableKey in tableSchema.ForeignKeys)
            {
                if (Settings.IsIgnored(tableKey.ForeignKeyTable.FullName)
                    || Settings.IsIgnored(tableKey.PrimaryKeyTable.FullName)
                    || Settings.IsEnum(tableKey.ForeignKeyTable)
                    || Settings.IsEnum(tableKey.PrimaryKeyTable))
                    continue;

                CreateAssociation(table, tableKey);
            }

            table.Type.Associations.IsProcessed = true;
        }
Пример #6
0
        private void CreateAssociation(Table foreignTable, TableKeySchema tableKeySchema)
        {
            if (Settings.IsIgnored(tableKeySchema.PrimaryKeyTable.FullName))
            {
                Debug.WriteLine("Skipping Association because one of the tables is skipped: " + tableKeySchema.Name);
                Trace.WriteLine("Skipping Association because one of the tables is skipped: " + tableKeySchema.Name);
                return;
            }

            // skip unsupported type
            if (tableKeySchema.ForeignKeyMemberColumns.Any(c => Settings.IsUnsupportedDbType(c))
                || tableKeySchema.PrimaryKeyMemberColumns.Any(c => Settings.IsUnsupportedDbType(c)))
            {
                Debug.WriteLine("Skipping Association because one of the associated columns is an unsupported db type: " + tableKeySchema.Name);
                Trace.WriteLine("Skipping Association because one of the associated columns is an unsupported db type: " + tableKeySchema.Name);
                return;
            }

            Table primaryTable = GetTable(tableKeySchema.PrimaryKeyTable, false);

            string primaryClass = primaryTable.Type.Name;
            string foreignClass = foreignTable.Type.Name;

            string name = string.Format("{0}_{1}", primaryClass, foreignClass);
            name = MakeUnique(AssociationNames, name);

            string foreignMembers = GetKeyMembers(foreignTable,
                tableKeySchema.ForeignKeyMemberColumns, tableKeySchema.Name);

            string primaryMembers = GetKeyMembers(primaryTable,
                tableKeySchema.PrimaryKeyMemberColumns, tableKeySchema.Name);

            AssociationKey key = AssociationKey.CreateForeignKey(name);
            bool isNew = !foreignTable.Type.Associations.Contains(key);

            Association foreignAssociation;

            if (isNew)
                foreignAssociation = new Association(name);
            else
                foreignAssociation = foreignTable.Type.Associations[key];

            foreignAssociation.IsForeignKey = true;
            foreignAssociation.ThisKey = foreignMembers;
            foreignAssociation.OtherKey = primaryMembers;
            foreignAssociation.Type = primaryClass;

            string prefix = GetMemberPrefix(foreignAssociation, primaryClass, foreignClass);

            if (isNew)
            {
                foreignAssociation.Member = ToPropertyName(foreignTable.Type.Name, prefix + primaryClass);
                foreignAssociation.Storage = CommonUtility.GetFieldName(foreignAssociation.Member);
                foreignTable.Type.Associations.Add(foreignAssociation);
            }
            else
            {
                PropertyNames[foreignTable.Type.Name].Add(foreignAssociation.Member);
            }

            // add reverse association
            key = AssociationKey.CreatePrimaryKey(name);
            isNew = !primaryTable.Type.Associations.Contains(key);

            Association primaryAssociation;
            if (isNew)
                primaryAssociation = new Association(name);
            else
                primaryAssociation = primaryTable.Type.Associations[key];

            primaryAssociation.IsForeignKey = false;
            primaryAssociation.ThisKey = foreignAssociation.OtherKey;
            primaryAssociation.OtherKey = foreignAssociation.ThisKey;
            primaryAssociation.Type = foreignClass;

            bool isOneToOne = IsOneToOne(tableKeySchema, foreignAssociation);

            if (primaryAssociation.Cardinality == null && isOneToOne)
                primaryAssociation.Cardinality = Cardinality.One;

            if (isNew)
            {
                string propertyName = prefix + foreignClass;
                if (!isOneToOne)
                {
                    if (settings.AssociationNaming == AssociationNamingEnum.ListSuffix)
                        propertyName += "List";
                    else if (settings.AssociationNaming == AssociationNamingEnum.Plural)
                        propertyName = StringUtil.ToPlural(propertyName);
                }

                primaryAssociation.Member = ToPropertyName(primaryTable.Type.Name, propertyName);
                primaryAssociation.Storage = CommonUtility.GetFieldName(primaryAssociation.Member);
                primaryTable.Type.Associations.Add(primaryAssociation);
            }
            else
            {
                PropertyNames[primaryTable.Type.Name].Add(primaryAssociation.Member);
            }

            if (IsTableKeySchemaCascadeDelete(tableKeySchema))
            {
                foreignAssociation.DeleteRule = "CASCADE";
            }

            if (settings.IncludeDeleteOnNull || foreignTable.Type.IsManyToMany())
            {
                if (!foreignAssociation.DeleteOnNull.HasValue && IsTableDeleteOnNull(tableKeySchema))
                    foreignAssociation.DeleteOnNull = true;
            }
            else
                foreignAssociation.DeleteOnNull = null;

            foreignAssociation.IsProcessed = true;
            primaryAssociation.IsProcessed = true;
        }
Пример #7
0
        private static string GetKeyMembers(Table table, MemberColumnSchemaCollection members, string name)
        {
            StringBuilder keyMembers = new StringBuilder();

            foreach (MemberColumnSchema member in members)
            {
                if (!table.Type.Columns.Contains(member.Name))
                    throw new InvalidOperationException(string.Format(
                        "Could not find column {0} for assoication {1}.",
                        member.Name,
                        name));

                Column column = table.Type.Columns[member.Name];
                if (keyMembers.Length > 0)
                    keyMembers.Append(',');

                keyMembers.Append(column.Member);
            }

            return keyMembers.ToString();
        }
Пример #8
0
            public override Table VisitTable(Table table)
            {
                if (table == null)
                    return null;

                writer.WriteStartElement("Table");

                if (table.Name != null)
                    writer.WriteAttributeString("Name", table.Name);

                if (table.Member != null)
                    writer.WriteAttributeString("Member", table.Member);

                if (table.AccessModifier.HasValue)
                    writer.WriteAttributeString("AccessModifier", table.AccessModifier.ToString());

                if (table.Modifier.HasValue)
                    writer.WriteAttributeString("Modifier", table.Modifier.ToString());

                isSubType = false;
                VisitType(table.Type);
                currentTableFun = "InsertFunction";
                VisitTableFunction(table.InsertFunction);
                currentTableFun = "UpdateFunction";
                VisitTableFunction(table.UpdateFunction);
                currentTableFun = "DeleteFunction";
                VisitTableFunction(table.DeleteFunction);
                writer.WriteEndElement();
                return table;
            }
Пример #9
0
            public override Table VisitTable(Table table)
            {
                if (table.Name == table.Member)
                    table.Member = null;

                if (table.AccessModifier.HasValue && table.AccessModifier.Value == AccessModifier.Public)
                    table.AccessModifier = null;
                
                return base.VisitTable(table);
            }
            public override Table VisitTable(Table table)
            {
                if (table == null)
                    return null;

                if ((table.Name == null) && (table.Member != null))
                    table.Name = table.Member;

                if ((table.Member == null) && (table.Name != null))
                    table.Member = table.Name;

                if (!table.AccessModifier.HasValue)
                    table.AccessModifier = 0;

                return base.VisitTable(table);
            }
Пример #11
0
            public override Table VisitTable(Table table)
            {
                if (table == null)
                    return null;

                return new Table(table.Name, VisitType(table.Type))
                           {
                               Member = table.Member,
                               AccessModifier = table.AccessModifier,
                               Modifier = table.Modifier,
                               Type = VisitType(table.Type),
                               InsertFunction = VisitTableFunction(table.InsertFunction),
                               UpdateFunction = VisitTableFunction(table.UpdateFunction),
                               DeleteFunction = VisitTableFunction(table.DeleteFunction)
                           };
            }
Пример #12
0
            private Table ReadTable(XmlTextReader reader)
            {
                ValidateAttributes(reader, new[] {"Name", "Member", "AccessModifier", "Modifier"});
                var table = new Table("", new Type(""));

                string attribute = reader.GetAttribute("Name");
                if (attribute == null)
                    throw Error.RequiredAttributeMissingViolation("Name", reader.LineNumber);

                table.Name = attribute;
                table.Member = reader.GetAttribute("Member");

                string convertAttribute = reader.GetAttribute("AccessModifier");
                try
                {
                    if (convertAttribute == null)
                        table.AccessModifier = null;
                    else
                        table.AccessModifier =
                            (AccessModifier) Enum.Parse(typeof (AccessModifier), convertAttribute, true);

                    convertAttribute = reader.GetAttribute("Modifier");
                    if (convertAttribute == null)
                        table.Modifier = null;
                    else
                        table.Modifier = (MemberModifier) Enum.Parse(typeof (MemberModifier), convertAttribute, true);
                }
                catch (ArgumentException)
                {
                    throw Error.InvalidEnumAttributeValueViolation(convertAttribute, reader.LineNumber);
                }

                if (reader.IsEmptyElement)
                    return table;

                int typeCount = 0;
                int insertCount = 0;
                int updateCount = 0;
                int deleteCount = 0;

                while (reader.Read())
                {
                    if ((reader.NodeType == XmlNodeType.Whitespace) || !IsInNamespace(reader))
                        continue;

                    XmlNodeType nodeType = reader.NodeType;
                    if (nodeType != XmlNodeType.Element)
                    {
                        if (nodeType == XmlNodeType.EndElement)
                        {
                            if (typeCount == 0)
                                throw Error.RequiredElementMissingViolation("Type", reader.LineNumber);

                            return table;
                        }
                        continue;
                    }

                    string name = reader.Name;
                    switch (name)
                    {
                        case "Type":
                            isTableType = true;
                            table.Type = ReadType(reader);
                            typeCount++;
                            if (typeCount > 1)
                                throw Error.ElementMoreThanOnceViolation("Type", reader.LineNumber);

                            continue;
                        case "InsertFunction":
                            table.InsertFunction = ReadTableFunction(reader);
                            insertCount++;
                            if (insertCount > 1)
                                throw Error.ElementMoreThanOnceViolation("InsertFunction", reader.LineNumber);

                            continue;
                        case "UpdateFunction":
                            table.UpdateFunction = ReadTableFunction(reader);
                            updateCount++;
                            if (updateCount > 1)
                                throw Error.ElementMoreThanOnceViolation("UpdateFunction", reader.LineNumber);

                            continue;
                        case "DeleteFunction":
                            table.DeleteFunction = ReadTableFunction(reader);
                            deleteCount++;
                            if (deleteCount > 1)
                                throw Error.ElementMoreThanOnceViolation("DeleteFunction", reader.LineNumber);

                            continue;
                        default:
                            throw Error.SchemaUnexpectedElementViolation(reader.Name, "Table", reader.LineNumber);
                    }
                }
                return table;
            }