// ManDB Extension

#include "stdafx.h"

#include "ManDB1.h"
#include "crack.h"
#include <afxdao.h>
#include <stdio.h>

VOID OutputString(EXTENSION_CONTROL_BLOCK *pECB, LPCSTR szBuffer);

///////////////////////////////////////////////////////////////////////
// The one and only CWinApp object
// NOTE: You may remove this object if you alter your project to no
// longer use MFC in a DLL.

CWinApp theApp;

static const TCHAR szExpires[] = _T("Expires: Thu, 01 Jan 1995 01:00:00 GMT\r\n");
static const TCHAR szNoCache[] = _T("Pragma: no-cache\r\n");

//const PCSTR Conn = "dsn=OLE_DB_NWind_Jet;uid=Admin;pwd=;";
///////////////////////////////////////////////////////////////////////
// command-parsing map

BEGIN_PARSE_MAP(CManDBExtension, CHttpServer)
// TODO: insert your ON_PARSE_COMMAND() and
// ON_PARSE_COMMAND_PARAMS() here to hook up your commands.
// For example:
ON_PARSE_COMMAND(Default, CManDBExtension, ITS_EMPTY)
ON_PARSE_COMMAND(dbRead, CManDBExtension, ITS_EMPTY)

DEFAULT_PARSE_COMMAND(Default, CManDBExtension)

END_PARSE_MAP(CManDBExtension)


///////////////////////////////////////////////////////////////////////
// The one and only CManDBExtension object

CManDBExtension theExtension;


///////////////////////////////////////////////////////////////////////
// CManDBExtension implementation

CManDBExtension::CManDBExtension()
{
}

CManDBExtension::~CManDBExtension()
{
}

BOOL CManDBExtension::GetExtensionVersion(HSE_VERSION_I NFO* pVer)
{
// Call default implementation for initialization
CHttpServer::GetExtensionVersion(pVer);

// Load description string
TCHAR sz[HSE_MAX_EXT_DLL_NAME_LEN+1];
ISAPIVERIFY(::LoadString(AfxGetResourceHandle(),
IDS_SERVER, sz, HSE_MAX_EXT_DLL_NAME_LEN));
_tcscpy(pVer->lpszExtensionDesc, sz);
return TRUE;
}


/*DWORD WINAPI HttpExtensionProc(EXTENSION_CONTROL_BLOCK *pECB )
{
CHttpServerContext* pCtxt = NULL;
DWORD dwWritten;
char szContent[] = "Content-type: text/html\r\n\r\n";

dwWritten = sizeof(szContent);
BOOL ris=pECB->ServerSupportFunction (pECB->ConnID,
HSE_REQ_SEND_RESPONSE_HEADER,
NULL,
&dwWritten,
(LPDWORD)szContent);

if (!ris)
return HSE_STATUS_ERROR;
else
return HSE_STATUS_SUCCESS;


}*/


BOOL CManDBExtension::TerminateExtension(DWORD dwFlags)
{
// extension is being terminated
//TODO: Clean up any per-instance resources
return TRUE;
}

///////////////////////////////////////////////////////////////////////
// CManDBExtension command handlers

void CManDBExtension:efault(CHttpServerContext* pCtxt)
{
FILE *fp=NULL;
fp= fopen("c:\\ManDB.log","wt");


fprintf(fp,"apertura log");

StartContent(pCtxt);
fprintf(fp,"Content Type");

WriteTitle(pCtxt);
fprintf(fp,"Title");
*pCtxt << _T("This default message was produced by the Internet");
*pCtxt << _T(" Server DLL Wizard. Edit your CManDBExtension:efault()");
*pCtxt << _T(" implementation to change it.\r\n");

EndContent(pCtxt);
fprintf(fp,"Chiusura CHTtpServerContext");
fclose(fp);
}

VOID OutputString(EXTENSION_CONTROL_BLOCK *pECB, LPCSTR szBuffer)
{
DWORD dwBuffer = strlen(szBuffer);
pECB->WriteClient(pECB->ConnID, (PVOID) szBuffer, &dwBuffer, 0);
}
/*
BOOL CManDBExtension::recInsert()
{
}*/
/*
Funzione per la lettura di un Database Generico

SE Layout = 'D'
ALLORA lettura di default
ALTRIMENTI
SE Layout='E'
ALLORA lettura con aggiunta di un link ke chiami metodo di delete
ALTRIMENTI
SE Layout='U'
ALLORA lettura con aggiunta di un link che chiami metodo Update
*/
void CManDBExtension::dbRead(CHttpServerContext* pCtxt)
{
FILE *fp=NULL;
fp=fopen("c:\\ManDB.log","wt");
fprintf(fp,"apertura log");
/* char buffer[50];
DWORD sBuffer=sizeof(buffer);

(*pCtxt).GetServerVariable(_T("HTTP_ACCEPT"), (PVOID)buffer,&sBuffer);*/
AddHeader(pCtxt, szExpires);
AddHeader(pCtxt, szNoCache);
StartContent(pCtxt);
fprintf(fp,"StartContent");
WriteTitle(pCtxt);
fprintf(fp,"Write Title");

CDaoWorkspace *ws;
CDaoDatabase *db;
// CDaoTableDef *td;
CDaoRecordset *rs;

ws=new CDaoWorkspace;
ws->Create(_T("VersionWorkspace"),_T("Admin"),_T("")) ;
fprintf(fp,"WorkSpace create...");
db=new CDaoDatabase(ws);
db->Open("C:\\pwsdb\\db1.mdb",FALSE,FALSE,_T(""));
fprintf(fp,"DataBase create...");
// td=new CDaoTableDef(db);
// td->Open("TD");

rs =new CDaoRecordset(db);
char *query="Schedario";
rs->Open(dbOpenTable,query,dbReadOnly);
fprintf(fp,"RecordSet create...");
int i=0;
long numField=rs->GetFieldCount();
*pCtxt<<"<table>";
rs->MoveFirst();
COleVariant *OleV =NULL;
OleV= new COleVariant;
fprintf(fp,"ColeVariant create...");
CString fieldValue;
while(!rs->IsEOF())
{
*pCtxt<<"<tr>";
fprintf(fp,"<tr>");
for(i=0; i<numField; i++)
{
*OleV = rs->GetFieldValue(i);
fieldValue=CCrack::strVARIANT(OleV);
*pCtxt<<"<td>";
*pCtxt<<fieldValue;
*pCtxt<<"</td>";
fprintf(fp,"<td>%d</td>",i);
}
*pCtxt<<"</tr>";
fprintf(fp,"</tr>");
rs->MoveNext();
}
*pCtxt<<"</table>";
//chiusura
rs->Close();
rs=NULL;
// td->Close();
// td=NULL;
db->Close();
db=NULL;
ws->Close();
ws=NULL;
fprintf(fp,"Chiusura Oggetti");



EndContent(pCtxt);
fprintf(fp,"Chiusura html");
fclose(fp);

}
/*
BOOL CManDBExtension::recDelete()
{
}
BOOL CManDBExtension::recUpdate()
{
}*/

/*
void OpenX(_RecordsetPtr rs, _ConnectionPtr Conn)

{

// Define ADO object pointers.

// Initialize pointers on define.

// These are in the ADODB:: namespace

rs = NULL;
Conn = NULL;


// Define string variables.

_bstr_t strCnn("Provider='sqloledb';Data Source='MySqlServer';"

"Initial Catalog='pubs';Integrated Security='SSPI';");


// Define Other Variables.

HRESULT hr = S_OK;

IADORecordBinding *picRs = NULL; // Interface Pointer declared.

CEmployeeRs emprs; // C++ Class object

DBDATE varDate;


try

{

// open connection and record set

TESTHR(Conn.CreateInstance(__uuidof(Connection)));

Conn->Open(strCnn,"","",adConnectUnspecified);


TESTHR(rs.CreateInstance(__uuidof(Recordset)));

rs->Open("Employee", _variant_t((IDispatch *)pConnection,true), adOpenKeyset, adLockOptimistic, adCmdTable);


// Open an IADORecordBinding interface pointer which we'll

// use for Binding Recordset to a class.

TESTHR(rs->QueryInterface(__uuidof(IADORecordBinding),(LPVOI D*)&picRs));


//Bind the Recordset to a C++ Class here.

TESTHR(picRs->BindToRecordset(&emprs));


// Assign the first employee record's hire date

// to a variable, then change the hire date.

varDate = emprs.m_sze_hiredate;


emprs.m_sze_hiredate.year=1900;
emprs.m_sze_hiredate.month=1;
emprs.m_sze_hiredate.day=1;
picRs->Update(&emprs);



// Requery Recordset and reset the hire date.

rs->Requery(adOptionUnspecified);

// Open an IADORecordBinding interface pointer which we'll

// use for Binding Recordset to a class.

TESTHR(rs->QueryInterface(__uuidof(IADORecordBinding),(LPVOI D*)&picRs));


// Rebind the Recordset to a C++ Class here.

TESTHR(picRs->BindToRecordset(&emprs));

emprs.m_sze_hiredate = varDate;

picRs->Update(&emprs);

}

catch(_com_error &e)

{

// Notify the user of errors if any.

// Pass a connection pointer accessed from the Connection.

PrintProviderError(pConnection);

PrintComError(e);

}


// Clean up objects before exit.

if (rs)
if (rs->State == adStateOpen)
rs->Close();

if (Conn)
if (Conn->State == adStateOpen)
Conn->Close();

}

*/
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CManDBExtension, CHttpServer)
//{{AFX_MSG_MAP(CManDBExtension)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0



///////////////////////////////////////////////////////////////////////
// If your extension will not use MFC, you'll need this code to make
// sure the extension objects can find the resource handle for the
// module. If you convert your extension to not be dependent on MFC,
// remove the comments arounn the following AfxGetResourceHandle()
// and DllMain() functions, as well as the g_hInstance global.

/****

static HINSTANCE g_hInstance;

HINSTANCE AFXISAPI AfxGetResourceHandle()
{
return g_hInstance;
}

BOOL WINAPI DllMain(HINSTANCE hInst, ULONG ulReason,
LPVOID lpReserved)
{
if (ulReason == DLL_PROCESS_ATTACH)
{
g_hInstance = hInst;
}

return TRUE;
}

****/