Example #1
0
        // ManyToMany
        protected Dictionary <string, object> TransPropertyValues(ManyToManyRelationship relationship,
                                                                  Dictionary <string, object> parent, Dictionary <string, object> child)
        {
            Dictionary <string, object> propertyValues = new Dictionary <string, object>();

            DirectRelationship firstDirectRelationship  = relationship.DirectRelationships[0];
            DirectRelationship secondDirectRelationship = relationship.DirectRelationships[1];

            for (int i = 0; i < firstDirectRelationship.Properties.Length; i++)
            {
                string propertyName        = firstDirectRelationship.Properties[i];
                string relatedPropertyName = firstDirectRelationship.RelatedProperties[i];
                propertyValues.Add(relatedPropertyName, parent[propertyName]);
            }

            for (int i = 0; i < secondDirectRelationship.RelatedProperties.Length; i++)
            {
                string relatedPropertyName = secondDirectRelationship.RelatedProperties[i];
                string propertyName        = secondDirectRelationship.Properties[i];

                propertyValues.Add(propertyName, child[relatedPropertyName]);
            }

            return(propertyValues);
        }
Example #2
0
        protected void Split(IEnumerable <T> children, XElement childEntitySchema, ManyToManyRelationship manyToManyRelationship, Dictionary <string, object> parentPropertyValues, string childPath,
                             ICollection <UpdateCommandNode <T> > childNodes)
        {
            string childEntity = childEntitySchema.Attribute(SchemaVocab.Name).Value;

            XElement mmKeySchema         = TransKeySchema(manyToManyRelationship, out XElement mmEntitySchema);
            string   mmEntity            = mmEntitySchema.Attribute(SchemaVocab.Name).Value;
            XElement mmConcurrencySchema = GetConcurrencySchema(mmEntitySchema);

            int index = 0;

            foreach (T child in children)
            {
                Dictionary <string, object> childPropertyValues = GetPropertyValues(child, childEntitySchema);
                Dictionary <string, object> mmPropertyValues    = TransPropertyValues(manyToManyRelationship, parentPropertyValues, childPropertyValues);

                Dictionary <string, object> mmUpdatePropertyValues = new Dictionary <string, object>(mmPropertyValues);

                UpdateCommandNode <T> mmExecuteCommand = CreateUpdateCommandNode(child, childEntity);

                mmExecuteCommand.EntitySchema              = mmEntitySchema;
                mmExecuteCommand.UniqueKeySchema           = mmKeySchema;
                mmExecuteCommand.ConcurrencySchema         = mmConcurrencySchema;
                mmExecuteCommand.PropertyValues            = mmPropertyValues;
                mmExecuteCommand.ParentPropertyValues      = parentPropertyValues;
                mmExecuteCommand.ParentRelationship        = manyToManyRelationship.DirectRelationships[0];
                mmExecuteCommand.FixedUpdatePropertyValues = mmUpdatePropertyValues;
                mmExecuteCommand.Path = string.Format("{0}[{1}]", childPath, index);

                childNodes.Add(mmExecuteCommand);
                index++;
            }
        }
Example #3
0
        public LambdaExpression GetCollectionSelector(ManyToManyRelationship relationship)
        {
            var memInfo = ((MemberExpression)relationship.SelfSelectorFromMap.Body).Member;

            return(GetMapping <IMapEntityMapping>(memInfo.DeclaringType).GetManyToManyMaps()
                   .Where(m => ((MemberExpression)m.EntitySelector.Body).Member.Name == memInfo.Name)
                   .Select(m => m.MapTableEntitySelector).FirstOrDefault());
        }
Example #4
0
        // ManyToMany
        protected bool IsDeleteTransSetNull(ManyToManyRelationship relationship)
        {
            DirectRelationship firstDirectRelationship = relationship.DirectRelationships[0];

            // pure relationship-table
            if (relationship.DirectRelationships.Length == 2)
            {
                DirectRelationship secondDirectRelationship = relationship.DirectRelationships[1];
                string             relationshipEntity       = firstDirectRelationship.RelatedEntity;

                if (relationshipEntity == secondDirectRelationship.Entity) // assert
                {
                    List <string> relationshipProperties = new List <string>(firstDirectRelationship.RelatedProperties);
                    relationshipProperties.AddRange(secondDirectRelationship.Properties);

                    XElement entitySchema = Schema.GetEntitySchema(relationshipEntity);

                    List <string> PropertyNames = entitySchema.Elements(SchemaVocab.Property)
                                                  .Where(x => x.Attribute(SchemaVocab.Column) != null)
                                                  .Select(x => x.Attribute(SchemaVocab.Name).Value).ToList();

                    if (PropertyNames.Count == relationshipProperties.Count)
                    {
                        bool pure = true;
                        relationshipProperties.Sort();
                        PropertyNames.Sort();
                        for (int i = 0; i < PropertyNames.Count; i++)
                        {
                            if (relationshipProperties[i] == PropertyNames[i])
                            {
                                continue;
                            }
                            pure = false;
                        }
                        if (pure)
                        {
                            return(false);
                        }
                    }
                }
            }

            //
            string   mmEntity       = firstDirectRelationship.RelatedEntity;
            XElement mmEntitySchema = GetEntitySchema(mmEntity);

            for (int i = 0; i < firstDirectRelationship.RelatedProperties.Length; i++)
            {
                string   relatedPropertyName = firstDirectRelationship.RelatedProperties[i];
                XElement mmPropertySchema    = GetPropertySchema(mmEntitySchema, relatedPropertyName);
                if (mmPropertySchema.Attribute(SchemaVocab.AllowDBNull).Value == SchemaVocab.True)
                {
                    return(true);
                }
            }

            return(false);
        }
Example #5
0
 public ManyToManyMapType(Type originalType, 
                         Type translatedType, 
                         Type mappedItemType,
                         ManyToManyRelationship relationship,
                         MemberInfo mapping)
         : base(originalType, translatedType)
 {
     this.MappedItemType = mappedItemType;
     this.Relationship = relationship;
     this.Mapping = mapping;
 }
Example #6
0
 public ManyToManyMapType(Type originalType,
                          Type translatedType,
                          Type mappedItemType,
                          ManyToManyRelationship relationship,
                          MemberInfo mapping)
     : base(originalType, translatedType)
 {
     this.MappedItemType = mappedItemType;
     this.Relationship   = relationship;
     this.Mapping        = mapping;
 }
Example #7
0
        // ManyToMany
        protected XElement TransKeySchema(ManyToManyRelationship relationship, out XElement mmEntitySchema)
        {
            DirectRelationship oneToManyRelationship = relationship.DirectRelationships[0];
            DirectRelationship manyToOneRelationship = relationship.DirectRelationships[1];

            string mmEntity = oneToManyRelationship.RelatedEntity;

            mmEntitySchema = GetEntitySchema(mmEntity);

            XElement             mmKeySchema        = new XElement(mmEntitySchema.Name);
            IEnumerable <string> mmKeyPropertyNames = oneToManyRelationship.RelatedProperties.Union(manyToOneRelationship.Properties);

            foreach (string mmPropertyName in mmKeyPropertyNames)
            {
                mmKeySchema.Add(mmEntitySchema.Elements(SchemaVocab.Property).First(p => p.Attribute(SchemaVocab.Name).Value == mmPropertyName));
            }

            return(mmKeySchema);
        }
        protected void Split(IEnumerable <T> children, IEnumerable <T> origChildren, XElement childEntitySchema, ManyToManyRelationship manyToManyRelationship,
                             Dictionary <string, object> parentPropertyValues, string childrenPath, string parentEntity, XElement schema,
                             ICollection <UpdateCommandNode <T> > updateCommandNodes)
        {
            string childEntity = childEntitySchema.Attribute(SchemaVocab.Name).Value;

            XElement mmKeySchema         = TransKeySchema(manyToManyRelationship, out XElement mmEntitySchema);
            string   mmEntity            = mmEntitySchema.Attribute(SchemaVocab.Name).Value;
            XElement mmConcurrencySchema = GetConcurrencySchema(mmEntitySchema);

            int origIndex = 0;

            foreach (T origChild in origChildren)
            {
                Dictionary <string, object> childPropertyValues = GetPropertyValues(origChild, childEntitySchema);
                Dictionary <string, object> mmPropertyValues    = TransPropertyValues(manyToManyRelationship, parentPropertyValues, childPropertyValues);

                Dictionary <string, object> mmUpdatePropertyValues = new Dictionary <string, object>(mmPropertyValues);

                UpdateCommandNode <T> mmExecuteCommand = CreateUpdateCommandNode(default(T), origChild, childEntity);
                mmExecuteCommand.EntitySchema              = mmEntitySchema;
                mmExecuteCommand.UniqueKeySchema           = mmKeySchema;
                mmExecuteCommand.ConcurrencySchema         = mmConcurrencySchema;
                mmExecuteCommand.PropertyValues            = new Dictionary <string, object>(); // avoid throwing NullReferenceException
                mmExecuteCommand.OrigPropertyValues        = mmPropertyValues;
                mmExecuteCommand.ParentPropertyValues      = parentPropertyValues;
                mmExecuteCommand.ParentRelationship        = manyToManyRelationship.DirectRelationships[0];
                mmExecuteCommand.FixedUpdatePropertyValues = mmUpdatePropertyValues;
                mmExecuteCommand.Path = string.Format("{0}[+{1}]", childrenPath, origIndex);

                updateCommandNodes.Add(mmExecuteCommand);
                origIndex++;
            }

            //
            List <string> createNodePaths = new List <string>();
            List <T>      createNodes     = new List <T>();

            int index = 0;

            foreach (T child in children)
            {
                Dictionary <string, object> childPropertyValues = GetPropertyValues(child, childEntitySchema);
                Dictionary <string, object> mmPropertyValues    = TransPropertyValues(manyToManyRelationship, parentPropertyValues, childPropertyValues);

                UpdateCommandNode <T> origChildCommandNode = Find(updateCommandNodes, mmPropertyValues);
                if (origChildCommandNode == null)
                {
                    createNodePaths.Add(string.Format("{0}[{1}]", childrenPath, index));
                    createNodes.Add(child);
                }
                else
                {
                    // update
                    SetUpdateCommandNode(origChildCommandNode, child);
                    origChildCommandNode.Path           = string.Format("{0}[{1}]", childrenPath, index);
                    origChildCommandNode.PropertyValues = mmPropertyValues;
                }
                index++;
            }

            // create
            ExecuteCommand <T>[] createCommands = GetCreateAggregationCommands(createNodes, childEntitySchema, manyToManyRelationship, parentPropertyValues, childrenPath, parentEntity, schema).ToArray();

            // reset path
            for (int i = 0; i < createCommands.Length; i++)
            {
                createCommands[i].Path = createNodePaths[i];
            }
            Commands.AddRange(createCommands);

            // delete
            UpdateCommandNode <T>[] deleteNodes    = updateCommandNodes.Where(p => p.AggregNode == null).ToArray();
            ExecuteCommand <T>[]    deleteCommands = GetDeleteAggregationCommands(deleteNodes.Select(p => p.OrigNode), childEntitySchema, manyToManyRelationship, parentPropertyValues, childrenPath, parentEntity, schema).ToArray();

            // reset path
            for (int i = 0; i < deleteCommands.Length; i++)
            {
                deleteCommands[i].Path = deleteNodes[i].Path;
            }
            DeleteCommands.AddRange(deleteCommands);
        }
Example #9
0
        private void FormRelationship_Load(object sender, EventArgs e)
        {
            foreach (ScriptObject scriptObject in _scriptObjects)
            {
                comboBoxPrimaryScriptObject.Items.Add(scriptObject);
            }
            comboBoxPrimaryScriptObject.DisplayMember = "Alias";
            comboBoxPrimaryScriptObject.ValueMember   = "Name";

            foreach (OneToManyRelationship oneToManyRelationship in _parent.OneToManyRelationships)
            {
                comboBoxIntermediatePrimaryRelationship.Items.Add(oneToManyRelationship);
            }
            comboBoxIntermediatePrimaryRelationship.DisplayMember = "Alias";
            comboBoxIntermediatePrimaryRelationship.ValueMember   = "Name";

            foreach (ScriptObject scriptObject in _scriptObjects)
            {
                comboBoxForeignScriptObject.Items.Add(scriptObject);
            }
            comboBoxForeignScriptObject.DisplayMember = "Alias";
            comboBoxForeignScriptObject.ValueMember   = "Name";

            groupBoxType.Enabled = false;
            string relationshipText = "";

            switch (TypeOfRelationship.Name)
            {
            case "ManyToOneRelationship":
                relationshipText = "Many-To-One Relationship";
                break;

            case "ManyToManyRelationship":
                relationshipText = "Many-To-Many Relationship";
                break;

            case "OneToOneRelationship":
                relationshipText = "One-To-One Relationship";
                break;

            case "OneToManyRelationship":
                relationshipText = "One-To-Many Relationship";
                break;

            default:
                throw new NotImplementedException("Unexpected relationship type: " + _primaryRelationship.GetType().Name);
            }
            if (_primaryRelationship == null)
            {
                this.Text = string.Format("New {0}", relationshipText);
                comboBoxPrimaryScriptObject.SelectedItem = _parent;

                checkBoxIsBase.Enabled = true;
            }
            else
            {
                this.Text               = string.Format("Edit {0} ({1})", _primaryRelationship.Name, relationshipText);
                textBoxName.Text        = _primaryRelationship.Name;
                textBoxAlias.Text       = _primaryRelationship.Alias;
                textBoxPath.Text        = _primaryRelationship.Path;
                textBoxDescription.Text = _primaryRelationship.Description;

                comboBoxPrimaryScriptObject.SelectedItem = _parent;
                foreach (Column column in _primaryRelationship.PrimaryColumns)
                {
                    ListViewItem item = new ListViewItem(column.Alias);
                    item.SubItems.Add(column.Name);
                    item.Tag = column;

                    listViewPrimaryColumn.Items.Add(item);
                }
                comboBoxForeignScriptObject.SelectedItem = _primaryRelationship.ForeignScriptObject;
                foreach (Column column in _primaryRelationship.ForeignColumns)
                {
                    ListViewItem item = new ListViewItem(column.Alias);
                    item.SubItems.Add(column.Name);
                    item.Tag = column;

                    listViewForeignColumn.Items.Add(item);
                }
                UpdatePrimaryFilters();
                UpdateForeignFilters();

                if (_primaryRelationship.GetType() == typeof(ManyToManyRelationship))
                {
                    ManyToManyRelationship manyToManyRelationship = (ManyToManyRelationship)_primaryRelationship;

                    comboBoxIntermediatePrimaryRelationship.SelectedItem = manyToManyRelationship.IntermediatePrimaryRelationship;
                    comboBoxIntermediateForeignRelationship.SelectedItem = manyToManyRelationship.IntermediateForeignRelationship;

                    tabStripRelationship.Pages.RemoveAt(0);
                    tabStripRelationship.Pages.RemoveAt(1);
                }
                else
                {
                    tabStripRelationship.Pages.RemoveAt(1);

                    comboBoxPrimaryFilter.SelectedItem = _primaryRelationship.Filter;
                    comboBoxForeignFilter.SelectedItem = _primaryRelationship.ForeignRelationship.Filter;
                }

                if (_primaryRelationship.GetType() == typeof(OneToOneRelationship))
                {
                    OneToOneRelationship relationship = (OneToOneRelationship)_primaryRelationship;

                    checkBoxIsBase.Checked      = relationship.IsBase;
                    checkBoxIsBase.Visible      = true;
                    radioButtonOneToOne.Checked = true;
                }
                else if (_primaryRelationship.GetType() == typeof(OneToManyRelationship))
                {
                    OneToManyRelationship relationship = (OneToManyRelationship)_primaryRelationship;

                    radioButtonOneToMany.Checked = true;
                }
                else if (_primaryRelationship.GetType() == typeof(ManyToOneRelationship))
                {
                    radioButtonManyToOne.Checked = true;
                }
                else if (_primaryRelationship.GetType() == typeof(ManyToManyRelationship))
                {
                    ManyToManyRelationship relationship = (ManyToManyRelationship)_primaryRelationship;

                    radioButtonManyToMany.Checked = true;
                }

                if (!_primaryRelationship.IsUserDefined)
                {
                    textBoxName.ReadOnly = true;

                    comboBoxPrimaryColumn.Enabled       = false;
                    comboBoxPrimaryScriptObject.Enabled = false;
                    buttonPrimaryColumnAdd.Enabled      = false;
                    listViewPrimaryColumn.Enabled       = false;
                    comboBoxPrimaryFilter.Enabled       = false;

                    comboBoxIntermediatePrimaryRelationship.Enabled = false;
                    comboBoxIntermediateForeignRelationship.Enabled = false;

                    comboBoxForeignColumn.Enabled       = false;
                    comboBoxForeignScriptObject.Enabled = false;
                    buttonForeignColumnAdd.Enabled      = false;
                    listViewForeignColumn.Enabled       = false;
                    comboBoxForeignFilter.Enabled       = false;

                    buttonAddPrimaryFilter.Enabled = false;
                    buttonAddIntermediatePrimaryRelationship.Enabled = false;
                    buttonAddIntermediateForeignRelationship.Enabled = false;
                    buttonAddForeignFilter.Enabled = false;
                }
            }

            listViewPrimaryColumn_Resize(null, null);
            listViewForeignColumn_Resize(null, null);

            //UpdatePrimaryFilters();
            //UpdateForeignFilters();
        }
Example #10
0
        private void buttonOk_Click(object sender, EventArgs e)
        {
            for (int i = this.Controls.Count - 1; i >= 0; i--)
            {
                Control control = this.Controls[i];
                control.Focus();

                // First tab
                tabStripRelationship.SelectedIndex = 0;
                if (!ValidateControl(control))
                {
                    DialogResult = DialogResult.None;
                    return;
                }

                // Second tab
                if (radioButtonOneToOne.Checked ||
                    radioButtonOneToMany.Checked ||
                    radioButtonManyToOne.Checked)
                {
                    tabStripRelationship.SelectedIndex = 1;
                    if (!ValidateControl(control))
                    {
                        DialogResult = DialogResult.None;
                        return;
                    }
                }
            }

            Column[] primaryColumns = new Column[listViewPrimaryColumn.Items.Count];
            for (int i = 0; i < listViewPrimaryColumn.Items.Count; i++)
            {
                ListViewItem listViewItem = listViewPrimaryColumn.Items[i];
                primaryColumns[i] = (Column)listViewItem.Tag;
            }

            Column[] foreignColumns = new Column[listViewForeignColumn.Items.Count];
            for (int i = 0; i < listViewForeignColumn.Items.Count; i++)
            {
                ListViewItem listViewItem = listViewForeignColumn.Items[i];
                foreignColumns[i] = (Column)listViewItem.Tag;
            }

            if (_primaryRelationship == null)
            {
                ScriptObject primaryScriptObject = (ScriptObject)comboBoxPrimaryScriptObject.SelectedItem;
                ScriptObject foreignScriptObject = (ScriptObject)comboBoxForeignScriptObject.SelectedItem;

                Filter primaryFilter = (Filter)comboBoxPrimaryFilter.SelectedItem;
                Filter foreignFilter = (Filter)comboBoxForeignFilter.SelectedItem;

                if (radioButtonOneToOne.Checked)
                {
                    _primaryRelationship             = new OneToOneRelationship(textBoxName.Text, true, _parent, primaryColumns, foreignScriptObject, foreignColumns, primaryFilter, checkBoxIsBase.Checked);
                    _primaryRelationship.Alias       = textBoxAlias.Text;
                    _primaryRelationship.Description = textBoxDescription.Text;
                    _foreignRelationship             = new OneToOneRelationship(primaryScriptObject.Alias, true, foreignScriptObject, foreignColumns, _parent, primaryColumns, foreignFilter, !checkBoxIsBase.Checked);
                    _foreignRelationship.Alias       = primaryScriptObject.Alias;
                }

                if (radioButtonOneToMany.Checked)
                {
                    _primaryRelationship             = new OneToManyRelationship(textBoxName.Text, true, _parent, primaryColumns, foreignScriptObject, foreignColumns, primaryFilter);
                    _primaryRelationship.Alias       = textBoxAlias.Text;
                    _primaryRelationship.Description = textBoxDescription.Text;
                    _foreignRelationship             = new ManyToOneRelationship(primaryScriptObject.Alias, true, foreignScriptObject, foreignColumns, _parent, primaryColumns, foreignFilter);
                    _foreignRelationship.Alias       = primaryScriptObject.Alias;
                }

                if (radioButtonManyToOne.Checked)
                {
                    _primaryRelationship             = new ManyToOneRelationship(textBoxName.Text, true, _parent, primaryColumns, foreignScriptObject, foreignColumns, primaryFilter);
                    _primaryRelationship.Alias       = textBoxAlias.Text;
                    _primaryRelationship.Description = textBoxDescription.Text;
                    _foreignRelationship             = new OneToManyRelationship(primaryScriptObject.AliasPlural, true, foreignScriptObject, foreignColumns, _parent, primaryColumns, foreignFilter);
                    _foreignRelationship.Alias       = primaryScriptObject.AliasPlural;
                }

                if (radioButtonManyToMany.Checked)
                {
                    OneToManyRelationship oneToManyRelationship = (OneToManyRelationship)comboBoxIntermediatePrimaryRelationship.SelectedItem;
                    ManyToOneRelationship manyToOneRelationship = (ManyToOneRelationship)comboBoxIntermediateForeignRelationship.SelectedItem;
                    _primaryRelationship             = new ManyToManyRelationship(textBoxName.Text, true, oneToManyRelationship, manyToOneRelationship, oneToManyRelationship.Filter);
                    _primaryRelationship.Alias       = textBoxAlias.Text;
                    _primaryRelationship.Description = textBoxDescription.Text;
                    _foreignRelationship             = new ManyToManyRelationship(oneToManyRelationship.Parent.AliasPlural, true, (OneToManyRelationship)manyToOneRelationship.ForeignRelationship, (ManyToOneRelationship)oneToManyRelationship.ForeignRelationship, manyToOneRelationship.ForeignRelationship.Filter);
                    _foreignRelationship.Alias       = oneToManyRelationship.Parent.AliasPlural;
                }
                _primaryRelationship.ForeignRelationship = _foreignRelationship;
                _foreignRelationship.ForeignRelationship = _primaryRelationship;
            }
            else
            {
                _primaryRelationship.Name                = textBoxName.Text;
                _primaryRelationship.Alias               = textBoxAlias.Text;
                _primaryRelationship.Description         = textBoxDescription.Text;
                _primaryRelationship.PrimaryColumns      = primaryColumns;
                _primaryRelationship.ForeignScriptObject = (ScriptObject)comboBoxForeignScriptObject.SelectedItem;
                _primaryRelationship.ForeignColumns      = foreignColumns;
                _primaryRelationship.Filter              = (Filter)comboBoxPrimaryFilter.SelectedItem;

                _primaryRelationship.ForeignRelationship.Filter = (Filter)comboBoxForeignFilter.SelectedItem;

                if (_primaryRelationship.GetType() == typeof(ManyToManyRelationship))
                {
                    ManyToManyRelationship manyToManyRelationship = (ManyToManyRelationship)_primaryRelationship;
                    manyToManyRelationship.IntermediatePrimaryRelationship = (OneToManyRelationship)comboBoxIntermediatePrimaryRelationship.SelectedItem;
                    manyToManyRelationship.IntermediateForeignRelationship = (ManyToOneRelationship)comboBoxIntermediateForeignRelationship.SelectedItem;
                }
            }
        }
Example #11
0
        public void FillTableSchema(Table tbl, TableType tableType)
        {
            TableColumnCollection columns = new TableColumnCollection();
            DataSet dsColumns;

            tbl.ForeignKeys = new ForeignKeyCollection();

            if (tableType == TableType.Function)
            {
                if (dsColumns2.Tables[Name] == null)
                {
                    var cmdColumns = new SqlCommand(ROUTINE_COLUMN_SQL_ALL);
                    var dt         = new DataTable(Name);
                    dt.Load(cmdColumns.GetReader());
                    dsColumns2.Tables.Add(dt);
                }
                dsColumns = dsColumns2;
            }
            else
            {
                if (dsColumns1.Tables[Name] == null)
                {
                    var cmdColumns = new SqlCommand(TABLE_COLUMN_SQL_ALL);
                    var dt         = new DataTable(Name);
                    dt.Load(cmdColumns.GetReader());
                    dsColumns1.Tables.Add(dt);
                }
                dsColumns = dsColumns1;
            }

            DataRow[] drColumns = dsColumns.Tables[Name].Select("TableName ='" + tbl.Name + "'", "OrdinalPosition ASC");

            for (int i = 0; i < drColumns.Length; i++)
            {
                TableColumn column = new TableColumn(tbl);
                column.ColumnName     = drColumns[i][SqlSchemaVariable.COLUMN_NAME].ToString();
                column.NativeDataType = drColumns[i][SqlSchemaVariable.DATA_TYPE].ToString();
                if (column.NativeDataType == "numeric")
                {
                    column.NativeDataType = string.Format("Decimal({0},{1})", drColumns[i]["precision"], drColumns[i]["scale"]);
                }
                column.DataType = GetDbType(column.NativeDataType);
                if (drColumns[i][SqlSchemaVariable.COLUMN_DEFAULT] != DBNull.Value)
                {
                    string defaultSetting = drColumns[i][SqlSchemaVariable.COLUMN_DEFAULT].ToString().Trim();
                    if (defaultSetting.ToLower().IndexOf("newsequentialid()") > -1)
                    {
                        column.DefaultSetting = SqlSchemaVariable.DEFAULT;
                    }
                    else
                    {
                        column.DefaultSetting = defaultSetting;
                    }
                }
                column.AutoIncrement = Convert.ToBoolean(drColumns[i][SqlSchemaVariable.IS_IDENTITY]);
                int maxLength;
                int.TryParse(drColumns[i][SqlSchemaVariable.MAX_LENGTH].ToString(), out maxLength);
                column.MaxLength = maxLength;
                if (maxLength > 0)
                {
                    column.NativeDataType += "(" + maxLength + ")";
                }
                column.IsNullable = drColumns[i][SqlSchemaVariable.IS_NULLABLE].ToString() == "YES";
                bool isComputed = (drColumns[i][SqlSchemaVariable.IS_COMPUTED].ToString() == "1");
                column.IsReadOnly = (column.NativeDataType == "timestamp" || isComputed);
                columns.Add(column);
                tbl.SchemaName = drColumns[i]["Owner"].ToString();
            }
            tbl.Columns = columns;

            if (dsIndex.Tables[Name] == null)
            {
                var       cmdIndex = new SqlCommand(INDEX_SQL_ALL);
                DataTable dt       = new DataTable(Name);
                dt.Load(cmdIndex.GetReader());
                dsIndex.Tables.Add(dt);
            }

            DataRow[] drIndexes = dsIndex.Tables[Name].Select("TableName ='" + tbl.Name + "'");
            for (int i = 0; i < drIndexes.Length; i++)
            {
                string      colName        = drIndexes[i][SqlSchemaVariable.COLUMN_NAME].ToString();
                string      constraintType = drIndexes[i][SqlSchemaVariable.CONSTRAINT_TYPE].ToString();
                TableColumn column         = columns.GetColumn(colName);

                if (Utility.IsMatch(constraintType, SqlSchemaVariable.PRIMARY_KEY))
                {
                    column.IsPrimaryKey = true;
                }
                else if (Utility.IsMatch(constraintType, SqlSchemaVariable.FOREIGN_KEY))
                {
                    column.IsForeignKey = true;
                }
                //HACK: Allow second pass naming adjust based on whether a column is keyed
                column.ColumnName = column.ColumnName;
            }

            if (dtFKR == null)
            {
                var cmdFKR = new SqlCommand(FOREIGN_KEY_RELATIONSHIPS);
                dtFKR = new DataTable();
                dtFKR.Load(cmdFKR.GetReader());

                var cmdFK = new SqlCommand(FOREIGN_KEYS);
                dtFK = new DataTable();
                dtFK.Load(cmdFK.GetReader());
            }

            DataRow[] drfkr;
            drfkr = dtFKR.Select("PkTable ='" + tbl.Name + "'");
            for (int i = 0; i < drfkr.Length; i++)
            {
                Relationship rel = new Relationship();
                rel.Name = drfkr[i]["RelName"].ToString();
                Table fktable = Db.Service.GetSchema(drfkr[i]["FkTable"].ToString());
                rel.ClassNameOne  = tbl.ClassName;
                rel.ClassNameMany = fktable.ClassName;
                string[] a = Regex.Split(rel.Name, "__");
                if (a.Length == 2)
                {
                    rel.PropertyNameMany = a[0]; // name used in primary table to fetch many of this table
                    rel.PropertyNameOne  = a[1]; // named used for foreign key in this table
                }
                else
                {
                    rel.PropertyNameOne  = tbl.ClassName;
                    rel.PropertyNameMany = fktable.ClassNamePlural;
                }

                DataRow[] drfk = dtFK.Select("PkTable = '" + tbl.Name + "' and RelName = '" + rel.Name + "'");
                for (int ii = 0; ii < drfk.Length; ii++)
                {
                    Relationship.KeyPair kp    = new Relationship.KeyPair();
                    TableColumn          fkcol = fktable.Columns.GetColumn(drfk[ii]["FkColumn"].ToString());
                    TableColumn          pkcol = columns.GetColumn(drfk[ii]["PkColumn"].ToString());
                    kp.vartype    = fkcol.VarType;
                    kp.ForeignKey = fkcol.Name;
                    kp.PrimaryKey = pkcol.Name;
                    rel.KeyPairs.Add(kp);
                    if (ii > 0)
                    {
                        rel.ForeignKey += ",";
                        rel.PrimaryKey += ",";
                    }
                    rel.PrimaryKey += kp.PrimaryKey;
                    rel.ForeignKey += kp.ForeignKey;
                }


                tbl.ForeignKeyTables.Add(rel);
            }

            drfkr = dtFKR.Select("FkTable ='" + tbl.Name + "'");
            for (int i = 0; i < drfkr.Length; i++)
            {
                Relationship rel = new Relationship();
                rel.Name = drfkr[i]["RelName"].ToString();
                Table pktable = Db.Service.GetSchema(drfkr[i]["PkTable"].ToString());
                rel.ClassNameOne  = pktable.ClassName;
                rel.ClassNameMany = tbl.ClassName;
                string[] a = Regex.Split(rel.Name, "__");
                if (a.Length == 2)
                {
                    rel.PropertyNameMany = a[0]; // name used in primary table to fetch many of this table
                    rel.PropertyNameOne  = a[1]; // named used for foreign key in this table
                }
                else
                {
                    rel.PropertyNameOne  = pktable.ClassName;
                    rel.PropertyNameMany = tbl.ClassNamePlural;
                }

                DataRow[] drfk = dtFK.Select("FkTable = '" + tbl.Name + "' and RelName = '" + rel.Name + "'");
                for (int ii = 0; ii < drfk.Length; ii++)
                {
                    Relationship.KeyPair kp    = new Relationship.KeyPair();
                    TableColumn          fkcol = columns.GetColumn(drfk[ii]["FkColumn"].ToString());
                    TableColumn          pkcol = pktable.Columns.GetColumn(drfk[ii]["PkColumn"].ToString());
                    kp.vartype    = fkcol.VarType;
                    kp.ForeignKey = fkcol.Name;
                    kp.PrimaryKey = pkcol.Name;
                    rel.KeyPairs.Add(kp);
                    if (ii > 0)
                    {
                        rel.ForeignKey += ",";
                        rel.PrimaryKey += ",";
                    }
                    rel.PrimaryKey       += kp.PrimaryKey;
                    rel.ForeignKey       += kp.ForeignKey;
                    fkcol.PropertyNameOne = rel.PropertyNameOne;
                }

                tbl.ForeignKeys.Add(rel);
            }

            if (dsManyToManyCheck.Tables[Name] == null)
            {
                var cmdM2M = new SqlCommand(MANY_TO_MANY_CHECK_ALL);
                var dt     = new DataTable(Name);
                dt.Load(cmdM2M.GetReader());
                dsManyToManyCheck.Tables.Add(dt);
            }

            DataRow[] drs = dsManyToManyCheck.Tables[Name].Select("PK_Table = '" + tbl.Name + "'");
            if (drs.Length > 0)
            {
                for (int count = 0; count < drs.Length; count++)
                {
                    string mapTable = drs[count]["FK_Table"].ToString();
                    string localKey = drs[count]["FK_Column"].ToString();
                    if (dsManyToManyMap.Tables[Name] == null)
                    {
                        var       cmdM2MMap = new SqlCommand(MANY_TO_MANY_FOREIGN_MAP_ALL);
                        DataTable dt        = new DataTable(Name);
                        dt.Load(cmdM2MMap.GetReader());
                        dsManyToManyMap.Tables.Add(dt);
                    }


                    DataRow[] drMap = dsManyToManyMap.Tables[Name].Select("FK_Table = '" + mapTable + "' AND PK_Table <> '" + tbl.Name + "'");

                    for (int i = 0; i < drMap.Length; i++)
                    {
                        ManyToManyRelationship m = new ManyToManyRelationship(mapTable);
                        m.ForeignTableName              = drMap[i]["PK_Table"].ToString();
                        m.ForeignPrimaryKey             = drMap[i]["PK_Column"].ToString();
                        m.MapTableLocalTableKeyColumn   = localKey;
                        m.MapTableForeignTableKeyColumn = drMap[i]["FK_Column"].ToString();
                        tbl.ManyToManys.Add(m);
                    }
                }
            }
        }
Example #12
0
        private void InitialUpdateRelationships(IList <Model.Table> tables)
        {
            foreach (Model.Table table in tables)
            {
                foreach (Key key in table.Keys)
                {
                    // Look for one to one relationship
                    if (key.Type != DatabaseConstant.KeyType.Foreign)
                    {
                        continue;
                    }
                    if (IsOneToOneRelationship(key))
                    {
                        Filter filter = Search.GetFilter(table.Filters, new Helper(DalAssemblyName).GetPrimaryKey((Model.Table)key.Parent).Name);
                        OneToOneRelationship oneToOneRelationship = new OneToOneRelationship("One_" + key.Name, false, table, key.Columns, key.ReferencedTable, key.ReferencedKey.Columns, filter, false);
                        table.AddRelationship(oneToOneRelationship);

                        // Back other way
                        Filter filter2 = Search.GetFilter(key.ReferencedKey.Parent.Filters, new Helper(DalAssemblyName).GetPrimaryKey((Model.Table)key.ReferencedKey.Parent).Name);
                        OneToOneRelationship oneToOneRelationship2 = new OneToOneRelationship("One_" + key.Name, false, key.ReferencedTable, key.ReferencedKey.Columns, table, key.Columns, filter2, true);
                        key.ReferencedTable.AddRelationship(oneToOneRelationship2);

                        oneToOneRelationship.ForeignRelationship  = oneToOneRelationship2;
                        oneToOneRelationship2.ForeignRelationship = oneToOneRelationship;
                    }
                    else
                    {
                        Filter filter = Search.GetFilter(table.Filters, key.Name);
                        ManyToOneRelationship manyToOneRelationship = new ManyToOneRelationship("One_" + key.Name, false, table, key.Columns, key.ReferencedTable, key.ReferencedKey.Columns, filter);
                        table.AddRelationship(manyToOneRelationship);

                        // Back other way
                        Filter filter2 = Search.GetFilter(key.ReferencedTable.Filters, key.ReferencedKey.Name);
                        OneToManyRelationship oneToManyRelationship = new OneToManyRelationship("Many_" + key.Name, false, key.ReferencedTable, key.ReferencedKey.Columns, table, key.Columns, filter2);
                        key.ReferencedTable.AddRelationship(oneToManyRelationship);

                        manyToOneRelationship.ForeignRelationship = oneToManyRelationship;
                        oneToManyRelationship.ForeignRelationship = manyToOneRelationship;
                    }
                }
            }
            // Temp list to search for foreign relationship
            List <Relationship> manyToManyRelationships = new List <Relationship>();

            foreach (Model.Table table in tables)
            {
                foreach (ManyToOneRelationship manyToOneRelationship in table.ManyToOneRelationships)
                {
                    string path = manyToOneRelationship.ForeignScriptObject.Name + " -> " + GetColumnNameList(manyToOneRelationship.ForeignColumns) + " -> " + manyToOneRelationship.PrimaryScriptObject.Name + " -> " + GetColumnNameList(manyToOneRelationship.PrimaryColumns);

                    foreach (ManyToOneRelationship manyToOneRelationship2 in table.ManyToOneRelationships)
                    {
                        if (manyToOneRelationship2.Name == manyToOneRelationship.Name)
                        {
                            continue;
                        }
                        path += " -> " + manyToOneRelationship2.PrimaryScriptObject.Name + " -> " + GetColumnNameList(manyToOneRelationship2.PrimaryColumns) + " -> " + manyToOneRelationship2.ForeignScriptObject.Name + " -> " + GetColumnNameList(manyToOneRelationship2.ForeignColumns);
                        ManyToManyRelationship manyToManyRelationship = new ManyToManyRelationship("Many_" + manyToOneRelationship.Name + "_" + manyToOneRelationship2.Name,
                                                                                                   false, (OneToManyRelationship)manyToOneRelationship.ForeignRelationship, manyToOneRelationship2, manyToOneRelationship2.ForeignRelationship.Filter);

                        manyToOneRelationship.ForeignScriptObject.AddRelationship(manyToManyRelationship);
                        manyToManyRelationships.Add(manyToManyRelationship);
                    }
                }
            }
            #region Fill foreign relationships
            foreach (Model.Table table in tables)
            {
                foreach (ManyToManyRelationship manyToManyRelationship in table.ManyToManyRelationships)
                {
                    string foreignRelationshipName = "Many_" + manyToManyRelationship.IntermediatePrimaryRelationship.ForeignRelationship.Name + "_" + manyToManyRelationship.IntermediateForeignRelationship.Name;

                    ManyToManyRelationship foreignRelationship = (ManyToManyRelationship)Search.GetRelationship(manyToManyRelationships.ToArray(), foreignRelationshipName);
                    manyToManyRelationship.ForeignRelationship = foreignRelationship;
                }
            }
            #endregion
        }
Example #13
0
        private void FormRelationship_Load(object sender, EventArgs e)
        {
            foreach (ScriptObject scriptObject in _scriptObjects)
            {
                comboBoxPrimaryScriptObject.Items.Add(scriptObject);
            }
            comboBoxPrimaryScriptObject.DisplayMember = "Alias";
            comboBoxPrimaryScriptObject.ValueMember   = "Name";

            foreach (OneToManyRelationship oneToManyRelationship in _parent.OneToManyRelationships)
            {
                comboBoxIntermediatePrimaryRelationship.Items.Add(oneToManyRelationship);
            }
            comboBoxIntermediatePrimaryRelationship.DisplayMember = "Alias";
            comboBoxIntermediatePrimaryRelationship.ValueMember   = "Name";

            foreach (ScriptObject scriptObject in _scriptObjects)
            {
                comboBoxForeignScriptObject.Items.Add(scriptObject);
            }
            comboBoxForeignScriptObject.DisplayMember = "Alias";
            comboBoxForeignScriptObject.ValueMember   = "Name";

            groupBoxType.Enabled = false;

            if (_primaryRelationship == null)
            {
                this.Text = "Add New Relationship";
                comboBoxPrimaryScriptObject.SelectedItem = _parent;

                checkBoxIsBase.Enabled = true;
            }
            else
            {
                this.Text = "Edit Relationship " + _primaryRelationship.Name;

                textBoxName.Text  = _primaryRelationship.Name;
                textBoxAlias.Text = _primaryRelationship.Alias;
                textBoxPath.Text  = _primaryRelationship.Path;

                comboBoxPrimaryScriptObject.SelectedItem = _parent;
                foreach (Column column in _primaryRelationship.PrimaryColumns)
                {
                    ListViewItem item = new ListViewItem(column.Alias);
                    item.SubItems.Add(column.Alias);
                    item.Tag = column;

                    listViewPrimaryColumn.Items.Add(item);
                }

                comboBoxForeignScriptObject.SelectedItem = _primaryRelationship.ForeignScriptObject;
                foreach (Column column in _primaryRelationship.ForeignColumns)
                {
                    ListViewItem item = new ListViewItem(column.Alias);
                    item.SubItems.Add(column.Alias);
                    item.Tag = column;

                    listViewForeignColumn.Items.Add(item);
                }

                if (_primaryRelationship.GetType() == typeof(ManyToManyRelationship))
                {
                    ManyToManyRelationship manyToManyRelationship = (ManyToManyRelationship)_primaryRelationship;

                    comboBoxIntermediatePrimaryRelationship.SelectedItem = manyToManyRelationship.IntermediatePrimaryRelationship;
                    comboBoxIntermediateForeignRelationship.SelectedItem = manyToManyRelationship.IntermediateForeignRelationship;

                    tabStripRelationship.Pages.RemoveAt(0);
                    tabStripRelationship.Pages.RemoveAt(1);
                }
                else
                {
                    tabStripRelationship.Pages.RemoveAt(1);

                    comboBoxPrimaryFilter.SelectedItem = _primaryRelationship.Filter;
                    comboBoxForeignFilter.SelectedItem = _primaryRelationship.ForeignRelationship.Filter;
                }

                if (_primaryRelationship.GetType() == typeof(OneToOneRelationship))
                {
                    OneToOneRelationship relationship = (OneToOneRelationship)_primaryRelationship;

                    checkBoxIsBase.Checked      = relationship.IsBase;
                    checkBoxIsBase.Visible      = true;
                    radioButtonOneToOne.Checked = true;
                }
                else if (_primaryRelationship.GetType() == typeof(OneToManyRelationship))
                {
                    OneToManyRelationship relationship = (OneToManyRelationship)_primaryRelationship;

                    radioButtonOneToMany.Checked = true;
                }
                else if (_primaryRelationship.GetType() == typeof(ManyToOneRelationship))
                {
                    radioButtonManyToOne.Checked = true;
                }
                else if (_primaryRelationship.GetType() == typeof(ManyToManyRelationship))
                {
                    ManyToManyRelationship relationship = (ManyToManyRelationship)_primaryRelationship;

                    radioButtonManyToMany.Checked = true;
                }

                if (!_primaryRelationship.IsUserDefined)
                {
                    textBoxName.ReadOnly = true;

                    comboBoxPrimaryColumn.Enabled       = false;
                    comboBoxPrimaryScriptObject.Enabled = false;
                    buttonPrimaryColumnAdd.Enabled      = false;
                    listViewPrimaryColumn.Enabled       = false;
                    comboBoxPrimaryFilter.Enabled       = false;

                    comboBoxIntermediatePrimaryRelationship.Enabled = false;
                    comboBoxIntermediateForeignRelationship.Enabled = false;

                    comboBoxForeignColumn.Enabled       = false;
                    comboBoxForeignScriptObject.Enabled = false;
                    buttonForeignColumnAdd.Enabled      = false;
                    listViewForeignColumn.Enabled       = false;
                    comboBoxForeignFilter.Enabled       = false;

                    buttonAddPrimaryFilter.Enabled = false;
                    buttonAddIntermediatePrimaryRelationship.Enabled = false;
                    buttonAddIntermediateForeignRelationship.Enabled = false;
                    buttonAddForeignFilter.Enabled = false;
                }
            }

            listViewPrimaryColumn_Resize(null, null);
            listViewForeignColumn_Resize(null, null);
        }
Example #14
0
        internal protected void Split(IEnumerable <T> children, XElement childEntitySchema, ManyToManyRelationship manyToManyRelationship, Dictionary <string, object> parentPropertyValues, string childrenPath)
        {
            string childEntity = childEntitySchema.Attribute(SchemaVocab.Name).Value;

            XElement mmKeySchema = TransKeySchema(manyToManyRelationship, out XElement mmEntitySchema);
            string   mmEntity    = mmEntitySchema.Attribute(SchemaVocab.Name).Value;

            int index = 0;

            foreach (T child in children)
            {
                Dictionary <string, object> childPropertyValues = GetPropertyValues(child, childEntitySchema);
                Dictionary <string, object> mmPropertyValues    = TransPropertyValues(manyToManyRelationship, parentPropertyValues, childPropertyValues);

                InsertCommand <T> mmExecuteCommand = CreateInsertCommand(child, childEntity);
                mmExecuteCommand.EntitySchema         = mmEntitySchema;
                mmExecuteCommand.UniqueKeySchema      = mmKeySchema;
                mmExecuteCommand.ParentPropertyValues = parentPropertyValues;
                mmExecuteCommand.ParentRelationship   = manyToManyRelationship.DirectRelationships[0];
                mmExecuteCommand.Path           = string.Format("{0}[{1}]", childrenPath, index);
                mmExecuteCommand.PropertyValues = mmPropertyValues;

                SetDefaultValues(mmExecuteCommand);

                Commands.Add(mmExecuteCommand);

                index++;
            }
        }
        protected IEnumerable <ExecuteCommand <T> > GetDeleteAggregationCommands(IEnumerable <T> children, XElement childEntitySchema, ManyToManyRelationship manyToManyRelationship,
                                                                                 Dictionary <string, object> parentPropertyValues, string childrenPath, string parentEntity, XElement schema)
        {
            DeleteAggregation <T> deleteAggregation = CreateDeleteAggregation(default(T), parentEntity, schema);

            deleteAggregation.Split(children, childEntitySchema, manyToManyRelationship, parentPropertyValues, childrenPath);

            // reverse back
            return(deleteAggregation.ExecuteCommands.Reverse());
        }
Example #16
0
        public void FillTableSchema(Table tbl, TableType tableType)
        {
            TableColumnCollection columns = new TableColumnCollection();
            DataSet dsColumns;

            tbl.ForeignKeys = new ForeignKeyCollection();

            if (tableType == TableType.Function)
            {
                if (dsColumns2.Tables[Name] == null)
                {
                    var cmdColumns = new SqlCommand(ROUTINE_COLUMN_SQL_ALL);
                    var dt = new DataTable(Name);
                    dt.Load(cmdColumns.GetReader());
                    dsColumns2.Tables.Add(dt);
                }
                dsColumns = dsColumns2;
            }
            else
            {
                if (dsColumns1.Tables[Name] == null)
                {
                    var cmdColumns = new SqlCommand(TABLE_COLUMN_SQL_ALL);
                    var dt = new DataTable(Name);
                    dt.Load(cmdColumns.GetReader());
                    dsColumns1.Tables.Add(dt);
                }
                dsColumns = dsColumns1;
            }

            DataRow[] drColumns = dsColumns.Tables[Name].Select("TableName ='" + tbl.Name + "'", "OrdinalPosition ASC");

            for (int i = 0; i < drColumns.Length; i++)
            {
                TableColumn column = new TableColumn(tbl);
                column.ColumnName = drColumns[i][SqlSchemaVariable.COLUMN_NAME].ToString();
                column.NativeDataType = drColumns[i][SqlSchemaVariable.DATA_TYPE].ToString();
                if (column.NativeDataType == "numeric")
                    column.NativeDataType = string.Format("Decimal({0},{1})", drColumns[i]["precision"], drColumns[i]["scale"]);
                column.DataType = GetDbType(column.NativeDataType);
                if (drColumns[i][SqlSchemaVariable.COLUMN_DEFAULT] != DBNull.Value)
                {
                    string defaultSetting = drColumns[i][SqlSchemaVariable.COLUMN_DEFAULT].ToString().Trim();
                    if (defaultSetting.ToLower().IndexOf("newsequentialid()") > -1)
                        column.DefaultSetting = SqlSchemaVariable.DEFAULT;
                    else
                        column.DefaultSetting = defaultSetting;
                }
                column.AutoIncrement = Convert.ToBoolean(drColumns[i][SqlSchemaVariable.IS_IDENTITY]);
                int maxLength;
                int.TryParse(drColumns[i][SqlSchemaVariable.MAX_LENGTH].ToString(), out maxLength);
                column.MaxLength = maxLength;
                if (maxLength > 0)
                    column.NativeDataType += "(" + maxLength + ")";
                column.IsNullable = drColumns[i][SqlSchemaVariable.IS_NULLABLE].ToString() == "YES";
                bool isComputed = (drColumns[i][SqlSchemaVariable.IS_COMPUTED].ToString() == "1");
                column.IsReadOnly = (column.NativeDataType == "timestamp" || isComputed);
                columns.Add(column);
                tbl.SchemaName = drColumns[i]["Owner"].ToString();
            }
            tbl.Columns = columns;

            if (dsIndex.Tables[Name] == null)
            {
                var cmdIndex = new SqlCommand(INDEX_SQL_ALL);
                DataTable dt = new DataTable(Name);
                dt.Load(cmdIndex.GetReader());
                dsIndex.Tables.Add(dt);
            }

            DataRow[] drIndexes = dsIndex.Tables[Name].Select("TableName ='" + tbl.Name + "'");
            for (int i = 0; i < drIndexes.Length; i++)
            {
                string colName = drIndexes[i][SqlSchemaVariable.COLUMN_NAME].ToString();
                string constraintType = drIndexes[i][SqlSchemaVariable.CONSTRAINT_TYPE].ToString();
                TableColumn column = columns.GetColumn(colName);

                if (Utility.IsMatch(constraintType, SqlSchemaVariable.PRIMARY_KEY))
                    column.IsPrimaryKey = true;
                else if (Utility.IsMatch(constraintType, SqlSchemaVariable.FOREIGN_KEY))
                    column.IsForeignKey = true;
                //HACK: Allow second pass naming adjust based on whether a column is keyed
                column.ColumnName = column.ColumnName;
            }

            if (dtFKR == null)
            {
                var cmdFKR = new SqlCommand(FOREIGN_KEY_RELATIONSHIPS);
                dtFKR = new DataTable();
                dtFKR.Load(cmdFKR.GetReader());

                var cmdFK = new SqlCommand(FOREIGN_KEYS);
                dtFK = new DataTable();
                dtFK.Load(cmdFK.GetReader());
            }

            DataRow[] drfkr;
            drfkr = dtFKR.Select("PkTable ='" + tbl.Name + "'");
            for (int i = 0; i < drfkr.Length; i++)
            {
                Relationship rel = new Relationship();
                rel.Name = drfkr[i]["RelName"].ToString();
                Table fktable = Db.Service.GetSchema(drfkr[i]["FkTable"].ToString());
                rel.ClassNameOne = tbl.ClassName;
                rel.ClassNameMany = fktable.ClassName;
                string[] a = Regex.Split(rel.Name, "__");
                if (a.Length == 2)
                {
                    rel.PropertyNameMany = a[0]; // name used in primary table to fetch many of this table
                    rel.PropertyNameOne = a[1]; // named used for foreign key in this table
                }
                else
                {
                    rel.PropertyNameOne = tbl.ClassName;
                    rel.PropertyNameMany = fktable.ClassNamePlural;
                }

                DataRow[] drfk = dtFK.Select("PkTable = '" + tbl.Name + "' and RelName = '" + rel.Name + "'");
                for (int ii = 0; ii < drfk.Length; ii++)
                {
                    Relationship.KeyPair kp = new Relationship.KeyPair();
                    TableColumn fkcol = fktable.Columns.GetColumn(drfk[ii]["FkColumn"].ToString());
                    TableColumn pkcol = columns.GetColumn(drfk[ii]["PkColumn"].ToString());
                    kp.vartype = fkcol.VarType;
                    kp.ForeignKey = fkcol.Name;
                    kp.PrimaryKey = pkcol.Name;
                    rel.KeyPairs.Add(kp);
                    if (ii > 0)
                    {
                        rel.ForeignKey += ",";
                        rel.PrimaryKey += ",";
                    }
                    rel.PrimaryKey += kp.PrimaryKey;
                    rel.ForeignKey += kp.ForeignKey;
                }


                tbl.ForeignKeyTables.Add(rel);
            }

            drfkr = dtFKR.Select("FkTable ='" + tbl.Name + "'");
            for (int i = 0; i < drfkr.Length; i++)
            {
                Relationship rel = new Relationship();
                rel.Name = drfkr[i]["RelName"].ToString();
                Table pktable = Db.Service.GetSchema(drfkr[i]["PkTable"].ToString());
                rel.ClassNameOne = pktable.ClassName;
                rel.ClassNameMany = tbl.ClassName;
                string[] a = Regex.Split(rel.Name, "__");
                if (a.Length == 2)
                {
                    rel.PropertyNameMany = a[0]; // name used in primary table to fetch many of this table
                    rel.PropertyNameOne = a[1]; // named used for foreign key in this table
                }
                else
                {
                    rel.PropertyNameOne = pktable.ClassName;
                    rel.PropertyNameMany = tbl.ClassNamePlural;
                }

                DataRow[] drfk = dtFK.Select("FkTable = '" + tbl.Name + "' and RelName = '" + rel.Name + "'");
                for (int ii = 0; ii < drfk.Length; ii++)
                {
                    Relationship.KeyPair kp = new Relationship.KeyPair();
                    TableColumn fkcol = columns.GetColumn(drfk[ii]["FkColumn"].ToString());
                    TableColumn pkcol = pktable.Columns.GetColumn(drfk[ii]["PkColumn"].ToString());
                    kp.vartype = fkcol.VarType;
                    kp.ForeignKey = fkcol.Name;
                    kp.PrimaryKey = pkcol.Name;
                    rel.KeyPairs.Add(kp);
                    if (ii > 0)
                    {
                        rel.ForeignKey += ",";
                        rel.PrimaryKey += ",";
                    }
                    rel.PrimaryKey += kp.PrimaryKey;
                    rel.ForeignKey += kp.ForeignKey;
                    fkcol.PropertyNameOne = rel.PropertyNameOne;
                }

                tbl.ForeignKeys.Add(rel);
            }

            if (dsManyToManyCheck.Tables[Name] == null)
            {
                var cmdM2M = new SqlCommand(MANY_TO_MANY_CHECK_ALL);
                var dt = new DataTable(Name);
                dt.Load(cmdM2M.GetReader());
                dsManyToManyCheck.Tables.Add(dt);
            }

            DataRow[] drs = dsManyToManyCheck.Tables[Name].Select("PK_Table = '" + tbl.Name + "'");
            if (drs.Length > 0)
            {
                for (int count = 0; count < drs.Length; count++)
                {
                    string mapTable = drs[count]["FK_Table"].ToString();
                    string localKey = drs[count]["FK_Column"].ToString();
                    if (dsManyToManyMap.Tables[Name] == null)
                    {
                        var cmdM2MMap = new SqlCommand(MANY_TO_MANY_FOREIGN_MAP_ALL);
                        DataTable dt = new DataTable(Name);
                        dt.Load(cmdM2MMap.GetReader());
                        dsManyToManyMap.Tables.Add(dt);
                    }


                    DataRow[] drMap = dsManyToManyMap.Tables[Name].Select("FK_Table = '" + mapTable + "' AND PK_Table <> '" + tbl.Name + "'");

                    for (int i = 0; i < drMap.Length; i++)
                    {
                        ManyToManyRelationship m = new ManyToManyRelationship(mapTable);
                        m.ForeignTableName = drMap[i]["PK_Table"].ToString();
                        m.ForeignPrimaryKey = drMap[i]["PK_Column"].ToString();
                        m.MapTableLocalTableKeyColumn = localKey;
                        m.MapTableForeignTableKeyColumn = drMap[i]["FK_Column"].ToString();
                        tbl.ManyToManys.Add(m);
                    }
                }
            }
        }
Example #17
0
        internal protected void Split(IEnumerable <T> children, XElement childEntitySchema, ManyToManyRelationship manyToManyRelationship, Dictionary <string, object> parentPropertyValues, string childrenPath)
        {
            string childEntity = childEntitySchema.Attribute(SchemaVocab.Name).Value;

            XElement mmKeySchema         = TransKeySchema(manyToManyRelationship, out XElement mmEntitySchema);
            string   mmEntity            = mmEntitySchema.Attribute(SchemaVocab.Name).Value;
            XElement mmConcurrencySchema = GetConcurrencySchema(mmEntitySchema);

            //
            bool mmDeleteTransSetNull = IsDeleteTransSetNull(manyToManyRelationship);

            Dictionary <string, object>      mmUpdatePropertyValues = null;
            IEnumerable <DirectRelationship> mmRelationships        = null;

            if (mmDeleteTransSetNull)
            {
                mmUpdatePropertyValues = new Dictionary <string, object>();
                DirectRelationship oneToManyRelationship = manyToManyRelationship.DirectRelationships[0];
                for (int i = 0; i < oneToManyRelationship.RelatedProperties.Length; i++)
                {
                    string relatedPropertyName = oneToManyRelationship.RelatedProperties[i];
                    mmUpdatePropertyValues.Add(relatedPropertyName, null);
                }
            }
            else
            {
                mmRelationships = GetDirectRelationships(mmEntity);
            }

            int index = 0;

            foreach (T child in children)
            {
                Dictionary <string, object> childPropertyValues = GetPropertyValues(child, childEntitySchema);
                Dictionary <string, object> mmPropertyValues    = TransPropertyValues(manyToManyRelationship, parentPropertyValues, childPropertyValues);

                ExecuteCommand <T> mmExecuteCommand;
                if (mmDeleteTransSetNull)
                {
                    UpdateCommand <T> mmUpdateCommand = CreateUpdateCommand(child, childEntity);
                    mmUpdateCommand.FixedUpdatePropertyValues = mmUpdatePropertyValues;
                    mmUpdateCommand.ConcurrencySchema         = mmConcurrencySchema;

                    mmExecuteCommand = mmUpdateCommand;
                }
                else
                {
                    DeleteCommand <T> mmDeleteCommand = CreateDeleteCommand(child, childEntity);
                    mmDeleteCommand.ConcurrencySchema  = mmConcurrencySchema;
                    mmDeleteCommand.ChildRelationships = mmRelationships;

                    mmExecuteCommand = mmDeleteCommand;
                }

                mmExecuteCommand.EntitySchema         = mmEntitySchema;
                mmExecuteCommand.UniqueKeySchema      = mmKeySchema;
                mmExecuteCommand.PropertyValues       = mmPropertyValues;
                mmExecuteCommand.ParentPropertyValues = parentPropertyValues;
                mmExecuteCommand.ParentRelationship   = manyToManyRelationship.DirectRelationships[0];
                mmExecuteCommand.Path = string.Format("{0}[{1}]", childrenPath, index);

                Commands.Add(mmExecuteCommand);

                index++;
            }
        }