00001 #ifndef DVMYSQL_SET_H
00002 #define DVMYSQL_SET_H
00003
00004 #include <vector>
00005
00006 #include <dvutil/shared_ptr.h>
00007
00008 #include <dvmysql/sqldb.h>
00009 #include <dvmysql/object.h>
00010 #include <dvmysql/table.h>
00011 #include <dvmysql/reference.h>
00012 #include <dvmysql/select_exp.h>
00013
00014 namespace Dv {
00015 namespace Sql {
00016
00017
00018
00019
00020
00021
00022
00023 template <typename T = Dv::Sql::Object>
00024 class Set {
00025 public:
00026
00027 typedef Dv::shared_ptr<Set<T> > Ref;
00028
00029
00030
00031 typedef std::vector<Dv::shared_ptr<T> > Container;
00032 typedef typename Container::const_iterator const_iterator;
00033 typedef typename Container::const_reverse_iterator const_reverse_iterator;
00034 typedef typename Container::iterator iterator;
00035 typedef typename Container::reverse_iterator reverse_iterator;
00036
00037
00038
00039
00040
00041 Set(Dv::Sql::Command& q) throw (std::logic_error) {
00042 set(q);
00043 }
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 Set(Dv::Sql::Db& db, const std::string& prefix,
00066 SelectExp select_exp = SelectExp(new Dv::Sql::true_boolexp()),
00067 const std::string& rest = "") throw (std::logic_error) {
00068 try {
00069 Dv::Sql::Command q(db, prefix.c_str(), select_exp, rest);
00070 set(q);
00071 }
00072 catch (std::exception& e) {
00073 throw std::logic_error(e.what());
00074 }
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 Set(const Dv::Sql::Table::Ref& table,
00098 SelectExp select_exp = SelectExp(new Dv::Sql::true_boolexp()),
00099 const std::string& rest = "") throw (std::logic_error) {
00100 try {
00101 Dv::Sql::Command q(table->db(), select_exp.query(table, rest).c_str());
00102 set(q);
00103 }
00104 catch (std::exception& e) {
00105 throw std::logic_error(e.what());
00106 }
00107 }
00108
00109
00110
00111
00112
00113
00114
00115
00116 Set(const T& t, const std::string& rest = "") throw (std::logic_error) {
00117 set(*t.match_query(rest));
00118 }
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143 template<typename Source>
00144 Set(const Source& target, const Dv::Sql::Reference& reference,
00145 const std::string& rest = "") throw (std::logic_error) {
00146 T source;
00147 for (size_t i=0; (i<reference.size()); ++i)
00148 source.copy_data(reference.from(i), target, reference.to(i), true);
00149 set(*source.match_query(rest));
00150 }
00151
00152
00153
00154
00155
00156
00157 size_t
00158 set(Dv::Sql::Command& q) throw (std::logic_error) {
00159 try {
00160 q.db().log() << FUNCTION_S << ": " << q.text() << std::endl;
00161 q.exec();
00162 for (unsigned int r=0; (r<q.nrows()); ++r) {
00163 typename Dv::shared_ptr<T> p(new T(q, r));
00164 objects_.push_back(p);
00165 }
00166 return size();
00167 }
00168 catch (std::exception& e) {
00169 throw std::logic_error(e.what());
00170 }
00171 }
00172
00173 const_reverse_iterator rbegin() const { return objects_.rbegin(); }
00174 const_reverse_iterator rend() const { return objects_.rend(); }
00175
00176 const_iterator begin() const { return objects_.begin(); }
00177 const_iterator end() const { return objects_.end(); }
00178 size_t size() const { return objects_.size(); }
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188 template<typename C>
00189 void get(size_t i, C& container, const_iterator i0, const_iterator i1 ) const
00190 throw (std::logic_error) {
00191 while (i0 != i1) {
00192 container.push_back((*i0)->get<typename C::value_type>(i));
00193 ++i0;
00194 }
00195 }
00196
00197
00198
00199
00200
00201
00202
00203 template<typename C>
00204 void get_insert(size_t i, C& container, const_iterator i0, const_iterator i1 ) const
00205 throw (std::logic_error) {
00206 while (i0 != i1) {
00207 container.insert((*i0)->get<typename C::value_type>(i));
00208 ++i0;
00209 }
00210 }
00211
00212 template<typename C>
00213 void get(size_t i, C& container) const throw (std::logic_error) {
00214 get(i, container, begin(), end());
00215 }
00216 template<typename C>
00217 void get(const Table::Column& c, C& container) const throw (std::logic_error) {
00218 get(c.index(), container);
00219 }
00220 template<typename C>
00221 void get(const std::string& col_name, C& container) const throw (std::logic_error) {
00222 if (size())
00223 get((*(begin()))->table()->find(col_name).index(), container);
00224 }
00225
00226 void append(const Set<T>& source) {
00227 objects_.insert(objects_.end(), source.begin(), source.end());
00228 }
00229
00230 private:
00231 Container objects_;
00232 };
00233
00234 template <typename T>
00235 std::ostream& operator<<(std::ostream& os, const Dv::Sql::Set<T>& s) {
00236 for (typename Set<T>::const_iterator i = s.begin(); i!=s.end(); ++i)
00237 os << **i << "\n";
00238 return os;
00239 }
00240 }
00241 }
00242 #endif