#include "CivTableTemp.h"
#include <fstream>

CivTableTemp::CivTableTemp(std::string tableName)
	:mTableName(tableName)
{
	mFieldTyepMap.insert(std::pair<FieldType, std::string>(CivTableTemp::FILED_INT, "int"));
	mFieldTyepMap.insert(std::pair<FieldType, std::string>(CivTableTemp::FILED_INTEGER,"integer"));
	mFieldTyepMap.insert(std::pair<FieldType, std::string>(CivTableTemp::FILED_DOUBLE, "double precision"));
	mFieldTyepMap.insert(std::pair<FieldType, std::string>(CivTableTemp::FILED_VARCHAR, "varchar(20)"));
	mFieldTyepMap.insert(std::pair<FieldType, std::string>(CivTableTemp::FILED_DATETIME, "timestamp"));
	mFieldTyepMap.insert(std::pair<FieldType, std::string>(CivTableTemp::FILED_JSON, "json"));
}

CivTableTemp::~CivTableTemp()
{

}

void CivTableTemp::addProperty(std::string fileName, FieldType filedType)
{
	FieldProperty prop = { fileName ,mFieldTyepMap.find(filedType)->second};
	mPropertys.push_back(prop);
}

void CivTableTemp::addPropertys(FiledPropertys props)
{
	mPropertys.insert(mPropertys.end(), props.begin(), props.end());
}

std::string CivTableTemp::createSql()
{
	if (mPropertys.size() <= 0)
		return "";

	
	char sql[512] = "create table ";
	strcat_s(sql, mTableName.c_str());
	strcat_s(sql, "(id serial primary key");
	int total = mPropertys.size();
	for (int i = 0; i < total; i++)
	{
		FieldProperty property = mPropertys[i];
		strcat_s(sql, ",");
		strcat_s(sql, property.mFiledName.c_str());
		strcat_s(sql, "  ");
		strcat_s(sql, property.mFieldType.c_str());
	}
	strcat_s(sql, ")");

	return sql;
}