00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <iostream>
00019 #include <sstream>
00020 #include <map>
00021
00022 #include "psfa/DbReadDriverMySql.hpp"
00023 #include "psfa/DbDriverMySqlHelper.hpp"
00024
00025
00026 psfa::DbReadDriverMySql::DbReadDriverMySql()
00027 {
00028 bool success = myHelper_.connect();
00029 if( !success ) {
00030 std::cout << "Es konnte keine Verbindung zur Datenbank hergestellt werden."
00031 << std::endl;
00032 exit(1);
00033 }
00034
00035 }
00036
00037
00038
00039 psfa::DbReadDriverMySql::~DbReadDriverMySql()
00040 {
00041 bool success = myHelper_.disconnect();
00042 if( !success ) {
00043 std::cerr << "Die Verbindung zur DB konnte nicht erfolgreich abgebaut werden."
00044 << std::endl;
00045 }
00046
00047 }
00048
00049
00050
00051 void
00052 psfa::DbReadDriverMySql::getPools( sVec& result )
00053 {
00054 std::ostringstream sql;
00055 sql << "SELECT p_name FROM pool ORDER BY p_name";
00056 myHelper_.getValueVector( sql, result );
00057 return;
00058 }
00059
00060
00061
00062 void
00063 psfa::DbReadDriverMySql::getSearchTags( std::string poolName,
00064 sVec& result )
00065 {
00066 std::ostringstream sql;
00067 sql << "SELECT pool_id FROM pool WHERE p_name = '"
00068 << poolName
00069 << "'";
00070 int poolId = myHelper_.getSingleValueInt( sql );
00071 sql.str("");
00072 sql << "SELECT s_name FROM search WHERE pool_id = '"
00073 << poolId
00074 << "' ORDER BY s_name";
00075 myHelper_.getValueVector( sql, result );
00076 return;
00077 }
00078
00079
00080 void
00081 psfa::DbReadDriverMySql::getKontextTags( std::string poolName,
00082 sVec& result )
00083 {
00084 std::ostringstream sql;
00085 sql << "SELECT pool_id FROM pool WHERE p_name = '"
00086 << poolName
00087 << "'";
00088 int poolId = myHelper_.getSingleValueInt( sql );
00089 sql.str("");
00090 sql << "SELECT k_name FROM kontext WHERE pool_id = '"
00091 << poolId
00092 << "' ORDER BY k_name";
00093 myHelper_.getValueVector( sql, result );
00094 return;
00095 }
00096
00097
00098
00099
00100 void
00101 psfa::DbReadDriverMySql::getUnits( std::string poolName, sVec& result )
00102 {
00103 std::ostringstream sql;
00104 sql << "SELECT pool_id FROM pool WHERE p_name = '"
00105 << poolName
00106 << "'";
00107 int poolId = myHelper_.getSingleValueInt( sql );
00108 sql.str("");
00109 sql << "SELECT u_name FROM unit WHERE pool_id = '"
00110 << poolId
00111 << "' ORDER BY u_name";
00112 myHelper_.getValueVector( sql, result );
00113 return;
00114 }
00115
00116
00117
00118 void
00119 psfa::DbReadDriverMySql::getFiles( std::string poolName, sVec& result )
00120 {
00121 std::ostringstream sql;
00122 sql << "SELECT pool_id FROM pool WHERE p_name = '"
00123 << poolName
00124 << "'";
00125 int poolId = myHelper_.getSingleValueInt( sql );
00126 sql.str("");
00127 sql << "SELECT f_name FROM file WHERE pool_id = '"
00128 << poolId
00129 << "' ORDER BY f_name";
00130 myHelper_.getValueVector( sql, result );
00131 return;
00132 }
00133
00134
00135
00136
00137 void
00138 psfa::DbReadDriverMySql::getUniqueContent( std::string poolName,
00139 sVec& result )
00140 {
00141 std::ostringstream sql;
00142 sql << "SELECT pool_id FROM pool WHERE p_name = '"
00143 << poolName
00144 << "'";
00145 int poolId = myHelper_.getSingleValueInt( sql );
00146 sql.str("");
00147 sql << "SELECT DISTINCT e_content FROM entry WHERE pool_id = '"
00148 << poolId
00149 << "' ORDER BY e_content";
00150 myHelper_.getValueVector( sql, result );
00151 return;
00152 }
00153
00154
00155
00156
00157 int
00158 psfa::DbReadDriverMySql::getEntries( std::string poolName,
00159 SearchData& search,
00160 std::vector< IndexData_SP >& result )
00161 {
00162 using std::ostringstream;
00163 using std::string;
00164 using mysqlcppapi::Result_Store;
00165 typedef std::map< string, string > sMap;
00166
00167 ostringstream sql;
00168 ostringstream tables;
00169 tables << " entry ";
00170
00171 ostringstream ands;
00172 if( search.searchTag != "" ) {
00173 sql.str("");
00174 sql << "SELECT search_id FROM search WHERE s_name = '"
00175 << search.searchTag
00176 << "'";
00177 int id = myHelper_.getSingleValueInt( sql );
00178 ands << " AND search_id = '"
00179 << id
00180 << "'";
00181 }
00182 if( search.content != "" ) {
00183 ands << " AND e_content = '"
00184 << search.content
00185 << "'";
00186 }
00187 if( search.attKey != "" && search.attValue != "" ) {
00188 tables << ", attribut";
00189 ands << " AND entry.entry_id = attribut.entry_id"
00190 << " AND attribut.a_key = '"
00191 << search.attKey
00192 << "' AND attribut.a_value = '"
00193 << search.attValue
00194 << "'";
00195 }
00196 if( search.kontextTag != "" ) {
00197 sql.str("");
00198 sql << "SELECT kontext_id FROM kontext WHERE k_name = '"
00199 << search.kontextTag
00200 << "'";
00201 int id = myHelper_.getSingleValueInt( sql );
00202 ands << " AND kontext_id = '"
00203 << id
00204 << "'";
00205 }
00206 if( search.unit != "" ) {
00207 sql.str("");
00208 sql << "SELECT unit_id FROM unit WHERE u_name = '"
00209 << search.unit
00210 << "'";
00211 int id = myHelper_.getSingleValueInt( sql );
00212 ands << " AND unit_id = '"
00213 << id
00214 << "'";
00215 }
00216
00217 sql.str("");
00218 sql << "SELECT pool_id FROM pool WHERE p_name = '"
00219 << poolName
00220 << "'";
00221 int poolId = myHelper_.getSingleValueInt( sql );
00222 sql.str("");
00223 sql << "SELECT * FROM"
00224 << tables.str()
00225 << " WHERE pool_id ='"
00226 << poolId
00227 << "'"
00228 << ands.str()
00229 << " ORDER BY e_content";
00230
00231 Result_Store dbResult;
00232 myHelper_.queryDb( sql, dbResult );
00233
00234 unsigned int size = dbResult.size();
00235 if( size == 0 ) {
00236 return 0;
00237 }
00238
00239 sMap nameMap;
00240 sMap::iterator dbName;
00241 std::string name;
00242 ostringstream nameStream;
00243
00244 result.reserve( size );
00245 IndexData_SP data;
00246 Result_Store::iterator i;
00247 Result_Store::iterator iEnd = dbResult.end();
00248 for( i = dbResult.begin(); i != iEnd; ++i ) {
00249 mysqlcppapi::Row row = *i;
00250 data = IndexData_SP( new IndexData );
00251
00252 if( search.incSearch ) {
00253 nameStream.str("");
00254 nameStream << "search_id" << row[2];
00255 dbName = nameMap.find( nameStream.str() );
00256 if( dbName == nameMap.end() ) {
00257 sql.str("");
00258 sql << "SELECT s_name FROM search WHERE search_id = '"
00259 << row[2]
00260 << "'";
00261 name = myHelper_.getSingleValueString( sql );
00262 sMap::value_type v( nameStream.str(), name );
00263 nameMap.insert( v );
00264 data->tag = name;
00265 }
00266 else {
00267 data->tag = dbName->second;
00268 }
00269 }
00270
00271 if( search.incKontext ) {
00272 nameStream.str("");
00273 nameStream << "kontext_id" << row[3];
00274 dbName = nameMap.find( nameStream.str() );
00275 if( dbName == nameMap.end() ) {
00276 sql.str("");
00277 sql << "SELECT k_name FROM kontext WHERE kontext_id = '"
00278 << row[3]
00279 << "'";
00280 name = myHelper_.getSingleValueString( sql );
00281 sMap::value_type v( nameStream.str(), name );
00282 nameMap.insert( v );
00283 data->kontext = name;
00284 }
00285 else {
00286 data->kontext = dbName->second;
00287 }
00288 }
00289
00290 if( search.incUnit ) {
00291 nameStream.str("");
00292 nameStream << "unit_id" << row[4];
00293 dbName = nameMap.find( nameStream.str() );
00294 if( dbName == nameMap.end() ) {
00295 sql.str("");
00296 sql << "SELECT u_name FROM unit WHERE unit_id = '"
00297 << row[4]
00298 << "'";
00299 name = myHelper_.getSingleValueString( sql );
00300 sMap::value_type v( nameStream.str(), name );
00301 nameMap.insert( v );
00302 data->unit = name;
00303 }
00304 else {
00305 data->unit = dbName->second;
00306 }
00307 }
00308
00309 if( search.incFileName ) {
00310 nameStream.str("");
00311 nameStream << "file_id" << row[5];
00312 dbName = nameMap.find( nameStream.str() );
00313 if( dbName == nameMap.end() ) {
00314 sql.str("");
00315 sql << "SELECT f_name FROM file WHERE file_id = '"
00316 << row[5]
00317 << "'";
00318 name = myHelper_.getSingleValueString( sql );
00319 sMap::value_type v( nameStream.str(), name );
00320 nameMap.insert( v );
00321 data->file = name;
00322 }
00323 else {
00324 data->file = dbName->second;
00325 }
00326 }
00327
00328 data->content = row[6];
00329
00330 if( search.incPosData ) {
00331 data->lineStart = row[7];
00332 data->columnStart = row[8];
00333 data->lineStop = row[9];
00334 data->columnStop = row[10];
00335 data->posStart = row[11];
00336 data->posEnd = row[12];
00337 }
00338
00339 if( search.incAtts ) {
00340 sql.str("");
00341 sql << "SELECT a_key, a_value FROM attribut WHERE entry_id = '"
00342 << row[0]
00343 << "'";
00344 Result_Store attResult;
00345 myHelper_.queryDb( sql, attResult );
00346 unsigned int attSize = attResult.size();
00347 if( attSize > 0 ) {
00348 psfa::AttMap atts;
00349 Result_Store::iterator a;
00350 Result_Store::iterator aEnd = attResult.end();
00351 for( a = attResult.begin(); a != aEnd; ++a ) {
00352 mysqlcppapi::Row attRow = *a;
00353 AttMap::value_type v( attRow[0], attRow[1] );
00354 data->attributes.insert( v );
00355 }
00356 }
00357 }
00358
00359 result.push_back( data );
00360 }
00361 return result.size();
00362 }