//------------------------------------------------------------------------------
// <autogenerated>
//     This code was generated by a code generation tool.
//     Date generated: 14/05/2018 17:05:24
//
//     Changes to this file may cause incorrect behavior and will be lost if 
//     the code is regenerated.
// </autogenerated>
//------------------------------------------------------------------------------
using ConanData.Database;
using ConanData.Datasets;
using System;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Text;

namespace ConanData.DataAccess.Base
{
    /// <summary>
    /// Data access class to perform Fetches, Deletes, Inserts and Updates 
    /// on table characters
    /// </summary>
    internal class CharactersDataAccessBase
    {
        #region Constants

        internal const String SCHEMA__NAME = "";
        internal const String TABLE__NAME = "characters";

        internal const String COLUMN__PLAYERID = "playerId";
        internal const String COLUMN__ID = "id";
        internal const String COLUMN__CHARNAME = "char_name";
        internal const String COLUMN__LEVEL = "level";
        internal const String COLUMN__RANK = "rank";
        internal const String COLUMN__GUILD = "guild";
        internal const String COLUMN__ISALIVE = "isAlive";
        internal const String COLUMN__LASTTIMEONLINE = "lastTimeOnline";

        #endregion

        private Connection _connection = null;

        public CharactersDataAccessBase(Connection connection)
        {
            _connection = connection;
        }

        #region Properties

        public Connection Connection => _connection;

        #endregion

        #region Fetch Typed DataSets

        /// <summary>
        /// Fetches all the records into a typed dataset from the 
        /// table characters
        /// </summary>
        /// <returns>A populated typed dataset</returns>
        public CharactersDataSet FetchDataSetAll()
        {
            Connection connection = Connection;

            StringBuilder sql = new StringBuilder();
            sql.Append(GetSelect());

            CharactersDataSet dataSet = new CharactersDataSet();
            connection.FillDataSet(sql.ToString(), null, CommandType.Text, 300, dataSet, new String[] { TABLE__NAME });
            return dataSet;
        }

        #endregion

        #region Fetch DataTables

        /// <summary>
        /// Fetches all the records into an untyped datatable from the 
        /// table characters
        /// </summary>
        /// <returns>A populated untyped datatable</returns>
        public DataTable FetchDataTableAll()
        {
            Connection connection = Connection;

            StringBuilder sql = new StringBuilder();
            sql.Append(GetSelect());

            return connection.ExecuteDataTable(sql.ToString(), null, CommandType.Text, 300, TABLE__NAME);
        }

        #endregion

        #region Support Methods

        protected String GetSelect()
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("SELECT CAST([{0}].[{1}] AS BIGINT) AS [AccountId],", TABLE__NAME, COLUMN__PLAYERID);
            sql.AppendFormat("[{0}].[{1}] AS [CharacterId],", TABLE__NAME, COLUMN__ID);
            sql.AppendFormat("[{0}].[{1}] AS [CharacterName],", TABLE__NAME, COLUMN__CHARNAME);
            sql.AppendFormat("[{0}].[{1}] AS [Level],", TABLE__NAME, COLUMN__LEVEL);
            sql.AppendFormat("[{0}].[{1}] AS [Rank],", TABLE__NAME, COLUMN__RANK);
            sql.AppendFormat("[{0}].[{1}] AS [GuildId],", TABLE__NAME, COLUMN__GUILD);
            sql.AppendFormat("[{0}].[{1}] AS [IsAlive],", TABLE__NAME, COLUMN__ISALIVE);
            sql.AppendFormat("[{0}].[{1}] AS [LastTimeOnline] ", TABLE__NAME, COLUMN__LASTTIMEONLINE);
            sql.AppendFormat("FROM {0}[{1}] ", SCHEMA__NAME, TABLE__NAME);
            return sql.ToString();
        }

        #endregion
    }
}
