00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "TeOCIConnect.h"
00025 #include "TeOCICursor.h"
00026 #include "TeOCIOracle.h"
00027 #include "TeProject.h"
00028
00029 #include <sys/stat.h>
00030
00031 bool
00032 TeOCIOracle::createSequence(const string &tableName)
00033 {
00034 string nameSeq = getNameSequence(tableName);
00035
00036 string seq = " CREATE SEQUENCE " + nameSeq;
00037 seq += " START WITH 1 INCREMENT BY 1 ORDER ";
00038 if (!execute(seq))
00039 {
00040 errorMessage_ = "Error creating sequence to table " + tableName + " !";
00041 return false;
00042 }
00043
00044 return true;
00045 }
00046
00047 bool
00048 TeOCIOracle::createAutoIncrementTrigger(const string &tableName, const string &fieldName)
00049 {
00050 string nameTri = getNameTrigger(tableName);
00051 string nameSeq = getNameSequence(tableName);
00052
00053 string tri;
00054 tri = "CREATE TRIGGER " + nameTri;
00055 tri += " BEFORE INSERT ON "+tableName;
00056 tri += " for each row";
00057 tri += " begin";
00058 tri += " select "+nameSeq+".NEXTVAL";
00059 tri += " into :new."+fieldName;
00060 tri += " from dual;";
00061 tri += " end;";
00062
00063 if(!execute(tri))
00064 {
00065 errorMessage_ = "Error creating trigger to table " + tableName + " !";
00066 return false;
00067 }
00068 return true;
00069 }
00070
00071 string
00072 TeOCIOracle::getNameSequence(const string &tableName)
00073 {
00074 string name;
00075 if(tableName.size()>21)
00076 name = tableName.substr(0,20) + "_seq";
00077 else
00078 name = tableName + "_seq";
00079
00080 return name;
00081 }
00082
00083 string
00084 TeOCIOracle::getNameTrigger(const string &tableName)
00085 {
00086 string name;
00087 if(tableName.size()>21)
00088 name = tableName.substr(0,20) + "_tri";
00089 else
00090 name = tableName + "_tri";
00091
00092 return name;
00093 }
00094
00095 string
00096 TeOCIOracle::escapeSequence (const string& from)
00097 {
00098 int fa = 0;
00099 string to = from;
00100 to.insert(0, " ");
00101 string::iterator it = to.begin();
00102 while(it != to.end())
00103 {
00104 int f = to.find("'", fa);
00105 if(f > fa)
00106 {
00107 to.insert(f, "'");
00108 fa = f + 2;
00109 }
00110 else
00111 break;
00112 }
00113 to = to.substr(1, to.size() - 1);
00114 return to;
00115 }
00116
00117 TeOCIOracle::TeOCIOracle()
00118 {
00119 connection_ = new TeOCIConnection();
00120 dbmsName_ = "OracleOCI";
00121 sequenceCont_ = -1;
00122 sequenceName_ = "";
00123 }
00124
00125 TeOCIOracle::~TeOCIOracle()
00126 {
00127 if (connection_)
00128 delete (connection_);
00129 connection_ = NULL;
00130 }
00131
00132 bool TeOCIOracle::beginTransaction()
00133 {
00134 if(!connection_)
00135 return false;
00136 return (connection_->transStart());
00137 }
00138
00139 bool TeOCIOracle::commitTransaction()
00140 {
00141 if(!connection_)
00142 return false;
00143 return (connection_->transCommit());
00144 }
00145
00146 bool TeOCIOracle::rollbackTransaction()
00147 {
00148 if(!connection_)
00149 return false;
00150 return (connection_->transRollback());
00151 }
00152
00153 bool
00154 TeOCIOracle::newDatabase(const string& database, const string& user, const string& password, const string& host, const int& port, bool terralibModel, const std::string& characterSet)
00155 {
00156 if (!connect(host,user,password,database,port))
00157 return false;
00158 if (terralibModel)
00159 {
00160
00161 if(!this->createConceptualModel())
00162 return false;
00163 }
00164 return true;
00165 }
00166
00167 bool
00168 TeOCIOracle::connect(const string& host, const string& user, const string& password, const string& database, int port)
00169 {
00170 if(connection_ == NULL || connection_->isConnected())
00171 {
00172 delete (connection_);
00173 connection_ = new TeOCIConnection();
00174 }
00175
00176 isConnected_ = false;
00177 if (connection_->connect(host.c_str(),user.c_str(),password.c_str()))
00178 {
00179 isConnected_ = true;
00180 host_ = host;
00181 user_ = user;
00182 password_ = password;
00183 database_ = database;
00184 portNumber_ = port;
00185 return true;
00186 }
00187 else
00188 {
00189 isConnected_ = false;
00190 errorMessage_ = "Error connecting to database server!";
00191 delete (connection_);
00192 connection_ = NULL;
00193 return false;
00194 }
00195 }
00196
00197 void
00198 TeOCIOracle::close()
00199 {
00200 clear();
00201 connection_->disconnect();
00202 isConnected_ = false;
00203 }
00204
00205 bool
00206 TeOCIOracle::listTables(vector<string>& tableList)
00207 {
00208 tableList.clear();
00209 TeOCIOraclePortal *ocip = (TeOCIOraclePortal*)getPortal();
00210 if(!ocip)
00211 return false;
00212
00213 string exist = " SELECT table_name FROM all_tables WHERE ";
00214 exist += " OWNER = '" + TeConvertToUpperCase(user_) + "'";
00215
00216 if(!ocip->query(exist))
00217 {
00218 delete ocip;
00219 return false;
00220 }
00221
00222 while(ocip->fetchRow())
00223 tableList.push_back (ocip->getData(0));
00224
00225 delete ocip;
00226 return true;
00227 }
00228
00229
00230 bool
00231 TeOCIOracle::tableExist(const string& table)
00232 {
00233 bool status;
00234 TeOCIOraclePortal *ocip = (TeOCIOraclePortal*)getPortal();
00235 if(!ocip)
00236 return false;
00237
00238 string exist = " SELECT table_name FROM all_tables WHERE";
00239 exist += " TABLE_NAME = '" + TeConvertToUpperCase(table) + "'";
00240 exist += " AND OWNER = '" + TeConvertToUpperCase(user_) + "'";
00241
00242 if(!ocip->query(exist))
00243 {
00244 delete ocip;
00245 return false;
00246 }
00247
00248 if(ocip->fetchRow())
00249 status = true;
00250 else
00251 status = false;
00252
00253 delete ocip;
00254 return (status);
00255 }
00256
00257 bool
00258 TeOCIOracle::viewExist(const string& view)
00259 {
00260 bool status;
00261 TeOCIOraclePortal *ocip = (TeOCIOraclePortal*)getPortal();
00262 if(!ocip)
00263 return false;
00264
00265 string exist = " SELECT view_name FROM user_views WHERE";
00266 exist += " VIEW_NAME = '" + TeConvertToUpperCase(view) + "'";
00267
00268 if(!ocip->query(exist))
00269 {
00270 delete ocip;
00271 return false;
00272 }
00273
00274 if(ocip->fetchRow())
00275 status = true;
00276 else
00277 status = false;
00278
00279 delete ocip;
00280 return (status);
00281 }
00282
00283 bool
00284 TeOCIOracle::columnExist(const string& table, const string& column, TeAttribute& attr)
00285 {
00286 bool status = false;
00287 TeOCIOraclePortal *ocip = (TeOCIOraclePortal*)getPortal();
00288 if(!ocip)
00289 return false;
00290
00291 string exist = " SELECT DATA_TYPE, DATA_LENGTH, DATA_SCALE FROM ALL_TAB_COLUMNS WHERE";
00292 exist += " TABLE_NAME = '" + TeConvertToUpperCase(table) + "'";
00293 exist += " AND COLUMN_NAME = '" + TeConvertToUpperCase(column) + "'";
00294 exist += " AND OWNER = '" + TeConvertToUpperCase(user_) + "'";
00295
00296 if(!ocip->query(exist))
00297 {
00298 delete ocip;
00299 return false;
00300 }
00301
00302 if(ocip->fetchRow())
00303 {
00304 attr.rep_.name_ = column;
00305
00306 string dataType = string(ocip->getData(0));
00307 int dataLength = atoi(ocip->getData(1));
00308 int dataScale = atoi(ocip->getData(2));
00309 bool number = false;
00310
00311 if(dataType=="VARCHAR2")
00312 {
00313 attr.rep_.type_ = TeSTRING;
00314 attr.rep_.numChar_ = dataLength;
00315 }
00316 else if (dataType=="BLOB")
00317 {
00318 attr.rep_.type_ = TeBLOB;
00319 attr.rep_.numChar_ = dataLength;
00320 }
00321 else if (dataType=="NUMBER")
00322 {
00323 number = true;
00324 }
00325 else if (dataType=="SDO_GEOMETRY")
00326 {
00327 attr.rep_.type_ = TeOBJECT;
00328 attr.rep_.numChar_ = dataLength;
00329 }
00330 else if (dataType== "CHAR")
00331 {
00332 attr.rep_.type_ = TeCHARACTER;
00333 attr.rep_.numChar_ = dataLength;
00334 }
00335 else if (dataType=="DATE")
00336 {
00337 attr.rep_.type_ = TeDATETIME;
00338 }
00339 else
00340 {
00341 attr.rep_.type_ = TeSTRING;
00342 attr.rep_.numChar_ = dataLength;
00343 }
00344
00345 if(number)
00346 {
00347 if(dataScale > 0)
00348 attr.rep_.type_ = TeREAL;
00349 else
00350 attr.rep_.type_ = TeINT;
00351 }
00352 status = true;
00353 }
00354
00355 delete ocip;
00356 return (status);
00357 }
00358
00359 bool
00360 TeOCIOracle::createTable(const string& table, TeAttributeList &attr)
00361 {
00362 short cont=0;
00363 string pkeys ="";
00364 bool hasAutoNumber=false;
00365 string fieldName="";
00366
00367 TeAttributeList::iterator it = attr.begin();
00368 string tablec;
00369 tablec = "CREATE TABLE " + table +" (";
00370
00371 while ( it != attr.end())
00372 {
00373 if (cont)
00374 tablec += ", ";
00375
00376 switch ((*it).rep_.type_)
00377 {
00378 case TeSTRING:
00379 if((*it).rep_.numChar_ > 0)
00380 {
00381 tablec += (*it).rep_.name_ + " VARCHAR2(" + Te2String((*it).rep_.numChar_) + ")";
00382 }
00383 else
00384 {
00385 tablec += (*it).rep_.name_ + " VARCHAR2(4000)";
00386 }
00387 break;
00388
00389 case TeREAL:
00390 if((*it).rep_.decimals_>0)
00391 tablec += (*it).rep_.name_ +" NUMBER(*,"+ Te2String((*it).rep_.decimals_) +") ";
00392 else
00393 tablec += (*it).rep_.name_ +" NUMBER(*,15) ";
00394 break;
00395
00396 case TeINT:
00397 case TeUNSIGNEDINT:
00398 tablec += (*it).rep_.name_ + " NUMBER(32) ";
00399 break;
00400
00401 case TeDATETIME:
00402 tablec += (*it).rep_.name_ + " DATE ";
00403 break;
00404
00405 case TeCHARACTER:
00406 tablec += (*it).rep_.name_ + " CHAR ";
00407 break;
00408
00409 case TeBOOLEAN:
00410 tablec += (*it).rep_.name_ + " NUMBER(1) ";
00411 break;
00412
00413 case TeBLOB:
00414 tablec += (*it).rep_.name_ + " BLOB ";
00415 break;
00416
00417 case TePOINTTYPE:
00418 case TePOINTSETTYPE:
00419 case TeNODETYPE:
00420 case TeNODESETTYPE:
00421 tablec += " x NUMBER(*,15), ";
00422 tablec += " y NUMBER(*,15) ";
00423 ++it;
00424 cont++;
00425 continue;
00426
00427 case TeLINE2DTYPE:
00428 case TeLINESETTYPE:
00429 tablec += " num_coords NUMBER(32) NOT NULL, ";
00430 tablec += " lower_x NUMBER(*,15) NOT NULL, ";
00431 tablec += " lower_y NUMBER(*,15) NOT NULL, ";
00432 tablec += " upper_x NUMBER(*,15) NOT NULL, ";
00433 tablec += " upper_y NUMBER(*,15) NOT NULL, ";
00434 tablec += " ext_max NUMBER(*,15) NOT NULL, ";
00435 tablec += " spatial_data BLOB ";
00436 ++it;
00437 cont++;
00438 continue;
00439
00440 case TePOLYGONTYPE:
00441 case TePOLYGONSETTYPE:
00442 tablec += " num_coords NUMBER(32) NOT NULL,";
00443 tablec += " num_holes NUMBER(32) NOT NULL,";
00444 tablec += " parent_id NUMBER(32) NOT NULL,";
00445 tablec += " lower_x NUMBER(*,15) NOT NULL,";
00446 tablec += " lower_y NUMBER(*,15) NOT NULL,";
00447 tablec += " upper_x NUMBER(*,15) NOT NULL,";
00448 tablec += " upper_y NUMBER(*,15) NOT NULL,";
00449 tablec += " ext_max NUMBER(*,15) NOT NULL,";
00450 tablec += " spatial_data BLOB ";
00451 ++it;
00452 cont++;
00453 continue;
00454
00455 case TeCELLTYPE:
00456 case TeCELLSETTYPE:
00457 tablec += " col_number NUMBER(32) NOT NULL,";
00458 tablec += " row_number NUMBER(32) NOT NULL,";
00459 tablec += " lower_x NUMBER(*,15),";
00460 tablec += " lower_y NUMBER(*,15),";
00461 tablec += " upper_x NUMBER(*,15),";
00462 tablec += " upper_y NUMBER(*,15) ";
00463 ++it;
00464 cont++;
00465 continue;
00466
00467 case TeRASTERTYPE:
00468 tablec += " band_id NUMBER(32) NOT NULL, ";
00469 tablec += " resolution_factor NUMBER(32), ";
00470 tablec += " subband NUMBER(32),";
00471 tablec += " lower_x NUMBER(*,15) DEFAULT 0.0, ";
00472 tablec += " lower_y NUMBER(*,15) DEFAULT 0.0, ";
00473 tablec += " upper_x NUMBER(*,15) DEFAULT 0.0, ";
00474 tablec += " upper_y NUMBER(*,15) DEFAULT 0.0, ";
00475 tablec += " block_size NUMBER(32), ";
00476 tablec += " spatial_data BLOB ";
00477 ++it;
00478 cont++;
00479 continue;
00480
00481 case TeTEXTTYPE:
00482 case TeTEXTSETTYPE:
00483 default:
00484 tablec += (*it).rep_.name_ + " VARCHAR2(255) ";
00485 break;
00486 }
00487
00488
00489 if(!((*it).rep_.defaultValue_.empty()))
00490 tablec += " DEFAULT '" + (*it).rep_.defaultValue_ + "' ";
00491
00492
00493 if(!((*it).rep_.null_))
00494 tablec += " NOT NULL ";
00495
00496
00497 if((*it).rep_.isAutoNumber_ && ((*it).rep_.type_==TeINT || (*it).rep_.type_==TeUNSIGNEDINT))
00498 {
00499 hasAutoNumber=true;
00500 fieldName=(*it).rep_.name_;
00501 }
00502
00503
00504 if ((*it).rep_.isPrimaryKey_ && (*it).rep_.type_ != TeBLOB )
00505 {
00506 if (!pkeys.empty())
00507 pkeys += ", ";
00508 pkeys += (*it).rep_.name_;
00509 }
00510
00511 ++it;
00512 cont++;
00513 }
00514
00515 if(!pkeys.empty())
00516 tablec += ", PRIMARY KEY (" + pkeys + ") ";
00517
00518 tablec += ")";
00519
00520 if(!execute(tablec))
00521 {
00522 if(errorMessage_.empty())
00523 errorMessage_ = "Error creating table " + table;
00524
00525 return false;
00526 }
00527
00528 if(hasAutoNumber)
00529 {
00530 string dropSql = " DROP TRIGGER "+ getNameTrigger(table);
00531 execute(dropSql);
00532 dropSql = " DROP SEQUENCE "+ getNameSequence(table);
00533 execute(dropSql);
00534
00535 if(!createSequence(table))
00536 {
00537 deleteTable(table);
00538 return false;
00539 }
00540
00541 if(!createAutoIncrementTrigger(table,fieldName))
00542 {
00543 deleteTable(table);
00544 string sql= "DROP SEQUENCE "+ getNameSequence(table);
00545 execute(sql);
00546 return false;
00547 }
00548 }
00549 return true;
00550 }
00551
00552 bool
00553 TeOCIOracle::addColumn (const string& table, TeAttributeRep &rep)
00554 {
00555 if(!tableExist(table))
00556 return false;
00557
00558 string field = TeGetExtension(rep.name_.c_str());
00559 if(field.empty())
00560 field = rep.name_;
00561
00562 string tab;
00563 tab = " ALTER TABLE " + table + " ADD ( ";
00564 tab += field + " ";
00565
00566 switch (rep.type_)
00567 {
00568 case TeSTRING:
00569 if(rep.numChar_ > 0)
00570 {
00571 tab += "VARCHAR2(" + Te2String(rep.numChar_) + ") ";
00572 }
00573 else
00574 {
00575 tab += "VARCHAR2(4000) ";
00576 }
00577 break;
00578
00579 case TeREAL:
00580 tab += "NUMBER(*,38)";
00581 break;
00582
00583 case TeINT:
00584 tab += "NUMBER(32)";
00585 break;
00586
00587 case TeDATETIME:
00588 tab += "DATE";
00589 break;
00590
00591 case TeCHARACTER:
00592 tab += "CHAR";
00593 break;
00594
00595 case TeBOOLEAN:
00596 tab += "NUMBER(1)";
00597 break;
00598
00599 case TeBLOB:
00600 tab += "BLOB";
00601 break;
00602
00603 default:
00604 tab += "VARCHAR2(" + Te2String(rep.numChar_) + ") ";
00605 break;
00606 }
00607
00608 tab += " ) ";
00609
00610 if(!connection_->execute(tab))
00611 {
00612 if(errorMessage_.empty())
00613 errorMessage_ = "Error inserting a column to table " + table + " !";
00614 return false;
00615 }
00616
00617 alterTableInfoInMemory(table);
00618 return true;
00619 }
00620
00621 bool
00622 TeOCIOracle::deleteTable (const string& table)
00623 {
00624 int f = table.find ("te_collection");
00625
00626 if( table=="te_theme" ||
00627 table=="te_layer" ||
00628 table=="te_representation" ||
00629 table=="te_tables_relation" ||
00630 table=="te_layer_table" ||
00631 table=="te_raster_metadata" ||
00632 table=="te_datum" ||
00633 table=="te_projection" ||
00634 table=="te_view" ||
00635 table=="te_chart_params" ||
00636 table=="te_legend" ||
00637 table=="te_visual" ||
00638 f == 0)
00639 {
00640 errorMessage_ = "Não é possível deletar tabelas do modelo!";
00641 return false;
00642 }
00643
00644 if(tableExist(table))
00645 {
00646 std::string del = "DROP TABLE " + table;
00647 if(!execute(del))
00648 return false;
00649 }
00650 else if(viewExist(table))
00651 {
00652 std::string delView = "DROP VIEW " + table;
00653 if(!execute(delView))
00654 return false;
00655 }
00656
00657 string seq = "DROP SEQUENCE " + getNameSequence(table);
00658 if(!execute(seq))
00659 return false;
00660
00661 return true;
00662 }
00663
00664 bool
00665 TeOCIOracle::execute (const string &q)
00666 {
00667 bool result = connection_->execute(q);
00668
00669 if(!result)
00670 errorMessage_ = connection_->getErrorMessage();
00671
00672 return result;
00673 }
00674
00675 TeDatabasePortal*
00676 TeOCIOracle::getPortal ()
00677 {
00678 TeOCIOraclePortal* ocip = new TeOCIOraclePortal (this);
00679 return ocip;
00680 }
00681
00682 bool
00683 TeOCIOracle::insertRelationInfo(const int tableId, const string& tField,
00684 const string& eTable, const string& eField, int& relId)
00685 {
00686 if(tableId < 0)
00687 {
00688 return false;
00689 }
00690
00691 TeOCIOraclePortal *ocip = (TeOCIOraclePortal*)getPortal();
00692 if(!ocip)
00693 return false;
00694
00695 relId = -1;
00696 string sel = "SELECT relation_id FROM te_tables_relation WHERE";
00697 sel += " related_table_id = " + Te2String(tableId);
00698 sel += " AND related_attr = '" + tField + "'";
00699 sel += " AND external_table_name = '" + eTable + "'";
00700 sel += " AND external_attr = '" + eField + "'";
00701 if (!ocip->query(sel))
00702 {
00703 delete ocip;
00704 return false;
00705 }
00706
00707 if (ocip->fetchRow())
00708 {
00709 relId = atoi(ocip->getData(0));
00710 delete ocip;
00711 return true;
00712 }
00713
00714 string seq = getNameSequence("te_tables_relation");
00715 string sql = " INSERT INTO te_tables_relation( ";
00716 sql += " relation_id, related_table_id, related_attr, ";
00717 sql += " external_table_name, external_attr) ";
00718 sql += " VALUES( ";
00719 sql += seq +".NEXTVAL ";
00720 sql += ", "+ Te2String(tableId);
00721 sql += ",'" + escapeSequence(tField) + "'";
00722 sql += ",'" + escapeSequence(eTable) + "'";
00723 sql += ",'" + escapeSequence(eField) + "')";
00724
00725 if(!execute(sql))
00726 {
00727 errorMessage_ = "Error inserting tables information!";
00728 delete ocip;
00729 return false;
00730 }
00731
00732 ocip->freeResult();
00733 sql = "SELECT "+ seq +".CURRVAL FROM DUAL";
00734 if (!ocip->query(sql))
00735 {
00736 errorMessage_ = "Error in the sequence te_table_relation_seq!";
00737 delete ocip;
00738 return false;
00739 }
00740
00741 if(!ocip->fetchRow())
00742 {
00743 errorMessage_ = "Sequence value not found!";
00744 delete ocip;
00745 return false;
00746 }
00747
00748 relId = atoi((const char*)ocip->getData(0));
00749 delete ocip;
00750 return true;
00751 }
00752
00753 bool
00754 TeOCIOracle::insertTableInfo (int layerId, TeTable &table, const string& user)
00755 {
00756 string ins, seq;
00757
00758 seq = getNameSequence("te_layer_table");
00759 ins = "INSERT INTO te_layer_table ( ";
00760 ins += " table_id, ";
00761 ins += " layer_id, attr_table, unique_id, attr_link, ";
00762 ins += " attr_initial_time, attr_final_time, attr_time_unit, ";
00763 ins += " attr_table_type, user_name) VALUES ( ";
00764 ins += seq + ".NEXTVAL ";
00765
00766 if(layerId>0)
00767 ins += ","+ Te2String(layerId);
00768 else
00769 ins += ", NULL ";
00770
00771 ins += ", '" + escapeSequence(table.name()) + "'";
00772 ins += ", '" + escapeSequence(table.uniqueName()) + "'";
00773 ins += ", '" + escapeSequence(table.linkName()) + "'";
00774 ins += ", '" + escapeSequence(table.attInitialTime()) + "'";
00775 ins += ", '" + escapeSequence(table.attFinalTime()) + "'";
00776 ins += ", " + Te2String(table.attTimeUnit());
00777 ins += ", " + Te2String(table.tableType());
00778 ins += ", '" + escapeSequence(user) + "'";
00779 ins += ")";
00780
00781 if(!execute(ins))
00782 {
00783 errorMessage_ = "Error inserting tables information!";
00784 return false;
00785 }
00786
00787 TeOCIOraclePortal *ocip = (TeOCIOraclePortal*)getPortal();
00788 if(!ocip)
00789 return false;
00790
00791 ins = "SELECT "+ seq +".CURRVAL FROM DUAL";
00792 if (!ocip->query(ins))
00793 {
00794 errorMessage_ = "Error in the sequence te_layer_table_seq!";
00795 delete ocip;
00796 return false;
00797 }
00798
00799 if(!ocip->fetchRow())
00800 {
00801 errorMessage_ = "Sequence value not found!";
00802 delete ocip;
00803 return false;
00804 }
00805
00806 int index = atoi((const char*)ocip->getData(0));
00807 table.setId(index);
00808 delete ocip;
00809 return true;
00810 }
00811
00812
00813 TeDBRelationType
00814 TeOCIOracle::existRelation(const string& tableName, const string& relName)
00815 {
00816 TeOCIOraclePortal *ocip = (TeOCIOraclePortal*)getPortal();
00817 if(!ocip)
00818 return TeNoRelation;
00819
00820 string integ = " SELECT DELETE_RULE ";
00821 integ += " FROM all_constraints ";
00822 integ += " WHERE CONSTRAINT_TYPE = 'R'";
00823 integ += " AND OWNER = '" + TeConvertToUpperCase(user_) + "'";
00824 integ += " AND CONSTRAINT_NAME = '" + TeConvertToUpperCase(relName) + "'";
00825
00826 string temp = " AND TABLE_NAME = '" + TeConvertToUpperCase(tableName) + "' ";
00827 integ += temp;
00828
00829 if(!ocip->query(integ))
00830 {
00831 delete ocip;
00832 return TeNoRelation;
00833 }
00834
00835 string cascade;
00836 if(ocip->fetchRow())
00837 {
00838 cascade = string(ocip->getData(0));
00839 if(cascade== "CASCADE")
00840 {
00841 delete ocip;
00842 return TeRICascadeDeletion;
00843 }
00844
00845 delete ocip;
00846 return TeRINoCascadeDeletion;
00847 }
00848
00849 delete ocip;
00850 return TeNoRelation;
00851 }
00852
00853
00854 bool
00855 TeOCIOracle::createRelation (const string& name, const string& table, const string& fieldName, const string& relatedTable, const string& relatedField, bool cascadeDeletion)
00856 {
00857 string relation = "ALTER TABLE " + table + " ADD ";
00858 relation += " CONSTRAINT " + name;
00859 relation += " FOREIGN KEY ( " + fieldName + ") ";
00860 relation += " REFERENCES " + relatedTable + "(" + relatedField + ")";
00861
00862 if (cascadeDeletion)
00863 relation += " ON DELETE CASCADE ";
00864
00865 if(!execute(relation))
00866 return false;
00867
00868 return true;
00869 }
00870
00871 string
00872 TeOCIOracle::getSQLStatistics (TeGroupingAttr& attrs)
00873 {
00874 string sql = "";
00875 string virg = "";
00876
00877 TeGroupingAttr::iterator it = attrs.begin();
00878 int count = 0;
00879 while(it != attrs.end())
00880 {
00881 if(count>0)
00882 virg = ",";
00883
00884 switch ((*it).second)
00885 {
00886 case TeSUM:
00887 sql += virg +" SUM( "+ (*it).first.name_ +") AS SUM_"+ Te2String(count);
00888 (*it).second = TeNOSTATISTIC;
00889 ++count;
00890 break;
00891 case TeMAXVALUE:
00892 sql += virg +" MAX( "+ (*it).first.name_ +") AS MAX_"+ Te2String(count);
00893 (*it).second = TeNOSTATISTIC;
00894 ++count;
00895 break;
00896 case TeMINVALUE:
00897 sql += virg +" MIN( "+ (*it).first.name_ +") AS MIN_"+ Te2String(count);
00898 (*it).second = TeNOSTATISTIC;
00899 ++count;
00900 break;
00901 case TeCOUNT:
00902 sql += virg +" COUNT( "+ (*it).first.name_ +") AS COUNT_"+ Te2String(count);
00903 (*it).second = TeNOSTATISTIC;
00904 ++count;
00905 break;
00906 case TeMEAN:
00907 sql += virg +" AVG( "+ (*it).first.name_ +") AS AVG_"+ Te2String(count);
00908 (*it).second = TeNOSTATISTIC;
00909 ++count;
00910 break;
00911 case TeSTANDARDDEVIATION:
00912 sql += virg +" STDDEV( "+ (*it).first.name_ +") AS STDDEV_"+ Te2String(count);
00913 (*it).second = TeNOSTATISTIC;
00914 ++count;
00915 break;
00916 case TeVARIANCE:
00917 sql += virg +" VARIANCE( "+ (*it).first.name_ +") AS VAR_"+ Te2String(count);
00918 (*it).second = TeNOSTATISTIC;
00919 ++count;
00920 break;
00921 default:
00922 break;
00923 }
00924 ++it;
00925 }
00926 return sql;
00927 }
00928
00929
00930 string
00931 TeOCIOracle::getSQLAutoNumber(const string& table)
00932 {
00933 return (getNameSequence(table) +".NEXTVAL");
00934 }
00935
00936
00937 bool
00938 TeOCIOracle::insertRasterBlock(const string& table, const string& blockId, const TeCoord2D& ll, const TeCoord2D& ur,
00939 unsigned char *buf,unsigned long size, int band, unsigned int res, unsigned int subband)
00940 {
00941 if (blockId.empty())
00942 {
00943 errorMessage_ = "bloco sem identificador";
00944 return false;
00945 }
00946
00947 TeOCIOraclePortal* portal = (TeOCIOraclePortal*) this->getPortal();
00948 if (!portal)
00949 return false;
00950
00951 bool update = false;
00952 string q =" SELECT * FROM " + table;
00953 q += " WHERE block_id='" + blockId + "'";
00954
00955 if (!portal->query(q))
00956 {
00957 delete portal;
00958 return false;
00959 }
00960
00961 if (portal->fetchRow())
00962 update = true;
00963
00964 delete portal;
00965
00966 try
00967 {
00968 if (!update)
00969 {
00970 q = "INSERT INTO "+ table +" (block_id, band_id, subband, ";
00971 q += " resolution_factor, lower_x, lower_y, upper_x, upper_y, ";
00972 q += " block_size, spatial_data) VALUES ( ";
00973 q += "'" + blockId + "'";
00974 q += ", " + Te2String(band);
00975 q += ", " + Te2String(subband);
00976 q += ", " + Te2String(res);
00977 q += ", " + Te2String(ll.x(), 15);
00978 q += ", " + Te2String(ll.y(), 15);
00979 q += ", " + Te2String(ur.x(), 15);
00980 q += ", " + Te2String(ur.y(), 15);
00981 q += ", " + Te2String(size);
00982 q += ", :blobValue";
00983 q += ")";
00984 if (!connection_->executeBLOBSTM(q, buf, size, ":blobValue"))
00985 return false;
00986 }
00987 else
00988 {
00989 q = " UPDATE "+ table +" SET spatial_data=:blobValue ";
00990 q += " WHERE block_id='" + blockId + "'";
00991 if (!connection_->executeBLOBSTM(q, buf, size, ":blobValue"))
00992 return false;
00993 }
00994 }
00995 catch(...)
00996 {
00997 errorMessage_ = "Error inserting raster block!";
00998 return false;
00999 }
01000 return true;
01001 }
01002
01003 bool
01004 TeOCIOracle::insertBlob (const string& tableName, const string& columnBlob, const string& whereClause, unsigned char* data, int size)
01005 {
01006 TeOCIOraclePortal* portal = (TeOCIOraclePortal*) getPortal();
01007 if (!portal)
01008 return false;
01009
01010 try
01011 {
01012 string q = "SELECT * FROM "+ tableName +" WHERE "+ whereClause;
01013 if((!portal->query(q)) || (!portal->fetchRow()))
01014 {
01015 delete portal;
01016 return false;
01017 }
01018 delete portal;
01019
01020 q = " UPDATE "+ tableName +" SET "+ columnBlob +" = :blobValue";
01021 q += " WHERE "+ whereClause;
01022 if (!connection_->executeBLOBSTM(q, data, size, ":blobValue"))
01023 return false;
01024 }
01025 catch(...)
01026 {
01027 errorMessage_ = "Error!";
01028 return false;
01029 }
01030 return true;
01031 }
01032
01033 bool
01034 TeOCIOracle::insertTable(TeTable &table)
01035 {
01036 string tableName = table.name();
01037 int size = table.size();
01038 TeAttributeList att = table.attributeList();
01039 TeAttributeList::iterator it = att.begin();
01040 TeAttributeList::iterator itEnd = att.end();
01041
01042 if (!beginTransaction())
01043 return false;
01044
01045 TeTableRow row;
01046 int i;
01047 unsigned int j;
01048 int blobIndex = -1;
01049 for ( i = 0; i < size; i++ )
01050 {
01051 row = table[i];
01052 it = att.begin();
01053
01054 j = 1;
01055 int jj = 0;
01056
01057 std::string columnNames = "";
01058 std::string columnValues = "";
01059 while ( it != itEnd )
01060 {
01061
01062
01063
01064 if(!columnNames.empty())
01065 {
01066 columnNames += ",";
01067 columnValues += ",";
01068 }
01069
01070 columnNames += it->rep_.name_;
01071
01072 if(row[jj].empty())
01073
01074 {
01075 columnValues += " NULL ";
01076 ++it;
01077 j++;
01078 jj++;
01079 continue;
01080 }
01081
01082 string oracleFormat="";
01083 string dateTime="";
01084 if(((*it).rep_.type_==TeDATETIME) && (!row[jj].empty()))
01085 {
01086 TeTime t (row[jj], (*it).dateChronon_, (*it).dateTimeFormat_, (*it).dateSeparator_,
01087 (*it).timeSeparator_, (*it).indicatorPM_);
01088
01089 dateTime = t.getDateTime("DDsMMsYYYYsHHsmmsSS");
01090 oracleFormat = " TO_DATE('" + dateTime + "', 'DD/MM/YYYY HH24:MI:SS')";
01091 }
01092
01093 switch ((*it).rep_.type_)
01094 {
01095 case TeSTRING:
01096 columnValues += "'"+ escapeSequence(row[jj]) +"'";
01097 break;
01098 case TeREAL:
01099 columnValues += row[jj];
01100 break;
01101 case TeINT:
01102 columnValues += row[jj];
01103 break;
01104 case TeDATETIME:
01105 columnValues += oracleFormat;
01106 break;
01107 case TeCHARACTER:
01108 columnValues += "'" + escapeSequence(row[jj]) + "'";
01109 break;
01110 case TeBOOLEAN:
01111 {
01112 std::string value = "0";
01113 if(row[jj] == "1" || TeConvertToLowerCase(row[jj]) == "t" || TeConvertToLowerCase(row[jj]) == "true")
01114 {
01115 value = "1";
01116 }
01117 columnValues += value;
01118 }
01119 break;
01120 case TeBLOB:
01121 blobIndex = jj;
01122 columnValues += " :blobValue ";
01123 break;
01124 default:
01125 columnValues += "'"+ escapeSequence(row[jj]) +"'";
01126 break;
01127 }
01128 ++it;
01129 j++;
01130 jj++;
01131 }
01132 std::string q = "INSERT INTO "+tableName+"("+columnNames+") VALUES("+columnValues+")";
01133
01134
01135 if(blobIndex>=0)
01136 {
01137 if(!connection_->executeBLOBSTM(q, (unsigned char *) row[blobIndex].c_str(), row[blobIndex].size(), ":blobValue"))
01138 {
01139 rollbackTransaction();
01140 return false;
01141 }
01142 }
01143 else
01144 {
01145 if(!execute(q))
01146 {
01147 rollbackTransaction();
01148 return false;
01149 }
01150 }
01151 }
01152 if (!commitTransaction())
01153 return false;
01154 return true;
01155 }
01156
01157 bool
01158 TeOCIOracle::alterTable (const string& table, TeAttributeRep &rep, const string& oldColName)
01159 {
01160 if(!tableExist(table))
01161 return false;
01162
01163 if(!oldColName.empty() && oldColName != rep.name_ )
01164 {
01165 string add = " ALTER TABLE "+ table +" RENAME COLUMN "+ oldColName +" TO "+ rep.name_;
01166 if(!execute(add))
01167 {
01168 if(errorMessage_.empty())
01169 errorMessage_ = "Error alter table " + table + " !";
01170 return false;
01171 }
01172 }
01173
01174 string tab = " ALTER TABLE "+ table +" MODIFY ( ";
01175 tab += rep.name_ + " ";
01176 switch (rep.type_)
01177 {
01178 case TeSTRING:
01179 if ( rep.numChar_ > 0 )
01180 tab += "VARCHAR2(" + Te2String(rep.numChar_) + ") ";
01181 else
01182 tab += "VARCHAR2(4000) ";
01183 break;
01184
01185 case TeREAL:
01186 if(rep.decimals_>0)
01187 tab += " NUMBER(*,"+ Te2String(rep.decimals_) +") ";
01188 else
01189 tab += " NUMBER(*,38) ";
01190 break;
01191
01192 case TeINT:
01193 tab += " NUMBER(32) ";
01194 break;
01195
01196 case TeDATETIME:
01197 tab += " DATE ";
01198 break;
01199
01200 case TeCHARACTER:
01201 tab += " CHAR ";
01202 break;
01203
01204 case TeBOOLEAN:
01205 tab += " NUMBER(1) ";
01206 break;
01207
01208 default:
01209 tab += " VARCHAR2(" + Te2String(rep.numChar_) + ") ";
01210 break;
01211 }
01212
01213 tab += " )";
01214
01215 if(!execute(tab))
01216 {
01217 if(errorMessage_.empty())
01218 errorMessage_ = "Error alter table " + table + " !";
01219 return false;
01220 }
01221
01222 string tableId;
01223 TeDatabasePortal* portal = getPortal();
01224 string sql = "SELECT table_id FROM te_layer_table WHERE attr_table = '" + table + "'";
01225 if(portal->query(sql) && portal->fetchRow())
01226 tableId = portal->getData(0);
01227
01228 delete portal;
01229
01230 if(tableId.empty() == false)
01231 {
01232 if(oldColName.empty() == false)
01233 {
01234
01235 sql = "UPDATE te_tables_relation SET related_attr = '" + rep.name_ + "'";
01236 sql += " WHERE related_table_id = " + tableId;
01237 sql += " AND related_attr = '" + oldColName + "'";
01238 if(execute(sql) == false)
01239 return false;
01240
01241 sql = "UPDATE te_tables_relation SET external_attr = '" + rep.name_ + "'";
01242 sql += " WHERE external_table_name = '" + table + "'";
01243 sql += " AND external_attr = '" + oldColName + "'";
01244 if(execute(sql) == false)
01245 return false;
01246
01247
01248 sql = "UPDATE te_grouping SET grouping_attr = '" + table + "." + rep.name_ + "'";
01249 sql += " WHERE grouping_attr = '" + table + "." + oldColName + "'";
01250 if(execute(sql) == false)
01251 return false;
01252 }
01253 else
01254 {
01255
01256 sql = "DELETE FROM te_tables_relation WHERE (related_table_id = " + tableId;
01257 sql += " AND related_attr = '" + rep.name_ + "')";
01258 sql += " OR (external_table_name = '" + table + "'";
01259 sql += " AND external_attr = '" + rep.name_ + "')";
01260 if(execute(sql) == false)
01261 return false;
01262
01263
01264 TeDatabasePortal* portal = getPortal();
01265 sql = "SELECT theme_id FROM te_grouping WHERE grouping_attr = '" + table + "." + oldColName + "'";
01266 if(portal->query(sql) && portal->fetchRow())
01267 {
01268 string themeId = portal->getData(0);
01269
01270 sql = "DELETE FROM te_legend WHERE theme_id = " + themeId + " AND group_id >= 0";
01271 if(execute(sql) == false)
01272 {
01273 delete portal;
01274 return false;
01275 }
01276 }
01277 delete portal;
01278
01279 sql = "DELETE FROM te_grouping";
01280 sql += " WHERE grouping_attr = '" + table + "." + oldColName + "'";
01281 if(execute(sql) == false)
01282 return false;
01283 }
01284 }
01285 alterTableInfoInMemory(table);
01286 return true;
01287 }
01288
01289 bool
01290 TeOCIOracle::alterTable(const string& oldTableName, const string& newTableName)
01291 {
01292 string sql = " ALTER TABLE "+ oldTableName +" RENAME TO "+ newTableName;
01293 if(!this->execute(sql))
01294 return false;
01295
01296
01297 sql = " UPDATE te_layer_table ";
01298 sql += " SET attr_table = '"+ newTableName +"'";
01299 sql += " WHERE attr_table = '"+ oldTableName +"'";
01300 execute(sql);
01301
01302
01303 sql = " UPDATE te_tables_relation ";
01304 sql += " SET external_table_name = '"+ newTableName +"'";
01305 sql += " WHERE external_table_name = '"+ oldTableName +"'";
01306 execute(sql);
01307
01308 alterTableInfoInMemory(newTableName, oldTableName);
01309 return true;
01310 }
01311
01312
01313 bool
01314 TeOCIOracle::updateTable (TeTable &table)
01315 {
01316 string tableName = table.name();
01317 TeAttributeList att = table.attributeList();
01318 TeAttributeList::iterator it = att.begin();
01319
01320 int blobIndex = -1;
01321 TeTableRow row;
01322 unsigned int i;
01323 unsigned int j;
01324 string uniqueName = table.uniqueName();
01325 string uniqueVal;
01326
01327 if (!beginTransaction())
01328 return false;
01329
01330 for ( i = 0; i < table.size(); i++ )
01331 {
01332 row = table[i];
01333 it = att.begin();
01334 string q = "UPDATE "+tableName+" SET ";
01335 j = 1;
01336 int jj = 0;
01337 while ( it != att.end() )
01338 {
01339 string oracleFormat;
01340 string dateTime;
01341 if((*it).rep_.type_==TeDATETIME)
01342 {
01343 TeTime t (row[jj], (*it).dateChronon_, (*it).dateTimeFormat_, (*it).dateSeparator_,
01344 (*it).timeSeparator_, (*it).indicatorPM_);
01345
01346 dateTime = t.getDateTime();
01347 oracleFormat = "DD/MM/YYYY HH24:MI:SS";
01348 }
01349
01350 if (uniqueName != (*it).rep_.name_)
01351 {
01352 q += (*it).rep_.name_ + "=";
01353 switch ((*it).rep_.type_)
01354 {
01355 case TeSTRING:
01356 q += "'"+escapeSequence(row[jj])+"'";
01357 break;
01358 case TeREAL:
01359 q += row[jj];
01360 break;
01361 case TeINT:
01362 q += row[jj];
01363 break;
01364 case TeDATETIME:
01365 q += " TO_DATE('" + dateTime + "', '"+ oracleFormat +"')";
01366 break;
01367 case TeCHARACTER:
01368 q += "'" + escapeSequence(row[jj]) + "'";
01369 break;
01370 case TeBLOB:
01371 blobIndex = jj;
01372 q += " :blobValue ";
01373 break;
01374 default:
01375 q += "'"+escapeSequence(row[jj])+"'";
01376 break;
01377 }
01378 if (j<att.size())
01379 q+= ",";
01380 }
01381 else
01382 uniqueVal = row[jj];
01383
01384 ++it;
01385 j++;
01386 jj++;
01387 }
01388 q += " WHERE " + uniqueName + " = " + uniqueVal;
01389
01390
01391 if(blobIndex>=0)
01392 {
01393 if (!connection_->executeBLOBSTM(q, (unsigned char *) row[blobIndex].c_str(),
01394 row[blobIndex].size(), ":blobValue"))
01395 continue;
01396 }
01397 else
01398 {
01399 if (!execute(q))
01400 continue;
01401 }
01402 }
01403 if (!commitTransaction())
01404 return false;
01405 return true;
01406 }
01407
01408
01409 bool
01410 TeOCIOracle::insertProjection (TeProjection *proj)
01411 {
01412 string insert = "INSERT INTO te_projection (";
01413 insert += " projection_id, ";
01414 insert += " name, long0, lat0,";
01415 insert += " offx, offy, stlat1, stlat2, unit, scale, hemis, datum )";
01416 insert += " VALUES ( ";
01417 insert += " te_projection_seq.NEXTVAL ";
01418 insert += ", '" + escapeSequence(proj->name()) + "'";
01419 insert += ", " + Te2String(proj->params().lon0*TeCRD,15);
01420 insert += ", " + Te2String(proj->params().lat0*TeCRD,15);
01421 insert += ", " + Te2String(proj->params().offx,15);
01422 insert += ", " + Te2String(proj->params().offy,15);
01423 insert += ", " + Te2String(proj->params().stlat1*TeCRD,15);
01424 insert += ", " + Te2String(proj->params().stlat2*TeCRD,15);
01425 insert += ", '" + escapeSequence(proj->params().units) + "'";
01426 insert += ", " + Te2String(proj->params().scale,15);
01427 insert += ", " + Te2String(proj->params().hemisphere, 15);
01428 insert += ", '" + escapeSequence(proj->params().datum.name()) + "'";
01429 insert += ")";
01430
01431 if(!execute(insert))
01432 {
01433 errorMessage_ = "Error inserting in the table te_projection!";
01434 return false;
01435 }
01436
01437 TeOCIOraclePortal *ocip = (TeOCIOraclePortal*) getPortal();
01438 if(!ocip)
01439 return false;
01440
01441 string seq = "SELECT te_projection_seq.CURRVAL FROM DUAL";
01442 if(!ocip->query(seq))
01443 {
01444 errorMessage_ = "Error in the sequence te_projection_seq!";
01445 delete ocip;
01446 return false;
01447 }
01448
01449 if(!ocip->fetchRow())
01450 {
01451 errorMessage_ = "Sequence value not found!";
01452 delete ocip;
01453 return false;
01454 }
01455
01456 proj->id(atoi((const char*)ocip->getData(0)));
01457 delete ocip;
01458
01459 int srsid = proj->epsgCode();
01460 return insertSRSId(proj, srsid);
01461 }
01462
01463
01464 bool
01465 TeOCIOracle::insertRepresentation (int layerId, TeRepresentation& rep)
01466 {
01467 if (layerId <= 0)
01468 return false;
01469
01470 string ins;
01471 TeOCIOraclePortal *ocip = (TeOCIOraclePortal*) getPortal();
01472 if(!ocip)
01473 return false;
01474
01475 ins = " INSERT INTO te_representation ( ";
01476 ins += " repres_id, ";
01477 ins += " layer_id, geom_type, geom_table, ";
01478 ins += " description, lower_x, lower_y, upper_x, upper_y, res_x, res_y, num_cols, ";
01479 ins += " num_rows) VALUES (";
01480 ins += " te_representation_seq.NEXTVAL ";
01481 ins += ", " + Te2String(layerId);
01482 ins += ", " + Te2String(static_cast<int>(rep.geomRep_));
01483 ins += ", '" + escapeSequence(rep.tableName_) + "'";
01484 ins += ", '" + escapeSequence(rep.description_) + "'";
01485 ins += ", " + Te2String(rep.box_.x1(),15);
01486 ins += ", " + Te2String(rep.box_.y1(),15);
01487 ins += ", " + Te2String(rep.box_.x2(),15);
01488 ins += ", " + Te2String(rep.box_.y2(),15);
01489 ins += ", " + Te2String(rep.resX_,15);
01490 ins += ", " + Te2String(rep.resY_,15);
01491 ins += ", " + Te2String(rep.nCols_);
01492 ins += ", " + Te2String(rep.nLins_);
01493 ins += ")";
01494
01495 if(!execute(ins))
01496 {
01497 errorMessage_ = "Error inserting in the table te_representation!";
01498 delete ocip;
01499 return false;
01500 }
01501
01502 string seq = "SELECT te_representation_seq.CURRVAL FROM DUAL";
01503 if(!ocip->query(seq))
01504 {
01505 errorMessage_ = "Error in the sequence te_representation_seq!";
01506 delete ocip;
01507 return false;
01508 }
01509
01510 if(!ocip->fetchRow())
01511 {
01512 errorMessage_ = "Sequence value not found!";
01513 delete ocip;
01514 return false;
01515 }
01516
01517 int id = atoi((const char*)ocip->getData(0));
01518 rep.id_ = id;
01519 delete ocip;
01520 return true;
01521 }
01522
01523
01524 bool
01525 TeOCIOracle::insertLegend (TeLegendEntry* leg)
01526 {
01527 TeOCIOraclePortal *ocip = (TeOCIOraclePortal*)getPortal();
01528 if(!ocip)
01529 return false;
01530
01531 string ins = "INSERT INTO te_legend ( ";
01532 ins += " legend_id, ";
01533 ins += " theme_id, group_id, ";
01534 ins += " num_objs, lower_value, upper_value, label) VALUES ( ";
01535 ins += "te_legend_seq.NEXTVAL";
01536 ins += ", " + Te2String(leg->theme());
01537 ins += ", " + Te2String(leg->group());
01538 ins += ", " + Te2String(leg->count());
01539 ins += ", '" + escapeSequence(leg->from()) + "'";
01540 ins += ", '" + escapeSequence(leg->to()) + "'";
01541 ins += ", '" + escapeSequence(leg->label()) + "'";
01542 ins += ")";
01543
01544 if (!execute(ins))
01545 {
01546 errorMessage_ = "Error inserting in the table te_legend!";
01547 delete ocip;
01548 return false;
01549 }
01550
01551 string seq = "SELECT te_legend_seq.CURRVAL FROM DUAL";
01552 if(!ocip->query(seq))
01553 {
01554 errorMessage_ = "Error in the sequence te_theme_seq!";
01555 delete ocip;
01556 return false;
01557 }
01558
01559 if(!ocip->fetchRow())
01560 {
01561 errorMessage_ = "Sequence value not found!";;
01562 delete ocip;
01563 return false;
01564 }
01565
01566 int index = atoi((const char*)ocip->getData(0));
01567 leg->id(index);
01568
01569 delete ocip;
01570 legendMap()[leg->id()] = leg;
01571 return insertVisual(leg);
01572 }
01573
01574 bool
01575 TeOCIOracle::insertPolygonSet(const string& table, TePolygonSet &ps)
01576 {
01577 if(!getValueSequence(table))
01578 return false;
01579
01580 for (unsigned int i = 0; i < ps.size(); i++ )
01581 {
01582 TePolygon& poly = ps [i];
01583 if (!insertPolygon (table,poly))
01584 return false;
01585 }
01586 return true;
01587 }
01588
01589 bool
01590 TeOCIOracle::insertPolygon (const string& table, TePolygon &poly)
01591 {
01592 double extmax;
01593 unsigned int i, k, ni, size;
01594 double *points = 0;
01595 try
01596 {
01597 long parentId = 0;
01598
01599 for ( k = 0; k < poly.size(); k++ )
01600 {
01601 TeLinearRing ring (poly[k]);
01602 TeBox b = ring.box();
01603 size = ring.size();
01604 ni = 0;
01605
01606
01607 extmax = MAX(b.width(),b.height());
01608
01609
01610 if (k==0)
01611 ni = poly.size()-1;
01612 else
01613 ni = 0;
01614
01615 ++sequenceCont_;
01616 if(k==0)
01617 parentId=sequenceCont_;
01618
01619
01620 string ins = "INSERT INTO " + table + " ( ";
01621 ins += " geom_id, ";
01622 ins += " object_id, num_coords, num_holes, ";
01623 ins += " parent_id, lower_x, lower_y, upper_x, upper_y, ";
01624 ins += " ext_max, spatial_data) VALUES ( ";
01625 ins += getNameSequence(table) +".NEXTVAL";
01626 ins += ", '" + escapeSequence(poly.objectId()) + "'";
01627 ins += ","+ Te2String(size);
01628 ins += ","+ Te2String(ni);
01629 ins += ","+ Te2String((long)parentId);
01630 ins += ","+ Te2String(b.lowerLeft().x(), 15);
01631 ins += ","+ Te2String(b.lowerLeft().y(), 15);
01632 ins += ","+ Te2String(b.upperRight().x(), 15);
01633 ins += ","+ Te2String(b.upperRight().y(), 15);
01634 ins += ","+ Te2String(extmax);
01635 ins += ", :blobValue ";
01636 ins += ")";
01637
01638
01639 points = new double[2*ring.size()];
01640 int iac = 0;
01641 for (i=0;i<ring.size();i++)
01642 {
01643 points[iac++]=ring[i].x();
01644 points[iac++]=ring[i].y();
01645 }
01646 unsigned long bLen = 2*(ring.size())*sizeof(double)*sizeof(unsigned char);
01647
01648 if (!connection_->executeBLOBSTM(ins, (unsigned char*)points, bLen, ":blobValue"))
01649 {
01650 errorMessage_ = "Error inserting in the table " + table + "!";
01651 delete[] points;
01652 return false;
01653 }
01654
01655 poly.geomId(sequenceCont_);
01656 if(points)
01657 delete[] points;
01658 points=NULL;
01659 }
01660 }
01661 catch(...)
01662 {
01663 errorMessage_ = "Error inserting polygon!";
01664 if(points)
01665 delete[] points;
01666 return false;
01667 }
01668
01669 return true;
01670 }
01671
01672 bool
01673 TeOCIOracle::updatePolygon (const string& table, TePolygon &poly)
01674 {
01675 double extmax;
01676 unsigned int k, ni, size, i;
01677 double *points = NULL;
01678
01679 try
01680 {
01681 TeBox b = poly[0].box();
01682 for ( k = 0; k < poly.size(); k++ )
01683 {
01684
01685 TeLinearRing ring ( poly[k] );
01686 size = ring.size();
01687 ni = 0;
01688 if (k==0)
01689 {
01690 extmax = MAX(b.width(),b.height());
01691 ni = poly.size()-1;
01692 }
01693 else
01694 ni = 0;
01695
01696
01697 string sql;
01698 sql = " UPDATE " + table + " SET ";
01699 sql += ", object_id = '" + poly.objectId() + "'";
01700 sql += ", num_coords = " + Te2String(size);
01701 sql += ", num_holes = " + Te2String(ni);
01702 sql += ", parent_id = " + ((long)poly[0].geomId());
01703 sql += ", lower_x = " + Te2String(b.lowerLeft().x(), 15);
01704 sql += ", lower_y = " + Te2String(b.lowerLeft().y(), 15);
01705 sql += ", upper_x = " + Te2String(b.upperRight().x(), 15);
01706 sql += ", upper_y = " + Te2String(b.upperRight().y(), 15);
01707 sql += ", ext_max = " + Te2String(extmax);
01708 sql += ", spatial_data = :blobValue ";
01709 sql += " WHERE geom_id = "+ Te2String(ring.geomId());
01710
01711
01712 points = new double[2*ring.size()];
01713 int iac = 0;
01714 for (i=0;i<ring.size();i++)
01715 {
01716 points[iac++]=ring[i].x();
01717 points[iac++]=ring[i].y();
01718 }
01719
01720 int bLen = 2*(ring.size())*sizeof(double)*sizeof(unsigned char);
01721
01722 if (!connection_->executeBLOBSTM(sql, (unsigned char*)points, bLen, ":blobValue"))
01723 {
01724 errorMessage_ = "Error inserting in the table " + table + "!";
01725 delete[] points;
01726 return false;
01727 }
01728
01729 if(points)
01730 delete[] points;
01731 points=NULL;
01732 }
01733 }
01734 catch(...)
01735 {
01736 errorMessage_ = "Error updating polygons!";
01737 if(points)
01738 delete[] points;
01739 return false;
01740 }
01741 return true;
01742 }
01743
01744 bool
01745 TeOCIOracle::getValueSequence(const string& table)
01746 {
01747 TeOCIOraclePortal *ocip = (TeOCIOraclePortal*)getPortal();
01748 if(!ocip)
01749 return false;
01750
01751 if (getNameSequence(table) != sequenceName_)
01752 {
01753 string seq = "SELECT "+ getNameSequence(table) +".NEXTVAL FROM DUAL";
01754
01755 if(!ocip->query(seq) || !ocip->fetchRow())
01756 {
01757 errorMessage_ = "Error in the sequence " + table + "_seq!";
01758 sequenceName_ = "";
01759 delete ocip;
01760 return false;
01761 }
01762
01763 sequenceName_ = getNameSequence(table);
01764 sequenceCont_ = atoi(ocip->getData(0));
01765 }
01766
01767 delete ocip;
01768 return true;
01769 }
01770
01771 bool
01772 TeOCIOracle::insertLineSet(const string& table, TeLineSet &ls)
01773 {
01774 if(!getValueSequence(table))
01775 return false;
01776
01777 for (unsigned int i = 0; i < ls.size(); i++ )
01778 {
01779 TeLine2D& line = ls [i];
01780 if (!insertLine (table,line))
01781 return false;
01782 }
01783 return true;
01784 }
01785
01786 bool
01787 TeOCIOracle::insertLine (const string& table, TeLine2D &line)
01788 {
01789 double extmax;
01790 unsigned int i, size;
01791 double *points = 0;
01792
01793 try
01794 {
01795 TeBox b = line.box();
01796 size = line.size();
01797
01798 extmax = MAX(b.width(),b.height());
01799
01800
01801 string ins = "INSERT INTO " + table + " ( ";
01802 ins += " geom_id, ";
01803 ins += " object_id, num_coords, ";
01804 ins += " lower_x, lower_y, upper_x, upper_y, ";
01805 ins += " ext_max, spatial_data) VALUES ( ";
01806 ins += getNameSequence(table) +".NEXTVAL";
01807 ins += ", '" + escapeSequence(line.objectId()) + "'";
01808 ins += ","+ Te2String(size);
01809 ins += ","+ Te2String(b.lowerLeft().x(), 15);
01810 ins += ","+ Te2String(b.lowerLeft().y(), 15);
01811 ins += ","+ Te2String(b.upperRight().x(), 15);
01812 ins += ","+ Te2String(b.upperRight().y(), 15);
01813 ins += ","+ Te2String(extmax);
01814 ins += ", :blobValue";
01815 ins += ")";
01816
01817
01818 points = new double[2*line.size()];
01819 int iac = 0;
01820 for (i=0;i<line.size();i++)
01821 {
01822 points[iac++]=line[i].x();
01823 points[iac++]=line[i].y();
01824 }
01825 unsigned long bLen = 2*(line.size())*sizeof(double)*sizeof(unsigned char);
01826
01827 if (!connection_->executeBLOBSTM(ins, (unsigned char*)points, bLen, ":blobValue"))
01828 {
01829 errorMessage_ = "Error inserting in the table " + table + "!";
01830 delete[] points;
01831 return false;
01832 }
01833 ++sequenceCont_;
01834 line.geomId(sequenceCont_);
01835 if(points)
01836 delete[] points;
01837 points=NULL;
01838 }
01839 catch(...)
01840 {
01841 errorMessage_ = "Error inserting line!";
01842 if(points)
01843 delete[] points;
01844 return false;
01845 }
01846
01847 return true;
01848 }
01849
01850
01851 bool
01852 TeOCIOracle::updateLine(const string& table, TeLine2D &line)
01853 {
01854 double extmax;
01855 unsigned int size, i;
01856 double *points = NULL;
01857
01858 try
01859 {
01860 TeBox b = line.box();
01861 size = line.size();
01862 extmax = MAX(b.width(),b.height());
01863
01864
01865 string sql = "UPDATE " + table + " SET ";
01866 sql += " object_id= '" + line.objectId() + "'";
01867 sql += ", num_coords= " + ((long)size);
01868 sql += ", lower_x= " + Te2String(b.lowerLeft().x(), 15);
01869 sql += ", lower_y= " + Te2String(b.lowerLeft().y(), 15);
01870 sql += ", upper_x= " + Te2String(b.upperRight().x(), 15);
01871 sql += ", upper_y= " + Te2String(b.upperRight().y(), 15);
01872 sql += ", ext_max= " + Te2String(extmax);
01873 sql += ", spatial_data = :blobValue ";
01874 sql += " WHERE geom_id = "+ Te2String(line.geomId());
01875
01876
01877 points = new double[2*line.size()];
01878 int iac = 0;
01879 for (i=0;i<line.size();i++)
01880 {
01881 points[iac++]=line[i].x();
01882 points[iac++]=line[i].y();
01883 }
01884 int bLen = 2*(line.size())*sizeof(double)*sizeof(unsigned char);
01885
01886 if (!connection_->executeBLOBSTM(sql, (unsigned char*)points, bLen, ":blobValue"))
01887 {
01888 errorMessage_ = "Error inserting in the table " + table + "!";
01889 delete[] points;
01890 return false;
01891 }
01892
01893 if(points)
01894 delete[] points;
01895 points = NULL;
01896 }
01897 catch(...)
01898 {
01899 errorMessage_ = "Error updating lines!";
01900 if(points)
01901 delete[] points;
01902 return false;
01903 }
01904 return true;
01905 }
01906
01907 bool
01908 TeOCIOracle::insertPoint(const string& table, TePoint &point)
01909 {
01910 string ins = "INSERT INTO " + table + " ( ";
01911 ins += " geom_id, ";
01912 ins += " object_id, x, y) ";
01913 ins += " VALUES ( ";
01914 ins += getNameSequence(table) +".NEXTVAL";
01915 ins += ", '" + escapeSequence(point.objectId()) + "'";
01916 ins += ", " + Te2String(point.location().x(), 15);
01917 ins += ", " + Te2String(point.location().y(), 15);
01918 ins += " ) ";
01919
01920 if(!execute(ins))
01921 {
01922 errorMessage_ = "Error inserting points!";
01923 return false;
01924 }
01925 return true;
01926 }
01927
01928
01929 bool
01930 TeOCIOracle::insertText (const string& table, TeText &text)
01931 {
01932 string ins = "INSERT INTO " + table + " ( ";
01933 ins += " geom_id, ";
01934 ins += " object_id, x, y, text_value, angle, height, alignment_vert, ";
01935 ins += " alignment_horiz) VALUES ( ";
01936 ins += getNameSequence(table) +".NEXTVAL";
01937 ins += ", '" + escapeSequence(text.objectId()) + "'";
01938 ins += ", " + Te2String(text.location().x(),15);
01939 ins += ", " + Te2String(text.location().y(),15);
01940 ins += ", '" + escapeSequence(text.textValue()) + "'";
01941 ins += ", " + Te2String(text.angle(),15);
01942 ins += ", " + Te2String(text.height(),15);
01943 ins += ", " + Te2String(text.alignmentVert(),15);
01944 ins += ", " + Te2String(text.alignmentHoriz(),15);
01945 ins += " )";
01946
01947 if(!execute(ins))
01948 {
01949 errorMessage_ = "Error inserting texts!";
01950 return false;
01951 }
01952 return true;
01953 }
01954
01955 bool
01956 TeOCIOracle::insertArc (const string& table, TeArc &arc)
01957 {
01958 string ins = "INSERT INTO " + table + " (";
01959 ins += " geom_id, ";
01960 ins += " object_id, from_node, to_node ) ";
01961 ins += " VALUES ( ";
01962 ins += getNameSequence(table) +".NEXTVAL";
01963 ins += ", '" + escapeSequence(arc.objectId()) + "'";
01964 ins += ", " + Te2String(arc.fromNode().geomId());
01965 ins += ", " + Te2String(arc.toNode().geomId());
01966 ins += " )";
01967
01968 if(!execute(ins))
01969 {
01970 errorMessage_ = "Error inserting arc!";
01971 return false;
01972 }
01973 return true;
01974 }
01975
01976 bool
01977 TeOCIOracle::insertNode (const string& table, TeNode &node)
01978 {
01979 string ins = "INSERT INTO " + table + " ( ";
01980 ins += " geom_id, ";
01981 ins += " object_id, x, y) ";
01982 ins += " VALUES ( ";
01983 ins += getNameSequence(table) +".NEXTVAL";
01984 ins += ", '" + escapeSequence(node.objectId()) + "'";
01985 ins += ", " + Te2String(node.location().x(), 15);
01986 ins += ", " + Te2String(node.location().y(), 15);
01987 ins += " ) ";
01988
01989 if(!execute(ins))
01990 {
01991 errorMessage_ = "Error inserting nodes!";
01992 return false;
01993 }
01994 return true;
01995 }
01996
01997 bool
01998 TeOCIOracle::insertCell (const string& table, TeCell &cell )
01999 {
02000 TeBox b = cell.box();
02001
02002 string ins = "INSERT INTO " + table + " ( ";
02003 ins += " geom_id, ";
02004 ins += " object_id, col_number, row_number, lower_x, ";
02005 ins += " lower_y, upper_x, upper_y ) ";
02006 ins += " VALUES ( ";
02007 ins += getNameSequence(table) +".NEXTVAL";
02008 ins += ", '" + escapeSequence(cell.objectId ()) + "'";
02009 ins += ", " + Te2String(cell.column());
02010 ins += ", " + Te2String(cell.line());
02011 ins += ", " + Te2String(b.lowerLeft().x(), 15);
02012 ins += ", " + Te2String(b.lowerLeft().y(), 15);
02013 ins += ", " + Te2String(b.upperRight().x(), 15);
02014 ins += ", " + Te2String(b.upperRight().y(), 15);
02015 ins += " )";
02016
02017 if(!execute(ins))
02018 {
02019 errorMessage_ = "Error inserting cells!";
02020 return false;
02021 }
02022 return true;
02023 }
02024
02025 bool
02026 TeOCIOracle::insertLayer(TeLayer* layer)
02027 {
02028 int index;
02029 TeProjection* proj = layer->projection();
02030 if (!proj || !insertProjection(proj))
02031 {
02032 errorMessage_ = "Não é possível inserir layer sem projeção";
02033 return false;
02034 }
02035 string ins = "INSERT INTO te_layer (";
02036 ins += " layer_id, ";
02037 ins += " projection_id, name ";
02038 ins += ", lower_x, lower_y, upper_x, upper_y, edition_time) ";
02039 ins += " VALUES ( ";
02040 ins += "te_layer_seq.NEXTVAL";
02041 ins += ", "+ Te2String(proj->id());
02042 ins += ", '" + escapeSequence(layer->name()) + "'";
02043 ins += ", " + Te2String(layer->box().x1(),15);
02044 ins += ", " + Te2String(layer->box().y1(),15);
02045 ins += ", " + Te2String(layer->box().x2(),15);
02046 ins += ", " + Te2String(layer->box().y2(),15);
02047 TeTime editionTime = layer->getEditionTime();
02048 ins += ", " + getSQLTime(editionTime);
02049 ins += ")";
02050
02051 if(!execute(ins))
02052 {
02053 errorMessage_ = "Error inserting in the table te_layer!";
02054 return false;
02055 }
02056
02057 TeOCIOraclePortal *ocip = (TeOCIOraclePortal*)getPortal();
02058 if(!ocip)
02059 return false;
02060
02061 ins = "SELECT te_layer_seq.CURRVAL FROM DUAL";
02062 if (!ocip->query(ins))
02063 {
02064 errorMessage_ = "Error in the sequence te_layer_seq!";
02065 delete ocip;
02066 return false;
02067 }
02068
02069 if(!ocip->fetchRow())
02070 {
02071 errorMessage_ = "Sequence value not found!";
02072 delete ocip;
02073 return false;
02074 }
02075
02076 index = atoi((const char*)ocip->getData(0));
02077 layer->id(index);
02078 delete ocip;
02079 layerMap()[layer->id()] = layer;
02080 return true;
02081 }
02082
02083 bool
02084 TeOCIOracle::insertProject(TeProject* project)
02085 {
02086 if (!project)
02087 return false;
02088
02089 string ins = "INSERT INTO te_project (";
02090 ins += " project_id, ";
02091 ins += " name, description, current_view) ";
02092 ins += " VALUES ( ";
02093 ins += "te_project_seq.NEXTVAL";
02094 ins += ", '" + escapeSequence(project->name()) + "'";
02095 ins += ", '" + escapeSequence(project->description()) + "'";
02096 ins += ", " + Te2String(project->getCurrentViewId());
02097 ins += ")";
02098
02099 if(!execute(ins))
02100 {
02101 errorMessage_ = "Error inserting in the table te_project!";
02102 return false;
02103 }
02104
02105 TeOCIOraclePortal *ocip = (TeOCIOraclePortal*)getPortal();
02106 if(!ocip)
02107 return false;
02108
02109 ins = "SELECT te_project_seq.CURRVAL FROM DUAL";
02110 if (!ocip->query(ins))
02111 {
02112 errorMessage_ = "Error in the sequence te_project_seq!";
02113 delete ocip;
02114 return false;
02115 }
02116
02117 if(!ocip->fetchRow())
02118 {
02119 errorMessage_ = "Sequence value not found!";
02120 delete ocip;
02121 return false;
02122 }
02123
02124 int index = atoi((const char*)ocip->getData(0));
02125 project->setId(index);
02126 projectMap()[project->id()] = project;
02127 for (unsigned int i=0; i<project->getViewVector().size(); i++)
02128 insertProjectViewRel(project->id(), project->getViewVector()[i]);
02129 delete ocip;
02130 return true;
02131 }
02132
02133 bool
02134 TeOCIOracle::deleteLayer(int layerId)
02135 {
02136
02137 if(!deleteLayerTable(layerId))
02138 return false;
02139
02140 TeOCIOraclePortal* portal = (TeOCIOraclePortal*) this->getPortal();
02141 if (!portal)
02142 return false;
02143
02144 string geomTable;
02145 string sql = "SELECT projection_id FROM te_layer WHERE layer_id = ";
02146 sql += Te2String(layerId);
02147
02148 if (!portal->query(sql))
02149 {
02150 delete portal;
02151 return false;
02152 }
02153
02154 if (!portal->fetchRow())
02155 {
02156 delete portal;
02157 return false;
02158 }
02159 string projId = portal->getData("projection_id");
02160 portal->freeResult();
02161
02162
02163 sql = "SELECT * FROM te_representation WHERE layer_id = "+ Te2String(layerId);
02164 if (!portal->query (sql))
02165 {
02166 delete portal;
02167 return false;
02168 }
02169
02170 while (portal->fetchRow())
02171 {
02172 geomTable = portal->getData("geom_table");
02173
02174
02175 TeGeomRep rep = TeGeomRep(atoi(portal->getData("geom_type")));
02176 if (rep == TeRASTER || rep == TeRASTERFILE)
02177 {
02178 TeOCIOraclePortal* portal2 = (TeOCIOraclePortal*)this->getPortal();
02179 sql = "SELECT lut_table, raster_table FROM " + geomTable;
02180 string tabName;
02181 if (!portal2->query (sql))
02182 {
02183 delete portal2;
02184 continue;
02185 }
02186
02187 while (portal2->fetchRow())
02188 {
02189
02190 tabName = portal2->getData(0);
02191 if (!tabName.empty() && this->tableExist(tabName))
02192 {
02193 sql = "DROP TABLE " + tabName;
02194 this->execute(sql);
02195
02196 sql= "DROP SEQUENCE "+ getNameSequence(tabName);
02197 this->execute(sql);
02198 }
02199
02200 tabName = portal2->getData(1);
02201 if (!tabName.empty() && this->tableExist(tabName))
02202 {
02203 sql = "DROP TABLE " + tabName;
02204 this->execute(sql);
02205
02206 sql= "DROP SEQUENCE "+ getNameSequence(tabName);
02207 this->execute(sql);
02208 }
02209 }
02210 delete portal2;
02211
02212 tabName = geomTable + "_metadata";
02213 if (!tabName.empty() && this->tableExist(tabName))
02214 {
02215 sql = "DROP TABLE " + tabName;
02216 this->execute(sql);
02217
02218 sql= "DROP SEQUENCE "+ getNameSequence(tabName);
02219 this->execute(sql);
02220
02221 }
02222
02223 }
02224 if (this->tableExist(geomTable))
02225 {
02226 sql = "DROP TABLE " + geomTable;
02227 if(tableExist(geomTable))
02228 {
02229 if (!this->execute(sql) )
02230 {
02231 delete portal;
02232 return false;
02233 }
02234 }
02235
02236 sql= "DROP SEQUENCE "+ getNameSequence(geomTable);
02237 this->execute(sql);
02238 }
02239 }
02240
02241 portal->freeResult();
02242 if (existRelation("te_representation","fk_rep_layer_id") != TeRICascadeDeletion)
02243 {
02244
02245 sql = "DELETE FROM te_representation WHERE layer_id = " +Te2String(layerId);
02246 if (!this->execute(sql) )
02247 {
02248 delete portal;
02249 return false;
02250 }
02251 }
02252
02253
02254 sql = "SELECT theme_id FROM te_theme WHERE layer_id=" + Te2String(layerId);
02255 if (!portal->query (sql))
02256 {
02257 delete portal;
02258 return false;
02259 }
02260
02261 int themeId;
02262 while (portal->fetchRow())
02263 {
02264 themeId = atoi(portal->getData("theme_id"));
02265 this->deleteTheme(themeId);
02266 }
02267
02268 sql = "DELETE FROM te_layer WHERE layer_id=" + Te2String(layerId);
02269 if (!this->execute(sql))
02270 {
02271 delete portal;
02272 return false;
02273 }
02274
02275
02276 sql = "DELETE FROM te_projection WHERE projection_id = "+ projId;
02277 if (!this->execute(sql))
02278 {
02279 delete portal;
02280 return false;
02281 }
02282
02283
02284 TeThemeMap::iterator it;
02285 for (it = themeMap().begin(); it != themeMap().end(); ++it)
02286 {
02287 TeTheme *theme = (TeTheme*)it->second;
02288 if (theme->layer()->id() == layerId)
02289 {
02290 themeMap().erase(theme->id());
02291 delete theme;
02292 }
02293 }
02294
02295
02296 TeLayer* layer = layerMap()[layerId];
02297 layerMap().erase(layerId);
02298 delete layer;
02299
02300 delete portal;
02301 return true;
02302 }
02303
02304 bool
02305 TeOCIOracle::insertTheme (TeAbstractTheme *theme)
02306 {
02307 double maxScale = theme->maxScale ();
02308 if(maxScale==TeMAXFLOAT)
02309 maxScale = 0.;
02310
02311 string ins = "INSERT INTO te_theme (";
02312 ins += " theme_id, ";
02313 ins += " layer_id, view_id, name, ";
02314 ins += " parent_id, priority, node_type, min_scale, max_scale, ";
02315 ins += " generate_attribute_where, generate_spatial_where, generate_temporal_where, ";
02316 ins += " collection_table, visible_rep, enable_visibility, lower_x, lower_y, upper_x, upper_y, creation_time) ";
02317 ins += " VALUES (";
02318 ins += "te_theme_seq.NEXTVAL";
02319
02320 if(theme->type()==TeTHEME)
02321 ins += ", " + Te2String(static_cast<TeTheme*>(theme)->layerId());
02322 else
02323 ins += ", NULL ";
02324
02325 ins += ", " + Te2String(theme->view());
02326 ins += ", '" + escapeSequence(theme->name()) + "'";
02327 ins += ", " + Te2String(theme->parentId ());
02328 ins += ", " + Te2String(theme->priority());
02329 ins += ", " + Te2String(theme->type ());
02330 ins += ", " + Te2String (theme->minScale(),15);
02331 ins += ", " + Te2String (maxScale,15);
02332 ins += ", '" + escapeSequence(theme->attributeRest()) + "'";
02333 ins += ", '" + escapeSequence(theme->spatialRest()) + "'";
02334 ins += ", '" + escapeSequence(theme->temporalRest()) + "'";
02335
02336 if(theme->type()==TeTHEME)
02337 ins += ", '" + escapeSequence(static_cast<TeTheme*>(theme)->collectionTable()) + "'";
02338 else
02339 ins += ", NULL ";
02340
02341 ins += ", " + Te2String(theme->visibleRep());
02342 ins += ", " + Te2String(theme->visibility());
02343 ins += ", " + Te2String (theme->box().x1(),15);
02344 ins += ", " + Te2String (theme->box().y1(),15);
02345 ins += ", " + Te2String (theme->box().x2(),15);
02346 ins += ", " + Te2String (theme->box().y2(),15);
02347 TeTime creationTime = theme->getCreationTime();
02348 ins += ", " + getSQLTime(creationTime);
02349 ins += ")";
02350
02351 if(!execute(ins))
02352 {
02353 errorMessage_ = "Error inserting in the table te_theme!";
02354 return false;
02355 }
02356
02357 TeOCIOraclePortal *ocip = (TeOCIOraclePortal*)getPortal();
02358 if(!ocip)
02359 return false;
02360
02361 string seq = "SELECT te_theme_seq.CURRVAL FROM DUAL";
02362 if(!ocip->query(seq))
02363 {
02364 errorMessage_ = "Error in the sequence te_theme_seq!";
02365 delete ocip;
02366 return false;
02367 }
02368
02369 if(!ocip->fetchRow())
02370 {
02371 errorMessage_ = "Sequence value not found!";
02372 delete ocip;
02373 return false;
02374 }
02375
02376 int index = atoi((const char*)ocip->getData(0));
02377 theme->id(index);
02378 delete ocip;
02379
02380
02381 if((theme->type() == TeTHEME || theme->type()==TeEXTERNALTHEME)&& static_cast<TeTheme*>(theme)->collectionTable().empty())
02382 {
02383 string colName = "te_collection_" + Te2String(index);
02384 static_cast<TeTheme*>(theme)->collectionTable(colName);
02385
02386 ins = "UPDATE te_theme SET ";
02387 ins += " collection_table = '" + escapeSequence(colName) + "'";
02388 ins += " WHERE theme_id = " + Te2String (index);
02389
02390 if(!execute(ins))
02391 {
02392 errorMessage_ = "Error inserting in the table te_theme!";
02393 return false;
02394 }
02395 }
02396
02397 if(theme->parentId() == 0)
02398 {
02399 std::string sql = "UPDATE te_theme SET";
02400 sql += " parent_id = " + Te2String(theme->id());
02401 sql += " WHERE theme_id = ";
02402 sql += Te2String(theme->id());
02403
02404 theme->parentId(theme->id());
02405
02406 if(!this->execute(sql))
02407 return false;
02408 }
02409
02410 bool status;
02411
02412
02413 int numSlices = 0;
02414 if(theme->grouping().groupMode_ != TeNoGrouping)
02415 {
02416 if(!insertGrouping (theme->id(), theme->grouping()))
02417 return false;
02418 numSlices = theme->grouping().groupNumSlices_;
02419 }
02420
02421
02422 theme->outOfCollectionLegend().group(-1);
02423 theme->outOfCollectionLegend().theme(theme->id());
02424 status = insertLegend (&(theme->outOfCollectionLegend()));
02425 if (!status)
02426 return status;
02427
02428 theme->withoutDataConnectionLegend().group(-2);
02429 theme->withoutDataConnectionLegend().theme(theme->id());
02430 status = insertLegend (&(theme->withoutDataConnectionLegend()));
02431 if (!status)
02432 return status;
02433
02434 theme->defaultLegend().group(-3);
02435 theme->defaultLegend().theme(theme->id());
02436 status = insertLegend (&(theme->defaultLegend()));
02437 if (!status)
02438 return status;
02439
02440 theme->pointingLegend().group(-4);
02441 theme->pointingLegend().theme(theme->id());
02442 status = insertLegend (&(theme->pointingLegend()));
02443 if (!status)
02444 return status;
02445
02446 theme->queryLegend().group(-5);
02447 theme->queryLegend().theme(theme->id());
02448 status = insertLegend (&(theme->queryLegend()));
02449 if (!status)
02450 return status;
02451
02452 theme->queryAndPointingLegend().group(-6);
02453 theme->queryAndPointingLegend().theme(theme->id());
02454 status = insertLegend (&(theme->queryAndPointingLegend()));
02455 if (!status)
02456 return status;
02457
02458 for (int i = 0; i < numSlices; i++)
02459 {
02460 theme->legend()[i].group(i);
02461 theme->legend()[i].theme(theme->id());
02462 status = insertLegend (&(theme->legend()[i]));
02463 if (!status)
02464 return status;
02465 }
02466 if (!status)
02467 return status;
02468
02469
02470 if(!theme->saveMetadata(this))
02471 return false;
02472
02473 themeMap()[theme->id()] = theme;
02474
02475 if(theme->type()==TeTHEME && !updateThemeTable(static_cast<TeTheme*>(theme)))
02476 return false;
02477
02478 return true;
02479 }
02480
02481
02482 bool
02483 TeOCIOracle::insertThemeTable (int themeId, int tableId, int relationId, int tableOrder)
02484 {
02485 string ins = "INSERT INTO te_theme_table ( ";
02486 ins += " theme_table_id, ";
02487 ins += " theme_id, table_id, relation_id, table_order)";
02488 ins += " VALUES ( ";
02489 ins += getNameSequence("te_theme_table") +".NEXTVAL ";
02490 ins += ", "+ Te2String(themeId);
02491 ins += ", "+ Te2String(tableId);
02492
02493 if(relationId>0)
02494 ins += ", "+ Te2String(relationId);
02495 else
02496 ins += ", null ";
02497
02498 ins += ", "+ Te2String(tableOrder);
02499 ins += ")";
02500
02501 if(!execute(ins))
02502 {
02503 errorMessage_ = "Error inserting in the table te_theme_table!";
02504 return false;
02505 }
02506
02507 return true;
02508 }
02509
02510 bool
02511 TeOCIOracle::insertThemeGroup(TeViewTree* tree)
02512 {
02513 string ins = "INSERT INTO te_theme (theme_id, view_id, name, ";
02514 ins += " parent_id, priority, node_type ) VALUES (";
02515 ins += "te_theme_seq.NEXTVAL";
02516 ins += ", " + Te2String(tree->view());
02517 ins += ", '" + escapeSequence(tree->name()) + "'";
02518 ins += ", " + Te2String(tree->parentId ());
02519 ins += ", " + Te2String(tree->priority());
02520 ins += ", " + Te2String(1);
02521 ins += ")";
02522
02523 TeOCIOraclePortal *ocip = (TeOCIOraclePortal*)getPortal();
02524 if(!ocip)
02525 return false;
02526
02527 try
02528 {
02529 if(!execute(ins))
02530 {
02531 errorMessage_ = "Error inserting in the table te_theme!";
02532 delete ocip;
02533 return false;
02534 }
02535
02536 string seq = "SELECT te_theme_seq.CURRVAL FROM DUAL";
02537 if(!ocip->query(seq))
02538 {
02539 errorMessage_ = "Error in the sequence te_theme_seq!";
02540 delete ocip;
02541 return false;
02542 }
02543
02544 if(!ocip->fetchRow())
02545 {
02546 errorMessage_ = "Sequence value not found!";
02547 delete ocip;
02548 return false;
02549 }
02550
02551 int index = atoi((const char*)ocip->getData(0));
02552 tree->id(index);
02553
02554 }
02555 catch(...)
02556 {
02557 errorMessage_ = "Error inserting in the table te_theme!";
02558 if(ocip)
02559 delete ocip;
02560 return false;
02561 }
02562
02563 delete ocip;
02564 return true;
02565 }
02566
02567 bool
02568 TeOCIOracle::generateLabelPositions (TeTheme *theme, const std::string& objectId)
02569 {
02570 string geomTable, upd;
02571 string collTable = theme->collectionTable();
02572
02573 if((collTable.empty()) || (!tableExist(collTable)))
02574 return false;
02575
02576 if( theme->layer()->hasGeometry(TeCELLS) ||
02577 theme->layer()->hasGeometry(TePOLYGONS) ||
02578 theme->layer()->hasGeometry(TeLINES) )
02579 {
02580 geomTable = theme->layer()->tableName(TeCELLS);
02581
02582 if(geomTable.empty())
02583 {
02584 geomTable = theme->layer()->tableName(TePOLYGONS);
02585 if(geomTable.empty())
02586 geomTable = theme->layer()->tableName(TeLINES);
02587 }
02588
02589 upd= "UPDATE " + collTable + " SET ";
02590 upd += " label_x = (SELECT MAX(lower_x + (upper_x - lower_x)/2) ";
02591 upd += "FROM " + geomTable + " WHERE object_id = c_object_id), ";
02592
02593 upd += " label_y = (SELECT MAX(lower_y + (upper_y - lower_y)/2) ";
02594 upd += "FROM " + geomTable + " WHERE object_id = c_object_id) ";
02595 upd += " WHERE label_x IS NULL OR label_y IS NULL";
02596 }
02597 else if(theme->layer()->hasGeometry(TePOINTS))
02598 {
02599 geomTable = theme->layer()->tableName(TePOINTS);
02600
02601 upd= " UPDATE " + collTable + " SET ";
02602 upd += " label_x = (SELECT MAX(x) ";
02603 upd += " FROM " + geomTable + " p WHERE object_id = c_object_id), ";
02604
02605 upd += " label_y = (SELECT MAX(y) ";
02606 upd += " FROM " + geomTable + " p WHERE object_id = c_object_id) ";
02607 upd += " WHERE label_x IS NULL OR label_y IS NULL";
02608 }
02609
02610 if (!upd.empty())
02611 {
02612 if (!objectId.empty())
02613 {
02614 upd += " AND c_object_id='"+objectId+"'";
02615 }
02616 if(!execute(upd))
02617 return false;
02618 }
02619
02620 return true;
02621 }
02622
02623 bool
02624 TeOCIOracle::insertView(TeView *view)
02625 {
02626
02627 TeProjection* proj = view->projection();
02628 if ( !proj || !insertProjection(proj))
02629 {
02630 errorMessage_ = "Não é possível inserir vista sem projeção";
02631 return false;
02632 }
02633
02634 string ins = "INSERT INTO te_view (view_id, projection_id, name, user_name, visibility, lower_x, lower_y, upper_x, upper_y, current_theme)";
02635 ins += " VALUES (";
02636 ins += "te_view_seq.NEXTVAL";
02637 ins += ", " + Te2String(proj->id());
02638 ins += ", '" + escapeSequence(view->name ()) + "'";
02639 ins += ", '" + escapeSequence(view->user ()) + "'";
02640 ins += ", " + Te2String((int)view->isVisible());
02641 ins += ", " + Te2String(view->getCurrentBox().lowerLeft().x(),15);
02642 ins += ", " + Te2String(view->getCurrentBox().lowerLeft().y(),15);
02643 ins += ", " + Te2String(view->getCurrentBox().upperRight().x(),15);
02644 ins += ", " + Te2String(view->getCurrentBox().upperRight().y(),15);
02645 if(view->getCurrentTheme() == -1)
02646 ins += ", null";
02647 else
02648 ins += ", " + Te2String(view->getCurrentTheme());
02649 ins += " )";
02650
02651 if(!execute (ins))
02652 {
02653 if(errorMessage_.empty())
02654 errorMessage_ = "Error inserting in the table te_view!";
02655 return false;
02656 }
02657
02658 TeOCIOraclePortal *ocip = (TeOCIOraclePortal*)getPortal();
02659 if(!ocip)
02660 return false;
02661
02662 ins = "SELECT te_view_seq.CURRVAL FROM DUAL";
02663 if (!ocip->query(ins))
02664 {
02665 if(errorMessage_.empty())
02666 errorMessage_ = "Error in the sequence te_view_seq!";
02667 delete ocip;
02668 return false;
02669 }
02670
02671 if(!ocip->fetchRow())
02672 {
02673 errorMessage_ = "Sequence value not found!";
02674 delete ocip;
02675 return false;
02676 }
02677
02678 int index = atoi((const char*)ocip->getData(0));
02679 view->id(index);
02680 delete ocip;
02681
02682 int size = view->size();
02683
02684 for (int th=0; th<size; th++)
02685 {
02686 TeViewNode* node = view->get(th);
02687 if (node->type() == TeTHEME)
02688 {
02689 TeTheme *theme = (TeTheme*) node;
02690 insertTheme (theme);
02691 }
02692 else
02693 {
02694 TeViewTree* tree = (TeViewTree*)node;
02695 insertViewTree (tree);
02696 }
02697 }
02698
02699
02700 viewMap()[view->id()] = view;
02701 return true;
02702 }
02703
02704 bool
02705 TeOCIOracle::insertViewTree (TeViewTree *tree)
02706 {
02707
02708 string ins = "INSERT INTO te_theme (theme_id, view_id, name, ";
02709 ins += " parent_id, node_type, priority) VALUES (";
02710 ins += " te_theme_seq.NEXTVAL";
02711 ins += ", " + Te2String(tree->view());
02712 ins += ", '" + escapeSequence(tree->name()) + "'";
02713 ins += ", " + Te2String(tree->parentId());
02714 ins += ", " + Te2String(tree->type());
02715 ins += ", " + Te2String(tree->priority());
02716 ins += ")";
02717
02718 if(!execute(ins))
02719 {
02720 if(errorMessage_.empty())
02721 errorMessage_ = "Error inserting in the table te_theme!";
02722 return false;
02723 }
02724
02725 TeOCIOraclePortal *ocip = (TeOCIOraclePortal*)getPortal();
02726 if(!ocip)
02727 return false;
02728
02729 string seq = "SELECT te_theme_seq.CURRVAL FROM DUAL";
02730 if(!ocip->query(seq))
02731 {
02732 errorMessage_ = "Error in the sequence te_theme_seq!";
02733 delete ocip;
02734 return false;
02735 }
02736
02737 if(!ocip->fetchRow())
02738 {
02739 errorMessage_ = "Sequence value not found!";
02740 delete ocip;
02741 return false;
02742 }
02743
02744 int index = atoi((const char*)ocip->getData(0));
02745 tree->id(index);
02746 delete ocip;
02747
02748 return true;
02749 }
02750
02751 string TeOCIOracle::concatValues(vector<string>& values, const string& unionString)
02752 {
02753 string concat = "";
02754
02755 for(unsigned int i = 0; i < values.size(); ++i)
02756 {
02757 if(i != 0)
02758 {
02759 concat += " || ";
02760
02761 if(!unionString.empty())
02762 {
02763 concat += "'";
02764 concat += unionString;
02765 concat += "'";
02766 concat += " || ";
02767 }
02768 }
02769
02770 concat += values[i];
02771 }
02772
02773 return concat;
02774 }
02775
02776 bool TeOCIOracle::getAttributeList(const string& tableName,TeAttributeList& attList)
02777 {
02778 attList.clear();
02779
02780 std::string owner = this->user();
02781 std::string plainTableName = tableName;
02782 std::set<std::string> setPrimaryKeys;
02783
02784 std::vector<std::string> vecTableName;
02785 TeSplitString(tableName, ".", vecTableName);
02786
02787 if(vecTableName.size() == 2)
02788 {
02789 owner = vecTableName[0];
02790 plainTableName = vecTableName[1];
02791 }
02792
02793 std::string sqlKey = "SELECT c.column_name as col_primary from ALL_CONS_COLUMNS c";
02794 sqlKey += " JOIN ALL_CONSTRAINTS ac ON c.constraint_name = ac.constraint_name";
02795 sqlKey += " WHERE upper(c.table_name) = upper('" + plainTableName + "')";
02796 sqlKey += " AND upper(c.OWNER) = upper('"+ owner + "') and constraint_type = 'P'";
02797
02798 std::string sqlColumns = "SELECT COLUMN_NAME, DATA_TYPE, DATA_LENGTH, DATA_SCALE";
02799 sqlColumns += " FROM ALL_TAB_COLUMNS";
02800 sqlColumns += " WHERE upper(TABLE_NAME) = upper('" + plainTableName + "')";
02801 sqlColumns += " AND upper(OWNER) = upper('"+ owner + "')";
02802
02803 TeDatabasePortal* portal = this->getPortal();
02804 if(!portal)
02805 return false;
02806
02807 if(!portal->query(sqlKey))
02808 {
02809 delete portal;
02810 return false;
02811 }
02812
02813 while(portal->fetchRow())
02814 {
02815 setPrimaryKeys.insert(portal->getData(0));
02816 }
02817
02818 portal->freeResult();
02819
02820 if(!portal->query(sqlColumns))
02821 {
02822 delete portal;
02823 return false;
02824 }
02825
02826 while(portal->fetchRow())
02827 {
02828 TeAttribute at;
02829 at.rep_.name_ = portal->getData(0);
02830 string type = TeConvertToUpperCase(portal->getData(1));
02831 int scale = portal->getInt(3);
02832 if((type=="NUMBER" && scale>0) || (type=="FLOAT"))
02833 at.rep_.type_ = TeREAL;
02834 else if(type=="NUMBER")
02835 at.rep_.type_ = TeINT;
02836 else if(type=="VARCHAR2")
02837 {
02838 at.rep_.type_ = TeSTRING;
02839 at.rep_.numChar_=portal->getInt(2);
02840 }
02841 else if(type=="BLOB")
02842 at.rep_.type_ = TeBLOB;
02843 else if(type=="CHAR")
02844 at.rep_.type_ = TeCHARACTER;
02845 else if(type=="DATE")
02846 at.rep_.type_ = TeDATETIME;
02847 else
02848 {
02849 at.rep_.type_ = TeSTRING;
02850 at.rep_.numChar_=portal->getInt(2);
02851 }
02852
02853 if(setPrimaryKeys.find(at.rep_.name_) != setPrimaryKeys.end())
02854 {
02855 at.rep_.isPrimaryKey_ = true;
02856 }
02857
02858 attList.push_back(at);
02859 }
02860 delete portal;
02861 return true;
02862 }
02863
02864 string TeOCIOracle::toUpper(const string& value)
02865 {
02866 string result = "upper(";
02867 result += value;
02868 result += ")";
02869
02870 return result;
02871 }
02872
02873 string
02874 TeOCIOracle::getSQLTime(const TeTime& time) const
02875 {
02876 string dateTime = time.getDateTime();
02877 string result;
02878 result = "TO_DATE('";
02879 result += dateTime;
02880 result += " ";
02881 result += "','DD/MM/YYYY HH24:MI:SS')";
02882 return result;
02883 }
02884
02885
02886 bool
02887 TeOCIOracle::getIndexesFromTable(const string& tableName, std::vector<TeDatabaseIndex>& vecIndexes)
02888 {
02889 std::vector<TeDatabaseIndex> indexes;
02890
02891 string query = "select index_name, column_name from ALL_IND_COLUMNS where table_name = '"+tableName+"' ";
02892 query += "and index_name not like ( select constraint_name from ALL_CONSTRAINTS ";
02893 query += "where table_name = '"+tableName+"' and constraint_type = 'P') order by index_name";
02894
02895 TeDatabasePortal* portal = getPortal();
02896
02897 std::vector<std::string> names;
02898 std::vector<std::string> cols;
02899
02900 std::set<std::string> idxNames;
02901
02902 std::multimap<std::string, std::string> idxAttrs;
02903
02904 if(portal->query(query))
02905 {
02906 while(portal->fetchRow())
02907 {
02908 std::string idxName = portal->getData("index_name");
02909 std::string attrName = portal->getData("column_name");
02910
02911 idxAttrs.insert(std::pair<std::string, std::string>(idxName, attrName));
02912
02913 idxNames.insert(idxName);
02914 }
02915
02916 }else
02917 {
02918 delete portal;
02919 return false;
02920 }
02921
02922 portal->freeResult();
02923
02924 delete portal;
02925 portal = NULL;
02926
02927 std::set<std::string>::iterator it = idxNames.begin();
02928 while(it != idxNames.end())
02929 {
02930 std::string idxName = *it;
02931
02932 TeDatabaseIndex idxx;
02933 idxx.setIndexName(idxName);
02934 idxx.setIsPrimaryKey(false);
02935
02936 std::vector<string> attNames;
02937 std::pair<std::multimap<std::string, std::string>::iterator, std::multimap<std::string, std::string>::iterator> itAttrs = idxAttrs.equal_range(idxName);
02938 std::multimap<std::string, std::string>::iterator itA;
02939 for (itA=itAttrs.first; itA!=itAttrs.second; ++itA)
02940 {
02941 std::string attrName = itA->second;
02942 attNames.push_back(attrName);
02943 }
02944
02945 idxx.setColumns(attNames);
02946
02947 indexes.push_back(idxx);
02948
02949 ++it;
02950 }
02951
02952 vecIndexes = indexes;
02953
02954 return true;
02955 }
02956
02957
02958
02959
02960 TeOCIOraclePortal::TeOCIOraclePortal(TeOCIOracle *pDatabase)
02961 {
02962 cursor_ = new TeOCICursor(pDatabase->connection_);
02963 db_ = pDatabase;
02964 numRows_ = 0;
02965 numFields_ = 0;
02966 curRow_ = 0;
02967 }
02968
02969
02970 TeOCIOraclePortal::~TeOCIOraclePortal ()
02971 {
02972 if(cursor_)
02973 delete cursor_;
02974 cursor_ = NULL;
02975 }
02976
02977
02978 bool TeOCIOraclePortal::isConnected()
02979 {
02980 return (cursor_->conn()->isConnected());
02981 }
02982
02983
02984 bool TeOCIOraclePortal::isEOF()
02985 {
02986 if (!isConnected())
02987 return true;
02988
02989 return false;
02990 }
02991
02992 bool
02993 TeOCIOraclePortal::getLinearRing (TeLine2D& line)
02994 {
02995
02996 double *points = NULL;
02997 try
02998 {
02999 int index = atoi (getData ("geom_id"));
03000 TeBox b (getDouble("lower_x"),getDouble("lower_y"),getDouble("upper_x"),getDouble("upper_y"));
03001 line.objectId (string (getData ("object_id")));
03002 line.geomId (index);
03003 line.setBox (b);
03004
03005 unsigned int ncoords = getInt("num_coords");
03006 unsigned int nords = ncoords*2;
03007
03008 if (!cursor_->readBlob(&points))
03009 return false;
03010
03011 line.clear();
03012
03013 for (unsigned int i=0;i<nords;++i)
03014 {
03015 double x = points[i];
03016 ++i;
03017 double y = points[i];
03018
03019 TeCoord2D c = TeCoord2D(x,y);
03020 line.add(c);
03021 }
03022 }
03023 catch(...)
03024 {
03025 errorMessage_ = "Error getting media!";
03026 return false;
03027 }
03028 return true;
03029 }
03030
03031 bool
03032 TeOCIOraclePortal::getLinearRing (TeLine2D& line, const unsigned int& initIndex)
03033 {
03034 double *points = NULL;
03035 try
03036 {
03037 line.objectId (string (getData (initIndex+1)));
03038 line.geomId (atoi (getData(initIndex)));
03039
03040 unsigned int ncoords = getInt(initIndex+2);
03041 unsigned int nords = ncoords*2;
03042
03043 if (!cursor_->readBlob(&points))
03044 return false;
03045
03046 line.clear();
03047
03048 for (unsigned int i=0;i<nords;++i)
03049 {
03050 double x = points[i];
03051 ++i;
03052 double y = points[i];
03053
03054 TeCoord2D c = TeCoord2D(x,y);
03055 line.add(c);
03056 }
03057 }
03058 catch(...)
03059 {
03060 errorMessage_ = "Error getting media!";
03061 return false;
03062 }
03063 return true;
03064 }
03065
03066
03067 bool
03068 TeOCIOraclePortal::moveFirst()
03069 {
03070 if (cursor_->moveFirst())
03071 return true;
03072 else
03073 return false;
03074 }
03075
03076
03077 bool
03078 TeOCIOraclePortal::moveNext()
03079 {
03080 if(cursor_->moveNext())
03081 return true;
03082 return false;
03083 }
03084
03085 bool TeOCIOraclePortal::query (const string &q,TeCursorLocation , TeCursorType , TeCursorEditType , TeCursorDataType )
03086 {
03087 errorMessage_.clear ();
03088
03089 if (!cursor_->isOpen())
03090 {
03091 if(!cursor_->open())
03092 {
03093 numRows_ = 0;
03094 return false;
03095 }
03096 }
03097
03098 if (!cursor_->query(q))
03099 {
03100 this->errorMessage_ = cursor_->getErrorMessage();
03101 numRows_ = 0;
03102 return false;
03103 }
03104
03105 numFields_= this->cursor_->numCol();
03106
03107 attList_.clear ();
03108 int i;
03109 for(i = 1; i <= numFields_ ; i++)
03110 {
03111 TeAttribute attribute;
03112
03113 switch (cursor_->colType(i))
03114 {
03115 case 3:
03116 attribute.rep_.type_ = TeINT;
03117 break;
03118
03119 case 2:
03120 case 4:
03121 attribute.rep_.type_ = TeREAL;
03122 break;
03123
03124 case 12:
03125 attribute.rep_.type_ = TeDATETIME;
03126 attribute.dateChronon_ = TeSECOND;
03127 attribute.dateTimeFormat_ = "DD/MM/YYYY HH24:MI:SS";
03128 break;
03129
03130 case 113:
03131 attribute.rep_.type_ = TeBLOB;
03132 break;
03133
03134 case 96:
03135 case 9:
03136 case 1:
03137 attribute.rep_.type_ = TeSTRING;
03138 break;
03139
03140 case 108:
03141 attribute.rep_.type_ = TeOBJECT;
03142 break;
03143 default :
03144 attribute.rep_.type_ = TeUNKNOWN;
03145 break;
03146 }
03147
03148
03149 attribute.rep_.name_ = cursor_->colName(i);
03150 attribute.rep_.numChar_ = cursor_->colSize(i);
03151 attList_.push_back ( attribute );
03152 }
03153 curRow_=-1;
03154 return true;
03155 }
03156
03157
03158 void TeOCIOraclePortal::freeResult ()
03159 {
03160 cursor_->freeResult();
03161 }
03162
03163
03164 bool TeOCIOraclePortal::fetchRow ()
03165 {
03166 try
03167 {
03168 if( !isConnected() )
03169 return false;
03170 if( numFields_ == 0)
03171 return false;
03172
03173 if (curRow_ == -1)
03174 {
03175 if(moveFirst())
03176 curRow_++;
03177 else
03178 return false;
03179 }
03180 else
03181
03182 if (moveNext())
03183 {
03184 curRow_++;
03185 return true;
03186 }
03187 else
03188 return false;
03189 }
03190
03191 catch(...)
03192 {
03193 return false;
03194 }
03195
03196 return true;
03197 }
03198
03199 bool TeOCIOraclePortal ::fetchRow (int i)
03200 {
03201 try
03202 {
03203 if( !isConnected() || (numFields_ == 0))
03204 return false;
03205
03206 if (curRow_ == -1)
03207 {
03208 if(moveFirst())
03209 curRow_++;
03210 else
03211 return false;
03212 }
03213
03214 if(curRow_ == i)
03215 return true;
03216
03217 if (cursor_->moveTo(i))
03218 {
03219 curRow_ = cursor_->currentRow();
03220 return true;
03221 }
03222 return false;
03223 }
03224 catch(...)
03225 {
03226 return false;
03227 }
03228 }
03229
03230 char* TeOCIOraclePortal::getData (int i)
03231 {
03232 char* result;
03233 if (i > numFields_ || i < 0)
03234 return "";
03235
03236 try
03237 {
03238 result = cursor_->getFieldValue(i+1);
03239 }
03240 catch(...)
03241 {
03242 errorMessage_ = "Error!";
03243 return "";
03244 }
03245 return result;
03246
03247 }
03248
03249 char* TeOCIOraclePortal::getData (const string &s)
03250 {
03251 char* result;
03252
03253 string fieldName;
03254 size_t pos = s.find(".", 0, 1);
03255 if (pos != string::npos)
03256 fieldName = s.substr(pos+1);
03257 else
03258 fieldName = s;
03259
03260 int index = getColumnIndex (fieldName);
03261 if(index == -1)
03262 return "";
03263 else
03264 result = getData(index);
03265
03266 return result;
03267 }
03268
03269
03270 int TeOCIOraclePortal::getInt (int i)
03271 {
03272 int value = atoi(getData(i));
03273 return value;
03274 }
03275
03276 int
03277 TeOCIOraclePortal::getInt (const string& s)
03278 {
03279 int value = atoi(getData(s));
03280 return value;
03281 }
03282
03283 double TeOCIOraclePortal ::getDouble (int i)
03284 {
03285 string strValue=getData(i);
03286 size_t find;
03287 find = strValue.find(",");
03288 if(find<strValue.size())
03289 strValue.replace(find, 1, ".");
03290
03291 return atof(strValue.c_str());
03292 }
03293
03294 double
03295 TeOCIOraclePortal::getDouble (const string& s)
03296 {
03297 string strValue=getData(s);
03298 size_t find;
03299 find = strValue.find(",");
03300 if(find<strValue.size())
03301 strValue.replace(find, 1, ".");
03302
03303 return atof(strValue.c_str());
03304 }
03305
03306
03307 bool
03308 TeOCIOraclePortal::getBool(int i)
03309 {
03310 char* fieldChar=0;
03311
03312 fieldChar = cursor_->getFieldValue(i+1);
03313 if(fieldChar == 0)
03314 return false;
03315
03316 return true;
03317 }
03318
03319
03320 bool
03321 TeOCIOraclePortal::getBool (const string& s)
03322 {
03323 string fieldName;
03324 size_t pos = s.find(".", 0, 1);
03325 if (pos != string::npos)
03326 fieldName = s.substr(pos+1);
03327 else
03328 fieldName = s;
03329
03330 int index = getColumnIndex (fieldName);
03331 if(index == -1)
03332 return false;
03333 return (getBool(index));
03334 }
03335
03336 bool
03337 TeOCIOraclePortal::getBlob(const string& s, unsigned char* &data, long& size)
03338 {
03339 try
03340 {
03341 unsigned int len = cursor_->getBlobSize(s);
03342 data = new unsigned char[len];
03343 if(!cursor_->readBlob(data, len, s))
03344 return false;
03345 size = len;
03346 }
03347 catch(...)
03348 {
03349 errorMessage_ = "Error getting media!";
03350 return false;
03351 }
03352 return false;
03353 }
03354
03355 TeTime
03356 TeOCIOraclePortal::getDate (int i)
03357 {
03358 TeTime temp;
03359 string result;
03360
03361 if (i > numFields_ || i < 0)
03362 return temp;
03363
03364 try
03365 {
03366 result = cursor_->getFieldValue(i+1);
03367 }
03368 catch(...)
03369 {
03370 errorMessage_ = "Error!";
03371 return temp;
03372 }
03373
03374 TeTime t(result, TeSECOND, "DDsMMsYYYYsHHsmmsSS");
03375 return t;
03376 }
03377
03378
03379 TeTime
03380 TeOCIOraclePortal::getDate (const string& s)
03381 {
03382 string result;
03383 TeTime temp;
03384
03385 string fieldName;
03386 size_t pos = s.find(".", 0, 1);
03387 if (pos != string::npos)
03388 fieldName = s.substr(pos+1);
03389 else
03390 fieldName = s;
03391
03392 int index = getColumnIndex (fieldName);
03393 if(index == -1)
03394 return temp;
03395 else
03396 result = getData(index);
03397
03398 TeTime t(result, TeSECOND, "DDsMMsYYYYsHHsmmsSS") ;
03399 return t;
03400 }
03401
03402 string
03403 TeOCIOraclePortal::getDateAsString(int i)
03404 {
03405 TeTime t = this->getDate(i);
03406 string date = t.getDateTime ();
03407
03408 if (!date.empty())
03409 { string tval = " TO_DATE ('"+ date +"','DDsMMsYYYYsHHsmmsSS') ";
03410 return tval;
03411 }
03412 else
03413 return "";
03414 }
03415
03416 string
03417 TeOCIOraclePortal::getDateAsString(const string& s)
03418 {
03419 TeTime t = this->getDate(s);
03420 string date = t.getDateTime ();
03421
03422 if (!date.empty())
03423 { string tval = " TO_DATE ('"+ date +"','DDsMMsYYYYsHHsmmsSS') ";
03424 return tval;
03425 }
03426 else
03427 return "";
03428 }
03429
03430
03431 bool
03432 TeOCIOraclePortal::fetchGeometry (TePolygon& poly)
03433 {
03434 try
03435 {
03436 TeLine2D line;
03437 int numHoles = atoi(this->getData("num_holes"));
03438 bool flag = this->fetchGeometry(line);
03439
03440 TeLinearRing ring(line);
03441 poly.objectId ( ring.objectId());
03442 poly.geomId ( ring.geomId() );
03443 poly.add ( ring );
03444 int parentId = poly.geomId();
03445
03446 while (flag && numHoles>0 && (atoi(this->getData("parent_id")) == parentId))
03447 {
03448 TeLine2D aux;
03449 this->getLinearRing(aux);
03450 TeLinearRing hole(aux);
03451 poly.add (hole);
03452 flag = this->fetchRow();
03453 }
03454 return flag;
03455 }
03456 catch(...)
03457 {
03458 errorMessage_ = "Error fetchGeometry!";
03459 return false;
03460 }
03461 }
03462
03463 bool
03464 TeOCIOraclePortal::fetchGeometry (TePolygon& poly, const unsigned int& initIndex)
03465 {
03466 try
03467 {
03468 TeLine2D line;
03469 int numHoles = atoi(this->getData(initIndex+3));
03470 getLinearRing(line, initIndex);
03471 bool flag = this->fetchRow();
03472
03473 TeLinearRing ring(line);
03474 poly.objectId ( ring.objectId());
03475 poly.geomId ( ring.geomId() );
03476 poly.add ( ring );
03477 int parentId = poly.geomId();
03478
03479 while (flag && numHoles>0 && (atoi(this->getData(initIndex+4)) == parentId))
03480 {
03481 TeLine2D aux;
03482 this->getLinearRing(aux, initIndex);
03483 TeLinearRing hole(aux);
03484 poly.add (hole);
03485 flag = this->fetchRow();
03486 }
03487 return flag;
03488 }
03489 catch(...)
03490 {
03491 errorMessage_ = "Error fetchGeometry!";
03492 return false;
03493 }
03494 }
03495
03496 bool
03497 TeOCIOraclePortal::fetchGeometry (TeLine2D& line)
03498 {
03499 try
03500 {
03501 getLinearRing(line);
03502 return (this->fetchRow());
03503 }
03504 catch(...)
03505 {
03506 errorMessage_ = cursor_->getErrorMessage();
03507 return false;
03508 }
03509 }
03510
03511 bool
03512 TeOCIOraclePortal::fetchGeometry (TeLine2D& line, const unsigned int& initIndex)
03513 {
03514 try
03515 {
03516 getLinearRing(line, initIndex);
03517 return (this->fetchRow());
03518 }
03519 catch(...)
03520 {
03521 errorMessage_ = cursor_->getErrorMessage();
03522 return false;
03523 }
03524 }
03525
03526
03527 bool
03528 TeOCIOraclePortal::fetchGeometry(TePoint& p)
03529 {
03530
03531 try
03532 {
03533 p.geomId( atoi(getData("geom_id")));
03534 p.objectId( string(getData("object_id")));
03535 double x = getDouble("x");
03536 double y = getDouble("y");
03537 TeCoord2D c(x, y);
03538 p.add(c);
03539 return(this->fetchRow());
03540 }
03541 catch(...)
03542 {
03543 errorMessage_ = "Error fetchGeometry!";
03544 return false;
03545 }
03546 }
03547
03548 bool
03549 TeOCIOraclePortal::fetchGeometry(TePoint& p, const unsigned int& initIndex)
03550 {
03551 try
03552 {
03553 p.geomId( atoi(getData(initIndex)));
03554 p.objectId( string(getData(initIndex+1)));
03555 double x = getDouble(initIndex+2);
03556 double y = getDouble(initIndex+3);
03557 TeCoord2D c(x, y);
03558 p.add(c);
03559 return(this->fetchRow());
03560 }
03561 catch(...)
03562 {
03563 errorMessage_ = "Error fetchGeometry!";
03564 return false;
03565 }
03566 }
03567
03568 bool
03569 TeOCIOraclePortal::fetchGeometry(TeNode& n)
03570 {
03571 try
03572 {
03573 TeCoord2D c(getDouble("x"), getDouble("y"));
03574 n.geomId( atol(getData("geom_id")));
03575 n.objectId( string(getData("object_id")));
03576 n.add(c);
03577 return(this->fetchRow());
03578 }
03579 catch(...)
03580 {
03581 errorMessage_ = "Error fetchGeometry!";
03582 return false;
03583 }
03584 return true;
03585 }
03586
03587 bool
03588 TeOCIOraclePortal::fetchGeometry(TeNode& n, const unsigned int& initIndex)
03589 {
03590 try
03591 {
03592 TeCoord2D c(getDouble(initIndex+2), getDouble(initIndex+3));
03593 n.geomId( atol(getData(initIndex)));
03594 n.objectId( string(getData(initIndex+1)));
03595 n.add(c);
03596 return(this->fetchRow());
03597 }
03598 catch(...)
03599 {
03600 errorMessage_ = "Error fetchGeometry!";
03601 return false;
03602 }
03603 return true;
03604 }
03605
03606 bool
03607 TeOCIOraclePortal::getRasterBlock(unsigned long& size, unsigned char* ptData)
03608 {
03609
03610
03611 try
03612 {
03613 unsigned int len = 0;
03614 if(!getCursor()->readBlob(ptData, len, "spatial_data"))
03615 return false;
03616 size = len;
03617 }
03618 catch(...)
03619 {
03620 size = 0;
03621 errorMessage_ = "ERRO!";
03622 return false;
03623 }
03624 return true;
03625 }
03626
03627
03628
03629