Exemplo n.º 1
0
        public IEnumerable <MemberGeneratorInfo> Create()
        {
            var generatorInfos    = CreateMembersCore(GetNonCompoundPKColumns());
            var coumboundPkColumn = GetCoumboundPKColumn();

            if (coumboundPkColumn != null)
            {
                string tableName           = ClassGenerator.GetTableName(_dbTable.Name);
                var    persistentClassInfo = _classInfos[tableName + ClassGenerator.KeyStruct].PersistentClassInfo;
                if (persistentClassInfo == null)
                {
                    throw new ArgumentNullException("persistentClassInfo with name " + _dbTable.Name + ClassGenerator.KeyStruct);
                }
                var pkDbColumns = coumboundPkColumn.Columns.OfType <string>().Select(s => _dbTable.GetColumn(s));
                var membersCore = CreateMembersCore(pkDbColumns, persistentClassInfo, TemplateType.ReadWriteMember, TemplateType.ReadWriteMember).ToList();
                var persistentReferenceMemberInfo = CreatePersistentReferenceMemberInfo(persistentClassInfo.Name, _classInfos[tableName].PersistentClassInfo, persistentClassInfo, TemplateType.FieldMember);
                membersCore.Add(new MemberGeneratorInfo(persistentReferenceMemberInfo, _dbTable.GetColumn(coumboundPkColumn.Columns[0])));
                if (pkDbColumns.Where(IsOneToOneOnTheKey).Count() == pkDbColumns.Count())
                {
                    string             refTableName       = ClassGenerator.GetTableName(_dbTable.ForeignKeys.First(key => key.Columns.Contains(pkDbColumns.ToList()[0].Name)).PrimaryKeyTable);
                    ClassGeneratorInfo classGeneratorInfo = _classInfos[refTableName];
                    membersCore.Add(new MemberGeneratorInfo(CreatePersistentReferenceMemberInfo(classGeneratorInfo.PersistentClassInfo.Name, _classInfos[tableName].PersistentClassInfo, classGeneratorInfo.PersistentClassInfo, TemplateType.XPOneToOneReadOnlyPropertyMember), null));
                }
                return(generatorInfos.Union(membersCore));
            }
            return(generatorInfos);
        }
Exemplo n.º 2
0
        IPersistentCoreTypeMemberInfo CreatePersistentCoreTypeMemberInfo(DBColumn column, IPersistentClassInfo persistentClassInfo, TemplateType templateType)
        {
            var persistentCoreTypeMemberInfo = _objectSpace.CreateWCObject <IPersistentCoreTypeMemberInfo>();

            persistentCoreTypeMemberInfo.Name     = column.Name;
            persistentCoreTypeMemberInfo.DataType = column.ColumnType;
            if (persistentClassInfo == null)
            {
                var tableName = ClassGenerator.GetTableName(_dbTable.Name);
                persistentClassInfo = _classInfos[tableName].PersistentClassInfo;
            }
            persistentClassInfo.OwnMembers.Add(persistentCoreTypeMemberInfo);
            persistentCoreTypeMemberInfo.SetDefaultTemplate(templateType);
            return(persistentCoreTypeMemberInfo);
        }
Exemplo n.º 3
0
        MemberGeneratorInfo CreateFkMember(DBColumn dbColumn, IPersistentClassInfo persistentClassInfo,
                                           TemplateType coreTemplateType, TemplateType refTemplateType)
        {
            var foreignKey = _dbTable.ForeignKeys.Where(key => key.Columns.Contains(dbColumn.Name)).First(key => !Equals(key.PrimaryKeyTable, "XPObjectType"));

            var tableName = ClassGenerator.GetTableName(foreignKey.PrimaryKeyTable);

            if (_classInfos.ContainsKey(tableName))
            {
                var classGeneratorInfo = _classInfos[tableName];
                return(new MemberGeneratorInfo(CreatePersistentReferenceMemberInfo(dbColumn.Name, persistentClassInfo,
                                                                                   classGeneratorInfo.PersistentClassInfo, GetTemplateType(refTemplateType, classGeneratorInfo)), dbColumn));
            }
            return(new MemberGeneratorInfo(CreatePersistentCoreTypeMemberInfo(dbColumn, persistentClassInfo, coreTemplateType), dbColumn));
        }
Exemplo n.º 4
0
        void CreateCollection(IPersistentReferenceMemberInfo persistentReferenceMemberInfo, IPersistentClassInfo owner)
        {
            var persistentCollectionMemberInfo = _objectSpace.CreateWCObject <IPersistentCollectionMemberInfo>();

            persistentReferenceMemberInfo.ReferenceClassInfo.OwnMembers.Add(persistentCollectionMemberInfo);
            persistentCollectionMemberInfo.Name  = persistentReferenceMemberInfo.Owner.Name + persistentReferenceMemberInfo.Name + "s";
            persistentCollectionMemberInfo.Owner = persistentReferenceMemberInfo.ReferenceClassInfo;
            var tableName = ClassGenerator.GetTableName(Regex.Replace(owner.Name, ClassGenerator.KeyStruct, "", RegexOptions.Singleline | RegexOptions.IgnoreCase));

            persistentCollectionMemberInfo.CollectionClassInfo = owner.CodeTemplateInfo.CodeTemplate.TemplateType == TemplateType.Struct ? _classInfos[tableName].PersistentClassInfo : owner;
            persistentCollectionMemberInfo.SetDefaultTemplate(TemplateType.XPCollectionMember);
            var persistentAssociationAttribute = _objectSpace.CreateWCObject <IPersistentAssociationAttribute>();

            persistentCollectionMemberInfo.TypeAttributes.Add(persistentAssociationAttribute);
            persistentAssociationAttribute.AssociationName = String.Format("{0}-{1}s", persistentReferenceMemberInfo.Name, persistentCollectionMemberInfo.Name);
        }
Exemplo n.º 5
0
        IPersistentMemberInfo CreatePersistentReferenceMemberInfo(string name, IPersistentClassInfo persistentClassInfo, IPersistentClassInfo persistentReferenceClassInfo, TemplateType templateType)
        {
            var persistentReferenceMemberInfo = _objectSpace.CreateWCObject <IPersistentReferenceMemberInfo>();

            persistentReferenceMemberInfo.Name = name;
            persistentReferenceMemberInfo.ReferenceClassInfo = persistentReferenceClassInfo;
            if (persistentClassInfo == null)
            {
                persistentClassInfo = _classInfos[ClassGenerator.GetTableName(_dbTable.Name)].PersistentClassInfo;
            }
            persistentClassInfo.OwnMembers.Add(persistentReferenceMemberInfo);
            persistentReferenceMemberInfo.SetDefaultTemplate(templateType);
            if (templateType != TemplateType.XPOneToOnePropertyMember && templateType != TemplateType.XPOneToOneReadOnlyPropertyMember && persistentClassInfo.CodeTemplateInfo.CodeTemplate.TemplateType == TemplateType.Class && persistentReferenceClassInfo.CodeTemplateInfo.CodeTemplate.TemplateType == TemplateType.Class)
            {
                CreateCollection(persistentReferenceMemberInfo, persistentClassInfo);
            }
            return(persistentReferenceMemberInfo);
        }
Exemplo n.º 6
0
        bool IsOneToOneOnTheKey(DBColumn dbColumn)
        {
            IEnumerable <DBForeignKey> foreignPKeys = _dbTable.ForeignKeys.Where(key => _dbTable.PrimaryKey.Columns.Contains(dbColumn.Name) && key.PrimaryKeyTable != _dbTable.Name && key.Columns.Contains(dbColumn.Name));
            var keies = foreignPKeys.Select(key => new { FK = key, PrimaryTable = _classInfos[ClassGenerator.GetTableName(key.PrimaryKeyTable)].DbTable });

            return(keies.Any(arg => arg.PrimaryTable.PrimaryKey.Columns.OfType <string>().All(s => arg.FK.PrimaryKeyTableKeyColumns.OfType <string>().Contains(s))));
        }