2 * Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
20 #include "expression.hpp"
31 template<typename... ColumnTypes>
32 T& select(ColumnTypes&&... cts);
34 template<typename TableType>
39 template<typename... ColumnTypes>
40 T& update(ColumnTypes&&... cts);
42 template<typename... ColumnTypes>
43 T& insert(ColumnTypes&&... cts);
45 template<typename... ColumnTypes>
46 T& remove(ColumnTypes&&... cts);
48 template<typename Expr>
52 template<typename L, typename R>
53 std::string processWhere(And<L,R>& expr);
55 template<typename L, typename R>
56 std::string processWhere(Or<L,R>& expr);
58 template<typename Expr>
59 std::string processWhere(Expr expr);
63 template<typename... ColumnTypes>
64 T& Crud<T>::select(ColumnTypes&&... cts)
66 static_cast<T*>(this)->cache.clear();
68 auto columnNames = static_cast<T*>(this)->getColumnNames(std::forward<ColumnTypes>(cts)...);
69 auto tableNames = static_cast<T*>(this)->getTableNames(std::forward<ColumnTypes>(cts)...);
75 for (const auto& c : columnNames) {
78 if (i++ < columnNames.size() - 1)
85 for (const auto& t : tableNames) {
88 if (i++ < tableNames.size() - 1)
92 static_cast<T*>(this)->cache.emplace_back(ss.str());
94 return *(static_cast<T*>(this));
98 T& Crud<T>::selectAll(void)
100 static_cast<T*>(this)->cache.clear();
102 std::stringstream ss;
103 ss << "SELECT * FROM " << static_cast<T*>(this)->name;
105 static_cast<T*>(this)->cache.emplace_back(ss.str());
107 return *(static_cast<T*>(this));
111 template<typename TableType>
112 T& Crud<T>::selectAll(void)
114 static_cast<T*>(this)->cache.clear();
116 std::stringstream ss;
117 auto tableName = static_cast<T*>(this)->getTableName(TableType());
118 ss << "SELECT * FROM " << tableName;
120 static_cast<T*>(this)->cache.emplace_back(ss.str());
122 return *(static_cast<T*>(this));
126 template<typename... ColumnTypes>
127 T& Crud<T>::update(ColumnTypes&&... cts)
129 static_cast<T*>(this)->cache.clear();
131 auto columnNames = static_cast<T*>(this)->getColumnNames(std::forward<ColumnTypes>(cts)...);
133 std::stringstream ss;
134 ss << "UPDATE " << static_cast<T*>(this)->name << " ";
138 for (const auto& c : columnNames) {
141 if (i++ < columnNames.size() - 1)
145 static_cast<T*>(this)->cache.emplace_back(ss.str());
147 return *(static_cast<T*>(this));
151 template<typename... ColumnTypes>
152 T& Crud<T>::insert(ColumnTypes&&... cts)
154 static_cast<T*>(this)->cache.clear();
156 auto columnNames = static_cast<T*>(this)->getColumnNames(std::forward<ColumnTypes>(cts)...);
158 std::stringstream ss;
159 ss << "INSERT INTO " << static_cast<T*>(this)->name << " (";
161 const int columnCount = columnNames.size();
162 for (int i = 0; i < columnCount; i++) {
163 ss << columnNames[i];
164 if (i < columnCount - 1)
170 for (int i = 0; i < columnCount; i++) {
172 if (i < columnCount - 1)
178 static_cast<T*>(this)->cache.emplace_back(ss.str());
180 return *(static_cast<T*>(this));
184 template<typename... ColumnTypes>
185 T& Crud<T>::remove(ColumnTypes&&... cts)
187 static_cast<T*>(this)->cache.clear();
189 auto columnNames = static_cast<T*>(this)->getColumnNames(std::forward<ColumnTypes>(cts)...);
191 std::stringstream ss;
192 ss << "DELETE FROM " << static_cast<T*>(this)->name;
194 static_cast<T*>(this)->cache.emplace_back(ss.str());
196 return *(static_cast<T*>(this));
200 template<typename Expr>
201 T& Crud<T>::where(Expr expr)
203 std::stringstream ss;
204 ss << "WHERE " << this->processWhere(expr);
206 static_cast<T*>(this)->cache.emplace_back(ss.str());
208 return *(static_cast<T*>(this));
212 template<typename L, typename R>
213 std::string Crud<T>::processWhere(And<L,R>& expr)
215 std::stringstream ss;
216 ss << this->processWhere(expr.l) << " ";
217 ss << static_cast<std::string>(expr) << " ";
218 ss << this->processWhere(expr.r);
224 template<typename L, typename R>
225 std::string Crud<T>::processWhere(Or<L,R>& expr)
227 std::stringstream ss;
228 ss << this->processWhere(expr.l) << " ";
229 ss << static_cast<std::string>(expr) << " ";
230 ss << this->processWhere(expr.r);
236 template<typename Expr>
237 std::string Crud<T>::processWhere(Expr expr)
239 std::stringstream ss;
240 ss << static_cast<T*>(this)->getColumnName(expr.l);
241 ss << " " << std::string(expr) << " ?";