#include "SQL.h"

std::string SQL::str()
{
    if (type == TYPE::select)
    {
        // SELECT * from t_tabname WHERE  id='1';
        if (sql_k.empty()) {}
        std::string s = "SELECT " + (sql_k.empty() ? "*" : sql_k) + " FROM `" + tabname + "`";
        if (!sql_c.empty()) { s += (" WHERE" + sql_c); }
        return s + ";";
    }
    else if (type == TYPE::update)
    {
        // UPDATE t_tabname SET a='1', b='2' WHERE id='1';
        std::string s = "UPDATE `" + tabname + "` SET " + sql_k + " WHERE" + sql_c;
        return s + ";";
    }
    else if (type == TYPE::insert)
    {
        // INSERT INTO t_tabname (a,b,c) VALUES('1','2','3');
        std::string s = "INSERT INTO `" + tabname + "` (" + sql_k + ") VALUES(" + sql_v + ");";
        return s;
    }
    else if (type == TYPE::del)
    {
        return "";
    }
    return "";
}

SQL& SQL::table(std::string t)
{
    tabname = t;
    return *this;
}

SQL& SQL::select(std::string k)
{
    if (!sql_k.empty()) { sql_k += ","; }
    sql_k += k;
    return *this;
}
SQL& SQL::where(std::string expr)
{
    sql_c += (" " + expr);
    return *this;
}

SQL& SQL::update(std::string k, std::string v)
{
    if (!sql_k.empty()) { sql_k += ","; }
    sql_k += ("`" + k + "`='" + v + "'");
    return *this;
}

SQL& SQL::insert(std::string k, std::string v)
{
    if (!sql_k.empty()) { sql_k += ","; }
    sql_k += ("`" + k + "`");
    if (!sql_v.empty()) { sql_v += ","; }
    sql_v += ("'" + v + "'");
    return *this;
}