
#include "SqlGenerate.h"
#include <QMapIterator>
 
SqlGenerate::SqlGenerate()
{

}

SqlGenerate::~SqlGenerate()
{

}

QString SqlGenerate::createTable(QString table, QMap<QString, QString> map)
{
	QString content = QString("create   table %1 (").arg(table);
	QMapIterator<QString, QString> i(map);
	while (i.hasNext())
	{
		i.next();
		content.append(QString("%1 %2  ").arg(i.key()).arg(i.value()));
		if (i.hasNext())
		{
			content.append(",");
		}
	}
	content.append(")");
	content += QString(";");
	return content;
}

QString SqlGenerate::insertData(QString table, QVariantMap map)
{
	//insert or replace into
	QString content = QString("insert or replace into %1 (").arg(table);
	QString values = QString("values(");
	QMapIterator<QString, QVariant> i(map);
	while (i.hasNext())
	{
		i.next();
		content.append(QString("%1").arg(i.key()));
		values.append("'" + i.value().toString().replace("'", "''") + "'"); //
		if (i.hasNext())
		{
			content.append(", ");
			values.append(",");
		}
	}
	content.append(") ");
	values.append(")");
	content.append(values);
	content += QString(";");
	return content;
}

QString SqlGenerate::selectWhere(QString table, QStringList Name, QString condition)
{
	//QString content = QString("select from  %1 ").arg(table);
	QString content = QString("select  ");// .arg(table);
	QListIterator<QString> i(Name);
	while (i.hasNext())
	{
		i.next();
		if (i.hasNext())
		{
			content += QString(" %1 ,").arg(i.previous());
		}
		else
		{
			content += QString(" %1 ").arg(i.previous());
		}
		i.next();

	}
	/*for(int i = 0; i < Name.size(); i++)
	{
	if (i + 1 == Name.size())
	{
	content += QString("%1 ").arg(Name.at(i));
	}
	else
	{
	content += QString("%1,").arg(Name.at(i));
	}
	}*/
	content += QString("from  %1").arg(table);
	if (!condition.isEmpty())
	{
		content += condition;// QString(" where %1").arg(condition);
	}
	content += QString(";");//  

 

	return content;
}

QString SqlGenerate::selectUnion(QStringList sTables, QStringList Name, QString condition /*= ""*/)
{
	QString s_select_Data;
	for (int j = 0; j < sTables.size(); j++)
	{

		QString content = QString("select  ");// .arg(table);
		QListIterator<QString> i(Name);
		while (i.hasNext())
		{
			i.next();
			if (i.hasNext())
			{
				content += QString(" %1 ,").arg(i.previous());
			}
			else
			{
				content += QString(" %1 ").arg(i.previous());
			}
			i.next();

		}
		content += QString("from  %1").arg(sTables.at(j));

		if (s_select_Data.isEmpty())
		{
			s_select_Data = content;
		}
		else
		{
			s_select_Data = s_select_Data + " union all  " + content;
		}
	}
	if (!condition.isEmpty())
	{
		s_select_Data += condition;// QString(" where %1").arg(condition);
	}
	s_select_Data += QString(";");//

	return s_select_Data;
}

QString SqlGenerate::createIndex(QString table, QString name)
{
	QString content = QString("create   index  %1_index on  %2  (%3)").arg(name).arg(table).arg(name);
	content += QString(";");//  
	return content;
}

QString SqlGenerate::updateData(QString table, QVariantMap map, QString condition)
{
	QString content = QString("update %1 set ").arg(table);
	QMapIterator<QString, QVariant>i(map);
	while (i.hasNext())
	{
		i.next();
		if (i.hasNext())
		{
			content += QString("%1 = '%2',").arg(i.key()).arg(i.value().toString().replace("'", "''"));
		}
		else
		{
			content += QString("%1= '%2' ").arg(i.key()).arg(i.value().toString().replace("'", "''"));
		}

	}
	content += QString("where %1").arg(condition);
	content += QString(";"); 
	return  content;
}

QString SqlGenerate::deleteData(QString table, QString condition)
{
	QString content = QString("delete %1 ").arg(table);
	content += QString("where %1").arg(condition);
	content += QString(";");
	return content;
}

bool SqlGenerate::checkCheckParameterSql(const QString& str)
{
	QStringList  keys;
	keys << "and";
	keys << "or";
	keys << "*";
	keys << "=";
	keys << " ";
	keys << "%0a";
	keys << "%0d";
	keys << "%";
	keys << "/";
	keys << "union";
	keys << "|";
	keys << "&";
	keys << "^";
	keys << "#";
	keys << "/*";
	keys << "*/";
	keys << "delete";
	keys << "insert";
	keys << "select";
	keys << "update";
	keys << "drop";
	for (int i = 0; i < keys.size(); i++)
	{
		if (str.contains(keys[i])) // != string::npos)
		{
			return false;
		}
	}
	return true;
}

