1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // CegoDbHandler.cc
4 // -----------------
5 // Cego db handler class implementation
6 //
7 // Design and Implementation by Bjoern Lemke
8 //
9 // (C)opyright 2000-2019 Bjoern Lemke
10 //
11 // IMPLEMENTATION MODULE
12 //
13 // Class: CegoDbHandler
14 //
15 // Description: Database handler class to the access the database backend via network
16 //
17 // Status: CLEAN
18 //
19 ///////////////////////////////////////////////////////////////////////////////
20 
21 // LFC INCLUDES
22 #include <lfcbase/AESCrypt.h>
23 #include <lfcbase/Datetime.h>
24 #include <lfcxml/Element.h>
25 
26 // CEGO INCLUDES
27 #include "CegoTypeConverter.h"
28 #include "CegoDbHandler.h"
29 #include "CegoXMLdef.h"
30 #include "CegoXMLHelper.h"
31 #include "CegoDefs.h"
32 
33 // POSIX INCLUDES
34 #include <string.h>
35 #include <stdlib.h>
36 
37 #define QUERY_MOREDATA 0
38 #define QUERY_ABORT 1
39 #define QUERY_RESET 2
40 
41 // native requests
42 #define SER_OK "ok"
43 #define SER_SACK "sac"
44 #define SER_ERROR "err"
45 #define SER_INFO "inf"
46 #define SER_SDATA "sdt"
47 #define SER_FDATA "fdt"
48 #define SER_FIN "fin"
49 #define SER_PRODINFO "pci"
50 #define SER_PROCRES "pcr"
51 #define SER_SESSION "ses"
52 #define SER_QUERY "qry"
53 #define SER_QUERYABORT "abt"
54 #define SER_DBPRODINFO "dpi"
55 
56 #define SER_BLOBINFO "bli"
57 #define SER_BLOBSIZE "bls"
58 #define SER_PUTBLOB "blp"
59 #define SER_GETBLOB "blg"
60 #define SER_DELBLOB "bld"
61 
62 #define SER_CLOBINFO "cli"
63 #define SER_CLOBSIZE "cls"
64 #define SER_PUTCLOB "clp"
65 #define SER_GETCLOB "clg"
66 #define SER_DELCLOB "cld"
67 
68 // distributed requests
69 #define SER_CREATETABLE "createtable"
70 #define SER_CREATEVIEW "creatview"
71 #define SER_CREATEPROCEDURE "createproc"
72 #define SER_CREATECHECK "createcheck"
73 #define SER_ALTERTABLE "altertable"
74 #define SER_CREATEINDEX "createindex"
75 #define SER_INSERT "insert"
76 #define SER_DELETE "delete"
77 #define SER_UPDATE "update"
78 #define SER_OBJECTINFO "objinfo"
79 #define SER_DROP_OBJECT "dropobj"
80 #define SER_GETTABLE "gettable"
81 #define SER_GETOBJLIST "getoblist"
82 #define SER_GETOBJLISTBYTABLE "getobjlistbytable"
83 #define SER_OBJRENAME "objrename"
84 #define SER_REORG "reorg"
85 #define SER_SYNC "sync"
86 #define SER_GETPAGECOUNT "getpgcount"
87 #define SER_STARTTRANSACTION "stta"
88 #define SER_COMMITTRANSACTION "ctta"
89 #define SER_ROLLBACKTRANSACTION "rbta"
90 #define SER_GETTID "gettid"
91 #define SER_SESSION_CLOSE "sesclose"
92 
CegoDbHandler(NetHandler * pN,CegoDbHandler::ProtocolType pt,CegoModule * pModule)93 CegoDbHandler::CegoDbHandler(NetHandler *pN, CegoDbHandler::ProtocolType pt, CegoModule *pModule)
94 {
95     _pN = pN;
96     _pModule = pModule;
97     _pRow = 0;
98 
99     _protType = pt;
100 
101     if ( _protType == CegoDbHandler::XML )
102     {
103 	Document *pDoc = new Document(XML_CEGO);
104 	pDoc->setAttribute(XML_VERSION_ATTR, XML_VERSION_VALUE);
105 	_xml.setDocument(pDoc);
106     }
107     else if ( _protType == CegoDbHandler::SERIAL )
108     {
109 	_pSer = new CegoSerial(_pN, false);
110     }
111     else // if ( _protType == CegoDbHandler::FASTSERIAL )
112     {
113 	_pSer = new CegoSerial(_pN, true);
114     }
115 
116     _wasReset = false;
117     _modId = _pModule->getModId("CegoDbHandler");
118 }
119 
~CegoDbHandler()120 CegoDbHandler::~CegoDbHandler()
121 {
122     if ( _protType == CegoDbHandler::XML )
123     {
124 	Document *pDoc = _xml.getDocument();
125 	pDoc->clear();
126 	delete pDoc;
127     }
128     else
129     {
130 	if ( _pSer )
131 	    delete _pSer;
132     }
133 }
134 
requestSession(const Chain & tableSet,const Chain & user,const Chain & password,bool doEncrypt)135 CegoDbHandler::ResultType CegoDbHandler::requestSession(const Chain& tableSet, const Chain& user, const Chain& password, bool doEncrypt)
136 {
137 #ifdef CGDEBUG
138     _pModule->log(_modId, Logger::DEBUG, Chain("Request session for user ") + user + Chain("/") + password);
139 #endif
140 
141     if ( _protType == CegoDbHandler::XML )
142     {
143 	Element* pRoot = new Element(XML_FRAME_ELEMENT);
144 
145 	pRoot->setAttribute(XML_TABLESET_ATTR, tableSet);
146 	pRoot->setAttribute(XML_USER_ATTR, user);
147 
148 	_tableSet = tableSet;
149 
150 	if ( doEncrypt )
151 	{
152 	    AESCrypt aescrypt(CEGOAESKEY, CEGOAESKEYLEN);
153 	    pRoot->setAttribute(XML_PASSWD_ATTR, aescrypt.encrypt(password));
154 	}
155 	else
156 	{
157 	    pRoot->setAttribute(XML_PASSWD_ATTR, password);
158 	}
159 	return sendXMLReq(XML_DBSESSION_REQUEST, pRoot);
160     }
161     else
162     {
163 	_tableSet = tableSet;
164 
165 	Chain pwd;
166 	if ( doEncrypt )
167 	{
168 	    AESCrypt aescrypt(CEGOAESKEY, CEGOAESKEYLEN);
169 	    pwd =  aescrypt.encrypt(password);
170 	}
171 	else
172 	{
173 	    pwd = password;
174 	}
175 
176 	_pSer->reset();
177 	_pSer->writeChain(Chain(SER_SESSION));
178 	_pSer->writeChain(tableSet);
179 	_pSer->writeChain(user);
180 	_pSer->writeChain(pwd);
181 
182 	return sendSerialReq();
183     }
184 }
185 
closeSession()186 CegoDbHandler::ResultType CegoDbHandler::closeSession()
187 {
188     if ( _protType == CegoDbHandler::XML )
189     {
190 	Element* pRoot = new Element(XML_FRAME_ELEMENT);
191 	return sendXMLReq(XML_SESSION_CLOSE, pRoot);
192     }
193     else
194     {
195 	_pSer->reset();
196 	_pSer->writeChain(Chain(SER_SESSION_CLOSE));
197 	return sendSerialReq();
198     }
199 }
200 
acceptSession()201 bool CegoDbHandler::acceptSession()
202 {
203 #ifdef CGDEBUG
204     _pModule->log(_modId, Logger::DEBUG, Chain("Accepting session"));
205 #endif
206 
207     try {
208 
209 	if ( _protType == CegoDbHandler::XML )
210 	{
211 	    _xml.getDocument()->clear();
212 	    _xml.setChain( _pN->getMsg() );
213 #ifdef CGDEBUG
214 	    _pModule->log(_modId, Logger::DEBUG, _pN->getMsg());
215 #endif
216 	    Chain docType;
217 
218 	    try
219 	    {
220 		_xml.parse();
221 		docType = _xml.getDocument()->getDocType();
222 	    }
223 	    catch ( Exception e )
224 	    {
225 		docType = Chain("ERROR");
226 	    }
227 #ifdef CGDEBUG
228 	    _pModule->log(_modId, Logger::DEBUG, Chain("XML parse ok"));
229 #endif
230 
231 	    if ( docType != Chain(XML_DBSESSION_REQUEST) )
232 	    {
233 
234 		_pModule->log(_modId, Logger::LOGERR, Chain("Invalid request"));
235 
236 		_xml.getDocument()->clear();
237 
238 		Element* pRoot = new Element(XML_FRAME_ELEMENT);
239 		pRoot->setAttribute(XML_MSG_ATTR, Chain("Invalid request"));
240 
241 		_xml.getDocument()->setRootElement(pRoot);
242 		_xml.getDocument()->setDocType(XML_ERROR_DOC);
243 
244 		Chain request;
245 		_xml.getXMLChain(request);
246 
247 		_pN->setMsg(request, request.length());
248 
249 		_pN->writeMsg();
250 
251 		return false;
252 	    }
253 	    else
254 	    {
255 		Element *pRoot = _xml.getDocument()->getRootElement();
256 		if ( pRoot )
257 		{
258 		    _tableSet = pRoot->getAttributeValue(XML_TABLESET_ATTR);
259 		    _user = pRoot->getAttributeValue(XML_USER_ATTR);
260 		    _password = pRoot->getAttributeValue(XML_PASSWD_ATTR);
261 
262 		    // response is sent after authorization check
263 
264 		    return true;
265 		}
266 		else
267 		{
268 		    throw Exception(EXLOC, "Cannot get root element from message");
269 		}
270 	    }
271 	}
272 	else
273 	{
274 #ifdef CGDEBUG
275 	    if ( _protType == CegoDbHandler::SERIAL )
276 	    {
277 		_pModule->log(_modId, Logger::DEBUG, Chain("--- SER ---"));
278 		_pModule->log(_modId, Logger::DEBUG, Chain(_pN->getMsg(), _pN->getMsgSize()));
279 		_pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
280 	    }
281 #endif
282 	    _pSer->reset();
283 	    Chain req = _pSer->readChain();
284 
285 	    // cout << "READING REQ = " << req << endl;
286 
287 	    if ( req != Chain(SER_SESSION))
288 	    {
289 		_pSer->reset();
290 		_pSer->writeChain(Chain(SER_ERROR));
291 		_pSer->writeChain(Chain("Invalid request"));
292 		_pN->writeMsg();
293 		return false;
294 	    }
295 	    else
296 	    {
297 		_tableSet = _pSer->readChain();
298 		_user = _pSer->readChain();
299 		_password = _pSer->readChain();
300 		// cout << "SESSION OK" << endl;
301 		return true;
302 	    }
303 	}
304     }
305     catch ( Exception e)
306     {
307 
308 	Chain msg;
309 	e.pop(msg);
310 
311 	_pModule->log(_modId, Logger::LOGERR, Chain("Aborting session. Reason=") + msg);
312 
313 	_pN->sendNack();
314 	return false;
315     }
316 }
317 
reqQueryOp(const Chain & cmd)318 CegoDbHandler::ResultType CegoDbHandler::reqQueryOp(const Chain& cmd)
319 {
320     if ( _protType == CegoDbHandler::XML )
321     {
322 	_xml.getDocument()->clear();
323 
324 	Element* pRoot = new Element(XML_FRAME_ELEMENT);
325 	pRoot->setAttribute(XML_CMD_ATTR, cmd);
326 
327 	_xml.getDocument()->setRootElement(pRoot);
328 	_xml.getDocument()->setDocType(XML_QUERY_REQUEST);
329 
330 	Chain request;
331 	_xml.getXMLChain(request);
332 
333 	_pN->setMsg(request, request.length());
334 
335 	_pN->writeMsg();
336 
337 	_pN->readMsg();
338 
339 	_xml.getDocument()->clear();
340 	_xml.setChain( _pN->getMsg() );
341 	_xml.parse();
342 
343 	Chain docType = _xml.getDocument()->getDocType();
344 
345 	pRoot = _xml.getDocument()->getRootElement();
346 
347 	if ( pRoot )
348 	{
349 	    _serMsg = pRoot->getAttributeValue(XML_MSG_ATTR);
350 	}
351 
352 	if ( docType == Chain(XML_OK_DOC) )
353 	{
354 	    return DB_OK;
355 	}
356 	else if ( docType == Chain(XML_ERROR_DOC) )
357 	{
358 	    return DB_ERROR;
359 	}
360 	else if ( docType == Chain(XML_DATA_DOC) )
361 	{
362 	    return DB_DATA;
363 	}
364 	else if ( docType == Chain(XML_INFO_DOC) )
365 	{
366 	    return DB_INFO;
367 	}
368 	else
369 	{
370 	    throw Exception(EXLOC, "Invalid document type");
371 	}
372     }
373     else
374     {
375 	_pSer->reset();
376 	_pSer->writeChain(Chain(SER_QUERY));
377 	_pSer->writeChain(cmd);
378 	return sendSerialReq();
379     }
380 }
381 
reqQueryAbort(unsigned long long idx)382 CegoDbHandler::ResultType CegoDbHandler::reqQueryAbort(unsigned long long idx)
383 {
384     if ( _protType == CegoDbHandler::XML )
385     {
386 	Element* pRoot = new Element(XML_FRAME_ELEMENT);
387 	pRoot->setAttribute(XML_TID_ATTR, Chain(idx));
388 	return sendXMLReq(XML_QUERYABORT_REQUEST, pRoot);
389     }
390     else
391     {
392 	_pSer->reset();
393 	_pSer->writeChain(Chain(SER_QUERYABORT));
394 	_pSer->writeChain(Chain(idx));
395 	return sendSerialReq();
396     }
397 }
398 
getMsg()399 const Chain& CegoDbHandler::getMsg()
400 {
401     return _serMsg;
402 }
403 
getAffected()404 unsigned long long CegoDbHandler::getAffected()
405 {
406     if ( _protType == CegoDbHandler::XML )
407     {
408 	Element *pRoot = _xml.getDocument()->getRootElement();
409 
410 	if ( pRoot )
411 	{
412 	    _serAffected =  pRoot->getAttributeValue(XML_AFFECTED_ATTR).asUnsignedLongLong();
413 	}
414     }
415     return _serAffected;
416 }
417 
getTid() const418 unsigned long long CegoDbHandler::getTid() const
419 {
420     return _serTid;
421 }
422 
getDbName() const423 const Chain& CegoDbHandler::getDbName() const
424 {
425     return _serDbName;
426 }
427 
getDbVersion() const428 const Chain& CegoDbHandler::getDbVersion() const
429 {
430     return _serDbVersion;
431 }
432 
getDateFormat() const433 const Chain& CegoDbHandler::getDateFormat() const
434 {
435     return _serDateFormat;
436 }
437 
getQuoteEscapeFlag() const438 char CegoDbHandler::getQuoteEscapeFlag() const
439 {
440     return _serQuoteEscapeFlag;
441 }
442 
getTableSet()443 const Chain& CegoDbHandler::getTableSet()
444 {
445     return _tableSet;
446 }
447 
getUser()448 const Chain& CegoDbHandler::getUser()
449 {
450     return _user;
451 }
452 
getPassword()453 const Chain& CegoDbHandler::getPassword()
454 {
455     return _password;
456 }
457 
getFormat()458 const Chain& CegoDbHandler::getFormat()
459 {
460     if ( _protType == CegoDbHandler::XML )
461     {
462 	Element *pRoot = _xml.getDocument()->getRootElement();
463 	if ( pRoot )
464 	{
465 	    _serFormat = pRoot->getAttributeValue(XML_FORMAT_ATTR);
466 	}
467     }
468     return _serFormat;
469 }
470 
getSchema()471 const ListT<CegoField>& CegoDbHandler::getSchema()
472 {
473     if ( _protType == CegoDbHandler::XML )
474     {
475 	Element *pRoot = _xml.getDocument()->getRootElement();
476 	if ( pRoot )
477 	{
478 	    _serSchema.Empty();
479 
480 	    Chain tableName = pRoot->getAttributeValue(XML_TABLENAME_ATTR);
481 
482 	    ListT<Element*> childList = pRoot->getChildren(Chain(XML_SCHEMA_ELEMENT));
483 	    Element **pRow = childList.First();
484 	    while ( pRow )
485 	    {
486 		Chain colTable = (*pRow)->getAttributeValue(XML_TABLENAME_ATTR);
487 		Chain colName = (*pRow)->getAttributeValue(XML_COLNAME_ATTR);
488 		Chain colType = (*pRow)->getAttributeValue(XML_COLTYPE_ATTR);
489 		Chain colSize = (*pRow)->getAttributeValue(XML_COLSIZE_ATTR);
490 		Chain colDim = (*pRow)->getAttributeValue(XML_COLDIM_ATTR);
491 
492 		Chain colNullable = (*pRow)->getAttributeValue(XML_COLNULLABLE_ATTR);
493 		Chain colDefValue = (*pRow)->getAttributeValue(XML_COLDEFVALUE_ATTR);
494 
495 		CegoTypeConverter tc;
496 		CegoDataType dataType = tc.getTypeId(colType);
497 
498 		bool isNullable;
499 		if ( colNullable == Chain(XML_TRUE_VALUE) )
500 		    isNullable=true;
501 		else
502 		    isNullable=false;
503 
504 		CegoFieldValue defValue;
505 		if ( colDefValue != Chain("") )
506 		{
507 		    defValue = CegoFieldValue(dataType, colDefValue);
508 		}
509 		CegoField f(CegoField(colTable, colTable, colName, dataType, colSize.asInteger(), colDim.asInteger(), defValue, isNullable));
510 
511 		_serSchema.Insert(f);
512 
513 		pRow = childList.Next();
514 	    }
515 	}
516     }
517     return _serSchema;
518 }
519 
getObjElement()520 Element* CegoDbHandler::getObjElement()
521 {
522     if ( _protType == CegoDbHandler::XML )
523     {
524 	Element *pRoot = _xml.getDocument()->getRootElement();
525 
526 	ListT<Element*> objElementList = pRoot->getChildren(XML_OBJ_ELEMENT);
527 
528 	Element **pOE = objElementList.First();
529 	if ( pOE )
530 	{
531 	    return *pOE;
532 	}
533     }
534     else
535     {
536 	throw Exception(EXLOC, Chain("Serial protocol still not supported"));
537 	// TODO
538     }
539     return 0;
540 }
541 
getObjType()542 CegoObject::ObjectType CegoDbHandler::getObjType()
543 {
544     if ( _protType == CegoDbHandler::XML )
545     {
546 	Element *pRoot = _xml.getDocument()->getRootElement();
547 
548 	if ( pRoot )
549 	{
550 	    ListT<Element*> objElementList = pRoot->getChildren(XML_OBJ_ELEMENT);
551 
552 	    Element **pOE = objElementList.First();
553 	    if ( pOE )
554 	    {
555 		CegoTypeConverter tc;
556 		return tc.getObjectTypeId( (*pOE)->getAttributeValue(XML_OBJTYPE_ATTR) );
557 	    }
558 	    throw Exception(EXLOC, "No object type id found");
559 	}
560 	throw Exception(EXLOC, "No root element found");
561     }
562     else
563     {
564 	throw Exception(EXLOC, Chain("Serial protocol still not supported"));
565 	// TODO
566     }
567 }
568 
getProcResult(ListT<CegoProcVar> & outParamList,CegoFieldValue & retValue)569 void CegoDbHandler::getProcResult(ListT<CegoProcVar>& outParamList, CegoFieldValue& retValue)
570 {
571     if ( _protType == CegoDbHandler::XML )
572     {
573 	Element *pRoot = _xml.getDocument()->getRootElement();
574 
575 	if ( pRoot )
576 	{
577 	    ListT<Element*> outParamElementList = pRoot->getChildren(XML_OUTPARAM_ELEMENT);
578 
579 	    ListT<CegoField> fl;
580 	    Element **pOP = outParamElementList.First();
581 	    while ( pOP )
582 	    {
583 		Chain paramName = (*pOP)->getAttributeValue(XML_NAME_ATTR);
584 		Chain paramValue = (*pOP)->getAttributeValue(XML_VALUE_ATTR);
585 		Chain paramType = (*pOP)->getAttributeValue(XML_TYPE_ATTR);
586 
587 		CegoTypeConverter tc;
588 		CegoDataType datatype = tc.getTypeId(paramType);
589 
590 		if ( paramName == Chain("") )
591 		{
592 		    retValue = CegoFieldValue(datatype, paramValue);
593 		}
594 		else
595 		{
596 		    CegoFieldValue fv(datatype, paramValue);
597 		    outParamList.Insert( CegoProcVar(paramName, CegoProcVar::OUTVAR, fv.getType(), fv.getLength(), fv.getDim(), fv));
598 		}
599 
600 		pOP = outParamElementList.Next();
601 	    }
602 	}
603 	else
604 	{
605 	    throw Exception(EXLOC, "No root element found");
606 	}
607     }
608     else
609     {
610 	outParamList = _outParamList;
611 	retValue = _retValue;
612     }
613 }
614 
receiveTableData(const ListT<CegoField> & schema)615 CegoDbHandler::ResultType CegoDbHandler::receiveTableData(const ListT<CegoField>& schema)
616 {
617     ListT<CegoFieldValue> fvl;
618 
619     ResultType res = receiveTableData(schema, fvl);
620 
621     CegoField *pF = schema.First();
622     CegoFieldValue *pFV = fvl.First();
623     while ( pF && pFV )
624     {
625 	pF->setValue(*pFV);
626 	pF = schema.Next();
627 	pFV = fvl.Next();
628     }
629     return res;
630 }
631 
receiveTableData(const ListT<CegoField> & schema,ListT<CegoFieldValue> & fvl)632 CegoDbHandler::ResultType CegoDbHandler::receiveTableData(const ListT<CegoField>& schema, ListT<CegoFieldValue>& fvl)
633 {
634 #ifdef CGDEBUG
635     _pModule->log(_modId, Logger::DEBUG, Chain("Receiving table data ..."));
636 #endif
637 
638     if ( _protType == CegoDbHandler::XML )
639     {
640 	if ( _pRow )
641 	    _pRow = _rowList.Next();
642 
643 	if ( _pRow == 0 )
644 	{
645 	    // get more data
646 
647 	    ResultType res = getMoreTableData();
648 
649 	    switch ( res )
650 	    {
651 	    case DB_DATA:
652 		_pRow = _rowList.First();
653 		break;
654 	    case DB_OK:
655 	    case DB_FIN:
656 	    case DB_ERROR:
657 		return res;
658 	    case DB_INFO:
659 		throw Exception(EXLOC, "Invalid result type");
660 	    }
661 	}
662 
663 	int col=1;
664 	CegoField *pF = schema.First();
665 	while ( pF )
666 	{
667 	    Chain colName = pF->getAttrName();
668 	    Chain colPos = Chain(NETMNG_COLPREFIX) + Chain(col);
669 
670 	    if ( (*_pRow)->hasAttribute(colPos) == false )
671 	    {
672 		CegoFieldValue fv;
673 		fvl.Insert(fv);
674 	    }
675 	    else
676 	    {
677 		Chain colVal = (*_pRow)->getAttributeValue(colPos);
678 
679 		if ( pF->getType() == INT_TYPE  )
680 		{
681 		    CegoFieldValue fv(INT_TYPE, colVal);
682 		    fvl.Insert(fv);
683 		}
684 		else if ( pF->getType() == LONG_TYPE  )
685 		{
686 		    CegoFieldValue fv(LONG_TYPE, colVal);
687 		    fvl.Insert(fv);
688 		}
689 		else if ( pF->getType() == VARCHAR_TYPE )
690 		{
691 		    char *pS = (char*)malloc(colVal.length());
692 		    strcpy(pS, (char*)colVal);
693 		    CegoFieldValue fv(VARCHAR_TYPE, pS, colVal.length(), true);
694 		    fvl.Insert(fv);
695 		}
696 		else if ( pF->getType() == BOOL_TYPE  )
697 		{
698 		    CegoFieldValue fv(BOOL_TYPE, colVal);
699 		    fvl.Insert(fv);
700 		}
701 		else if ( pF->getType() == DATETIME_TYPE  )
702 		{
703 		    CegoFieldValue fv(DATETIME_TYPE, colVal);
704 		    fvl.Insert(fv);
705 		}
706 		else if ( pF->getType() == FLOAT_TYPE  )
707 		{
708 		    CegoFieldValue fv(FLOAT_TYPE, colVal);
709 		    fvl.Insert(fv);
710 		}
711 		else if ( pF->getType() == DOUBLE_TYPE  )
712 		{
713 		    CegoFieldValue fv(DOUBLE_TYPE, colVal);
714 		    fvl.Insert(fv);
715 		}
716 		else if ( pF->getType() == SMALLINT_TYPE  )
717 		{
718 		    CegoFieldValue fv(SMALLINT_TYPE, colVal);
719 		    fvl.Insert(fv);
720 		}
721 		else if ( pF->getType() == TINYINT_TYPE  )
722 		{
723 		    CegoFieldValue fv(TINYINT_TYPE, colVal);
724 		    fvl.Insert(fv);
725 		}
726 		else if ( pF->getType() == BIGINT_TYPE )
727 		{
728 		    CegoFieldValue fv(BIGINT_TYPE, colVal);
729 		    fvl.Insert(fv);
730 		}
731 		else if ( pF->getType() == DECIMAL_TYPE )
732 		{
733 		    CegoFieldValue fv(DECIMAL_TYPE, colVal);
734 		    fvl.Insert(fv);
735 		}
736 		else if ( pF->getType() == FIXED_TYPE )
737 		{
738 		    CegoFieldValue fv(FIXED_TYPE, colVal);
739 		    fvl.Insert(fv);
740 		}
741 		else if ( pF->getType() == BLOB_TYPE )
742 		{
743 		    CegoFieldValue fv(BLOB_TYPE, colVal);
744 		    fvl.Insert(fv);
745 		}
746 	    }
747 
748 	    col++;
749 	    pF = schema.Next();
750 	}
751 	return DB_DATA;
752     }
753     else
754     {
755 	// cout << "Num Ahead = " << _pSer->numAhead() << endl;
756 
757 	if ( _pSer->numAhead() > 0 )
758 	{
759 	    _pSer->readRow(schema, fvl);
760 	    return DB_DATA;
761 	}
762 	else
763 	{
764 	    ResultType res = getMoreTableData();
765 	    switch ( res )
766 	    {
767 	    case DB_DATA:
768 		_pSer->readRow(schema, fvl);
769 		break;
770 	    case DB_OK:
771 	    case DB_ERROR:
772 		break;
773 	    case DB_INFO:
774 	    case DB_FIN:
775 		break;
776 	    }
777 	    return res;
778 	}
779     }
780 }
781 
abortQuery()782 void CegoDbHandler::abortQuery()
783 {
784 #ifdef CGDEBUG
785     _pModule->log(_modId, Logger::DEBUG, Chain("Aborting query"));
786 #endif
787 
788     _pN->sendChar(QUERY_ABORT);
789 
790     _pN->readMsg();
791 
792     if ( _protType == CegoDbHandler::XML )
793     {
794 
795 	_xml.getDocument()->clear();
796 	_xml.setChain( _pN->getMsg() );
797 
798 #ifdef CGDEBUG
799 	_pModule->log(_modId, Logger::DEBUG, Chain("--- XML ---"));
800 	_pModule->log(_modId, Logger::DEBUG, _pN->getMsg());
801 	_pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
802 #endif
803 
804 	_xml.parse();
805 
806 	Chain docType = _xml.getDocument()->getDocType();
807 
808 	if ( docType == Chain(XML_OK_DOC) )
809 	{
810 	    _rowList.Empty();
811 	    _pRow = 0;
812 	}
813 	else if ( docType == Chain(XML_ERROR_DOC) )
814 	{
815 	    _rowList.Empty();
816 	    _pRow = 0;
817 	}
818     }
819     else
820     {
821 	_pSer->reset();
822     }
823 }
824 
resetQuery()825 void CegoDbHandler::resetQuery()
826 {
827 #ifdef CGDEBUG
828     _pModule->log(_modId, Logger::DEBUG, Chain("Resetting query"));
829 #endif
830 
831     _pN->sendChar(QUERY_RESET);
832 
833     _rowList.Empty();
834     _pRow = 0;
835 }
836 
sendCollectedData()837 void CegoDbHandler::sendCollectedData()
838 {
839     char c = QUERY_MOREDATA;
840 
841     if ( _protType == CegoDbHandler::XML )
842     {
843 	c = _pN->recvChar();
844     }
845     else
846     {
847 	if ( _serSync )
848 	    c = _pN->recvChar();
849     }
850 
851     if ( c == QUERY_ABORT )
852     {
853 	_pModule->log(_modId, Logger::LOGERR, Chain("User query abort"));
854 	throw Exception(EXLOC, "Query aborted by user");
855     }
856     else if ( c == QUERY_RESET )
857     {
858 	_wasReset=true;
859 	if ( _protType == CegoDbHandler::XML )
860 	{
861 	    _xml.getDocument()->clear();
862 	}
863 	else
864 	{
865 	    _pSer->reset();
866 	}
867     }
868     else if ( c == QUERY_MOREDATA )
869     {
870 	if ( _protType == CegoDbHandler::XML )
871 	{
872 	    Chain xmlString;
873 
874 	    _xml.getDocument()->setDocType(XML_DATA_DOC);
875 	    _xml.getXMLChain(xmlString);
876 
877 #ifdef CGDEBUG
878 	    _pModule->log(_modId, Logger::DEBUG, Chain("--- XML ---"));
879 	    _pModule->log(_modId, Logger::DEBUG, xmlString);
880 	    _pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
881 #endif
882 
883 	    _pN->setMsg(xmlString, xmlString.length());
884 
885 	    _pN->writeMsg();
886 
887 	    _xml.getDocument()->clear();
888 	}
889 	else
890 	{
891 
892 
893 #ifdef CGDEBUG
894 
895 	    if ( _protType == CegoDbHandler::SERIAL )
896 	    {
897 		_pModule->log(_modId, Logger::DEBUG, Chain("--- SER ---"));
898 		_pModule->log(_modId, Logger::DEBUG, Chain(_pN->getMsg(), _pN->getMsgSize()));
899 		_pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
900 	    }
901 #endif
902 
903 	    _pN->writeMsg();
904 	    _pSer->reset();
905 	    _serSync = true;
906 	}
907     }
908     else
909     {
910 	throw Exception(EXLOC, "Unknown query sync");
911     }
912     return;
913 }
914 
wasReset()915 bool CegoDbHandler::wasReset()
916 {
917     if ( _wasReset )
918     {
919 	_wasReset = false;
920 	return true;
921     }
922     else
923     {
924 	_wasReset = false;
925 	return false;
926     }
927 }
928 
sendFinishData()929 void CegoDbHandler::sendFinishData()
930 {
931     char c = 0;
932 
933     if ( _protType == CegoDbHandler::XML )
934     {
935 	c = _pN->recvChar();
936     }
937     else
938     {
939 	if ( _serSync )
940 	    c = _pN->recvChar();
941     }
942 
943     if ( c == QUERY_ABORT )
944     {
945 	// can be ignored
946 	// _pModule->log(_modId, Logger::LOGERR, Chain("User query abort"));
947 	// throw Exception(EXLOC, "Query aborted by user");
948     }
949     else if ( c == QUERY_RESET )
950     {
951 	// _wasReset=true;
952 	// _xml.getDocument()->clear();
953     }
954     else if ( c == QUERY_MOREDATA )
955     {
956 	// nothing to to
957     }
958 
959     if ( _protType == CegoDbHandler::XML )
960     {
961 
962 	_xml.getDocument()->clear();
963 	_xml.getDocument()->setDocType(XML_OK_DOC);
964 
965 	Element* pRoot = new Element(XML_FRAME_ELEMENT);
966 	_xml.getDocument()->setRootElement(pRoot);
967 
968 	Chain xmlString;
969 	_xml.getXMLChain(xmlString);
970 
971 #ifdef CGDEBUG
972 	_pModule->log(_modId, Logger::DEBUG, Chain("--- XML ---"));
973 	_pModule->log(_modId, Logger::DEBUG, xmlString);
974 	_pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
975 #endif
976 	_pN->setMsg(xmlString, xmlString.length());
977 
978     }
979     else
980     {
981 	_pSer->reset();
982 	_pSer->writeChain(SER_FIN);
983 
984 #ifdef CGDEBUG
985 	if ( _protType == CegoDbHandler::SERIAL )
986 	{
987 	    _pModule->log(_modId, Logger::DEBUG, Chain("--- SER ---"));
988 	    _pModule->log(_modId, Logger::DEBUG, Chain(_pN->getMsg(), _pN->getMsgSize()));
989 	    _pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
990 	}
991 #endif
992 
993     }
994 
995     _pN->writeMsg();
996 }
997 
sendErrorData(const Chain & msg)998 void CegoDbHandler::sendErrorData(const Chain& msg)
999 {
1000     char c = 0;
1001 
1002     if ( _protType == CegoDbHandler::XML )
1003     {
1004 	c = _pN->recvChar();
1005     }
1006     else
1007     {
1008 	if ( _serSync )
1009 	    c = _pN->recvChar();
1010     }
1011 
1012     if ( c == QUERY_ABORT )
1013     {
1014 	// can be ignored
1015 	// we send error back anyway
1016     }
1017     else if ( c == QUERY_RESET )
1018     {
1019 	// we send error back anyway
1020     }
1021     else if ( c == QUERY_MOREDATA )
1022     {
1023 	// we send error back anyway
1024     }
1025 
1026     if ( _protType == CegoDbHandler::XML )
1027     {
1028 	_xml.getDocument()->clear();
1029 	_xml.getDocument()->setDocType(XML_ERROR_DOC);
1030 
1031 	Element* pRoot = new Element(XML_FRAME_ELEMENT);
1032 	pRoot->setAttribute(XML_MSG_ATTR, msg);
1033 	_xml.getDocument()->setRootElement(pRoot);
1034 
1035 	Chain xmlString;
1036 	_xml.getXMLChain(xmlString);
1037 
1038 #ifdef CGDEBUG
1039 	_pModule->log(_modId, Logger::DEBUG, Chain("--- XML ---"));
1040 	_pModule->log(_modId, Logger::DEBUG, xmlString);
1041 	_pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
1042 #endif
1043 
1044 	_pN->setMsg(xmlString, xmlString.length());
1045     }
1046     else
1047     {
1048 	_pSer->reset();
1049 	_pSer->writeChain(SER_ERROR);
1050 	_pSer->writeChain(msg);
1051 
1052 #ifdef CGDEBUG
1053 	if ( _protType == CegoDbHandler::SERIAL )
1054 	{
1055 	    _pModule->log(_modId, Logger::DEBUG, Chain("--- SER ---"));
1056 	    _pModule->log(_modId, Logger::DEBUG, Chain(_pN->getMsg(), _pN->getMsgSize()));
1057 	    _pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
1058 	}
1059 #endif
1060 
1061     }
1062 
1063     _pN->writeMsg();
1064 }
1065 
sendSessionConfirm(const Chain & msg,unsigned long long tid,const Chain & dbName,const Chain & dbVersion,const Chain & dateTimeFormat,char quoteEscapeFlag)1066 void CegoDbHandler::sendSessionConfirm(const Chain& msg,
1067 				       unsigned long long tid,
1068 				       const Chain& dbName,
1069 				       const Chain& dbVersion,
1070 				       const Chain& dateTimeFormat,
1071 				       char quoteEscapeFlag)
1072 {
1073     if ( _protType == CegoDbHandler::XML )
1074     {
1075 	_xml.getDocument()->clear();
1076 	_xml.getDocument()->setDocType(XML_SACK_DOC);
1077 
1078 	Element* pRoot = new Element(XML_FRAME_ELEMENT);
1079 	pRoot->setAttribute(XML_MSG_ATTR, msg);
1080 	pRoot->setAttribute(XML_TID_ATTR, Chain(tid));
1081 	pRoot->setAttribute(XML_DBPRODNAME_ATTR, dbName);
1082 	pRoot->setAttribute(XML_DBPRODVERSION_ATTR, dbVersion);
1083 	pRoot->setAttribute(XML_DATETIMEFORMAT_ATTR, dateTimeFormat);
1084 
1085 	if ( quoteEscapeFlag )
1086 	    pRoot->setAttribute(XML_QESCMODE_ATTR, XML_ON_VALUE);
1087 	else
1088 	    pRoot->setAttribute(XML_QESCMODE_ATTR, XML_OFF_VALUE);
1089 
1090 	_xml.getDocument()->setRootElement(pRoot);
1091 
1092 	Chain xmlString;
1093 	_xml.getXMLChain(xmlString);
1094 
1095 #ifdef CGDEBUG
1096 	_pModule->log(_modId, Logger::DEBUG, Chain("--- XML ---"));
1097 	_pModule->log(_modId, Logger::DEBUG, xmlString);
1098 	_pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
1099 #endif
1100 
1101 	_pN->setMsg(xmlString, xmlString.length());
1102     }
1103     else
1104     {
1105 	_pSer->reset();
1106 
1107 	_pSer->writeChain(Chain(SER_SACK));
1108 	_pSer->writeChain(msg);
1109 	_pSer->writeChain(Chain(tid));
1110 	_pSer->writeChain(dbName);
1111 	_pSer->writeChain(dbVersion);
1112 	_pSer->writeChain(dateTimeFormat);
1113 	if ( quoteEscapeFlag )
1114 	    _pSer->writeChain(Chain("Y"));
1115 	else
1116 	    _pSer->writeChain(Chain("N"));
1117 
1118 #ifdef CGDEBUG
1119 	if ( _protType == CegoDbHandler::SERIAL )
1120 	{
1121 	    _pModule->log(_modId, Logger::DEBUG, Chain("--- SER ---"));
1122 	    _pModule->log(_modId, Logger::DEBUG, Chain(_pN->getMsg(), _pN->getMsgSize()));
1123 	    _pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
1124 	}
1125 #endif
1126 
1127     }
1128 
1129     _pN->writeMsg();
1130 }
1131 
sendResponse(const Chain & msg,unsigned long long affected)1132 void CegoDbHandler::sendResponse(const Chain& msg, unsigned long long affected)
1133 {
1134     if ( _protType == CegoDbHandler::XML )
1135     {
1136 	_xml.getDocument()->clear();
1137 	_xml.getDocument()->setDocType(XML_OK_DOC);
1138 
1139 	Element* pRoot = new Element(XML_FRAME_ELEMENT);
1140 	pRoot->setAttribute(XML_MSG_ATTR, msg);
1141 	pRoot->setAttribute(XML_AFFECTED_ATTR, Chain(affected));
1142 
1143 	_xml.getDocument()->setRootElement(pRoot);
1144 
1145 	Chain xmlString;
1146 	_xml.getXMLChain(xmlString);
1147 
1148 #ifdef CGDEBUG
1149 	_pModule->log(_modId, Logger::DEBUG, Chain("--- XML ---"));
1150 	_pModule->log(_modId, Logger::DEBUG, xmlString);
1151 	_pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
1152 #endif
1153 
1154 	_pN->setMsg(xmlString, xmlString.length());
1155     }
1156     else
1157     {
1158 	_pSer->reset();
1159 	_pSer->writeChain(Chain(SER_OK));
1160 	_pSer->writeChain(msg);
1161 	_pSer->writeChain(Chain(affected));
1162 
1163 #ifdef CGDEBUG
1164 	if ( _protType == CegoDbHandler::SERIAL )
1165 	{
1166 	    _pModule->log(_modId, Logger::DEBUG, Chain("--- SER ---"));
1167 	    _pModule->log(_modId, Logger::DEBUG, Chain(_pN->getMsg(), _pN->getMsgSize()));
1168 	    _pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
1169 	}
1170 #endif
1171 
1172     }
1173     _pN->writeMsg();
1174 }
1175 
sendError(const Chain & msg)1176 void CegoDbHandler::sendError(const Chain& msg)
1177 {
1178     if ( _protType == CegoDbHandler::XML )
1179     {
1180 	_xml.getDocument()->clear();
1181 	_xml.getDocument()->setDocType(XML_ERROR_DOC);
1182 
1183 	Element* pRoot = new Element(XML_FRAME_ELEMENT);
1184 	pRoot->setAttribute(XML_MSG_ATTR, msg);
1185 
1186 	_xml.getDocument()->setRootElement(pRoot);
1187 
1188 	Chain xmlString;
1189 	_xml.getXMLChain(xmlString);
1190 
1191 #ifdef CGDEBUG
1192 	_pModule->log(_modId, Logger::DEBUG, Chain("--- XML ---"));
1193 	_pModule->log(_modId, Logger::DEBUG, xmlString);
1194 	_pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
1195 #endif
1196 
1197 	_pN->setMsg(xmlString, xmlString.length());
1198 
1199     }
1200     else
1201     {
1202 	_pSer->reset();
1203 	_pSer->writeChain(Chain(SER_ERROR));
1204 	_pSer->writeChain(msg);
1205 
1206 #ifdef CGDEBUG
1207 	if ( _protType == CegoDbHandler::SERIAL )
1208 	{
1209 	    _pModule->log(_modId, Logger::DEBUG, Chain("--- SER ---"));
1210 	    _pModule->log(_modId, Logger::DEBUG, Chain(_pN->getMsg(), _pN->getMsgSize()));
1211 	    _pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
1212 	}
1213 #endif
1214 
1215     }
1216     _pN->writeMsg();
1217 }
1218 
sendBlobInfo(PageIdType pageId)1219 void CegoDbHandler::sendBlobInfo(PageIdType pageId)
1220 {
1221     if ( _protType == CegoDbHandler::XML )
1222     {
1223 	_xml.getDocument()->clear();
1224 	_xml.getDocument()->setDocType(XML_OK_DOC);
1225 
1226 	Element* pRoot = new Element(XML_FRAME_ELEMENT);
1227 	pRoot->setAttribute(XML_PAGEID_ATTR, Chain(pageId));
1228 
1229 	_xml.getDocument()->setRootElement(pRoot);
1230 
1231 	Chain xmlString;
1232 	_xml.getXMLChain(xmlString);
1233 
1234 #ifdef CGDEBUG
1235 	_pModule->log(_modId, Logger::DEBUG, Chain("--- XML ---"));
1236 	_pModule->log(_modId, Logger::DEBUG, xmlString);
1237 	_pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
1238 #endif
1239 
1240 	_pN->setMsg(xmlString, xmlString.length());
1241     }
1242     else
1243     {
1244 	_pSer->reset();
1245 	_pSer->writeChain(Chain(SER_BLOBINFO));
1246 	_pSer->writeChain(Chain(pageId));
1247     }
1248     _pN->writeMsg();
1249 }
1250 
sendBlobSize(unsigned long long blobSize)1251 void CegoDbHandler::sendBlobSize(unsigned long long blobSize)
1252 {
1253     if ( _protType == CegoDbHandler::XML )
1254     {
1255 
1256 	_xml.getDocument()->clear();
1257 	_xml.getDocument()->setDocType(XML_OK_DOC);
1258 
1259 	Element* pRoot = new Element(XML_FRAME_ELEMENT);
1260 	pRoot->setAttribute(XML_SIZE_ATTR, Chain(blobSize));
1261 
1262 	_xml.getDocument()->setRootElement(pRoot);
1263 
1264 	Chain xmlString;
1265 	_xml.getXMLChain(xmlString);
1266 
1267 #ifdef CGDEBUG
1268 	_pModule->log(_modId, Logger::DEBUG, Chain("--- XML ---"));
1269 	_pModule->log(_modId, Logger::DEBUG, xmlString);
1270 	_pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
1271 #endif
1272 
1273 	_pN->setMsg(xmlString, xmlString.length());
1274 
1275     }
1276     else
1277     {
1278 	_pSer->reset();
1279 	_pSer->writeChain(Chain(SER_BLOBSIZE));
1280 	_pSer->writeChain(Chain(blobSize));
1281     }
1282     _pN->writeMsg();
1283 }
1284 
putBlob(CegoBlob & blob)1285 CegoDbHandler::ResultType CegoDbHandler::putBlob(CegoBlob& blob)
1286 {
1287     if ( _protType == CegoDbHandler::XML )
1288     {
1289 	_xml.getDocument()->clear();
1290 
1291 	Element* pRoot = new Element(XML_FRAME_ELEMENT);
1292 	pRoot->setAttribute(XML_TABLESET_ATTR, _tableSet);
1293 	pRoot->setAttribute(XML_SIZE_ATTR, blob.getSize());
1294 
1295 	_xml.getDocument()->setRootElement(pRoot);
1296 	_xml.getDocument()->setDocType(XML_PUTBLOB_REQUEST);
1297 
1298 	Chain request;
1299 	_xml.getXMLChain(request);
1300 
1301 	_pN->setMsg(request, request.length());
1302     }
1303     else
1304     {
1305 	_pSer->reset();
1306 	_pSer->writeChain(Chain(SER_PUTBLOB));
1307 	_pSer->writeChain(Chain(_tableSet));
1308 	_pSer->writeChain(Chain(blob.getSize()));
1309     }
1310 
1311     _pN->writeMsg();
1312 
1313     _pN->readMsg();
1314 
1315     if ( _protType == CegoDbHandler::XML )
1316     {
1317 	_xml.getDocument()->clear();
1318 	_xml.setChain( _pN->getMsg() );
1319 	_xml.parse();
1320 
1321 	Chain docType = _xml.getDocument()->getDocType();
1322 
1323 	if ( docType == Chain(XML_ERROR_DOC) )
1324 	{
1325 	    return DB_ERROR;
1326 	}
1327 
1328 	Element* pRoot;
1329 	pRoot = _xml.getDocument()->getRootElement();
1330 	if ( pRoot )
1331 	{
1332 	    blob.setPageId(pRoot->getAttributeValue(XML_PAGEID_ATTR).asUnsignedLongLong());
1333 	}
1334     }
1335     else
1336     {
1337 	_pSer->reset();
1338 	Chain req = _pSer->readChain();
1339 
1340 	if ( req == Chain(SER_ERROR) )
1341 	{
1342 	    return DB_ERROR;
1343 	}
1344 
1345 	blob.setPageId(_pSer->readChain().asUnsignedLongLong());
1346     }
1347 
1348     blob.reset();
1349 
1350     while ( blob.nextChunk(BLOBCHUNKSIZE) )
1351     {
1352 	_pN->setMsg((char*)blob.getChunkPtr(), blob.getChunkSize());
1353 
1354 	_pN->writeMsg();
1355 
1356 	if ( _pN->recvAck() == false )
1357 	{
1358 	    _pModule->log(_modId, Logger::LOGERR, Chain("User query abort"));
1359 	    return DB_ERROR;
1360 	}
1361     }
1362     return DB_OK;
1363 }
1364 
getBlob(CegoBlob & blob)1365 CegoDbHandler::ResultType CegoDbHandler::getBlob(CegoBlob& blob)
1366 {
1367     if ( _protType == CegoDbHandler::XML )
1368     {
1369 	_xml.getDocument()->clear();
1370 
1371 	Element* pRoot = new Element(XML_FRAME_ELEMENT);
1372 	pRoot->setAttribute(XML_TABLESET_ATTR, _tableSet);
1373 	pRoot->setAttribute(XML_PAGEID_ATTR, blob.getPageId());
1374 
1375 	_xml.getDocument()->setRootElement(pRoot);
1376 	_xml.getDocument()->setDocType(XML_GETBLOB_REQUEST);
1377 
1378 	Chain request;
1379 	_xml.getXMLChain(request);
1380 
1381 	_pN->setMsg(request, request.length());
1382     }
1383     else
1384     {
1385 	_pSer->reset();
1386 	_pSer->writeChain(Chain(SER_GETBLOB));
1387 	_pSer->writeChain(Chain(_tableSet));
1388 	_pSer->writeChain(Chain(blob.getPageId()));
1389     }
1390     _pN->writeMsg();
1391 
1392     _pN->readMsg();
1393 
1394     unsigned long long blobSize = 0;
1395 
1396     if ( _protType == CegoDbHandler::XML )
1397     {
1398 	_xml.getDocument()->clear();
1399 	_xml.setChain( _pN->getMsg() );
1400 	_xml.parse();
1401 
1402 	Chain docType = _xml.getDocument()->getDocType();
1403 
1404 	if ( docType == Chain(XML_ERROR_DOC) )
1405 	{
1406 	    return DB_ERROR;
1407 	}
1408 
1409 	Element* pRoot;
1410 	pRoot = _xml.getDocument()->getRootElement();
1411 	if ( pRoot )
1412 	{
1413 	    blobSize = pRoot->getAttributeValue(XML_SIZE_ATTR).asUnsignedLongLong();
1414 	}
1415 	else
1416 	{
1417 	    throw Exception(EXLOC, Chain("Cannot get blob size"));
1418 	}
1419     }
1420     else
1421     {
1422 	_pSer->reset();
1423 	Chain req = _pSer->readChain();
1424 
1425 	if ( req == Chain(SER_ERROR) )
1426 	{
1427 	    return DB_ERROR;
1428 	}
1429 
1430 	blobSize = _pSer->readChain().asInteger();
1431 
1432     }
1433 
1434     blob.allocate(blobSize);
1435     blob.reset();
1436 
1437     int recvSize = 0;
1438     while ( recvSize < blobSize )
1439     {
1440        	_pN->sendAck();
1441 	_pN->readMsg();
1442 	Chain msg(_pN->getMsg(), _pN->getMsgSize() );
1443 
1444 	blob.putChunk((unsigned char*)_pN->getMsg(), _pN->getMsgSize() );
1445 
1446 	recvSize += _pN->getMsgSize();
1447     }
1448 
1449     return DB_OK;
1450 }
1451 
delBlob(CegoBlob & blob)1452 CegoDbHandler::ResultType CegoDbHandler::delBlob(CegoBlob& blob)
1453 {
1454     if ( _protType == CegoDbHandler::XML )
1455     {
1456 	_xml.getDocument()->clear();
1457 
1458 	Element* pRoot = new Element(XML_FRAME_ELEMENT);
1459 	pRoot->setAttribute(XML_TABLESET_ATTR, _tableSet);
1460 	pRoot->setAttribute(XML_PAGEID_ATTR, blob.getPageId());
1461 
1462 	_xml.getDocument()->setRootElement(pRoot);
1463 	_xml.getDocument()->setDocType(XML_DELBLOB_REQUEST);
1464 
1465 	Chain request;
1466 	_xml.getXMLChain(request);
1467 
1468 	_pN->setMsg(request, request.length());
1469     }
1470     else
1471     {
1472 	_pSer->reset();
1473 	_pSer->writeChain(Chain(SER_DELBLOB));
1474 	_pSer->writeChain(Chain(_tableSet));
1475 	_pSer->writeChain(Chain(blob.getPageId()));
1476 
1477     }
1478     _pN->writeMsg();
1479 
1480     _pN->readMsg();
1481 
1482     if ( _protType == CegoDbHandler::XML )
1483     {
1484 	_xml.getDocument()->clear();
1485 	_xml.setChain( _pN->getMsg() );
1486 	_xml.parse();
1487 
1488 	Chain docType = _xml.getDocument()->getDocType();
1489 
1490 	if ( docType == Chain(XML_OK_DOC) )
1491 	{
1492 	    return DB_OK;
1493 	}
1494 	else if ( docType == Chain(XML_ERROR_DOC) )
1495 	{
1496 	    return DB_ERROR;
1497 	}
1498 	else
1499 	{
1500 	    throw Exception(EXLOC, Chain("Invalid document type"));
1501 	}
1502     }
1503     else
1504     {
1505 	_pSer->reset();
1506 	Chain req = _pSer->readChain();
1507 
1508 	if ( req == Chain(SER_ERROR) )
1509 	{
1510 	    return DB_ERROR;
1511 	}
1512 	return DB_OK;
1513     }
1514 }
1515 
sendClobInfo(PageIdType pageId)1516 void CegoDbHandler::sendClobInfo(PageIdType pageId)
1517 {
1518     if ( _protType == CegoDbHandler::XML )
1519     {
1520 	_xml.getDocument()->clear();
1521 	_xml.getDocument()->setDocType(XML_OK_DOC);
1522 
1523 	Element* pRoot = new Element(XML_FRAME_ELEMENT);
1524 	pRoot->setAttribute(XML_PAGEID_ATTR, Chain(pageId));
1525 
1526 	_xml.getDocument()->setRootElement(pRoot);
1527 
1528 	Chain xmlString;
1529 	_xml.getXMLChain(xmlString);
1530 
1531 #ifdef CGDEBUG
1532 	_pModule->log(_modId, Logger::DEBUG, Chain("--- XML ---"));
1533 	_pModule->log(_modId, Logger::DEBUG, xmlString);
1534 	_pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
1535 #endif
1536 
1537 	_pN->setMsg(xmlString, xmlString.length());
1538     }
1539     else
1540     {
1541 	_pSer->reset();
1542 	_pSer->writeChain(Chain(SER_CLOBINFO));
1543 	_pSer->writeChain(Chain(pageId));
1544     }
1545     _pN->writeMsg();
1546 }
1547 
sendClobSize(unsigned long long clobSize)1548 void CegoDbHandler::sendClobSize(unsigned long long clobSize)
1549 {
1550     if ( _protType == CegoDbHandler::XML )
1551     {
1552 	_xml.getDocument()->clear();
1553 	_xml.getDocument()->setDocType(XML_OK_DOC);
1554 
1555 	Element* pRoot = new Element(XML_FRAME_ELEMENT);
1556 	pRoot->setAttribute(XML_SIZE_ATTR, Chain(clobSize));
1557 
1558 	_xml.getDocument()->setRootElement(pRoot);
1559 
1560 	Chain xmlString;
1561 	_xml.getXMLChain(xmlString);
1562 
1563 #ifdef CGDEBUG
1564 	_pModule->log(_modId, Logger::DEBUG, Chain("--- XML ---"));
1565 	_pModule->log(_modId, Logger::DEBUG, xmlString);
1566 	_pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
1567 #endif
1568 
1569 	_pN->setMsg(xmlString, xmlString.length());
1570     }
1571     else
1572     {
1573 	_pSer->reset();
1574 	_pSer->writeChain(Chain(SER_CLOBSIZE));
1575 	_pSer->writeChain(Chain(clobSize));
1576     }
1577     _pN->writeMsg();
1578 }
1579 
putClob(CegoClob & clob)1580 CegoDbHandler::ResultType CegoDbHandler::putClob(CegoClob& clob)
1581 {
1582     if ( _protType == CegoDbHandler::XML )
1583     {
1584 	_xml.getDocument()->clear();
1585 
1586 	Element* pRoot = new Element(XML_FRAME_ELEMENT);
1587 	pRoot->setAttribute(XML_TABLESET_ATTR, _tableSet);
1588 	pRoot->setAttribute(XML_SIZE_ATTR, clob.getSize());
1589 
1590 	_xml.getDocument()->setRootElement(pRoot);
1591 	_xml.getDocument()->setDocType(XML_PUTCLOB_REQUEST);
1592 
1593 	Chain request;
1594 	_xml.getXMLChain(request);
1595 
1596 	_pN->setMsg(request, request.length());
1597     }
1598     else
1599     {
1600 	_pSer->reset();
1601 	_pSer->writeChain(Chain(SER_PUTCLOB));
1602 	_pSer->writeChain(Chain(_tableSet));
1603 	_pSer->writeChain(Chain(clob.getSize()));
1604 
1605     }
1606 
1607     _pN->writeMsg();
1608 
1609     _pN->readMsg();
1610 
1611     if ( _protType == CegoDbHandler::XML )
1612     {
1613 	_xml.getDocument()->clear();
1614 	_xml.setChain( _pN->getMsg() );
1615 	_xml.parse();
1616 
1617 	Chain docType = _xml.getDocument()->getDocType();
1618 
1619 	if ( docType == Chain(XML_ERROR_DOC) )
1620 	{
1621 	    return DB_ERROR;
1622 	}
1623 
1624 	Element* pRoot;
1625 	pRoot = _xml.getDocument()->getRootElement();
1626 	if ( pRoot )
1627 	{
1628 	    clob.setPageId(pRoot->getAttributeValue(XML_PAGEID_ATTR).asUnsignedLongLong());
1629 	}
1630     }
1631     else
1632     {
1633 	_pSer->reset();
1634 	Chain req = _pSer->readChain();
1635 
1636 	if ( req == Chain(SER_ERROR) )
1637 	{
1638 	    return DB_ERROR;
1639 	}
1640 
1641 	clob.setPageId(_pSer->readChain().asUnsignedLongLong());
1642     }
1643 
1644     clob.reset();
1645 
1646     while ( clob.nextChunk(CLOBCHUNKSIZE) )
1647     {
1648 	_pN->setMsg((char*)clob.getChunkPtr(), clob.getChunkSize());
1649 
1650 	_pN->writeMsg();
1651 
1652 	if ( _pN->recvAck() == false )
1653 	{
1654 	    _pModule->log(_modId, Logger::LOGERR, Chain("User query abort"));
1655 	    return DB_ERROR;
1656 	}
1657     }
1658     return DB_OK;
1659 }
1660 
getClob(CegoClob & clob)1661 CegoDbHandler::ResultType CegoDbHandler::getClob(CegoClob& clob)
1662 {
1663 
1664     if ( _protType == CegoDbHandler::XML )
1665     {
1666 	_xml.getDocument()->clear();
1667 
1668 	Element* pRoot = new Element(XML_FRAME_ELEMENT);
1669 	pRoot->setAttribute(XML_TABLESET_ATTR, _tableSet);
1670 	pRoot->setAttribute(XML_PAGEID_ATTR, clob.getPageId());
1671 
1672 	_xml.getDocument()->setRootElement(pRoot);
1673 	_xml.getDocument()->setDocType(XML_GETCLOB_REQUEST);
1674 
1675 	Chain request;
1676 	_xml.getXMLChain(request);
1677 
1678 	_pN->setMsg(request, request.length());
1679     }
1680     else
1681     {
1682 	_pSer->reset();
1683 	_pSer->writeChain(Chain(SER_GETCLOB));
1684 	_pSer->writeChain(Chain(_tableSet));
1685 	_pSer->writeChain(Chain(clob.getPageId()));
1686     }
1687     _pN->writeMsg();
1688 
1689     _pN->readMsg();
1690 
1691     unsigned long long clobSize = 0;
1692 
1693     if ( _protType == CegoDbHandler::XML )
1694     {
1695 	_xml.getDocument()->clear();
1696 	_xml.setChain( _pN->getMsg() );
1697 	_xml.parse();
1698 
1699 	Chain docType = _xml.getDocument()->getDocType();
1700 
1701 	if ( docType == Chain(XML_ERROR_DOC) )
1702 	{
1703 	    return DB_ERROR;
1704 	}
1705 
1706 	Element* pRoot;
1707 	pRoot = _xml.getDocument()->getRootElement();
1708 	if ( pRoot )
1709 	{
1710 	    clobSize = pRoot->getAttributeValue(XML_SIZE_ATTR).asUnsignedLongLong();
1711 	}
1712 	else
1713 	{
1714 	    throw Exception(EXLOC, Chain("Cannot get clob size"));
1715 	}
1716 
1717     }
1718     else
1719     {
1720 	_pSer->reset();
1721 	Chain req = _pSer->readChain();
1722 
1723 	if ( req == Chain(SER_ERROR) )
1724 	{
1725 	    return DB_ERROR;
1726 	}
1727 	clobSize = _pSer->readChain().asInteger();
1728     }
1729 
1730     clob.allocate(clobSize);
1731     clob.reset();
1732 
1733     int recvSize = 0;
1734     while ( recvSize < clobSize )
1735     {
1736        	_pN->sendAck();
1737 	_pN->readMsg();
1738 	Chain msg(_pN->getMsg(), _pN->getMsgSize() );
1739 
1740 	clob.putChunk((char*)_pN->getMsg(), _pN->getMsgSize() );
1741 
1742 	recvSize += _pN->getMsgSize();
1743     }
1744     return DB_OK;
1745 }
1746 
delClob(CegoClob & clob)1747 CegoDbHandler::ResultType CegoDbHandler::delClob(CegoClob& clob)
1748 {
1749     if ( _protType == CegoDbHandler::XML )
1750     {
1751 	_xml.getDocument()->clear();
1752 
1753 	Element* pRoot = new Element(XML_FRAME_ELEMENT);
1754 	pRoot->setAttribute(XML_TABLESET_ATTR, _tableSet);
1755 	pRoot->setAttribute(XML_PAGEID_ATTR, clob.getPageId());
1756 
1757 	_xml.getDocument()->setRootElement(pRoot);
1758 	_xml.getDocument()->setDocType(XML_DELCLOB_REQUEST);
1759 
1760 	Chain request;
1761 	_xml.getXMLChain(request);
1762 
1763 	_pN->setMsg(request, request.length());
1764     }
1765     else
1766     {
1767 	_pSer->reset();
1768 	_pSer->writeChain(Chain(SER_DELCLOB));
1769 	_pSer->writeChain(Chain(_tableSet));
1770 	_pSer->writeChain(Chain(clob.getPageId()));
1771     }
1772     _pN->writeMsg();
1773 
1774     _pN->readMsg();
1775 
1776     if ( _protType == CegoDbHandler::XML )
1777     {
1778 	_xml.getDocument()->clear();
1779 	_xml.setChain( _pN->getMsg() );
1780 	_xml.parse();
1781 
1782 	Chain docType = _xml.getDocument()->getDocType();
1783 
1784 	if ( docType == Chain(XML_OK_DOC) )
1785 	{
1786 	    return DB_OK;
1787 	}
1788 	else if ( docType == Chain(XML_ERROR_DOC) )
1789 	{
1790 	    return DB_ERROR;
1791 	}
1792 	else
1793 	{
1794 	    throw Exception(EXLOC, Chain("Invalid document type"));
1795 	}
1796     }
1797     else
1798     {
1799 	_pSer->reset();
1800 	Chain req = _pSer->readChain();
1801 
1802 	if ( req == Chain(SER_ERROR) )
1803 	{
1804 	    return DB_ERROR;
1805 	}
1806 	return DB_OK;
1807     }
1808 }
1809 
sendProcResult(const Chain & msg,const ListT<CegoProcVar> & outParamList,CegoFieldValue * pRetValue)1810 void CegoDbHandler::sendProcResult(const Chain& msg, const ListT<CegoProcVar>& outParamList, CegoFieldValue* pRetValue)
1811 {
1812 #ifdef CGDEBUG
1813     _pModule->log(_modId, Logger::DEBUG, Chain("Sending procedure result"));
1814 #endif
1815 
1816     if ( _protType == CegoDbHandler::XML )
1817     {
1818 	_xml.getDocument()->clear();
1819 	_xml.getDocument()->setDocType(XML_OK_DOC);
1820 
1821 	Element* pRoot = new Element(XML_FRAME_ELEMENT);
1822 	pRoot->setAttribute(XML_MSG_ATTR, msg);
1823 
1824 	if ( pRetValue )
1825 	{
1826 	    Element *pOutParam = new Element(XML_OUTPARAM_ELEMENT);
1827 
1828 	    CegoXMLHelper xh;
1829 
1830 	    CegoTypeConverter tc;
1831 	    pOutParam->setAttribute(XML_TYPE_ATTR, tc.getTypeString(pRetValue->getType()));
1832 
1833 	    pOutParam->setAttribute(XML_VALUE_ATTR, pRetValue->valAsChain());
1834 	    pRoot->addContent(pOutParam);
1835 	}
1836 
1837 	CegoProcVar *pVar = outParamList.First();
1838 	while ( pVar )
1839 	{
1840 	    Element *pOutParam = new Element(XML_OUTPARAM_ELEMENT);
1841 	    pOutParam->setAttribute(XML_NAME_ATTR, pVar->getName());
1842 
1843 	    CegoTypeConverter tc;
1844 	    pOutParam->setAttribute(XML_TYPE_ATTR, tc.getTypeString(pVar->getValue().getType()));
1845 	    pOutParam->setAttribute(XML_VALUE_ATTR, pVar->getValue().valAsChain());
1846 
1847 	    pVar = outParamList.Next();
1848 	    pRoot->addContent(pOutParam);
1849 	}
1850 
1851 	_xml.getDocument()->setRootElement(pRoot);
1852 
1853 	Chain xmlString;
1854 	_xml.getXMLChain(xmlString);
1855 
1856 #ifdef CGDEBUG
1857 	_pModule->log(_modId, Logger::DEBUG, Chain("--- XML ---"));
1858 	_pModule->log(_modId, Logger::DEBUG, xmlString);
1859 	_pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
1860 #endif
1861 
1862 	_pN->setMsg(xmlString, xmlString.length());
1863     }
1864     else
1865     {
1866 	_pSer->reset();
1867 	_pSer->writeChain(Chain(SER_PROCRES));
1868 
1869 	CegoTypeConverter tc;
1870 
1871 	if ( pRetValue )
1872 	{
1873 	    _pSer->writeChain(Chain("@OUT"));
1874 	    _pSer->writeChain(tc.getTypeString(pRetValue->getType()));
1875 	    _pSer->writeChain(pRetValue->valAsChain());
1876 	}
1877 
1878 	CegoProcVar *pVar = outParamList.First();
1879 	while ( pVar )
1880 	{
1881 	    _pSer->writeChain(pVar->getName());
1882 	    _pSer->writeChain(tc.getTypeString(pVar->getValue().getType()));
1883 	    _pSer->writeChain(pVar->getValue().valAsChain());
1884 
1885 	    pVar = outParamList.Next();
1886 	}
1887 
1888 #ifdef CGDEBUG
1889 	if ( _protType == CegoDbHandler::SERIAL )
1890 	{
1891 	    _pModule->log(_modId, Logger::DEBUG, Chain("--- SER ---"));
1892 	    _pModule->log(_modId, Logger::DEBUG, Chain(_pN->getMsg(), _pN->getMsgSize()));
1893 	    _pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
1894 	}
1895 #endif
1896     }
1897 
1898     _pN->writeMsg();
1899 }
1900 
sendProdInfo()1901 void CegoDbHandler::sendProdInfo()
1902 {
1903     if ( _protType == CegoDbHandler::XML )
1904     {
1905 	_xml.getDocument()->clear();
1906 	_xml.getDocument()->setDocType(XML_OK_DOC);
1907 
1908 	Element* pRoot = new Element(XML_FRAME_ELEMENT);
1909 	pRoot->setAttribute(XML_DBPRODNAME_ATTR, XML_DBPRODNAME_VALUE);
1910 	pRoot->setAttribute(XML_DBPRODVERSION_ATTR, XML_DBPRODVERSION_VALUE);
1911 
1912 	_xml.getDocument()->setRootElement(pRoot);
1913 
1914 	Chain xmlString;
1915 	_xml.getXMLChain(xmlString);
1916 
1917 #ifdef CGDEBUG
1918 	_pModule->log(_modId, Logger::DEBUG, Chain("--- XML ---"));
1919 	_pModule->log(_modId, Logger::DEBUG, xmlString);
1920 	_pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
1921 #endif
1922 
1923 	_pN->setMsg(xmlString, xmlString.length());
1924     }
1925     else
1926     {
1927 	_pSer->reset();
1928 	_pSer->writeChain(Chain(SER_PRODINFO));
1929 	_pSer->writeChain(CEGO_PRODUCT);
1930 	_pSer->writeChain(CEGO_VERSION);
1931     }
1932     _pN->writeMsg();
1933 }
1934 
getMoreTableData()1935 CegoDbHandler::ResultType CegoDbHandler::getMoreTableData()
1936 {
1937 #ifdef CGDEBUG
1938     _pModule->log(_modId, Logger::DEBUG, Chain("Sending ack ..."));
1939 #endif
1940 
1941     _pN->sendChar(QUERY_MOREDATA);
1942 
1943 #ifdef CGDEBUG
1944     _pModule->log(_modId, Logger::DEBUG, Chain("Reading data ..."));
1945 #endif
1946 
1947     _pN->readMsg();
1948 
1949     if ( _protType == CegoDbHandler::XML )
1950     {
1951 	_xml.getDocument()->clear();
1952 	_xml.setChain( _pN->getMsg() );
1953 
1954 #ifdef CGDEBUG
1955 	_pModule->log(_modId, Logger::DEBUG, Chain("--- XML ---"));
1956 	_pModule->log(_modId, Logger::DEBUG, _pN->getMsg());
1957 	_pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
1958 #endif
1959 
1960 	_xml.parse();
1961 
1962 	Chain docType = _xml.getDocument()->getDocType();
1963 
1964 	if ( docType == Chain(XML_DATA_DOC) )
1965 	{
1966 	    Element *pRoot = _xml.getDocument()->getRootElement();
1967 	    if ( pRoot )
1968 	    {
1969 		_rowList = pRoot->getChildren(XML_ROW_ELEMENT);
1970 	    }
1971 	    return DB_DATA;
1972 	}
1973 	else if ( docType == Chain(XML_OK_DOC) )
1974 	{
1975 	    _rowList.Empty();
1976 	    return DB_OK;
1977 	}
1978 	else if ( docType == Chain(XML_ERROR_DOC) )
1979 	{
1980 	    return DB_ERROR;
1981 	}
1982 	else
1983 	{
1984 	    throw Exception(EXLOC, Chain("Invalid document type"));
1985 	}
1986     }
1987     else
1988     {
1989 
1990 #ifdef CGDEBUG
1991 	if ( _protType == CegoDbHandler::SERIAL )
1992 	{
1993 	    _pModule->log(_modId, Logger::DEBUG, Chain("--- SER ---"));
1994 	    _pModule->log(_modId, Logger::DEBUG, Chain(_pN->getMsg(), _pN->getMsgSize()));
1995 	    _pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
1996 	}
1997 #endif
1998 
1999 	_pSer->reset();
2000 	Chain req = _pSer->readChain();
2001 
2002 	// cout << "Reading req " << req << endl;
2003 	if ( req == Chain(SER_SDATA) )
2004 	{
2005 	    return DB_DATA;
2006 	}
2007 	else if ( req == Chain(SER_FDATA) )
2008 	{
2009 	    return DB_DATA;
2010 	}
2011 	else if ( req == Chain(SER_FIN) )
2012 	{
2013 	    _pSer->reset();
2014 	    return DB_FIN;
2015 	}
2016 	else if ( req == Chain(SER_ERROR) )
2017 	{
2018 	    _serMsg = _pSer->readChain();
2019 	    _pSer->reset();
2020 	    return DB_ERROR;
2021 	}
2022 	else
2023 	{
2024 	    throw Exception(EXLOC, Chain("Invalid serial request"));
2025 	}
2026     }
2027 }
2028 
acceptRequest()2029 CegoDbHandler::RequestType CegoDbHandler::acceptRequest()
2030 {
2031 
2032 #ifdef CGDEBUG
2033     _pModule->log(_modId, Logger::DEBUG, Chain("Accepting request ..."));
2034 #endif
2035 
2036     if ( _pN->waitMsg(NETMNG_WAITMSG_TIMEOUT) == false )
2037 	return CegoDbHandler::REQTIMEOUT;
2038 
2039     _pN->readMsg();
2040 
2041     if ( _protType == CegoDbHandler::XML )
2042     {
2043 
2044 #ifdef CGDEBUG
2045 	_pModule->log(_modId, Logger::DEBUG, Chain("--- XML ---"));
2046 	_pModule->log(_modId, Logger::DEBUG, _pN->getMsg());
2047 	_pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
2048 #endif
2049 
2050 	_xml.getDocument()->clear();
2051 	_xml.setChain( _pN->getMsg() );
2052 	_xml.parse();
2053 
2054 	Chain docType = _xml.getDocument()->getDocType();
2055 
2056 	if ( docType == Chain(XML_QUERY_REQUEST) )
2057 	{
2058 	    return CegoDbHandler::QUERY;
2059 	}
2060 	else if ( docType == Chain(XML_QUERYABORT_REQUEST) )
2061 	{
2062 	    return CegoDbHandler::QUERYABORT;
2063 	}
2064 	else if ( docType == Chain(XML_DBPRODINFO_REQUEST) )
2065 	{
2066 	    return CegoDbHandler::DBPRODINFO;
2067 	}
2068 	else if (  docType == Chain(XML_CREATETABLE_REQUEST) )
2069 	{
2070 	    return CegoDbHandler::CREATETABLE;
2071 	}
2072 	else if (  docType == Chain(XML_CREATEVIEW_REQUEST) )
2073 	{
2074 	    return CegoDbHandler::CREATEVIEW;
2075 	}
2076 	else if (  docType == Chain(XML_CREATEPROCEDURE_REQUEST) )
2077 	{
2078 	    return CegoDbHandler::CREATEPROCEDURE;
2079 	}
2080 	else if (  docType == Chain(XML_CREATECHECK_REQUEST) )
2081 	{
2082 	    return CegoDbHandler::CREATECHECK;
2083 	}
2084 	else if (  docType == Chain(XML_ALTERTABLE_REQUEST) )
2085 	{
2086 	    return CegoDbHandler::ALTERTABLE;
2087 	}
2088 	else if (  docType == Chain(XML_CREATEINDEX_REQUEST) )
2089 	{
2090 	    return CegoDbHandler::CREATEINDEX;
2091 	}
2092 	else if (  docType == Chain(XML_INSERT_REQUEST) )
2093 	{
2094 	    return CegoDbHandler::INSERT;
2095 	}
2096 	else if (  docType == Chain(XML_DELETE_REQUEST) )
2097 	{
2098 	    return CegoDbHandler::DELETETABLE;
2099 	}
2100 	else if (  docType == Chain(XML_UPDATE_REQUEST) )
2101 	{
2102 	    return CegoDbHandler::UPDATE;
2103 	}
2104 	else if (  docType == Chain(XML_OBJECTINFO_REQUEST) )
2105 	{
2106 	    return CegoDbHandler::OBJECTINFO;
2107 	}
2108 	else if (  docType == Chain(XML_DROP_OBJECT_REQUEST) )
2109 	{
2110 	    return CegoDbHandler::DROPOBJECT;
2111 	}
2112 	else if (  docType == Chain(XML_GETTABLE_REQUEST) )
2113 	{
2114 	    return CegoDbHandler::GETTABLE;
2115 	}
2116 	else if (  docType == Chain(XML_GETOBJLIST_REQUEST) )
2117 	{
2118 	    return CegoDbHandler::GETOBJLIST;
2119 	}
2120 	else if (  docType == Chain(XML_GETOBJLISTBYTABLE_REQUEST) )
2121 	{
2122 	    return CegoDbHandler::GETOBJLISTBYTABLE;
2123 	}
2124 	else if (  docType == Chain(XML_OBJRENAME_REQUEST) )
2125 	{
2126 	    return CegoDbHandler::OBJRENAME;
2127 	}
2128 	else if (  docType == Chain(XML_REORG_REQUEST) )
2129 	{
2130 	    return CegoDbHandler::REORGOBJ;
2131 	}
2132 	else if (  docType == Chain(XML_SYNC_REQUEST) )
2133 	{
2134 	    return CegoDbHandler::SYNC;
2135 	}
2136 	else if (  docType == Chain(XML_GETPAGECOUNT_REQUEST) )
2137 	{
2138 	    return CegoDbHandler::GETPAGECOUNT;
2139 	}
2140 	else if (  docType == Chain(XML_PUTBLOB_REQUEST) )
2141 	{
2142 	    return CegoDbHandler::PUTBLOB;
2143 	}
2144 	else if (  docType == Chain(XML_GETBLOB_REQUEST) )
2145 	{
2146 	    return CegoDbHandler::GETBLOB;
2147 	}
2148 	else if (  docType == Chain(XML_DELBLOB_REQUEST) )
2149 	{
2150 	    return CegoDbHandler::DELBLOB;
2151 	}
2152 	else if (  docType == Chain(XML_PUTCLOB_REQUEST) )
2153 	{
2154 	    return CegoDbHandler::PUTCLOB;
2155 	}
2156 	else if (  docType == Chain(XML_GETCLOB_REQUEST) )
2157 	{
2158 	    return CegoDbHandler::GETCLOB;
2159 	}
2160 	else if (  docType == Chain(XML_DELCLOB_REQUEST) )
2161 	{
2162 	    return CegoDbHandler::DELCLOB;
2163 	}
2164 	else if (  docType == Chain(XML_STARTTRANSACTION_REQUEST) )
2165 	{
2166 	    return CegoDbHandler::STARTTRANSACTION;
2167 	}
2168 	else if (  docType == Chain(XML_COMMITTRANSACTION_REQUEST) )
2169 	{
2170 	    return CegoDbHandler::COMMITTRANSACTION;
2171 	}
2172 	else if (  docType == Chain(XML_ROLLBACKTRANSACTION_REQUEST) )
2173 	{
2174 	    return CegoDbHandler::ROLLBACKTRANSACTION;
2175 	}
2176 	else if (  docType == Chain(XML_GETTID_REQUEST) )
2177 	{
2178 	    return CegoDbHandler::GETTID;
2179 	}
2180 	else if (  docType == Chain(XML_SESSION_CLOSE) )
2181 	{
2182 	    return CegoDbHandler::SESSION_CLOSE;
2183 	}
2184 	else
2185 	{
2186 	    return CegoDbHandler::UNKNOWN;
2187 	}
2188     }
2189     else
2190     {
2191 
2192 #ifdef CGDEBUG
2193 	if ( _protType == CegoDbHandler::SERIAL )
2194 	{
2195 	    _pModule->log(_modId, Logger::DEBUG, Chain("--- SER ---"));
2196 	    _pModule->log(_modId, Logger::DEBUG, Chain(_pN->getMsg(), _pN->getMsgSize()));
2197 	    _pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
2198 	}
2199 #endif
2200 
2201 	_pSer->reset();
2202 	Chain req = _pSer->readChain();
2203 
2204 	if ( req == Chain(SER_QUERY) )
2205 	{
2206 	    _serQueryCmd = _pSer->readChain();
2207 	    return CegoDbHandler::QUERY;
2208 	}
2209 	else if ( req == Chain(SER_QUERYABORT) )
2210 	{
2211 	    _serTid = _pSer->readChain().asUnsignedLongLong();
2212 	    return CegoDbHandler::QUERYABORT;
2213 	}
2214 	else if ( req == Chain(SER_DBPRODINFO) )
2215 	{
2216 	    return CegoDbHandler::DBPRODINFO;
2217 	}
2218 	else if (  req == Chain(SER_CREATETABLE) )
2219 	{
2220 	    return CegoDbHandler::CREATETABLE;
2221 	}
2222 	else if (  req == Chain(SER_CREATEVIEW) )
2223 	{
2224 	    return CegoDbHandler::CREATEVIEW;
2225 	}
2226 	else if (  req == Chain(SER_CREATEPROCEDURE) )
2227 	{
2228 	    return CegoDbHandler::CREATEPROCEDURE;
2229 	}
2230 	else if (  req == Chain(SER_CREATECHECK) )
2231 	{
2232 	    return CegoDbHandler::CREATECHECK;
2233 	}
2234 	else if (  req == Chain(SER_ALTERTABLE) )
2235 	{
2236 	    return CegoDbHandler::ALTERTABLE;
2237 	}
2238 	else if (  req == Chain(SER_CREATEINDEX) )
2239 	{
2240 	    return CegoDbHandler::CREATEINDEX;
2241 	}
2242 	else if (  req == Chain(SER_INSERT) )
2243 	{
2244 	    return CegoDbHandler::INSERT;
2245 	}
2246 	else if (  req == Chain(SER_DELETE) )
2247 	{
2248 	    _serAffected = _pSer->readChain().asInteger();
2249 	    return CegoDbHandler::DELETETABLE;
2250 	}
2251 	else if (  req == Chain(SER_UPDATE) )
2252 	{
2253 	    _serAffected = _pSer->readChain().asInteger();
2254 	    return CegoDbHandler::UPDATE;
2255 	}
2256 	else if (  req == Chain(SER_OBJECTINFO) )
2257 	{
2258 	    return CegoDbHandler::OBJECTINFO;
2259 	}
2260 	else if (  req == Chain(SER_DROP_OBJECT) )
2261 	{
2262 	    return CegoDbHandler::DROPOBJECT;
2263 	}
2264 	else if (  req == Chain(SER_GETTABLE) )
2265 	{
2266 	    return CegoDbHandler::GETTABLE;
2267 	}
2268 	else if (  req == Chain(SER_GETOBJLIST) )
2269 	{
2270 	    return CegoDbHandler::GETOBJLIST;
2271 	}
2272 	else if (  req == Chain(SER_GETOBJLISTBYTABLE) )
2273 	{
2274 	    return CegoDbHandler::GETOBJLISTBYTABLE;
2275 	}
2276 	else if (  req == Chain(SER_OBJRENAME) )
2277 	{
2278 	    return CegoDbHandler::OBJRENAME;
2279 	}
2280 	else if (  req == Chain(SER_REORG) )
2281 	{
2282 	    return CegoDbHandler::REORGOBJ;
2283 	}
2284 	else if (  req == Chain(SER_SYNC) )
2285 	{
2286 	    return CegoDbHandler::SYNC;
2287 	}
2288 	else if (  req == Chain(SER_GETPAGECOUNT) )
2289 	{
2290 	    return CegoDbHandler::GETPAGECOUNT;
2291 	}
2292 	else if (  req == Chain(SER_PUTBLOB) )
2293 	{
2294 	    _serTableSet = _pSer->readChain();
2295 	    _serBlobSize = _pSer->readChain().asUnsignedLongLong();
2296 	    return CegoDbHandler::PUTBLOB;
2297 	}
2298 	else if (  req == Chain(SER_GETBLOB) )
2299 	{
2300 	    _serTableSet = _pSer->readChain();
2301 	    _serPageId = (PageIdType)_pSer->readChain().asUnsignedLongLong();
2302 	    return CegoDbHandler::GETBLOB;
2303 	}
2304 	else if (  req == Chain(SER_DELBLOB) )
2305 	{
2306 	    return CegoDbHandler::DELBLOB;
2307 	}
2308 	else if (  req == Chain(SER_PUTCLOB) )
2309 	{
2310 	    _serTableSet = _pSer->readChain();
2311 	    _serClobSize = _pSer->readChain().asUnsignedLongLong();
2312 	    return CegoDbHandler::PUTCLOB;
2313 	}
2314 	else if (  req == Chain(SER_GETCLOB) )
2315 	{
2316 	    _serTableSet = _pSer->readChain();
2317 	    _serPageId = (PageIdType)_pSer->readChain().asUnsignedLongLong();
2318 	    return CegoDbHandler::GETCLOB;
2319 	}
2320 	else if (  req == Chain(SER_DELCLOB) )
2321 	{
2322 	    return CegoDbHandler::DELCLOB;
2323 	}
2324 	else if (  req == Chain(SER_STARTTRANSACTION) )
2325 	{
2326 	    return CegoDbHandler::STARTTRANSACTION;
2327 	}
2328 	else if (  req == Chain(SER_COMMITTRANSACTION) )
2329 	{
2330 	    return CegoDbHandler::COMMITTRANSACTION;
2331 	}
2332 	else if (  req == Chain(SER_ROLLBACKTRANSACTION) )
2333 	{
2334 	    return CegoDbHandler::ROLLBACKTRANSACTION;
2335 	}
2336 	else if (  req == Chain(SER_GETTID) )
2337 	{
2338 	    return CegoDbHandler::GETTID;
2339 	}
2340 	else if (  req == Chain(SER_SESSION_CLOSE) )
2341 	{
2342 	    return CegoDbHandler::SESSION_CLOSE;
2343 	}
2344 	else
2345 	{
2346 	    return CegoDbHandler::UNKNOWN;
2347 	}
2348     }
2349 }
2350 
getQueryArg()2351 Chain CegoDbHandler::getQueryArg()
2352 {
2353     if ( _protType == CegoDbHandler::XML )
2354     {
2355 	Element *pRoot = _xml.getDocument()->getRootElement();
2356 
2357 	if ( pRoot )
2358 	{
2359 	    _serQueryCmd = pRoot->getAttributeValue(XML_CMD_ATTR);
2360 	}
2361     }
2362     return _serQueryCmd;
2363 }
2364 
getPutBlobArg(Chain & tableSet,unsigned long long & blobSize)2365 void CegoDbHandler::getPutBlobArg(Chain& tableSet, unsigned long long& blobSize)
2366 {
2367     if ( _protType == CegoDbHandler::XML )
2368     {
2369 	Element *pRoot = _xml.getDocument()->getRootElement();
2370 	if ( pRoot )
2371 	{
2372 	    tableSet = pRoot->getAttributeValue(XML_TABLESET_ATTR);
2373 	    blobSize = pRoot->getAttributeValue(XML_SIZE_ATTR).asUnsignedLongLong();
2374 	}
2375     }
2376     else
2377     {
2378 	tableSet = _serTableSet;
2379 	blobSize = _serBlobSize;
2380     }
2381 }
2382 
getGetBlobArg(Chain & tableSet,PageIdType & pageId)2383 void CegoDbHandler::getGetBlobArg(Chain& tableSet, PageIdType& pageId)
2384 {
2385     if ( _protType == CegoDbHandler::XML )
2386     {
2387 	Element *pRoot = _xml.getDocument()->getRootElement();
2388 	if ( pRoot )
2389 	{
2390 	    tableSet = pRoot->getAttributeValue(XML_TABLESET_ATTR);
2391 	    pageId = (PageIdType)pRoot->getAttributeValue(XML_PAGEID_ATTR).asUnsignedLongLong();
2392 	}
2393     }
2394     else
2395     {
2396 	tableSet = _serTableSet;
2397 	pageId = _serPageId;
2398     }
2399 }
2400 
getPutClobArg(Chain & tableSet,unsigned long long & clobSize)2401 void CegoDbHandler::getPutClobArg(Chain& tableSet, unsigned long long& clobSize)
2402 {
2403     if ( _protType == CegoDbHandler::XML )
2404     {
2405 	Element *pRoot = _xml.getDocument()->getRootElement();
2406 	if ( pRoot )
2407 	{
2408 	    tableSet = pRoot->getAttributeValue(XML_TABLESET_ATTR);
2409 	    clobSize = pRoot->getAttributeValue(XML_SIZE_ATTR).asUnsignedLongLong();
2410 	}
2411     }
2412     else
2413     {
2414 	tableSet = _serTableSet;
2415 	clobSize = _serClobSize;
2416     }
2417 }
2418 
getGetClobArg(Chain & tableSet,PageIdType & pageId)2419 void CegoDbHandler::getGetClobArg(Chain& tableSet, PageIdType& pageId)
2420 {
2421     if ( _protType == CegoDbHandler::XML )
2422     {
2423 	Element *pRoot = _xml.getDocument()->getRootElement();
2424 	if ( pRoot )
2425 	{
2426 	    tableSet = pRoot->getAttributeValue(XML_TABLESET_ATTR);
2427 	    pageId = pRoot->getAttributeValue(XML_PAGEID_ATTR).asUnsignedLongLong();
2428 	}
2429     }
2430     else
2431     {
2432 	tableSet = _serTableSet;
2433 	pageId = _serPageId;
2434     }
2435 }
2436 
collectSchema(const ListT<CegoField> & schema,const Chain & format)2437 void CegoDbHandler::collectSchema(const ListT<CegoField>& schema, const Chain& format)
2438 {
2439 #ifdef CGDEBUG
2440     _pModule->log(_modId, Logger::DEBUG, Chain("Collecting schema ..."));
2441 #endif
2442 
2443     if ( _protType == CegoDbHandler::XML )
2444     {
2445 	_xml.getDocument()->clear();
2446 
2447 	Element* pRoot = new Element(XML_FRAME_ELEMENT);
2448 	pRoot->setAttribute(XML_FORMAT_ATTR, format);
2449 
2450 	_xml.getDocument()->setRootElement(pRoot);
2451 	_xml.getDocument()->setDocType(XML_DATA_DOC);
2452 
2453 	CegoField *pF = schema.First();
2454 	while ( pF )
2455 	{
2456 	    Chain tname;
2457 	    if (pF->getTableAlias().length() > 0)
2458 	    {
2459 		tname = pF->getTableAlias();
2460 	    }
2461 	    else
2462 	    {
2463 		tname = pF->getTableName();
2464 	    }
2465 
2466 	    Element *pColElement = new Element(XML_SCHEMA_ELEMENT);
2467 	    pColElement->setAttribute(XML_TABLENAME_ATTR, tname);
2468 
2469 	    CegoXMLHelper xh;
2470 	    xh.setColInfo(pColElement, pF);
2471 
2472 	    pRoot->addContent(pColElement);
2473 	    pF = schema.Next();
2474 	}
2475 
2476 	Chain response;
2477 	_xml.getXMLChain(response);
2478 
2479 #ifdef CGDEBUG
2480 	_pModule->log(_modId, Logger::DEBUG, Chain("--- XML ---"));
2481 	_pModule->log(_modId, Logger::DEBUG, response);
2482 	_pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
2483 #endif
2484 
2485 	_pN->setMsg((char*)response, response.length());
2486 	_pN->writeMsg();
2487 
2488 	// document must be cleared because of follow up collectData calls
2489 	_xml.getDocument()->clear();
2490     }
2491     else
2492     {
2493 	_pSer->reset();
2494 	_pSer->writeChain(Chain(SER_SDATA));
2495 	_pSer->writeChain(format);
2496 	_pSer->writeSchema(schema);
2497 	_serSync = false;
2498     }
2499 }
2500 
sendObjInfo(CegoDecodableObject & oe)2501 void CegoDbHandler::sendObjInfo(CegoDecodableObject& oe)
2502 {
2503     if ( _protType == CegoDbHandler::XML )
2504     {
2505 	_xml.getDocument()->clear();
2506 
2507 	Element* pRoot = new Element(XML_FRAME_ELEMENT);
2508 	pRoot->addContent(oe.getElement());
2509 
2510 	_xml.getDocument()->setRootElement(pRoot);
2511 	_xml.getDocument()->setDocType(XML_INFO_DOC);
2512 
2513 	Chain response;
2514 	_xml.getXMLChain(response);
2515 
2516 #ifdef CGDEBUG
2517 	_pModule->log(_modId, Logger::DEBUG, Chain("--- XML ---"));
2518 	_pModule->log(_modId, Logger::DEBUG, response);
2519 	_pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
2520 #endif
2521 
2522 	_pN->setMsg((char*)response, response.length());
2523 	_pN->writeMsg();
2524 
2525 	_xml.getDocument()->clear();
2526     }
2527     else
2528     {
2529 	_pSer->reset();
2530 	_pSer->writeChain(Chain(SER_INFO));
2531 	_pSer->writeObject(oe);
2532 
2533 #ifdef CGDEBUG
2534 	if ( _protType == CegoDbHandler::SERIAL )
2535 	{
2536 	    _pModule->log(_modId, Logger::DEBUG, Chain("--- SER ---"));
2537 	    _pModule->log(_modId, Logger::DEBUG, Chain(_pN->getMsg(), _pN->getMsgSize()));
2538 	    _pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
2539 	}
2540 #endif
2541 
2542 	_pN->writeMsg();
2543 	_pSer->reset();
2544     }
2545 }
2546 
collectData(const ListT<CegoField> & schema)2547 void CegoDbHandler::collectData(const ListT<CegoField>& schema)
2548 {
2549     if ( _protType == CegoDbHandler::XML )
2550     {
2551 	Element* pRoot = _xml.getDocument()->getRootElement();
2552 	if ( pRoot == 0)
2553 	{
2554 	    pRoot = new Element(XML_FRAME_ELEMENT);
2555 	    _xml.getDocument()->setRootElement(pRoot);
2556 	}
2557 
2558 	Element *pRowElement = new Element(XML_ROW_ELEMENT);
2559 
2560 	int col=1;
2561 	CegoField *pF = schema.First();
2562 	while ( pF )
2563 	{
2564 
2565 	    Chain colPos = Chain(NETMNG_COLPREFIX) + Chain(col);
2566 
2567 	    if ( ! pF->getValue().isNull() )
2568 	    {
2569 		pRowElement->setAttribute(colPos, pF->getValue().valAsChain());
2570 	    }
2571 	    col++;
2572 	    pF = schema.Next();
2573 	}
2574 
2575 	pRoot->addContent(pRowElement);
2576     }
2577     else
2578     {
2579 	if ( _pSer->isReset() )
2580 	    _pSer->writeChain(SER_FDATA);
2581 
2582 	_pSer->writeRow(schema);
2583     }
2584 }
2585 
collectData(const ListT<CegoField> & schema,const ListT<CegoFieldValue> & fvl)2586 void CegoDbHandler::collectData(const ListT<CegoField>& schema, const ListT<CegoFieldValue>& fvl)
2587 {
2588     if ( _protType == CegoDbHandler::XML )
2589     {
2590 	Element* pRoot = _xml.getDocument()->getRootElement();
2591 	if ( pRoot == 0)
2592 	{
2593 	    pRoot = new Element(XML_FRAME_ELEMENT);
2594 	    _xml.getDocument()->setRootElement(pRoot);
2595 	}
2596 
2597 	Element *pRowElement = new Element(XML_ROW_ELEMENT);
2598 
2599 	int col=1;
2600 	CegoField *pF = schema.First();
2601 	CegoFieldValue *pFV = fvl.First();
2602 
2603 	while ( pF && pFV )
2604 	{
2605 	    Chain colPos = Chain(NETMNG_COLPREFIX) + Chain(col);
2606 
2607 	    if ( ! pFV->isNull() )
2608 	    {
2609 		pRowElement->setAttribute(colPos, pFV->valAsChain());
2610 	    }
2611 	    col++;
2612 	    pF = schema.Next();
2613 	    pFV = fvl.Next();
2614 	}
2615 
2616 	pRoot->addContent(pRowElement);
2617     }
2618     else
2619     {
2620 	if ( _pSer->isReset() )
2621 	    _pSer->writeChain(SER_FDATA);
2622 	_pSer->writeRow(fvl);
2623     }
2624 }
2625 
numCollected() const2626 int CegoDbHandler::numCollected() const
2627 {
2628     if ( _protType == CegoDbHandler::XML )
2629     {
2630 	// not supported for XML protocol
2631 	return 0;
2632     }
2633     else
2634     {
2635 	return _pSer->numWritten();
2636     }
2637 }
2638 
getNetHandler()2639 NetHandler* CegoDbHandler::getNetHandler()
2640 {
2641     return _pN;
2642 }
2643 
sendXMLReq(const Chain & reqType,Element * pRoot)2644 CegoDbHandler::ResultType CegoDbHandler::sendXMLReq(const Chain& reqType, Element* pRoot)
2645 {
2646     _xml.getDocument()->clear();
2647     _xml.getDocument()->setRootElement(pRoot);
2648     _xml.getDocument()->setDocType(reqType);
2649 
2650     Chain request;
2651     _xml.getXMLChain(request);
2652     _xml.getDocument()->clear();
2653 
2654 #ifdef CGDEBUG
2655     _pModule->log(_modId, Logger::DEBUG, Chain("--- XML ---"));
2656     _pModule->log(_modId, Logger::DEBUG, request);
2657     _pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
2658 #endif
2659 
2660     _pN->setMsg(request, request.length());
2661 
2662     _pN->writeMsg();
2663 
2664     _pN->readMsg();
2665 
2666 #ifdef CGDEBUG
2667     _pModule->log(_modId, Logger::DEBUG, Chain("--- XML ---"));
2668     _pModule->log(_modId, Logger::DEBUG, _pN->getMsg());
2669     _pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
2670 #endif
2671 
2672     _xml.getDocument()->clear();
2673     _xml.setChain( _pN->getMsg() );
2674 
2675     Chain docType;
2676 
2677     try
2678     {
2679 	_xml.parse();
2680 	docType = _xml.getDocument()->getDocType();
2681     }
2682     catch ( Exception e )
2683     {
2684 	docType = Chain("INVALID");
2685     }
2686 
2687 
2688     if ( docType == Chain(XML_OK_DOC) )
2689     {
2690 	return DB_OK;
2691     }
2692     else if ( docType == Chain(XML_SACK_DOC) )
2693     {
2694 
2695 	Element *pRoot = _xml.getDocument()->getRootElement();
2696 
2697 	if ( pRoot )
2698 	{
2699 	    _serMsg = pRoot->getAttributeValue(XML_MSG_ATTR);
2700 	    _serTid = pRoot->getAttributeValue(XML_TID_ATTR).asUnsignedLongLong();
2701 	    _serDbName = pRoot->getAttributeValue(XML_DBPRODNAME_ATTR);
2702 	    _serDbVersion = pRoot->getAttributeValue(XML_DBPRODVERSION_ATTR);
2703 	    _serDateFormat = pRoot->getAttributeValue(XML_DATETIMEFORMAT_ATTR);
2704 	    _serQuoteEscapeFlag = ( pRoot->getAttributeValue(XML_QESCMODE_ATTR) == Chain(XML_ON_VALUE) );
2705 	}
2706 	return DB_OK;
2707     }
2708     else if ( docType == Chain(XML_ERROR_DOC) )
2709     {
2710 
2711 	Element *pRoot = _xml.getDocument()->getRootElement();
2712 
2713 	if ( pRoot )
2714 	{
2715 	    _serMsg = pRoot->getAttributeValue(XML_MSG_ATTR);
2716 	}
2717 
2718 	return DB_ERROR;
2719     }
2720     else if ( docType == Chain(XML_INFO_DOC) )
2721     {
2722 	return DB_INFO;
2723     }
2724     else
2725     {
2726 	_serMsg =  Chain("Wrong protocol");
2727 	return DB_ERROR;
2728     }
2729 }
2730 
sendSerialReq()2731 CegoDbHandler::ResultType CegoDbHandler::sendSerialReq()
2732 {
2733 
2734 #ifdef CGDEBUG
2735     if ( _protType == CegoDbHandler::SERIAL )
2736     {
2737 	_pModule->log(_modId, Logger::DEBUG, Chain("--- SER ---"));
2738 	_pModule->log(_modId, Logger::DEBUG, Chain(_pN->getMsg(), _pN->getMsgSize()));
2739 	_pModule->log(_modId, Logger::DEBUG, Chain("--- --- ---"));
2740     }
2741 #endif
2742 
2743     // cout << "Writing req " << Chain(_pN->getMsg(), _pN->getMsgSize()) << endl;
2744     _pN->writeMsg();
2745 
2746     _pN->readMsg();
2747 
2748     _pSer->reset();
2749     Chain req = _pSer->readChain();
2750 
2751     // cout << "Reading req " << req << endl;
2752 
2753     if ( req == Chain(SER_OK))
2754     {
2755 	_serMsg = _pSer->readChain();
2756 	_serAffected = _pSer->readChain().asInteger();
2757 	return DB_OK;
2758     }
2759     else if ( req == Chain(SER_FIN))
2760     {
2761 	_serMsg = Chain("No rows");
2762 	_serAffected = 0;
2763 	return DB_FIN;
2764     }
2765     else if ( req == Chain(SER_SACK))
2766     {
2767 	_serMsg = _pSer->readChain();
2768 	_serTid = _pSer->readChain().asUnsignedLongLong();
2769 	_serDbName = _pSer->readChain();
2770 	_serDbVersion = _pSer->readChain();
2771 	_serDateFormat = _pSer->readChain();
2772 
2773 	Chain qescmode = _pSer->readChain();
2774 	_serQuoteEscapeFlag = ( qescmode == Chain("Y") );
2775 
2776 	return DB_OK;
2777     }
2778     else if ( req == Chain(SER_PROCRES))
2779     {
2780 	_serMsg = Chain("Procedure executed");
2781 
2782 	while ( _pSer->numAhead() > 0 )
2783 	{
2784 	    Chain paramName = _pSer->readChain();
2785 	    Chain paramType = _pSer->readChain();
2786 	    Chain paramValue = _pSer->readChain();
2787 
2788 	    CegoTypeConverter tc;
2789 	    CegoDataType datatype = tc.getTypeId(paramType);
2790 
2791 	    if ( paramName == Chain("@OUT") )
2792 	    {
2793 		_retValue = CegoFieldValue(datatype, paramValue);
2794 	    }
2795 	    else
2796 	    {
2797 		CegoFieldValue fv(datatype, paramValue);
2798 		_outParamList.Insert( CegoProcVar(paramName, CegoProcVar::OUTVAR, fv.getType(), fv.getLength(), fv.getDim(), fv));
2799 	    }
2800 	}
2801 	return DB_OK;
2802     }
2803     else if ( req == Chain(SER_ERROR) )
2804     {
2805 	_serMsg = _pSer->readChain();
2806 	return DB_ERROR;
2807     }
2808     else if ( req == Chain(SER_INFO) )
2809     {
2810 	_serMsg = _pSer->readChain();
2811 	return DB_INFO;
2812     }
2813     else if ( req == Chain(SER_SDATA) )
2814     {
2815 	_serFormat = _pSer->readChain();
2816 	_serSchema = _pSer->readSchema();
2817 	return DB_DATA;
2818     }
2819     else
2820     {
2821 	_serMsg = Chain("Wrong protocol");
2822 	return DB_ERROR;
2823     }
2824 }
2825