#include "stdafx.h"
#include "SqliteUtil.h"

#include "sqlite3.h"

#ifdef _DEBUG
#include <iostream>
#endif

CSqliteUtil *CSqliteUtil::mInstance;
CSqliteUtil::CSqliteUtil(void)
{
	mInstance = NULL;
	mDatebaase = NULL;
}

CSqliteUtil::~CSqliteUtil(void)
{
}

void CSqliteUtil::init()
{
	open();
}

CSqliteUtil* CSqliteUtil::instance()
{
	if (NULL == mInstance) {
		mInstance = new CSqliteUtil();
	}
	return mInstance;
}

void CSqliteUtil::open()
{
	char *errMsg;
	sqlite3_open("datebase.db", &mDatebaase);
	char *sql = "CREATE TABLE upload_data(\
		ID INTEGER PRIMARY KEY,\
		md5 VARCHAR(32),\
		id_card VARCHAR(40),\
		file_path VARCHAR(255),\
		name VARCHAR(36),\
		upload INTEGER\
		);";

	sqlite3_exec(mDatebaase, sql , 0 , 0 , &errMsg);
}

void CSqliteUtil::close()
{
	sqlite3_close(mDatebaase);
}

BOOL CSqliteUtil::exist(const CString& path)
{
	char sql[1024];
	USES_CONVERSION;
	_snprintf(sql, 1024, "select * from upload_data where file_path = '%s'", T2A(path));
#ifdef _DEBUG
	std::cout<<sql<<std::endl;
#endif

	return false;
}

void CSqliteUtil::clear(char **result)
{
	sqlite3_free_table(result);
}

char **CSqliteUtil::read_all(int *row, int *column)
{
	char *zErrMsg = 0;
	char **result = 0;
	char *sql = "select * from upload_data where upload != '0'";

	sqlite3_get_table(mDatebaase, sql, &result, row, column, &zErrMsg);
	for(int i=0;i<((*row)+1)* (*column);i++)
		printf("result[%d]=%s\n",i,result[i]);

	return result;
}