Beispiel #1
0
        public object Clone()
        {
            AlterTableSQLCommand cmd = new AlterTableSQLCommand(this.TagMapping, this.TableName, this.AlterType);
            cmd.ColumnDefinitions = this.ColumnDefinitions.Clone() as ArrayList;

            foreach (ForeignKey fk in ForeignKeys)
                cmd.ForeignKeys.Add(new ForeignKey(fk.Name, fk.ForeignKeys, fk.ParentTable, fk.ReferenceKeys));

            if (this.PrimaryKey != null)
                cmd.PrimaryKey = this.PrimaryKey.Clone() as PrimaryKey;

            return cmd;
        }
        public ArrayList CreateInitCommands()
        {
            _InitCommands = new ArrayList();

            ArrayList dropFkCommands = new ArrayList();
            ArrayList createFkCommands = new ArrayList();
            Hashtable commands = new Hashtable();

            foreach (EntityMapping entity in _Mapping.Entities)
            {
                string type = String.Empty;

                // if entity table exist
                //		true : return entity table
                //		false : return a new table
                if (!commands.Contains(entity.Table))
                    commands.Add(entity.Table, new CreateTableSQLCommand(entity, entity.Schema, entity.Table));
                CreateTableSQLCommand entityTab = (CreateTableSQLCommand)commands[entity.Table];


                // add a primary key into entity table
                foreach (PrimaryKeyMapping pkm in entity.Ids)
                {
                    AddPrimaryKeysTo(entityTab, entity.GetIdField(pkm), pkm.Generator);
                }

                // if the entity has a discriminator
                //		true : add a field into entity table
                if (entity.DiscriminatorField != null & entity.DiscriminatorValue != null)
                {
                    AddColumnTo(entityTab, entity.DiscriminatorField, DbType.AnsiString, 255, 0, 0, false, null);
                    //GeneratorMapping discriminatorGenerator=new GeneratorMapping();
                    //discriminatorGenerator.Name=GeneratorMapping.GeneratorType.business;
                    //AddPrimaryKeysTo(entityTab, entity.DiscriminatorField, discriminatorGenerator);
                }

                // add all fields
                if (entity.Attributes != null)
                {
                    foreach (AttributeMapping attribute in entity.Attributes)
                    {
                        // if the attribute has a same table to entity
                        //		true : add the field attribute into entity table
                        if (attribute.Table == entity.Table)
                        {
                            AddColumnTo(entityTab, attribute.Field, attribute.DbType, attribute.Size, attribute.Precision, attribute.Scale, attribute.IsNotNull, attribute.DefaultValue);
                        }


                            //		false : create an attribute table
                        else
                        {
                            // if attribute table exist
                            //		true : return attribute table
                            //		false : return a new table
                            if (!commands.Contains(attribute.Table))
                                commands.Add(attribute.Table, new CreateTableSQLCommand(attribute, attribute.Table));
                            CreateTableSQLCommand attributeTab = (CreateTableSQLCommand)commands[attribute.Table];

                            // if the attribute has a discriminator
                            //		true : add fields into attribute table
                            if (attribute.Discriminator != null && attribute.DiscriminatorValue != null)
                            {
                                AddColumnTo(attributeTab, attribute.Discriminator, DbType.AnsiString, 255, 0, 0, true, null);
                                //								type = _Dialect.ConvertDbTypeToString( DbType.AnsiString, 255, 0, 0);
                                //								AddColumnTo(attributeTab, "TYPE", type, true, null);
                            }

                            // add field attribute into attribute table
                            AddColumnTo(attributeTab, attribute.Field, attribute.DbType, attribute.Size, attribute.Precision, attribute.Scale, attribute.IsNotNull, attribute.DefaultValue);

                            // add field containing Id of the entity into attribute table
                            foreach (PrimaryKeyMapping pkm in entity.Ids)
                            {
                                AddForeignKeysTo(attributeTab, attribute.ParentField, entity.GetIdField(pkm), entity.Table, pkm.Generator, null);
                            }
                        }
                    }
                }

                // add all foreign keys
                if (entity.References != null)
                {
                    foreach (ReferenceMapping reference in entity.References)
                    {
                        if (reference.Rules != null)
                        {
                            for (int index = 0; index < reference.Rules.Count; index++)
                            {
                                RuleMapping rule = reference.Rules[index];

                                if (!commands.Contains(rule.ParentTable))
                                    commands.Add(rule.ParentTable, new CreateTableSQLCommand(rule, rule.ParentTable));

                                CreateTableSQLCommand parentTable = (CreateTableSQLCommand)commands[rule.ParentTable];

                                if (!commands.Contains(rule.ChildTable))
                                    commands.Add(rule.ChildTable, new CreateTableSQLCommand(rule, rule.ChildTable));

                                CreateTableSQLCommand childTable = (CreateTableSQLCommand)commands[rule.ChildTable];

                                if (reference.DiscriminatorField != null && index == 0)
                                {
                                    AddColumnTo(childTable, reference.DiscriminatorField, DbType.AnsiString, 36, 0, 0, false, null);
                                }

                                // Creates constraints if specified and handled by the dialect
                                if (_Dialect.GetDisableForeignKeyScope() != DBDialect.ForeignKeyScope.None && rule.Constraint != null && rule.Constraint != String.Empty)
                                {
                                    AlterTableSQLCommand alter = null;

                                    // Detects the parent of the constraints using the position of he PK
                                    if ((index == 0 && rule.ParentField == reference.EntityParent.IdFields) || (index != 0 && rule.ParentField == reference.Rules[index - 1].ChildField))
                                    {
                                        alter = new AlterTableSQLCommand(null, rule.ChildTable, AlterTypeEnum.Add);
                                        alter.ForeignKeys.Add(new ForeignKey(rule.Constraint, new string[] { rule.ChildField }, rule.ParentTable, new string[] { rule.ParentField }));
                                    }
                                    else
                                    {
                                        alter = new AlterTableSQLCommand(null, rule.ParentTable, AlterTypeEnum.Add);
                                        alter.ForeignKeys.Add(new ForeignKey(rule.Constraint, new string[] { rule.ParentField }, rule.ChildTable, new string[] { rule.ChildField }));
                                    }

                                    createFkCommands.Add(alter);

                                    // Generates the DROP command for this constraint
                                    AlterTableSQLCommand drop = alter.Clone() as AlterTableSQLCommand;
                                    drop.AlterType = AlterTypeEnum.Drop;

                                    dropFkCommands.Add(drop);
                                }


                                GeneratorMapping generator = null;

                                for (int i = 0; i < rule.ParentFields.Length; i++)
                                {
                                    string parentField = rule.ParentFields[i];
                                    string childField = rule.ChildFields[i];

                                    // create a parentColumn
                                    if (reference.EntityParent.Table == rule.ParentTable && reference.EntityParent.Ids[parentField] != null)
                                    {
                                        generator = reference.EntityParent.Ids[parentField].Generator;
                                        AddPrimaryKeysTo(parentTable, parentField, generator);
                                        if (!string.IsNullOrEmpty(rule.ChildDefaultValue))
                                            AddForeignKeysTo(childTable, childField, parentField, rule.ParentTable, generator, rule.ChildDefaultValues[i]);
                                        else
                                            AddForeignKeysTo(childTable, childField, parentField, rule.ParentTable, generator, null);

                                    }


                                    // create a childColumn
                                    if (_Mapping.Entities[reference.EntityChild].Table == childTable.TableName
                                        && _Mapping.Entities[reference.EntityChild].Ids[childField] != null)
                                    {
                                        generator = _Mapping.Entities[reference.EntityChild].Ids[childField].Generator;
                                        AddPrimaryKeysTo(childTable, childField, generator);
                                        if (!string.IsNullOrEmpty(rule.ParentDefaultValue))
                                            AddForeignKeysTo(parentTable, parentField, childField, rule.ChildTable, generator, rule.ParentDefaultValues[i]);
                                        else
                                            AddForeignKeysTo(parentTable, parentField, childField, rule.ChildTable, generator, null);

                                    }

                                }
                            }
                        }
                    }
                }
            }

            _InitCommands.AddRange(dropFkCommands);
            _InitCommands.AddRange(commands.Values);
            _InitCommands.AddRange(createFkCommands);

            return _InitCommands;
        }
Beispiel #3
0
        public void Visit(AlterTableSQLCommand command)
        {
            _Query.Append(ALTER).Append(TABLE).Append(FormatAttribute(command.TableName)).Append(SPACE);
            

            switch (command.AlterType)
            {
                case AlterTypeEnum.AlterColumn:
                    throw new NotImplementedException();

                case AlterTypeEnum.Add:
                    _Query.Append(SPACE).Append(ADD);
                    break;

                case AlterTypeEnum.Drop:
                    _Query.Append(SPACE).Append(DROP);
                    break;
            }

            ArrayList alterList = new ArrayList();

            if(command.PrimaryKey != null)
            {
                string[] pkFields = new string[command.PrimaryKey.Columns.Count];
                for (int i = 0; i < pkFields.Length; i++)
                    pkFields[i] = FormatAttribute(((ColumnDefinition)command.PrimaryKey.Columns[i]).ColumnName);

                string constr = String.Concat(SPACE, CONSTRAINT, FormatAttribute(command.PrimaryKey.Name), SPACE);

                if (command.AlterType == AlterTypeEnum.Add)
                {
                    constr += String.Concat(SPACE, PRIMARY, KEY, CLUSTERED, OPENBRACE, string.Join(COMMA, pkFields), CLOSEBRACE);
                }
                

                alterList.Add(constr);
            }

            foreach (ForeignKey fk in command.ForeignKeys)
            {
                string constr = CONSTRAINT;
                
                if(command.AlterType == AlterTypeEnum.Add)
                {
                    string[] foreignFields = (string[])fk.ForeignKeys.Clone();
                    string[] referenceFields = (string[])fk.ReferenceKeys.Clone();

                    for(int i=0; i<foreignFields.Length; i++)
                    {
                        foreignFields[i] = FormatAttribute(foreignFields[i]);
                        referenceFields[i] = FormatAttribute(referenceFields[i]);
                    }

                    // " {0} FOREIGN KEY ( {1} ) REFERENCES {2} ( {3} )"
                    constr += string.Concat(SPACE,
                        FormatAttribute(fk.Name),
                        SPACE,
                        FOREIGN, KEY, OPENBRACE, SPACE,
                        string.Join(COMMA, foreignFields), SPACE,
                        CLOSEBRACE, REFERENCES, FormatAttribute(fk.ParentTable), SPACE,
                        OPENBRACE, SPACE, string.Join(COMMA, referenceFields), SPACE, CLOSEBRACE);
                }
                else if(command.AlterType == AlterTypeEnum.Drop)
                {
                    constr += FormatAttribute(fk.Name);
                }
                else
                    throw new ArgumentException("ForeignKeys");

                alterList.Add(constr);
            }

            _Query.Append(String.Join(COMMA, (string[])alterList.ToArray(typeof(string))));
        }