void MailboxReader::execute() { while ( q->hasResults() ) { Row * r = q->nextRow(); UString n = r->getUString( "name" ); uint id = r->getInt( "id" ); Mailbox * m = ::mailboxes->find( id ); if ( !m || m->name() != n ) { m = Mailbox::obtain( n ); if ( n != m->d->name ) m->d->name = n; m->setId( id ); ::mailboxes->insert( id, m ); } if ( r->getBoolean( "deleted" ) ) m->setType( Mailbox::Deleted ); else m->setType( Mailbox::Ordinary ); uint uidvalidity = r->getInt( "uidvalidity" ); if ( m->d->uidvalidity != uidvalidity ) { m->d->uidvalidity = uidvalidity; m->abortSessions(); } if ( !r->isNull( "owner" ) ) m->setOwner( r->getInt( "owner" ) ); m->setUidnextAndNextModSeq( r->getInt( "uidnext" ), r->getBigint( "nextmodseq" ), q->transaction() ); m->setFlag( r->getEString( "flag" ) ); } if ( !q->done() || done ) return; done = true; if ( q->transaction() ) q->transaction()->commit(); ::readers->remove( this ); ::wiped = false; if ( q->failed() && !EventLoop::global()->inShutdown() ) { List<Mailbox> * c = Mailbox::root()->children(); if ( c && !c->isEmpty() ) log( "Couldn't update mailbox tree: " + q->error() ); else log( "Couldn't create mailbox tree: " + q->error(), Log::Disaster ); } if ( owner ) owner->execute(); };
void createParmFiles2(string mask, int popid, int popno, int totaltrait, int totalmarker, string db, string name_str) { int distinctpop = -1; try { ifstream sqlparmfile("../exe/SQLparms.txt"); string servername, dbname, username, password; getline(sqlparmfile,servername); getline(sqlparmfile,dbname); getline(sqlparmfile,username); getline(sqlparmfile,password); Connection conn( dbname.c_str(), servername.c_str(), username.c_str(), password.c_str()); Query query = conn.query(); string pp = "pop"+toStrings(popno); //SELECT COUNT(DISTINCT pop1 ) as number FROM structure where popstructid = 1; query << "SELECT COUNT(DISTINCT "<<pp<<" ) as number FROM structure WHERE popstructid = " << popid; StoreQueryResult ares = query.store(); if(!ares) { cerr << query.error() << endl; return; } else { for(size_t i = 0; i < ares.num_rows(); i ++) { distinctpop = ares[i]["number"] ; } } } catch(BadQuery er) { cerr << "Error: " << er.what() << endl; return; } catch(const BadConversion& er) { cerr << "Conversion error: " << er.what() << endl; return; } catch(const Exception& er) { cerr << "Error: " << er.what() << endl; return; } ofstream myfile; myfile.open("parms2.txt"); myfile <<mask<<" "<<popid<<" "<<popno<<" "<<totaltrait<<" "<<totalmarker<<" "<<name_str<<" "<<db<<" "<<distinctpop<< " " << team << endl; myfile<<"vanilla"<<endl; myfile.close(); }
int main() { Connection con(false); if ( con.connect("ogame_dev","localhost","root","") ) { Query query = con.query("SELECT * FROM user"); if ( StoreQueryResult res = query.store() ) { cout << "Table user:"******"-------------------------------------------" << endl; for ( size_t i = 0; i < res.num_rows(); i++ ) { cout << "| "; for ( size_t y = 0; y < res.num_fields(); y++) { cout << res[i][y] << " | "; } cout << endl; } cout << "-------------------------------------------" << endl; } else { cerr << "Error query users table: " << query.error() << endl; return -1; } return 0; } else { cerr << "Error DB connection: " << con.error() << endl; return -1; } }
int writeStructureFile(int popid, int popnum) { int noidis = 0; int tts=-1; try { ifstream sqlparmfile("../exe/SQLparms.txt"); string servername, dbname, username, password; getline(sqlparmfile,servername); getline(sqlparmfile,dbname); getline(sqlparmfile,username); getline(sqlparmfile,password); Connection conn( dbname.c_str(), servername.c_str(), username.c_str(), password.c_str()); Query query = conn.query(); string pp = "pop"+toStrings(popnum); query << "SELECT "<<pp<<" FROM structure WHERE popstructid = " << popid << " ORDER BY sampleid"; StoreQueryResult ares = query.store(); if(!ares) { cerr << query.error() << endl; return -1; } else { ofstream myfile; string fname = "data.test.Z"; myfile.open(fname.c_str()); tts = ares.num_rows(); for(size_t i = 0; i < ares.num_rows(); i ++) { myfile << ares[i][pp.c_str()] << "\n"; } myfile.close(); } } catch(BadQuery er) { cerr << "Error: " << er.what() << endl; return -1; } catch(const BadConversion& er) { cerr << "Conversion error: " << er.what() << endl; return -1; } catch(const Exception& er) { cerr << "Error: " << er.what() << endl; return -1; } return tts; }
void ConfigureVehicle::refreshTable() { //qDebug()<<"refresh the configurevehicle table"; Query q; if (!q.selectVehicle(model)) { //qDebug()<<"something happened with selectVehicle"; QString p = "somethign happened with refreshTable selectVehicle: " + q.error(); QMessageBox message(QMessageBox::Critical,"Problem!",p,QMessageBox::Ok,this,Qt::Dialog); message.exec(); } }
int gettsid(string mask) { try { ifstream sqlparmfile("../exe/SQLparms.txt"); string servername, dbname, username, password; getline(sqlparmfile,servername); getline(sqlparmfile,dbname); getline(sqlparmfile,username); getline(sqlparmfile,password); Connection conn( dbname.c_str(), servername.c_str(), username.c_str(), password.c_str()); //cout << conn.error() << endl; Query query = conn.query(); query << "SELECT get_ts_id('" << mask << "') as id"; StoreQueryResult ares = query.store(); if(!ares) { cerr << query.error() << endl; return -1; } int id; for(size_t i = 0; i < ares.num_rows(); i++) { id = ares[i]["id"]; return id; } } catch(BadQuery er) { cerr<< "Error: " <<er.what() <<endl; return -1; } catch(const BadConversion& er) { cerr <<"Conversion error: " << er.what() << endl; return -1; } catch (const Exception& er) { cerr << "Error: " <<er.what() << endl; return -1; } }
void execute() { if ( !q ) { q = new Query( "select not exists (select * from " "information_schema.table_privileges where " "privilege_type='DELETE' and table_name=" "'messages' and grantee=$1) and not exists " "(select u.usename from pg_catalog.pg_class c " "left join pg_catalog.pg_user u on " "(u.usesysid=c.relowner) where c.relname=" "'messages' and u.usename=$1) as allowed", this ); q->bind( 1, Configuration::text( Configuration::DbUser ) ); q->execute(); } if ( !q->done() ) return; Row * r = q->nextRow(); if ( q->failed() || !r || r->getBoolean( "allowed" ) == false ) { EString s( "Refusing to start because we have too many " "privileges on the messages table in secure " "mode." ); result->setError( s ); l->log( s, Log::Disaster ); if ( q->failed() ) { l->log( "Query: " + q->description(), Log::Disaster ); l->log( "Error: " + q->error(), Log::Disaster ); } } else { result->setState( Query::Completed ); } result->notify(); }
void MySQLStorage::store (Event* e) { try { this->init(); } catch (const exception& e) { stringstream errss; errss << "Couldn't initialise MySQL database: " << e.what(); throw runtime_error (errss.str()); } try { // Store the event to the database. this->dbCheck(); Query query (this->conn.query()); query << "INSERT INTO `log` " << "( `time`" << ", `ms`" << ", `eventId`" << ", `dataId`" << ", `message`" #ifdef GOT_DT_ERROR_CODE << ", `errorCode`" #endif << ", `datastreamId`" << ", `datastreamName`" << ", `hostname`" #ifdef GOT_DT_LOG_LEVEL << ", `logLevel`" #endif << ", `pid`" << ")" << " VALUES " << " ( FROM_UNIXTIME(" << e->getTime().tv_sec << ")" << " , '" << e->getTime().tv_usec/1000 << "', '" << e->getId() // SQL safe? (uuid) << "', '" << e->getDataId() // SQL safe? (uuid) << "', '" << e->getMessage() // Need to escape #ifdef GOT_DT_ERROR_CODE << "', '" << e->getErrorCode() // SQL safe (int) #endif << "', '" << e->getDatastreamId() // SQL safe? (uuid) << "', '" << e->getDatastreamName() // SQL safe? (datastreamNames can't contain '; etc) << "', '" << e->getHostname() // SQL safe? #ifdef GOT_DT_LOG_LEVEL << "', '" << e->getLogLevel() // SQL safe (int) #endif << "', '" << e->getPid() // SQL safe (int) << "');"; //DBG ("INSERT Query string: " << query.str()); if (query.exec() == false) { stringstream ee; ee << "Query failed, error: '" << query.error() << "'"; throw runtime_error (ee.str()); } } catch (mysqlpp::BadQuery e) { this->handleBadQuery (e); } catch (mysqlpp::Exception e) { this->handleException (e); } }
void MySQLStorage::init (void) { vector<pair<string, string> > fields; fields.push_back (make_pair ("id", "int(10) unsigned NOT NULL auto_increment")); fields.push_back (make_pair ("eventId", "varchar(255) collate latin1_bin NOT NULL default ''")); fields.push_back (make_pair ("dataId", "varchar(255) collate latin1_bin NOT NULL default ''")); fields.push_back (make_pair ("message", "varchar(255) collate latin1_bin NOT NULL default ''")); fields.push_back (make_pair ("errorCode", "int(10) unsigned NOT NULL default 0")); fields.push_back (make_pair ("datastreamId", "varchar(255) collate latin1_bin NOT NULL default ''")); fields.push_back (make_pair ("datastreamName", "varchar(255) collate latin1_bin NOT NULL default ''")); fields.push_back (make_pair ("hostname", "varchar(255) collate latin1_bin NOT NULL default ''")); fields.push_back (make_pair ("logLevel", "int(10) unsigned NOT NULL default 0")); // could be enum? fields.push_back (make_pair ("pid", "int(10) unsigned NOT NULL default 0")); fields.push_back (make_pair ("time", "DATETIME NOT NULL")); fields.push_back (make_pair ("ms", "int(10) unsigned NOT NULL default 0")); string table (MySQLStorage::defaultDbTable); try { this->dbCheck(); Query query (this->conn.query()); // Create the database if necessary query << "CREATE TABLE IF NOT EXISTS `" << table << "` " << "("; bool first (true); auto iField (fields.begin()), fEnd (fields.end()); while (iField != fEnd) { if (!first) { query << ", "; } else { first = false; } query << "`" << iField->first << "` " << iField->second; ++iField; } query << ", PRIMARY KEY (`id`)" << ") " << "ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_bin; "; if (query.exec() == false) { stringstream ee; ee << "Query failed, error: '" << query.error() << "'"; throw runtime_error (ee.str()); } // Check that all of the columns exist iField = fields.begin(); while (iField != fEnd) { query.reset(); query << "SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'dt'" << " AND TABLE_NAME = '" << table << "' AND COLUMN_NAME = '" << iField->first << "';"; UseQueryResult res (query.use()); if (!res) { stringstream ee; ee << "Query failed, error: '" << query.error() << "'"; throw runtime_error (ee.str()); } if (Row row = res.fetch_row()) { } else { // Need to add the column //DBG ("Couldn't find the column '" << iField->first << "'"); query.reset(); query << "ALTER TABLE `" << table << "` ADD `" << iField->first << "` " << iField->second << ";"; //DBG (name << " ALTER query string: " << query.str()); if (query.exec() == false) { stringstream ee; ee << "Query failed, error: '" << query.error() << "'"; throw runtime_error (ee.str()); } } ++iField; } } catch (mysqlpp::BadQuery e) { this->handleBadQuery (e); } catch (mysqlpp::Exception e) { this->handleException (e); } }
int writeMarkerFile(string mask, string filename) { int noidis = 0; try { ifstream sqlparmfile("../exe/SQLparms.txt"); string servername, dbname, username, password; getline(sqlparmfile,servername); getline(sqlparmfile,dbname); getline(sqlparmfile,username); getline(sqlparmfile,password); Connection conn( dbname.c_str(), servername.c_str(), username.c_str(), password.c_str()); Query query = conn.query(); query << "Select get_ms_id('"<<mask<<"') as id"; StoreQueryResult ares = query.store(); int id; if(!ares) { cerr<<query.error()<<endl; return -1; } for(size_t i = 0; i<ares.num_rows(); i++) { id = ares[i]["id"]; //cout << id << endl; } query.reset(); //cout << "ID" << id << endl; query << "SELECT id FROM marker WHERE markersetid= " << id << " ORDER BY idx"; //cout << query.str() << endl; ares = query.store(); if(!ares) { cerr << "Right here. " << endl; cerr << query.error()<<endl; return -1; } else { //run here query once to get size of samples and than store it in a 2d int array Query q3 = conn.query(); q3 << "SELECT value FROM markerval" << team << ", marker WHERE markerid = marker.id AND markerid = " << ares [0]["id"] << " ORDER BY idx"; StoreQueryResult ares3 = q3.store(); int totalind= ares3.num_rows(); int totalmark = ares.num_rows(); q3.reset(); double towrite[totalind][totalmark]; ofstream myfile; myfile.open(filename.c_str()); ofstream myfile11; string fn = "data.vocab"; myfile11.open(fn.c_str()); noidis = ares.num_rows(); for (size_t i = 0; i <ares.num_rows(); i++) { myfile11 << ares[i]["id"] << "\n"; Query q2 = conn.query(); q2 << "SELECT value FROM markerval" << team << ", marker WHERE markerid = marker.id AND markerid = " << ares [i]["id"] << " ORDER BY idx"; //cout << q2.str() << endl; StoreQueryResult ares2 = q2.store(); // noidis = ares2.num_rows(); //cout << noidis << endl; for(size_t j = 0; j <ares2.num_rows(); j ++) { towrite[j][i] = (double)ares2[j]["value"] ; //myfile << ares2[j]["value"] << "\t"; } //myfile << endl; q2.reset(); } myfile11.close(); for(int ind=0; ind <totalind ; ind++ ) { for(int mark=0; mark<totalmark; mark++) { myfile << towrite[ind][mark] << "\t"; } myfile << endl; } myfile.close(); } } catch(BadQuery er) { cerr << "Error: " << er.what() << endl; return -1; } catch(const BadConversion& er) { cerr << "Conversion error " << er.what() << endl; return -1; } catch(const Exception& er) { cerr << "Error :" << er.what() << endl; return -1; } //cout << noidis; return noidis; }
int writeTraitFile(string mask) { int noidis = 0; int tts=-1; try { ifstream sqlparmfile("../exe/SQLparms.txt"); string servername, dbname, username, password; getline(sqlparmfile,servername); getline(sqlparmfile,dbname); getline(sqlparmfile,username); getline(sqlparmfile,password); Connection conn( dbname.c_str(), servername.c_str(), username.c_str(), password.c_str()); Query query = conn.query(); query << "SELECT get_ts_id('" << mask << "') as id"; StoreQueryResult ares = query.store(); int id; if(!ares) { cerr << query.error() << endl; return -1; } for(size_t i=0; i < ares.num_rows(); i ++) { id = ares[i]["id"]; } query.reset(); query << "SELECT id FROM trait WHERE traitsetid = " << id << " ORDER BY idx"; ares = query.store(); if(!ares) { cerr << query.error() << endl; return -1; } else { tts = ares.num_rows(); int fnum=1; ofstream myfile1; string fname1 = "tr_id"; myfile1.open(fname1.c_str()); for(size_t i = 0; i < ares.num_rows(); i ++) { ofstream myfile; string fname = "tr"+toStrings(fnum); myfile.open(fname.c_str()); Query q2 = conn.query(); myfile1 << ares[i]["id"] << "\n"; query << "SELECT value FROM traitval" << team << ",trait WHERE traitid=trait.id AND traitid = " << ares[i]["id"] << " ORDER BY idx"; StoreQueryResult ares2 = query.store(); noidis = ares2.num_rows(); for(size_t j = 0; j < ares2.num_rows(); j ++) { myfile << ares2[j]["value"] << "\n"; } myfile << endl; q2.reset(); myfile.close(); fnum++; } myfile1.close(); } } catch(BadQuery er) { cerr << "Error: " << er.what() << endl; return -1; } catch(const BadConversion& er) { cerr << "Conversion error: " << er.what() << endl; return -1; } catch(const Exception& er) { cerr << "Error: " << er.what() << endl; return -1; } return tts; }
void insertIntoDB(int msid, string name, int totalind) { int towrite[totalind][10]; float eigen[totalind][5]; try { for(int fileno=2; fileno<=10; fileno++) { string file = "structout"+toStrings(fileno)+"_f"; ifstream myfile(file.c_str()); if(myfile.is_open()) { while(! myfile.eof()) { string line; getline(myfile, line); if(line.compare("Inferred ancestry of individuals:")==0) { getline(myfile, line); cout<<"found ::: Infere "<<endl; break; } } if(myfile.eof()) { cout<<" couldn't found"<<endl; return; } else { int linecount=0; while(! myfile.eof()) { string line; getline(myfile, line); vector<string> tokens; Tokenize(line, tokens," "); cout<<"label "<<atoi(tokens[1].data())<<endl; towrite[linecount][0]=atoi(tokens[1].data()); //vector<string> tokens1; //Tokenize(tokens[3], tokens1,": "); //vector<string> tokens22; //cout<<"tokens1[1] "<<tokens1[1].data()<<"tokens[3] "<<tokens[3].data()<<" "<<tokens[4].data()<<" "<<tokens[5].data()<<endl; //Tokenize(tokens1[1], tokens22," "); double maxx = -1; int maxpopp = -1; for(int kk=0; kk < fileno; kk++) { double perc = atof(tokens[kk+5].data()); if(perc > maxx) { cout<<"perc "<<perc<<" "<<maxx<<endl; maxpopp = kk+1; maxx = perc; } } cout<<"pop "<<maxpopp<<endl; //towrite[linecount][fileno-1]=atoi(tokens1[0].data()); towrite[linecount][fileno-1]=maxpopp; linecount++; if(linecount==totalind) break; } } } else { cout<<"file not found: "<<file <<endl; return; } } string file = "eig_coef"; ifstream myfile(file.c_str()); if(myfile.is_open()) { for(int i=0; i < totalind; i++) { string line; getline(myfile, line); vector<string> tokens; Tokenize(line, tokens,"\t"); cout<<"eigen1 "<<atof(tokens[0].data())<<"\teigen2 "<<atof(tokens[1].data())<<"\teigen3 "<<atof(tokens[2].data())<<endl; eigen[i][0]=atof(tokens[0].data()); eigen[i][1]=atof(tokens[1].data()); eigen[i][2]=atof(tokens[2].data()); eigen[i][3]=atof(tokens[3].data()); eigen[i][4]=atof(tokens[4].data()); } } else { cout<<"file not found: "<<file <<endl; return; } //insert ifstream sqlparmfile("../exe/SQLparms.txt"); string servername, dbname, username, password; getline(sqlparmfile,servername); getline(sqlparmfile,dbname); getline(sqlparmfile,username); getline(sqlparmfile,password); Connection conn( dbname.c_str(), servername.c_str(), username.c_str(), password.c_str()); Query query = conn.query(); query << "SELECT createPopStruct(" << msid<< ",'" <<name << "' ) as id"; StoreQueryResult ares = query.store(); int id; if(!ares) { cerr << query.error() << endl; return; } for(size_t i=0; i < ares.num_rows(); i ++) { id = ares[i]["id"]; } //start inserting query.reset(); for(int i=0; i < totalind; i++) { string qstri; qstri = "INSERT INTO structure (popstructid, sampleid, pop2,pop3,pop4,pop5,pop6,pop7,pop8,pop9,pop10,eig1,eig2,eig3,eig4,eig5) VALUES ("; qstri.append(toStrings(id)); qstri.append(","); for(int p=0; p<10; p++) { qstri.append(toStrings(towrite[i][p])); qstri.append(","); } for(int p=0; p<4; p++) { qstri.append(toStrings(eigen[i][p])); qstri.append(","); } qstri.append(toStrings(eigen[i][4])); qstri.append(")"); cout<<qstri<<endl; query << qstri; query.execute(); cout << query.error() << endl; query.reset(); } query.reset(); query << "UPDATE popstruct SET loadcmpt = 1 WHERE id = " << id << ";" << endl; query.execute(); } catch(BadQuery er) { cerr << "Error: " << er.what() << endl; } catch(const BadConversion& er) { cerr << "Conversion error: " << er.what() << endl; } catch(const Exception& er) { cerr << "Error: " << er.what() << endl; } }