/*
*
* ProLinga-Data
*
* Copyright (C) 2002-2009 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 "DatConfig.hpp"
#include "DCSession.hpp"
#include "Utility.hpp"
DCDataModel::DCDataModel()
{
strcpy(modelName, "");
dm = NULL;
rowId = 0;
nextPtr = NULL;
}
DCDataModel::~DCDataModel()
{
/* Nothing ? */
}
GdaDataModel *DCDataModel::getDataModel(DCDataModel **modelPtr, const char *name)
{
DCDataModel *curPtr;
/* Check if connection exists in memory */
curPtr = *modelPtr;
while (curPtr != NULL)
{
if (strcmp(curPtr->modelName, name) == 0)
return curPtr->dm;
curPtr = curPtr->nextPtr;
}
/* Return */
return NULL;
}
void DCDataModel::putDataModel(DCDataModel **modelPtr, const char *name, GdaDataModel *dmp)
{
DCDataModel *curPtr, *newPtr, *prevPtr;
/* Check if connection exists in memory */
curPtr = *modelPtr;
prevPtr = NULL;
while (curPtr != NULL)
{
if (strcmp(curPtr->modelName, name) == 0)
{
g_object_unref(curPtr->dm);
curPtr->dm = dmp;
break;
}
prevPtr = curPtr;
curPtr = curPtr->nextPtr;
}
/* Create new data model */
newPtr = new DCDataModel;
strcpy(newPtr->modelName, name);
newPtr->dm = dmp;
newPtr->nextPtr = NULL;
/* Append data model in list */
if (prevPtr == NULL)
*modelPtr = newPtr;
else
prevPtr->nextPtr = newPtr;
}
int DCDataModel::deleteOneDataModel(DCDataModel **modelPtr, const char *name)
{
DCDataModel *curPtr, *prevPtr;
int retValue = -1;
/* Check if connection exists in memory */
curPtr = *modelPtr;
prevPtr = NULL;
while (curPtr != NULL)
{
if (strcmp(curPtr->modelName, name) == 0)
{
/* Unlink data model */
if (prevPtr == NULL)
*modelPtr = curPtr->nextPtr;
else
prevPtr->nextPtr = curPtr->nextPtr;
/* Delete Data Model */
delete curPtr;
retValue = 0;
break;
}
prevPtr = curPtr;
curPtr = curPtr->nextPtr;
}
/* Return */
return retValue;
}
void DCDataModel::deleteList(DCDataModel **modelPtr)
{
DCDataModel *curPtr, *nextPtr;
curPtr = *modelPtr;
*modelPtr = NULL;
while (curPtr != NULL)
{
nextPtr = curPtr->nextPtr;
g_object_unref(curPtr->dm);
delete curPtr;
curPtr = nextPtr;
}
}
DCConnection::DCConnection()
{
strcpy(conId, "");
connection = NULL;
metaStore = NULL;
nextPtr = NULL;
}
DCConnection::~DCConnection()
{
g_object_unref(metaStore);
/* Nothing; Connections will be freed by "gda_client_close_all_connections" call */
if (connection != NULL)
gda_connection_close(connection);
}
int DCConnection::openGdaConnection(DCConnection **conPtr, \
const char *connectionId, const char *userName, \
const char *passWord, GdaConnectionOptions gdaOptions)
{
DCConnectionPtr curPtr, newPtr, prevPtr;
GdaConnection *tmpCon;
GdaMetaStore *tmpStore;
int retValue = 0;
char dsn[255];
char *metaStoreFile;
bool storeExists, metaStoreUpdateOK;
/* Check if connection exists in memory */
curPtr = *conPtr;
prevPtr = NULL;
while (curPtr != NULL)
{
if (strcmp(curPtr->conId, connectionId) == 0)
{
return 70014;
}
prevPtr = curPtr;
curPtr = curPtr->nextPtr;
}
/* Create connection id */
if (userName != NULL)
{
strcpy(dsn,gda_rfc1738_encode(userName));
if (passWord != NULL)
{
strcat(dsn,":");
strcat(dsn,gda_rfc1738_encode(passWord));
}
strcat(dsn,"@");
strcat(dsn,connectionId);
}
else
strcpy(dsn,connectionId);
/* Open new connection */
tmpCon = gda_connection_open_from_dsn(dsn, NULL, gdaOptions, NULL);
if (tmpCon == NULL)
return 70001;
/* Get Meta Store DB Name */
metaStoreFile = new char[strlen(DEF_META_STORE_DIR)+strlen(connectionId)+6];
strcpy(metaStoreFile, DEF_META_STORE_DIR);
strcat(metaStoreFile, "/");
strcat(metaStoreFile, connectionId);
strcat(metaStoreFile, ".db");
/* Check if Meta Store exists */
storeExists = FileExists(metaStoreFile);
/* Open Meta Store */
// tmpStore = GDA_META_STORE(g_object_new(GDA_TYPE_META_STORE,"cnc", tmpCon, "schema", "metastore",NULL));
tmpStore = gda_meta_store_new_with_file(metaStoreFile);
g_object_set(G_OBJECT(tmpCon), "meta-store", tmpStore, NULL);
delete metaStoreFile;
/* Initialize Meta Store if opened first time */
if (storeExists != TRUE)
{
metaStoreUpdateOK = gda_connection_update_meta_store(tmpCon, NULL, NULL);
if (metaStoreUpdateOK == FALSE)
return 70028;
}
/* Create new Connection Record */
newPtr = new DCConnection;
strcpy(newPtr->conId, connectionId);
newPtr->connection = tmpCon;
newPtr->metaStore = tmpStore;
newPtr->nextPtr = NULL;
if (prevPtr == NULL)
*conPtr = newPtr;
else
prevPtr->nextPtr = newPtr;
/* Return */
return retValue;
}
int DCConnection::closeGdaConnection(DCConnection **conPtr, const char *connectionId)
{
DCConnectionPtr curPtr, prevPtr, nextPtr;
int retValue = 1;
/* Check if connection exists in memory */
prevPtr = NULL;
curPtr = *conPtr;
nextPtr = NULL;
while (curPtr != NULL)
{
nextPtr = curPtr->nextPtr;
if (strcmp(curPtr->conId, connectionId) == 0)
{
if (prevPtr == NULL)
*conPtr = nextPtr;
else
prevPtr->nextPtr = nextPtr;
delete curPtr;
retValue = 0;
break;
}
prevPtr = curPtr;
curPtr = curPtr->nextPtr;
}
/* Return */
return retValue;
}
GdaConnection *DCConnection::getGdaConnection(DCConnectionPtr *conPtr, const char *connectionId)
{
DCConnectionPtr curPtr;
/* Check if connection exists in memory */
curPtr = *conPtr;
while (curPtr != NULL)
{
if (strcmp(curPtr->conId, connectionId) == 0)
return curPtr->connection;
curPtr = curPtr->nextPtr;
}
/* Return */
return NULL;
}
GdaMetaStore *DCConnection::getGdaMetaStore(DCConnectionPtr *conPtr, const char *connectionId)
{
DCConnectionPtr curPtr;
/* Check if connection exists in memory */
curPtr = *conPtr;
while (curPtr != NULL)
{
if (strcmp(curPtr->conId, connectionId) == 0)
return curPtr->metaStore;
curPtr = curPtr->nextPtr;
}
/* Return */
return NULL;
}
void DCConnection::deleteList(DCConnection **conPtr)
{
DCConnection *curPtr, *nextPtr;
curPtr = *conPtr;
*conPtr = NULL;
while (curPtr != NULL)
{
nextPtr = curPtr->nextPtr;
delete curPtr;
curPtr = nextPtr;
}
}
void DCConnection::printList(DCConnectionPtr conPtr) const
{
DCConnectionPtr curPtr;
curPtr = conPtr;
while (curPtr != NULL)
{
printf("Value %s\n", curPtr->conId);
curPtr = curPtr->nextPtr;
}
}
DCSession::DCSession()
{
strcpy(sessionId, "");
conStartPtr = NULL;
transactionId = NULL;
// transaction = NULL;
modelStartPtr = NULL;
nextPtr = NULL;
}
DCSession::~DCSession()
{
/* Free transaction object */
if (transactionId != NULL)
{
delete transactionId;
// g_object_unref(transaction);
}
/* Delete Connection entries */
if (conStartPtr != NULL)
conStartPtr->deleteList(&conStartPtr);
/* Delete Data Models */
if (modelStartPtr != NULL)
modelStartPtr->deleteList(&modelStartPtr);
}
bool DCSession::sessionExists(DCSessionPtr *sesPtr, const char *session)
{
DCSessionPtr curPtr;
/* Check if session exists in memory */
curPtr = *sesPtr;
while (curPtr != NULL)
{
if (strcmp(curPtr->sessionId, session) == 0)
return true;
curPtr = curPtr->nextPtr;
}
return false;
}
char *DCSession::createSession(DCSessionPtr *sesPtr, char *retValue)
{
DCSessionPtr newPtr;
char randomChars[]="abcdefghijklmnopqrstuvwxyz0123456789-";
char sesId[32] = "\0";
//char libgdaConfigFileName[1024], shellCmd[1024];
int i,j;
int randomNumber;
// gchar *file_contents;
// gsize len;
/* Generate SessionId */
j = strlen(randomChars);
srand(time(NULL));
sesId[0] = 'P';
sesId[1] = 'D';
sesId[2] = '-';
for (i=3;i<=30;i++)
{
randomNumber = (rand() %j);
sesId[i] = randomChars[randomNumber];
}
sesId[i] = '\0';
// /* TEST/DEBUG ONLY */
// if (*sesPtr == NULL)
// strcpy(sesId, "PD-Test0");
// else
// strcpy(sesId, "PD-Test1");
// /* TEST/DEBUG ONLY */
/* Create new Session */
newPtr = new DCSession;
strcpy(newPtr->sessionId, sesId);
/* Check if already exists */
/* Link in List */
newPtr->nextPtr = *sesPtr;
*sesPtr = newPtr;
/* FIXME: libgda */
/* Copy libgda config file in place */
/* This should be fixed when libgda matures */
/* Get config file name */
//sprintf(libgdaConfigFileName, "%s/.libgda/config", g_get_home_dir());
/* Copy file (if source exists and target writable */
//sprintf(shellCmd,"cp /etc/libgda/config.prolinga %s 2>1", libgdaConfigFileName);
//system(shellCmd);
/* Check if file contains data */
//g_file_get_contents (libgdaConfigFileName, &file_contents, &len, NULL);
/* Return */
strcpy(retValue, newPtr->sessionId);
return retValue;
}
int DCSession::destroySession(DCSessionPtr *sesPtr, const char *session)
{
DCSessionPtr curPtr, prevPtr;
/* Init */
curPtr = *sesPtr;
prevPtr = NULL;
/* Locate Session */
while (curPtr != NULL)
{
if (strcmp(curPtr->sessionId, session) == 0)
{
/* Unlink entry */
if (prevPtr == NULL)
*sesPtr = curPtr->nextPtr;
else
prevPtr->nextPtr = curPtr->nextPtr;
/* Delete entry */
delete curPtr;
/* End */
break;
}
prevPtr = curPtr;
curPtr = curPtr->nextPtr;
}
/* Return */
return 0;
}
int DCSession::openGdaConnection(DCSessionPtr sesPtr, const char *session, const char *connectionId, \
const char *userName, const char *passWord, const GdaConnectionOptions gdaOptions)
{
DCSessionPtr curPtr;
int retValue = 0;
/* Check if session exists in memory */
curPtr = sesPtr;
while (curPtr != NULL)
{
if (strcmp(curPtr->sessionId, session) == 0)
{
retValue = curPtr->conStartPtr->openGdaConnection(&curPtr->conStartPtr, \
connectionId, userName, passWord, gdaOptions);
break;
}
curPtr = curPtr->nextPtr;
}
return retValue;
}
int DCSession::closeGdaConnection(DCSessionPtr sesPtr, const char *session, const char *connectionId)
{
DCSessionPtr curPtr;
int retValue = 0;
/* Check if session exists in memory */
curPtr = sesPtr;
while (curPtr != NULL)
{
if (strcmp(curPtr->sessionId, session) == 0)
{
retValue = curPtr->conStartPtr->closeGdaConnection(&curPtr->conStartPtr, connectionId);
break;
}
curPtr = curPtr->nextPtr;
}
return retValue;
}
GdaConnection *DCSession::getGdaConnection(DCSessionPtr sesPtr, const char *session, const char *connectionId)
{
DCSessionPtr curPtr;
/* Check if session exists in memory */
curPtr = sesPtr;
while (curPtr != NULL)
{
if (strcmp(curPtr->sessionId, session) == 0)
return curPtr->conStartPtr->getGdaConnection(&curPtr->conStartPtr, connectionId);
curPtr = curPtr->nextPtr;
}
return NULL;
}
void DCSession::clearTransactionId(DCSession *sesPtr, const char *session)
{
DCSessionPtr curPtr;
/* Check if session exists in memory */
curPtr = sesPtr;
while (curPtr != NULL)
{
if (strcmp(curPtr->sessionId, session) == 0)
{
if (curPtr->transactionId != NULL)
{
delete curPtr->transactionId;
// g_object_unref(curPtr->transaction);
curPtr->transactionId = NULL;
}
}
curPtr = curPtr->nextPtr;
}
}
char *DCSession::getTransactionId(DCSession *sesPtr, const char *session)
{
DCSessionPtr curPtr;
/* Check if session exists in memory */
curPtr = sesPtr;
while (curPtr != NULL)
{
if (strcmp(curPtr->sessionId, session) == 0)
return curPtr->transactionId;
curPtr = curPtr->nextPtr;
}
/* Return */
return NULL;
}
void DCSession::putTransactionId(DCSession *sesPtr, const char *session, const char *transId)
{
DCSessionPtr curPtr;
/* Check if session exists in memory */
curPtr = sesPtr;
while (curPtr != NULL)
{
if (strcmp(curPtr->sessionId, session) == 0)
{
//curPtr->transaction = trans;
curPtr->transactionId = new char[(strlen(transId)+1)];
strcpy(curPtr->transactionId, transId);
break;
}
curPtr = curPtr->nextPtr;
}
}
GdaDataModel *DCSession::getDataModel(DCSessionPtr sesPtr, const char *session, const char *name)
{
DCSessionPtr curPtr;
/* Check if session exists in memory */
curPtr = sesPtr;
while (curPtr != NULL)
{
if (strcmp(curPtr->sessionId, session) == 0)
return curPtr->modelStartPtr->getDataModel(&curPtr->modelStartPtr, name);
curPtr = curPtr->nextPtr;
}
return NULL;
}
void DCSession::putDataModel(DCSessionPtr sesPtr, const char *session, const char *name, GdaDataModel *dmp)
{
DCSessionPtr curPtr;
/* Check if session exists in memory */
curPtr = sesPtr;
while (curPtr != NULL)
{
if (strcmp(curPtr->sessionId, session) == 0)
{
curPtr->modelStartPtr->putDataModel(&curPtr->modelStartPtr, name, dmp);
break;
}
curPtr = curPtr->nextPtr;
}
}
void DCSession::deleteAllDataModels(DCSessionPtr sesPtr, const char *session)
{
DCSessionPtr curPtr;
/* Check if session exists in memory */
curPtr = sesPtr;
while (curPtr != NULL)
{
if (strcmp(curPtr->sessionId, session) == 0)
{
curPtr->modelStartPtr->deleteList(&curPtr->modelStartPtr);
return;
}
curPtr = curPtr->nextPtr;
}
}
int DCSession::deleteOneDataModel(DCSessionPtr sesPtr, const char *session, const char *name)
{
DCSessionPtr curPtr;
/* Check if session exists in memory */
curPtr = sesPtr;
while (curPtr != NULL)
{
if (strcmp(curPtr->sessionId, session) == 0)
return curPtr->modelStartPtr->deleteOneDataModel(&curPtr->modelStartPtr, name);
curPtr = curPtr->nextPtr;
}
/* Return */
return -1;
}
void DCSession::printList(DCSessionPtr sesPtr) const
{
DCSessionPtr curPtr;
curPtr = sesPtr;
while (curPtr != NULL)
{
printf("Value %s\n", curPtr->sessionId);
curPtr = curPtr->nextPtr;
}
}