ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/DBS/Servers/AppServer/src/objectlayer/Util.cpp
Revision: 1.1
Committed: Mon Oct 31 22:10:35 2005 UTC (19 years, 6 months ago) by afaq
Branch: MAIN
Branch point for: v00
Log Message:
Initial revision

File Contents

# User Rev Content
1 afaq 1.1 #include "Util.hpp"
2     #include "RowNSchemaBinding.hpp"
3     #include <iostream>
4     #include <sstream>
5     #include <time.h>
6    
7     using namespace std;
8    
9    
10     Util::Util(){}
11     void Util::setSchema(Dictionary* schema) {
12     this->schema = schema;
13     }
14     string Util::getToken(string data, int index) {
15     int firstIndex = data.find(".",index);
16     //cout<<"index is "<<index<<" firstIndex "<<firstIndex<<endl;
17     int dataLen = data.length();
18     if( (firstIndex < 0) && (index < dataLen) ) {
19     return(data.substr(index, (dataLen - index )));
20     }
21     if(firstIndex < 0) {
22     return("");
23     }
24     return(data.substr(index, (firstIndex - index )));
25     }
26    
27    
28     string Util::getTokenAt(string data, int index) {
29     int location = 0;
30     int dataLen = data.length();
31     string token = "";
32     for(int i = 0; i <= index; ++i) {
33     if(dataLen <= location) {
34     return("");
35     }
36     token = getToken(data,location);
37     location = location + token.length() + 1;
38     }
39     return(token);
40    
41     }
42    
43    
44     string Util::eraseEndChars(string data, int howMany) {
45     int len = data.length() - howMany;
46     if(len < 0) {
47     return data;
48     }
49     data.erase(len,howMany);
50     return(data);
51     }
52    
53     Dictionary_iter Util::getMappedValue(string key,Dictionary_iter b, Dictionary_iter e) {
54     Dictionary_iter mapIterator;
55     for(mapIterator = b; mapIterator != e; mapIterator++) {
56     //if( toUpper(name) == toUpper(key) ) {
57     if( mapIterator->first == key ) {
58     return(mapIterator);
59     }
60     int nameLen = (mapIterator->first).length();
61     int keyLen = key.length();
62     if( (keyLen < nameLen) && ( keyLen == 63 ) ) {
63     //if( toUpper(name.substr(0,keyLen)) == toUpper(key) ) {
64     if( (mapIterator->first).substr(0,keyLen) == key ) {
65     return(mapIterator);
66     }
67     }
68     }
69     return(mapIterator);
70     }
71    
72    
73     //string Util::getDataType(string key, Dictionary_iter bs, Dictionary_iter es) {
74     string Util::getDataType(string key) {
75     Dictionary_iter retIterator = this->getMappedValue(key, schema->begin(), schema->end());
76     return retIterator == schema->end() ? "" : retIterator->second ;
77     }
78    
79    
80    
81     /*Keys_iter Util::getKey(RowInterface* aRow, Keys_iter bk, Keys_iter ek) {
82     for(Keys_iter keyIterator = bk; keyIterator != ek; ++keyIterator) {
83     if(this->isSet(aRow, *keyIterator, this->getDataType(*keyIterator) ) ) {
84     cout<<"returnning "<<*keyIterator<<endl;
85     return(keyIterator);
86     }
87     }
88     cout<<"return end iterator"<<endl;
89     return(ek);
90     }*/
91    
92     Keys Util::getKey(RowInterface* aRow, Keys_iter bk, Keys_iter ek) {
93     Keys toReturn;
94     //cout<<"inside getKey"<<endl;
95     for(Keys_iter i = bk; i != ek; ++i) {
96     //cout<<"getKey i "<<*i<<endl;
97     if(this->isSet(aRow, *i, this->getDataType(*i) ) ) {
98     //cout<<"pushing back "<<*i<<endl;
99     toReturn.push_back(*i);
100     }
101     }
102     return(toReturn);
103     }
104    
105     Keys_iter Util::getNullKey(RowInterface* aRow, Keys_iter bk, Keys_iter ek) {
106     for(Keys_iter i = bk; i != ek; ++i) {
107     if(!this->isSet(aRow, *i, this->getDataType(*i) ) ) {
108     return(i);
109     }
110     }
111     return(ek);
112     }
113    
114     ListOfLists_iter Util::getListOfKey(RowInterface* aRow, ListOfLists_iter b, ListOfLists_iter e) {
115     for(ListOfLists_iter i = b; i != e; ++i) {
116     if( this->isKeySet(aRow, (*i).begin(), (*i).end()) ) {
117     return(i);
118     }
119     }
120     return(e);
121     }
122    
123    
124     bool Util::isKeySet(RowInterface* aRow, Keys_iter bk, Keys_iter ek) {
125     for(Keys_iter i = bk; i != ek; ++i) {
126     //cout<<"checking isKeySet "<<*i<<endl;
127     if(!this->isSet(aRow, *i, this->getDataType(*i) ) ) {
128     //cout<<"retuning..................... false"<<endl;
129     return(false);
130     }
131     }
132     //cout<<"retuning.................. true"<<endl;
133     return(true);
134     }
135    
136     bool Util::isListOfKeySet(RowInterface* aRow, ListOfLists_iter b, ListOfLists_iter e) {
137     for(ListOfLists_iter i = b; i != e; ++i) {
138     if( this->isKeySet(aRow, (*i).begin(), (*i).end()) ) {
139     //cout<<"returnning true from isUniqueKeySet"<<endl;
140     return(true);
141     }
142     }
143     //cout<<"returnning false from isUniqueKeySet"<<endl;
144     return(false);
145     }
146    
147    
148     bool Util::isSet(RowInterface* aRow, string name, string dataType) {
149     //cout<<"inside isSet name "<<name<<" dataType "<<dataType<<endl;
150     if(dataType.length() == 0) {
151     return(false);
152     }
153     void* value;
154     if(dataType == "STRING") {
155     value = (string*)aRow->getValue(name);
156     }
157     if(dataType == "CHARACTER") {
158     value = (char*)aRow->getValue(name);
159     }
160     if(dataType == "INTEGER") {
161     //cout<<"INSIDE Util::isSet for INTEGER"<<endl;
162     value = (int*)aRow->getValue(name);
163     }
164     if(dataType == "FLOAT") {
165     //cout<<"INSIDE Util::isSet for FLOAT"<<endl;
166     value = (float*)aRow->getValue(name);
167     }
168    
169     //cout<<"value is "<<value<<endl;
170     //cout<<"returnning from isSet"<<endl;
171     if(value != NULL) {
172     return(true);
173     } else {
174     return(false);
175     }
176     }
177    
178    
179    
180     string Util::getStrValue(RowInterface* aRow, string name, string dataType) {
181     //cout<<"NAME is "<<name<<" DATATYPE is "<<dataType<<endl;
182     if(dataType == "CHARACTER") {
183     char* value = (char*)aRow->getValue(name);
184     char actualValue = *value;
185     return this->ctoa(actualValue);
186     }
187     if(dataType == "INTEGER") {
188     //cout << "getStrValue:name " << name << endl;
189     int* value = (int*)aRow->getValue(name);
190     int actualValue = *value;
191     return this->itoa(actualValue);
192     }
193     if(dataType == "STRING") {
194     string* value = (string*)aRow->getValue(name);
195     string actualValue = *value;
196     return(actualValue);
197     }
198     if(dataType == "FLOAT") {
199     float* value = (float*)aRow->getValue(name);
200     float actualValue = *value;
201     //cout << "Util::getStrValue float " << actualValue << endl;
202     return this->ftoa(actualValue);
203     }
204    
205     }
206    
207    
208     void Util::setValue(RowInterface* aRow, string name, string dataType, string value) {
209     cout<<"void Util::setValue value is "<<value<<endl;
210     if( dataType == "STRING" ) {
211     //cout << "dies here STRING"<< "dataType:name" << dataType << name << endl;
212     string strValue = (string) value;
213     cout<<" STRING "<<name<<" value is "<<strValue<<endl;
214     aRow->setValue(name,&strValue);
215     }
216     if( dataType == "CHARACTER" ) {
217     char charValue = *(value.c_str());
218     cout<<" CHARACTER "<<name<<" value is "<<charValue<<endl;
219     aRow->setValue(name,&charValue );
220     }
221     if( dataType == "INTEGER" ) {
222     int intValue = atoi(value.c_str());
223     cout<<" INTEGER "<<name<<" value is "<<intValue<<endl;
224     aRow->setValue(name,&intValue );
225     cout << "Just after INTEGERR setValue" << name << intValue << endl;
226     }
227     if( dataType == "FLOAT" ) {
228     float floatValue = this->atof(value);
229     cout<<" FLOAT "<<name<<" value is "<<floatValue<<endl;
230     aRow->setValue(name,&floatValue );
231     }
232    
233     }
234    
235    
236     bool Util::isConsistant(RowInterface* aRowInDB, RowInterface* aRow) {
237     for(Dictionary_iter i = schema->begin(); i != schema->end(); ++i) {
238     if( this->isSet(aRow, i->first, i->second) ) {
239     if( this->isSet(aRowInDB, i->first, i->second) ) {
240     //cout<<"\nComparing "<<i->first<<"\naRow\t"<<this->getStrValue(aRow, i->first, i->second)<<"\naRowInDB\t"<<this->getStrValue(aRowInDB, i->first, i->second)<<endl;
241     if ( this->getStrValue(aRow, i->first, i->second) !=
242     this->getStrValue(aRowInDB, i->first, i->second) ) {
243     return(false);
244     }
245     } else {
246     cout<<"Data is present in DB but coloumn "<<i->first<<" is NULL "<<endl;
247     return(false);
248     }
249     } else if(this->isSet(aRowInDB, i->first, i->second) ) {
250     string value = this->getStrValue(aRowInDB, i->first, i->second);
251     this->setValue(aRow, i->first, i->second, value);
252     }
253     }
254     return(true);
255     }
256     /*
257     bool Util::isInMultiRef(string tabelName, Dictionary_iter b, Dictionary_iter e) {
258     for(Dictionary_iter i = b; i != e; ++i) {
259     if(tabelName == this->getTokenAt(i->second,0) ) {
260     return(true);
261     }
262     }
263     return(false);
264     }
265     */
266    
267    
268     bool Util::toSetCol(string name, Dictionary_iter b, Dictionary_iter e) {
269     bool toSet = true;
270     cout<<"UTIL name is "<<name<<endl;
271     string tableNameOneFromRs = this->getTokenAt(name ,0);
272     int nameLen = name.length();
273     if( tableNameOneFromRs.length() == nameLen) {
274     return(true);
275     }
276     string nameOneFromRs = tableNameOneFromRs + "." + this->getTokenAt(name , 1);
277     cout<<"nameOneFromRs "<<nameOneFromRs<<" name "<<name<<endl;
278     if( nameOneFromRs.length() < nameLen ) {
279     //cout<<"colNameOneIndex "<<colNameOneIndex<<endl;
280     string nameTwoFromRs = this->getTokenAt(name , 2) + "." + this->getTokenAt(name , 3);
281     cout<<"nameTwoFromRs "<<nameTwoFromRs<<endl;
282     for(Dictionary_iter i = b; i != e; ++i) {
283     string nameOneFromSatisfy = i->first;
284     cout<<"nameOneFromSatisfy "<<i->first<<" tableNameOneFromRs "<<tableNameOneFromRs<<endl;
285     if( tableNameOneFromRs == i->first ) {
286     //cout<<"nameTwoFromSatisfy "<<nameTwoFromSatisfy<<"\nnameTwoFromSatisfy.substr(0,nameTwoFromRs.length() "<<i->second.substr(0,nameTwoFromRs.length())<<"\nnameTwoFromRs "<<nameTwoFromRs<<endl;
287     if( nameLen = 63 ) {
288     cout<<"len is 63"<<endl;
289     if( nameTwoFromRs != i->second.substr(0,nameTwoFromRs.length()) ) {
290     return(false);
291     }
292     } else if( i->second != nameTwoFromRs ) {
293     return(false);
294     }
295     }
296     }
297     }
298     cout<<"Before retunning bool Util::toSetCol("<<endl;
299     return(toSet);
300     }
301    
302    
303     void Util::fillPrimaryKeys(string tableName, Keys & toReturn, Dictionary_iter b, Dictionary_iter e) {//MultiRef
304     //cout<<"RowNSchemaBinding init "<<endl;
305     RowNSchemaBinding rowNSchemaBinding;
306     cout<<"tableName "<<tableName<<endl;
307     Keys* pKeys = (rowNSchemaBinding.getSchemaObject(tableName))->getPrimaryKeys();
308     for(Keys_iter i = pKeys->begin(); i != pKeys->end(); ++i) {
309     bool added = false;
310     //cout<<"line 1"<<endl;
311     for(Dictionary_iter j = b; j != e; ++j) {
312     //cout<<"line 2"<<endl;
313     if(j->second == *i) {
314     //cout<<"line 3"<<endl;
315     toReturn.push_back(*i + "." + j->first);
316     //cout<<"line 4"<<endl;
317     added = true;
318     }
319     }
320     if(!added) {
321     //cout<<"line 5"<<endl;
322     toReturn.push_back(*i);
323     }
324     //cout<<"line 6"<<endl;
325     }
326     //cout<<"line 7"<<endl;
327     //copy( pKeys->begin(), pKeys->end(), back_inserter(toReturn) );
328     }
329    
330     //Keys Util::getPrimaryKeys(Keys_iter b, Keys_iter e) {//SchemaOrder
331     Keys Util::getPrimaryKeys(Keys_iter bk, Keys_iter ek, Dictionary_iter bd, Dictionary_iter ed) {//SchemaOrder & MultiRef
332     Keys toReturn;
333     for(Keys_iter i = bk; i != ek; ++i) {
334     //this->fillPrimaryKeys((*i+"row"), toReturn);
335     this->fillPrimaryKeys((*i+"row"), toReturn, bd, ed);
336     }
337     for(Keys_iter i = toReturn.begin(); i != toReturn.end(); ++i) {
338     cout<<"Primary key is "<<*i<<endl;
339     }
340     return toReturn;
341     }
342    
343     /*string Util::makeClause(RowInterface* aRow, Keys_iter b, Keys_iter e) {
344     string clause="";
345     for(Keys_iter i = b; i != e; ++i) {
346     cout<<"key "<<*i<<" keyType "<<this->getDataType(*i)<<endl;
347     clause += *i + "=" +
348     this->getStrValue(aRow, *i , this->getDataType(*i)) + " AND ";
349     }
350     clause = this->eraseEndChars(clause,5);
351     cout<<"clause "<<clause<<endl;
352     return clause;
353     }*/
354     bool Util::isInMultiRef(string key, Dictionary_iter b, Dictionary_iter e) {//Pass Key a.b.c.d or TableName and MultiRef
355     if( b == e ) {
356     return false;
357     }
358     for(Dictionary_iter i = b; i != e; ++i) {
359     if ( (i->first == (string)(this->getTokenAt(key,2) + "." + this->getTokenAt(key,3)) ) &&
360     (i->second == (string)(this->getTokenAt(key,0) + "." + this->getTokenAt(key,1)) ) ) {
361     return true;
362     }
363     if(key == this->getTokenAt(i->second,0) ) {
364     return(true);
365     }
366     }
367     return false;
368     }
369    
370     void Util::equatePKWithRef(RowInterface* aRow, Dictionary_iter b, Dictionary_iter e) {//Refrences
371     cout<<"insdie Util::equatePKWithRef"<<endl;
372     for(Dictionary_iter r = b; r != e; ++r) {
373     //cout<<" r->second "<<r->second<<endl;
374     string dataType = this->getDataType(r->second);//DataType has to be same for both fields in refrences
375     //cout<<"dataType "<<dataType<<endl;
376     string value;
377     string name;
378     bool toSet = false;
379     if(this->isSet(aRow, r->second, dataType) ) {
380     value = this->getStrValue(aRow, r->second, dataType);
381     name = r->first;
382     toSet = true;
383     } else if(this->isSet(aRow, r->first, dataType) ) {
384     value = this->getStrValue(aRow, r->first, dataType);
385     name = r->second;
386     toSet = true;
387     }
388     if(toSet) {
389     this->setValue(aRow, name, dataType, value);
390     }
391     }
392     }
393    
394     void Util::equatePKWithMultiRef(RowInterface* aRow, Dictionary_iter b, Dictionary_iter e) {//Multi Refrences
395     cout<<"insdie Util::equatePKWithMultiRef"<<endl;
396     for(Dictionary_iter m = b; m != e; ++m) {
397     string dataType = this->getDataType(m->first);
398     string value;
399     bool toSet = false;
400     if(this->isSet(aRow, m->first, dataType) ) {
401     value = this->getStrValue(aRow, m->first, dataType);
402     toSet = true;
403     }
404     if(toSet) {
405     this->setValue(aRow, m->second + "." + m->first , dataType, value);
406     }
407     }
408     }
409    
410    
411     int Util::atoi(string in) {
412     std::istringstream istr(in);
413     int i;
414     istr >> i;
415     return i;
416     }
417    
418    
419     string Util::itoa(int i){
420     char temp[20];
421     sprintf(temp,"%d",i);
422     return((string)temp);
423     }
424    
425     string Util::ctoa(char c){
426     string s(1, c);
427     return s;
428     }
429    
430    
431     std::string Util::ftoa(float f) {
432    
433     char temp[40];
434     sprintf(temp,"%f",f);
435     return((string)temp);
436    
437     }
438    
439    
440    
441     float Util::atof(std::string fin) {
442     std::istringstream istr(fin);
443     float f;
444     istr >> f;
445     return f;
446     //cout <<" NO IMPLEMENTATION Yet Util::atof";
447     }
448    
449    
450    
451     void Util::tokenize(const string& str, vector<string>& tokens, const string& delimiters) {
452     // Skip delimiters at beginning.
453     string::size_type lastPos = str.find_first_not_of(delimiters, 0);
454     // Find first "non-delimiter".
455     string::size_type pos = str.find_first_of(delimiters, lastPos);
456     while (string::npos != pos || string::npos != lastPos) {
457     // Found a token, add it to the vector.
458     tokens.push_back(str.substr(lastPos, pos - lastPos));
459     // Skip delimiters. Note the "not_of"
460     lastPos = str.find_first_not_of(delimiters, pos);
461     // Find next "non-delimiter"
462     pos = str.find_first_of(delimiters, lastPos);
463     }
464     }
465    
466     long Util::getTime() {
467     time_t seconds = time (NULL);
468     return((long) seconds);
469     }
470