Пример #1
0
        private DbmlEnum.Enum GetEnum(TableSchema tableSchema)
        {
            // Anything coming in here should already have passed through Settings.IsEnum().
            // Because of this, we know not only that it is an Enum table, but also that all
            // of the desired columns exist.

            DbmlEnum.Enum myEnum       = _enumDatabase.Enums.Where(e => e.Table == tableSchema.FullName).FirstOrDefault();
            DbmlEnum.Enum existingEnum = _existingEnumDatabase.Enums.Where(e => e.Table == tableSchema.FullName).FirstOrDefault()
                                         ?? new DbmlEnum.Enum();

            if (myEnum == null)
            {
                myEnum = new DbmlEnum.Enum
                {
                    Name = (String.IsNullOrEmpty(existingEnum.Name))
                                     ? ToEnumName(tableSchema.Name)
                                     : existingEnum.Name,
                    Table = tableSchema.FullName,
                    Type  = tableSchema.PrimaryKey.MemberColumns[0].SystemType.FullName,
                    Flags = existingEnum.Flags,
                    IncludeDataContract = existingEnum.IncludeDataContract,
                    Items = GetEnumItems(tableSchema, existingEnum)
                };
                _enumDatabase.Enums.Add(myEnum);
            }

            return(myEnum);
        }
Пример #2
0
        private List <DbmlEnum.Item> GetEnumItems(TableSchema tableSchema, DbmlEnum.Enum existingEnum)
        {
            List <DbmlEnum.Item> itemList = new List <DbmlEnum.Item>();

            string primaryKey        = tableSchema.PrimaryKey.MemberColumns[0].Name;
            string nameColumn        = settings.GetEnumNameColumnName(tableSchema);
            string descriptionColumn = settings.GetEnumDescriptionColumnName(tableSchema);

            DataTable table = tableSchema.GetTableData();

            if (table.Rows.Count == 0)
            {
                throw new ApplicationException(string.Format(
                                                   "Table '{0}' was identified as an enum table but does not contain any rows. Please insert rows into the table or ignore the table.",
                                                   tableSchema.FullName));
            }

            foreach (DataRow row in table.Rows)
            {
                long          value         = Int64.Parse(row[primaryKey].ToString());
                DbmlEnum.Item existingValue = existingEnum.Items.Where(v => v.Value == value).FirstOrDefault()
                                              ?? new DbmlEnum.Item();

                string description = (table.Columns.Contains(descriptionColumn))
                    ? row[descriptionColumn] as String
                    : null;

                itemList.Add(new DbmlEnum.Item
                {
                    Name               = StringUtil.ToPascalCase(row[nameColumn].ToString()),
                    Value              = value,
                    Description        = description ?? existingValue.Description,
                    DataContractMember = existingValue.DataContractMember
                });
            }

            return(itemList);
        }
Пример #3
0
        private string IsOrWasEnumAssociation(DataObjectBase dataObject, out bool isEnum, out bool wasEnum)
        {
            string       name         = String.Empty;
            ColumnSchema columnSchema = dataObject as ColumnSchema;

            isEnum  = false;
            wasEnum = false;

            if (columnSchema == null || !columnSchema.IsForeignKeyMember)
            {
                return(name);
            }

            foreach (TableKeySchema tableKeySchema in columnSchema.Table.ForeignKeys)
            {
                // find columns ...
                ColumnSchema primaryColumn = null;
                for (int i = 0; i < tableKeySchema.ForeignKeyMemberColumns.Count; i++)
                {
                    if (tableKeySchema.ForeignKeyMemberColumns[i].Column != columnSchema)
                    {
                        continue;
                    }

                    primaryColumn = tableKeySchema.PrimaryKeyMemberColumns[i].Column;
                    break;
                }

                if (primaryColumn == null)
                {
                    continue;
                }

                // Is Enum
                TableSchema primaryKeyTable = tableKeySchema.PrimaryKeyTable;

                if (Settings.IsEnum(primaryKeyTable))
                {
                    name   = GetEnum(primaryKeyTable).Name;
                    isEnum = true;
                }

                // Was Enum
                DbmlEnum.Enum existingEnum = _existingEnumDatabase.Enums
                                             .Where(e => e.Table == primaryKeyTable.FullName)
                                             .FirstOrDefault();

                if (existingEnum != null)
                {
                    if (String.IsNullOrEmpty(name))
                    {
                        name = existingEnum.Name;
                    }
                    wasEnum = true;
                }

                if (isEnum || wasEnum)
                {
                    break;
                }

                // find column in pktable, if that is fkey too, check that parent.
                if (primaryColumn.IsForeignKeyMember && primaryColumn != dataObject)
                {
                    return(IsOrWasEnumAssociation(primaryColumn, out isEnum, out wasEnum));
                }
            }

            return(name);
        }
Пример #4
0
        private DbmlEnum.Enum GetEnum(TableSchema tableSchema)
        {
            // Anything coming in here should already have passed through Settings.IsEnum().
            // Because of this, we know not only that it is an Enum table, but also that all
            // of the desired columns exist.

            DbmlEnum.Enum myEnum = _enumDatabase.Enums.Where(e => e.Table == tableSchema.FullName).FirstOrDefault();
            DbmlEnum.Enum existingEnum = _existingEnumDatabase.Enums.Where(e => e.Table == tableSchema.FullName).FirstOrDefault()
                ?? new DbmlEnum.Enum();

            if (myEnum == null)
            {
                myEnum = new DbmlEnum.Enum
                             {
                                 Name = (String.IsNullOrEmpty(existingEnum.Name))
                                     ? ToEnumName(tableSchema.Name)
                                     : existingEnum.Name,
                                 Table = tableSchema.FullName,
                                 Type = tableSchema.PrimaryKey.MemberColumns[0].SystemType.FullName,
                                 Flags = existingEnum.Flags,
                                 IncludeDataContract = existingEnum.IncludeDataContract,
                                 Items = GetEnumItems(tableSchema, existingEnum)
                             };
                _enumDatabase.Enums.Add(myEnum);
            }

            return myEnum;
        }