#include "ado.h"
#include <stdio.h>
#include <ctype.h>
#include <string.h>

#include <string>
#include <iostream>
#include <iterator>
#include <map>
#include <vector>
#include <sstream>
#include <fstream>

#ifdef WIN32
#include <io.h>
#include <direct.h>
#else
#include <unistd.h>
#endif

using namespace std;

_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
HRESULT hr;

bool open(char* username, char* password, char* dbname, char* ip)
{
	try
	{
		::CoInitialize(NULL); //初始化COM环境
		m_pConnection.CreateInstance(__uuidof(Connection)); //创建连接对象
		char connStr[200] = "";
		sprintf(connStr, "Provider=SQLOLEDB;Data Source=%s;Initial Catalog=%s;", ip, dbname);
		cout << connStr << endl;
		hr = m_pConnection->Open(connStr, username, password, -1);
		if (hr != S_OK)
			return true;
	}
	catch (_com_error e)
	{
		cout << e.Description() << endl;
		return true;
	}
	return false;
}

// 有输入参数的查询
int ado_QuerySQL(char *SQL, int inputValueCount, char ** inputValue, int * outputColumn, int * outputValueCount, char **** outputValue)
{
	Fields *  fields = NULL;
	long ColCount = 0;
	long RowCount = 0;

	*outputColumn = 0;
	*outputValueCount = 0;

	m_pRecordset.CreateInstance(__uuidof(Recordset));

	m_pRecordset->Open(SQL, m_pConnection.GetInterfacePtr(), adOpenStatic, adLockOptimistic, adCmdText);

	hr = m_pRecordset->get_Fields(&fields);	//得到记录集的字段集和	

	if (SUCCEEDED(hr))
	{
		RowCount = m_pRecordset->GetRecordCount();

		ColCount = m_pRecordset->Fields->Count;

		*outputValueCount = (int)RowCount;
		*outputColumn = (int)ColCount;
	}

	if (*outputValueCount <= 0)
	{
		return 0;
	}
	int index = 0;
	// 开始分配内存并且存储
	*outputValue = (char ***)calloc(RowCount + 1, sizeof(char**));
	while (!m_pRecordset->adoEOF)
	{
		(*outputValue)[index] = (char **)calloc(ColCount + 1, sizeof(char *));
		for (long i = 0; i < ColCount; i++)
		{
			BSTR bstrColName;
			fields->Item[i]->get_Name(&bstrColName);
			_variant_t taskStyle = NULL;
			taskStyle = m_pRecordset->GetCollect(bstrColName);
			if (taskStyle.vt != VT_NULL) {
				const char* str = NULL;



				_bstr_t bst_t = (_bstr_t)taskStyle;

				str = (const char*)bst_t;

				(*outputValue)[index][i] = (char *)calloc(strlen(str) + 1, sizeof(char));
				strcpy((*outputValue)[index][i], str);

			}
			else
			{
				//printf("NULL\n");
				(*outputValue)[index][i] = (char *)calloc(10000, sizeof(char));
				strcpy((*outputValue)[index][i], "");
			}


		}
		m_pRecordset->MoveNext();///移到下一条记录
		index++;
	}
	return 0;
}

// 无输入参数的查询
int ado_QuerySQLNoInputParam(char *SQL, int * outputColumn, int * outputValueCount, char **** outputValue)
{
	return ado_QuerySQL(SQL, 0, NULL, outputColumn, outputValueCount, outputValue);
}

// 无参数操作
int ado_ExecuteSQLNoInputParam(char *SQL)
{
	return ado_ExecuteSQL(SQL, 0, NULL);
}

// 执行类操作
int ado_ExecuteSQL(char *SQL, int valueCount, char **value)
{
	try
	{
		m_pConnection->Execute(_bstr_t(SQL), 0, adCmdText);
		printf("删除结束\n");
		//LogInsert ("写入结束!");
	}
	catch (_com_error e)
	{
		printf(e.Description());
		return -1;
	}

	return 0;
}

void close(void)
{

	try
	{
		if (m_pRecordset != NULL) {
			m_pRecordset->Close();
			m_pConnection->Close();
		}
		printf("2222");
		//::CoUninitialize(); //释放环境
	}
	catch (_com_error e)
	{
		cout << e.Description() << endl;
	}
}