Go to the documentation of this file.00001 #include <rms/rms.hpp>
00002 #include <sstream>
00003 #include <iostream>
00004 #include <cstdlib>
00005
00006 using namespace std;
00007 using namespace librms;
00008
00009 rms::rms(string host, unsigned int port, string user, string password, string database) :
00010 host_(host), user_(user), password_(password), database_(database)
00011 {
00012 port_ = port;
00013 connected_ = false;
00014
00015 conn_ = mysql_init(NULL);
00016 }
00017
00018 rms::~rms()
00019 {
00020 if (connected_)
00021 {
00022 mysql_close(conn_);
00023 }
00024 }
00025
00026 unsigned int rms::get_port() const
00027 {
00028 return port_;
00029 }
00030
00031 string rms::get_host() const
00032 {
00033 return host_;
00034 }
00035
00036 string rms::get_user() const
00037 {
00038 return user_;
00039 }
00040
00041 string rms::get_password() const
00042 {
00043 return password_;
00044 }
00045
00046 string rms::get_database() const
00047 {
00048 return database_;
00049 }
00050
00051 bool rms::connected()
00052 {
00053 return connected_;
00054 }
00055
00056 bool rms::connect()
00057 {
00058 if (!mysql_real_connect(conn_, host_.c_str(), user_.c_str(), password_.c_str(), database_.c_str(), port_, NULL, 0))
00059 {
00060 cerr << "MySQL Error: '" << mysql_error(conn_) << "'" << endl;
00061 } else
00062 {
00063 connected_ = true;
00064 }
00065 return connected_;
00066 }
00067
00068 study rms::get_study(unsigned int id)
00069 {
00070 stringstream ss;
00071 ss << "SELECT * FROM `studies` WHERE `id`=" << id << ";";
00072 MYSQL_RES *res = this->query(ss.str());
00073 if (res)
00074 {
00075 MYSQL_ROW row = mysql_fetch_row(res);
00076 study s(this, atoi(row[0]), string(row[1]), string(row[2]), string(row[3]), atoi(row[4]), atoi(row[5]) != 0, atoi(row[6]) != 0, atoi(row[7]) != 0, string(row[8]), string(row[9]));
00077 mysql_free_result(res);
00078 return s;
00079 }
00080
00081
00082 study s(this, 1, "", "", "", 1, false, false, false, "", "");
00083 return s;
00084 }
00085
00086 MYSQL_RES *rms::query(string query)
00087 {
00088 if (connected_)
00089 {
00090 if (mysql_query(conn_, query.c_str()) == 0)
00091 {
00092
00093 return mysql_use_result(conn_);
00094 } else
00095 {
00096 cerr << "MySQL Error: '" << mysql_error(conn_) << "'" << endl;
00097 }
00098 } else
00099 {
00100 cerr << "Error: no connection to RMS." << endl;
00101 }
00102
00103
00104 return NULL;
00105 }