//===============================================================================
// 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.Data;
using Howest.SchemaDiscovery.Translators.SqlServer.Helpers;
#endregion

namespace Howest.SchemaDiscovery.Translators.SqlServer
{
    internal static class SqlServerStoredProcedureTranslator
    {
        public static SqlServerStoredProcedure SqlServerStoredProcedureCollectionToStoreProcedure(DataRow sqlServerStoredProcedure, DataTable sqlServerParametersColletions)
        {
            SqlServerStoredProcedure storedProcedure = new SqlServerStoredProcedure();
            storedProcedure.Name = sqlServerStoredProcedure["routine_name"].ToString();

            foreach(DataRow row in sqlServerParametersColletions.Rows)
            {
                SqlServerParameter parameter = new SqlServerParameter();
                parameter.DBName = row["parameter_name"].ToString();
                parameter.Direction = SqlServerTranslatorHelper.GetDirection(row["parameter_mode"].ToString());

                SqlServerDbDataType dbDataType = new SqlServerDbDataType();
                dbDataType.ProviderType = SqlServerDataTypeConverter.String2DatabaseType(row["data_type"].ToString());
                dbDataType.Type = SqlServerDataTypeConverter.DatabaseType2NetType(dbDataType.ProviderType);
                dbDataType.Precision = SqlServerTranslatorHelper.GetPrecision(row["numeric_precision"]);
                dbDataType.Scale = SqlServerTranslatorHelper.GetScale(row["numeric_scale"]);
                dbDataType.Size = SqlServerTranslatorHelper.GetParameterSize(dbDataType.ProviderType, row);
                parameter.DbDataType = dbDataType;

                storedProcedure.Parameters.Add(parameter);
            }

            return storedProcedure;
        } 
    }
}