/// <summary> /// Creates a new TSqlModel, loads all *.sql files specified in the SqlPaths property /// into the new model, and then parses the model. /// </summary> /// <exception cref="System.InvalidOperationException">No path to *.sql files exists in SqlPaths properties.</exception> public void LoadModel() { if (!this.SqlPaths.Any(x => !string.IsNullOrWhiteSpace(x))) { throw new InvalidOperationException("No path to *.sql files exists in SqlPaths properties."); } var procFiles = new List <string>(); foreach (var sqlPath in this.SqlPaths) { if (sqlPath.EndsWith(".sql")) { procFiles.Add(sqlPath); } else { procFiles.AddRange(Directory.GetFiles(sqlPath, "*.sql", SearchOption.AllDirectories)); } } var model = new dac.TSqlModel(_sqlServerVersion, new dac.TSqlModelOptions()); foreach (var procFile in procFiles) { model.AddObjects(File.ReadAllText(procFile)); } LoadModel(model); }
/// <summary> /// Creates a new TSqlModel, loads each specified sql statement into the new model, /// and then parses the model /// </summary> /// <param name="sqlStatements">One or more sql statements to load, such as CREATE TABLE or CREATE PROCEDURE statements.</param> public void LoadModel(params string[] sqlStatements) { var model = new dac.TSqlModel(_sqlServerVersion, new dac.TSqlModelOptions()); foreach (var sqlStatement in sqlStatements) { model.AddObjects(sqlStatement); } LoadModel(model); }
/// <summary> /// Parses the specified TSqlModel /// </summary> /// <param name="model">The model.</param> public void LoadModel(dac.TSqlModel model) { var sqlTables = model.GetObjects(dac.DacQueryScopes.UserDefined, dac.Table.TypeClass); var sqlViews = model.GetObjects(dac.DacQueryScopes.UserDefined, dac.View.TypeClass); var primaryKeys = model.GetObjects(dac.DacQueryScopes.UserDefined, dac.PrimaryKeyConstraint.TypeClass).Select(o => o.GetReferenced().Where(r => r.ObjectType.Name == "Column")).SelectMany(c => c); var foreignKeyDictionaries = new[] { GetForeignKeys(sqlTables), GetForeignKeys(sqlViews) }; var foreignKeys = foreignKeyDictionaries .SelectMany(x => x) .ToDictionary(pair => pair.Key, pair => pair.Value); _tables = sqlTables.Select(sqlTable => new Table(sqlTable, primaryKeys, foreignKeys)).ToList(); _views = sqlViews.Select(sqlView => new View(sqlView, primaryKeys, foreignKeys)).ToList(); _procedures = model.GetObjects(dac.DacQueryScopes.UserDefined, dac.Procedure.TypeClass).Select(sqlProc => new Procedure(sqlProc, this.ProcedurePrefix, primaryKeys, foreignKeys)).ToList(); _modelLoaded = true; }
static void Main(string[] args) { ParsedArgs parsedArgs = new ParsedArgs(args); Microsoft.SqlServer.Dac.Model.TSqlModel sqlModel = new Microsoft.SqlServer.Dac.Model.TSqlModel(parsedArgs.DacPacFileName); //Where takes a predicate thing. No, I haven't figured out how to do that without lambda stuff yet. But this is tolerably readable for main control flow, I think. var tables = sqlModel.GetObjects(DacQueryScopes.Default, Table.TypeClass).ToList().Where(table => table.Name.ToString().EndsWith("_dimSrc_stg]")); var views = sqlModel.GetObjects(DacQueryScopes.Default, View.TypeClass).ToList().Where(view => view.Name.ToString().EndsWith("_dimSrc_stg]")); foreach (var table in tables) { ProcessTSqlObjectIntoDimensionScriptFiles(parsedArgs, table, Table.Columns, Table.Schema); } foreach (var view in views) { ProcessTSqlObjectIntoDimensionScriptFiles(parsedArgs, view, View.Columns, View.Schema); } Console.WriteLine("Press any key to close!"); Console.ReadLine(); }
/// <summary> /// Creates a new TSqlModel, loads each specified sql statement into the new model, /// and then parses the model /// </summary> /// <param name="sqlStatements">One or more sql statements to load, such as CREATE TABLE or CREATE PROCEDURE statements.</param> public void LoadModel(params string[] sqlStatements) { var model = new dac.TSqlModel(dac.SqlServerVersion.Sql100, new dac.TSqlModelOptions()); foreach (var sqlStatement in sqlStatements) { model.AddObjects(sqlStatement); } LoadModel(model); }
/// <summary> /// Creates a new TSqlModel, loads all *.sql files specified in the SqlPaths property /// into the new model, and then parses the model. /// </summary> /// <exception cref="System.InvalidOperationException">No path to *.sql files exists in SqlPaths properties.</exception> public void LoadModel() { if (!this.SqlPaths.Any(x => !string.IsNullOrWhiteSpace(x))) throw new InvalidOperationException("No path to *.sql files exists in SqlPaths properties."); var procFiles = new List<string>(); foreach (var sqlPath in this.SqlPaths) { if (sqlPath.EndsWith(".sql")) { procFiles.Add(sqlPath); } else { procFiles.AddRange(Directory.GetFiles(sqlPath, "*.sql", SearchOption.AllDirectories)); } } var model = new dac.TSqlModel(dac.SqlServerVersion.Sql100, new dac.TSqlModelOptions()); foreach (var procFile in procFiles) { model.AddObjects(File.ReadAllText(procFile)); } LoadModel(model); }