﻿using System.Collections.Generic;
using Codeo.Data.Generator.Data.Entity;
using Codeo.Data.Generator.Data.Provider.Contract;
using Codeo.Data.Orm.Contract;
using Codeo.Data.Orm.Factory;
using Codeo.Data.Orm.Impl;
using Codeo.Log.Interceptor;
using Ninject;
using Codeo.Log.Contract.Service;

namespace Codeo.Data.Generator.Data.Provider.Impl
{
    public class SqlViewProvider : PersistenceProviderBase, IViewProvider
    {
        public SqlViewProvider()
        {
                
        }

        [Inject]
        public SqlViewProvider(IDataContext dataContext, IStatementBuilderFactory statementBuildFactory, ILogService logService) : base( dataContext, statementBuildFactory, logService)
        {

        }

        [Trace]
        public virtual IEnumerable<DataStructure> GetAll()
        {
            IStatementBuilder statement = _statementBuilderFactory.Build(QueryType.SelectMultipleRows);
            BuildGetViewsQuery(ref statement);
            IResult query = Execute(statement);
            IEnumerable<DataStructure> res = query.ToList<DataStructure>();
            return res;
        }

        private static void BuildGetViewsQuery(ref IStatementBuilder pStatement)
        {
            pStatement.Append("select obj.name as Name, 'View' as Type ");
            pStatement.Append("from sys.objects obj ");
            pStatement.Append("inner join sys.schemas sch on obj.schema_id = sch.schema_id ");
            pStatement.Append("where type = 'V' ");
            //pStatement.Append("and sch.name = 'dbo'");
        }
    }
}
