/*
 * QueryBuilder.h
 *
 *  Created on: Jan 14, 2014
 *      Author: stephan
 */

#ifndef QUERYBUILDER_H_
#define QUERYBUILDER_H_

#include <sqlite3.h>
#include <string.h>
#include <stdio.h>
#include <vector>
#include "LacunaObj.h"

using namespace std;
#define MAX_SQL_LEN 8096*2
class QueryBuilder {
public:
	QueryBuilder();
	char* BuildAddQuery(TableData* tableData);
	char** BuildAddQuery(vector<TableData*>* dataVect);
	char* BuildFindQuery(TableData* tableData);
	char* BuildFindIdxOnlyQuery(TableData* tableData);
	char* BuildFindIdxOnlyWithLimitQuery(TableData* tableData, int maxReturnRows);
	char* BuildFindIdxOnlyWithFromAndLimitQuery(TableData* tableData, int from, int maxReturnRows);
	char* BuildFindIdxOnlyWithBetweenQuery(TableData* tableData, int from, int to);
	char* BuildRemoveQuery(TableData* tableData);
	char* BuildRemoveUpToQuery(TableData* tableData,int arg);
	char* BuildRemoveFromQuery(TableData* tableData, int arg);
	char* BuildRemoveIdxOnlyQuery(TableData* tableData);
	char* BuildRemoveQuery(vector<TableData*>* dataVect);
	char* BuildClearTableQuery(TableData* tableData);
	char* BuildUpdateQuery(TableData* objData);
	char* BuildUpdateQuery(TableData* findData, TableData* updateData);
	char** BuildUpdateQuery(vector<TableData*>* findData, vector<TableData*>* updateData);
	char* BuildCreateTableQuery(TableData* tableData);
	char* BuildDropTableQuery(TableData* tableData);
	virtual ~QueryBuilder();

private:
	int addFields(TableData* data, char* sqlClause, int sqlLen);
	int addPrimaryKeyClause(TableData* data, char* sqlClause, int sqlLen);
	int addForeignKeyClause(TableData* data, char* sqlClause, int sqlLen);
	int addForeignReferencesClause(TableData* data, char* sqlClause, int sqlLen);
	int recursiveUpdateBuilder(TableData* objData, char* sqlClause, int sqlLen);
	int recursiveUpdateBuilder(TableData* findData, TableData* updateData, char* sqlClause, int sqlLen);
	int recursiveUpdateBuilderForBind(TableData* findData, TableData* updateData, char* sqlClause, int sqlLen);
	int addUpToClause(TableData* data, int arg, char* sqlClause, int sqlLen);
	int addFromClause(TableData* data, int arg, char* sqlClause, int sqlLen);
	int addBetweenClause(TableData* data, int from, int to, char* sqlClause, int sqlLen);
	int addWhereClause(TableData* data, char* sqlClause, int sqlLen);
	int addWhereClauseIdxOnly(TableData* data, char* sqlClause, int sqlLen);
	int addOrderByClause(TableData* data, char* sqlClause, int sqlLen);
	int addSetClause(TableData*, char* sqlClause, int sqlLen);
	int addValuesClause(TableData*, char* sqlClause, int sqlLen);
	int addColNamesAndValuesClause(TableData*, char* sqlClause, int sqlLen);
	int addColNamesAndValuesClauseForBind(TableData*, char* sqlClause, int sqlLen);
	int addValues(const ColumnNameList* nameList, const ColumnDataList* dataList, char* sqlClause, int sqlLen);
};

#endif /* QUERYBUILDER_H_ */
