#pragma once
//////////////////////////////////////////////////////////////////////
// Copyright (c) 2010, Oliver 'kfs1' Smith <oliver@kfs.org>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// - Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// - Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// - Neither the name of KingFisher Software nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////
//

#if defined(DBA_ENABLE_MYSQL)

#ifndef _DBA_HAVE_DATABASES
# define _DBA_HAVE_DATABASES
#endif

#include <mysql.h>
#include <stdlib.h>

// Define your preferred MySQL connection flags.
#ifndef DBA_DEFAULT_FLAGS_MYSQL
# define DBA_DEFAULT_FLAGS_MYSQL (CLIENT_COMPRESS | CLIENT_MULTI_STATEMENTS | CLIENT_MULTI_RESULTS)
#endif

namespace DBA
{

    class MySQLConnection : public Connection
    {
    public:
        MySQLConnection() ;
        virtual ~MySQLConnection() ;

        // Define default connection flags.
        static unsigned int DefaultFlags() { return (DBA_DEFAULT_FLAGS_MYSQL) ; }

        virtual void Connect(const Credentials&, unsigned int flags=DefaultFlags(), bool autoReconnect=false) ;
        virtual void Disconnect() ;
        virtual bool IsConnected() const { return m_conn != NULL ; }

        virtual bool HasAccurateRowCount() const { return true ; }

        virtual void EscapeString(const char* src, char* dest, size_t destSize) ;

        virtual bool FetchRow() ;

        virtual bool NextResultSet() ;

        virtual const char* GetError()
        {
            return mysql_error(m_conn) ;
        }

    protected:
        // Execute SQL, does not try to retrieve results
        virtual void _execute(size_t stlen, const char* statement) ;
        // Retrieve/process results from execution.
        virtual void _processResultSet() ;
        // Cleanup the result set.
        virtual void _releaseResultSet() ;

        virtual const char* GetColumn(unsigned int index) const
        {
            if ( index >= m_cols )
                throw std::invalid_argument("Index beyond end of row") ;
            return m_columns[index] ;
        }

        virtual Connection& operator >> (const void*& into)
        {
            into = m_columns[NextColumn()] ;
            return *this ;
        }

        virtual Connection& operator >> (const char*& into)
        {
            into = m_columns[NextColumn()] ;
            return *this ;
        }

        virtual Connection& operator >> (const unsigned char*& into)
        {
            into = (const unsigned char*)m_columns[NextColumn()] ;
            return *this ;
        }

        signed long long GetNextSigned()
        {
            return strtoll(GetNextString(), NULL, 10) ;
        }
        unsigned long long GetNextUnsigned()
        {
            return strtoull(GetNextString(), NULL, 10) ;
        }

        virtual Connection& operator >> (char& into)
        {
            into = (char)GetNextSigned() ;
            return *this ;
        }
        virtual Connection& operator >> (unsigned char& into)
        {
            into = (unsigned char)GetNextUnsigned() ;
            return *this ;
        }

        virtual Connection& operator >> (short& into)
        {
            into = (short)GetNextSigned() ;
            return *this ;
        }
        virtual Connection& operator >> (unsigned short& into)
        {
            into = (unsigned short)GetNextUnsigned() ;
            return *this ;
        }

        virtual Connection& operator >> (int& into)
        {
            into = (int)GetNextSigned() ;
            return *this ;
        }
        virtual Connection& operator >> (unsigned int& into)
        {
            into = (int)GetNextUnsigned() ;
            return *this ;
        }

        virtual Connection& operator >> (long long& into)
        {
            into = GetNextSigned() ;
            return *this ;
        }
        virtual Connection& operator >> (unsigned long long& into)
        {
            into = GetNextUnsigned() ;
            return *this ;
        }

        virtual Connection& operator >> (float& into)
        {
            into = strtof(GetNextString(), NULL) ;
            return *this ;
        }
        virtual Connection& operator >> (double& into)
        {
            into = strtod(GetNextString(), NULL) ;
            return *this ;
        }

    private:
        MYSQL*      m_conn ;
        MYSQL_RES*  m_result ;
        char**      m_columns ;
    } ;
}

#endif // DBA_ENABLE_MYSQL