//#include <iostream>
//#include <vector>
//#include "ado.h"

#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>
using namespace std;

int bmain() {
	ado ado0;
	bool log = ado0.open("infodba", "infodba", "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=infodba;Initial Catalog=TC12;Data Source=DHRC");
	if (log == false) {
		cout << "登录成功" << endl;
		char *sql = "select * from FTHZHJBM WHERE PREFIX='FCC20209ww9'";
		//ado0.execute(sql);

	}
	else {
		cout << "登录失败" << endl;
	}


	ado0.close();
	return 0;
}

bool ado::open(char* UserName, char* Pwd, const char* CnnStr)
{
	try
	{
		::CoInitialize(NULL); //初始化COM环境
		m_pConnection.CreateInstance(__uuidof(Connection)); //创建连接对象
		m_pConnection->Open(CnnStr, UserName, Pwd, adModeUnknown);
	}
	catch (_com_error e)
	{
		return true;
	}
	return false;
}
bool  ado::open(char* username, char* password, char* dbname, char* ip)
{
	try
	{
		::CoInitialize(NULL); //初始化COM环境
		m_pConnection.CreateInstance(__uuidof(Connection)); //创建连接对象
		char connStr[200] = "";
		sprintf_s(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;
}
bool ado::execute(char *sqlStr)
{

	
		cout << "完成执行" << endl;
		try
		{
			m_pRecordSet = m_pConnection->Execute(sqlStr, NULL, 1);
			cout << "插入成功" << endl;
			//m_pRecordSet->Close();
			/*if(m_pConnection->State){
			m_pConnection->Close();
			}*/
		}
		catch (_com_error e)
		{
			printf(e.Description());
			return -1;
		}
	return false;
}
// 有输入参数的查询
int ado::ado_QuerySQL(char *SQL, int inputValueCount, char ** inputValue)
{
	Fields *  fields = NULL;
	long ColCount = 0;
	long RowCount = 0;

int 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;
	}

	if (outputValueCount <= 0)
	{
		return 0;
	}
	else {
		return 1;
	}
}

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

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

	*outputColumn = 0;
	*outputValueCount = 0;
	try
	{
		m_pRecordSet.CreateInstance(__uuidof(Recordset));
		m_pRecordSet->Open(SQL, m_pConnection.GetInterfacePtr(), adOpenStatic, adLockOptimistic, adCmdText);
		long ColCount = m_pRecordSet->Fields->Count;
		if (ColCount == 0) {
			return -1;
		}
		*outputColumn = ColCount;
		int index = 0;
		while (NULL != m_pRecordSet && !m_pRecordSet->adoEOF && adStateClosed != m_pRecordSet->State)
		{
			// 开始分配内存并且存储
			*outputValue = (char***)calloc(1, sizeof(char**));
			(*outputValue)[index] = (char**)calloc(1, sizeof(char*));
			for (long i = 0; i < ColCount; i++)
			{
				_variant_t rusultGet = m_pRecordSet->Fields->GetItem(long(i))->Value;
				if (rusultGet.vt != VT_NULL) {
					rusultGet.ChangeType(VT_BSTR);
					(*outputValue)[index][i] = (char*)calloc(1000, sizeof(char));
					const char* str;
					_bstr_t bst_t = rusultGet.bstrVal;
					str = (const char*)bst_t;
					strcpy_s((*outputValue)[index][i], sizeof((*outputValue)[index][i]), str);
				}
				else
				{
					(*outputValue)[index][i] = (char*)calloc(1, sizeof(char));
					strcpy_s((*outputValue)[index][i], sizeof((*outputValue)[index][i]), "");
				}
			}
			m_pRecordSet->MoveNext();///移到下一条记录
			index++;
		}
		*outputValueCount = index;
	}
	catch (_com_error e)
	{
		return 0;
	}
	return 1;
}

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

bool ado::executeInsert(char* selectSql, char * insertSql,int result)
{
	bool resultSql = false;
	try
	{
	//	m_pRecordSet = m_pConnection->Execute(selectSql, NULL, 1);

		//_variant_t vPrefix, vFlow;


		if (result==0) {
			cout << "执行插入" << endl;
			try {
				cout << insertSql << endl;
				m_pRecordSet = m_pConnection->Execute(insertSql, NULL, 1);
				cout << "插入成功" << endl;
				resultSql = true;
			}
			catch (_com_error &e)
			{
				// 输出更详细的错误信息
				_bstr_t bstrDescription(e.Description());
				_bstr_t bstrSource(e.Source());
				_bstr_t bstrErrorMessage(e.ErrorMessage());

				printf("插入错误:\n");
				printf("  错误描述: %s\n", (LPCSTR)bstrDescription);
				printf("  错误来源: %s\n", (LPCSTR)bstrSource);
				printf("  错误代码: 0x%08X\n", e.Error());
				printf("  错误信息: %s\n", (LPCSTR)bstrErrorMessage);
				resultSql = false;
			}
		}
		m_pRecordSet->Close();
		/*if(m_pConnection->State){
		m_pConnection->Close();
		}*/
		return resultSql;
	}
	catch (_com_error &e2)
	{
		_bstr_t bstrDescription(e2.Description());
		printf("连接错误: %s\n", (LPCSTR)bstrDescription);
		return false;
	}

	return resultSql;
}
bool ado::execute2(char* selectSql,char * updatesql,int result)
{
	bool resultSql = false;
	try
	{
		//m_pRecordSet = m_pConnection->Execute(selectSql, NULL, 1);
		_variant_t vPrefix, vFlow;

	//	int result = ado_QuerySQLNoInputParam(selectSql);
		//if (!m_pRecordSet->adoEOF) {
		if (result == 1) {
			cout << "执行更新" << endl;
			try {
				cout << updatesql << endl;
				m_pRecordSet = m_pConnection->Execute(updatesql, NULL, 1);
				cout << "更新成功" << endl;
				resultSql = true;
			}
			catch (_com_error e)
			{
				printf("error%s\n", e.Description());
				resultSql = false;
			}
		}
		m_pRecordSet->Close();
		/*if(m_pConnection->State){
		m_pConnection->Close();
		}*/
	}
	catch (_com_error)
	{
		//printf("error%s\n", e.Description() );
		return false;
	}

	return resultSql;
}
bool ado::close()
{
	try
	{
		m_pConnection->Close();
		CoUninitialize();
		cout << "退出登录" << endl;
	}
	catch (_com_error)
	{
		return true;
	}

	return false;
}
