Example #1
0
        private static void PrepareSourceTree(Source currentSource, ref ClassDef currentClassDef)
        {
            while (currentSource != null)
            {
                Source childSource = currentSource.ChildSource;
                currentSource.EntityName = currentClassDef.GetTableName();
                if (childSource != null)
                {
                    string           relationshipName = childSource.Name;
                    IRelationshipDef relationshipDef  = currentClassDef.GetRelationship(relationshipName);
                    if (relationshipDef == null)
                    {
                        string message = string.Format("'{0}' does not have a relationship called '{1}'.",
                                                       currentClassDef.ClassName, relationshipName);
                        throw new RelationshipNotFoundException(message);
                    }
                    foreach (RelPropDef relPropDef in relationshipDef.RelKeyDef)
                    {
                        string ownerFieldName   = currentClassDef.GetPropDef(relPropDef.OwnerPropertyName).DatabaseFieldName;
                        string relatedFieldName =
                            relationshipDef.RelatedObjectClassDef.GetPropDef(relPropDef.RelatedClassPropName).DatabaseFieldName;
                        QueryField fromField = new QueryField(relPropDef.OwnerPropertyName, ownerFieldName, currentSource);
                        QueryField toField   = new QueryField(relPropDef.RelatedClassPropName, relatedFieldName, childSource);
                        currentSource.Joins[0].JoinFields.Add(new Source.Join.JoinField(fromField, toField));
                    }
                    currentClassDef = (ClassDef)relationshipDef.RelatedObjectClassDef;
                }

                currentSource = childSource;
            }
        }
Example #2
0
        public void Test_GetLabel_WhenUIFormFieldHasClassDef()
        {
            //---------------Set up test pack-------------------
            ClassDef classDef    = CreateTestClassDef("");
            var      uiFormField = new UIFormFieldStub(null, "TestProperty");

            uiFormField.SetLabel(null);
            uiFormField.SetClassDef(classDef);
            //---------------Assert Precondition----------------
            Assert.IsNotNull(classDef.GetPropDef(uiFormField.PropertyName));
            //Assert.AreSame(classDef, uiFormField.GetClassDef());
            Assert.AreSame(classDef, uiFormField.ClassDef);
            Assert.IsNull(uiFormField.Label);
            //---------------Execute Test ----------------------
            var actualLabel = uiFormField.GetLabel();

            //---------------Test Result -----------------------
            Assert.AreEqual("Tested Property:", actualLabel);
        }
Example #3
0
        private bool HasPropDef(string propertyName)
        {
            var propDef = ClassDef.GetPropDef(propertyName, false);

            return(propDef != null);
        }
Example #4
0
        private void CreateRelationships(ClassDefCol classDefCol, ClassDef classDef)
        {
            //ALTER TABLE `invoice` 
            //  ADD CONSTRAINT `Invoice_InvoiceStatus_FK` FOREIGN KEY `Invoice_InvoiceStatus_FK` (`InvoiceStatusID`)
            //    REFERENCES `invoicestatus` (`InvoiceStatusID`)
            //    ON DELETE RESTRICT
            //    ON UPDATE RESTRICT;

            if (classDef.SuperClassDef != null && classDef.SuperClassDef.ORMapping == ORMapping.SingleTableInheritance)
            {
                return;
            }
            string sqlStatement;
            sqlStatement = "ALTER TABLE ";
            sqlStatement += _databaseConnection.SqlFormatter.DelimitTable(classDef.TableName);
            List<string> constraints = new List<string>();
            foreach (RelationshipDef relationshipDef in classDef.RelationshipDefCol)
            {
                if (relationshipDef is SingleRelationshipDef)
                {
                    if (!classDefCol.Contains(relationshipDef.RelatedObjectAssemblyName, relationshipDef.RelatedObjectClassName))
                    {
                        throw new Exception("Related class not found:" + relationshipDef.RelatedObjectAssemblyName + "." + relationshipDef.RelatedObjectClassName);
                    }
                    IClassDef relatedClassDef = classDefCol[relationshipDef.RelatedObjectAssemblyName, relationshipDef.RelatedObjectClassName];

                    string constraintName = _databaseConnection.SqlFormatter.DelimitField(
                        classDef.TableName + "_" + relationshipDef.RelationshipName + "_FK");
                    string constraintSql = " ADD CONSTRAINT " + constraintName;
                    constraintSql += " FOREIGN KEY " + constraintName;
                    List<string> props = new List<string>();
                    List<string> relProps = new List<string>();
                    foreach (RelPropDef relPropDef in relationshipDef.RelKeyDef)
                    {
                        string propName = relPropDef.OwnerPropertyName;
                        string relPropName = relPropDef.RelatedClassPropName;
                        IPropDef ownerPropDef = classDef.GetPropDef(propName);
                        if (ownerPropDef != null)
                        {
                            propName = ownerPropDef.DatabaseFieldName;
                        }
                        IPropDef relatedPropDef = relatedClassDef.GetPropDef(relPropName);
                        if (relatedPropDef != null)
                        {
                            relPropName = relatedPropDef.DatabaseFieldName;
                        }
                        props.Add(_databaseConnection.SqlFormatter.DelimitField(propName));
                        relProps.Add(_databaseConnection.SqlFormatter.DelimitField(relPropName));
                    }
                    constraintSql += " (" + String.Join(",", props.ToArray()) + ")";
                    constraintSql += " REFERENCES ";
                    IClassDef relatedBaseClassDef = relatedClassDef;
                    while (relatedBaseClassDef.SuperClassDef != null && relatedBaseClassDef.SuperClassDef.ORMapping == ORMapping.SingleTableInheritance)
                    {
                        relatedBaseClassDef = (ClassDef)relatedBaseClassDef.SuperClassDef.SuperClassClassDef;
                    }
                    string relatedTableName = relatedBaseClassDef.TableName;
                    constraintSql += _databaseConnection.SqlFormatter.DelimitTable(relatedTableName);
                    constraintSql += " (" + String.Join(",", relProps.ToArray()) + ")";
                    constraintSql += " ON DELETE RESTRICT ";
                    constraintSql += " ON UPDATE RESTRICT";
                    constraints.Add(constraintSql);
                }
            }
            sqlStatement += String.Join(", ", constraints.ToArray());
            sqlStatement += ";";
            if (constraints.Count > 0)
            {
                _statements.Add(sqlStatement);
            }
        }
Example #5
0
        private static void PrepareSourceTree(Source currentSource, ref ClassDef currentClassDef)
        {
            while (currentSource != null)
            {
                Source childSource = currentSource.ChildSource;
                currentSource.EntityName = currentClassDef.GetTableName();
                if (childSource != null)
                {
                    string relationshipName = childSource.Name;
                    IRelationshipDef relationshipDef = currentClassDef.GetRelationship(relationshipName);
                    if (relationshipDef == null)
                    {
                        string message = string.Format("'{0}' does not have a relationship called '{1}'.",
                                                       currentClassDef.ClassName, relationshipName);
                        throw new RelationshipNotFoundException(message);
                    }
                    foreach (RelPropDef relPropDef in relationshipDef.RelKeyDef)
                    {
                        string ownerFieldName = currentClassDef.GetPropDef(relPropDef.OwnerPropertyName).DatabaseFieldName;
                        string relatedFieldName = 
                            relationshipDef.RelatedObjectClassDef.GetPropDef(relPropDef.RelatedClassPropName).DatabaseFieldName;
                        QueryField fromField = new QueryField(relPropDef.OwnerPropertyName, ownerFieldName, currentSource);
                        QueryField toField = new QueryField(relPropDef.RelatedClassPropName, relatedFieldName, childSource);
                        currentSource.Joins[0].JoinFields.Add(new Source.Join.JoinField(fromField, toField));
                    }
                    currentClassDef = (ClassDef) relationshipDef.RelatedObjectClassDef;
                }

                currentSource = childSource;
            }
        }
Example #6
0
 public void TestGetMissingPropDefReturnsException()
 {
     //---------------Set up test pack-------------------
     ClassDef classDef = new ClassDef(typeof(String), null, new PropDefCol(), null, null);
     //---------------Execute Test ----------------------
     try
     {
         classDef.GetPropDef("wrongprop", true);
         Assert.Fail("Expected to throw an InvalidPropertyNameException");
     }
         //---------------Test Result -----------------------
     catch (InvalidPropertyNameException ex)
     {
         StringAssert.Contains("The property definition for the property 'wrongprop' could not be found", ex.Message);
     }
 }
Example #7
0
 public void TestGetMissingPropDefReturnsNull()
 {
     ClassDef classDef = new ClassDef(typeof(String), null, new PropDefCol(), null, null);
     Assert.IsNull(classDef.GetPropDef("wrongprop", false));
 }
Example #8
0
        private void CreateRelationships(ClassDefCol classDefCol, ClassDef classDef)
        {
            //ALTER TABLE `invoice`
            //  ADD CONSTRAINT `Invoice_InvoiceStatus_FK` FOREIGN KEY `Invoice_InvoiceStatus_FK` (`InvoiceStatusID`)
            //    REFERENCES `invoicestatus` (`InvoiceStatusID`)
            //    ON DELETE RESTRICT
            //    ON UPDATE RESTRICT;

            if (classDef.SuperClassDef != null && classDef.SuperClassDef.ORMapping == ORMapping.SingleTableInheritance)
            {
                return;
            }
            string sqlStatement;

            sqlStatement  = "ALTER TABLE ";
            sqlStatement += _databaseConnection.SqlFormatter.DelimitTable(classDef.TableName);
            List <string> constraints = new List <string>();

            foreach (RelationshipDef relationshipDef in classDef.RelationshipDefCol)
            {
                if (relationshipDef is SingleRelationshipDef)
                {
                    if (!classDefCol.Contains(relationshipDef.RelatedObjectAssemblyName, relationshipDef.RelatedObjectClassName))
                    {
                        throw new Exception("Related class not found:" + relationshipDef.RelatedObjectAssemblyName + "." + relationshipDef.RelatedObjectClassName);
                    }
                    IClassDef relatedClassDef = classDefCol[relationshipDef.RelatedObjectAssemblyName, relationshipDef.RelatedObjectClassName];

                    string constraintName = _databaseConnection.SqlFormatter.DelimitField(
                        classDef.TableName + "_" + relationshipDef.RelationshipName + "_FK");
                    string constraintSql = " ADD CONSTRAINT " + constraintName;
                    constraintSql += " FOREIGN KEY " + constraintName;
                    List <string> props    = new List <string>();
                    List <string> relProps = new List <string>();
                    foreach (RelPropDef relPropDef in relationshipDef.RelKeyDef)
                    {
                        string   propName     = relPropDef.OwnerPropertyName;
                        string   relPropName  = relPropDef.RelatedClassPropName;
                        IPropDef ownerPropDef = classDef.GetPropDef(propName);
                        if (ownerPropDef != null)
                        {
                            propName = ownerPropDef.DatabaseFieldName;
                        }
                        IPropDef relatedPropDef = relatedClassDef.GetPropDef(relPropName);
                        if (relatedPropDef != null)
                        {
                            relPropName = relatedPropDef.DatabaseFieldName;
                        }
                        props.Add(_databaseConnection.SqlFormatter.DelimitField(propName));
                        relProps.Add(_databaseConnection.SqlFormatter.DelimitField(relPropName));
                    }
                    constraintSql += " (" + String.Join(",", props.ToArray()) + ")";
                    constraintSql += " REFERENCES ";
                    IClassDef relatedBaseClassDef = relatedClassDef;
                    while (relatedBaseClassDef.SuperClassDef != null && relatedBaseClassDef.SuperClassDef.ORMapping == ORMapping.SingleTableInheritance)
                    {
                        relatedBaseClassDef = (ClassDef)relatedBaseClassDef.SuperClassDef.SuperClassClassDef;
                    }
                    string relatedTableName = relatedBaseClassDef.TableName;
                    constraintSql += _databaseConnection.SqlFormatter.DelimitTable(relatedTableName);
                    constraintSql += " (" + String.Join(",", relProps.ToArray()) + ")";
                    constraintSql += " ON DELETE RESTRICT ";
                    constraintSql += " ON UPDATE RESTRICT";
                    constraints.Add(constraintSql);
                }
            }
            sqlStatement += String.Join(", ", constraints.ToArray());
            sqlStatement += ";";
            if (constraints.Count > 0)
            {
                _statements.Add(sqlStatement);
            }
        }