TeTable.cpp

Go to the documentation of this file.
00001 /************************************************************************************
00002 TerraLib - a library for developing GIS applications.
00003 Copyright Đ 2001-2007 INPE and Tecgraf/PUC-Rio.
00004 
00005 This code is part of the TerraLib library.
00006 This library is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU Lesser General Public
00008 License as published by the Free Software Foundation; either
00009 version 2.1 of the License, or (at your option) any later version.
00010 
00011 You should have received a copy of the GNU Lesser General Public
00012 License along with this library.
00013 
00014 The authors reassure the license terms regarding the warranties.
00015 They specifically disclaim any warranties, including, but not limited to,
00016 the implied warranties of merchantability and fitness for a particular purpose.
00017 The library provided hereunder is on an "as is" basis, and the authors have no
00018 obligation to provide maintenance, support, updates, enhancements, or modifications.
00019 In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
00020 indirect, special, incidental, or consequential damages arising out of the use
00021 of this library and its documentation.
00022 *************************************************************************************/
00023 
00024 #include "TeTable.h"
00025 #include "TeUtils.h"
00026 #include <iostream>
00027 
00028 using namespace std;
00029 
00030 
00031 string tableJoin(TeAttrTableVector& vecTable, string geomTable, string attrLink)
00032 {
00033         string parClause = "";
00034         string fromResult = "";
00035         string firstTable = "";
00036         string cJoin = "";
00037 
00038         TeAttrTableVector::iterator it; 
00039         
00040         if(geomTable.empty())
00041         {
00042                 //find the first static table
00043                 it = vecTable.begin();
00044                 while(it!=vecTable.end())
00045                 {
00046                         if((*it).tableType() != TeAttrExternal)
00047                         {
00048                                 firstTable = (*it).name();
00049                                 attrLink = (*it).linkName();
00050                                 break;
00051                         }
00052                         ++it;
00053                 }
00054                 cJoin = " LEFT "; //the first table is a attribute table
00055         }
00056         else
00057         {
00058                 firstTable = geomTable;
00059                 cJoin = " LEFT "; //the first table is a geometry table
00060         }
00061                 
00062         
00063         fromResult += firstTable; 
00064         
00065         
00066         //if donīt exist a table static or temporal 
00067         if(firstTable.empty() || attrLink.empty())      
00068                 return "";
00069                         
00070         it = vecTable.begin();
00071 
00072         while(it!=vecTable.end())
00073         {
00074                 if( ((*it).name()!=firstTable) && ((*it).tableType()!=TeAttrExternal))
00075                 {
00076                         fromResult += " "+ cJoin +" JOIN "+ (*it).name() +" ON "+ firstTable;
00077                         fromResult += "."+ attrLink +" = "+ (*it).name() +"."+ (*it).linkName() +")";
00078                         parClause += "(";
00079                 }
00080 
00081                 else if ((*it).name()!=firstTable)
00082                 {
00083                         int idTableStatic = (*it).relatedTableId();
00084                         string colTableStatic = (*it).relatedAttribute();
00085                         string nameTableStatic;   
00086 
00087                         //verify if static table is in the vector of the attribute tables
00088                         for(unsigned int j=0; j<vecTable.size(); j++)
00089                         {
00090                                 if(vecTable[j].id()==idTableStatic)
00091                                 {
00092                                         nameTableStatic = vecTable[j].name();
00093                                         break;
00094                                 }
00095                         }
00096 
00097                         if(nameTableStatic.empty())
00098                                 return "";
00099 
00100                         fromResult += " "+ cJoin +" JOIN "+ (*it).name() +" ON "+ nameTableStatic +"."+ colTableStatic;
00101                         fromResult += " = " + (*it).name() +"."+ (*it).linkName() +")";
00102                         parClause += "(";
00103                 }
00104 
00105                 ++it;
00106                 cJoin = " LEFT ";
00107         }
00108 
00109         return (parClause + fromResult);
00110 }
00111 
00112 
00113 // ---- Handle class - TeTable
00114 
00115 TeTable::TeTable():
00116         id_(-1),
00117         name_(""),
00118         type_(TeAttrStatic),
00119         order_(-1),
00120         attLink_(""),
00121         attUnique_(""),
00122         separator_(','),
00123         attInitialTime_(""),
00124         attFinalTime_(""),
00125         attTimeUnit_(TeSECOND),
00126         relatedTableId_(-1),
00127         relatedAttribute_("")
00128 {
00129         pImpl_ = new TeTableImpl;
00130         pImpl_->refCount_ = 1;  
00131 }
00132 
00133 TeTable::TeTable(const string& name):
00134         id_(-1),
00135         name_(name),
00136         type_(TeAttrStatic),
00137         order_(-1),
00138         attLink_(""),
00139         attUnique_(""),
00140         separator_(','),
00141         attInitialTime_(""),
00142         attFinalTime_(""),
00143         attTimeUnit_(TeSECOND),
00144         relatedTableId_(-1),
00145         relatedAttribute_("")
00146 {
00147         pImpl_ = new TeTableImpl;
00148         pImpl_->refCount_ = 1;  
00149 }
00150 
00151 TeTable::TeTable(const string& name, const TeAttributeList& attList, const string& uniqueName,
00152                                  const string& linkName, TeAttrTableType tableType):
00153         id_(-1),
00154         name_(name),
00155         type_(tableType),
00156         order_(-1),
00157         attList_(attList),
00158         attLink_(linkName),
00159         attUnique_(uniqueName),
00160         separator_(','),
00161         attInitialTime_(""),
00162         attFinalTime_(""),
00163         attTimeUnit_(TeSECOND),
00164         relatedTableId_(-1),
00165         relatedAttribute_("")
00166 {
00167         pImpl_ = new TeTableImpl;
00168         pImpl_->refCount_ = 1;  
00169 }
00170 
00171 TeTable::~TeTable()
00172 {
00173         if ( --(pImpl_->refCount_) <= 0 )
00174                 delete pImpl_;  
00175         attList_.clear();
00176 }
00177 
00178 // Copy constructor
00179 // copies the representation pointer
00180 // increments the reference counter
00181 
00182 TeTable::TeTable(const TeTable& other)
00183 {
00184         id_= other.id_;
00185         name_ = other.name_;
00186         type_ = other.type_;
00187         attLink_ = other.attLink_;
00188         attUnique_ = other.attUnique_;
00189         separator_ = other.separator_;
00190         order_ = other.order_;
00191         attInitialTime_ = other.attInitialTime_;
00192         attFinalTime_ = other.attFinalTime_;
00193         attTimeUnit_ = other.attTimeUnit_;
00194         attList_.clear();
00195         attList_.resize(other.attList_.size());
00196         attList_=other.attList_;
00197         relatedTableId_ = other.relatedTableId_;
00198         relatedTableName_ = other.relatedTableName_;
00199         relatedAttribute_ = other.relatedAttribute_;
00200         pImpl_ = other.pImpl_;
00201         pImpl_->refCount_++;
00202 }
00203 
00204 // Operator =
00205 // Copies the representation pointer
00206 // Decrements the reference counter of the current object
00207 // Increments the reference counter for the new object
00208 TeTable& 
00209 TeTable::operator=(const TeTable& rhs)
00210 {
00211         if ( this != &rhs )
00212         {
00213                 rhs.pImpl_->refCount_++;
00214         
00215                 if ( --(pImpl_->refCount_) <= 0 )
00216                         delete pImpl_;
00217                 pImpl_ = rhs.pImpl_;
00218 
00219                 separator_ = rhs.separator_;
00220                 attLink_ = rhs.attLink_;
00221                 attUnique_ = rhs.attUnique_;
00222                 order_ = rhs.order_;
00223                 attInitialTime_ = rhs.attInitialTime_;
00224                 attFinalTime_ = rhs.attFinalTime_;
00225                 attTimeUnit_ = rhs.attTimeUnit_;
00226                 type_ = rhs.type_;
00227                 name_ = rhs.name_;      
00228                 attList_.clear();
00229                 attList_ = rhs.attList_;
00230                 relatedTableId_ = rhs.relatedTableId_;
00231                 relatedTableName_ = rhs.relatedTableName_;
00232                 relatedAttribute_ = rhs.relatedAttribute_;
00233                 id_ = rhs.id_;
00234         }
00235         return *this;
00236 }
00237 
00238 bool 
00239 TeTable::setTableType( TeAttrTableType attType, int relatedTableId, const string& relatedAttribute)
00240 { 
00241         type_ = attType; 
00242         if ( attType == TeAttrExternal && relatedTableId > 0 && !relatedAttribute.empty())
00243         {
00244                 relatedTableId_ = relatedTableId;
00245                 relatedAttribute_ = relatedAttribute;
00246 
00247                 return true;
00248         }
00249         return false;
00250 }
00251 
00252 void
00253 TeTable::add ( const TeTableRow& row )
00254 {
00255         pImpl_->add ( row );
00256 }
00257 
00258 void
00259 TeTable::setValue (int row, int col, string& val)
00260 {
00261         pImpl_->setValue (row,col,val);
00262 }
00263 
00264 unsigned int
00265 TeTable::size () const
00266 {
00267         return pImpl_->size();
00268 }
00269 
00270 void TeTable::clear ()
00271 {
00272         pImpl_->clear();
00273 }
00274 
00275 TeTableRow
00276 TeTable::operator [] (int i) const 
00277 {
00278         return pImpl_->operator [] ( i );
00279 }
00280 
00281 string 
00282 TeTable::operator () ( int row, int col )
00283 {
00284         return pImpl_->operator () (row, col );
00285 }
00286 
00287 
00288 bool
00289 TeTable::attrLink(TeAttribute& att)
00290 {       
00291         TeAttributeList::iterator it = attList_.begin();
00292         while (it != attList_.end())
00293         {
00294                 if (TeConvertToUpperCase((*it).rep_.name_) == TeConvertToUpperCase(attLink_))
00295                 {
00296                         att = (*it);
00297                         return true;
00298                 }
00299                 ++it;
00300         }
00301         return false;
00302 }
00303 
00304 bool 
00305 TeTable::attrUnique(TeAttribute& attr)
00306 {
00307         TeAttributeList::iterator it = attList_.begin();
00308         while(it!=attList_.end())
00309         {
00310                 if(TeConvertToUpperCase((*it).rep_.name_) == TeConvertToUpperCase(attUnique_))
00311                 {
00312                         attr = (*it);
00313                         return true;
00314                 }
00315                 ++it;
00316         }
00317         return false;
00318 }
00319 
00320 int
00321 TeTable::attrLinkPosition()
00322 {
00323         int i = 0;
00324         TeAttributeList::iterator it = attList_.begin();
00325         while (it != attList_.end())
00326         {
00327                 if (TeConvertToUpperCase((*it).rep_.name_) == TeConvertToUpperCase(attLink_))
00328                         return i;
00329                 ++it;
00330                 ++i;
00331         }
00332         return -1;
00333 }
00334 
00335 int
00336 TeTable::attrUniquePosition()
00337 {
00338         int i = 0;
00339         TeAttributeList::iterator it = attList_.begin();
00340         while (it != attList_.end())
00341         {
00342                 if (TeConvertToUpperCase((*it).rep_.name_) == TeConvertToUpperCase(attUnique_))
00343                         return i;
00344                 ++it;
00345                 ++i;
00346         }
00347         return -1;
00348 }
00349 
00350 bool 
00351 TeTable::attributeNames(vector<string>& attrs)
00352 {
00353         attrs.clear();
00354         TeAttributeList::iterator it = attList_.begin();
00355         while(it!=attList_.end())
00356         {
00357                 attrs.push_back((*it).rep_.name_);
00358                 ++it;
00359         }
00360         return true;
00361 }
00362 
00363 
00364 void 
00365 TeTable::primaryKeys(vector<string>& keys)
00366 {
00367         keys.empty();
00368         TeAttributeList::iterator it = attList_.begin();
00369         while(it!=attList_.end())
00370         {
00371                 if((*it).rep_.isPrimaryKey_)
00372                         keys.push_back((*it).rep_.name_);
00373                 ++it;
00374         }
00375         return;
00376 }

Generated on Sun Jul 29 04:01:28 2012 for TerraLib - Development Source by  doxygen 1.5.3