public void AddFile(FileInfo file)
        {
            var name        = file.FullName;
            var lastChanged = file.LastWriteTimeUtc;

            if (_files.ContainsKey(name))
            {   //do we need to clear out some old ones??
                var existingScripts = _files[name];
                foreach (var f in existingScripts)
                {
                    IList <ParseError> err;
                    var parsed = _parser.Parse(new StringReader("--"), out err);
                    _model.AddOrUpdateObjects(parsed as TSqlScript, f, new TSqlObjectOptions());
                }
            }

            var scripts = GetScripts(name);

            _caches[name] = lastChanged;
            var scriptList = new List <string>();

            foreach (var script in scripts)
            {
                IList <ParseError> err;
                var parsed = _parser.Parse(new StringReader(script.Content), out err);
                _model.AddOrUpdateObjects(parsed as TSqlScript, script.Path, new TSqlObjectOptions());
                scriptList.Add(script.Path);
            }

            _files[name] = scriptList;
        }
Exemple #2
0
 private static void AddScriptsToModel(TSqlModel model, IEnumerable <Tuple <string, string> > scriptsWithNamedSources)
 {
     foreach (var scriptWithName in scriptsWithNamedSources)
     {
         model.AddOrUpdateObjects(scriptWithName.Item1, scriptWithName.Item2, null);
     }
 }
Exemple #3
0
 protected void AddScriptsToModel(TSqlModel model)
 {
     foreach (Tuple <string, string> tuple in this.TestScripts)
     {
         // Item1 = script, Item2 = (logicl) source file name
         model.AddOrUpdateObjects(tuple.Item1, tuple.Item2, new TSqlObjectOptions());
     }
 }
Exemple #4
0
        private TSqlModel BuildInitialEmptyModel()
        {
            var dummyModel = new TSqlModel(_version, _options);

            dummyModel.AddOrUpdateObjects("create procedure a as select 1", "dummy", new TSqlObjectOptions());
            dummyModel.DeleteObjects("dummy");
            return(dummyModel);
        }
        public void Merge()
        {
            var pre  = new StringBuilder();
            var post = new StringBuilder();

            foreach (var source in sources)
            {
                if (!File.Exists(source))
                {
                    continue;
                }

                using (var model = GetModel(source))
                {
                    foreach (var obj in model.GetObjects(DacQueryScopes.UserDefined))
                    {
                        TSqlScript ast;
                        if (obj.TryGetAst(out ast))
                        {
                            var name = obj.Name.ToString();
                            var info = obj.GetSourceInformation();
                            if (info != null && !string.IsNullOrWhiteSpace(info.SourceName))
                            {
                                name = info.SourceName;
                            }

                            if (!string.IsNullOrWhiteSpace(name) && !name.EndsWith(".xsd", StringComparison.OrdinalIgnoreCase))
                            {
                                target.AddOrUpdateObjects(ast, name, new TSqlObjectOptions()); // WARNING throwing away ansi nulls and quoted identifiers!
                            }
                        }
                    }
                }

                using (var package = DacPackage.Load(source))
                {
                    if (!(package.PreDeploymentScript is null))
                    {
                        using (var stream = new StreamReader(package.PreDeploymentScript))
                        {
                            pre.Append(stream.ReadToEnd());
                        }
                    }

                    if (!(package.PostDeploymentScript is null))
                    {
                        using (var stream = new StreamReader(package.PostDeploymentScript))
                        {
                            post.Append(stream.ReadToEnd());
                        }
                    }
                }
            }

            WriteFinalDacpac(target, pre.ToString(), post.ToString());
        }
Exemple #6
0
        public void Merge()
        {
            var pre  = String.Empty;
            var post = String.Empty;

            foreach (var source in _sources)
            {
                var model = getModel(source);
                foreach (var obj in model.GetObjects(DacQueryScopes.UserDefined))
                {
                    TSqlScript ast;
                    if (obj.TryGetAst(out ast))
                    {
                        var name = obj.Name.ToString();
                        var info = obj.GetSourceInformation();
                        if (info != null && !string.IsNullOrWhiteSpace(info.SourceName))
                        {
                            name = info.SourceName;
                        }

                        if (!string.IsNullOrWhiteSpace(name) && !name.EndsWith(".xsd"))
                        {
                            _target.AddOrUpdateObjects(ast, name, new TSqlObjectOptions());    //WARNING throwing away ansi nulls and quoted identifiers!
                        }
                    }
                }

                using (var package = DacPackage.Load(source))
                {
                    if (!(package.PreDeploymentScript is null))
                    {
                        pre += new StreamReader(package.PreDeploymentScript).ReadToEnd();
                    }
                    if (!(package.PostDeploymentScript is null))
                    {
                        post += new StreamReader(package.PostDeploymentScript).ReadToEnd();
                    }
                }
            }

            WriteFinalDacpac(_target, pre, post);
        }
Exemple #7
0
        public static void ValidateQuery(string query, SqlTestDB db, bool expectQueryToPass)
        {
            Console.WriteLine("Testing query '{0}'", query);
            Console.WriteLine("Loading model from DB");
            using (TSqlModel model = TSqlModel.LoadFromDatabase(db.BuildConnectionString()))
            {
                // Currently we just give a message but no source information. Will work around this for now by ensuring the model is fully valid before proceeding
                bool modelInitiallyValid = PrintModelState(model, true);
                if (!modelInitiallyValid)
                {
                    Console.WriteLine("Quitting due to invalid model");
                    return;
                }
                AssertModelHasNProcedures(model, 0);

                string myFileName = "QueryFile.sql";

                // validate bad query fails validation
                model.AddOrUpdateObjects(CreateQueryAsProc(query), myFileName, null);
                PrintModelState(model, expectQueryToPass);

                AssertModelHasNProcedures(model, 1);
            }
        }
Exemple #8
0
 //
 // Summary:
 //     Adds or updates the objects defined for a specified sourceName with the objects
 //     defined in the inputScript. If any objects were previously added with the
 //     same sourceName these will be completely replaced The object definitions
 //     are based on the contents of a TSql Script string plus additional metadata
 //     defined by a Microsoft.SqlServer.Dac.Model.TSqlObjectOptions object The script
 //     should consist of valid TSql DDL statements.
 //
 // Parameters:
 //   inputScript:
 //     Script containing TSql DDL statements
 //
 //   sourceName:
 //     A name to identify the inputScript, for example a fileName such as "MyTable.sql"
 //     or simply an alias like "dbo.Table". Scripts are cached and TSqlObjects are
 //     linked to the source name.  Any future Update/Delete operations will remove
 //     all existing objects with the same script name and replace them with the
 //     new objects.
 //
 //   options:
 //     Options defining how to interpret the script
 //
 // Exceptions:
 //   System.ArgumentNullException:
 //     If the supplied inputScript is null.
 //
 //   System.ArgumentException:
 //     If the supplied sourceName is null or whitespace, or if it ends in ".xsd".
 //     Note: source names ending in ".xsd" are currently not supported. These relate
 //     to Xml Schema Collections and adding of these is currently not supported
 //     in the public API.
 //
 //   System.Runtime.Remoting.RemotingException:
 //     If communication with the Microsoft.SqlServer.Dac.Model.TSqlObjectService
 //     fails.
 public void AddOrUpdateObjects(string inputScript, string sourceName, TSqlObjectOptions options)
 {
     model.AddOrUpdateObjects(inputScript, sourceName, options);
 }
Exemple #9
0
        public void Merge()
        {
            var pre  = String.Empty;
            var post = String.Empty;

            foreach (var source in _sources)
            {
                if (!File.Exists(source))
                {
                    Console.WriteLine("File {0} does not exist and is being skipped.", source);
                    continue;
                }

                Console.WriteLine("{0} : Processing dacpac {1}", DateTimeOffset.Now.ToString("o", System.Globalization.CultureInfo.InvariantCulture), source);


                if (source == _sources.First())
                {
                    Console.WriteLine("{0}: Copying dacpac options from {1} to {2}", DateTimeOffset.Now.ToString("o", System.Globalization.CultureInfo.InvariantCulture), source, _targetPath);

                    _first = new TSqlModel(_sources.First());
                    var options = _first.CopyModelOptions();
                    _target = new TSqlModel(_first.Version, options);
                }

                var model = getModel(source);

                foreach (var obj in model.GetObjects(DacQueryScopes.UserDefined))
                {
                    TSqlScript ast;
                    if (obj.TryGetAst(out ast))
                    {
                        var name = obj.Name.ToString();
                        var info = obj.GetSourceInformation();
                        if (info != null && !string.IsNullOrWhiteSpace(info.SourceName))
                        {
                            name = info.SourceName;
                        }

                        if (!string.IsNullOrWhiteSpace(name) && !name.EndsWith(".xsd"))
                        {
                            _target.AddOrUpdateObjects(ast, name, new TSqlObjectOptions());    //WARNING throwing away ansi nulls and quoted identifiers!
                        }
                    }
                }

                AddGlobalCustomData(new HeaderParser(source).GetCustomData()
                                    .Where(x => x.Type != "Assembly").ToList());

                using (var package = DacPackage.Load(source))
                {
                    if (!(package.PreDeploymentScript is null))
                    {
                        pre += new StreamReader(package.PreDeploymentScript).ReadToEnd();
                    }
                    if (!(package.PostDeploymentScript is null))
                    {
                        post += new StreamReader(package.PostDeploymentScript).ReadToEnd();
                    }
                }
            }

            WriteFinalDacpac(_target, pre, post);
        }
Exemple #10
0
 private TSqlModel BuildInitialEmptyModel()
 {
     var dummyModel = new TSqlModel(_version, _options);
     dummyModel.AddOrUpdateObjects("create procedure a as select 1", "dummy", new TSqlObjectOptions());
     dummyModel.DeleteObjects("dummy");
     return dummyModel;
 }
Exemple #11
0
        public void Merge()
        {
            var options = new TSqlObjectOptions();

            //var pre = String.Empty;
            //var post = String.Empty;
            //Dictionary<string, Reference> externalReferences = new Dictionary<string, Reference>();
            //Dictionary<string, SqlCmdVar> sqlVariables = new Dictionary<string, SqlCmdVar>();

            foreach (var source in _sources)
            {
                TSqlModel model = new TSqlModel(source);

                /*
                 * var customDatas = model.GetCustomData();
                 * foreach (CustomData customData in customDatas)
                 * {
                 *      if (customData.Category == "Reference" && customData.DataType == "SqlSchema")
                 *      {
                 *              var reference = new Reference(customData);
                 *              if (!reference.IsSameDatabaseReference && !externalReferences.ContainsKey(reference.LogicalName))
                 *              {
                 *                      externalReferences.Add(reference.LogicalName, reference);
                 *              }
                 *              Console.WriteLine("DacPac Reference: {0} / {1} / {2} / {3}",
                 *                      reference.Path,
                 *                      reference.LogicalName,
                 *                      reference.ExternalParts,
                 *                      reference.SuppressMissingDependenciesErrors);
                 *      }
                 *      else if (customData.Category == "SqlCmdVariables")
                 *      {
                 *              var sqlVars = SqlCmdVar.ParseCustomData(customData);
                 *              foreach (SqlCmdVar sqlVar in sqlVars)
                 *              {
                 *                      if (!sqlVariables.ContainsKey(sqlVar.Name))
                 *                      {
                 *                              sqlVariables.Add(sqlVar.Name, sqlVar);
                 *                      }
                 *                      Console.WriteLine("DacPac SQL Variable: {0} / {1}",
                 *                              sqlVar.Name,
                 *                              sqlVar.Value);
                 *              }
                 *      }
                 * }
                 */

                foreach (TSqlObject obj in model.GetObjects(DacQueryScopes.UserDefined))
                {
                    if (obj.TryGetAst(out TSqlScript ast))
                    //if (obj.TryGetScript(out string script))
                    {
                        var name = obj.Name.ToString();
                        SourceInformation info = obj.GetSourceInformation();
                        if (info != null && !string.IsNullOrWhiteSpace(info.SourceName))
                        {
                            name = info.SourceName;
                        }

                        if (!string.IsNullOrWhiteSpace(name) && !name.EndsWith(".xsd"))
                        {
                            _targetModel.AddOrUpdateObjects(ast, name, options);
                            //_targetModel.AddObjects(ast);
                        }
                    }
                }

                //using (var package = DacPackage.Load(source))
                //{
                //	pre += new StreamReader(package.PreDeploymentScript).ReadToEnd();
                //	post += new StreamReader(package.PostDeploymentScript).ReadToEnd();
                //}
            }

            //Console.WriteLine("Start Compile...");
            //foreach (Reference reference in externalReferences.Values)
            //{
            //	_target.AddReference(
            //		reference.Path,
            //		reference.LogicalName,
            //		reference.ExternalParts,
            //		reference.SuppressMissingDependenciesErrors);
            //}
            //_target.AddSqlVariables(sqlVariables.Values.ToList());
            WriteFinalDacpac(_targetModel /*, pre, post*/);
        }