00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <ecpgtype.h>
00023 #include <ecpglib.h>
00024 #include <ecpgerrno.h>
00025 #include <sqlca.h>
00026
00027 #line 1 "HCoreDatabasePGSQL.pgc"
00028 #include "cpsp/HCoreDatabasePGSQL.hh"
00029 #include <biu/assertbiu.hh>
00030 #include <biu/LatticeDescriptorCUB.hh>
00031 #include <biu/LatticeDescriptorFCC.hh>
00032
00033 namespace cpsp
00034 {
00035
00036 const std::string HCoreDatabasePGSQL::DEF_DBNAME = "bitest";
00037 const std::string HCoreDatabasePGSQL::DEF_USERNAME = "mmann";
00038 const std::string HCoreDatabasePGSQL::DEF_PASSWORD = "R00mO14";
00039
00040 HCoreDatabasePGSQL::HCoreDatabasePGSQL() :
00041 connected(false), cursorOpen(false)
00042 {
00043 }
00044
00045 HCoreDatabasePGSQL::HCoreDatabasePGSQL( const std::string& hostName,
00046 const std::string& dbName,
00047 const std::string& user,
00048 const std::string& password) :
00049 connected(false), cursorOpen(false)
00050 {
00051 connect(hostName, dbName, user, password);
00052 }
00053
00054 HCoreDatabasePGSQL::~HCoreDatabasePGSQL()
00055 {
00056 disconnect();
00057 }
00058
00059
00060 bool HCoreDatabasePGSQL::connect( const std::string& hostName,
00061 const std::string& dbName,
00062 const std::string& user,
00063 const std::string& password)
00064 {
00065 #ifndef NDEBUG
00066
00067
00068 #line 39 "HCoreDatabasePGSQL.pgc"
00069
00070 #endif
00071 if (connected)
00072 disconnect();
00073
00074
00075 std::string dbAtHost = dbName+"@"+hostName;
00076
00077
00078
00079
00080
00081 #line 47 "HCoreDatabasePGSQL.pgc"
00082 const char * dbAtHostName = dbAtHost . c_str () ;
00083
00084 #line 48 "HCoreDatabasePGSQL.pgc"
00085 const char * userName = user . c_str () ;
00086
00087 #line 49 "HCoreDatabasePGSQL.pgc"
00088 const char * passw = password . c_str () ;
00089
00090 #line 50 "HCoreDatabasePGSQL.pgc"
00091
00092
00093
00094 { ECPGconnect(__LINE__, 0, dbAtHostName , userName , passw , NULL, 0);
00095 #line 53 "HCoreDatabasePGSQL.pgc"
00096
00097 if (sqlca.sqlcode < 0) sqlprint();}
00098 #line 53 "HCoreDatabasePGSQL.pgc"
00099
00100
00101 connected = sqlca.sqlcode == ECPG_NO_ERROR;
00102
00103
00104 if (connected) {
00105 { ECPGsetcommit(__LINE__, "on", NULL);
00106 #line 59 "HCoreDatabasePGSQL.pgc"
00107
00108 if (sqlca.sqlcode < 0) sqlprint();}
00109 #line 59 "HCoreDatabasePGSQL.pgc"
00110
00111 }
00112 #ifndef NDEBUG
00113 assertbiu(sqlca.sqlcode != -402, "Connection to database '"+(std::string(dbAtHostName))+"' failed.");
00114 #endif
00115
00116 return connected;
00117 }
00118
00119 bool HCoreDatabasePGSQL::disconnect() {
00120 bool retVal = true;
00121 if (cursorOpen) {
00122 { ECPGdo(__LINE__, 0, 1, NULL, "close tableCursor", ECPGt_EOIT, ECPGt_EORT);
00123 #line 71 "HCoreDatabasePGSQL.pgc"
00124
00125 if (sqlca.sqlcode < 0) sqlprint();}
00126 #line 71 "HCoreDatabasePGSQL.pgc"
00127
00128 cursorOpen = false;
00129 }
00130 if (connected) {
00131 { ECPGdisconnect(__LINE__, "CURRENT");
00132 #line 75 "HCoreDatabasePGSQL.pgc"
00133
00134 if (sqlca.sqlcode < 0) sqlprint();}
00135 #line 75 "HCoreDatabasePGSQL.pgc"
00136
00137 retVal &= sqlca.sqlcode == ECPG_NO_ERROR;
00138 }
00139 return retVal;
00140 }
00141
00142 void HCoreDatabasePGSQL::closeCursor() {
00143 if (cursorOpen) {
00144 { ECPGdo(__LINE__, 0, 1, NULL, "close tableCursor", ECPGt_EOIT, ECPGt_EORT);
00145 #line 83 "HCoreDatabasePGSQL.pgc"
00146
00147 if (sqlca.sqlcode < 0) sqlprint();}
00148 #line 83 "HCoreDatabasePGSQL.pgc"
00149
00150 cursorOpen = false;
00151 { ECPGtrans(__LINE__, NULL, "commit");
00152 #line 85 "HCoreDatabasePGSQL.pgc"
00153
00154 if (sqlca.sqlcode < 0) sqlprint();}
00155 #line 85 "HCoreDatabasePGSQL.pgc"
00156
00157 }
00158 }
00159
00160
00161 bool HCoreDatabasePGSQL::initCoreAccess(const biu::LatticeDescriptor& latDescr, const unsigned int size,
00162 const unsigned int minContacts, const unsigned int maxContacts)
00163 {
00164 closeCursor();
00165 if (!connected) {
00166 return false;
00167 }
00168
00169
00170 this->coreSize = size;
00171 this->minHH = minContacts;
00172 this->maxHH = maxContacts;
00173
00174
00175
00176
00177
00178
00179 #line 105 "HCoreDatabasePGSQL.pgc"
00180 const char * s_latName = latDescr . getName () . c_str () ;
00181
00182 #line 106 "HCoreDatabasePGSQL.pgc"
00183 const int s_size = coreSize ;
00184
00185 #line 107 "HCoreDatabasePGSQL.pgc"
00186 const int s_minCont = minHH ;
00187
00188 #line 108 "HCoreDatabasePGSQL.pgc"
00189 const int s_maxCont = maxHH ;
00190
00191 #line 109 "HCoreDatabasePGSQL.pgc"
00192
00193
00194
00195 { ECPGtrans(__LINE__, NULL, "begin transaction ");
00196 #line 112 "HCoreDatabasePGSQL.pgc"
00197
00198 if (sqlca.sqlcode < 0) sqlprint();}
00199 #line 112 "HCoreDatabasePGSQL.pgc"
00200
00201
00202 #line 120 "HCoreDatabasePGSQL.pgc"
00203
00204
00205 { ECPGdo(__LINE__, 0, 1, NULL, "declare tableCursor cursor for select core_id , contacts , size from core_stat where size = ? and lat_id in ( select lat_id from lattice where name = ? ) and contacts >= ? and contacts <= ? order by contacts desc , core_id",
00206 ECPGt_int,&(s_size),(long)1,(long)1,sizeof(int),
00207 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
00208 ECPGt_char,&(s_latName),(long)0,(long)1,(1)*sizeof(char),
00209 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
00210 ECPGt_int,&(s_minCont),(long)1,(long)1,sizeof(int),
00211 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
00212 ECPGt_int,&(s_maxCont),(long)1,(long)1,sizeof(int),
00213 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);
00214 #line 122 "HCoreDatabasePGSQL.pgc"
00215
00216 if (sqlca.sqlcode < 0) sqlprint();}
00217 #line 122 "HCoreDatabasePGSQL.pgc"
00218
00219
00220 cursorOpen = sqlca.sqlcode == ECPG_NO_ERROR;
00221
00222 return cursorOpen;
00223 }
00224
00225 bool HCoreDatabasePGSQL::getNextCore(HCore& toFill) {
00226
00227
00228 toFill.reset(INT_MAX, INT_MAX);
00229 if (!connected || !cursorOpen)
00230 return false;
00231
00232
00233
00234
00235
00236
00237
00238
00239 #line 139 "HCoreDatabasePGSQL.pgc"
00240 int s_core_id ;
00241
00242 #line 140 "HCoreDatabasePGSQL.pgc"
00243 int s_contacts ;
00244
00245 #line 141 "HCoreDatabasePGSQL.pgc"
00246 int s_size ;
00247
00248 #line 142 "HCoreDatabasePGSQL.pgc"
00249
00250
00251
00252 { ECPGdo(__LINE__, 0, 1, NULL, "fetch next in tableCursor", ECPGt_EOIT,
00253 ECPGt_int,&(s_core_id),(long)1,(long)1,sizeof(int),
00254 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
00255 ECPGt_int,&(s_contacts),(long)1,(long)1,sizeof(int),
00256 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
00257 ECPGt_int,&(s_size),(long)1,(long)1,sizeof(int),
00258 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);
00259 #line 145 "HCoreDatabasePGSQL.pgc"
00260
00261 if (sqlca.sqlcode < 0) sqlprint();}
00262 #line 145 "HCoreDatabasePGSQL.pgc"
00263
00264
00265 if (sqlca.sqlcode != 0 || s_contacts < (int)minHH) {
00266 closeCursor();
00267 return false;
00268 }
00269
00270
00271 toFill.reset((unsigned int)s_contacts, (unsigned int)s_size);
00272
00273
00274
00275
00276
00277 #line 157 "HCoreDatabasePGSQL.pgc"
00278 int s_x , s_y , s_z ;
00279
00280 #line 158 "HCoreDatabasePGSQL.pgc"
00281
00282
00283
00284 #line 161 "HCoreDatabasePGSQL.pgc"
00285
00286 { ECPGdo(__LINE__, 0, 1, NULL, "declare pointCursor cursor for select x , y , z from core_point where core_id = ? ",
00287 ECPGt_int,&(s_core_id),(long)1,(long)1,sizeof(int),
00288 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);
00289 #line 162 "HCoreDatabasePGSQL.pgc"
00290
00291 if (sqlca.sqlcode < 0) sqlprint();}
00292 #line 162 "HCoreDatabasePGSQL.pgc"
00293
00294 if (sqlca.sqlcode == 0) {
00295 { ECPGdo(__LINE__, 0, 1, NULL, "fetch next in pointCursor", ECPGt_EOIT,
00296 ECPGt_int,&(s_x),(long)1,(long)1,sizeof(int),
00297 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
00298 ECPGt_int,&(s_y),(long)1,(long)1,sizeof(int),
00299 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
00300 ECPGt_int,&(s_z),(long)1,(long)1,sizeof(int),
00301 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);
00302 #line 164 "HCoreDatabasePGSQL.pgc"
00303
00304 if (sqlca.sqlcode < 0) sqlprint();}
00305 #line 164 "HCoreDatabasePGSQL.pgc"
00306
00307 while (sqlca.sqlcode == 0) {
00308 toFill.addPoint(HCore::CorePoint(s_x, s_y, s_z));
00309 { ECPGdo(__LINE__, 0, 1, NULL, "fetch next in pointCursor", ECPGt_EOIT,
00310 ECPGt_int,&(s_x),(long)1,(long)1,sizeof(int),
00311 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
00312 ECPGt_int,&(s_y),(long)1,(long)1,sizeof(int),
00313 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
00314 ECPGt_int,&(s_z),(long)1,(long)1,sizeof(int),
00315 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);
00316 #line 167 "HCoreDatabasePGSQL.pgc"
00317
00318 if (sqlca.sqlcode < 0) sqlprint();}
00319 #line 167 "HCoreDatabasePGSQL.pgc"
00320
00321 }
00322 { ECPGdo(__LINE__, 0, 1, NULL, "close pointCursor", ECPGt_EOIT, ECPGt_EORT);
00323 #line 169 "HCoreDatabasePGSQL.pgc"
00324
00325 if (sqlca.sqlcode < 0) sqlprint();}
00326 #line 169 "HCoreDatabasePGSQL.pgc"
00327
00328 }
00329
00330 return toFill.isValid();
00331 }
00332
00333
00334 }