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;
}
Exemple #2
0
static bool handle_name_exists(const StoreQueryResult& result)
{
	size_t rowCount = result.num_rows();
	assert(rowCount < 2);

	return rowCount == 1;
}
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();
}
Exemple #4
0
/**
   @brief Return the list of all orders of a given user
 
   @param[in] pp    An instance of User
 
   @return    A vector of Order
 */
vector<Order *> & Order::ordersForUser(User & pp)
{
    vector<Order *> *orders = new vector<Order *>;
    
    // get an instance of the database
    Database& db = Database::instance();
    
    try {
        // ask Database for a valid connection to mySQL
        Connection *conn = db.getConnection();
        
        // obtain an instance of mysqlpp::Query and init it
        Query q = conn->query();
        q << "SELECT * FROM orders WHERE uid = " << pp.uniqueID()
          << " ORDER BY oid, date";
        
        StoreQueryResult res = q.store();
        if (!res.empty()) {
            orders->reserve(res.num_rows());
            StoreQueryResult::const_iterator it;

            for (it = res.begin(); it != res.end(); it++){
                Row row = *it;
                orders->push_back(new Order(row));
            }
        }
    } catch (std::exception &e) {
        cerr << "an error occurred: " << e.what() << endl;
    }
    
    return *orders;
}
Exemple #5
0
/**
   @brief Returns the list of available categories.
 
   @return    Vector of pointer to Category
 */
vector<Category *> &Category::catalog()
{
    // get an instance of the database
    Database& db = Database::instance();
    vector<Category *> *catalog = NULL;
    
    try {
        // ask Database for a valid connection to mySQL
        Connection *conn = db.getConnection();
        
        // obtain an instance of mysqlpp::Query and init it
        Query q = conn->query(SQL_CATEGORY_CAT);
        StoreQueryResult res = q.store();
        
        if (!res.empty()) {
            catalog = new vector<Category *>;
            catalog->reserve(res.num_rows());
            
            for (size_t i = 0; i < res.num_rows(); ++i) {
                Category *c = new Category();
                c->setValueForKey(KEY_CAT_CID, 
                                 (string) res[i][KEY_CAT_CID]);
                c->setValueForKey(KEY_CAT_NAME, 
                                  (string) res[i][KEY_CAT_NAME]);
                catalog->push_back(c);
            }
        }                
    }
    catch (const mysqlpp::BadQuery& e) {
        // Something went wrong with the SQL query.
        cerr << "Query failed: " << e.what() << endl;
    }
    catch (const Exception& er) {
        cerr << "Error: " << er.what() << endl;
    }    
    
    return *catalog;
}
Exemple #6
0
int db::get_station(std::string mac) {
	StoreQueryResult res = q_get_station.store(mac.c_str(), "");

	// found station
	if (res.num_rows() > 0) {
		mysqlpp::StoreQueryResult::const_iterator it;
		mysqlpp::Row row = *(res.begin());
		return row[0];
	}

	cerr << mac << endl;
	// new station to db
	SimpleResult r_exp = q_insert_station.execute("0", "0", mac.c_str());
	return r_exp.insert_id();
}
Exemple #7
0
/**
   @brief Return the list of products of a given category
 
   @param[in]    aCid    The category ID
 
   @return    A vector of ProductProxy
 
   @note Pass zero as category ID to get all products 
 */
vector<ProductProxy *> & ProductProxy::catalog(int aCid)
{
    // get an instance of the database
    Database& db = Database::instance();
    vector<ProductProxy *> *catalog = NULL;
    
    try {
        // ask Database for a valid connection to mySQL
        Connection *conn = db.getConnection();
        
        // obtain an instance of mysqlpp::Query and init it
        Query q = conn->query();
        q << SQL_CATALOG_PROXY;
        if (aCid != 0)
            q << "WHERE cid = " << aCid;
        q << " ORDER BY pid, category, name";
        StoreQueryResult res = q.store();
        
        if (!res.empty()) {
            catalog = new vector<ProductProxy *>;
            catalog->reserve(res.num_rows());
            
            for (size_t i = 0; i < res.num_rows(); ++i) {
                catalog->push_back(new ProductProxy((int) res[i][0]));
            }
        }                
    }
    catch (const mysqlpp::BadQuery& e) {
        // Something went wrong with the SQL query.
        cerr << "Query failed: " << e.what() << endl;
    }
    catch (const Exception& er) {
        cerr << "Error: " << er.what() << endl;
    }        
    return *catalog;
}
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;
    }

}
Exemple #9
0
int main() {
    try {
        Connection conn(false);
        conn.connect("map", "localhost", "root", "dtbs");
        Query query = conn.query();

        /* To insert stuff with escaping */
      /*  query << "INSERT INTO map " <<
                     "VALUES (" <<
                     "'', " << // This is left empty because the column is AUTO_INCREMENT 
                     "\"" << escape << "some_var_that_contains_some_value" << "\"" <<
                     ");";
        query.execute();*/
        /* That's it for INSERT */

        /* Now SELECT */
        query << "SELECT * FROM Station";
        StoreQueryResult ares = query.store();
        for (size_t i = 0; i < ares.num_rows(); i++)
           cout << "Name: " << ares[i]["id"] << " - Address: " << ares[i]["name"] << endl;

        /* Let's get a count of something */
        query << "SELECT COUNT(*) FROM Station";
        StoreQueryResult bres = query.store();
        cout << "Total rows: " << bres[0]["id"];

    } catch (BadQuery er) { // handle any connection or
        // query errors that may come up
        cerr << "Error: " << er.what() << endl;
        return -1;
    } catch (const BadConversion& er) {
        // Handle bad conversions
        cerr << "Conversion error: " << er.what() << endl <<
                "\tretrieved data size: " << er.retrieved <<
                ", actual size: " << er.actual_size << endl;
        return -1;
    } catch (const Exception& er) {
        // Catch-all for any other MySQL++ exceptions
        cerr << "Error: " << er.what() << endl;
        return -1;
    }

    return (EXIT_SUCCESS);
}
Exemple #10
0
bool MasterServer::get_fileinfo(const string &fid, FileInfo &fileinfo)
{
	//查找cache
	map<string, FileInfo>::iterator it = m_fileinfo_cache.find(fid);
	if(it != m_fileinfo_cache.end())
	{
		fileinfo = it->second;
		return true;
	}
	//查找数据库
	if(m_db_connection == NULL)
		return false;

	char sql_str[1024];
	snprintf(sql_str, 1024, "select fid,name,size,chunkid,chunkip,chunkport,findex,foffset from SFS.fileinfo_%s where fid='%s'"
							,fid.substr(0,2).c_str(), fid.c_str());
	Query query = m_db_connection->query(sql_str);
	StoreQueryResult res = query.store();
	if (!res || res.empty())
		return false;

	size_t i;
	for(i=0; i<res.num_rows(); ++i)
	{
		ChunkPath chunk_path;
		fileinfo.fid      = res[i]["fid"].c_str();
		fileinfo.name     = res[i]["name"].c_str();
		fileinfo.size     = atoi(res[i]["size"].c_str());
		chunk_path.id     = res[i]["chunkid"].c_str();
		chunk_path.ip     = res[i]["chunkip"].c_str();
		chunk_path.port   = atoi(res[i]["chunkport"].c_str());
		chunk_path.index  = atoi(res[i]["findex"].c_str());
		chunk_path.offset = atoi(res[i]["foffset"].c_str());

		fileinfo.add_chunkpath(chunk_path);
	}
	//添加到cache
	m_fileinfo_cache.insert(std::make_pair(fileinfo.fid, fileinfo));

	return true;
}
Exemple #11
0
int getNextTaskId()
//searching for a task to start
{
    int id = -1,progress;
    ostringstream strbuf;
    strbuf << "SELECT id,progress FROM tasks WHERE started=0 and running=0 ORDER BY date ASC LIMIT 1";
    
    Connection con(use_exceptions);
    
    try
    {
	Query query = con.query();
	con.connect(DATABASE, HOST, USER, PASSWORD);
	query << strbuf.str();
 StoreQueryResult res = query.store();
	cout<<strbuf.str()<<endl;
	
        if (res && res.num_rows() > 0)
	{
		mysqlpp::Row row;
		row = res.at(0);
		id = row["id"];
		progress = row["progress"];
	}

    }
    catch (const BadQuery& er)
    {    
    // Handle any query errors
        cerr << "getNextTaskId - Query error: " << er.what() << endl;
	    id = -1;
    }
    catch (const BadConversion& er)
    {
    // Handle bad conversions
        cerr << "getNextTaskId - Conversion error: " << er.what() << endl <<
			    "\tretrieved data size: " << er.retrieved <<
			    ", actual size: " << er.actual_size << endl;
	    id = -1;
    }
    catch (const Exception& er)
    {
    // Catch-all for any other MySQL++ exceptions
        cerr << "getNextTaskId - Error: " << er.what() << endl;
	    id = -1;
    }
    if(id>=1 && ! (progress >=1)) { //first time run, convert formula/density
    try{
    	strbuf.str("");
    strbuf << "SELECT energy,formula0,formula1,formula12,rhoin0,rhoin1,rhoin12 FROM tasks WHERE id="<<id;
    	Query query = con.query();
	con.connect(DATABASE, HOST, USER, PASSWORD);
	query << strbuf.str();
 StoreQueryResult res = query.store();
	cout<<strbuf.str()<<endl;
	mysqlpp::Row row;
		row = res.at(0);
		string formula0(row["formula0"]), formula1(row["formula1"]), formula12(row["formula12"]);
		double energy=row["energy"];
		double rhoin0=row["rhoin0"];
		double rhoin1=row["rhoin1"];
		double rhoin12=row["rhoin12"];
		double rho0=0.,rho1=0.,rho12=0.;
		double beta0=0.,beta1=0.,beta12=0.;
		cout<<formula0.size()<<endl;
		cout<<"formula0= "<<formula0<<endl;
		cout<<"formula1= "<<formula1<<endl;
		cout<<"formula12= "<<formula12<<endl;
		cout<<"rhoin0= "<<rhoin0<<endl;
		cout<<"rhoin1= "<<rhoin1<<endl;
		cout<<"rhoin12= "<<rhoin12<<endl;
		compound cmpd0(formula0,energy,rhoin0), cmpd1(formula1,energy,rhoin1), cmpd12(formula12,energy,rhoin12);
		strbuf.str("");
        strbuf << "UPDATE tasks SET rho0="<<cmpd0.rho_el<<",rho1="<<cmpd1.rho_el<<",rho12="<<cmpd12.rho_el<<",beta0="<<cmpd0.beta<<",beta1="<<cmpd1.beta<<",beta12="<<cmpd12.beta<<" WHERE id="<< id;
        //cout<<strbuf.str()<<endl;
        query.exec(strbuf.str());
	cout<<"rho0="<<cmpd0.rho_el<<"\tbeta="<<cmpd0.beta<<endl;
	cout<<"rho1="<<cmpd1.rho_el<<"\tbeta="<<cmpd1.beta<<endl;
	cout<<"rho12(Maximum)="<<cmpd12.rho_el<<"\tbeta="<<cmpd12.beta<<endl;
    }
    
       catch (const BadQuery& er)
    {
    // Handle any query errors
        cerr << "updateDone - Query error: " << er.what() << endl;
        return -1;
    }
    catch (const BadConversion& er)
    {
    // Handle bad conversions
        cerr << "updateDone - Conversion error: " << er.what() << endl <<
                "\tretrieved data size: " << er.retrieved <<
                ", actual size: " << er.actual_size << endl;
        return -1;
    }
    catch (const Exception& er)
    {
    // Catch-all for any other MySQL++ exceptions
        cerr << "updateDone - Error: " << er.what() << endl;
        return -1;
    }
}
 
    return id;
}
const char* WebModelUnified::website_search(const char* req){
    JSONObject root;
    JSONArray jsarray;
    Connection* conn = connect();
    Query query = conn->query();

    string reqstr(req);
    string reqstr_spaced = replaceChar(reqstr, '+', ' ');
    vector<string> splittedstr;
    split(splittedstr, reqstr_spaced, boost::algorithm::is_any_of(" "));

    int titleForce = 10;
    int descriptionForce = 1;
    int urlForce = 3;

    query << "SELECT * , ";
    //Occurences total
    for(size_t i1 = 0; i1 < splittedstr.size(); i1++)
    {
        string s = splittedstr[i1];
        if(i1 != 0){
            query << " + ";
        }

        query << "((" <<
          titleForce << " * (char_length(title) - char_length(replace(title,'" << s << "',''))) + " <<
          descriptionForce << " * (char_length(description) - char_length(replace(description,'" << s << "',''))) + " <<
          urlForce << " * (char_length(url) - char_length(replace(url,'" << s << "','')))" <<
        ") / char_length('" << s << "'))";
    }
    query << " as Occurances " << " FROM website ";

    //Where clause
    for(size_t i1 = 0; i1 < splittedstr.size(); i1++)
    {
        string s = splittedstr[i1];
        if(i1 == 0) {
            query << "WHERE ";
        } else {
            query << "OR ";
        }
        query << "(url LIKE '%" << s << "%' or title LIKE '%" << s << "%' or description LIKE '%" << s << "%') ";
    }

    query << " ORDER BY " << "Occurances desc, title ASC ";

    StoreQueryResult ares = query.store();
    unsigned int numrow = ares.num_rows();

    for(unsigned int i = 0; i < numrow; i++)
    {
        JSONObject result;

        result[L"title"] = new JSONValue(wchartFromChar(ares[i]["title"]));
        result[L"description"] = new JSONValue(wchartFromChar(ares[i]["description"]));
        result[L"url"] = new JSONValue(wchartFromChar(ares[i]["url"]));
        JSONValue* resultVal = new JSONValue(result);

        jsarray.push_back(resultVal);
    }

    root[L"results"] = new JSONValue(jsarray);

    JSONValue* jvalue = new JSONValue(root);

    const char* returnStr = fromWString(jvalue->Stringify());
    delete jvalue;
    return returnStr;
}
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;
        }
	
}
Exemple #14
0
int main(int argc,char **argv)
{
	{
		char *ee = getenv("ZT_NETCONF_MYSQL_HOST");
		if (!ee) {
			fprintf(stderr,"missing environment variable: ZT_NETCONF_MYSQL_HOST\n");
			return -1;
		}
		strcpy(mysqlHost,ee);
		ee = getenv("ZT_NETCONF_MYSQL_PORT");
		if (!ee)
			strcpy(mysqlPort,"3306");
		else strcpy(mysqlPort,ee);
		ee = getenv("ZT_NETCONF_MYSQL_DATABASE");
		if (!ee) {
			fprintf(stderr,"missing environment variable: ZT_NETCONF_MYSQL_DATABASE\n");
			return -1;
		}
		strcpy(mysqlDatabase,ee);
		ee = getenv("ZT_NETCONF_MYSQL_USER");
		if (!ee) {
			fprintf(stderr,"missing environment variable: ZT_NETCONF_MYSQL_USER\n");
			return -1;
		}
		strcpy(mysqlUser,ee);
		ee = getenv("ZT_NETCONF_MYSQL_PASSWORD");
		if (!ee) {
			fprintf(stderr,"missing environment variable: ZT_NETCONF_MYSQL_PASSWORD\n");
			return -1;
		}
		strcpy(mysqlPassword,ee);
	}

	char buf[131072],buf2[131072];
	Identity signingIdentity;
	std::string dictBuf;

	try {
		dbCon = new Connection(mysqlDatabase,mysqlHost,mysqlUser,mysqlPassword,(unsigned int)strtol(mysqlPort,(char **)0,10));
		if (dbCon->connected()) {
			fprintf(stderr,"connected to mysql server successfully\n");
		} else {
			fprintf(stderr,"unable to connect to database server\n");
			return -1;
		}
	} catch (std::exception &exc) {
		fprintf(stderr,"unable to connect to database server: %s\n",exc.what());
		return -1;
	}

	// Send ready message to tell parent that the service is up, and to
	// solicit netconf-init.
	{
		Dictionary response;
		response["type"] = "ready";
		std::string respm = response.toString();
		uint32_t respml = (uint32_t)htonl((uint32_t)respm.length());
		stdoutWriteLock.lock();
		write(STDOUT_FILENO,&respml,4);
		write(STDOUT_FILENO,respm.data(),respm.length());
		stdoutWriteLock.unlock();
	}

	for(;;) {
		for(int l=0;l<4;) {
			int n = (int)read(STDIN_FILENO,buf + l,4 - l);
			if (n < 0) {
				fprintf(stderr,"error reading frame size from stdin: %s\n",strerror(errno));
				return -1;
			}
			l += n;
		}
		unsigned int fsize = (unsigned int)ntohl(*((const uint32_t *)buf));

		while (dictBuf.length() < fsize) {
			int n = (int)read(STDIN_FILENO,buf,std::min((int)sizeof(buf),(int)(fsize - dictBuf.length())));
			if (n < 0) {
				fprintf(stderr,"error reading frame from stdin: %s\n",strerror(errno));
				return -1;
			}
			for(int i=0;i<n;++i)
				dictBuf.push_back(buf[i]);
		}
		Dictionary request(dictBuf);
		dictBuf = "";

		if (!dbCon->connected()) {
			fprintf(stderr,"connection to database server lost\n");
			return -1;
		}

		// Check QNetworkConfigRefresh (MEMORY table) and push network
		// config refreshes to queued peer/network pairs.
		try {
			Dictionary to;
			{
				Query q = dbCon->query();
				q << "SELECT DISTINCT LOWER(HEX(Node_id)) AS Node_id,LOWER(HEX(Network_id)) AS Network_id FROM QNetworkConfigRefresh";
				StoreQueryResult rs = q.store();
				for(unsigned long i=0;i<rs.num_rows();++i) {
					std::string &nwids = to[rs[i]["Node_id"].c_str()];
					if (nwids.length())
						nwids.push_back(',');
					nwids.append(rs[i]["Network_id"]);
				}
			}

			{
				Query q = dbCon->query();
				q << "DELETE FROM QNetworkConfigRefresh";
				q.exec();
			}

			Dictionary response;
			response["type"] = "netconf-push";
			response["to"] = to.toString();
			std::string respm = response.toString();
			uint32_t respml = (uint32_t)htonl((uint32_t)respm.length());

			stdoutWriteLock.lock();
			write(STDOUT_FILENO,&respml,4);
			write(STDOUT_FILENO,respm.data(),respm.length());
			stdoutWriteLock.unlock();
		} catch ( ... ) {}

		try {
			const std::string &reqType = request.get("type");
			if (reqType == "netconf-init") { // initialization to set things like netconf's identity
				Identity netconfId(request.get("netconfId"));
				if ((netconfId)&&(netconfId.hasPrivate())) {
					signingIdentity = netconfId;
					fprintf(stderr,"got netconf signing identity: %s\n",signingIdentity.toString(false).c_str());
				} else {
					fprintf(stderr,"netconfId invalid or lacks private key\n");
					return -1;
				}
			} else if (reqType == "netconf-request") { // NETWORK_CONFIG_REQUEST packet
				if (!signingIdentity) {
					fprintf(stderr,"no signing identity; missing netconf-init?\n");
					return -1;
				}

				// Deserialize querying peer identity and network ID
				Identity peerIdentity(request.get("peerId"));
				uint64_t nwid = strtoull(request.get("nwid").c_str(),(char **)0,16);
				std::string fromAddr(request.get("from",""));

				// Meta-information from node, such as (future) geo-location stuff
				Dictionary meta;
				if (request.contains("meta"))
					meta.fromString(request.get("meta"));

				// Check validity of node's identity, ignore request on failure
				if (!peerIdentity.locallyValidate()) {
					fprintf(stderr,"identity failed validity check: %s\n",peerIdentity.toString(false).c_str());
					continue;
				}

				// Save node's identity if unknown
				{
					Query q = dbCon->query();
					q << "SELECT identity FROM Node WHERE id = " << peerIdentity.address().toInt();
					StoreQueryResult rs = q.store();
					if (rs.num_rows() > 0) {
						if (rs[0]["identity"] != peerIdentity.toString(false)) {
							// TODO: handle collisions...
							continue;
						}
					} else {
						q = dbCon->query();
						q << "INSERT INTO Node (id,creationTime,identity) VALUES (" << peerIdentity.address().toInt() << "," << Utils::now() << "," << quote << peerIdentity.toString(false) << ")";
						if (!q.exec()) {
							fprintf(stderr,"error inserting Node row for peer %s, aborting netconf request\n",peerIdentity.address().toString().c_str());
							continue;
						}
						// TODO: launch background validation
					}
				}

				// Look up core network information
				bool isOpen = false;
				unsigned int multicastPrefixBits = 0;
				unsigned int multicastDepth = 0;
				bool emulateArp = false;
				bool emulateNdp = false;
				unsigned int arpCacheTtl = 0;
				unsigned int ndpCacheTtl = 0;
				std::string name;
				std::string desc;
				{
					Query q = dbCon->query();
					q << "SELECT name,`desc`,isOpen,multicastPrefixBits,multicastDepth,emulateArp,emulateNdp,arpCacheTtl,ndpCacheTtl FROM Network WHERE id = " << nwid;
					StoreQueryResult rs = q.store();
					if (rs.num_rows() > 0) {
						name = rs[0]["name"].c_str();
						desc = rs[0]["desc"].c_str();
						isOpen = ((int)rs[0]["isOpen"] > 0);
						emulateArp = ((int)rs[0]["emulateArp"] > 0);
						emulateNdp = ((int)rs[0]["emulateNdp"] > 0);
						arpCacheTtl = (unsigned int)rs[0]["arpCacheTtl"];
						ndpCacheTtl = (unsigned int)rs[0]["ndpCacheTtl"];
						multicastPrefixBits = (unsigned int)rs[0]["multicastPrefixBits"];
						multicastDepth = (unsigned int)rs[0]["multicastDepth"];
					} else {
						Dictionary response;
						response["peer"] = peerIdentity.address().toString();
						response["nwid"] = request.get("nwid");
						response["type"] = "netconf-response";
						response["requestId"] = request.get("requestId");
						response["error"] = "OBJ_NOT_FOUND";
						std::string respm = response.toString();
						uint32_t respml = (uint32_t)htonl((uint32_t)respm.length());

						stdoutWriteLock.lock();
						write(STDOUT_FILENO,&respml,4);
						write(STDOUT_FILENO,respm.data(),respm.length());
						stdoutWriteLock.unlock();

						continue; // ABORT, wait for next request
					}
				}

				// Check membership if this is a closed network
				bool authenticated = true;
				if (!isOpen) {
					Query q = dbCon->query();
					q << "SELECT Node_id FROM NetworkNodes WHERE Network_id = " << nwid << " AND Node_id = " << peerIdentity.address().toInt();
					StoreQueryResult rs = q.store();
					if (!rs.num_rows()) {
						Dictionary response;
						response["peer"] = peerIdentity.address().toString();
						response["nwid"] = request.get("nwid");
						response["type"] = "netconf-response";
						response["requestId"] = request.get("requestId");
						response["error"] = "ACCESS_DENIED";
						std::string respm = response.toString();
						uint32_t respml = (uint32_t)htonl((uint32_t)respm.length());

						stdoutWriteLock.lock();
						write(STDOUT_FILENO,&respml,4);
						write(STDOUT_FILENO,respm.data(),respm.length());
						stdoutWriteLock.unlock();

						authenticated = false;
					}
				}

				// Update most recent activity entry for this peer, also indicating
				// whether authentication was successful.
				{
					if (fromAddr.length()) {
						Query q = dbCon->query();
						q << "INSERT INTO NetworkActivity (Network_id,Node_id,lastActivityTime,authenticated,lastActivityFrom) VALUES (" << nwid << "," << peerIdentity.address().toInt() << "," << Utils::now() << "," << (authenticated ? 1 : 0) << "," << quote << fromAddr << ") ON DUPLICATE KEY UPDATE lastActivityTime = VALUES(lastActivityTime),authenticated = VALUES(authenticated),lastActivityFrom = VALUES(lastActivityFrom)";
						q.exec();
					} else {
						Query q = dbCon->query();
						q << "INSERT INTO NetworkActivity (Network_id,Node_id,lastActivityTime,authenticated) VALUES (" << nwid << "," << peerIdentity.address().toInt() << "," << Utils::now() << "," << (authenticated ? 1 : 0) << ") ON DUPLICATE KEY UPDATE lastActivityTime = VALUES(lastActivityTime),authenticated = VALUES(authenticated)";
						q.exec();
					}
				}

				if (!authenticated)
					continue; // ABORT, wait for next request

				// Get list of etherTypes in comma-delimited hex format
				std::string etherTypeWhitelist;
				{
					Query q = dbCon->query();
					q << "SELECT DISTINCT LOWER(HEX(etherType)) AS etherType FROM NetworkEthertypes WHERE Network_id = " << nwid;
					StoreQueryResult rs = q.store();
					for(unsigned long i=0;i<rs.num_rows();++i) {
						if (etherTypeWhitelist.length() > 0)
							etherTypeWhitelist.push_back(',');
						etherTypeWhitelist.append(rs[i]["etherType"].c_str());
					}
				}

				// Get multicast group rates in dictionary format
				Dictionary multicastRates;
				{
					Query q = dbCon->query();
					q << "SELECT DISTINCT multicastGroupMac,multicastGroupAdi,preload,maxBalance,accrual FROM NetworkMulticastRates WHERE Network_id = " << nwid;
					StoreQueryResult rs = q.store();
					for(unsigned long i=0;i<rs.num_rows();++i) {
						unsigned long preload = (unsigned long)rs[i]["preload"];
						unsigned long maxBalance = (unsigned long)rs[i]["maxBalance"];
						unsigned long accrual = (unsigned long)rs[i]["accrual"];
						unsigned long long mac = (unsigned long long)rs[i]["multicastGroupMac"];
						sprintf(buf,"%.12llx/%lx",(mac & 0xffffffffffffULL),(unsigned long)rs[i]["multicastGroupAdi"]);
						sprintf(buf2,"%lx,%lx,%lx",preload,maxBalance,accrual);
						multicastRates[buf] = buf2;
					}
				}

				// Check for (or assign?) static IP address assignments
				std::string ipv4Static;
				std::string ipv6Static;
				{
					Query q = dbCon->query();
					q << "SELECT INET_NTOA(ip) AS ip,netmaskBits FROM IPv4Static WHERE Node_id = " << peerIdentity.address().toInt() << " AND Network_id = " << nwid;
					StoreQueryResult rs = q.store();
					if (rs.num_rows() > 0) {
						for(int i=0;i<rs.num_rows();++i) {
							if (ipv4Static.length())
								ipv4Static.push_back(',');
							ipv4Static.append(rs[i]["ip"].c_str());
							ipv4Static.push_back('/');
							ipv4Static.append(rs[i]["netmaskBits"].c_str());
						}
					}

					// Try to auto-assign if there's any auto-assign networks with space
					// available.
					if (!ipv4Static.length()) {
						unsigned char addressBytes[5];
						peerIdentity.address().copyTo(addressBytes,5);

						q = dbCon->query();
						q << "SELECT ipNet,netmaskBits FROM IPv4AutoAssign WHERE Network_id = " << nwid;
						rs = q.store();
						if (rs.num_rows() > 0) {
							for(int aaRow=0;aaRow<rs.num_rows();++aaRow) {
								uint32_t ipNet = (uint32_t)((unsigned long)rs[aaRow]["ipNet"]);
								unsigned int netmaskBits = (unsigned int)rs[aaRow]["netmaskBits"];

								uint32_t tryIp = (((uint32_t)addressBytes[1]) << 24) |
								                 (((uint32_t)addressBytes[2]) << 16) |
								                 (((uint32_t)addressBytes[3]) << 8) |
								                 ((((uint32_t)addressBytes[4]) % 254) + 1);
								tryIp &= (0xffffffff >> netmaskBits);
								tryIp |= ipNet;

								for(int k=0;k<100000;++k) {
									Query q2 = dbCon->query();
									q2 << "INSERT INTO IPv4Static (Network_id,Node_id,ip,netmaskBits) VALUES (" << nwid << "," << peerIdentity.address().toInt() << "," << tryIp << "," << netmaskBits << ")";
									if (q2.exec()) {
										sprintf(buf,"%u.%u.%u.%u",(unsigned int)((tryIp >> 24) & 0xff),(unsigned int)((tryIp >> 16) & 0xff),(unsigned int)((tryIp >> 8) & 0xff),(unsigned int)(tryIp & 0xff));
										if (ipv4Static.length())
											ipv4Static.push_back(',');
										ipv4Static.append(buf);
										ipv4Static.push_back('/');
										sprintf(buf,"%u",netmaskBits);
										ipv4Static.append(buf);
										break;
									} else { // insert will fail if IP is in use due to uniqueness constraints in DB
										++tryIp;
										if ((tryIp & 0xff) == 0)
											tryIp |= 1;
										tryIp &= (0xffffffff >> netmaskBits);
										tryIp |= ipNet;
									}
								}

								if (ipv4Static.length())
									break;
							}
						}
					}
				}

				// Assemble response dictionary to send to peer
				Dictionary netconf;
				sprintf(buf,"%.16llx",(unsigned long long)nwid);
				netconf[ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID] = buf;
				netconf[ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO] = peerIdentity.address().toString();
				netconf[ZT_NETWORKCONFIG_DICT_KEY_NAME] = name;
				netconf[ZT_NETWORKCONFIG_DICT_KEY_DESC] = desc;
				netconf[ZT_NETWORKCONFIG_DICT_KEY_IS_OPEN] = (isOpen ? "1" : "0");
				netconf[ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES] = etherTypeWhitelist;
				netconf[ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_RATES] = multicastRates.toString();
				sprintf(buf,"%llx",(unsigned long long)Utils::now());
				netconf[ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP] = buf;
				netconf[ZT_NETWORKCONFIG_DICT_KEY_EMULATE_ARP] = (emulateArp ? "1" : "0");
				netconf[ZT_NETWORKCONFIG_DICT_KEY_EMULATE_NDP] = (emulateNdp ? "1" : "0");
				if (arpCacheTtl) {
					sprintf(buf,"%x",arpCacheTtl);
					netconf[ZT_NETWORKCONFIG_DICT_KEY_ARP_CACHE_TTL] = buf;
				}
				if (ndpCacheTtl) {
					sprintf(buf,"%x",ndpCacheTtl);
					netconf[ZT_NETWORKCONFIG_DICT_KEY_NDP_CACHE_TTL] = buf;
				}
				if (multicastPrefixBits) {
					sprintf(buf,"%x",multicastPrefixBits);
					netconf[ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_PREFIX_BITS] = buf;
				}
				if (multicastDepth) {
					sprintf(buf,"%x",multicastDepth);
					netconf[ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_DEPTH] = buf;
				}
				if (ipv4Static.length())
					netconf[ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC] = ipv4Static;
				if (ipv6Static.length())
					netconf[ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC] = ipv6Static;
				if ((!isOpen)&&(authenticated)) {
					CertificateOfMembership com(Utils::now(),ZT_NETWORK_AUTOCONF_DELAY * 3,nwid,peerIdentity.address());
					com.sign(signingIdentity);
					netconf[ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP] = com.toString();
				}

				// Send netconf as service bus response
				{
					Dictionary response;
					response["peer"] = peerIdentity.address().toString();
					response["nwid"] = request.get("nwid");
					response["type"] = "netconf-response";
					response["requestId"] = request.get("requestId");
					response["netconf"] = netconf.toString();
					std::string respm = response.toString();
					uint32_t respml = (uint32_t)htonl((uint32_t)respm.length());

					stdoutWriteLock.lock();
					write(STDOUT_FILENO,&respml,4);
					write(STDOUT_FILENO,respm.data(),respm.length());
					stdoutWriteLock.unlock();

					// LOOP, wait for next request
				}
			}
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;
}
int main(int argc, char* argv[])
{
        Connection c("magpie", "localhost", "stephen",
                        "iloverae", 0);
        ifstream ifs;
        ifs.open(argv[1]);
        string junk, email, firstName, lastName, userName;

        int sectionNum = argv[1][3] - 48;

        Query q = c.query();
        Query r = c.query();
 	Query s = c.query();
 	Query t = c.query();
 	while (ifs.peek() != EOF)
	{
            int uid;
        	try
        	{
        		getline (ifs, junk, ':');
        		getline (ifs, email, '"');
        		userName = email;
        		userName.erase(userName.find_first_of('@'));
        		getline (ifs, junk, '"');
        		getline (ifs, firstName, ' ');
        		getline (ifs, lastName, '"');
        		getline (ifs, junk);
        		if (lastName[2] == ' ') {
                    lastName.erase(0,3);
                }
                if (lastName[1] == '\'') {
                    lastName.erase(1,1);
                }
                //
                // Check and see whether user already exists.
                s << "SELECT * FROM user WHERE username='******'" << endl;
                StoreQueryResult uqr = s.store();
                if (uqr.num_rows() <= 0) {
        		    cout << "Creating student account for " << email  << " " << userName << " " << firstName << " " << lastName << endl;
                    q << "INSERT INTO user (username,password,firstname,lastname,role) VALUES ('" << userName << "', SHA1('" << userName << "'), '" << firstName << "','" << lastName << "','student')" << endl;
                    q.execute();

                    r << "SELECT last_insert_id()" << endl;
                    StoreQueryResult lid = r.store();
                    uid = lid[0][0];
                }
                else {
                    cout << "(user " << userName << " already in database.)" << endl;
                    uid = uqr[0]["uid"];
                }

                t << "INSERT INTO courseLink (uid,courseid) VALUES (" << uid << ", " << sectionNum << ")" << endl;
                t.execute();
        		
        		q.reset();
        		r.reset();
        		s.reset();
        		t.reset();
        		
                }
                catch (const Exception &e)
                {
                      cout << "I just caught error: " << e.what() << endl;
                      exit(1);
        	}
	}
	c.disconnect();
	return 0;
}