Savarese Software Research Corporation
GroupSessionDatabase.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2006-2009 Savarese Software Research Corporation
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     https://www.savarese.com/software/ApacheLicense-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00022 #ifndef __SSRC_WSPR_GROUP_SESSION_DATABASE_H
00023 #define __SSRC_WSPR_GROUP_SESSION_DATABASE_H
00024 
00025 #include <ssrc/wispers/utility/Properties.h>
00026 #include <ssrc/wispers/utility/AppendToContainer.h>
00027 #include <ssrc/wispers/group_session/types.h>
00028 #include <ssrc/wispers/database/DatabaseTransaction.h>
00029 #include <ssrc/wispers/database/RowOperations.h>
00030 
00031 __BEGIN_NS_SSRC_WSPR_GROUP_SESSION
00032 
00033 using namespace NS_SSRC_WSPR_DATABASE;
00034 using NS_SSRC_WSPR_UTILITY::Properties;
00035 using NS_SSRC_WSPR_UTILITY::AppendToContainer;
00036 
00040 class GroupSessionDatabase : public DatabaseWrapper {
00041 
00042   prepared_statement_ptr _count_sessions;
00043   prepared_statement_ptr _count_reservations;
00044   prepared_statement_ptr _get_participant_sessions;
00045   prepared_statement_ptr _get_observer_sessions;
00046   prepared_statement_ptr _find_participants;
00047   prepared_statement_ptr _find_observers;
00048   prepared_statement_ptr _find_members_for_uid;
00049   prepared_statement_ptr _find_members_for_participant;
00050   prepared_statement_ptr _remove_observer_by_uid;
00051   prepared_statement_ptr _get_member_uids;
00052   prepared_statement_ptr _get_member_uids_discard;
00053   prepared_statement_ptr _find_expired_gsids;
00054   prepared_statement_ptr _get_sessions_by_type;
00055   prepared_statement_ptr _remove_reservation;
00056   prepared_statement_ptr _remove_expired_sessions;
00057   prepared_statement_ptr _get_reservation_participants;
00058   prepared_statement_ptr _remove_expired_reservations;
00059   prepared_statement_ptr _get_participant_reservations;
00060   prepared_statement_ptr _find_sessions;
00061   prepared_statement_ptr _is_member;
00062   prepared_statement_ptr _count_observers;
00063   prepared_statement_ptr _activate_reservation_participants;
00064 
00065 public:
00066   typedef RowOperations<GroupSession> GroupSessionOperations;
00067   typedef RowOperations<Member> MemberOperations;
00068   typedef RowOperations<Reservation> ReservationOperations;
00069 
00070   GroupSessionOperations group_session_ops;
00071   MemberOperations participant_ops;
00072   MemberOperations observer_ops;
00073   ReservationOperations reservation_ops;
00074   MemberOperations reservation_participant_ops;
00075 
00076   GroupSessionDatabase(const string & db_file, const Properties & properties)
00077     SSRC_DECL_THROW(DatabaseException) :
00078     DatabaseWrapper(db_file),
00079   // TODO: figure out a better way...
00080 #define GSDB_INIT_QUERY(key) \
00081     _ ## key(_database.prepare(*properties.get_ptr<string>( \
00082       "wspr","config", "group_session", "GroupSessionDatabase", "Query", #key)))
00083 
00084     GSDB_INIT_QUERY(count_sessions),
00085     GSDB_INIT_QUERY(count_reservations),
00086     GSDB_INIT_QUERY(get_participant_sessions),
00087     GSDB_INIT_QUERY(get_observer_sessions),
00088     GSDB_INIT_QUERY(find_participants),
00089     GSDB_INIT_QUERY(find_observers),
00090     GSDB_INIT_QUERY(find_members_for_uid),
00091     GSDB_INIT_QUERY(find_members_for_participant),
00092     GSDB_INIT_QUERY(remove_observer_by_uid),
00093     GSDB_INIT_QUERY(get_member_uids),
00094     GSDB_INIT_QUERY(get_member_uids_discard),
00095     GSDB_INIT_QUERY(find_expired_gsids),
00096     GSDB_INIT_QUERY(get_sessions_by_type),
00097     GSDB_INIT_QUERY(remove_reservation),
00098     GSDB_INIT_QUERY(remove_expired_sessions),
00099     GSDB_INIT_QUERY(get_reservation_participants),
00100     GSDB_INIT_QUERY(remove_expired_reservations),
00101     GSDB_INIT_QUERY(get_participant_reservations),
00102     GSDB_INIT_QUERY(find_sessions),
00103     GSDB_INIT_QUERY(is_member),
00104     GSDB_INIT_QUERY(count_observers),
00105     GSDB_INIT_QUERY(activate_reservation_participants),
00106 
00107 #undef GSDB_INIT_QUERY
00108     group_session_ops(_database),
00109     participant_ops(_database, DefaultValueBinder(), DefaultValueLoader(),
00110                     MemberOperations::ImplicitColumns, "Participant"),
00111     observer_ops(_database,
00112                  DefaultValueBinder(), DefaultValueLoader(),
00113                  MemberOperations::ImplicitColumns, "Observer"),
00114     reservation_ops(_database,
00115                     DefaultValueBinder(), DefaultValueLoader(),
00116                     ReservationOperations::ExplicitColumns),
00117     reservation_participant_ops(_database,
00118                                 DefaultValueBinder(), DefaultValueLoader(),
00119                                 MemberOperations::ImplicitColumns,
00120                                 "ReservationParticipant")
00121   { }
00122 
00123   virtual ~GroupSessionDatabase() { }
00124 
00125   static unsigned int remove_member_by_uid(PreparedStatement & query,
00126                                            const uid_type member)
00127     SSRC_DECL_THROW(DatabaseException)
00128   {
00129     return query.execute(member).changes;
00130   }
00131 
00132   template<typename iterator_type>
00133   static unsigned int remove_members_by_uid(PreparedStatement & query,
00134                                             const iterator_type & begin,
00135                                             const iterator_type & end)
00136     SSRC_DECL_THROW(DatabaseException)
00137   {
00138     iterator_type it = begin;
00139     unsigned int total = 0;
00140 
00141     while(it != end) {
00142       total+=remove_member_by_uid(query, *it);
00143       ++it;
00144     }
00145 
00146     return total;
00147   }
00148 
00149   unsigned int count_sessions() const SSRC_DECL_THROW(DatabaseException) {
00150     unsigned int result =
00151       _count_sessions->execute().result_set->value<unsigned int>();
00152     _count_sessions->reset();
00153     return result;
00154   }
00155 
00156   unsigned int count_reservations() const SSRC_DECL_THROW(DatabaseException) {
00157     unsigned int result =
00158       _count_reservations->execute().result_set->value<unsigned int>();
00159     _count_reservations->reset();
00160     return result;
00161   }
00162 
00163   template<typename functor>
00164   unsigned int
00165   for_each_participant(const functor & apply, const gsid_type gsid) const
00166     SSRC_DECL_THROW(DatabaseException)
00167   {
00168     return _find_participants->for_each(apply, gsid);
00169   }
00170 
00171   template<typename container_type>
00172   unsigned int
00173   find_participants(container_type & results, const gsid_type gsid) const
00174     SSRC_DECL_THROW(DatabaseException)
00175   {
00176     return
00177       for_each_participant(AppendToContainer<container_type>(results), gsid);
00178   }
00179 
00180   template<typename functor>
00181   unsigned int
00182   for_each_participant_session(const functor & apply,
00183                                const uid_type uid) const
00184     SSRC_DECL_THROW(DatabaseException)
00185   {
00186     return _get_participant_sessions->for_each(apply, uid);
00187   }
00188 
00189   template<typename container_type>
00190   unsigned int
00191   get_participant_sessions(container_type & results,
00192                            const uid_type uid) const
00193     SSRC_DECL_THROW(DatabaseException)
00194   {
00195     return for_each_participant_session(AppendToContainer<container_type>(results), uid);
00196   }
00197 
00198   template<typename functor>
00199   unsigned int
00200   for_each_observer_session(const functor & apply,
00201                             const uid_type uid) const
00202     SSRC_DECL_THROW(DatabaseException)
00203   {
00204     return _get_observer_sessions->for_each(apply, uid);
00205   }
00206 
00207   template<typename container_type>
00208   unsigned int
00209   get_observer_sessions(container_type & results,
00210                         const uid_type uid) const
00211     SSRC_DECL_THROW(DatabaseException)
00212   {
00213     return
00214       for_each_observer_session(AppendToContainer<container_type>(results), uid);
00215   }
00216 
00217   template<typename functor>
00218   unsigned int
00219   for_each_member_for_uid(const functor & apply, const uid_type uid) const
00220     SSRC_DECL_THROW(DatabaseException)
00221   {
00222     return _find_members_for_uid->for_each(apply, uid);
00223   }
00224 
00225   template<typename container_type>
00226   unsigned int
00227   find_members_for_uid(container_type & results, const uid_type uid) const
00228     SSRC_DECL_THROW(DatabaseException)
00229   {
00230     return for_each_member_for_uid(AppendToContainer<container_type>(results), uid);
00231   }
00232 
00233   template<typename functor>
00234   unsigned int for_each_member_for_participant(const functor & apply,
00235                                                const uid_type uid) const
00236     SSRC_DECL_THROW(DatabaseException)
00237   {
00238     return _find_members_for_participant->for_each(apply, uid);
00239   }
00240 
00241   template<typename container_type>
00242   unsigned int find_members_for_participant(container_type & results,
00243                                             const uid_type uid) const
00244     SSRC_DECL_THROW(DatabaseException)
00245   {
00246     return for_each_member_for_participant(AppendToContainer<container_type>(results), uid);
00247   }
00248 
00249   template<typename functor>
00250   unsigned int
00251   for_each_observer(const functor & apply, const gsid_type gsid) const
00252     SSRC_DECL_THROW(DatabaseException)
00253   {
00254     return _find_observers->for_each(apply, gsid);
00255   }
00256 
00257   template<typename container_type>
00258   unsigned int
00259   find_observers(container_type & results, const gsid_type gsid) const
00260     SSRC_DECL_THROW(DatabaseException)
00261   {
00262     return for_each_observer(AppendToContainer<container_type>(results), gsid);
00263   }
00264 
00265   unsigned int remove_observer_by_uid(const uid_type observer) {
00266     return remove_member_by_uid(*_remove_observer_by_uid, observer);
00267   }
00268 
00269   template<typename iterator_type>
00270   unsigned int remove_observers_by_uid(const iterator_type & begin,
00271                                        const iterator_type & end)
00272   {
00273     return remove_members_by_uid(*_remove_observer_by_uid, begin, end);
00274   }
00275 
00276   template<typename functor>
00277   unsigned int
00278   for_each_member_uid(const functor & apply, const gsid_type gsid) const
00279     SSRC_DECL_THROW(DatabaseException)
00280   {
00281     return _get_member_uids->for_each(apply, gsid);
00282   }
00283 
00284   template<typename container_type>
00285   unsigned int
00286   get_member_uids(container_type & results, const gsid_type gsid) const
00287     SSRC_DECL_THROW(DatabaseException)
00288   {
00289     return
00290       for_each_member_uid(AppendToContainer<container_type>(results), gsid);
00291   }
00292 
00293   template<typename functor>
00294   unsigned int
00295   for_each_member_uid_discard(const functor & apply,
00296                               const gsid_type gsid,
00297                               const uid_type discard) const
00298     SSRC_DECL_THROW(DatabaseException)
00299   {
00300     return _get_member_uids_discard->for_each(apply, gsid, discard);
00301   }
00302 
00303   template<typename container_type>
00304   unsigned int
00305   get_member_uids_discard(container_type & results,
00306                       const gsid_type gsid, const uid_type discard) const
00307     SSRC_DECL_THROW(DatabaseException)
00308   {
00309     return
00310       for_each_member_uid_discard(AppendToContainer<container_type>(results),
00311                                   gsid, discard);
00312   }
00313 
00314   template<typename functor>
00315   unsigned int
00316   for_each_expired_gsid(const functor & apply, const sec_type expires) const
00317     SSRC_DECL_THROW(DatabaseException)
00318   {
00319     return _find_expired_gsids->for_each(apply, expires);
00320   }
00321 
00322   template<typename container_type>
00323   unsigned int
00324   find_expired_gsids(container_type & results, const sec_type expires) const
00325     SSRC_DECL_THROW(DatabaseException)
00326   {
00327     return
00328       for_each_expired_gsid(AppendToContainer<container_type>(results), expires);
00329   }
00330 
00331   template<typename functor>
00332   unsigned int
00333   for_each_session_by_type(const functor & apply,
00334                            const string & type) const
00335     SSRC_DECL_THROW(DatabaseException)
00336   {
00337     return _get_sessions_by_type->for_each(apply, type);
00338   }
00339 
00340   template<typename container_type>
00341   unsigned int
00342   get_sessions_by_type(container_type & results,
00343                        const string & type) const
00344     SSRC_DECL_THROW(DatabaseException)
00345   {
00346     return
00347       for_each_session_by_type(AppendToContainer<container_type>(results), type);
00348   }
00349 
00350   unsigned int remove_expired_sessions(const sec_type expires)
00351     SSRC_DECL_THROW(DatabaseException)
00352   {
00353     return _remove_expired_sessions->execute(expires).changes;
00354   }
00355 
00356   unsigned int remove_reservation(const gsid_type gsid, const uid_type creator)
00357     SSRC_DECL_THROW(DatabaseException)
00358   {
00359     return _remove_reservation->execute(gsid, creator).changes;
00360   }
00361 
00362   template<typename functor>
00363   unsigned int
00364   for_each_reservation_participant(const functor & apply,
00365                                    const gsid_type gsid) const
00366     SSRC_DECL_THROW(DatabaseException)
00367   {
00368     return _get_reservation_participants->for_each(apply, gsid);
00369   }
00370 
00371   template<typename container_type>
00372   unsigned int
00373   get_reservation_participants(container_type & results,
00374                                const gsid_type gsid) const
00375     SSRC_DECL_THROW(DatabaseException)
00376   {
00377     return
00378       for_each_reservation_participant(AppendToContainer<container_type>(results), gsid);
00379   }
00380 
00381   unsigned int remove_expired_reservations(const sec_type expires)
00382     SSRC_DECL_THROW(DatabaseException)
00383   {
00384     return _remove_expired_reservations->execute(expires).changes;
00385   }
00386 
00387   template<typename functor>
00388   unsigned int
00389   for_each_participant_reservation(const functor & apply,
00390                                    const uid_type uid) const
00391     SSRC_DECL_THROW(DatabaseException)
00392   {
00393     return _get_participant_reservations->for_each(apply, uid);
00394   }
00395 
00396   template<typename container_type>
00397   unsigned int
00398   get_participant_reservations(container_type & results,
00399                                const uid_type uid) const
00400     SSRC_DECL_THROW(DatabaseException)
00401   {
00402     return
00403       for_each_participant_reservation(AppendToContainer<container_type>(results), uid);
00404   }
00405 
00406   template<typename functor>
00407   unsigned int for_each_session(const functor & apply,
00408                                      const db_limit_type limit = -1,
00409                                      const db_offset_type offset = 0) const
00410     SSRC_DECL_THROW(DatabaseException)
00411   {
00412     return _find_sessions->for_each(apply, limit, offset);
00413   }
00414   
00415   template<typename container_type>
00416   unsigned int find_sessions(container_type & results,
00417                              const db_limit_type limit = -1,
00418                              const db_offset_type offset = 0) const
00419     SSRC_DECL_THROW(DatabaseException)
00420   {
00421     return
00422       for_each_session(AppendToContainer<container_type>(results), limit, offset);
00423   }
00424 
00425   bool is_member(const uid_type member, const gsid_type gsid) const
00426     SSRC_DECL_THROW(DatabaseException)
00427   {
00428     bool result = _is_member->execute(member, gsid).result_set;
00429     _is_member->reset();
00430     return result;
00431   }
00432 
00433   unsigned int count_observers(const gsid_type gsid) const
00434     SSRC_DECL_THROW(DatabaseException)
00435   {
00436     unsigned int result =
00437       _count_observers->execute(gsid).result_set->value<unsigned int>();
00438     _count_observers->reset();
00439     return result;
00440   }
00441 
00442   unsigned int activate_reservation_participants(const gsid_type gsid) const
00443     SSRC_DECL_THROW(DatabaseException)
00444   {
00445     return _activate_reservation_participants->execute(gsid).changes;
00446   }
00447 };
00448 
00449 __END_NS_SSRC_WSPR_GROUP_SESSION
00450 
00451 #endif

Savarese Software Research Corporation
Copyright © 2006-2011 Savarese Software Research Corporation. All rights reserved.