#include "StdAfx.h" #include "connect.h" #include "server.h" #include "sqlfile.h" long TPrassiConnection::OpenFile(int nLogicNum, LPCSTR sWhere) { TSqlFile* pSqlFile; long nHandle = nLogicNum*1000; for ( ; m_sqlFiles.Lookup(nHandle, pSqlFile); nHandle++); pSqlFile = new TSqlFile(m_nFirm, nLogicNum, sWhere, Id()); if (pSqlFile->Open() == 0) m_sqlFiles.SetAt(nHandle, pSqlFile); else { delete pSqlFile; nHandle = 0; } return nHandle; } TSqlFile* TPrassiConnection::GetFile(long nHandle) { TSqlFile* pSqlFile = NULL; m_sqlFiles.Lookup(nHandle, pSqlFile); if (pSqlFile == NULL && nHandle < 1000) { int nLogicNum = nHandle; pSqlFile = new TSqlFile(m_nFirm, nLogicNum, NULL, Id()); if (pSqlFile->Exists()) m_sqlFiles.SetAt(nLogicNum, pSqlFile); else { delete pSqlFile; pSqlFile = NULL; } } return pSqlFile; } BOOL TPrassiConnection::CloseFile(long nHandle) { TSqlFile* pSqlFile = GetFile(nHandle); if (pSqlFile) { delete pSqlFile; m_sqlFiles.RemoveKey(nHandle); } return pSqlFile != NULL; } BOOL TPrassiConnection::SetFirm(int nFirm) { BOOL bOk = m_nFirm == nFirm; if (!bOk) { TPrassiServer& s = GetServer(); bOk = s.TestFirm(nFirm); if (bOk) { long nHandle; TSqlFile* pSqlFile; for (POSITION pos = m_sqlFiles.GetStartPosition(); pos; ) { m_sqlFiles.GetNextAssoc(pos, nHandle, pSqlFile); pSqlFile->SetFirm(nFirm); } } } return bOk; } /////////////////////////////////////////////////////////// // Jolly2File static TSqlFile* Jolly2File(TConnection& conn, void *pJolly) { TSqlFile* pFile = NULL; const CStringArray& argv = *(CStringArray*)pJolly; if (argv.GetSize() > 1) { TPrassiConnection& c = (TPrassiConnection&)conn; const long n = atol(argv[1]); pFile = c.GetFile(n); } return pFile; } static TSqlFile* Jolly2FileInt(TConnection& conn, void *pJolly, int& num) { TSqlFile* pFile = NULL; const CStringArray& argv = *(CStringArray*)pJolly; if (argv.GetSize() > 2) { pFile = Jolly2File(conn, pJolly); num = atoi(argv[2]); } return pFile; } static TSqlFile* Jolly2FileIntInt(TConnection& conn, void *pJolly, int& num, int& flag) { TSqlFile* pFile = NULL; const CStringArray& argv = *(CStringArray*)pJolly; if (argv.GetSize() > 3) { pFile = Jolly2File(conn, pJolly); num = atoi(argv[2]); flag = atoi(argv[3]); } return pFile; } static TSqlFile* Jolly2FileString(TConnection& conn, void *pJolly, CString& str) { TSqlFile* pFile = NULL; const CStringArray& argv = *(CStringArray*)pJolly; if (argv.GetSize() > 2) { pFile = Jolly2File(conn, pJolly); str = argv[2]; } return pFile; } static TSqlFile* Jolly2FileStringInt(TConnection& conn, void *pJolly, CString& str, int& num) { TSqlFile* pFile = NULL; const CStringArray& argv = *(CStringArray*)pJolly; if (argv.GetSize() > 3) { pFile = Jolly2File(conn, pJolly); str = argv[2]; num = atoi(argv[3]); } return pFile; } static TSqlFile* Jolly2FileStringString(TConnection& conn, void *pJolly, CString& str, CString& val) { TSqlFile* pFile = NULL; const CStringArray& argv = *(CStringArray*)pJolly; if (argv.GetSize() > 3) { pFile = Jolly2File(conn, pJolly); str = argv[2]; val = argv[3]; } return pFile; } static int Jolly2Int(void *pJolly) { const CStringArray& argv = *(CStringArray*)pJolly; int n = 0; if (argv.GetSize() > 1) n = atoi(argv[1]); return n; } static CString Jolly2String(void *pJolly) { const CStringArray& argv = *(CStringArray*)pJolly; CString str; if (argv.GetSize() > 1) str = argv[1]; return str; } /////////////////////////////////////////////////////////// // int FileClearFields(int nHandle) BOOL f_FileClearFields(TConnection& conn, void* pJolly) { TSqlFile* pFile = Jolly2File(conn, pJolly); if (pFile) return conn.ReturnInteger(pFile->ClearFields()); return FALSE; } /////////////////////////////////////////////////////////// // int FileClose(int nHandle) BOOL f_FileClose(TConnection& conn, void* pJolly) { const CStringArray& argv = *(CStringArray*)pJolly; if (argv.GetSize() > 1) { TPrassiConnection& c = (TPrassiConnection&)conn; long nHandle = atol(argv[1]); TSqlFile* pFile = c.GetFile(nHandle); if (pFile) { int err = pFile->Close(); if (err == 0) c.CloseFile(nHandle); return conn.ReturnInteger(err); } } return FALSE; } /////////////////////////////////////////////////////////// // int FileFirst(int nHandle) int f_FileFirst(TConnection& conn, void* pJolly) { int nLocks; TSqlFile* pFile = Jolly2FileInt(conn, pJolly, nLocks); if (pFile) return conn.ReturnInteger(pFile->First(nLocks)); return FALSE; } /////////////////////////////////////////////////////////// // int FileGetField(int nHandle, CString strName) BOOL f_FileGetField(TConnection& conn, void* pJolly) { CString strName; TSqlFile* pFile = Jolly2FileString(conn, pJolly, strName); if (pFile) { const char* str = pFile->GetField(strName); if (str) return conn.ReturnString(str); } return FALSE; } /////////////////////////////////////////////////////////// // CString FileGetFieldInfo(int nLogicNum, int nIndex) BOOL f_FileGetFieldInfo(TConnection& conn, void* pJolly) { int nIndex; TSqlFile* pFile = Jolly2FileInt(conn, pJolly, nIndex); if (pFile) { CString strInfo = pFile->GetFieldInfo(nIndex); return conn.ReturnString(strInfo); } return FALSE; } /////////////////////////////////////////////////////////// // int FileGetItems(long nHandle) BOOL f_FileGetItems(TConnection& conn, void* pJolly) { TSqlFile* pFile = Jolly2File(conn, pJolly); if (pFile) { int items = pFile->GetRecordCount(); if (items >= 0) return conn.ReturnInteger(items); } return FALSE; } /////////////////////////////////////////////////////////// // int FileGetKey(int nHandle) BOOL f_FileGetKey(TConnection& conn, void* pJolly) { TSqlFile* pFile = Jolly2File(conn, pJolly); if (pFile) { int key = pFile->GetKey(); if (key > 0) return conn.ReturnInteger(key); } return FALSE; } /////////////////////////////////////////////////////////// // CString FileGetKeyExpr(int nLogicNum) BOOL f_FileGetKeyExpr(TConnection& conn, void* pJolly) { int key; TSqlFile* pFile = Jolly2FileInt(conn, pJolly, key); if (pFile) return conn.ReturnString(pFile->GetKeyExpr(key)); return FALSE; } /////////////////////////////////////////////////////////// // int FileGetPosition(int nHandle) BOOL f_FileGetPosition(TConnection& conn, void* pJolly) { TSqlFile* pFile = Jolly2File(conn, pJolly); if (pFile) return conn.ReturnInteger(pFile->GetPosition()); return FALSE; } /////////////////////////////////////////////////////////// // int FileLast(int nHandle, int nLock) BOOL f_FileLast(TConnection& conn, void* pJolly) { int nLocks; TSqlFile* pFile = Jolly2FileInt(conn, pJolly, nLocks); if (pFile) return conn.ReturnInteger(pFile->Last(nLocks)); return FALSE; } /////////////////////////////////////////////////////////// // int FileLock(int nHandle) BOOL f_FileLock(TConnection& conn, void* pJolly) { TSqlFile* pFile = Jolly2File(conn, pJolly); if (pFile) return conn.ReturnInteger(pFile->ExclusiveLock()); return FALSE; } /////////////////////////////////////////////////////////// // int FileMove(int nHandle, int pos) BOOL f_FileMove(TConnection& conn, void* pJolly) { int nPos, nLocks; TSqlFile* pFile = Jolly2FileIntInt(conn, pJolly, nPos, nLocks); if (pFile) return conn.ReturnInteger(pFile->Move(nPos, nLocks)); return FALSE; } /////////////////////////////////////////////////////////// // int FileNext(int nHandle) BOOL f_FileNext(TConnection& conn, void* pJolly) { int nLocks; TSqlFile* pFile = Jolly2FileInt(conn, pJolly, nLocks); if (pFile) return conn.ReturnInteger(pFile->Next(nLocks)); return FALSE; } /////////////////////////////////////////////////////////// // int FileOpen(int nLogicNum) BOOL f_FileOpen(TConnection& conn, void* pJolly) { const CStringArray& argv = *(CStringArray*)pJolly; if (argv.GetSize() > 1) { TPrassiConnection& c = (TPrassiConnection&)conn; int nLogicNum = atoi(argv[1]); LPCSTR sWhere = NULL; if (argv.GetSize() > 2) sWhere = argv[2]; long nHandle = c.OpenFile(nLogicNum, sWhere); if (nHandle) return c.ReturnInteger(nHandle); } return FALSE; } /////////////////////////////////////////////////////////// // int FileOpenTable(const char* str) BOOL f_FileOpenTable(TConnection& conn, void* pJolly) { const CStringArray& argv = *(CStringArray*)pJolly; if (argv.GetSize() > 1) { TPrassiConnection& c = (TPrassiConnection&)conn; const char* tab = argv[1]; int nLogicNum = 5; if (!isalnum(*tab)) { nLogicNum--; tab++; } CString strWhere; strWhere.Format("COD='%s'", tab); if (argv.GetSize() > 2) { strWhere += " AND "; strWhere += argv[2]; } long nHandle = c.OpenFile(nLogicNum, strWhere); if (nHandle) return c.ReturnInteger(nHandle); } return FALSE; } /////////////////////////////////////////////////////////// // int FilePrev(int nHandle) BOOL f_FilePrev(TConnection& conn, void* pJolly) { int nLocks; TSqlFile* pFile = Jolly2FileInt(conn, pJolly, nLocks); if (pFile) return conn.ReturnInteger(pFile->Prev(nLocks)); return FALSE; } /////////////////////////////////////////////////////////// // int FileRead(int nHandle, CString where, int flags) BOOL f_FileRead(TConnection& conn, void* pJolly) { CString fields; int flags; TSqlFile* pFile = Jolly2FileStringInt(conn, pJolly, fields, flags); if (pFile) return conn.ReturnInteger(pFile->Read(fields, flags)); return FALSE; } /////////////////////////////////////////////////////////// // int FileRemove(int nHandle, CString key) BOOL f_FileRemove(TConnection& conn, void* pJolly) { CString key; TSqlFile* pFile = Jolly2FileString(conn, pJolly, key); if (pFile) return conn.ReturnInteger(pFile->Remove(key)); return FALSE; } /////////////////////////////////////////////////////////// // int FileRewrite(int nHandle, CString key) BOOL f_FileRewrite(TConnection& conn, void* pJolly) { CString key; TSqlFile* pFile = Jolly2FileString(conn, pJolly, key); if (pFile) return conn.ReturnInteger(pFile->Rewrite(key)); return FALSE; } /////////////////////////////////////////////////////////// // int FileSetField(int nHandle, int field, CString value) // int FileSetField(int nHandle, CString field, CString value) BOOL f_FileSetField(TConnection& conn, void* pJolly) { CString strField; CString strValue; TSqlFile* pFile = Jolly2FileStringString(conn, pJolly, strField, strValue); if (pFile) { const int nIndex = atoi(strField); int err; if (nIndex > 0) err = pFile->SetField(nIndex, strValue); else err = pFile->SetField(strField, strValue); return conn.ReturnInteger(err); } return FALSE; } /////////////////////////////////////////////////////////// // int FileSetKey(int nHandle, int key) BOOL f_FileSetKey(TConnection& conn, void* pJolly) { int key; TSqlFile* pFile = Jolly2FileInt(conn, pJolly, key); if (pFile) { int nErr = pFile->SetKey(key); return conn.ReturnInteger(nErr); } return FALSE; } /////////////////////////////////////////////////////////// // int FileSkip(int nHandle, int pos) BOOL f_FileSkip(TConnection& conn, void* pJolly) { int nPos, nLocks; TSqlFile* pFile = Jolly2FileIntInt(conn, pJolly, nPos, nLocks); if (pFile) return conn.ReturnInteger(pFile->Skip(nPos, nLocks)); return FALSE; } /////////////////////////////////////////////////////////// // int FileUnlock(int nHandle) BOOL f_FileUnlock(TConnection& conn, void* pJolly) { TSqlFile* pFile = Jolly2File(conn, pJolly); if (pFile) return conn.ReturnInteger(pFile->ExclusiveUnlock()); return FALSE; } /////////////////////////////////////////////////////////// // int FileWrite(int nHandle) BOOL f_FileWrite(TConnection& conn, void* pJolly) { TSqlFile* pFile = Jolly2File(conn, pJolly); if (pFile) return conn.ReturnInteger(pFile->Write()); return FALSE; } /////////////////////////////////////////////////////////// // int FirmGet() BOOL f_FirmGet(TConnection& conn, void* pJolly) { TPrassiConnection& c = (TPrassiConnection&)conn; return c.ReturnInteger(c.GetFirm()); } /////////////////////////////////////////////////////////// // int FirmSet() BOOL f_FirmSet(TConnection& conn, void* pJolly) { int nFirm = Jolly2Int(pJolly); TPrassiConnection& c = (TPrassiConnection&)conn; return c.ReturnBool(c.SetFirm(nFirm)); } /////////////////////////////////////////////////////////// // int FirmTest() BOOL f_FirmTest(TConnection& conn, void* pJolly) { int nFirm = Jolly2Int(pJolly); TPrassiServer& s = GetServer(); return conn.ReturnBool(s.TestFirm(nFirm)); } /////////////////////////////////////////////////////////// // void* FtpGetFile(CString) BOOL f_FtpGetFile(TConnection& conn, void* pJolly) { CString filename; filename = Jolly2String(pJolly); FILE* f = fopen(filename, "rb"); if (f != NULL) { fseek(f, 0, SEEK_END); DWORD dwSize = ftell(f); BYTE* pData = conn.Server().GetBuffer(dwSize); fseek(f, 0, SEEK_SET); size_t r = fread(pData, dwSize, 1, f); fclose(f); } return f != NULL; }