//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// CommonHelpers satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace CommonHelpers {

//**************************************************************************************************
[HDbOrmType][HAsyncStatic][HSyncOnlyDisposable]
public partial class HDbQuery: HQueryExecContextBase {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

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

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

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

/// <exception cref="ArgumentNullException" />
/// <exception cref="InvalidOperationException" />
/// <exception cref="InvalidCastException" />
/// <exception cref="HDatabaseException" />
[HAsyncStatic][HOptionalResult]
public static T GetFieldValue<T>(string fieldName,bool treatNullAsDefaultValue=false)
{
   HArgChecking.VerifyNotNull( fieldName );
   return HasData ?
      HDB.GetFieldValue<T>( ProcedureName,
         DataReader, fieldName, FieldIndexer, treatNullAsDefaultValue )
      : default(T)!;
}
/// <exception cref="ArgumentNullException" />
/// <exception cref="InvalidOperationException" />
/// <exception cref="InvalidCastException" />
/// <exception cref="HDatabaseException" />
[HAsyncStatic][HOptionalResult][HOptionalThroughMinusOne]
public static int GetFieldIndex(string fieldName,bool throwIfNotFound=false)
{
   HArgChecking.VerifyNotNull( fieldName );
   return HasData ?
      HDB.GetFieldIndex( ProcedureName, DataReader, fieldName, FieldIndexer, throwIfNotFound )
      : -1;
}
/// <exception cref="ArgumentNullException" />
/// <exception cref="InvalidOperationException" />
/// <exception cref="InvalidCastException" />
/// <exception cref="HDatabaseException" />
[HAsyncStatic]
public static bool HasColumn(string fieldName) =>
   GetFieldIndex( fieldName ) >= 0;

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

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

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

/// <exception cref="HDatabaseException" />
[HAsyncStatic]
public static List<RSet_Item> VerifyRSet<RSet_Item>(List<RSet_Item>? list) =>
   list ?? throw new HDatabaseException(HLocal.TEXT(
      "Database query has not returned expected result set.",
      "Запрос к базе данных не вернул ожидаемый резалт-сет." ));
/// <exception cref="HDatabaseException" />
[HAsyncStatic]
public static List<RSet_Item> VerifyRSet<RSet_Item>( List<RSet_Item>? list,
                                                     int resultSetNo ) =>
   list ?? throw new HDatabaseException(HString.FormatAsText(HLocal.TEXT(
      "Database query has not returned expected result set #{0}.",
      "Запрос к базе данных не вернул ожидаемый резалт-сет #{0}." ),
      resultSetNo ));

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

} // CommonHelpers
