From: sangwan.kwon Date: Mon, 15 Jan 2018 06:51:42 +0000 (+0900) Subject: Support multiple condition on where clause X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fheads%2Fref%2Fdrafts%2Ftizen;p=platform%2Fcore%2Fsecurity%2Fklay.git Support multiple condition on where clause Change-Id: I0adc9d253389e64a4cc8679684793df444746343 Signed-off-by: sangwan.kwon --- diff --git a/include/klay/db/query-builder/expression.hxx b/include/klay/db/query-builder/expression.hxx index 2957ef8..2606efd 100644 --- a/include/klay/db/query-builder/expression.hxx +++ b/include/klay/db/query-builder/expression.hxx @@ -3,8 +3,34 @@ namespace qxx { namespace condition { -template -struct Binary { +struct Base {}; + +template +struct And : public Base { + L l; + R r; + + And(L l, R r) : l(l), r(r) {} + operator std::string() const + { + return "AND"; + } +}; + +template +struct Or : public Base { + L l; + R r; + + Or(L l, R r) : l(l), r(r) {} + operator std::string() const + { + return "OR"; + } +}; + +template +struct Binary : public Base { L l; R r; @@ -13,32 +39,36 @@ struct Binary { } // namespace condition -template +template struct Expression { Type value; }; -template -Expression expr(Type value) { +template +Expression expr(Type value) +{ return {value}; } -template +template struct Lesser : public condition::Binary { using condition::Binary::Binary; - operator std::string() const { + operator std::string() const + { return "<"; } }; -template -Lesser operator<(Expression expr, R r) { +template +Lesser operator<(Expression expr, R r) +{ return {expr.value, r}; } -template -struct Equal : public condition::Binary { +template +struct Equal : public condition::Binary +{ using condition::Binary::Binary; operator std::string() const { @@ -46,13 +76,15 @@ struct Equal : public condition::Binary { } }; -template -Equal operator==(Expression expr, R r) { +template +Equal operator==(Expression expr, R r) +{ return {expr.value, r}; } -template -struct Greater : public condition::Binary { +template +struct Greater : public condition::Binary +{ using condition::Binary::Binary; operator std::string() const { @@ -60,9 +92,28 @@ struct Greater : public condition::Binary { } }; -template -Greater operator>(Expression expr, R r) { +template +Greater operator>(Expression expr, R r) +{ return {expr.value, r}; } +template::value + && std::is_base_of::value>::type> +condition::And operator &&(const L& l, const R& r) +{ + return {l, r}; +} + +template::value + && std::is_base_of::value>::type> +condition::Or operator ||(const L& l, const R& r) +{ + return {l, r}; +} + } // namespace qxx diff --git a/include/klay/db/query-builder/table.hxx b/include/klay/db/query-builder/table.hxx index bd7cebb..d08f7d2 100644 --- a/include/klay/db/query-builder/table.hxx +++ b/include/klay/db/query-builder/table.hxx @@ -3,6 +3,7 @@ #include "column.hxx" #include "table-impl.hxx" #include "tuple-helper.hxx" +#include "expression.hxx" #include #include @@ -58,6 +59,35 @@ private: } }; + template + std::string processWhere(condition::And& expr) { + std::stringstream ss; + ss << this->processWhere(expr.l) << " "; + ss << static_cast(expr) << " "; + ss << this->processWhere(expr.r); + + return ss.str(); + } + + template + std::string processWhere(condition::Or& expr) { + std::stringstream ss; + ss << this->processWhere(expr.l) << " "; + ss << static_cast(expr) << " "; + ss << this->processWhere(expr.r); + + return ss.str(); + } + + template + std::string processWhere(Expr expr) { + std::stringstream ss; + ss << this->impl.getColumnName(expr.l); + ss << " = ?"; + + return ss.str(); + } + std::string name; ImplType impl; @@ -114,10 +144,8 @@ template template Table Table::where(Expr expr) { - auto name = this->impl.getColumnName(expr.l); - std::stringstream ss; - ss << "WHERE " << name << " " << std::string(expr) << " ?"; + ss << "WHERE " << this->processWhere(expr); this->cache.emplace_back(ss.str()); diff --git a/include/klay/db/query-builder/test-main.cpp b/include/klay/db/query-builder/test-main.cpp index 1741ec4..1b93640 100644 --- a/include/klay/db/query-builder/test-main.cpp +++ b/include/klay/db/query-builder/test-main.cpp @@ -25,11 +25,15 @@ int main() { std::string select2 = admin.select(&Admin::id, &Admin::uid, &Admin::key); std::string select3 = admin.select(&Admin::uid, &Admin::key).where(expr(&Admin::id) > 3); std::string select4 = admin.selectAll().where(expr(&Admin::uid) > 3); + std::string select5 = admin.selectAll().where(expr(&Admin::uid) > 3 && expr(&Admin::pkg) == "dpm"); + std::string select6 = admin.selectAll().where(expr(&Admin::uid) > 3 || expr(&Admin::pkg) == "dpm"); std::cout << select1 << '\n'; // SELECT id pkg uid key FROM admin std::cout << select2 << '\n'; // SELECT id uid key FROM admin std::cout << select3 << '\n'; // SELECT uid key FROM admin WHERE id = ? std::cout << select4 << '\n'; // SELECT * FROM admin WHERE uid = ? + std::cout << select5 << '\n'; // SELECT * FROM admin WHERE uid = ? AND pkg = ? + std::cout << select6 << '\n'; // SELECT * FROM admin WHERE uid = ? OR pkg = ? return 0; }