From b9e68a8a9fff6552cbe1e0b3e48d505d74881ea0 Mon Sep 17 00:00:00 2001 From: prabatuty Date: Sat, 19 Mar 2011 16:30:06 +0000 Subject: [PATCH] changes for windows jdbc driverw --- include/Network.h | 6 ++++-- include/SqlNetworkHandler.h | 5 ++++- include/SqlNwStatement.h | 3 ++- include/os.h | 1 + src/network/NetworkPacket.cxx | 15 +++++++++---- src/network/TCPClient.cxx | 43 +++++++++++++++++++++++++------------- src/sql/SqlFactory.cxx | 25 ++++++++++++++++++---- src/sqlnetwork/SqlNwConnection.cxx | 9 ++++---- src/sqlnetwork/SqlNwStatement.cxx | 22 +++++++++---------- src/storage/os.cxx | 14 ++++++++++--- 10 files changed, 98 insertions(+), 45 deletions(-) diff --git a/include/Network.h b/include/Network.h index cc475b85..487d2fe6 100644 --- a/include/Network.h +++ b/include/Network.h @@ -68,9 +68,10 @@ class ResponsePacket DbRetVal errRetVal; int stmtID; int rows; - long long lastAutoIncVal; bool isSelect; char errorString[ERROR_STRING_LENGTH]; + long lastAutoIncVal; + long lastAutoIncVal1; //Added for VC Windows to work. For long long , it pads another 4 bytes to structure DbRetVal marshall(); DbRetVal unmarshall(); }; @@ -129,6 +130,7 @@ class UDPClient : public NetworkClient{ struct PacketHeader; class TCPClient : public NetworkClient{ public: + WSADATA wsaData; int sockfd; struct sockaddr_in srvAddr; ResponsePacket *respPkt; @@ -157,7 +159,7 @@ class NetworkTable DbRetVal initialize(); void destroy(){} DbRetVal readNetworkConfig(); - NetworkClient* getNetworkClient() { return nwClient; } + NetworkClient* getetworkClient() { return nwClient; } DbRetVal connect(); DbRetVal disconnect(); DbRetVal connectIfNotConnected(); diff --git a/include/SqlNetworkHandler.h b/include/SqlNetworkHandler.h index 2df08541..44d27d3f 100644 --- a/include/SqlNetworkHandler.h +++ b/include/SqlNetworkHandler.h @@ -22,14 +22,17 @@ #include #include #include -#include +#include + class SqlNetworkHandler { DbRetVal applyExecPackets(List sList, List pList); public: +#ifndef CSQL_CLIENT_LIB static List stmtList; static List tableNameList; static AbsSqlConnection *conn; +#endif static SqlApiImplType type; static int stmtID; static int sockfd; diff --git a/include/SqlNwStatement.h b/include/SqlNwStatement.h index a988f941..fa5855b7 100644 --- a/include/SqlNwStatement.h +++ b/include/SqlNwStatement.h @@ -27,7 +27,8 @@ class DllExport SqlNwStatement: public AbsSqlStatement { public: - SqlNwStatement(){innerStmt = NULL; con = NULL; isPrepared=false; isSel = true; nullInfoDml = NULL; nullInfoSel = NULL; } + SqlNwStatement(){innerStmt = NULL; con = NULL; isPrepared=false; + isSel = true; nullInfoDml = NULL; nullInfoSel = NULL; } void setConnection(AbsSqlConnection *conn) { if (innerStmt) innerStmt->setConnection(conn->getInnerConnection()); diff --git a/include/os.h b/include/os.h index f4ffc9c8..6f1a407b 100644 --- a/include/os.h +++ b/include/os.h @@ -230,6 +230,7 @@ class DllExport os static int close(int fd); static file_desc openFile(const char *name, FileOpenMode flags, size_t size); static int closeFile(file_desc fd); + static int closeSocket(int fd); static int lockFile(int fd); static int unlockFile(int fd); static int truncate(const char* fname); diff --git a/src/network/NetworkPacket.cxx b/src/network/NetworkPacket.cxx index bea718d4..ca5e5980 100644 --- a/src/network/NetworkPacket.cxx +++ b/src/network/NetworkPacket.cxx @@ -21,10 +21,11 @@ #include #include #include -#include +//#include #include +#ifndef CSQL_CLIENT_LIB #include - +#endif DbRetVal PacketPrepare::marshall() { printDebug(DM_Network, "PacketPrepare::marshall called\n"); @@ -431,6 +432,7 @@ DbRetVal SqlPacketExecute::unmarshall() DbRetVal SqlPacketParamMetadata::marshall() { +#ifndef CSQL_CLIENT_LIB printDebug(DM_Network, "SqlPacketParamMetadata::marshall called\n"); bufferSize = sizeof(int) * 2; printDebug(DM_Network, "NOOFPARAMS %d buffer size %d\n", noParams, bufferSize); @@ -466,7 +468,8 @@ DbRetVal SqlPacketParamMetadata::marshall() } delete fldInfo; printDebug(DM_Network, "SqlPacketParamMetadata::marshall ended\n"); - return OK; +#endif + return OK; } DbRetVal SqlPacketParamMetadata::unmarshall() @@ -484,6 +487,7 @@ DbRetVal SqlPacketParamMetadata::unmarshall() DbRetVal SqlPacketProjMetadata::marshall() { +#ifndef CSQL_CLIENT_LIB printDebug(DM_Network, "SqlPacketParamMetadata::marshall called\n"); bufferSize = sizeof(int) * 2; printDebug(DM_Network, "NOOFPROJS %d buffer size %d\n", noProjs, bufferSize); @@ -520,7 +524,8 @@ DbRetVal SqlPacketProjMetadata::marshall() } delete fldInfo; printDebug(DM_Network, "SqlPacketParamMetadata::marshall ended\n"); - return OK; +#endif + return OK; } DbRetVal SqlPacketProjMetadata::unmarshall() @@ -609,6 +614,7 @@ DbRetVal SqlPacketResultSet::unmarshall() DbRetVal SqlPacketShowTables::marshall() { +#ifndef CSQL_CLIENT_LIB bufferSize = numOfTables * IDENTIFIER_LENGTH; buffer = (char*) malloc(bufferSize); char *bufIter = buffer; @@ -619,6 +625,7 @@ DbRetVal SqlPacketShowTables::marshall() strncpy(bufIter, elem->name, IDENTIFIER_LENGTH); bufIter += IDENTIFIER_LENGTH; } +#endif return OK; } diff --git a/src/network/TCPClient.cxx b/src/network/TCPClient.cxx index 87c9a667..4e507f82 100644 --- a/src/network/TCPClient.cxx +++ b/src/network/TCPClient.cxx @@ -56,7 +56,7 @@ DbRetVal TCPClient::send(NetworkPacketType type) return ErrNoConnection; } printError(ErrOS, "Unable to send the packet"); - os::close(sockfd); + os::closeSocket(sockfd); sockfd = -1; isConnectedFlag = false; return ErrNoConnection; @@ -79,10 +79,11 @@ DbRetVal TCPClient::send(NetworkPacketType type, int stmtid) if (errno == EPIPE) { printError(ErrNoConnection, "connection not present"); isConnectedFlag = false; + os::closeSocket(sockfd); return ErrNoConnection; } printError(ErrOS, "Unable to send the packet"); - close(sockfd); + os::closeSocket(sockfd); sockfd = -1; isConnectedFlag = false; return ErrNoConnection; @@ -110,10 +111,11 @@ DbRetVal TCPClient::send(NetworkPacketType type, char *buf, int len) if (errno == EPIPE) { printError(ErrNoConnection, "connection not present"); isConnectedFlag = false; + os::closeSocket(sockfd); return ErrNoConnection; } printError(ErrOS, "Unable to send the packet"); - close(sockfd); + os::closeSocket(sockfd); sockfd = -1; isConnectedFlag = false; return ErrNoConnection; @@ -123,10 +125,11 @@ DbRetVal TCPClient::send(NetworkPacketType type, char *buf, int len) if (errno == EPIPE) { printError(ErrNoConnection, "connection not present"); isConnectedFlag = false; + os::closeSocket(sockfd); return ErrNoConnection; } printError(ErrOS, "Unable to send the packet"); - close(sockfd); + os::closeSocket(sockfd); sockfd = -1; isConnectedFlag = false; return ErrNoConnection; @@ -139,7 +142,7 @@ DbRetVal TCPClient::send(NetworkPacketType type, char *buf, int len) DbRetVal TCPClient::receive() { DbRetVal rv = OK; -// printf("NW:TCP receive\n"); + printDebug(DM_Network, "NW:TCP receive\n"); fd_set fdset; FD_ZERO(&fdset); FD_SET(sockfd, &fdset); @@ -149,17 +152,19 @@ DbRetVal TCPClient::receive() int ret = os::select(sockfd+1, &fdset, 0, 0, &timeout); if (ret <= 0) { printError(ErrPeerTimeOut,"Response timeout for peer site"); - os::close(sockfd); + os::closeSocket(sockfd); sockfd = -1; isConnectedFlag = false; return ErrPeerTimeOut; } socklen_t len = sizeof(struct sockaddr); + printDebug(DM_Network, "Sizeof response packet %d\n", sizeof(ResponsePacket)); int numbytes = os::recv(sockfd, respPkt, sizeof(ResponsePacket), 0); + printDebug(DM_Network, " NUMBYTES packet %d\n", numbytes); if (numbytes == -1) { printError(ErrOS, "Unable to receive response from peer"); - os::close(sockfd); + os::closeSocket(sockfd); sockfd = -1; isConnectedFlag = false; return ErrNoConnection; @@ -169,10 +174,16 @@ DbRetVal TCPClient::receive() DbRetVal TCPClient::connect() { - //printf("NW:TCP connect %s %d %d\n", hostName, port, networkid); + printDebug(DM_Network, "NW:TCP connect %s %d %d\n", hostName, port, networkid); //TODO::send endian to the peer site //do not do endian conversion here. it will be done at the server side - isConnectedFlag = false; + int iResult=0; + iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); + if (iResult != 0) { + printf("WSAStartup failed: %d\n", iResult); + return ErrSysInit; + } + isConnectedFlag = false; struct hostent *he; int numbytes; if ((he=gethostbyname(hostName)) == NULL) { // get the host info @@ -213,8 +224,7 @@ DbRetVal TCPClient::connect() printError(ErrOS, "Unable to receive response from peer"); return ErrOS; } -// printf("Response from peer site is %d\n", response); -// if (response != 1) return ErrPeerResponse; + // if (response != 1) return ErrPeerResponse; //packet header information pktHdr->srcNetworkID = networkid; @@ -226,26 +236,29 @@ DbRetVal TCPClient::connect() DbRetVal TCPClient::disconnect() { if (isConnectedFlag) { -// printf("NW:TCP disconnect %s %d\n", hostName, port); pktHdr->packetType = SQL_NW_PKT_DISCONNECT; pktHdr->packetLength = 0; int numbytes=0; if ((numbytes=os::send(sockfd, pktHdr, sizeof(PacketHeader), MSG_NOSIGNAL)) == -1) { if (errno == EPIPE) { printError(ErrNoConnection, "connection not present"); + os::closeSocket(sockfd); + WSACleanup(); isConnectedFlag = false; return ErrNoConnection; } printError(ErrOS, "Unable to send the packet"); - close(sockfd); + os::closeSocket(sockfd); + WSACleanup(); sockfd = -1; isConnectedFlag = false; return ErrNoConnection; } else { -// printf("Sent bytes %d\n", numbytes); + printDebug(DM_Network,"Sent bytes %d\n", numbytes); DbRetVal rv = receive(); isConnectedFlag = false; - close(sockfd); + os::closeSocket(sockfd); + WSACleanup(); sockfd = -1; } } diff --git a/src/sql/SqlFactory.cxx b/src/sql/SqlFactory.cxx index 0e5cdab9..158bfe24 100644 --- a/src/sql/SqlFactory.cxx +++ b/src/sql/SqlFactory.cxx @@ -14,6 +14,7 @@ * * ***************************************************************************/ #include +#ifndef CSQL_CLIENT_LIB #include #include #include @@ -21,10 +22,12 @@ #include #include #include +#endif #include #include - +#ifndef CSQL_CLIENT_LIB Config Conf::config; +#endif AbsSqlConnection* SqlFactory::createConnection(SqlApiImplType implFlag) { @@ -42,6 +45,7 @@ AbsSqlConnection* SqlFactory::createConnection(SqlApiImplType implFlag) #endif switch(implFlag) { +#ifndef CSQL_CLIENT_LIB case CSql: if (!isSqlLogNeeded) conn = new SqlConnection(); else { @@ -55,6 +59,7 @@ AbsSqlConnection* SqlFactory::createConnection(SqlApiImplType implFlag) conn->setInnerConnection(sqlCon); } break; +#endif case CSqlNetwork: { SqlNwConnection *sqlNwCon = new SqlNwConnection(CSqlNetwork); @@ -62,6 +67,7 @@ AbsSqlConnection* SqlFactory::createConnection(SqlApiImplType implFlag) conn = sqlNwCon; break; } +#ifndef CSQL_CLIENT_LIB case CSqlDirect: conn = new SqlConnection(); break; @@ -73,7 +79,9 @@ AbsSqlConnection* SqlFactory::createConnection(SqlApiImplType implFlag) conn->setInnerConnection(sqlCon); break; } +#endif #ifndef MMDB +#ifndef CSQL_CLIENT_LIB case CSqlAdapter: { conn = new SqlOdbcConnection(); @@ -106,6 +114,7 @@ AbsSqlConnection* SqlFactory::createConnection(SqlApiImplType implFlag) conn = gwconn; break; } +#endif case CSqlNetworkAdapter: { SqlNwConnection *sqlNwCon = new SqlNwConnection(CSqlNetworkAdapter); @@ -142,7 +151,8 @@ AbsSqlStatement* SqlFactory::createStatement(SqlApiImplType implFlag) #endif switch(implFlag) { - case CSql: +#ifndef CSQL_CLIENT_LIB + case CSql: if (!isSqlLogNeeded) stmt = new SqlStatement(); else { AbsSqlStatement *sqlStmt = new SqlStatement(); @@ -150,6 +160,7 @@ AbsSqlStatement* SqlFactory::createStatement(SqlApiImplType implFlag) stmt->setInnerStatement(sqlStmt); } break; +#endif case CSqlNetwork: { SqlNwStatement *sqlNwStmt = new SqlNwStatement(); @@ -157,6 +168,8 @@ AbsSqlStatement* SqlFactory::createStatement(SqlApiImplType implFlag) stmt = sqlNwStmt; break; } +#ifndef CSQL_CLIENT_LIB + case CSqlDirect: stmt = new SqlStatement(); break; @@ -168,8 +181,11 @@ AbsSqlStatement* SqlFactory::createStatement(SqlApiImplType implFlag) stmt->setInnerStatement(sqlStmt); break; } +#endif #ifndef MMDB - case CSqlAdapter: +#ifndef CSQL_CLIENT_LIB + + case CSqlAdapter: { stmt = new SqlOdbcStatement(); stmt->setInnerStatement(NULL); @@ -190,7 +206,8 @@ AbsSqlStatement* SqlFactory::createStatement(SqlApiImplType implFlag) stmt = gwstmt; break; } - case CSqlNetworkAdapter: +#endif + case CSqlNetworkAdapter: { SqlNwStatement *sqlNwStmt = new SqlNwStatement(); sqlNwStmt->setInnerStatement(NULL); diff --git a/src/sqlnetwork/SqlNwConnection.cxx b/src/sqlnetwork/SqlNwConnection.cxx index 3f35c2a8..9cf89873 100644 --- a/src/sqlnetwork/SqlNwConnection.cxx +++ b/src/sqlnetwork/SqlNwConnection.cxx @@ -46,7 +46,8 @@ DbRetVal SqlNwConnection::connect (char *user, char * pass) pkt->setBuffer(buffer); pkt->marshall(); rv = nwClient->send(SQL_NW_PKT_CONNECT, buffer, bufsize); - if (rv != OK) { + printDebug(DM_Network, "DEBUG:::NWCONNECT send connect pkt %d\n", rv); + if (rv != OK) { printError(rv, "Data could not be sent"); delete pkt; delete nwClient; @@ -54,7 +55,8 @@ DbRetVal SqlNwConnection::connect (char *user, char * pass) return rv; } rv = nwClient->receive(); - if (rv == ErrPeerTimeOut) { + printDebug(DM_Network, "DEBUG:::NWCONNECT receive connect pkt %d \n", rv); + if (rv == ErrPeerTimeOut) { delete pkt; delete nwClient; nwClient = NULL; @@ -67,7 +69,7 @@ DbRetVal SqlNwConnection::connect (char *user, char * pass) nwClient = NULL; return rpkt->errRetVal; } - isConnOpen = true; + isConnOpen = true; delete pkt; return OK; } @@ -132,5 +134,4 @@ DbRetVal SqlNwConnection::rollback() return ErrNoConnection; } return rv; - } diff --git a/src/sqlnetwork/SqlNwStatement.cxx b/src/sqlnetwork/SqlNwStatement.cxx index c2b993aa..6e3097ba 100644 --- a/src/sqlnetwork/SqlNwStatement.cxx +++ b/src/sqlnetwork/SqlNwStatement.cxx @@ -105,19 +105,19 @@ DbRetVal SqlNwStatement::prepare(char *stmtstr) if (numbytes == -1) { printError(ErrOS, "Error reading from socket\n"); conn->setConnClosed(false); - os::close(fd); + os::closeSocket(fd); ((TCPClient *)(conn->nwClient))->sockfd = -1; conn->setIsConnectedFlag(false); delete pkt; return ErrNoConnection; } -// printf("HEADER says packet type is %d\n", header.packetType); + printDebug(DM_Network, "HEADER says packet type is %d\n", header.packetType); buffer = (char*) malloc(header.packetLength); numbytes = os::recv(fd,buffer,header.packetLength,0); if (numbytes == -1) { printError(ErrOS, "Error reading from socket\n"); conn->setConnClosed(false); - os::close(fd); + os::closeSocket(fd); ((TCPClient *)(conn->nwClient))->sockfd = -1; conn->setIsConnectedFlag(false); delete pkt; @@ -165,7 +165,7 @@ DbRetVal SqlNwStatement::prepare(char *stmtstr) if (numbytes == -1) { printError(ErrOS, "Error reading from socket\n"); conn->setConnClosed(false); - os::close(fd); + os::closeSocket(fd); ((TCPClient *)(conn->nwClient))->sockfd = -1; conn->setIsConnectedFlag(false); delete pkt; @@ -177,7 +177,7 @@ DbRetVal SqlNwStatement::prepare(char *stmtstr) if (numbytes == -1) { printError(ErrOS, "Error reading from socket\n"); conn->setConnClosed(false); - os::close(fd); + os::closeSocket(fd); ((TCPClient *)(conn->nwClient))->sockfd = -1; conn->setIsConnectedFlag(false); ::free (buffer); @@ -305,7 +305,7 @@ void* SqlNwStatement::fetch(DbRetVal &ret) if (numbytes == -1) { printError(ErrOS, "Connection lost with peer\n"); conn->setConnClosed(false); - os::close(fd); + os::closeSocket(fd); ((TCPClient *)(conn->nwClient))->sockfd = -1; conn->setIsConnectedFlag(false); ret = ErrNoConnection; @@ -318,7 +318,7 @@ void* SqlNwStatement::fetch(DbRetVal &ret) if (numbytes == -1) { printError(ErrOS, "Connection lost with peer\n"); conn->setConnClosed(false); - os::close(fd); + os::closeSocket(fd); ((TCPClient *)(conn->nwClient))->sockfd = -1; conn->setIsConnectedFlag(false); ret = ErrNoConnection; @@ -651,7 +651,7 @@ List SqlNwStatement::getAllTableNames(DbRetVal &ret) if (numbytes == -1) { printError(ErrOS, "Connection lost with peer."); conn->setConnClosed(false); - os::close(fd); + os::closeSocket(fd); ((TCPClient *)(conn->nwClient))->sockfd = -1; conn->setIsConnectedFlag(false); ret = ErrNoConnection; @@ -663,7 +663,7 @@ List SqlNwStatement::getAllTableNames(DbRetVal &ret) if (numbytes == -1) { printError(ErrOS, "Connection lost with peer."); conn->setConnClosed(false); - os::close(fd); + os::closeSocket(fd); ((TCPClient *)(conn->nwClient))->sockfd = -1; conn->setIsConnectedFlag(false); ret = ErrNoConnection; @@ -756,7 +756,7 @@ void *SqlNwStatement::getLoadedRecords(char *tblName, DbRetVal &ret) if (numbytes == -1) { printError(ErrOS, "Connection lost with peer"); conn->setConnClosed(false); - os::close(fd); + os::closeSocket(fd); ((TCPClient *)(conn->nwClient))->sockfd = -1; conn->setIsConnectedFlag(false); ret = ErrNoConnection; @@ -768,7 +768,7 @@ void *SqlNwStatement::getLoadedRecords(char *tblName, DbRetVal &ret) if (numbytes == -1) { printError(ErrOS, "Connection lost with peer"); conn->setConnClosed(false); - os::close(fd); + os::closeSocket(fd); ((TCPClient *)(conn->nwClient))->sockfd = -1; conn->setIsConnectedFlag(false); ret = ErrNoConnection; diff --git a/src/storage/os.cxx b/src/storage/os.cxx index 218d1bf9..3dd44493 100644 --- a/src/storage/os.cxx +++ b/src/storage/os.cxx @@ -98,7 +98,7 @@ void* os::shm_attach(shared_memory_id id, const void *ptr, int flag) #ifdef WINNT char fName[MAX_FILE_PATH_LEN]; sprintf(fName, "SHM_ID_%d", id); - HANDLE hFile = ::OpenFileMapping(FILE_MAP_ALL_ACCESS,false, fName); + HANDLE hFile = ::OpenFileMapping(FILE_MAP_ALL_ACCESS,false, (LPCWSTR)fName); if (hFile == NULL) { printf("unable to do mmap\n"); @@ -179,12 +179,20 @@ int os::close(int fd) { return ::close(fd); } +int os::closeSocket(int fd) +{ +#ifdef WINNT + return ::closesocket(fd); +#else + return ::close(fd); +#endif +} file_desc os::openFile(const char *name, FileOpenMode flags, size_t size) { #ifdef WINNT - HANDLE hFile = CreateFile(name, GENERIC_READ | GENERIC_WRITE, + HANDLE hFile = CreateFile((LPCWSTR)name, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) @@ -398,7 +406,7 @@ pid_t os::createProcess(const char* cmdName, const char* execName) sinfo.cb = sizeof(sinfo); ZeroMemory( &pinfo, sizeof(pinfo) ); - if (!::CreateProcess(cmdName, NULL, NULL, NULL, FALSE, 0, NULL, NULL, + if (!::CreateProcess((LPCWSTR)cmdName, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &sinfo, &pinfo)) { return -1; -- 2.11.4.GIT