Back to index

nordugrid-arc-nox  1.1.0~rc6
test_db.cpp
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil -*-
00002 
00003 #ifdef HAVE_CONFIG_H
00004 #include <config.h>
00005 #endif
00006 
00007 #include "MysqlWrapper.h"
00008 #include <iostream>
00009 #include <fstream>
00010 #include <string>
00011 #include <list>
00012 #include <map>
00013 
00014 int main(void) {
00015   std::string server = "127.0.0.1";
00016   int port = 3306;
00017   std::string dbname = "voms_myvo";
00018   std::string user = "root";
00019   std::string password = "aa1122";
00020   Arc::MySQLDatabase mydb(server, 3306);
00021 
00022   bool res = false;
00023   res = mydb.connect(dbname, user, password);
00024   if (res == false) {
00025     std::cerr << "Can't establish connection to mysql database" << std::endl;
00026     return 0;
00027   }
00028 
00029   Arc::MySQLQuery myquery(&mydb);
00030   std::cout << "Is connected? " << mydb.isconnected() << std::endl;
00031   std::string querystr = "select * from roles";
00032   myquery.execute(querystr);
00033 
00034   int num_rows, num_colums;
00035   num_rows = myquery.get_num_rows();
00036   num_colums = myquery.get_num_colums();
00037 
00038   Arc::QueryRowResult strlist;
00039   strlist = myquery.get_row();
00040 
00041   for (int i = 0; i < strlist.size(); i++)
00042     std::cout << "The value of " << i << "th field :" << strlist[i] << std::endl;
00043 
00044   std::string str1, str2;
00045   std::string fieldname = "role";
00046   str1 = myquery.get_row_field(0, fieldname);
00047   fieldname = "rid";
00048   str2 = myquery.get_row_field(0, fieldname);
00049   std::cout << "Number of rows: " << num_rows << " Number of colums: " << num_colums << std::endl;
00050   std::cout << str1 << "  " << str2 << std::endl;
00051 
00052   //Get role, the sql sentence can be put in some independent place, and then we
00053   //can adapt to different database schema without changing the code itself;
00054   std::string role = "'VO-Admin'";
00055   std::string userid = "1";
00056   querystr = "SELECT groups.dn, role FROM groups, m  LEFT JOIN roles ON roles.rid = m.rid WHERE groups.gid = m.gid AND roles.role =" + role + "AND m.userid =" + userid;
00057   myquery.execute(querystr);
00058   Arc::QueryArrayResult strarray;
00059   num_rows = myquery.get_num_rows();
00060   std::cout << "Get " << num_rows << " rows" << std::endl;
00061   for (int i = 0; i < num_rows; i++) {
00062     strlist = myquery.get_row();
00063     strarray.push_back(strlist);
00064   }
00065 
00066 
00067   querystr = "SELECT groups.dn, role FROM groups, m  LEFT JOIN roles ON roles.rid = m.rid WHERE groups.gid = m.gid AND roles.role = ? AND m.userid = ?";
00068   Arc::QueryArrayResult strarray1;
00069   Arc::MySQLQuery myquery1(&mydb);
00070   std::vector<std::string> args;
00071   args.push_back(role);
00072   args.push_back(userid);
00073   myquery1.get_array(querystr, strarray1, args);
00074   std::cout << "Get an result array with " << strarray1.size() << " rows" << std::endl;
00075 
00076   return 0;
00077 }