Branch data Line data Source code
1 : : /*
2 : : * Copyright 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 MultiRowOperations class.
20 : : */
21 : :
22 : : #ifndef __SSRC_WSPR_DATABASE_MULTI_ROW_OPERATIONS_H
23 : : #define __SSRC_WSPR_DATABASE_MULTI_ROW_OPERATIONS_H
24 : :
25 : : #include <ssrc/wispers/database/RowOperations.h>
26 : :
27 : : __BEGIN_NS_SSRC_WSPR_DATABASE
28 : :
29 : : template<typename Row, typename ValueBinder = DefaultValueBinder,
30 : : typename ValueLoader = DefaultValueLoader,
31 : : typename RowOps = RowOperations<Row,ValueBinder,ValueLoader> >
32 [ + - + - : 4 : struct MultiRowOperations : public RowOps {
+ - + - ]
33 : : typedef RowOps super;
34 : :
35 : : WISP_IMPORT_T(super, row_type);
36 : : WISP_IMPORT_T(super, find_result_type);
37 : : WISP_IMPORT_T(super, binder_type);
38 : : WISP_IMPORT_T(super, loader_type);
39 : :
40 : : private:
41 : : Database & _db;
42 : : const string _table_name;
43 : : string _select_columns;
44 : :
45 : : public:
46 : 4 : MultiRowOperations(Database & db,
47 : : const binder_type & binder = DefaultValueBinder(),
48 : : const loader_type & loader = DefaultValueLoader(),
49 : : const bool explicit_columns = super::ImplicitColumns,
50 : : const string & table_name = row_type::table_name()) :
51 : : super(db, binder, loader, explicit_columns, table_name),
52 : : _db(db), _table_name(table_name),
53 [ + - + - : 4 : _select_columns(super::select_columns_expression(explicit_columns))
+ - + - ]
54 : 4 : { }
55 : :
56 : 4 : prepared_statement_ptr prepare_find_rows(const unsigned int n)
57 : : SSRC_DECL_THROW(DatabaseException)
58 : : {
59 : : using detail::PrimaryKeyCond;
60 : 8 : std::ostringstream query;
61 : :
62 [ + - + - : 4 : query << "SELECT " << _select_columns << " FROM `"
+ - + - +
- + - + -
+ - + - +
- ]
63 : : << _table_name << "` WHERE ";
64 : :
65 [ - + + + ]: 8 : for(unsigned int i = 0; i < n - 1; ++i) {
66 [ # # # # : 4 : PrimaryKeyCond<row_type, row_type::PrimaryKeyCount - 1>::apply((query << "(")) << ") OR ";
# # + - +
- + - ]
67 : : }
68 : :
69 [ + - + - : 4 : PrimaryKeyCond<row_type, row_type::PrimaryKeyCount - 1>::apply((query << "(")) << ")";
+ - + - +
- + - ]
70 : :
71 [ + - + - : 8 : return _db.prepare(query.str());
+ - + - +
- + - ]
72 : : }
73 : :
74 : : template<typename Iterator, typename functor>
75 : 4 : unsigned int for_each_row(const Iterator & begin, const Iterator & end,
76 : : const unsigned int size,
77 : : const functor & apply)
78 : : SSRC_DECL_THROW(DatabaseException)
79 : : {
80 : 8 : prepared_statement_ptr && query = prepare_find_rows(size);
81 [ + - + - ]: 4 : typename binder_type::binder_type & binder_ = super::binder.binder(*query);
82 : 4 : unsigned int i = 0;
83 : :
84 [ + + + + ]: 12 : for(Iterator it = begin; it != end; ++it, i+=row_type::PrimaryKeyCount) {
85 [ + - + - ]: 8 : row_type::bind_primary_key(binder_, *it, i);
86 : : }
87 : :
88 [ + - + - : 8 : return query->for_each(apply);
+ - + - ]
89 : : }
90 : : };
91 : :
92 : : template<typename Row, typename ValueBinder = DefaultValueBinder,
93 : : typename ValueLoader = DefaultValueLoader>
94 : : struct MultiRowOperationsReadOnly :
95 : : public MultiRowOperations<Row, ValueBinder, ValueLoader,
96 : : RowOperationsReadOnly<Row,ValueBinder,ValueLoader> >
97 : : {
98 : : typedef
99 : : MultiRowOperations<Row, ValueBinder, ValueLoader,
100 : : RowOperationsReadOnly<Row,ValueBinder,ValueLoader> >
101 : : super;
102 : :
103 : : WISP_IMPORT_T(super, row_type);
104 : : WISP_IMPORT_T(super, find_result_type);
105 : : WISP_IMPORT_T(super, binder_type);
106 : : WISP_IMPORT_T(super, loader_type);
107 : :
108 : : MultiRowOperationsReadOnly(Database & db,
109 : : const binder_type & binder = DefaultValueBinder(),
110 : : const loader_type & loader = DefaultValueLoader(),
111 : : const bool explicit_columns =
112 : : super::ImplicitColumns,
113 : : const string & table_name =
114 : : row_type::table_name()) :
115 : : super(db, binder, loader, explicit_columns, table_name)
116 : : { }
117 : : };
118 : :
119 : : __END_NS_SSRC_WSPR_DATABASE
120 : :
121 : : #endif
|