#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_SQLITE)

#ifndef _DBA_HAVE_DATABASES
# define _DBA_HAVE_DATABASES
#endif

#include <sqlite3.h>

#ifndef DBA_DEFAULT_FLAGS_SQLITE
# define DBA_DEFAULT_FLAGS_SQLITE (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE)  // Defined as default behavior for earlier sqlite_open calls.
#endif

namespace DBA
{
    // Define default SQLite connection flags.
    class SQLiteConnection : public Connection
    {
    public:
        SQLiteConnection() ;
        virtual ~SQLiteConnection() ;

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

        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 false ; }

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

        virtual bool FetchRow() ;

        const char* GetError()
        {
            return sqlite3_errmsg(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 (char*)sqlite3_column_text(m_result, index) ;
        }

        virtual Connection& operator >> (const void*& into)
        {
            into = sqlite3_column_blob(m_result, NextColumn()) ;
            return *this ;
        }

        virtual Connection& operator >> (const char*& into)
        {
            into = (const char*)sqlite3_column_text(m_result, NextColumn()) ;
            return *this ;
        }

        virtual Connection& operator >> (const unsigned char*& into)
        {
            into = (const unsigned char*)sqlite3_column_text(m_result, NextColumn()) ;
            return *this ;
        }

        signed int GetNextInt()
        {
            return sqlite3_column_int(m_result, NextColumn()) ;
        }

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

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

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

        virtual Connection& operator >> (long long& into)
        {
            into = sqlite3_column_int64(m_result, NextColumn()) ;
            return *this ;
        }
        virtual Connection& operator >> (unsigned long long& into)
        {
            into = (unsigned long long)sqlite3_column_int64(m_result, NextColumn()) ;
            return *this ;
        }

        virtual Connection& operator >> (float& into)
        {
            into = (float)sqlite3_column_double(m_result, NextColumn()) ;
            return *this ;
        }
        virtual Connection& operator >> (double& into)
        {
            into = sqlite3_column_double(m_result, NextColumn()) ;
            return *this ;
        }

    private:
        sqlite3*        m_conn ;
        sqlite3_stmt*   m_result ;
    } ;
}

#endif // DBA_ENABLE_SQLITE