00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <sstream>
00019 #include <numeric>
00020
00021 #include "psfa/DbDriverMySqlHelper.hpp"
00022 #include "psfa/Config.hpp"
00023
00024
00025
00026 int psfa::DbDriverMySqlHelper::fileId_ = 0;
00027
00028 int psfa::DbDriverMySqlHelper::overhead_ = 0;
00029
00030 std::vector< int > psfa::DbDriverMySqlHelper::lengths_;
00031
00032
00033
00034 bool
00035 psfa::DbDriverMySqlHelper::connect()
00036 {
00037 Config* conf = Config::getConfig();
00038 try {
00039 con_.set_Host( conf->getConfigValue( "mysql", "host" ) );
00040 con_.set_User( conf->getConfigValue( "mysql", "user" ) );
00041 con_.set_Password( conf->getConfigValue( "mysql", "password" ) );
00042 con_.connect();
00043 con_.select_database( conf->getConfigValue( "mysql", "db" ) );
00044 }
00045 catch (mysqlcppapi::ex_BadQuery& er) {
00046 return false;
00047 }
00048 return con_.is_connected();
00049 }
00050
00051
00052
00053 bool
00054 psfa::DbDriverMySqlHelper::disconnect()
00055 {
00056 if( con_.is_connected() ) {
00057 con_.close();
00058 return true;
00059 }
00060 return false;
00061 }
00062
00063
00064
00065 mysqlcppapi::Result_NoData
00066 psfa::DbDriverMySqlHelper::executeSql( const std::ostringstream& sql )
00067 {
00068 try {
00069 mysqlcppapi::Query query = con_.create_Query();
00070 query << sql.str();
00071 mysqlcppapi::Result_NoData res = query.execute();
00072 return res;
00073 }
00074 catch(mysqlcppapi::ex_BadQuery& er) {
00075 mysqlcppapi::Result_NoData r;
00076 return r;
00077 }
00078 catch(mysqlcppapi::ex_BadConversion& er) {
00079 mysqlcppapi::Result_NoData r;
00080 return r;
00081 }
00082 }
00083
00084
00085
00086 void
00087 psfa::DbDriverMySqlHelper::queryDb( const std::ostringstream& sql,
00088 mysqlcppapi::Result_Store& store )
00089 {
00090 try {
00091 mysqlcppapi::Query query = con_.create_Query();
00092 query << sql.str();
00093 store = query.store();
00094 return;
00095 }
00096 catch(mysqlcppapi::ex_BadQuery& er) {
00097 return;
00098 }
00099 catch(mysqlcppapi::ex_BadConversion& er) {
00100 return;
00101 }
00102 }
00103
00104
00105
00106 int
00107 psfa::DbDriverMySqlHelper::getNextId( const std::string table,
00108 const std::string idField )
00109 {
00110 try {
00111 mysqlcppapi::Query query = con_.create_Query();
00112 query << "SELECT MAX("
00113 << idField
00114 << ") FROM "
00115 << table;
00116 mysqlcppapi::Result_Store res = query.store();
00117 mysqlcppapi::Row row = res.fetch_row();
00118 int id = row[0];
00119 if( id ) {
00120 return id + 1;
00121 }
00122 else {
00123 return 1;
00124 }
00125 }
00126 catch(mysqlcppapi::ex_BadQuery& er) {
00127 return 0;
00128 }
00129 catch(mysqlcppapi::ex_BadConversion& er) {
00130 return 0;
00131 }
00132 }
00133
00134
00135
00136
00137 const std::string
00138 psfa::DbDriverMySqlHelper::getSingleValueString( const std::ostringstream& sql )
00139 {
00140 try {
00141 mysqlcppapi::Query query = con_.create_Query();
00142 query << sql.str();
00143 mysqlcppapi::Result_Store res = query.store();
00144 if( res.size() != 0 ) {
00145 mysqlcppapi::Row row = res.fetch_row();
00146 return row[0];
00147 }
00148 else {
00149 return "";
00150 }
00151 }
00152 catch(mysqlcppapi::ex_BadQuery& er) {
00153 return "";
00154 }
00155 catch(mysqlcppapi::ex_BadConversion& er) {
00156 return "";
00157 }
00158 }
00159
00160
00161
00162 int
00163 psfa::DbDriverMySqlHelper::getSingleValueInt( const std::ostringstream& sql )
00164 {
00165 try {
00166 mysqlcppapi::Query query = con_.create_Query();
00167 query << sql.str();
00168 mysqlcppapi::Result_Store res = query.store();
00169 if( res.size() != 0 ) {
00170 mysqlcppapi::Row row = res.fetch_row();
00171 return row[0];
00172 }
00173 else {
00174 return 0;
00175 }
00176 }
00177 catch(mysqlcppapi::ex_BadQuery& er) {
00178 return 0;
00179 }
00180 catch(mysqlcppapi::ex_BadConversion& er) {
00181 return 0;
00182 }
00183 }
00184
00185 int
00186 psfa::DbDriverMySqlHelper::calculateFilePosition( int line,
00187 int column,
00188 int fileId )
00189 {
00190 std::ostringstream sql;
00191
00192 if( line < 1 ) {
00193 return 0;
00194 }
00195 --line;
00196 if( fileId_ != fileId ) {
00197 sql.str("");
00198 sql << "SELECT f_dos FROM file WHERE file_id ='"
00199 << fileId
00200 << "'";
00201 overhead_ = getSingleValueInt( sql );
00202
00203 sql.str("");
00204 sql << "SELECT r_length FROM file_row WHERE file_id ='"
00205 << fileId
00206 << "' ORDER BY __id";
00207 lengths_.clear();
00208 getValueVector( sql, lengths_ );
00209 fileId_ = fileId;
00210 }
00211 int pos = std::accumulate( lengths_.begin(),
00212 lengths_.begin() + line,
00213 0 )
00214 + column
00215 + line * overhead_
00216 - 1;
00217 return pos;
00218 }