/* * * 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 #include "DatMain.hpp" #include "DCDataSource.hpp" #include "DCExecuteQuery.hpp" #include "ExecuteSql.hpp" #include "DCSession.hpp" #include "DatConfig.hpp" extern DCSessionPtr sesPtr; int DCExecuteQuery(const xmlDocPtr docObj, const char *sessionId, char **externalError, xmlDocPtr *docReturn) { xmlNodePtr curReturn, curTmp; xmlXPathContextPtr ctxObj; xmlXPathObjectPtr resObj; GdaConnection *connection; GdaDataModel *dm; GdaColumn *fa; gint rowId; gint columnId; GValue *value; char *objDataSource, *sqlBuffer, tmpString[255]; /* Create XPath Environment */ ctxObj = xmlXPathNewContext(docObj); /* Get DataSource name */ resObj = xmlXPathEval((const xmlChar *)"/ProLinga/Object/@DataSource", ctxObj); objDataSource = (char *)xmlXPathCastToString(resObj); /* Get Connection Object to Data Source */ connection = sesPtr->getGdaConnection(sesPtr, sessionId, objDataSource); if (connection == NULL) return 70002; /* Get SQL statement */ resObj = xmlXPathEval((const xmlChar *)"/ProLinga/Object/Query/Sql", ctxObj); sqlBuffer = new char[(strlen((char *)xmlXPathCastToString(resObj))+1)]; strcpy(sqlBuffer, (char *)xmlXPathCastToString(resObj)); /* Execute Statement */ dm = ExecuteSqlSingleCommand(connection, sqlBuffer, sesPtr, sessionId); if (dm == NULL) { delete sqlBuffer; *externalError = ConnectionError(connection); return 90001; } /* No longer needed */ delete sqlBuffer; /* Create return document */ *docReturn = xmlNewDoc((const xmlChar *)XML_VERSION); (*docReturn)->children = xmlNewDocNode(*docReturn, NULL, (const xmlChar *)ROOT_ELEM, NULL); curReturn = xmlDocGetRootElement(*docReturn); curReturn = xmlNewTextChild(curReturn, NULL, (const xmlChar *)"Object", (const xmlChar *)""); xmlNewProp(curReturn, (const xmlChar *)"DataSource", (const xmlChar *)objDataSource); curReturn = xmlNewTextChild(curReturn, NULL, (const xmlChar *)"Query", (const xmlChar *)""); curReturn = xmlNewTextChild(curReturn, NULL, (const xmlChar *)"Result", (const xmlChar *)""); /* Get Column Headers */ curTmp = xmlNewTextChild(curReturn, NULL, (const xmlChar *)"ColumnHeaderInstances", (const xmlChar *)""); for (columnId = 0; columnId < gda_data_model_get_n_columns(dm); columnId++) { /* Get Field Attributes */ fa = gda_data_model_describe_column(dm, columnId); curTmp = xmlNewTextChild(curTmp, NULL, (const xmlChar *)"ColumnHeaderInstance", (const xmlChar *)""); sprintf(tmpString, "%d", columnId + 1); xmlNewProp(curTmp, (const xmlChar *)"SequenceNo", (const xmlChar *)tmpString); xmlNewProp(curTmp, (const xmlChar *)"Name", (const xmlChar *)gda_data_model_get_column_title(dm, columnId)); xmlNewProp(curTmp, (const xmlChar *)"StorageType", (const xmlChar *)gda_g_type_to_string (gda_column_get_g_type (fa))); // sprintf(tmpString, "%ld", gda_column_get_defined_size(fa)); // sprintf(tmpString, "%ld", fa->priv->defined_size); // xmlNewProp(curTmp, (const xmlChar *)"Size", (const xmlChar *)tmpString); // sprintf(tmpString, "%ld", gda_column_get_scale(fa)); // xmlNewProp(curTmp, (const xmlChar *)"Decimals", (const xmlChar *)tmpString); // xmlNewProp(curTmp, (const xmlChar *)"Decimals", (const xmlChar *)gda_value_stringify (gda_column_get_attribute (fa, GDA_ATTRIBUTE_NUMERIC_SCALE))); curTmp = curTmp->parent; //g_object_unref(fa); } /* Get Data */ curTmp = xmlNewTextChild(curReturn, NULL, (const xmlChar *)"DataInstances", (const xmlChar *)""); for (rowId = 0; rowId < gda_data_model_get_n_rows(dm); rowId++) { curTmp = xmlNewTextChild(curTmp, NULL, (const xmlChar *)"DataInstance", (const xmlChar *)""); sprintf(tmpString, "%d", rowId + 1); xmlNewProp(curTmp, (const xmlChar *)"SequenceNo", (const xmlChar *)tmpString); for (columnId = 0; columnId < gda_data_model_get_n_columns(dm); columnId++) { value = (GValue *) gda_data_model_get_value_at(dm, columnId, rowId, NULL); xmlNewTextChild(curTmp, NULL, (const xmlChar *)gda_data_model_get_column_title(dm, columnId), (const xmlChar *)gda_value_stringify (value)); } curTmp = curTmp->parent; } /* Clean up */ g_object_unref(dm); /* Return */ return 0; }