Back to index

nordugrid-arc-nox  1.1.0~rc6
Public Member Functions | Private Attributes
Arc::MySQLQuery Class Reference

#include <MysqlWrapper.h>

Inheritance diagram for Arc::MySQLQuery:
Inheritance graph
[legend]
Collaboration diagram for Arc::MySQLQuery:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 MySQLQuery (Database *db)
virtual ~MySQLQuery ()
virtual int get_num_colums ()
 Get the colum number in the query result.
virtual int get_num_rows ()
 Get the row number in the query result.
virtual bool execute (const std::string &sqlstr)
 Execute the query.
virtual QueryRowResult get_row (int row_number) const
 Get the value of one row in the query result.
virtual QueryRowResult get_row () const
 Get the value of one row in the query result, the row number will be automatically increased each time the method is called.
virtual std::string get_row_field (int row_number, std::string &field_name)
 Get the value of one specific field in one specific row.
virtual bool get_array (std::string &sqlstr, QueryArrayResult &result, std::vector< std::string > &arguments)
 Query the database by using some parameters into sql sentence e.g.

Private Attributes

MySQLDatabasedb_
MYSQL_RES * res
int num_rows
int num_colums
std::map< std::string, int > field_names

Detailed Description

Definition at line 49 of file MysqlWrapper.h.


Constructor & Destructor Documentation

Definition at line 66 of file MysqlWrapper.cpp.

    : db_(NULL), res(NULL) {
    MySQLDatabase *database = NULL;
    database = dynamic_cast<MySQLDatabase*>(db);
    if(database == NULL) std::cerr<<"The parameter of constructor should be MySQLDatabase type"<<std::endl;
    //  db_ = new MySQLDatabase(*database);
    db_ = database;
  }

Definition at line 75 of file MysqlWrapper.cpp.

                          {
    //  if(db_!=NULL) delete db_;
    db_ == NULL;
    if (res)
      mysql_free_result(res);
  }

Member Function Documentation

bool Arc::MySQLQuery::execute ( const std::string &  sqlstr) [virtual]

Execute the query.

Parameters:
sqlstrThe sql sentence used to query

Implements Arc::Query.

Definition at line 82 of file MysqlWrapper.cpp.

                                                  {
    //std::cout << "Query: " << sqlstr << std::endl;
    if (db_->mysql == NULL)
      std::cerr << "mysql object is NULL" << std::endl;
    if (mysql_query(db_->mysql, sqlstr.c_str())) {
      std::cerr << "Database query failed" << std::endl;
      return false;
    }
    res = mysql_store_result(db_->mysql);
    if (res) {
      num_colums = 0;
      MYSQL_FIELD *field = NULL;
      while (true) {
        field = mysql_fetch_field(res);
        if (field) {
          if (field->name)
            field_names[field->name] = num_colums;
          num_colums++;
        }
        else
          break;
      }
      num_rows = mysql_num_rows(res);
    }
    return true;
  }

Here is the caller graph for this function:

bool Arc::MySQLQuery::get_array ( std::string &  sqlstr,
QueryArrayResult result,
std::vector< std::string > &  arguments 
) [virtual]

Query the database by using some parameters into sql sentence e.g.

"select table.value from table where table.name = ?"

Parameters:
sqlstrThe sql sentence with some parameters marked with "?".
resultThe result in an array which includes all of the value in query result.
argumentsThe argument list which should exactely correspond with the parametes in sql sentence.

Implements Arc::Query.

Definition at line 143 of file MysqlWrapper.cpp.

                                                                                                         {
    //replace the "?" in sql sentence with the values from argument of this method.
    //e.g. if the sql sentence is "select table.value from table where table.name = ?",
    //and the third argument of this method is std::string& name, then the "name" will replace
    //the "?" in the above sql sentence.
    //The repalacement will be sequentially done, which means the first argument in the vector will replace
    //the first "?" in sql sentence, and the second one will replace the second "?".
    //The values inside the third arguments--"arguments" should all be std::string type, since we will not
    //distinguish them in this method itself.
    std::string arg_str;
    size_t found = std::string::npos;
    int i = 0;
    while (true) {
      if (i < arguments.size())
        arg_str = arguments[i++];
      else
        arg_str = "";
      found = sqlstr.find("?", found + 1);
      if ((found != std::string::npos) && arg_str.empty()) {
        std::cerr << "There is no enough arguments given in method: MySQLQuery::getarray " << std::endl;
        return false;
      }
      if (found == std::string::npos)
        break;
      sqlstr.replace(found, 1, arg_str);
    }

    //std::cout << "The sql sentence after replacement: " << sqlstr << std::endl;
    QueryRowResult row_value;
    if (execute(sqlstr)) {
      int rows = get_num_rows();
      for (int i = 0; i < rows; i++) {
        row_value = get_row();
        result.push_back(row_value);
      }
      return true;
    }
    return false;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

Get the colum number in the query result.

Implements Arc::Query.

Definition at line 109 of file MysqlWrapper.cpp.

                                 {
    return num_colums;
  }

Here is the caller graph for this function:

int Arc::MySQLQuery::get_num_rows ( ) [virtual]

Get the row number in the query result.

Implements Arc::Query.

Definition at line 113 of file MysqlWrapper.cpp.

                               {
    return num_rows;
  }

Here is the caller graph for this function:

QueryRowResult Arc::MySQLQuery::get_row ( int  row_number) const [virtual]

Get the value of one row in the query result.

Parameters:
row_numberThe number of the row
Returns:
A vector includes all the values in the row

Implements Arc::Query.

Definition at line 117 of file MysqlWrapper.cpp.

                                                         {
    mysql_data_seek(res, row_number);
    return get_row();
  }

Here is the call graph for this function:

Here is the caller graph for this function:

Get the value of one row in the query result, the row number will be automatically increased each time the method is called.

Implements Arc::Query.

Definition at line 122 of file MysqlWrapper.cpp.

                                           {
    MYSQL_ROW row = mysql_fetch_row(res);
    QueryRowResult row_value;
    if (row == NULL)
      return row_value;

    std::string field;
    for (int i = 0; i < num_colums; i++) {
      if(row[i] == NULL) field="";
      else field = row[i];
      row_value.push_back(field);
    }
    return row_value;
  }

Here is the caller graph for this function:

std::string Arc::MySQLQuery::get_row_field ( int  row_number,
std::string &  field_name 
) [virtual]

Get the value of one specific field in one specific row.

Parameters:
row_numberThe row number inside the query result
field_nameThe field name for the value which will be return
Returns:
The value of the specified filed in the specified row

Implements Arc::Query.

Definition at line 137 of file MysqlWrapper.cpp.

                                                                           {
    int field_number = field_names[field_name];
    QueryRowResult row_value = get_row(row_number);
    return row_value[field_number];
  }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 65 of file MysqlWrapper.h.

std::map<std::string, int> Arc::MySQLQuery::field_names [private]

Definition at line 69 of file MysqlWrapper.h.

Definition at line 68 of file MysqlWrapper.h.

Definition at line 67 of file MysqlWrapper.h.

MYSQL_RES* Arc::MySQLQuery::res [private]

Definition at line 66 of file MysqlWrapper.h.


The documentation for this class was generated from the following files: