/// <summary>
        /// Initializes host with database objects from the specified database scripts.
        /// </summary>
        /// <param name="sqlScripts">Collection of database script names to parse.</param>
        public void Initialize(IList<string> sqlScripts)
        {
            using (TSqlModel model = new TSqlModel(SqlServerVersion.Sql120, new TSqlModelOptions()))
            {
                foreach (string script in sqlScripts)
                {
                    model.AddObjects(File.ReadAllText(script));
                }

                _schema = DatabaseSchema.FromModel(model);
            }
        }
        /// <summary>
        /// Initializes host with database package on the specified path.
        /// </summary>
        /// <param name="databasePackagePath">Absolute path to the database package.</param>
        public void Initialize(string databasePackagePath)
        {
            var options = new ModelLoadOptions();
            options.LoadAsScriptBackedModel = true;
            options.ModelStorageType = DacSchemaModelStorageType.File;
            TSqlModel model = TSqlModel.LoadFromDacpac(databasePackagePath, options);

            using (model)
            {
                _schema = DatabaseSchema.FromModel(model);
            }
        }
Exemplo n.º 3
0
        internal static DatabaseSchema FromModel(TSqlModel model)
        {
            DatabaseSchema retVal = new DatabaseSchema();
            var tableModels = model.GetObjects(DacQueryScopes.All, ModelSchema.Table).ToArray();
            retVal.Tables = new TableInfo[tableModels.Length];
            for (int i = 0; i < tableModels.Length; i++)
                retVal.Tables[i] = GetSchemaForTable(tableModels[i]);

            var viewModels = model.GetObjects(DacQueryScopes.All, ModelSchema.View).ToArray();
            retVal.Views = new ViewInfo[viewModels.Length];
            for (int i = 0; i < viewModels.Length; i++)
                retVal.Views[i] = GetSchemaForView(viewModels[i]);

            var keys = model.GetObjects(DacQueryScopes.All, ModelSchema.PrimaryKeyConstraint).ToArray();
            UpdatePrimaryKeysInformation(retVal.Tables, keys);

            var indexes = model.GetObjects(DacQueryScopes.All, ModelSchema.Index).ToArray();
            retVal.Indexes = new IndexInfo[indexes.Length];
            for (int i = 0; i < indexes.Length; i++)
            {
                var index = indexes[i];
                string tableName = index.Name.Parts[1];
                TableInfo targetTable = retVal.Tables.FirstOrDefault(t => t.ShortName == tableName);
                if (null == targetTable)
                {
                    throw new InvalidOperationException(
                        "Could not find target table for index " + index.Name);
                }

                IndexInfo info = new IndexInfo();
                info.Table = targetTable;
                info.Unique = index.GetProperty<bool>(Index.Unique);

                var columns = index.GetReferenced(Index.Columns).ToArray();
                info.Columns = new ColumnInfo[columns.Length];
                for (int j = 0; j < info.Columns.Length; j++)
                {
                    info.Columns[j] = GetSchemaForColumn(columns[j]);
                }

                retVal.Indexes[i] = info;
            }

            return retVal;
        }