#include "SqliteInterface.h"

#include <iostream>

SqliteInterface::SqliteInterface()
    : mSqliteDb(nullptr)
{

}

SqliteInterface::~SqliteInterface()
{

}

std::unique_ptr<SqliteInterface> SqliteInterface::Create()
{
    return std::make_unique<SqliteInterface>();
}

void SqliteInterface::Open(const DatabasePtr& db)
{
    int rc = sqlite3_open(db->GetPath().c_str(), &mSqliteDb);
    if( rc )
    {
        std::cout << "Can't open database: %s\n" << sqlite3_errmsg(mSqliteDb) << std::endl;
        sqlite3_close(mSqliteDb);
    }
}

static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
    for(int i=0; i<argc; i++)
    {
        //std::cout << "%s = %s\n" << azColName[i] << argv[i] ? argv[i] : "NULL" << std::endl;

        std::cout <<  "NULL" << std::endl;
    }
    return 0;
}

void SqliteInterface::Run(const std::string& statement)
{
    char *zErrMsg = 0;
    int rc = sqlite3_exec(mSqliteDb, statement.c_str(), callback, 0, &zErrMsg);
    if (rc != SQLITE_OK)
    {
        std::cout << "SQL error: %s\n" << zErrMsg << std::endl;
        sqlite3_free(zErrMsg);
    }
}

void SqliteInterface::Close()
{
    sqlite3_close(mSqliteDb);
}
