//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// DataHelpers.SQL satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using u = user_rtx;

namespace DataHelpers.SQL {

/// <summary>
/// </summary>
[h_db_orm_type][u.async_local][u.sync_only_disposable]
public partial class HDbQuery: HQueryExecContextBase {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/// <exception cref="InvalidOperationException" />
public HDbQuery()
{}

//==================================================================================================

public static bool HasData =>
   HasCurrentInstance;
public static bool NoData =>
   !HasCurrentInstance;

//--------------------------------------------------------------------------------------------------

/// <exception cref="InvalidOperationException" />
/// <exception cref="InvalidCastException" />
/// <exception cref="HDatabaseException" />
[u.async_local][u.opt_result]
public static T GetFieldValue<T>( string fieldName, bool treatNullAsDefaultValue=false ) =>
   HasData ?
      HDB.GetFieldValue<T>( ProcedureName,
         DataReader, fieldName, FieldIndexer, treatNullAsDefaultValue )
      : default!;

/// <exception cref="InvalidOperationException" />
/// <exception cref="InvalidCastException" />
/// <exception cref="HDatabaseException" />
[u.async_local][u.opt_result][u.opt_through_minus_1]
public static int GetFieldIndex( string fieldName, bool throwIfNotFound=false ) =>
   HasData ?
      HDB.GetFieldIndex( ProcedureName, DataReader, fieldName, FieldIndexer, throwIfNotFound )
      : -1;

/// <exception cref="InvalidOperationException" />
/// <exception cref="InvalidCastException" />
/// <exception cref="HDatabaseException" />
[u.async_local]
public static bool HasColumn( string fieldName ) =>
   GetFieldIndex( fieldName ) >= 0;

//--------------------------------------------------------------------------------------------------

public static string NormalizeStrValue( string? str ) =>
   str!=null ? str.Trim() : "";

//==================================================================================================

/// <exception cref="HDatabaseException" />
[u.async_local]
public static List<RSet_Item> VerifyRSet<RSet_Item>( List<RSet_Item>? list ) =>
   list ?? throw new HDatabaseException(u.local.T(
      "Database query has not returned expected result set.",
      "Запрос к базе данных не вернул ожидаемый резалт-сет." ));
/// <exception cref="ArgumentOutOfRangeException" />
/// <exception cref="HDatabaseException" />
[u.async_local]
public static List<RSet_Item> VerifyRSet<RSet_Item>( List<RSet_Item>? list,
                                                     [u.positive] int resultSetNo )
{
   u.arg_check.Positive( resultSetNo, nameof(resultSetNo) );
   return list ?? throw HDBExceptionCreator.New(u.local.T(
      "Database query has not returned expected result set #{0}.",
      "Запрос к базе данных не вернул ожидаемый резалт-сет #{0}." ),
      resultSetNo );
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HDbQuery

} // DataHelpers.SQL
