context: // ReSharper disable All
  using System.Collections.Generic;
  using System.Data;
  using System.Dynamic;
  using System.Linq;
  using MixERP.Net.DbFactory;
  using MixERP.Net.EntityParser;
  using MixERP.Net.Framework;
  using MixERP.Net.Framework.Extensions;
  using Npgsql;
  using PetaPoco;
  using Serilog;
  
  namespace MixERP.Net.Schemas.Core.Data
  {
      /// <summary>
      /// Provides simplified data access features to perform SCRUD operation on the database table "core.nationalities".
      /// </summary>
      public class Nationality : DbAccess, INationalityRepository
      {
          /// <summary>
          /// The schema of this table. Returns literal "core".
          /// </summary>
          public override string _ObjectNamespace => "core";
  
          /// <summary>
          /// The schema unqualified name of this table. Returns literal "nationalities".
          /// </summary>
          public override string _ObjectName => "nationalities";
  
          /// <summary>
          /// Login id of application user accessing this table.
          /// </summary>
          public long _LoginId { get; set; }
  
          /// <summary>
          /// User id of application user accessing this table.
          /// </summary>
          public int _UserId { get; set; }
  
          /// <summary>
          /// The name of the database on which queries are being executed to.
          /// </summary>
          public string _Catalog { get; set; }
  
          /// <summary>
          /// Performs SQL count on the table "core.nationalities".
          /// </summary>
          /// <returns>Returns the number of rows of the table "core.nationalities".</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
          public long Count()
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return 0;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to count entity \"Nationality\" was denied to the user with Login ID {LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT COUNT(*) FROM core.nationalities;";
              return Factory.Scalar<long>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Executes a select query on the table "core.nationalities" to return all instances of the "Nationality" class. 
          /// </summary>
          /// <returns>Returns a non-live, non-mapped instances of "Nationality" class.</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
          public IEnumerable<MixERP.Net.Entities.Core.Nationality> GetAll()
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.ExportData, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to the export entity \"Nationality\" was denied to the user with Login ID {LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM core.nationalities ORDER BY nationality_code;";
              return Factory.Get<MixERP.Net.Entities.Core.Nationality>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Executes a select query on the table "core.nationalities" to return all instances of the "Nationality" class to export. 
          /// </summary>
          /// <returns>Returns a non-live, non-mapped instances of "Nationality" class.</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
          public IEnumerable<dynamic> Export()
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.ExportData, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to the export entity \"Nationality\" was denied to the user with Login ID {LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM core.nationalities ORDER BY nationality_code;";
              return Factory.Get<dynamic>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Executes a select query on the table "core.nationalities" with a where filter on the column "nationality_code" to return a single instance of the "Nationality" class. 
          /// </summary>
          /// <param name="nationalityCode">The column "nationality_code" parameter used on where filter.</param>
          /// <returns>Returns a non-live, non-mapped instance of "Nationality" class mapped to the database row.</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
          public MixERP.Net.Entities.Core.Nationality Get(string nationalityCode)
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to the get entity \"Nationality\" filtered by \"NationalityCode\" with value {NationalityCode} was denied to the user with Login ID {_LoginId}", nationalityCode, this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM core.nationalities WHERE nationality_code=@0;";
              return Factory.Get<MixERP.Net.Entities.Core.Nationality>(this._Catalog, sql, nationalityCode).FirstOrDefault();
          }
  
          /// <summary>
          /// Gets the first record of the table "core.nationalities". 
          /// </summary>
          /// <returns>Returns a non-live, non-mapped instance of "Nationality" class mapped to the database row.</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
          public MixERP.Net.Entities.Core.Nationality GetFirst()
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to the get the first record of entity \"Nationality\" was denied to the user with Login ID {_LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM core.nationalities ORDER BY nationality_code LIMIT 1;";
              return Factory.Get<MixERP.Net.Entities.Core.Nationality>(this._Catalog, sql).FirstOrDefault();
          }
  
          /// <summary>
          /// Gets the previous record of the table "core.nationalities" sorted by nationalityCode.
          /// </summary>
          /// <param name="nationalityCode">The column "nationality_code" parameter used to find the next record.</param>
          /// <returns>Returns a non-live, non-mapped instance of "Nationality" class mapped to the database row.</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
          public MixERP.Net.Entities.Core.Nationality GetPrevious(string nationalityCode)
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to the get the previous entity of \"Nationality\" by \"NationalityCode\" with value {NationalityCode} was denied to the user with Login ID {_LoginId}", nationalityCode, this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM core.nationalities WHERE nationality_code < @0 ORDER BY nationality_code DESC LIMIT 1;";
              return Factory.Get<MixERP.Net.Entities.Core.Nationality>(this._Catalog, sql, nationalityCode).FirstOrDefault();
          }
  
          /// <summary>
          /// Gets the next record of the table "core.nationalities" sorted by nationalityCode.
          /// </summary>
          /// <param name="nationalityCode">The column "nationality_code" parameter used to find the next record.</param>
          /// <returns>Returns a non-live, non-mapped instance of "Nationality" class mapped to the database row.</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
          public MixERP.Net.Entities.Core.Nationality GetNext(string nationalityCode)
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to the get the next entity of \"Nationality\" by \"NationalityCode\" with value {NationalityCode} was denied to the user with Login ID {_LoginId}", nationalityCode, this._LoginId);
  
gt:                     throw new UnauthorizedException("Access is denied.");