//===============================================================================
// Microsoft patterns & practices
// Web Service Software Factory
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

#region Using Statements
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Howest.SchemaDiscovery.DbObjects;
using Howest.SchemaDiscovery.DbObjects.SqlServer;
using Howest.SchemaDiscovery.Translators.SqlServer.Helpers;
using Howest.SchemaDiscovery.Translators.SqlServer.Data;
#endregion

namespace Howest.SchemaDiscovery.Translators.SqlServer
{
    internal static class SqlServerViewTranslator
    {
        public static SqlServerView SqlServerViewCollectionToView(DataRow sqlServerView, DataTable sqlServerColumnsColletions)
        {
            SqlServerView view = new SqlServerView();

            view.Name = sqlServerView["table_name"].ToString();
            view.Schema = sqlServerView["table_schema"].ToString();
			view.Fullname = view.Schema + "." + view.Name;

            foreach(DataRow row in sqlServerColumnsColletions.Rows)
            {
                SqlServerColumn column = new SqlServerColumn(view);
                SqlServerDbDataType dbDataType = new SqlServerDbDataType();

                column.Name = row["ColumnName"].ToString();
                column.IsNullable = (bool)row["AllowDBNull"];

                dbDataType.ProviderType = SqlServerDataTypeConverter.SqlDbType2DatabaseType(row["ProviderType"]);
                dbDataType.Type = (Type)row["DataType"];
                dbDataType.Size = (int)row["ColumnSize"];
                dbDataType.Precision = (Int16)row["NumericPrecision"];
                dbDataType.Scale = (Int16)row["NumericScale"];

                column.DbDataType = dbDataType;
                view.Columns.Add(column);
            }

            return view;
        } 
    }
}