/////////////////////////////////////////////////////////////////////////////
// Name:        odbctools.h
// Purpose:     Crude odbc manipulation for wxWindows on win32 (mingw)
// Author:      Peter Weston (Peter.Weston AT holistech.co.uk)
// Modified by: 
// Created:     26-Apr-2002
// RCS-ID:      
// Copyright:   (c) Copyright 2002-2003 Peter Weston
// Licence:     wxWindows Library Licence, Version 3
/////////////////////////////////////////////////////////////////////////////
// version: V0.2

#include "odbctools.h"

odbcTools::odbcTools() {
	static const wxChar *FUNC_NAME = _T("SQLConfigDataSource");
	static const wxChar *LIB_NAME = _T("odbccp32");

	SQLConfigDataSource=NULL;
	odbccp32lib.Load(LIB_NAME);

	if (!odbccp32lib.IsLoaded()) {
		wxLogError(_T("Failed to open the odbc control panel odbccp32.dll. You may need to reinstall odbc."));
	} else {
		SQLConfigDataSource = (SQLConfigDataSourceTYPE)odbccp32lib.GetSymbol(FUNC_NAME);
		if (!SQLConfigDataSource) {
			wxLogError(_T("Failed to find SQLConfigDataSource entry point in odbccp32.dll. You may need to reinstall odbc."));
		}
	}
}

odbcTools::~odbcTools() {
	if (odbccp32lib.IsLoaded()) odbccp32lib.Unload();
}

bool odbcTools::CreateAccessFile(const wxString filename) {
// silently deletes any existing file before creating it an empty access database
// spots directories...
	bool ok=false;
	
	if (wxFileExists(filename)) {
		if (!wxDirExists(filename)) {
			if (!wxRemoveFile(filename)) wxLogError(_T("Failed to remove ") + filename + _T(" before recreating it. Could be a file permissions problem."));
		} else {
			wxLogError(_T("Can't create file ")+ filename + _T(" - that is a directory."));
		}
	}
	if (!wxFileExists(filename)) {
		ok=DoConfigCommand(_T("CREATE_DB=\"") + filename + _T("\"**"));
	} else {
		wxLogError(_T("Failed to create database - there is already a file called ") + filename);
	}
	if (!ok) wxLogError(_T("Failed to create database ") + filename);
	return ok;
}

bool odbcTools::CreateDSN(const wxString dsn, const wxString filename) {
	if (!wxFileExists(filename)) {
		wxLogError(_T("Can't create DSN - file ") + filename + _T(" doesn't exist."));
	} else {
		return DoConfigCommand(_T("DSN=") + dsn + _T("*DBQ=") + filename + _T("*DESCRIPTION=") + dsn + _T("**"));
	}	
	return false;
}

bool odbcTools::DoConfigCommand(const wxString command) {
	bool result=FALSE;
	
	if (!SQLConfigDataSource) {
		wxLogError(_T("Entry point not found while trying to execute ") + command);
	} else {
		char *cmd=NULL;
		cmd = getCmd(command);
		result=SQLConfigDataSource((HWND) NULL,1,ODBCDRIVER,cmd);
		delete cmd;
		if (!result) {
			wxLogError(_T("Failed to execute ") + command + _T(". You might be able to do the same through the odbc control panel."));
		}
	}
	return result;
}

char * odbcTools::getCmd(const wxString text) {
// replaces * with null, returned string must be deleted after use.
// used to get round wxString's null termination - odbc commands have embedded nulls
	char * res= copystring(text.c_str());
	for (unsigned int c=0;c<text.Len();c++) if (res[c]==_T('*')) res[c]=_T('\0');
	return res;
}

