Web Wispers 1.2.2 C++ Unit Test Coverage
Current view: top level - ssrc/wispers/group_session - GroupSessionDatabase.h (source / functions) Hit Total Coverage
Test: Web Wispers 1.2.2 C++ Unit Tests Lines: 72 72 100.0 %
Date: 2012-04-09 Functions: 23 23 100.0 %
Branches: 89 178 50.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright 2006-2009 Savarese Software Research Corporation
       3                 :            :  *
       4                 :            :  * Licensed under the Apache License, Version 2.0 (the "License");
       5                 :            :  * you may not use this file except in compliance with the License.
       6                 :            :  * You may obtain a copy of the License at
       7                 :            :  *
       8                 :            :  *     https://www.savarese.com/software/ApacheLicense-2.0
       9                 :            :  *
      10                 :            :  * Unless required by applicable law or agreed to in writing, software
      11                 :            :  * distributed under the License is distributed on an "AS IS" BASIS,
      12                 :            :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      13                 :            :  * See the License for the specific language governing permissions and
      14                 :            :  * limitations under the License.
      15                 :            :  */
      16                 :            : 
      17                 :            : /**
      18                 :            :  * @file
      19                 :            :  * This header defines the GroupSessionDatabase class.
      20                 :            :  */
      21                 :            : 
      22                 :            : #ifndef __SSRC_WSPR_GROUP_SESSION_DATABASE_H
      23                 :            : #define __SSRC_WSPR_GROUP_SESSION_DATABASE_H
      24                 :            : 
      25                 :            : #include <ssrc/wispers/utility/Properties.h>
      26                 :            : #include <ssrc/wispers/utility/AppendToContainer.h>
      27                 :            : #include <ssrc/wispers/group_session/types.h>
      28                 :            : #include <ssrc/wispers/database/DatabaseTransaction.h>
      29                 :            : #include <ssrc/wispers/database/RowOperations.h>
      30                 :            : 
      31                 :            : __BEGIN_NS_SSRC_WSPR_GROUP_SESSION
      32                 :            : 
      33                 :            : using namespace NS_SSRC_WSPR_DATABASE;
      34                 :            : using NS_SSRC_WSPR_UTILITY::Properties;
      35                 :            : using NS_SSRC_WSPR_UTILITY::AppendToContainer;
      36                 :            : 
      37                 :            : /**
      38                 :            :  *
      39                 :            :  */
      40                 :            : class GroupSessionDatabase : public DatabaseWrapper {
      41                 :            : 
      42                 :            :   prepared_statement_ptr _count_sessions;
      43                 :            :   prepared_statement_ptr _count_reservations;
      44                 :            :   prepared_statement_ptr _get_participant_sessions;
      45                 :            :   prepared_statement_ptr _get_observer_sessions;
      46                 :            :   prepared_statement_ptr _find_participants;
      47                 :            :   prepared_statement_ptr _find_observers;
      48                 :            :   prepared_statement_ptr _find_members_for_uid;
      49                 :            :   prepared_statement_ptr _find_members_for_participant;
      50                 :            :   prepared_statement_ptr _remove_observer_by_uid;
      51                 :            :   prepared_statement_ptr _get_member_uids;
      52                 :            :   prepared_statement_ptr _get_member_uids_discard;
      53                 :            :   prepared_statement_ptr _find_expired_gsids;
      54                 :            :   prepared_statement_ptr _get_sessions_by_type;
      55                 :            :   prepared_statement_ptr _remove_reservation;
      56                 :            :   prepared_statement_ptr _remove_expired_sessions;
      57                 :            :   prepared_statement_ptr _get_reservation_participants;
      58                 :            :   prepared_statement_ptr _remove_expired_reservations;
      59                 :            :   prepared_statement_ptr _get_participant_reservations;
      60                 :            :   prepared_statement_ptr _find_sessions;
      61                 :            :   prepared_statement_ptr _is_member;
      62                 :            :   prepared_statement_ptr _count_observers;
      63                 :            :   prepared_statement_ptr _activate_reservation_participants;
      64                 :            : 
      65                 :            : public:
      66                 :            :   typedef RowOperations<GroupSession> GroupSessionOperations;
      67                 :            :   typedef RowOperations<Member> MemberOperations;
      68                 :            :   typedef RowOperations<Reservation> ReservationOperations;
      69                 :            : 
      70                 :            :   GroupSessionOperations group_session_ops;
      71                 :            :   MemberOperations participant_ops;
      72                 :            :   MemberOperations observer_ops;
      73                 :            :   ReservationOperations reservation_ops;
      74                 :            :   MemberOperations reservation_participant_ops;
      75                 :            : 
      76                 :         16 :   GroupSessionDatabase(const string & db_file, const Properties & properties)
      77                 :            :     SSRC_DECL_THROW(DatabaseException) :
      78                 :            :     DatabaseWrapper(db_file),
      79                 :            :   // TODO: figure out a better way...
      80                 :            : #define GSDB_INIT_QUERY(key) \
      81                 :            :     _ ## key(_database.prepare(*properties.get_ptr<string>( \
      82                 :            :       "wspr","config", "group_session", "GroupSessionDatabase", "Query", #key)))
      83                 :            : 
      84         [ +  - ]:         16 :     GSDB_INIT_QUERY(count_sessions),
      85         [ +  - ]:         16 :     GSDB_INIT_QUERY(count_reservations),
      86         [ +  - ]:         16 :     GSDB_INIT_QUERY(get_participant_sessions),
      87         [ +  - ]:         16 :     GSDB_INIT_QUERY(get_observer_sessions),
      88         [ +  - ]:         16 :     GSDB_INIT_QUERY(find_participants),
      89         [ +  - ]:         16 :     GSDB_INIT_QUERY(find_observers),
      90         [ +  - ]:         16 :     GSDB_INIT_QUERY(find_members_for_uid),
      91         [ +  - ]:         16 :     GSDB_INIT_QUERY(find_members_for_participant),
      92         [ +  - ]:         16 :     GSDB_INIT_QUERY(remove_observer_by_uid),
      93         [ +  - ]:         16 :     GSDB_INIT_QUERY(get_member_uids),
      94         [ +  - ]:         16 :     GSDB_INIT_QUERY(get_member_uids_discard),
      95         [ +  - ]:         16 :     GSDB_INIT_QUERY(find_expired_gsids),
      96         [ +  - ]:         16 :     GSDB_INIT_QUERY(get_sessions_by_type),
      97         [ +  - ]:         16 :     GSDB_INIT_QUERY(remove_reservation),
      98         [ +  - ]:         16 :     GSDB_INIT_QUERY(remove_expired_sessions),
      99         [ +  - ]:         16 :     GSDB_INIT_QUERY(get_reservation_participants),
     100         [ +  - ]:         16 :     GSDB_INIT_QUERY(remove_expired_reservations),
     101         [ +  - ]:         16 :     GSDB_INIT_QUERY(get_participant_reservations),
     102         [ +  - ]:         16 :     GSDB_INIT_QUERY(find_sessions),
     103         [ +  - ]:         16 :     GSDB_INIT_QUERY(is_member),
     104         [ +  - ]:         16 :     GSDB_INIT_QUERY(count_observers),
     105         [ +  - ]:         16 :     GSDB_INIT_QUERY(activate_reservation_participants),
     106                 :            : 
     107                 :            : #undef GSDB_INIT_QUERY
     108                 :            :     group_session_ops(_database),
     109                 :            :     participant_ops(_database, DefaultValueBinder(), DefaultValueLoader(),
     110                 :            :                     MemberOperations::ImplicitColumns, "Participant"),
     111                 :            :     observer_ops(_database,
     112                 :            :                  DefaultValueBinder(), DefaultValueLoader(),
     113                 :            :                  MemberOperations::ImplicitColumns, "Observer"),
     114                 :            :     reservation_ops(_database,
     115                 :            :                     DefaultValueBinder(), DefaultValueLoader(),
     116                 :            :                     ReservationOperations::ExplicitColumns),
     117                 :            :     reservation_participant_ops(_database,
     118                 :            :                                 DefaultValueBinder(), DefaultValueLoader(),
     119                 :            :                                 MemberOperations::ImplicitColumns,
     120   [ +  -  +  -  :        368 :                                 "ReservationParticipant")
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     121                 :         16 :   { }
     122                 :            : 
     123   [ +  -  +  -  :         32 :   virtual ~GroupSessionDatabase() { }
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   -  + ]
     124                 :            : 
     125                 :            :   static unsigned int remove_member_by_uid(PreparedStatement & query,
     126                 :            :                                            const uid_type member)
     127                 :            :     SSRC_DECL_THROW(DatabaseException)
     128                 :            :   {
     129                 :            :     return query.execute(member).changes;
     130                 :            :   }
     131                 :            : 
     132                 :            :   template<typename iterator_type>
     133                 :            :   static unsigned int remove_members_by_uid(PreparedStatement & query,
     134                 :            :                                             const iterator_type & begin,
     135                 :            :                                             const iterator_type & end)
     136                 :            :     SSRC_DECL_THROW(DatabaseException)
     137                 :            :   {
     138                 :            :     iterator_type it = begin;
     139                 :            :     unsigned int total = 0;
     140                 :            : 
     141                 :            :     while(it != end) {
     142                 :            :       total+=remove_member_by_uid(query, *it);
     143                 :            :       ++it;
     144                 :            :     }
     145                 :            : 
     146                 :            :     return total;
     147                 :            :   }
     148                 :            : 
     149                 :          2 :   unsigned int count_sessions() const SSRC_DECL_THROW(DatabaseException) {
     150                 :            :     unsigned int result =
     151         [ +  - ]:          2 :       _count_sessions->execute().result_set->value<unsigned int>();
     152                 :          2 :     _count_sessions->reset();
     153                 :          2 :     return result;
     154                 :            :   }
     155                 :            : 
     156                 :          2 :   unsigned int count_reservations() const SSRC_DECL_THROW(DatabaseException) {
     157                 :            :     unsigned int result =
     158         [ +  - ]:          2 :       _count_reservations->execute().result_set->value<unsigned int>();
     159                 :          2 :     _count_reservations->reset();
     160                 :          2 :     return result;
     161                 :            :   }
     162                 :            : 
     163                 :            :   template<typename functor>
     164                 :            :   unsigned int
     165                 :            :   for_each_participant(const functor & apply, const gsid_type gsid) const
     166                 :            :     SSRC_DECL_THROW(DatabaseException)
     167                 :            :   {
     168                 :            :     return _find_participants->for_each(apply, gsid);
     169                 :            :   }
     170                 :            : 
     171                 :            :   template<typename container_type>
     172                 :            :   unsigned int
     173                 :            :   find_participants(container_type & results, const gsid_type gsid) const
     174                 :            :     SSRC_DECL_THROW(DatabaseException)
     175                 :            :   {
     176                 :            :     return
     177                 :            :       for_each_participant(AppendToContainer<container_type>(results), gsid);
     178                 :            :   }
     179                 :            : 
     180                 :            :   template<typename functor>
     181                 :            :   unsigned int
     182                 :          6 :   for_each_participant_session(const functor & apply,
     183                 :            :                                const uid_type uid) const
     184                 :            :     SSRC_DECL_THROW(DatabaseException)
     185                 :            :   {
     186                 :          6 :     return _get_participant_sessions->for_each(apply, uid);
     187                 :            :   }
     188                 :            : 
     189                 :            :   template<typename container_type>
     190                 :            :   unsigned int
     191                 :          6 :   get_participant_sessions(container_type & results,
     192                 :            :                            const uid_type uid) const
     193                 :            :     SSRC_DECL_THROW(DatabaseException)
     194                 :            :   {
     195                 :          6 :     return for_each_participant_session(AppendToContainer<container_type>(results), uid);
     196                 :            :   }
     197                 :            : 
     198                 :            :   template<typename functor>
     199                 :            :   unsigned int
     200                 :          4 :   for_each_observer_session(const functor & apply,
     201                 :            :                             const uid_type uid) const
     202                 :            :     SSRC_DECL_THROW(DatabaseException)
     203                 :            :   {
     204                 :          4 :     return _get_observer_sessions->for_each(apply, uid);
     205                 :            :   }
     206                 :            : 
     207                 :            :   template<typename container_type>
     208                 :            :   unsigned int
     209                 :          4 :   get_observer_sessions(container_type & results,
     210                 :            :                         const uid_type uid) const
     211                 :            :     SSRC_DECL_THROW(DatabaseException)
     212                 :            :   {
     213                 :            :     return
     214                 :          4 :       for_each_observer_session(AppendToContainer<container_type>(results), uid);
     215                 :            :   }
     216                 :            : 
     217                 :            :   template<typename functor>
     218                 :            :   unsigned int
     219                 :          1 :   for_each_member_for_uid(const functor & apply, const uid_type uid) const
     220                 :            :     SSRC_DECL_THROW(DatabaseException)
     221                 :            :   {
     222                 :          1 :     return _find_members_for_uid->for_each(apply, uid);
     223                 :            :   }
     224                 :            : 
     225                 :            :   template<typename container_type>
     226                 :            :   unsigned int
     227                 :          1 :   find_members_for_uid(container_type & results, const uid_type uid) const
     228                 :            :     SSRC_DECL_THROW(DatabaseException)
     229                 :            :   {
     230                 :          1 :     return for_each_member_for_uid(AppendToContainer<container_type>(results), uid);
     231                 :            :   }
     232                 :            : 
     233                 :            :   template<typename functor>
     234                 :            :   unsigned int for_each_member_for_participant(const functor & apply,
     235                 :            :                                                const uid_type uid) const
     236                 :            :     SSRC_DECL_THROW(DatabaseException)
     237                 :            :   {
     238                 :            :     return _find_members_for_participant->for_each(apply, uid);
     239                 :            :   }
     240                 :            : 
     241                 :            :   template<typename container_type>
     242                 :            :   unsigned int find_members_for_participant(container_type & results,
     243                 :            :                                             const uid_type uid) const
     244                 :            :     SSRC_DECL_THROW(DatabaseException)
     245                 :            :   {
     246                 :            :     return for_each_member_for_participant(AppendToContainer<container_type>(results), uid);
     247                 :            :   }
     248                 :            : 
     249                 :            :   template<typename functor>
     250                 :            :   unsigned int
     251                 :            :   for_each_observer(const functor & apply, const gsid_type gsid) const
     252                 :            :     SSRC_DECL_THROW(DatabaseException)
     253                 :            :   {
     254                 :            :     return _find_observers->for_each(apply, gsid);
     255                 :            :   }
     256                 :            : 
     257                 :            :   template<typename container_type>
     258                 :            :   unsigned int
     259                 :            :   find_observers(container_type & results, const gsid_type gsid) const
     260                 :            :     SSRC_DECL_THROW(DatabaseException)
     261                 :            :   {
     262                 :            :     return for_each_observer(AppendToContainer<container_type>(results), gsid);
     263                 :            :   }
     264                 :            : 
     265                 :            :   unsigned int remove_observer_by_uid(const uid_type observer) {
     266                 :            :     return remove_member_by_uid(*_remove_observer_by_uid, observer);
     267                 :            :   }
     268                 :            : 
     269                 :            :   template<typename iterator_type>
     270                 :            :   unsigned int remove_observers_by_uid(const iterator_type & begin,
     271                 :            :                                        const iterator_type & end)
     272                 :            :   {
     273                 :            :     return remove_members_by_uid(*_remove_observer_by_uid, begin, end);
     274                 :            :   }
     275                 :            : 
     276                 :            :   template<typename functor>
     277                 :            :   unsigned int
     278                 :         11 :   for_each_member_uid(const functor & apply, const gsid_type gsid) const
     279                 :            :     SSRC_DECL_THROW(DatabaseException)
     280                 :            :   {
     281                 :         11 :     return _get_member_uids->for_each(apply, gsid);
     282                 :            :   }
     283                 :            : 
     284                 :            :   template<typename container_type>
     285                 :            :   unsigned int
     286                 :         11 :   get_member_uids(container_type & results, const gsid_type gsid) const
     287                 :            :     SSRC_DECL_THROW(DatabaseException)
     288                 :            :   {
     289                 :            :     return
     290                 :         11 :       for_each_member_uid(AppendToContainer<container_type>(results), gsid);
     291                 :            :   }
     292                 :            : 
     293                 :            :   template<typename functor>
     294                 :            :   unsigned int
     295                 :            :   for_each_member_uid_discard(const functor & apply,
     296                 :            :                               const gsid_type gsid,
     297                 :            :                               const uid_type discard) const
     298                 :            :     SSRC_DECL_THROW(DatabaseException)
     299                 :            :   {
     300                 :            :     return _get_member_uids_discard->for_each(apply, gsid, discard);
     301                 :            :   }
     302                 :            : 
     303                 :            :   template<typename container_type>
     304                 :            :   unsigned int
     305                 :            :   get_member_uids_discard(container_type & results,
     306                 :            :                       const gsid_type gsid, const uid_type discard) const
     307                 :            :     SSRC_DECL_THROW(DatabaseException)
     308                 :            :   {
     309                 :            :     return
     310                 :            :       for_each_member_uid_discard(AppendToContainer<container_type>(results),
     311                 :            :                                   gsid, discard);
     312                 :            :   }
     313                 :            : 
     314                 :            :   template<typename functor>
     315                 :            :   unsigned int
     316                 :          4 :   for_each_expired_gsid(const functor & apply, const sec_type expires) const
     317                 :            :     SSRC_DECL_THROW(DatabaseException)
     318                 :            :   {
     319                 :          4 :     return _find_expired_gsids->for_each(apply, expires);
     320                 :            :   }
     321                 :            : 
     322                 :            :   template<typename container_type>
     323                 :            :   unsigned int
     324                 :          4 :   find_expired_gsids(container_type & results, const sec_type expires) const
     325                 :            :     SSRC_DECL_THROW(DatabaseException)
     326                 :            :   {
     327                 :            :     return
     328                 :          4 :       for_each_expired_gsid(AppendToContainer<container_type>(results), expires);
     329                 :            :   }
     330                 :            : 
     331                 :            :   template<typename functor>
     332                 :            :   unsigned int
     333                 :          2 :   for_each_session_by_type(const functor & apply,
     334                 :            :                            const string & type) const
     335                 :            :     SSRC_DECL_THROW(DatabaseException)
     336                 :            :   {
     337                 :          2 :     return _get_sessions_by_type->for_each(apply, type);
     338                 :            :   }
     339                 :            : 
     340                 :            :   template<typename container_type>
     341                 :            :   unsigned int
     342                 :          2 :   get_sessions_by_type(container_type & results,
     343                 :            :                        const string & type) const
     344                 :            :     SSRC_DECL_THROW(DatabaseException)
     345                 :            :   {
     346                 :            :     return
     347                 :          2 :       for_each_session_by_type(AppendToContainer<container_type>(results), type);
     348                 :            :   }
     349                 :            : 
     350                 :          2 :   unsigned int remove_expired_sessions(const sec_type expires)
     351                 :            :     SSRC_DECL_THROW(DatabaseException)
     352                 :            :   {
     353                 :          2 :     return _remove_expired_sessions->execute(expires).changes;
     354                 :            :   }
     355                 :            : 
     356                 :            :   unsigned int remove_reservation(const gsid_type gsid, const uid_type creator)
     357                 :            :     SSRC_DECL_THROW(DatabaseException)
     358                 :            :   {
     359                 :            :     return _remove_reservation->execute(gsid, creator).changes;
     360                 :            :   }
     361                 :            : 
     362                 :            :   template<typename functor>
     363                 :            :   unsigned int
     364                 :          2 :   for_each_reservation_participant(const functor & apply,
     365                 :            :                                    const gsid_type gsid) const
     366                 :            :     SSRC_DECL_THROW(DatabaseException)
     367                 :            :   {
     368                 :          2 :     return _get_reservation_participants->for_each(apply, gsid);
     369                 :            :   }
     370                 :            : 
     371                 :            :   template<typename container_type>
     372                 :            :   unsigned int
     373                 :          2 :   get_reservation_participants(container_type & results,
     374                 :            :                                const gsid_type gsid) const
     375                 :            :     SSRC_DECL_THROW(DatabaseException)
     376                 :            :   {
     377                 :            :     return
     378                 :          2 :       for_each_reservation_participant(AppendToContainer<container_type>(results), gsid);
     379                 :            :   }
     380                 :            : 
     381                 :            :   unsigned int remove_expired_reservations(const sec_type expires)
     382                 :            :     SSRC_DECL_THROW(DatabaseException)
     383                 :            :   {
     384                 :            :     return _remove_expired_reservations->execute(expires).changes;
     385                 :            :   }
     386                 :            : 
     387                 :            :   template<typename functor>
     388                 :            :   unsigned int
     389                 :            :   for_each_participant_reservation(const functor & apply,
     390                 :            :                                    const uid_type uid) const
     391                 :            :     SSRC_DECL_THROW(DatabaseException)
     392                 :            :   {
     393                 :            :     return _get_participant_reservations->for_each(apply, uid);
     394                 :            :   }
     395                 :            : 
     396                 :            :   template<typename container_type>
     397                 :            :   unsigned int
     398                 :            :   get_participant_reservations(container_type & results,
     399                 :            :                                const uid_type uid) const
     400                 :            :     SSRC_DECL_THROW(DatabaseException)
     401                 :            :   {
     402                 :            :     return
     403                 :            :       for_each_participant_reservation(AppendToContainer<container_type>(results), uid);
     404                 :            :   }
     405                 :            : 
     406                 :            :   template<typename functor>
     407                 :          1 :   unsigned int for_each_session(const functor & apply,
     408                 :            :                                      const db_limit_type limit = -1,
     409                 :            :                                      const db_offset_type offset = 0) const
     410                 :            :     SSRC_DECL_THROW(DatabaseException)
     411                 :            :   {
     412                 :          1 :     return _find_sessions->for_each(apply, limit, offset);
     413                 :            :   }
     414                 :            :   
     415                 :            :   template<typename container_type>
     416                 :          1 :   unsigned int find_sessions(container_type & results,
     417                 :            :                              const db_limit_type limit = -1,
     418                 :            :                              const db_offset_type offset = 0) const
     419                 :            :     SSRC_DECL_THROW(DatabaseException)
     420                 :            :   {
     421                 :            :     return
     422                 :          1 :       for_each_session(AppendToContainer<container_type>(results), limit, offset);
     423                 :            :   }
     424                 :            : 
     425                 :          2 :   bool is_member(const uid_type member, const gsid_type gsid) const
     426                 :            :     SSRC_DECL_THROW(DatabaseException)
     427                 :            :   {
     428                 :          2 :     bool result = _is_member->execute(member, gsid).result_set;
     429                 :          2 :     _is_member->reset();
     430                 :          2 :     return result;
     431                 :            :   }
     432                 :            : 
     433                 :            :   unsigned int count_observers(const gsid_type gsid) const
     434                 :            :     SSRC_DECL_THROW(DatabaseException)
     435                 :            :   {
     436                 :            :     unsigned int result =
     437                 :            :       _count_observers->execute(gsid).result_set->value<unsigned int>();
     438                 :            :     _count_observers->reset();
     439                 :            :     return result;
     440                 :            :   }
     441                 :            : 
     442                 :            :   unsigned int activate_reservation_participants(const gsid_type gsid) const
     443                 :            :     SSRC_DECL_THROW(DatabaseException)
     444                 :            :   {
     445                 :            :     return _activate_reservation_participants->execute(gsid).changes;
     446                 :            :   }
     447                 :            : };
     448                 :            : 
     449                 :            : __END_NS_SSRC_WSPR_GROUP_SESSION
     450                 :            : 
     451                 :            : #endif