/**
 * @file    CSQLiteException.h
 * @ingroup CSQLite
 * @brief   Encapsulation of the error message from SQLite3 on a std::runtime_error.
 */
#pragma once

#include <stdexcept>
#include <string>

#include "sqlite3.h"
#include "GdCPP_Exports.h"

/**
 * @brief Encapsulation of the error message from SQLite3, based on std::runtime_error.
 */
class CSQLiteException : public std::runtime_error
{
public:
    /**
     * @brief Encapsulation of the error message from SQLite3, based on std::runtime_error.
     *
     * @param[in] aErrorMessage The string message describing the SQLite error
     */
    explicit CSQLiteException(const char* aErrorMessage):
        std::runtime_error(aErrorMessage),
        mErrcode(-1), // 0 would be SQLITE_OK, which doesn't make sense
        mExtendedErrcode(-1)
    {
    }
    explicit CSQLiteException(const std::string& aErrorMessage):
        std::runtime_error(aErrorMessage),
        mErrcode(-1), // 0 would be SQLITE_OK, which doesn't make sense
        mExtendedErrcode(-1)
    {
    }

    /**
     * @brief Encapsulation of the error message from SQLite3, based on std::runtime_error.
     *
     * @param[in] aErrorMessage The string message describing the SQLite error
     * @param[in] ret           Return value from function call that failed.
     */
    CSQLiteException(const char* aErrorMessage, int ret):
        std::runtime_error(aErrorMessage),
        mErrcode(ret),
        mExtendedErrcode(-1)
    {
    }
    CSQLiteException(const std::string& aErrorMessage, int ret):
        std::runtime_error(aErrorMessage),
        mErrcode(ret),
        mExtendedErrcode(-1)
    {
    }
   /**
     * @brief Encapsulation of the error message from SQLite3, based on std::runtime_error.
     *
     * @param[in] apSQLite The SQLite object, to obtain detailed error messages from.
     */
    explicit CSQLiteException(sqlite3* apSQLite):
        std::runtime_error(sqlite3_errmsg(apSQLite)),
        mErrcode(sqlite3_errcode(apSQLite)),
        mExtendedErrcode(sqlite3_extended_errcode(apSQLite))
    {
    }
    /**
     * @brief Encapsulation of the error message from SQLite3, based on std::runtime_error.
     *
     * @param[in] apSQLite  The SQLite object, to obtain detailed error messages from.
     * @param[in] ret       Return value from function call that failed.
     */
    CSQLiteException(sqlite3* apSQLite, int ret):
        std::runtime_error(sqlite3_errmsg(apSQLite)),
        mErrcode(ret),
        mExtendedErrcode(sqlite3_extended_errcode(apSQLite))
    {
    }

    /// Return the result code (if any, otherwise -1).
    inline int getErrorCode() const noexcept // nothrow
    {
        return mErrcode;
    }

    /// Return the extended numeric result code (if any, otherwise -1).
    inline int getExtendedErrorCode() const noexcept // nothrow
    {
        return mExtendedErrcode;
    }

    /// Return a string, solely based on the error code
    inline const char* getErrorStr() const noexcept
    {
        return sqlite3_errstr(mErrcode);
    }

private:
    int mErrcode;         ///< Error code value
    int mExtendedErrcode; ///< Detailed error code if any
};

