1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 ***************************************************************************/
20 #include <AbsSqlStatement.h>
31 enum NetworkPacketType
39 SQL_NW_PKT_EXECDIRECT
=100,
40 SQL_NW_PKT_CONNECT
=101,
41 SQL_NW_PKT_PREPARE
=102,
42 SQL_NW_PKT_PARAM_METADATA
=103,
43 SQL_NW_PKT_PROJ_METADATA
=104,
44 SQL_NW_PKT_EXECUTE
=105,
46 SQL_NW_PKT_RESULT_SET
=107,
47 SQL_NW_PKT_COMMIT
=108,
48 SQL_NW_PKT_ROLLBACK
=109,
50 SQL_NW_PKT_DISCONNECT
=111,
51 SQL_NW_PKT_SHOWTABLES
=112,
52 SQL_NW_PKT_ISTABLEPRESENT
=113,
53 SQL_NW_PKT_GETRECORDS
=114,
54 SQL_NW_PKT_LOADRECORDS
=115,
55 SQL_NW_PKT_LASTAIVAL
=116,
63 stmtID
= 0; retVal
= 0; errRetVal
= OK
; isSelect
= false; rows
=0;
64 lastAutoIncVal
=0; errorString
[0] = '\0';
67 int retVal
; // will include for fetch end flag, params flag, proj flag
71 long long lastAutoIncVal
;
73 char errorString
[ERROR_STRING_LENGTH
];
75 DbRetVal
unmarshall();
80 char hostName
[IDENTIFIER_LENGTH
];
84 int responseTimeout
; //in secs
90 virtual DbRetVal
send( NetworkPacketType type
) =0;
91 virtual DbRetVal
send( NetworkPacketType type
, int stmtid
)=0;
92 virtual DbRetVal
send( NetworkPacketType type
, char *buf
, int len
)=0;
93 virtual DbRetVal
receive()=0;
94 virtual DbRetVal
connect()=0;
95 virtual DbRetVal
disconnect()=0;
96 virtual void * getResponsePacket()=0;
97 virtual ~NetworkClient(){}
98 void setHost(char *host
, int portno
, int nwid
)
100 strcpy(hostName
, host
);
104 int getNetworkID() { return networkid
; }
105 void setConnectionTimeout(int timeout
) { connectTimeout
=timeout
;}
106 void setResponseTimeout(int timeout
) { responseTimeout
=timeout
;}
107 void setEntryption(bool encr
) { encrypt
=encr
;}
108 void setConnectFlag(bool flag
) { isConnectedFlag
=flag
;}
109 bool isConnected() { return isConnectedFlag
; }
110 void setIsConnectedFlag(bool fl
) { isConnectedFlag
= fl
; }
111 void setCacheClient() { cacheClient
= true; }
112 bool isCacheClient() { return cacheClient
; }
114 class UDPClient
: public NetworkClient
{
117 struct sockaddr_in srvAddr
;
118 struct sockaddr_in fromAddr
;
119 UDPClient(){ isConnectedFlag
=false; cacheClient
= false;}
120 DbRetVal
send(NetworkPacketType type
);
121 DbRetVal
send(NetworkPacketType type
,int stmtid
);
122 DbRetVal
send(NetworkPacketType type
, char *buf
, int len
);
125 DbRetVal
disconnect();
126 void * getResponsePacket() { return NULL
; }
130 class TCPClient
: public NetworkClient
{
133 struct sockaddr_in srvAddr
;
134 ResponsePacket
*respPkt
;
135 PacketHeader
*pktHdr
;
137 DbRetVal
send(NetworkPacketType type
);
138 DbRetVal
send(NetworkPacketType type
, int stmtid
);
139 DbRetVal
send(NetworkPacketType type
, char *buf
, int len
);
142 DbRetVal
disconnect();
143 void * getResponsePacket() { return respPkt
; }
153 NetworkClient
* nwClient
;
155 NetworkTable() { nwClient
= NULL
; }
157 DbRetVal
initialize();
159 DbRetVal
readNetworkConfig();
160 NetworkClient
* getNetworkClient() { return nwClient
; }
162 DbRetVal
disconnect();
163 DbRetVal
connectIfNotConnected();
168 static NetworkClient
* createClient(NetworkMode mode
)
170 NetworkClient
* client
= NULL
;
174 client
= new UDPClient();
177 client
= new TCPClient();
192 /*class SqlPacketHeader
200 //TOTOD:: bool encrypt;
203 NetworkPacketType pktType
;
205 //should be called after successful marshall call
206 char* getMarshalledBuffer(){ return buffer
; }
207 int getBufferSize() { return bufferSize
; }
208 void setBuffer(char *buf
) { buffer
= buf
; }
209 void setBufferSize(int bufSize
) { bufferSize
= bufSize
; }
211 virtual DbRetVal
marshall()=0;
212 virtual DbRetVal
unmarshall()=0;
213 virtual ~BasePacket(){};
215 class PacketPrepare
:public BasePacket
218 PacketPrepare() { buffer
=NULL
; bufferSize
=0; noParams
= 0; stmtString
= NULL
;
219 stmtID
= 0; stmtLength
= 0; type
= NULL
; length
= NULL
;
220 pktType
= NW_PKT_PREPARE
;}
221 ~PacketPrepare() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
230 DbRetVal
unmarshall();
232 class PacketFree
: public BasePacket
235 PacketFree() { buffer
=NULL
; bufferSize
=0; stmtID
= 0; pktType
= NW_PKT_FREE
;}
236 ~PacketFree() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
239 DbRetVal
unmarshall();
242 class PacketExecute
: public BasePacket
245 PacketExecute() { buffer
=NULL
; bufferSize
=0;
246 stmtID
= 0; noParams
= 0; paramValues
= NULL
;
247 pktType
= NW_PKT_EXECUTE
;}
248 ~PacketExecute() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
249 //TODO::need to free paramvalues based on marshall or unmarshall
258 void setStatementList(List stmtlist
);
259 void setParams(List list
);
262 DbRetVal
unmarshall();
264 class PacketCommit
: public BasePacket
267 PacketCommit() { txnID
=0; noOfStmts
= 0; stmtBufSize
= NULL
; stmtBuffer
= NULL
;
268 buffer
= NULL
; bufferSize
= 0; pktType
= NW_PKT_COMMIT
; }
269 ~PacketCommit() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
274 void setExecPackets(int tid
, List list
);
275 void getExecPacketList(List stmtList
, List
&list
);
277 DbRetVal
unmarshall();
280 class SqlPacketConnect
: public BasePacket
285 strcpy(userName
, "");
286 strcpy(passWord
, "");
289 pktType
= SQL_NW_PKT_CONNECT
;
291 ~SqlPacketConnect() { free(buffer
); bufferSize
= 0; buffer
= NULL
;
293 char userName
[IDENTIFIER_LENGTH
];
294 char passWord
[IDENTIFIER_LENGTH
];
296 void setConnParam(char *user
, char *pass
, char tp
)
298 strcpy(userName
, user
);
299 strcpy(passWord
, pass
);
303 DbRetVal
unmarshall();
306 class SqlPacketPrepare
: public BasePacket
310 { buffer
=NULL
; bufferSize
=0;
311 stmtString
= NULL
; stmtLength
= 0; pktType
= SQL_NW_PKT_PREPARE
; }
312 ~SqlPacketPrepare() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
316 DbRetVal
unmarshall();
319 class SqlPacketExecute
: public BasePacket
324 //TODO::need to free paramvalues based on marshall or unmarshall
333 char *localBuf
[10]; //to store paramValues if noParams <10
335 void setStatementList(List stmtlist
);
336 void setParams(List list
);
337 void setNullInfo(char *nInfo
) { nullInfo
= nInfo
; }
338 char *getNullInfo() { return nullInfo
; }
340 DbRetVal
unmarshall();
343 class SqlPacketParamMetadata
: public BasePacket
346 SqlPacketParamMetadata()
347 { buffer
=NULL
; bufferSize
=0; noParams
= 0;
348 stmtID
= 0; data
= NULL
; pktType
= SQL_NW_PKT_PARAM_METADATA
;}
349 ~SqlPacketParamMetadata() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
354 DbRetVal
unmarshall();
357 class SqlPacketProjMetadata
: public BasePacket
360 SqlPacketProjMetadata() { buffer
=NULL
; bufferSize
=0; noProjs
= 0;
361 data
= NULL
; stmtID
= 0; pktType
= SQL_NW_PKT_PROJ_METADATA
; }
362 ~SqlPacketProjMetadata() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
367 DbRetVal
unmarshall();
370 class SqlPacketFetch
: public BasePacket
373 SqlPacketFetch() { buffer
=NULL
; bufferSize
= 0;
374 stmtID
= 0; pktType
= SQL_NW_PKT_FETCH
; }
375 ~SqlPacketFetch() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
378 DbRetVal
unmarshall();
381 class SqlPacketFree
: public BasePacket
384 SqlPacketFree() { buffer
=NULL
; stmtID
=0;bufferSize
= 0;
385 pktType
= SQL_NW_PKT_FREE
; }
386 ~SqlPacketFree() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
389 DbRetVal
unmarshall();
392 class SqlPacketResultSet
: public BasePacket
395 SqlPacketResultSet() { buffer
=NULL
; bufferSize
= 0; noProjs
= 0;
396 nullInfo
= NULL
; projValues
=NULL
;
397 hasData
=0; nullInfoLen
=0;
398 pktType
= SQL_NW_PKT_RESULT_SET
; }
399 ~SqlPacketResultSet() { free(buffer
);
400 bufferSize
= 0; buffer
= NULL
;
401 if (projValues
) delete[] projValues
;
409 void setNullInfo(char *info
){ nullInfo
= info
; }
410 void setProjList(List list
);
412 DbRetVal
unmarshall();
415 class SqlPacketShowTables
: public BasePacket
418 SqlPacketShowTables() { buffer
= NULL
; bufferSize
= 0; data
= NULL
;
419 numOfTables
=0; pktType
= SQL_NW_PKT_SHOWTABLES
; }
420 ~SqlPacketShowTables() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
424 DbRetVal
unmarshall();
427 class SqlPacketIsTablePresent
: public BasePacket
430 SqlPacketIsTablePresent() { buffer
= NULL
; bufferSize
= 0;
431 tblName
[0] = '\0'; pktType
= SQL_NW_PKT_ISTABLEPRESENT
; }
432 ~SqlPacketIsTablePresent() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
433 char tblName
[IDENTIFIER_LENGTH
];
434 void setTableName(char *tName
) { strcpy(tblName
, tName
); }
436 DbRetVal
unmarshall();
439 class SqlPacketGetRecords
: public BasePacket
442 SqlPacketGetRecords() { buffer
= NULL
; bufferSize
= 0; pages
=0;
443 tblName
[0] = '\0'; pktType
= SQL_NW_PKT_GETRECORDS
; }
444 ~SqlPacketGetRecords() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
445 char tblName
[IDENTIFIER_LENGTH
];
447 void setTableName(char *tName
) { strcpy(tblName
, tName
); }
448 void setPages(int pgs
) { pages
= pgs
; }
450 DbRetVal
unmarshall();
453 class SqlPacketLoadRecords
: public BasePacket
456 SqlPacketLoadRecords() { buffer
= NULL
; bufferSize
= 0; pages
= 0;
457 pktType
= SQL_NW_PKT_LOADRECORDS
; }
458 ~SqlPacketLoadRecords() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
460 void setPages(int pgs
) { pages
= pgs
; }
462 DbRetVal
unmarshall();
471 AbsSqlStatement
*stmt
;
474 List tableNamesList
; // will be populated only for show tables query
475 NetworkStmt() { srcNetworkID
=0; stmtID
=0; stmt
=NULL
; type
= UnknownStatement
; }
484 NetworkServer() { port
=0; sockfd
= -1;}
485 void setServerPort(int p
) { port
= p
; }
486 int getSocket(){ return sockfd
; }
487 virtual DbRetVal
start()=0;
488 virtual DbRetVal
stop()=0;
489 virtual DbRetVal
handleClient()=0;
492 class UDPServer
: public NetworkServer
494 struct sockaddr_in clientAddress
;
496 UDPServer() { port
= 0; sockfd
= -1; }
499 DbRetVal
handleClient();
502 class TCPServer
: public NetworkServer
505 struct sockaddr_in clientAddress
;
507 TCPServer() { port
= 0; sockfd
= -1; clientfd
= -1;}
510 DbRetVal
handleClient();