00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00030 #ifndef DB_PLANNER_DATABASE_H_
00031 #define DB_PLANNER_DATABASE_H_
00032
00033 #include "qvariant_convert.h"
00034 #include <map>
00035 #include <string>
00036 #include <vector>
00037 #include <QtSql>
00038 using std::map;
00039 using std::string;
00040 using std::vector;
00041 class QVariant;
00042
00043 namespace db_planner {
00044
00046 class Table {
00047 private:
00049 vector<vector<QVariant> > data_;
00051 map<string, int> column_names_;
00052 int num_columns_, num_rows_;
00053 public:
00054 Table() : num_columns_(0), num_rows_(0) {}
00055
00056 int NumColumns() const { return num_columns_; }
00057
00058 int NumRows() const { return num_rows_; }
00059
00061 bool Populate(QSqlQuery query);
00062
00064 bool GetColumnIndex(const string& column_name, int* index) const {
00065 map<string, int>::const_iterator map_iter = column_names_.find(column_name);
00066 if (map_iter == column_names_.end()) return false;
00067 *index = map_iter->second;
00068 return true;
00069 }
00070
00073 template <class FieldType>
00074 bool GetField(const size_t column_num,
00075 const size_t row_num,
00076 FieldType* result) const {
00077 return (column_num < data_.size())
00078 ? QVariantConvert(data_[column_num][row_num], result)
00079 : false;
00080 }
00081 };
00082
00087 class DatabaseConnection {
00088 private:
00090 QSqlDatabase db_;
00092 static int connection_num_;
00094 bool connected_;
00095 public:
00096 DatabaseConnection(const string& host_name,
00097 const int port,
00098 const string& user_name,
00099 const string& password,
00100 const string& database_name,
00101 const string& connection_type = "QPSQL");
00103 virtual ~DatabaseConnection() { db_.close(); }
00104
00106
00108 bool Query(const string& sql, Table* table = NULL) const;
00110 bool isConnected() const {return connected_;}
00111 };
00112
00113 }
00114
00115 #endif // DB_PLANNER_DATABASE_H_