/*
*
* ProLinga-Data
*
* Copyright (C) 2002-2008 Xobas Software.
* All rights reserved.
*
* This file is part of ProLinga-Data.
*
* ProLinga-Data is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ProLinga-Data is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ProLinga-Data. If not, see .
*
* More information is available at the following addresses:
*
* Website : http://www.prolinga.org
*
* Email : prolinga-list@prolinga.org
*
*
*/
#include "DatCommon.h"
#include
#include
#include
#include
#include "DatMain.hpp"
#include "DCDataSource.hpp"
#include "DCSession.hpp"
#include "DatConfig.hpp"
extern DCSessionPtr sesPtr;
char *ConnectionError(GdaConnection * connection)
{
GList *list;
GList *node;
GdaConnectionEvent *error;
char errorString[4096];
char *ep;
list = (GList *) gda_connection_get_events (connection);
for (node = g_list_first (list); node != NULL; node = g_list_next (node))
{
error = (GdaConnectionEvent *) node->data;
sprintf(errorString,"Error no: %ld - Desc: %s - Source: %s - Sqlstate: %s", \
gda_connection_event_get_code (error), \
gda_connection_event_get_description (error), \
gda_connection_event_get_source (error), \
gda_connection_event_get_sqlstate (error));
/* Just get the first error */
break;
}
ep = new char[(strlen(errorString)+1)];
strcpy(ep, errorString);
/* Return */
return ep;
}
int DCOpenDataSource(const xmlDocPtr docObj, const char *sessionId, char **externalError, xmlDocPtr *docReturn)
{
xmlNodePtr curReturn, curTmp;
xmlXPathContextPtr ctxObj;
xmlXPathObjectPtr resObj;
char *objDataSource, *objUserName = NULL, *objPassWord = NULL;
int retValue = 0, options = 0;
/* Init XPath */
// xmlXPathInit();
/* Create XPath Environment */
ctxObj = xmlXPathNewContext(docObj);
/* Get DataSource Name */
resObj = xmlXPathEval((const xmlChar *)"/ProLinga/Object/@DataSource", ctxObj);
objDataSource = (char *)xmlXPathCastToString(resObj);
/* Get User Name */
resObj = xmlXPathEval((const xmlChar *)"/ProLinga/Object/UserName", ctxObj);
objUserName = (char *)xmlXPathCastToString(resObj);
if ((objUserName != NULL) && (strlen(objUserName) == 0))
objUserName = NULL;
/* Get Password */
resObj = xmlXPathEval((const xmlChar *)"/ProLinga/Object/Password", ctxObj);
objPassWord = (char *)xmlXPathCastToString(resObj);
if ((objPassWord != NULL) && (strlen(objPassWord) == 0))
objPassWord = NULL;
/* Get Options */
resObj = xmlXPathEval((const xmlChar *)"/ProLinga/Object/ReadOnlyConnection", ctxObj);
if (strcmp((char *)xmlXPathCastToString(resObj), "True") == 0)
options = options + 1;
resObj = xmlXPathEval((const xmlChar *)"/ProLinga/Object/DontShareConnection", ctxObj);
if (strcmp((char *)xmlXPathCastToString(resObj), "True") == 0)
options = options + 2;
/* Check if connection exist. If not open connection */
retValue = sesPtr->openGdaConnection(sesPtr, sessionId, objDataSource, objUserName, objPassWord, options);
if (retValue != 0)
return retValue;
/* Create return document */
*docReturn = xmlNewDoc((const xmlChar *)XML_VERSION);
(*docReturn)->children = xmlNewDocNode(*docReturn, NULL, (const xmlChar *)ROOT_ELEM, NULL);
curReturn = xmlDocGetRootElement(*docReturn);
curTmp = xmlNewTextChild(curReturn, NULL, (const xmlChar *)"Object", (const xmlChar *)"");
xmlNewProp(curTmp, (const xmlChar *)"DataSource", (const xmlChar *)objDataSource);
/* Return */
return 0;
}
int DCCloseDataSource(const xmlDocPtr docObj, const char *sessionId, char **externalError, xmlDocPtr *docReturn)
{
xmlNodePtr curReturn, curTmp;
xmlXPathContextPtr ctxObj;
xmlXPathObjectPtr resObj;
char *objDataSource;
int retValue = 0;
/* Init XPath */
// xmlXPathInit();
/* Create XPath Environment */
ctxObj = xmlXPathNewContext(docObj);
/* Get DataSource Name */
resObj = xmlXPathEval((const xmlChar *)"/ProLinga/Object/@DataSource", ctxObj);
objDataSource = (char *)xmlXPathCastToString(resObj);
/* Check if connection exist. If not close connection */
retValue = sesPtr->closeGdaConnection(sesPtr, sessionId, objDataSource);
if (retValue == 0)
{
/* Create return document */
*docReturn = xmlNewDoc((const xmlChar *)XML_VERSION);
(*docReturn)->children = xmlNewDocNode(*docReturn, NULL, (const xmlChar *)ROOT_ELEM, NULL);
curReturn = xmlDocGetRootElement(*docReturn);
curTmp = xmlNewTextChild(curReturn, NULL, (const xmlChar *)"Object", (const xmlChar *)"");
xmlNewProp(curTmp, (const xmlChar *)"DataSource", (const xmlChar *)objDataSource);
}
else
retValue = 70002;
/* Return */
return retValue;
}