/*
*
* ProLinga-Run
*
* Copyright (C) 2002-2009 Xobas Software.
* All rights reserved.
*
* This file is part of ProLinga-Run.
*
* ProLinga-Run 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-Run 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-Run. If not, see .
*
* More information is available at the following addresses:
*
* Website : http://www.prolinga.org
*
* Email : prolinga-list@prolinga.org
*
*
*/
#include "RunCommon.h"
#include
#include
#include
#include "Main.hpp"
#include "DataRef.hpp"
#include "BuiltIn.hpp"
#include "DataCommand.hpp"
extern CaDataSessionPtr caDataSesPtr;
extern BiSqlInfoPtr biSqlInfoPtr;
extern BiSqlReturnPtr biSqlReturnPtr;
extern BiSqlStatusPtr biSqlStatusPtr;
/* Table.Record */
extern TablePtr tablePtr;
/* Command : SQL */
/* Syntax : ... */
void CmdSql(const char **argList, int argHits, bool isMainThread)
{
char retValue[255], retValue2[255], cmdValue[102400], sqlCmd[10240];
char *token, *recordName, valueDate[11], *argListWork = NULL;
const char *value;
int i, j, k, numEntries = 0, dateFormat;
/* Init */
biSqlInfoPtr->setSqlInfo(NULL);
biSqlReturnPtr->setSqlReturn(0);
biSqlStatusPtr->setSqlStatus(0);
/* Connect */
if (strcmp(argList[1], "CONNECT") == 0)
{
/* Check if already connected */
if (caDataSesPtr->getDataSessionId(caDataSesPtr, getDataRef(argList[2], isMainThread, retValue, false)) != NULL)
biSqlStatusPtr->setSqlStatus(10201);
else
{
/* Get and Set Data Session Id */
biSqlStatusPtr->setSqlStatus(dataCreateDataSession(getDataRef(argList[2], isMainThread, retValue, false)));
}
}
else if (strcmp(argList[1], "DISCONNECT") == 0)
{
/* Check if connected */
if (caDataSesPtr->getDataSessionId(caDataSesPtr, getDataRef(argList[2], isMainThread, retValue, false)) == NULL)
biSqlStatusPtr->setSqlStatus(10202);
else
{
/* Reset Data Session Id */
biSqlStatusPtr->setSqlStatus(dataClearDataSession(getDataRef(argList[2], isMainThread, retValue, false)));
}
}
else if (strcmp(argList[1], "ACTION") == 0)
{
/* Create new SQL Command buffer */
strcpy(sqlCmd, getDataRef(argList[4], isMainThread, cmdValue, false));
/* Get rest of command */
for (i = 5; i<=argHits; i++)
{
if ( strncmp(argList[i], "EXPANDINSERT", 12) == 0)
{
/* Create workable argList entry */
argListWork = new char[(strlen(argList[i])+1)];
strcpy(argListWork,argList[i]);
/* Get Arguments */
token = strtok(argListWork, "(");
j = 1;
while (token != NULL)
{
if (j == 2)
recordName = token;
token = strtok(NULL, ")");
j++;
}
/* Get number of DataDictionaries record */
numEntries = tablePtr->getNumEntries(&tablePtr, recordName);
/* Process all entries */
for (k=1; k<=numEntries; k++)
{
/* Get Date Format Source (if applicable) */
dateFormat = tablePtr->getDateFormatSeqNo(tablePtr, recordName, k);
value = tablePtr->getValueSeqNo(&tablePtr, recordName, k);
if (dateFormat > 0)
{
if (value == NULL)
value = PL_INVALID_DATE;
else
{
FormatDate(value, dateFormat, valueDate, PL_DT_FORMAT_1);
value = valueDate;
}
}
if (value != NULL)
{
if (k>1)
strcat(sqlCmd, ",");
strcat(sqlCmd, "'");
strcat(sqlCmd, value);
strcat(sqlCmd, "'");
}
else
{
if (k>1)
strcat(sqlCmd, ",");
strcat(sqlCmd, "'");
strcat(sqlCmd, "");
strcat(sqlCmd, "'");
}
}
/* Clean up */
if (argListWork != NULL)
delete argListWork;
}
else if ( strncmp(argList[i], "EXPANDUPDATE", 12) == 0)
{
/* Create workable argList entry */
argListWork = new char[(strlen(argList[i])+1)];
strcpy(argListWork,argList[i]);
/* Get Arguments */
token = strtok(argListWork, "(");
j = 1;
while (token != NULL)
{
if (j == 2)
recordName = token;
token = strtok(NULL, ")");
j++;
}
/* Get number of DataDictionaries record */
numEntries = tablePtr->getNumEntries(&tablePtr, recordName);
/* Process all entries */
for (k=1; k<=numEntries; k++)
{
/* Get Date Format Source (if applicable) */
dateFormat = tablePtr->getDateFormatSeqNo(tablePtr, recordName, k);
value = tablePtr->getValueSeqNo(&tablePtr, recordName, k);
if (dateFormat > 0)
{
if (value == NULL)
value = PL_INVALID_DATE;
else
{
FormatDate(value, dateFormat, valueDate, PL_DT_FORMAT_1);
value = valueDate;
}
}
if (value != NULL)
{
if (k>1)
strcat(sqlCmd, ",");
strcat(sqlCmd, "\"");
strcat(sqlCmd, tablePtr->getNameSeqNo(&tablePtr, recordName, k));
strcat(sqlCmd, "\"");
strcat(sqlCmd, "=");
strcat(sqlCmd, "'");
strcat(sqlCmd, value);
strcat(sqlCmd, "'");
}
else
{
if (k>1)
strcat(sqlCmd, ",");
strcat(sqlCmd, "\"");
strcat(sqlCmd, tablePtr->getNameSeqNo(&tablePtr, recordName, k));
strcat(sqlCmd, "\"");
strcat(sqlCmd, "=");
strcat(sqlCmd, "'");
strcat(sqlCmd, "");
strcat(sqlCmd, "'");
}
}
/* Clean up */
if (argListWork != NULL)
delete argListWork;
}
else
{
//strcat(sqlCmd, " ");
getDataRef(argList[i], isMainThread, cmdValue, false);
/* Get Date Format (if applicable) */
dateFormat = getDateFormatDataRef(argList[i]);
if (dateFormat > 0)
FormatDate(cmdValue, dateFormat, cmdValue, PL_DT_FORMAT_1);
strcat(sqlCmd, cmdValue);
}
}
strcat(sqlCmd, "\0");
//printf("ACTION : %s\n", sqlCmd);
/* Execute Action Sql */
biSqlStatusPtr->setSqlStatus(dataExecuteAction(getDataRef(argList[2], isMainThread, retValue, false) , sqlCmd));
}
else if (strcmp(argList[1], "QUERY") == 0)
{
if (strcmp(argList[(argHits-1)], "TEXT") == 0)
{
/* Feature not implemented yet */
biSqlStatusPtr->setSqlStatus(10298);
return;
}
/* Create new SQL Command buffer */
strcpy(sqlCmd, getDataRef(argList[4], isMainThread, cmdValue, false));
/* Get total length of command */
for (i = 5; i<=(argHits-2); i++)
{
//strcat(sqlCmd, " ");
strcat(sqlCmd, getDataRef(argList[i], isMainThread, cmdValue, false));
}
strcat(sqlCmd, "\0");
//printf("QUERY : %s\n", sqlCmd);
/* Execute Query Sql */
biSqlStatusPtr->setSqlStatus(dataExecuteQueryDataModel(getDataRef(argList[2], isMainThread, retValue, false) , sqlCmd, getDataRef(argList[argHits], isMainThread, retValue2, false) ));
}
}