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);
        }
        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;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="IndexMethodBuilder" /> class.
 /// </summary>
 /// <param name="index">The index to build method for.</param>
 internal IndexMethodBuilder(IndexInfo index)
 {
     _index = index;
 }
        /// <summary>
        /// Gets the signature of the read method for the specified index.
        /// </summary>
        /// <param name="index">The index to get signature for.</param>
        internal static MethodSignature GetIndexSignature(IndexInfo index)
        {
            string returnType = index.Unique
                ? index.Table.EntityName
                : string.Format("IEnumerable<{0}>", index.Table.EntityName);

            string name = string.Format("Get{0}",
                                        index.Unique ? index.Table.EntityName : index.Table.ShortName);

            Dictionary<string, string> parameters = new Dictionary<string, string>(index.Columns.Length);
            foreach (var column in index.Columns)
            {
                string key = char.ToLowerInvariant(column.Name[0]) + column.Name.Substring(1);
                parameters.Add(key, column.ClrType);
            }

            return new MethodSignature(name, returnType, parameters);
        }
 /// <summary>
 /// Writes read interface methods definition for the specified index.
 /// </summary>
 /// <param name="index">The index to write interface for.</param>
 public void WriteIndexDataProviderInterface(IndexInfo index)
 {
     var signature = MethodSignature.GetIndexSignature(index);
     WriteLine(signature.ToString("i", null));
 }
 /// <summary>
 /// Writes read methods implementation for the specified index.
 /// </summary>
 /// <param name="index">The index to write methods for.</param>
 public void WriteIndexDataProviderImplementation(IndexInfo index)
 {
     var signature = MethodSignature.GetIndexSignature(index);
     IMethodBuilder builder = new IndexMethodBuilder(index);
     WrapMethodBody(signature, builder);
 }