Ejemplo n.º 1
0
        public override void GenerateCreateScript(RelationDefinitionBase rel, StringBuilder script, bool unicodeStrings)
        {
            script.AppendFormat("CREATE TABLE {0}.{1}(", rel.SourceFragment.Selector, rel.SourceFragment.Name);

            GenerateRelScript(rel.Left, script, unicodeStrings, rel);
            script.Append(", ");
            GenerateRelScript(rel.Right, script, unicodeStrings, rel);

            script.AppendLine(");");
            script.AppendLine();
        }
Ejemplo n.º 2
0
        public void TestGenerateScriptHierachy()
        {
            var p = new MSSQLProvider(GetTestDB(), null);

            var sv = p.GetSourceView();

            var model = new WXMLModel();

            var smc = new SourceToModelConnector(sv, model);

            smc.ApplySourceViewToModel(false, relation1to1.Hierarchy, true, true, false);

            Assert.AreEqual(28, model.GetActiveEntities().Count());
            Assert.AreEqual(32, model.GetSourceFragments().Count());

            var msc = new ModelToSourceConnector(new SourceView(), model);

            string script = msc.GenerateSourceScript(p, false);

            Assert.IsFalse(string.IsNullOrEmpty(script));
            Console.WriteLine(script);

            Assert.AreEqual(sv.GetSourceFragments().Count(), new Regex("CREATE TABLE ").Matches(script).Count);
            IEnumerable <SourceConstraint> pks = sv.GetSourceFragments().SelectMany(item => item.Constraints.Where(cns => cns.ConstraintType == SourceConstraint.PrimaryKeyConstraintTypeName));

            Assert.AreEqual(pks.Count(), new Regex("PRIMARY KEY CLUSTERED").Matches(script).Count);
            Assert.AreEqual(2, new Regex("UNIQUE NONCLUSTERED").Matches(script).Count);
            Assert.AreEqual(1, new Regex("UNIQUE CLUSTERED").Matches(script).Count);
            Assert.AreEqual(sv.GetSourceFragments().SelectMany(item => item.Constraints.Where(cns => cns.ConstraintType == SourceConstraint.ForeignKeyConstraintTypeName)).Count(), new Regex("FOREIGN KEY").Matches(script).Count);

            msc = new ModelToSourceConnector(sv, model);

            script = msc.GenerateSourceScript(p, false);

            Assert.IsTrue(string.IsNullOrEmpty(script), script);

            RelationDefinitionBase r = model.GetActiveRelations().First(item => item.Constraint == RelationConstraint.PrimaryKey);

            r.Constraint = RelationConstraint.Unique;
            r.SourceFragment.Constraints.Single(item => item.ConstraintType == SourceConstraint.PrimaryKeyConstraintTypeName).ConstraintType = SourceConstraint.UniqueConstraintTypeName;

            msc = new ModelToSourceConnector(sv, model);

            script = msc.GenerateSourceScript(p, false);

            Assert.IsTrue(string.IsNullOrEmpty(script), script);
        }
Ejemplo n.º 3
0
 public abstract void GenerateCreateScript(RelationDefinitionBase rel, StringBuilder script, bool unicodeStrings);
        protected virtual void OnPopulateSchema()
        {
            Members.Add(SchemaDef);

            #region энам табличек

            CreateTablesLinkEnum(_entity, SchemaDef);

            #endregion энам табличек

            //#region bool ChangeValueType(EntityPropertyAttribute c, object value, ref object newvalue)

            //CreateChangeValueTypeMethod(_entity, SchemaDef);

            //#endregion bool ChangeValueType(EntityPropertyAttribute c, object value, ref object newvalue)

            //#region string[] GetSuppressedColumns()

            //// первоначальная реализация или есть отличие в suppressed properties
            //if (_entity.BaseEntity == null ||
            //    (_entity.BaseEntity.SuppressedProperties.Count + _entity.SuppressedProperties.Count != 0) ||
            //    _entity.BaseEntity.SuppressedProperties.Count != _entity.SuppressedProperties.Count ||
            //    !(_entity.SuppressedProperties.TrueForAll(p => _entity.BaseEntity.SuppressedProperties.Exists(pp => pp == p)) &&
            //    _entity.BaseEntity.SuppressedProperties.TrueForAll(p => _entity.SuppressedProperties.Exists(pp => pp == p))))
            //{

            //    var method = new CodeMemberMethod
            //    {
            //        Name = "GetSuppressedFields",
            //        // тип возвращаемого значения
            //        ReturnType = new CodeTypeReference(typeof(string[])),
            //        // модификаторы доступа
            //        Attributes = MemberAttributes.Public
            //    };

            //    SchemaDef.Members.Add(method);
            //    if (_entity.BaseEntity != null)
            //        method.Attributes |= MemberAttributes.Override;
            //    else
            //        // реализует метод базового класса
            //        method.ImplementationTypes.Add(typeof(IEntitySchemaBase));
            //    CodeArrayCreateExpression arrayExpression = new CodeArrayCreateExpression(
            //        new CodeTypeReference(typeof(string[]))
            //    );


            //    foreach (var suppressedProperty in _entity.SuppressedProperties)
            //    {
            //        arrayExpression.Initializers.Add(
            //            new CodePrimitiveExpression(suppressedProperty)
            //        );
            //    }

            //    method.Statements.Add(new CodeMethodReturnStatement(arrayExpression));
            //}

            //#endregion EntityPropertyAttribute[] GetSuppressedColumns()

            #region сущность реализует связь

            RelationDefinitionBase relation = _entity.Model.GetActiveRelations()
                                              .SingleOrDefault(item => item.UnderlyingEntity == _entity);

            if (relation != null)
            {
                SelfRelationDefinition sd = relation as SelfRelationDefinition;
                if (sd == null)
                {
                    ImplementIRelation((RelationDefinition)relation, _entity, SchemaDef);
                }
                else
                {
                    ImplementIRelation(sd, _entity, SchemaDef);
                }
            }

            #endregion сущность реализует связь

            //#region public void GetSchema(OrmSchemaBase schema, Type t)

            //if (_entity.BaseEntity == null)
            //{
            //    CodeMemberField schemaField = new CodeMemberField(
            //        new CodeTypeReference(typeof(Worm.ObjectMappingEngine)),
            //        "_schema"
            //        );
            //    CodeMemberField typeField = new CodeMemberField(
            //        new CodeTypeReference(typeof(Type)),
            //        "_entityType"
            //        );
            //    schemaField.Attributes = MemberAttributes.Family;
            //    SchemaDef.Members.Add(schemaField);
            //    typeField.Attributes = MemberAttributes.Family;
            //    SchemaDef.Members.Add(typeField);
            //    var method = new CodeMemberMethod
            //    {
            //        Name = "GetSchema",
            //        // тип возвращаемого значения
            //        ReturnType = null,
            //        // модификаторы доступа
            //        Attributes = MemberAttributes.Public | MemberAttributes.Final
            //    };

            //    SchemaDef.Members.Add(method);

            //    method.Parameters.Add(
            //        new CodeParameterDeclarationExpression(
            //            new CodeTypeReference(typeof(Worm.ObjectMappingEngine)),
            //            "schema"
            //            )
            //        );
            //    method.Parameters.Add(
            //        new CodeParameterDeclarationExpression(
            //            new CodeTypeReference(typeof(Type)),
            //            "t"
            //            )
            //        );
            //    // реализует метод базового класса
            //    method.ImplementationTypes.Add(typeof(ISchemaInit));
            //    method.Statements.Add(
            //        new CodeAssignStatement(
            //            new CodeFieldReferenceExpression(
            //                new CodeThisReferenceExpression(),
            //                "_schema"
            //                ),
            //            new CodeArgumentReferenceExpression("schema")
            //            )
            //        );
            //    method.Statements.Add(
            //        new CodeAssignStatement(
            //            new CodeFieldReferenceExpression(
            //                new CodeThisReferenceExpression(),
            //                "_entityType"
            //                ),
            //            new CodeArgumentReferenceExpression("t")
            //            )
            //        );
            //}

            //#endregion public void GetSchema(OrmSchemaBase schema, Type t)
        }
Ejemplo n.º 5
0
 public static string GetLinqRelationField(this RelationDefinitionBase rel)
 {
     return((string)rel.Items[LinqContextGenerator.LinqRelationField]);
 }
Ejemplo n.º 6
0
 public RelationDefinitionBase GetSimilarRelation(RelationDefinitionBase relation)
 {
     return(_relations.Find(relation.Similar));
 }
Ejemplo n.º 7
0
 public void AddRelation(RelationDefinitionBase r)
 {
     _relations.Add(r);
 }
Ejemplo n.º 8
0
 private static void GenerateRelScript(SelfRelationTarget rt, StringBuilder script, bool unicodeStrings, RelationDefinitionBase rel)
 {
     for (int i = 0; i < rt.FieldName.Length; i++)
     {
         script.Append(rt.FieldName[i]).Append(" ");
         if (rel is SelfRelationDefinition)
         {
             SelfRelationDefinition   r  = rel as SelfRelationDefinition;
             ScalarPropertyDefinition sp = r.Properties.Skip(i).First();
             script.Append(GetType(sp.SourceField, sp.PropertyType, sp.Attributes, unicodeStrings)).Append(" NOT NULL");
         }
         else if (rel is RelationDefinition)
         {
             LinkTarget lt = rt as LinkTarget;
             ScalarPropertyDefinition sp = lt.Properties.Skip(i).First();
             script.Append(GetType(sp.SourceField, sp.PropertyType, sp.Attributes, unicodeStrings)).Append(" NOT NULL");
         }
         else
         {
             throw new NotSupportedException(rel.GetType().ToString());
         }
     }
 }