Savarese Software Research Corporation
MultiRowOperations.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 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_DATABASE_MULTI_ROW_OPERATIONS_H
00023 #define __SSRC_WSPR_DATABASE_MULTI_ROW_OPERATIONS_H
00024 
00025 #include <ssrc/wispers/database/RowOperations.h>
00026 
00027 __BEGIN_NS_SSRC_WSPR_DATABASE
00028 
00029 template<typename Row, typename ValueBinder = DefaultValueBinder,
00030          typename ValueLoader = DefaultValueLoader,
00031          typename RowOps = RowOperations<Row,ValueBinder,ValueLoader> >
00032 struct MultiRowOperations : public RowOps {
00033   typedef RowOps super;
00034 
00035   WISP_IMPORT_T(super, row_type);
00036   WISP_IMPORT_T(super, find_result_type);
00037   WISP_IMPORT_T(super, binder_type);
00038   WISP_IMPORT_T(super, loader_type);
00039 
00040 private:
00041   Database & _db;
00042   const string _table_name;
00043   string _select_columns;
00044   
00045 public:
00046   MultiRowOperations(Database & db,
00047                      const binder_type & binder = DefaultValueBinder(),
00048                      const loader_type & loader = DefaultValueLoader(),
00049                      const bool explicit_columns = super::ImplicitColumns,
00050                      const string & table_name = row_type::table_name()) :
00051     super(db, binder, loader, explicit_columns, table_name),
00052     _db(db), _table_name(table_name),
00053     _select_columns(super::select_columns_expression(explicit_columns))
00054   { }
00055 
00056   prepared_statement_ptr prepare_find_rows(const unsigned int n)
00057     SSRC_DECL_THROW(DatabaseException)
00058   {
00059     using detail::PrimaryKeyCond;
00060     std::ostringstream query;
00061 
00062     query << "SELECT " << _select_columns << " FROM `"
00063           << _table_name << "` WHERE ";
00064 
00065     for(unsigned int i = 0; i < n - 1; ++i) {
00066       PrimaryKeyCond<row_type, row_type::PrimaryKeyCount - 1>::apply((query << "(")) << ") OR ";
00067     }
00068 
00069     PrimaryKeyCond<row_type, row_type::PrimaryKeyCount - 1>::apply((query << "(")) << ")";
00070 
00071     return _db.prepare(query.str());
00072   }
00073 
00074   template<typename Iterator, typename functor>
00075   unsigned int for_each_row(const Iterator & begin, const Iterator & end,
00076                             const unsigned int size,
00077                             const functor &  apply)
00078     SSRC_DECL_THROW(DatabaseException)
00079   {
00080     prepared_statement_ptr && query = prepare_find_rows(size);
00081     typename binder_type::binder_type & binder_ = super::binder.binder(*query);
00082     unsigned int i = 0;
00083 
00084     for(Iterator it = begin; it != end; ++it, i+=row_type::PrimaryKeyCount) {
00085       row_type::bind_primary_key(binder_, *it, i);
00086     }
00087 
00088     return query->for_each(apply);
00089   }
00090 };
00091 
00092 template<typename Row, typename ValueBinder = DefaultValueBinder,
00093          typename ValueLoader = DefaultValueLoader>
00094 struct MultiRowOperationsReadOnly :
00095   public MultiRowOperations<Row, ValueBinder, ValueLoader,
00096                             RowOperationsReadOnly<Row,ValueBinder,ValueLoader> >
00097 {
00098   typedef
00099   MultiRowOperations<Row, ValueBinder, ValueLoader,
00100                      RowOperationsReadOnly<Row,ValueBinder,ValueLoader> >
00101   super;
00102 
00103   WISP_IMPORT_T(super, row_type);
00104   WISP_IMPORT_T(super, find_result_type);
00105   WISP_IMPORT_T(super, binder_type);
00106   WISP_IMPORT_T(super, loader_type);
00107 
00108   MultiRowOperationsReadOnly(Database & db,
00109                              const binder_type & binder = DefaultValueBinder(),
00110                              const loader_type & loader = DefaultValueLoader(),
00111                              const bool explicit_columns =
00112                              super::ImplicitColumns,
00113                              const string & table_name =
00114                              row_type::table_name()) :
00115     super(db, binder, loader, explicit_columns, table_name)
00116   { }
00117 };
00118 
00119 __END_NS_SSRC_WSPR_DATABASE
00120 
00121 #endif

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