Example #1
0
        public int AddForeignKey(
            Table masterTable,
            Table slaveTable)
        {
            if (masterTable is null)
            {
                throw new ArgumentNullException(nameof(masterTable));
            }
            if (slaveTable is null)
            {
                throw new ArgumentNullException(nameof(slaveTable));
            }

            void GetUniqueForeignKeyName(out string localForegnKeyName)
            {
                localForegnKeyName = $"{masterTable.Name}Id";

                int uniqueForeignKeyEnding = 1;

                while (
                    !_attributeValidator.IsUniqueName(table: slaveTable, attributeName: localForegnKeyName) &&
                    uniqueForeignKeyEnding <= MaxSlaveTableLinks)
                {
                    localForegnKeyName = $"{localForegnKeyName}{uniqueForeignKeyEnding++}";
                }

                if (uniqueForeignKeyEnding > MaxSlaveTableLinks)
                {
                    throw new ArgumentException($"The amount of links ({MaxSlaveTableLinks}) is over.");
                }
            }

            GetUniqueForeignKeyName(out string foregnKeyName);

            ForeignKey foreignKey = new ForeignKey(
                name: foregnKeyName,
                isNullable: true)
            {
                TableId = slaveTable.Id
            };

            _attributeRepository.Add(entity: foreignKey);

            return(foreignKey.Id);
        }
Example #2
0
        public void RenameAttribute(Attribute attribute, string name)
        {
            if (attribute is null)
            {
                throw new ArgumentNullException(nameof(attribute));
            }
            if (name is null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (attribute.Name == name)
            {
                return;
            }

            if (!_attributeValidator.IsValidName(name: name))
            {
                throw new ArgumentException($"Invalid primary key name {name}.");
            }

            Table table = _tableService.GetTableById(tableId: attribute.TableId);

            if (!_attributeValidator.IsUniqueName(table: table, attributeName: name))
            {
                throw new InvalidOperationException($"The attribute {name} is already exists.");
            }

            Database database = _databaseService.GetById(id: table.DatabaseId);

            if (_deployService.IsDeployed(database: database))
            {
                _deployService.RenameDeployedAttribute(attribute: attribute, validAttributeName: name);
            }

            _attributeService.Rename(attribute: attribute, name: name);
        }