header

S_SQLRowset.cpp

Go to the documentation of this file.
00001 /*
00002   Copyright (c) Members of the EGEE Collaboration. 2004. 
00003   See http://www.eu-egee.org/partners/ for details on the copyright
00004   holders.  
00005 
00006   Licensed under the Apache License, Version 2.0 (the "License"); 
00007   you may not use this file except in compliance with the License. 
00008   You may obtain a copy of the License at 
00009 
00010     http://www.apache.org/licenses/LICENSE-2.0 
00011 
00012   Unless required by applicable law or agreed to in writing, software 
00013   distributed under the License is distributed on an "AS IS" BASIS, 
00014   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
00015   See the License for the specific language governing permissions and 
00016   limitations under the License.
00017 */
00018 
00019 
00020 #include "S_SQLRowset.h"
00021 #include "util/Statement.h"
00022 
00023 static bool debug = false;
00024 #include "debug.h"
00025 
00026 using namespace std;
00027 
00028 _wrs__metadata* S_SQLRowset::createMetadata(int colNum, std::string &table)
00029 {
00030   _wrs__metadata* metadata = soap_new__wrs__metadata(soap,-1);
00031   metadata->column_count = StringOf(colNum) ;
00032   metadata->__sizecolumn_definition = colNum ;
00033   metadata->column_definition
00034     = soap_new__wrs__metadata_column_definition(soap ,colNum);
00035 
00036   DBResult dbResult;
00037   vector<string> results;
00038   handleRequest(table, 1, colNum, dbResult, results);
00039 
00040   vector<string>::const_iterator it = results.begin();
00041   for(int i=0;i<colNum;i++) {
00042     metadata->column_definition[i].column_index = StringOf(i+1);
00043     metadata->column_definition[i].auto_increment = "false";
00044     metadata->column_definition[i].case_sensitive = "true";
00045     metadata->column_definition[i].currency = "false";
00046     metadata->column_definition[i].nullable = "1";
00047     metadata->column_definition[i].signed_ = "true";
00048     metadata->column_definition[i].searchable = "true";
00049 
00050     std::string tmp = *it;
00051     size_t pos = tmp.find('.');
00052     if ( pos == string::npos ) {
00053       metadata->column_definition[i].column_label = tmp ;
00054       metadata->column_definition[i].column_name = tmp ;
00055     } else {
00056       metadata->column_definition[i].column_label
00057         = metadata->column_definition[i].column_name
00058         = tmp.substr(pos+1);
00059       metadata->column_definition[i].table_name
00060         = tmp.substr(0,pos);
00061     }
00062     it++;
00063   }
00064   return metadata;
00065 }
00066 
00067 void S_SQLRowset::makePropertyDocument(wsdai__PropertyDocumentType* doc)
00068 {
00069   /* wsdai__PropertyDocumentType */
00070   std::string endpoint;
00071   std::string address;
00072   transEndPoint(soap, address, endpoint, "SQLResponse", dran);
00073   doc->wsdai__DataResourceAbstractName = endpoint;
00074   doc->wsdai__DataResourceManagement
00075      = _wsdai__DataResourceManagement__ExternallyManaged;
00076 
00077   wsdai__DatasetMapType *dmt = soap_new_wsdai__DatasetMapType(soap,-1);
00078   dmt->wsdai__MessageQName = "wsdair:GetTuples" ;
00079   dmt->wsdai__DatasetFormatURI = WRS_FORMAT_URI ;
00080   doc->wsdai__DatasetMap.push_back(dmt);
00081 
00082   _wsdai__DataResourceDescription *drd
00083      = soap_new__wsdai__DataResourceDescription(soap,-1);
00084   doc->wsdai__DataResourceDescription = drd;
00085   drd->__mixed = soap_strdup(soap, "AMGA");
00086 
00087   doc->wsdai__Readable = true;
00088   doc->wsdai__Writeable = true;
00089   doc->wsdai__ConcurrentAccess = true;
00090   doc->wsdai__TransactionInitiation = _wsdai__TransactionInitiation__Automatic;
00091   doc->wsdai__TransactionIsolation = _wsdai__TransactionIsolation__ReadCommitted;
00092   doc->wsdai__ChildSensitiveToParent = _wsdai__ChildSensitiveToParent__Sensitive;
00093   doc->wsdai__ParentSensitiveToChild = _wsdai__ParentSensitiveToChild__Sensitive;
00094 
00095 
00096   /* Language Map */
00097   wsdai__LanguageMapType *lmt = soap_new_wsdai__LanguageMapType(soap,-1);
00098   lmt->wsdai__MessageQName = "wsdair:SQLExecute";
00099   lmt->wsdai__LanguageURI = "http://www.sql.org/sql-92";
00100   doc->wsdai__LanguageMap.push_back(lmt);
00101   lmt = soap_new_wsdai__LanguageMapType(soap,-1);
00102   lmt->wsdai__MessageQName = "wsdai:GenericQuery";
00103   lmt->wsdai__LanguageURI = "http://cern.ch/amga";
00104   doc->wsdai__LanguageMap.push_back(lmt);
00105 
00106 }
00107 
00108 int S_SQLRowset::CountRows(std::string &table, int &rows) {
00109   DBResult dbResult;
00110   std::vector<string> results;
00111   int count;
00112 
00113   int ret = checkDBConnection(soap, helper);
00114   if (ret != SOAP_OK ) return ret;
00115 
00116   Statement statement(*(helper->dbConn->dbServer->dbConn), debug);
00117 
00118   std::string query = "SELECT COUNT(*) FROM " ;
00119   query.append(table) ;
00120 
00121   if ( statement.exec(query) ) 
00122     return servBusy(soap, "DB error");
00123   
00124   statement.bind(1, &count);
00125 
00126   if(statement.fetch()){
00127     count = 0;
00128   } else {
00129     rows = count;
00130   }
00131 
00132   statement.unBind();
00133   statement.close();
00134 
00135   return SOAP_OK;
00136 }
00137 
00138 int S_SQLRowset::makePropertyDocument(int num_column
00139                                     , wsdair__SQLRowsetPropertyDocumentType* doc
00140                                     , std::string &table)
00141 {
00142   makePropertyDocument(doc);
00143 
00144   /* wsdair__SQLRowsetPropertyDocumentType */
00145   doc->wsdair__RowSchema = soap_new_wsdair__RowSchemaType(soap,-1);
00146   doc->wsdair__RowSchema->wrs__metadata = createMetadata(num_column, table);
00147   doc->wsdair__AccessMode = _wsdair__AccessMode__Random;
00148   return CountRows(table, doc->wsdair__NoOfRows) ;
00149 }
00150 

Generated on Mon Apr 16 13:59:18 2012 for AMGA by  doxygen 1.4.7