public void Can_Add_Reference()
        {
            var parser = new HeaderParser(".\\Test.dacpac");
            string fileName = string.Format("c:\\bloonblah{0}.dacpac", Guid.NewGuid().ToString().Replace("{", "").Replace("}", "").Replace("-", ""));
            const string logicalName = "blooblah.dacpac";
            const string externalParts = "[$(blooblah)]";
            const string suppressMissingDependenciesErrors = "False";

            var newCustomData = new CustomData("Reference", "SqlSchema");
            newCustomData.AddMetadata("FileName", fileName);
            newCustomData.AddMetadata("LogicalName", logicalName);
            newCustomData.AddMetadata("ExternalParts", externalParts);
            newCustomData.AddMetadata("SupressMissingDependenciesErrors", suppressMissingDependenciesErrors);

            var writer = new HeaderWriter(".\\Test.dacpac", new DacHacFactory());
            writer.AddCustomData(newCustomData);
            writer.Close();

            var actualItem = parser.GetCustomData()
                .Where(
                    p =>
                        p.Category == "Reference" && p.Type == "SqlSchema" &&
                        p.Items.Any(item => item.Name == "FileName" && item.Value == fileName));

            Assert.IsNotNull(actualItem);
        }
        public void existing_metadata_items_are_overwritten()
        {
            const string expected = "111";

            var cd = new CustomData("a", "b");
            cd.AddMetadata("d", "e");
            cd.AddMetadata("d", expected);

            Assert.AreEqual(1, cd.Items.Count);
            Assert.AreEqual(expected, cd.Items.First().Value);
        }
Пример #3
0
        public void DeleteCustomData(CustomData data)
        {
            var reader = XmlReader.Create(new StringReader(_xml));
            var element = XElement.Load(reader);
            var namespaceManager = new XmlNamespaceManager(reader.NameTable);
            namespaceManager.AddNamespace("p", "http://schemas.microsoft.com/sqlserver/dac/Serialization/2012/02");

            var nodes =
                element.XPathSelectElements(
                    string.Format("//p:Header/p:CustomData[@Category='{0}' and @Type='{1}']", data.Category, data.Type),
                    namespaceManager);
            XElement nodeToRemove = null;
            foreach (var node in nodes)
            {
                var isMatch = true;

                foreach (var item in data.Items)
                {
                    var child =
                        node.XPathSelectElement(
                            string.Format("//p:Metadata[@Name='{0}' and @Value='{1}']", item.Name, item.Value),
                            namespaceManager);
                    if (null == child)
                    {
                        isMatch = false;
                        break;
                    }
                }

                if (isMatch)
                {
                    nodeToRemove = node;
                    break;
                }
            }

            if (nodeToRemove != null)
                nodeToRemove.Remove();

            _xml = element.ToString(SaveOptions.OmitDuplicateNamespaces);

            if (_autoCommitEveryOperation)
                CommitChanges();
        }
Пример #4
0
        public List<CustomData> GetCustomData()
        {
            var dac = new DacHacXml(_dacPacPath);
            var xml = dac.GetXml("Model.xml");

            var reader = XmlReader.Create(new StringReader(xml));
            reader.MoveToContent();

            var data = new List<CustomData>();
            CustomData currentCustomData = null;

            while (reader.Read())
            {
                if (reader.Name == "CustomData" && reader.NodeType == XmlNodeType.Element)
                {
                    var cat = reader.GetAttribute("Category");
                    var type = reader.GetAttribute("Type");

                    currentCustomData = new CustomData(cat, type);
                    data.Add(currentCustomData);
                }

                if (reader.Name == "Metadata" && reader.NodeType == XmlNodeType.Element)
                {
                    var name = reader.GetAttribute("Name");
                    var value = reader.GetAttribute("Value");

                    currentCustomData.AddMetadata(name, value);
                }

                if (reader.Name == "Header" && reader.NodeType == XmlNodeType.EndElement)
                {
                    break; //gone too far
                }
            }
            dac.Close();

            return data;
        }
Пример #5
0
 public void AddCustomData(CustomData data)
 {
     AddCustomData(data.Category, data.Type, data.Items);
     EnsureSqlVarsWritten(data);
 }
Пример #6
0
 public void AddCustomData(CustomData data)
 {
     AddCustomData(data.Category, data.Type, data.Items);
     EnsureSqlVarsWritten(data);
 }
Пример #7
0
        private void EnsureSqlVarsWritten(CustomData data)
        {
            if (data.RequiredSqlCmdVars.Count == 0)
                return;

            var haveChanges = false;
            XElement rootNode;
            var customData = GetOrAddCustomDataNode("SqlCmdVariables", "SqlCmdVariable", out rootNode);
            var reader = XmlReader.Create(new StringReader(_xml));
            var namespaceManager = new XmlNamespaceManager(reader.NameTable);
            var ns = XNamespace.Get("http://schemas.microsoft.com/sqlserver/dac/Serialization/2012/02");
            namespaceManager.AddNamespace("p", "http://schemas.microsoft.com/sqlserver/dac/Serialization/2012/02");

            foreach (var cmdVar in data.RequiredSqlCmdVars)
            {
                if (
                    customData.XPathSelectElement(string.Format("/p:MetaData[@Name='{0}']", cmdVar), namespaceManager) !=
                    null)
                {
                    continue;
                }

                var newMetaNode = new XElement(ns + "Metadata");
                newMetaNode.SetAttributeValue("Name", cmdVar);
                newMetaNode.SetAttributeValue("Value", "");
                customData.Add(newMetaNode);
                haveChanges = true;
            }

            if (haveChanges)
            {
                _xml = rootNode.ToString(SaveOptions.None);

                if (_autoCommitEveryOperation)
                    CommitChanges();
            }
        }